Repository navigation
Refactor: Work out the binding of each column reference beside the lookup - #3108
Merged
Merged
Conversation
This was referenced Nov 22, 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.
Part of #2723, and only its next step: the binding of a column reference is worked out, beside today's lookup, and used nowhere. It does not close the issue.
What changes
src/engine/bind.jsgets the column part.bind.scope(select, databaseid, outer)makes the scope of one SELECT: its sources in the order of FROM and JOIN, each with its column definitions when the source has them, and a link to the scope of the SELECT around it.Select.compileenters its scope before anything else and leaves it at the end, so a subquery, which is compiled inside the compile of its parent, gets the parent's scope as its outer one.bind.column(col, scope)says what aColumnrefers to:{scope, source, column}, or{scope, any: [...]}when the source is decided per row, or{scope, ambiguous: [...]}, or{unresolved: true}.scopeis 0 for the SELECT itself and 1 for the one around it.Its rules are the rules of 4.x where 4.x made a choice.
source.columnis that source, in the nearest scope that has it.Today's lookup is in two places, and the binding is asked beside both, but only when
alasql.yy.Column.onBindis set. Nothing sets it outside tests, so a query does not callbind.columnat all. It pays for the scope stack inSelect.compileand for nothing else, and a mistake inbind.columncan not reach a query.Column.toJS(src/expression/expression.js). Its body is nowlookupJS, unchanged. WithoutonBind,toJScallslookupJSand nothing more. With it,toJScallsbind.columnand thenlookupJS, and hands both toonBind.compileSelect1(src/select/columns.js), which has its own lookup for a column that stands alone.An error thrown by
bind.columnin the test mode is caught and handed toonBindas{error}, so it can not change what a test sees. Nothing is kept on theColumnnode, and no generated code changes.src/select/having.jsis untouched, and so is the HAVING alias rule (#2878).The test mode
When
alasql.yy.Column.onBindis set, it gets the binding and the code of today's lookup for each reference.yarn test-bind(test/bind/) runs the suite with it, reads today's answer from the code the lookup wrote, and counts the outcomes.test/bind/README.mdsays how.The check runs in CI as the job "Binding check of column references", built like the run-state check. It fails when
bind.columnthrows, when a difference is not one of the six cases below, when no reference was compared, or when the suite run crashes. The six known cases do not fail it.knownCaseintest/bind/record.jsholds one rule per case. It sees the reference, its scope and the SQL text, and reads the alias of 1b and the function call of 2a from the text. I tried both failing paths by hand, with abind.columnthat throws for one column name and with a statement that is a new difference, and the exit code was 1 both times.On this branch the suite compiles 5,559 column references inside a SELECT.
The 114 are a column read from a group (
g['a'], 60), the row itself as_or a SELECT whose row is the context (p['a'], 53), and one APPLY argument read from the outer scope.Every case where they differ
The 34 references are three kinds of difference, in six cases. None is fixed here. The counts per case are now the ones the check prints. The first version of this text had the split inside kind 1 and kind 2 wrong (17 and 1, and 6, 1 and 7), while the totals of 18, 14 and 2 were right.
1. A bare name that the only table does not define (18)
Today's lookup takes the default table without looking at its columns. The binding finds no source, in this scope or outside.
SELECT CASE a+1 WHEN b THEN 111 WHEN c THEN 222 WHEN d THEN 333 WHEN e THEN 444 ELSE 555 END FROM t1 ORDER BY 1, wheret1has the columnsxandy(test258.js). The other statement isSELECT (SELECT count(*) FROM t1 AS x WHERE x.b<t1.b) FROM t1 WHERE (a>b-2 AND a<b+2) OR c>d ORDER BY 1t1, and the value isundefinedin every rowSELECT a, COUNT(*) AS n FROM t GROUP BY a HAVING SUM(n) > 0, wherethasa,b,cnt(test887.js)t1a is tracked by #3109 (milestone v5.0.0), which decides whether an unknown column stays
undefinedor becomes an error. Until then 4.x keeps readingundefined. 1b is tracked by #2878, the alias rule of HAVING, and is left alone here.2. A bare name that two tables define (14)
bind.columnsays ambiguous. Today's lookup throws for this in a plain expression, but not in these three places, where it takes the first table.SELECT UPPER(w) AS u, LENGTH(w) AS l FROM two t1 JOIN three t2 ON t1.a = t2.aandSELECT t2.a FROM two t1 JOIN three t2 ON t1.a = t2.a WHERE TRIM(w) = 'yy'(test2615.js),SELECT r.name AS n FROM p, r WHERE p.id = r.id AND UPPER(name) = "BOB"(test2917.js)SELECT ta.id, x, z FROM ta JOIN tb ON ta.id = tb.id, where both tables havex(test2873.js)ta)SELECT SUM(b) AS sb,a,c FROM test LEFT JOIN test1 ON test.a = test1.a GROUP BY c,test.a(test009.js), and the same shape intest229.jsandtest416.jstest)All three are tracked by #2928 (milestone v5.0.0), which rejects an ambiguous bare column inside a built-in function and in the select list. Until then 4.x keeps taking the first table, as #2930 decided for 2a. 2b and 2c look like the same rule reached by another path.
3. A bare name that another source has (2)
SELECT SUM(b) AS sb,a,c FROM test LEFT JOIN test1 ON test.a = test1.a GROUP BY c,test.a, where onlytest1hasc(test009.js), andSELECT c, SUM(b) AS s FROM one t0 JOIN (SELECT a,c FROM two) t1 ON t0.a=t1.a GROUP BY c(test832.js)test,t0)test1,t1)The lookup writes
(this.groupSources.get(g) || g)['test']['c']here, a read ofcfrom the first table. Both statements still give the right values today (cis 7 and 8 in the first, 70 and 80 in the second, on small test data). I did not trace which other code decides the value, so I can not say when this lookup matters.What the check does not cover
bind.columngives scope 1 for it (test E below), and no test in the suite has one that today's lookup answers differently.source.columnwhere no source has that name, such asSELECT zz.a FROM t. I met it while trying the failing path of the check. Today's lookup writes a read ofzz, and the binding says unresolved. No test in the suite compiles one, so it is not among the six cases, and the check would fail on a test that adds it.Tests
test/test2723-B.js, in the named databasetest2723B, reads the binding throughColumn.onBind: one source, an alias, an ambiguous name, sources without definitions (one, several, beside a table), a column of the outer SELECT with and without the source name, an unresolved name, and that a statement outside a SELECT has no scope while results are as before.Suite: 2738 pass, 213 skip, 0 fail. The run-state check gives an empty list, the dialect test gives all cases as expected, and
yarn test-parserpasses. No change to the grammar or the lockfiles.package.jsongets one script line..github/workflows/Build and test.ymlgets the jobbinding-check, a copy ofrun-state-checkwith the same pinned actions and permissions, and another last step (bun test/bind/run.js).Compile time
Measured again with the binding worked out only in the test mode. Microseconds for one
Select.compileof a parsed statement, the best of sixteen processes of 12 rounds of 300 compiles, with main and the PR taking turns. Node 24 and Bun 1.4.3.SELECT a, b, a + b AS c FROM ? WHERE a > ?The load average was 6 to 8 during the runs, lower than for the first numbers, which is why main is faster here too. The two halves of the run did not agree on every line (a single line moved between -1% and +7%), so a few percent is inside the noise. What I take from it is that a compile now costs between nothing and about 0.3 microseconds more. That is the scope of each SELECT, which stays on. Before this change it was up to 5 microseconds (+28% on the three-table join on Bun). A cached statement pays it once.
Parity differences
From the parity job of this PR on its second commit (7d338c1), against the base (1a730c6) with the test files of this PR. 78,567 statements aligned in 587 files.
No statement of the existing suite gives another result, error or generated code because of this PR. The lines that show are these.
test/test2723-B.js. On the base nothing callsColumn.onBind, so its first test stops at its first assert and the statements after it do not run.SELECT * FROM xlsx("test/test815.xlsx", {cellDates: false})intest815.js, a date that differs in the eighth digit between two runs of the suite. It showed in the same way in Resolve db.table in one place with bind.table to close #2705 #3099 and is not from this PR.Column.prototype.lookupJSandSelect.prototype.compileInScope. The report gives the count and not the names, so that is my reading. Both classes are already intest/parity/api-allowed.txtsince Give expression nodes one base class with walk() and equals() to close #2721 #3088, and a line there covers the prototype, so the file is not changed.Sizes:
alasql.fs.js949,347 to 953,889 bytes (+0.48%),alasql.min.js541,656 to 543,486 bytes (+0.34%), gzip +795 bytes on the minified file (the gzip number is from my machine).The next step, not started
Decide the six cases above, then let the generated code read the binding: first the select list and
Column.toJSfor a plain row (p[source][column]), with the lookup kept as the check in the other direction, then the group context and the outer scope.