Skip to content

Track scope for CTEs to fix ambiguous column bug - #4159

Open
mafredri wants to merge 5 commits into
sqlc-dev:mainfrom
coder:mafredri/fix-ambiguous-column
Open

mafredri wants to merge 5 commits into
sqlc-dev:mainfrom
coder:mafredri/fix-ambiguous-column

Conversation

@mafredri

@mafredri mafredri commented Oct 29, 2025 •

Copy link
Copy Markdown

EDIT: Sorry, I did not intend to open this PR over here yet, it was meant for coder/sqlc. But since it's here already. I'll keep it open.


While adding a simple EXISTS condition, I ran into an sqlc bug because it wasn't tracking scope for CTEs.

query.sql:11:20: column reference "owner_id" is ambiguous
query.sql:24:20: column "w_owner_id" does not exist
query.sql:38:20: column reference "owner_id" is ambiguous

The query could look something like this:

WITH cte AS (
	SELECT owner_id
	FROM table_with_owner_id
	WHERE EXISTS (SELECT 1 FROM another_table_with_owner_id)
)
SELECT * FROM cte;

Test queries have been added that reproduce the bug.

Some of the existing testdata also had to be updated because the tracking got improved.


This bug was initially solved via Coder Tasks (Claude Code), but the solution was messy and followed the "throw code at it until it works", as such it has been mostly rewritten.

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang labels Oct 29, 2025
@mafredri
mafredri force-pushed the mafredri/fix-ambiguous-column branch 5 times, most recently from 5f67321 to 7c1a82b Compare October 29, 2025 12:09
mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2025
mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2025
ethanndickson and others added 4 commits May 8, 2026 07:28
This pulls in 78 upstream commits since coder/sqlc's prior fork point
(b807fe9), most importantly:

* sqlc-dev#4390: strip \restrict / \unrestrict psql meta-commands
  from schema files (fixes sqlc-dev#4065 / coder/internal#965).
* sqlc-dev#4378: upgrade Go toolchain to 1.26.2.
* Various dependency bumps and parser improvements.

Conflict resolution:

* All content conflicts were import-path collisions — our renamed
  imports (github.com/coder/sqlc) versus upstream's additions of new
  sqlc-dev/sqlc imports. Resolved by taking upstream's side, then
  re-applying the global rename across .go and go.mod files (including
  newly-added upstream files that auto-merged).
* internal/sqltest/pgx.go: accepted upstream deletion (removed in
  sqlc-dev#4379, 'Remove github.com/jackc/pgx/v4 dependency').
@mafredri
mafredri force-pushed the mafredri/fix-ambiguous-column branch from 7c1a82b to 7a30c2e Compare November 13, 2026 17:10
mafredri added a commit to coder/sqlc that referenced this pull request Nov 13, 2026
A subquery inside a CTE made sqlc resolve column references in that
CTE, and in CTEs selecting from it, against the subquery's tables too.
Valid queries then failed to generate with false positives such as
`column reference "owner_id" is ambiguous` or `column "w_owner_id" does
not exist`. Track the CTE each reference belongs to when resolving
columns and parameters.

Parameters inside a CTE, or compared against a CTE column, now get the
referenced type instead of a nullable fallback: cte_left_join and
cte_update generate string, bool and int64 where they generated
pgtype.Text, pgtype.Bool and pgtype.Int8.

The fix previously lived only on the mafredri/fix-ambiguous-column.import
branch that coder/coder pinned. It never reached main, so coder/coder
lost it when it moved its pin to main's merge of upstream v1.31.1 (#6).
The upstream PR, sqlc-dev#4159, is still open.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants