Repository navigation
compiler: NULLIF is nullable and takes the type of its first argument - #4733
Open
NotTheOrange21 wants to merge 1 commit into
Open
NotTheOrange21 wants to merge 1 commit into
NotTheOrange21 wants to merge 1 commit into
Conversation
NULLIF(a, b) parses as an A_Expr with the "=" operator, so outputColumns took the comparison branch: the column got the boolean type of a comparison and was marked non-null. That is wrong twice over. NULLIF yields a's value, so the column has a's type, and it yields NULL when the two arguments are equal, so the column is always nullable. Repro (from sqlc-dev#4695): CREATE TABLE depots (id BIGSERIAL PRIMARY KEY, name text NOT NULL); -- name: ListNames :many SELECT d.id, nullif(d.name, 'unnamed') AS name FROM depots d; Before, the row struct held a bool and scanning a row whose name is "unnamed" failed. After, it holds the nullable form of text. The first argument decides the type, so handle the shapes this path can infer: a column reference, a type cast, a function call the catalog knows, a mathematical expression, and a literal. Anything else stays untyped rather than claiming to be a boolean. Co-authored-by: James Hiscock <james@jameshiscock.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & Why
NULLIF(a, b)parses as anA_Exprwhose operator is=, sooutputColumnstook the comparison branch and gave the column the boolean type of a comparison, marked non-null. Both are wrong:NULLIFyieldsa's value, so the column hasa's type, and it yieldsNULLwhen the arguments are equal, so it is always nullable.Repro from #4695 (PostgreSQL, pgx/v5):
Before this change the generated row struct held a
bool, so scanning a row whosenameis'unnamed'failed at runtime. After it, the field ispgtype.Text(orsql.NullStringwithdatabase/sql), which is what the query actually returns.How
internal/compiler/output_columns.gonow handlesast.A_Expr_Kind_NULLIFbefore the operator switch and types the column from the first argument, always nullable. The shapes this analysis path can infer are covered: a column reference, a type cast, a function call the catalog knows, a mathematical expression, and a literal. Anything else staysanyrather than claiming to be a boolean — that is the pre-existing behaviour for expressions this path cannot type.The second argument is ignored for typing, which matches PostgreSQL:
NULLIF(a, b)isa's type, and the arguments are compared after coercion.Tests
internal/endtoend/testdata/null_if_type/pgx/v5— new case covering the NULLIF is generated as never null #4695 repro; the generated field ispgtype.Text.internal/endtoend/testdata/null_if_type/postgresql/stdlib— golden updated frombooltosql.NullInt64(NULLIF(id, $1)).internal/endtoend/testdata/null_if_type/postgresql/pganalyzer— same, for the managed-database context.go test ./...passes. Themanaged-dbcontext is skipped here (no PostgreSQL/MySQL available locally), so that golden is updated by hand to match what the analyzer now reports.Thanks to @Bockit for the diagnosis and the first patch in #4695.
Closes #4695