Repository navigation
Track scope for CTEs to fix ambiguous column bug - #1
Merged
Merged
Conversation
mafredri
force-pushed
the
mafredri/fix-ambiguous-column
branch
5 times, most recently
from
October 29, 2025 12:09
5f67321 to
7c1a82b
Compare
mafredri
commented
Oct 29, 2025
| ) | ||
| SELECT * | ||
| FROM wtask | ||
| ORDER BY CASE WHEN w_owner_id = @owner_id THEN 0 ELSE 1 END; |
Member
Author
There was a problem hiding this comment.
Review: Why didn't I just use this workaround?
Because the CTE wasn't indexed and as such, unknown column w_owner_id.
mafredri
added a commit
to coder/coder
that referenced
this pull request
Oct 29, 2025
Refs coder/sqlc#1 Unblocks #20501 Upstream sqlc-dev/sqlc#4159
mafredri
added a commit
to coder/coder
that referenced
this pull request
Oct 29, 2025
Refs coder/sqlc#1 Unblocks #20501 Upstream sqlc-dev/sqlc#4159
mafredri
added a commit
to coder/coder
that referenced
this pull request
Oct 29, 2025
mafredri
force-pushed
the
mafredri/fix-ambiguous-column
branch
from
November 13, 2026 17:10
7c1a82b to
7a30c2e
Compare
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.
sqlc does not track scope for CTEs. A subquery inside a CTE makes column references in the CTE, and in CTEs that select from it, resolve against the subquery's tables too, so valid queries fail to generate:
A minimal shape that triggers it:
This change tracks the CTE a column reference belongs to when resolving columns and parameters. The new
cte_ambiguous_columntestdata reproduces the errors above. Parameters inside a CTE, or compared against a CTE column, now get the referenced type instead of a nullable fallback:cte_left_joingeneratesname string(wasdollar_1 pgtype.Text), andcte_updategeneratesbool,string,int64(waspgtype.Bool,pgtype.Text,pgtype.Int8).mainnever had this fix. The fork was pinned to the side branchmafredri/fix-ambiguous-column.import, and #6 merged upstream v1.31.1 intomainwithout it. This branch is the fix applied tomain(337309bf), with the new testdata regenerated for the v1.31.1 version stamp.go test ./...against PostgreSQL 16 fails the same tests onmainand on this branch:TestReplay/{base,managed-db}/wasm_plugin_sqlc_gen_unsafe_paths. Its expected error path depends on where the repository is checked out.Upstream: sqlc-dev#4159