Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

compiler: NULLIF is nullable and takes the type of its first argument - #4733

Open
NotTheOrange21 wants to merge 1 commit into
sqlc-dev:mainfrom
NotTheOrange21:fix/nullif-is-nullable
Open

NotTheOrange21 wants to merge 1 commit into
sqlc-dev:mainfrom
NotTheOrange21:fix/nullif-is-nullable

Conversation

@NotTheOrange21

Copy link
Copy Markdown

What & Why

NULLIF(a, b) parses as an A_Expr whose operator is =, so outputColumns took the comparison branch and gave the column the boolean type of a comparison, marked non-null. Both are wrong: NULLIF yields a's value, so the column has a's type, and it yields NULL when the arguments are equal, so it is always nullable.

Repro from #4695 (PostgreSQL, pgx/v5):

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 this change the generated row struct held a bool, so scanning a row whose name is 'unnamed' failed at runtime. After it, the field is pgtype.Text (or sql.NullString with database/sql), which is what the query actually returns.

How

internal/compiler/output_columns.go now handles ast.A_Expr_Kind_NULLIF before 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 stays any rather 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) is a'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 is pgtype.Text.
  • internal/endtoend/testdata/null_if_type/postgresql/stdlib — golden updated from bool to sql.NullInt64 (NULLIF(id, $1)).
  • internal/endtoend/testdata/null_if_type/postgresql/pganalyzer — same, for the managed-database context.
  • go test ./... passes. The managed-db context 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

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NULLIF is generated as never null

1 participant