fix: stop claiming a table for columns that resolve to none - #23
Merged
Merged
Conversation
`ColumnOrigin::Concrete { table, column }` is a claim that the output
column really derives from `table.column`. The resolver emitted that
claim in two places where it had proven nothing, putting an invented name
in the table slot:
SELECT bare_col
bare_col <- Concrete { table: "?unknown?", column: "bare_col" }
WITH cte AS (SELECT present FROM source) SELECT missing FROM cte
missing <- Concrete { table: "?cte?", column: "missing" }
Neither name can appear in `tables.inputs`, so the column graph
referenced relations the table graph denied existed, and a consumer had
no way to separate proven lineage from a guess short of matching those
sentinel strings.
A third site already reported the same state differently: when every
binding is a CTE or derived table and none has the column, no physical
relation is left to attribute it to, and the result was `Ambiguous` with
an empty candidate list.
All three now produce `ColumnOrigin::Unresolved { column }`. That leaves
each remaining variant with one meaning: `Concrete` is proven, `Ambiguous`
is a real choice between at least two known relations, `Unresolved` is
neither. Both invariants are documented.
`apply_catalog` needs no change. It refines `Ambiguous`, and `Unresolved`
is not `Ambiguous`, so a provider that resolves a column by name alone
cannot turn an unresolved origin back into a fabricated concrete one --
the bug is unrepresentable rather than guarded against.
`ColumnOrigin` is deliberately left exhaustive. It encodes how much was
proven; a consumer that silently ignores a new resolution state is the
failure it exists to prevent, so a new variant should break their build.
This matches the rule ARCHITECTURE.md already states for sqlparser AST
variants, and is why the CLI and the PyO3 bridge both had to be updated
here rather than falling through a wildcard arm.
Known limitation, pinned by a test: a column hidden behind an unexpanded
`SELECT *` inside a CTE or derived table also reads as `Unresolved`,
although the relation is known and the column very likely exists. Naming
it needs an origin that can carry a relation.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This was referenced Sep 21, 2026
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.
Reconstructed from the second commit of #12 (
e8e4790), one of the contributions in #15. Same defect, different fix — see Differences from #12.Defect
ColumnOrigin::Concrete { table, column }is a claim that the output column really derives fromtable.column. The resolver emitted it in two places where it had proven nothing, putting an invented name in the table slot:Neither name can appear in
tables.inputs, so the column graph referenced relations the table graph denied existed. A consumer could not separate proven lineage from a guess without matching those sentinel strings.A third site already produced the same state under a different spelling: when every binding is a CTE or derived table and none has the column, no physical relation is left to attribute it to, and the result was
Ambiguouswith an empty candidate list.Fix
All three sites now produce
ColumnOrigin::Unresolved { column }, leaving each variant with exactly one meaning:ConcreteAmbiguousUnresolvedBoth invariants are documented on the type and on
CatalogProvider::resolve_column.apply_catalogneeds no change. It refinesAmbiguous, andUnresolvedis notAmbiguous, so a provider that resolves a column by name alone cannot turn an unresolved origin back into a fabricated concrete one. The bug is unrepresentable rather than guarded against.Differences from #12
#12 reused
Ambiguous { candidates: [] }for the two fabrication sites.Ambiguous's doc had to grow a second meaning ("non-empty means genuine ambiguity, empty means unresolved"), and every consumer asking "is this proven?" had to writematches!(o, Ambiguous { candidates, .. } if candidates.is_empty()). This matters per-source, not per-mapping — a set operation mixes both in one mapping:apply_catalogguard. fix: two column lineage correctness bugs (nondeterministic ordering, fabricated origins) #12 had to add&& !candidates.is_empty(), described there as load-bearing. It is load-bearing — but only because the two meanings shared a variant. With a distinct variant the guard has nothing to guard.Ambiguousmean one thing.#[non_exhaustive].ColumnOriginencodes how much was proven. A consumer that silently ignores a new resolution state is the failure the enum exists to prevent, so a new variant should break their build — the same rule ARCHITECTURE.md already states for sqlparser AST variants. This is why the CLI and the PyO3 bridge had to be updated here instead of falling through a wildcard arm. (Note: feat: expose lineage uncertainty and expand dialects #16 marksColumnOriginnon-exhaustive; that decision should be revisited when feat: expose lineage uncertainty and expand dialects #16 is reconstructed.)Dialectis#[non_exhaustive](feat: support every dialect sqlparser exposes #18). The asymmetry is deliberate:Dialectis an input consumers construct,ColumnOriginis an output they branch on.Surfaces
CLI now distinguishes the two states it previously merged:
Python gains
kind == "unresolved"withcolumnset (table,candidates,base_sourcesareNone); verified against a built wheel.Known limitation, pinned by a test
A column hidden behind an unexpanded
SELECT *inside a CTE or derived table also reads asUnresolved:The relation is known and
pvery likely exists — the star just was not expanded, and a catalog listingt's columns does not help here either. Honest but coarse. Naming it needs an origin that can carry a relation (#16'sNamedWildcard), socolumn_behind_an_unexpanded_star_is_unresolvedpins today's behavior to make that change visible when it lands.Tests
6 new, 99 total. The three sites, the mixed-source case, the star limitation, and both sides of the catalog boundary (an unresolved column stays unresolved; genuine ambiguity is still resolved).
Compatibility
Breaking: new
ColumnOriginvariant, and previously-Concreteorigins for unresolved columns change shape. 0.3.0, alongside theDialectvariants from #18.🤖 Generated with Claude Code