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

fix(compiler): infer VALUES columns and apply subquery column aliases - #4735

Open
m3lonxd wants to merge 1 commit into
sqlc-dev:mainfrom
m3lonxd:fix/values-subselect-columns-4198
Open

m3lonxd wants to merge 1 commit into
sqlc-dev:mainfrom
m3lonxd:fix/values-subselect-columns-4198

Conversation

@m3lonxd

@m3lonxd m3lonxd commented Dec 5, 2026

Copy link
Copy Markdown

Fixes #4198

Summary

Queries selecting from a VALUES subquery or CTE with column aliases (for example, SELECT * FROM (VALUES ('SAVED'), ('IDLE')) AS pending_sale_status(status) inside a CTE) failed during compilation or star expansion with two separate gaps:

  1. Missing output-column inference for VALUES lists:

    • In the compiler (internal/compiler/output_columns.go), outputColumns handled SelectStmt.TargetList and UNION branches (Larg), but fell through with an empty column slice when SelectStmt.ValuesLists was populated.
    • In the core analyzer (internal/core/analyzer/analyzer.go), typeValuesLists type-checked the cell expressions without populating a.columns (column1, column2, ...).
    • Both now infer default column1..columnN columns across all rows in ValuesLists, widening NULL cells into nullable types when another row supplies a concrete type.
  2. Subquery column aliases ignored on RangeSubselect in sourceTables:

    • sourceTables in internal/compiler/output_columns.go read n.Alias.Aliasname for the relation name (without guarding nil Aliasname), and ignored n.Alias.Colnames (e.g. AS pending_sale_status(status) or AS renamed(item_id, item_name)).
    • It now clones column metadata and applies non-empty column aliases from n.Alias.Colnames (matching bindRangeSubselect in the core analyzer).

Changes

  • internal/compiler/output_columns.go: Add valuesColumns for SelectStmt.ValuesLists and apply RangeSubselect.Alias.Colnames in sourceTables.
  • internal/core/analyzer/analyzer.go: Populate a.columns (column1..columnN, merged types/nullability) inside typeValuesLists.
  • internal/endtoend/testdata/cte_values/: Add end-to-end PostgreSQL (pgx/v5) golden test covering the #4198 VALUES CTE, mixed-type/nullable multi-row VALUES, direct WITH cte(a, b) AS (VALUES ...) aliasing, default column1/column2 naming, and RangeSubselect column aliases.

Verification

Run locally with Go 1.26.5 after the final edit:

  • gofmt -l internal/compiler/output_columns.go internal/core/analyzer/analyzer.go — clean
  • go vet ./... — passed
  • CGO_ENABLED=0 go build ./... — passed
  • go test -race -count=1 ./internal/compiler/... ./internal/core/... — passed
  • SQLC_TEST_CORE=1 go test -count=1 -v ./internal/endtoend -run 'TestReplay/.*/cte_values' — passed across base, managed-db (live PostgreSQL), and core contexts
  • go test -count=1 ./internal/endtoend -run 'TestReplay/base|TestFormat|TestJsonSchema' — passed
  • cd internal/endtoend/testdata && go build ./cte_values/... — passed

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.

Inferring columns from some CTE's is invalid

1 participant