Skip to content

fix: resolver fails to look up CTE columns#4430

Open
msaher wants to merge 1 commit intosqlc-dev:mainfrom
msaher:fix/4288
Open

fix: resolver fails to look up CTE columns#4430
msaher wants to merge 1 commit intosqlc-dev:mainfrom
msaher:fix/4288

Conversation

@msaher
Copy link
Copy Markdown

@msaher msaher commented May 8, 2026

Closes #4288

The resolver fails to infer the type of a CTE column because it only searches normal tables. My solution is to have two passes, one where we search the CTE columns first (making them shadow regular tables) then if not found, we search through regular tables.

We cannot simply add the CTE column in the existing typeMap because a CTE and a regular table will have the same column name which will trigger the found > 1 check and the column will be considered ambiguous. So we use a different type map for CTEs so we can separate the search

I have noticed three other cases where only regular tables are searched. They are ast.BetweenExpr, ast.ResTarget, and ast.In. I'm not sure if they should be changed so I left them as they are and they're not failing any tests, but I added a comment about them in case we need to update them.

There is some duplication when populating the CTEs and searching through them. I have chosen not to create a helper function for searching as that seems to be the style. However, I'm happy to create one if the maintainers think its needed.

@msaher msaher marked this pull request as draft May 8, 2026 07:48
@msaher msaher force-pushed the fix/4288 branch 5 times, most recently from a1eb004 to cf988cd Compare May 8, 2026 10:48
Closes sqlc-dev#4228.

The resolver fails to infer the type of a CTE column because it only
searches normal tables. My solution is to have two passes, one where
we search the CTE columns first (making them shadow regular tables) then
if not found, we search through regular tables.

We cannot simply add the CTE column in the existing `typeMap` because a
CTE and a regular table will have the same column name which will
trigger the `found > 1` check and the column will be considered
ambiguous. So we use a different typeMap for CTEs so we can separate the
search
Comment on lines -31 to +30
func (q *Queries) BadQuery(ctx context.Context, dollar_1 pgtype.Text) error {
_, err := q.db.Exec(ctx, badQuery, dollar_1)
func (q *Queries) BadQuery(ctx context.Context, name string) error {
_, err := q.db.Exec(ctx, badQuery, name)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change affects the existing cte_left_join test case. It now uses native Go types

@msaher msaher marked this pull request as ready for review May 8, 2026 11:06
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.

sqlc generate failed:column "new_streak" does not exist

1 participant