Skip to content

Refactor: Work out the binding of each column reference beside the lookup - #3108

Merged
mathiasrw merged 2 commits into
mainfrom
fix/2723-column-binding
Nov 22, 2026
Merged

mathiasrw merged 2 commits into
mainfrom
fix/2723-column-binding

Conversation

@mathiasrw

@mathiasrw mathiasrw commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

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.js gets 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.compile enters 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 a Column refers to: {scope, source, column}, or {scope, any: [...]} when the source is decided per row, or {scope, ambiguous: [...]}, or {unresolved: true}. scope is 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.

  1. A name written as source.column is that source, in the nearest scope that has it.
  2. A bare name that the column definitions of exactly one source have is that source's column.
  3. Found in two, it is ambiguous. 4.x throws there ("exists in two source tables"), in the places where it looks at all.
  4. Found in none, and the SELECT has sources without column definitions (a subquery, a parameter, a function, a table made without columns): one such source is the source, several are decided per row, the first whose row has the key (Write the per-row read of a column from a source without column definitions only in the binding #2680, unchanged).
  5. Else the name is looked up in the SELECT around it.

Today's lookup is in two places, and the binding is asked beside both, but only when alasql.yy.Column.onBind is set. Nothing sets it outside tests, so a query does not call bind.column at all. It pays for the scope stack in Select.compile and for nothing else, and a mistake in bind.column can not reach a query.

  • Column.toJS (src/expression/expression.js). Its body is now lookupJS, unchanged. Without onBind, toJS calls lookupJS and nothing more. With it, toJS calls bind.column and then lookupJS, and hands both to onBind.
  • The select list in compileSelect1 (src/select/columns.js), which has its own lookup for a column that stands alone.

An error thrown by bind.column in the test mode is caught and handed to onBind as {error}, so it can not change what a test sees. Nothing is kept on the Column node, and no generated code changes. src/select/having.js is untouched, and so is the HAVING alias rule (#2878).

The test mode

When alasql.yy.Column.onBind is 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.md says how.

The check runs in CI as the job "Binding check of column references", built like the run-state check. It fails when bind.column throws, 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. knownCase in test/bind/record.js holds 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 a bind.column that 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.

Outcome References
The same source, or the same sources for a per-row decision 5,411
Today's code names no source, so there is nothing to compare 114
They differ 34

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.

Case Example Today Binding References
1a. The column does not exist. Tracked by #3109 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, where t1 has the columns x and y (test258.js). The other statement is SELECT (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 1 t1, and the value is undefined in every row unresolved 16
1b. The name is an alias of the select list, inside an aggregate in HAVING. Tracked by #2878 SELECT a, COUNT(*) AS n FROM t GROUP BY a HAVING SUM(n) > 0, where t has a, b, cnt (test887.js) t unresolved 2 (the aggregate is compiled twice)

1a is tracked by #3109 (milestone v5.0.0), which decides whether an unknown column stays undefined or becomes an error. Until then 4.x keeps reading undefined. 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.column says ambiguous. Today's lookup throws for this in a plain expression, but not in these three places, where it takes the first table.

Case Example Today Binding References
2a. An argument of a built-in function. Tracked by #2928 SELECT UPPER(w) AS u, LENGTH(w) AS l FROM two t1 JOIN three t2 ON t1.a = t2.a and SELECT 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) the first table ambiguous 9
2b. A column that stands alone in the select list. Tracked by #2928 SELECT ta.id, x, z FROM ta JOIN tb ON ta.id = tb.id, where both tables have x (test2873.js) the first table (ta) ambiguous 1
2c. The select list of a query with GROUP BY. Tracked by #2928 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 in test229.js and test416.js the first table (test) ambiguous 4

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)

Case Example Today Binding References
3. The select list of a query with GROUP BY, where the first table does not define the column SELECT SUM(b) AS sb,a,c FROM test LEFT JOIN test1 ON test.a = test1.a GROUP BY c,test.a, where only test1 has c (test009.js), and SELECT 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) the first table (test, t0) the source that has it (test1, t1) 2

The lookup writes (this.groupSources.get(g) || g)['test']['c'] here, a read of c from the first table. Both statements still give the right values today (c is 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

  • A reference that no test compiles. An unqualified column of the outer query in a correlated subquery (Correlated subquery with an unqualified outer column returns no rows #2644) is such a case: bind.column gives scope 1 for it (test E below), and no test in the suite has one that today's lookup answers differently.
  • A name written as source.column where no source has that name, such as SELECT zz.a FROM t. I met it while trying the failing path of the check. Today's lookup writes a read of zz, 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.
  • A reference outside a SELECT, such as the SET and WHERE of an UPDATE, a DELETE, or a CHECK constraint. There is no scope there yet.

Tests

test/test2723-B.js, in the named database test2723B, reads the binding through Column.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-parser passes. No change to the grammar or the lockfiles. package.json gets one script line. .github/workflows/Build and test.yml gets the job binding-check, a copy of run-state-check with 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.compile of 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.

Query Runtime main PR Change
one table with WHERE and ORDER BY Node 6.45 6.42 0%
Bun 3.30 3.42 +4%
join of two tables Node 8.50 8.86 +4%
Bun 4.82 5.07 +5%
join of three tables with GROUP BY and HAVING Node 26.55 26.91 +1%
Bun 14.11 13.95 -1%
correlated subquery in the select list and in EXISTS Node 20.31 20.28 0%
Bun 12.05 12.43 +3%
SELECT a, b, a + b AS c FROM ? WHERE a > ? Node 7.80 7.82 0%
Bun 4.01 4.26 +6%
twenty columns of one table Node 18.59 18.54 0%
Bun 9.05 9.21 +2%

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.

Kind Count
result 1
error 0
params 0
generated code 0
only in this PR 9
diverged 0
API paths (1,718) 0 changed, 2 allowed

No statement of the existing suite gives another result, error or generated code because of this PR. The lines that show are these.

  • The 9 statements only in this PR are in the new test/test2723-B.js. On the base nothing calls Column.onBind, so its first test stops at its first assert and the statements after it do not run.
  • The one result difference is SELECT * FROM xlsx("test/test815.xlsx", {cellDates: false}) in test815.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.
  • The 2 allowed API differences should be the two classes that got a new method, Column.prototype.lookupJS and Select.prototype.compileInScope. The report gives the count and not the names, so that is my reading. Both classes are already in test/parity/api-allowed.txt since 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.js 949,347 to 953,889 bytes (+0.48%), alasql.min.js 541,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.toJS for 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant