fix(from): model JOIN ... USING as a merged column - #347
Merged
Conversation
StrictQuery<DB, 'select id from a join b using (id)'>
// was QueryTypeError<'ambiguous column: id'>[]
USING was not modeled at all. AliasOf special-cased only `on` and `as`, so
the joined table's alias was literally set to the word `using`, and
CountBareMatches counted the column once per source with no merge exemption.
But USING produces one column, not two, in every dialect that has it, and
referencing it bare is the entire point of the syntax.
A source now carries the columns its join merged, and its copy of one of
them does not count towards ambiguity. Two things stay exactly as they were:
a column outside the USING list is still ambiguous across the same join, and
an ON join reports ambiguity as before.
`using(id)` glued to its paren is left as it was - the spaced form is what
this reads - so that case errs on the side of the report it already gave
rather than a silent merge.
Fixes #284
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
tiagolauer
force-pushed
the
fix/284-join-using-merged
branch
from
August 2, 2026 13:04
493c138 to
2703c19
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.
Fixes #284.
The bug
Expected
{ id: number }[]. USING merges the join column in every dialect that has it; referencing it bare is the whole point of the syntax.The fix
USING was not modeled at all:
AliasOfonly special-casedonandas, so the joined table's alias was set to the literal wordusing— which also brokeselect b.y from a join b using (id).CountBareMatchescounted the column once per source, with no merge exemption.A
Sourcenow carries the columns its join merged (mergedColumns, alongside the existing optionalderivedQuery), and a source's copy of one of those columns does not count towards ambiguity. The type still resolves from the first matching source, so nothing about the value changes.using(id)glued to its paren is deliberately left alone — only the spaced form is read — so that case keeps the report it already gave rather than silently merging.Verification
tests/join-using-merged.test-d.ts, nine cases. Four red on master:b.y), which theusingalias used to breakControls, all still reporting: an ON join is still
ambiguous column: id, a column outside the USING list is still ambiguous, an unknown column is still unknown, and loose mode is unchanged.tsc --noEmitclean, 192 core + 155 plugin runtime tests pass, budget 194,721 (+2,235).