GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — fix(postgresql): set query result column nullable if selected column contains json operator or null value in case expression by swallowstalker · Pull Request #3739 · sqlc-dev/sqlc Conversation
|
|
Can we add this test case as well please? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Where in your tests are we setting the expectation of the generated results? Or is the committed code the expectation? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
GetNullable3B added
In my PR description I proposed type precedence, for multiple type inside case expression. For example
and so on. but on the other side, I think that the user should be given freedom if the return value of case expr has several type variance (in short, just return |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I have generated the result using code from this PR, and then make it as expectation (commited on repo), but to be honest I have a problem in github workflow tests, it seems that the binary that the workflow use is the previous version before I made these changes, so the generated go file would always be different than the one I commited on the repo. Especially on |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…s printed selected columns without parentheses, it will generate different type on from query.sql
dcf4684 to
e7ddabf
Compare
|
So after thinking about it, I realized we really only care about what Postgres thinks the returned values are, not what we think they should be. For example, if you insert at least 4 rows into SELECT CASE WHEN id = 1 THEN '1' WHEN id = 2 THEN null WHEN id = 3 THEN id WHEN id = 4 THEN 4.5 ELSE '10' END FROM "mytable"; Returns: The strings for But if you change the query to this: SELECT CASE WHEN id = 1 THEN '1'::text WHEN id = 2 THEN null WHEN id = 3 THEN id WHEN id = 4 THEN 4.5 ELSE '10' END FROM "mytable"; You get an error: or this: SELECT CASE WHEN id = 1 THEN 'foobar' WHEN id = 2 THEN null WHEN id = 3 THEN id WHEN id = 4 THEN 4.5 ELSE '10' END FROM "mytable"; You get a similar error: So to sum up: I think you are on the right track, with forcing these types of queries into a type rather than an any/interface{} I don't know how realistic these are in the real world. Perhaps we should try to find queries that hit an actual table of data, rather than case statements of literals. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I agree that it's not quite realistic, I better left it to sqlc user to decide which data type that will be used in case of multiple data type exist on case-when-then-else. In short, I'll make it interface then |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
# Conflicts: # go.sum
… user more freedom to choose query column type
| col = &Column{Name: name, DataType: "float", NotNull: true} | ||
| case *ast.Boolean: | ||
| col = &Column{Name: name, DataType: "bool", NotNull: true} | ||
| case *ast.Null: |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
this is new addition to handle null const value. I need to distinguish between any and null, because null in CaseExpr would determine query column result nullability, but does not make it included in column type decision.
for ast.A_Const condition, i have converted DataType null back to any (line 136, outputColumns function) because we don't know its type and to preserve backward compatibility
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
| } else { | ||
| cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false}) | ||
| defaultCol = &Column{Name: name, DataType: "null", NotNull: false} |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
missing ELSE clause on CaseExpr is considered to return null value
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Based on the tests, I believe this fixes the issue for me. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
from my previous PR, I mentioned @kyleconroy to ask for review. would you mind checking it @kyleconroy? sorry i still can't assign reviewer for this PR |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
bumping this PR, is there anything more i can do for this PR? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Tried this on current create table books (id int primary key, title text not null); create table shelves (id int primary key, label text not null, book_id int); -- name: Shelf :many select case when b.id > 0 then s.label else null::text end shelf from books b left join shelves s on s.book_id = b.id; With this PR, Scalar subqueries (#4262) aren't covered here; I'll propose that fix separately. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.