sql: make zero-column relation aliases visible to name resolution#37646
Draft
ggevay wants to merge 1 commit into
Draft
sql: make zero-column relation aliases visible to name resolution#37646ggevay wants to merge 1 commit into
ggevay wants to merge 1 commit into
Conversation
A Scope tracked relation names only through the table_name of its column items, so a relation exposing zero columns, e.g. (SELECT) AS q, left no trace in the scope. Such an alias did not shadow same-named aliases in outer scopes, so q.a silently bound to an outer q instead of erroring, and it escaped duplicate table name detection, so FROM t, (SELECT) AS t was accepted. Both diverge from PostgreSQL, where range table entries exist independent of their columns. Track the names of zero-arity relations in a dedicated Scope field, maintained by scope construction, product, project, and aliasing. Name resolution refuses to match items beyond the closest zero-arity relation with the referenced table name, duplicate detection includes the tracked names, and qualified wildcards on zero-column relations now expand to zero columns as in PostgreSQL. Fixes SQL-313. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Fixes SQL-313
Motivation
A relation alias that exposes zero columns, e.g.
(SELECT) AS q, a zero-column CTE, view, or table, was invisible to name resolution.Scopetracked relation names only through thetable_nameof its column items, so a column-less relation left no trace in the scope. This diverged from PostgreSQL, where range table entries exist independent of their columns, in two user-visible ways:q.ainside a subquery silently bound to an outerqinstead of erroring.SELECT * FROM t, (SELECT) AS twere accepted instead of erroring withtable name "t" specified more than once.Description
User-facing change: zero-column relation aliases now shadow outer aliases of the same name and participate in duplicate-alias detection, matching PostgreSQL. Queries that previously bound a qualified column reference to an outer alias of the same name now fail with
column "q.a" does not exist, and duplicate zero-column aliases (in comma joins, explicitJOIN,CROSS JOIN,NATURAL JOIN,USINGjoin aliases, andDELETE ... USING) are now rejected withtable name "t" specified more than once. Qualified wildcards on zero-column relations (SELECT q.* FROM (SELECT) AS q) now expand to zero columns instead of erroring, also matching PostgreSQL.Implementation:
Scopegains azero_arity_table_namesfield, a lightweight analogue of PostgreSQL's column-less range table entries. It is maintained by scope construction (from_source),product,project, andplan_table_alias, cleared where non-lateral subqueries make outer relations invisible, and carried into the group scope so shadowing survivesGROUP BY. Qualified column resolution refuses to match items from lateral levels farther out than the closest zero-arity relation with the referenced name, andScope::table_namesincludes the tracked names so duplicate detection sees them. The design degrades gracefully: where the new field is not propagated, behavior is exactly the old behavior.Known deliberate gap: a whole-row reference to a zero-column relation (
SELECT q FROM (SELECT) AS q) still errors withcolumn "q" does not exist, where PostgreSQL returns an empty record(). Supporting that requires zero-field record support and is a different root cause.Verification
LATERAL, non-lateral sibling visibility, grouping, wildcard expansion, duplicate detection in all join forms).test/sqllogictest/scoping.sltcovering the original repro, all duplicate-alias variants, CTEs, zero-column views and tables,LATERAL,GROUP BY, outer joins with a zero-arity side, qualified wildcards, andDELETE ... USING.cargo testformz-sqlandmz-transform. The only non-passes (CockroachDBINDEXsyntax bails, duplicateUSINGcolumn "unsupported") were confirmed pre-existing on main.🤖 Generated with Claude Code