Repository navigation
Fix unqualified columns of joined subqueries in expressions to close #832 - #2579
Open
joshuablac wants to merge 2 commits into
Open
joshuablac wants to merge 2 commits into
joshuablac wants to merge 2 commits into
Conversation
…laSQL#832 compileDefCols only registered columns of tables, so a column coming from a subquery in FROM or JOIN was unknown when compiling an expression. In SELECT c - b FROM (SELECT a,b FROM one) t0 JOIN (SELECT a,c FROM two) t1 ... "c" was looked up in the first source and the result was undefined. The named columns of a subquery's SELECT list are now registered for its alias. They only fill names that are not defined yet, so table columns and the existing ambiguity rules are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With a schemaless source (FROM ?, CSV(), a table without column definitions, SELECT *) an unqualified name may belong to that source. Registering the subquery's columns changed which source such a name resolved to, e.g. x in FROM ? t0 JOIN (SELECT id, x FROM ?) t1 moved from t0 to t1. Keep the previous resolution in that case. Co-Authored-By: Claude Opus 5.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.
Closes #832
Problem
An unqualified column that comes from a subquery in
FROMorJOINresolves to the wrong source when it is used inside an expression:develop[{a:1, bc:undefined}, {a:2, bc:undefined}][{a:1, bc:90}, {a:2, bc:180}]one t0 JOIN (SELECT a,c FROM two) t1 ... c - bbc: undefinedbc: 90 / 180... JOIN (SELECT a, c * 2 AS dbl FROM two) t1 ... WHERE dbl + b > 300Column does not exist: dbl[{a:2, dbl:400}]Plain column references like
SELECT c, balready worked, because the SELECT list searches all sources at runtime. Expressions (c - b,WHERE,ORDER BY) rely ondefcolsinstead.Cause
compileDefColsregisters the columns of tables inFROM/JOIN, but does nothing for subquery sources (fr instanceof yy.Select,jn.select). An unqualified column from a subquery therefore isn't indefcols. It is compiled as a lookup on the default (first) source:p['t0']['c'], which isundefined. When the first source is a table, the result is theColumn does not existerror.Fix
After the existing table handling,
compileDefColsregisters the named columns of each subquery's SELECT list (col.as, or the column name) for the subquery's alias. Two limits keep today's results intact:*. A schemaless source (FROM ?,CSV(), a table created without columns,SELECT *) could also hold an unqualified name, so the previous resolution is kept. For example, inFROM ? t0 JOIN (SELECT id, x FROM ?) t1,xstill resolves tot0. This meansFROM ? JOIN (subquery)isn't fixed by this PR.'-') rules, and a name two sources share keeps today's resolution. No query that works today starts throwing an "ambiguous column" error.Tests
test/test832.js:WHEREandORDER BYFROM ?,SELECT *subquery) keeps resolving to the first sourcedevelopand pass with this change.develop:ashared by two subqueries, by a subquery and a table, byFROM ?and a subquery, and by a column-less table and a subquery.bun test, same command asyarn test-only): 2254 pass, 213 skip. The only failures are the 2 CLI tests intest2149.js, which fail the same way on unmodifieddevelopin my local setup (a space in my checkout path, and the version-injection step ofbuild.shnot running).This fix was written with AI assistance (Claude Code) and verified locally as described above.
🤖 Generated with Claude Code