Skip to content

Refactor: Name the rules for an ambiguous and an unknown column beside the binding - #3112

Merged
mathiasrw merged 1 commit into
mainfrom
fix/2723-4x-rules
Nov 22, 2026
Merged

mathiasrw merged 1 commit into
mainfrom
fix/2723-4x-rules

Conversation

@mathiasrw

@mathiasrw mathiasrw commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Part of #2723, step 1 of letting the generated code read the binding. It does not close the issue, and no generated code reads anything new.

Why

bind.column says what a column reference refers to. In a few places 4.x reads something else, and queries rely on it. Before the generated code can read the binding, those readings have to be written down as rules, so that a later step keeps them and 5.0 can remove them one by one.

"The first table" turned out to mean three different things. With t0(k, x), t1(k, v) and t2(k, v) joined in that order, the bare name v gives this today.

Place Query Today
alone in the select list SELECT v ... 10 and 11, from t1
argument of a built-in function SELECT ABS(v) AS r ... undefined, a read of t0.v
grouped select list SELECT v, COUNT(*) AS n ... GROUP BY t0.k undefined, a read of t0.v
plain expression ... WHERE v > 0 the error "exists in two source tables"

What changes

src/engine/bind.js gets three 4.x rules and bind.read(ref, scope, place), which applies the rules of a place to a binding. Each rule has a comment that says what it keeps and which issue removes it.

Rule When What 4.x reads Removed by
onlyTable A bare name that no source can have, in a SELECT with one source. Every place that source, and the value is undefined #3109, and #2878 for an alias of the select list inside an aggregate in HAVING
firstTable A bare name that two tables define, as the argument of a built-in function or in the select list of a grouped query the first source of FROM, whether it defines the name or not #2928
firstWithColumn A bare name that two tables define, alone in the select list of a query without grouping the first source that has it #2928

In a plain expression an ambiguous name is an error in 4.x too, so there is no rule for it.

The names differ from my plan, which had a default-table, a per-row and a strict rule. The strict one is no 4.x rule, it is what the binding says, and the default-table rule was two rules with two different issues, so it is split.

The place is told by the code that compiles the reference, not guessed. The select list passes 'select'. The grouped select list (src/select/columns.js) and the arguments of a built-in function (src/functions/functions.js) set the place on the scope around their call and set it back. Everything else is 'row'.

Only in the test mode

As with the binding, nothing of this runs for a user. The two call sites read Column.onBind once and set the place only when it is set. Column.toJS and the select list call bind.read only on the path that already required Column.onBind. The scope carries nothing new. The place is a property that only the test mode writes.

Column.onBind gets the 4.x reading as a ninth argument.

The binding check

test/bind/ now compares today's lookup with the binding after the rule of the place is applied. The list of known cases and the rules that read the SQL text with regular expressions are gone. The check fails on any difference, and on an error thrown by bind.column or a rule.

On this branch the suite compiles 5,669 column references inside a SELECT.

Outcome References
The binding is what today's lookup reads 5,509
The same by onlyTable (21 in a plain expression, 1 in a function, 1 in the select list) 23
The same by firstTable (12 in a function, 8 in a grouped select list) 20
The same by firstWithColumn 3
Today's code names no source (a group or the row itself) 114
Differences 0

Tests

test/test2723-C.js, in the named database test2723C, pins today's results in the four places with the three tables above, and with two tables where the first one defines the name. It also pins the unknown column of the only table (WHERE nosuch IS NULL, ABS(nosuch)) and the HAVING alias (HAVING SUM(n) > 0). For each place it reads the binding and the 4.x reading through Column.onBind. The comment at the top says that these are today's results and not the ones 5.0 should give.

Suite: 2751 pass, 213 skip, 0 fail. The run-state check gives an empty list and yarn test-parser passes.

Compile time

With the test mode off. Microseconds for one Select.compile of a parsed statement, the best of sixteen processes of 12 rounds of 300 compiles (twenty for the last two queries), 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.42 6.71 +5%
Bun 3.46 3.43 -1%
join of two tables Node 8.92 9.23 +3%
Bun 5.18 5.24 +1%
join of three tables with GROUP BY and HAVING Node 27.87 28.07 +1%
Bun 14.54 14.30 -2%
correlated subquery in the select list and in EXISTS Node 21.41 21.06 -2%
Bun 12.50 12.78 +2%
SELECT a, b, a + b AS c FROM ? WHERE a > ? Node 8.09 8.20 +1%
Bun 4.20 4.28 +2%
twenty columns of one table Node 18.80 18.90 +1%
Bun 9.30 9.42 +1%
six built-in functions in the select list and in WHERE Node 12.50 12.56 0%
Bun 5.76 6.10 +6%
three bare columns in a grouped select list Node 14.21 14.35 +1%
Bun 6.39 6.57 +3%

The machine was under heavy load from other work (load average 35 to 98), so these numbers are weaker than the ones in #3108. The first two queries do not go through any changed line with the test mode off, and they move by as much as the last two, which do. In two runs of the last two queries the +6% was once on one and once on the other. I read the table as no measurable change. The code that a compile runs in addition is one read of Column.onBind and a try and finally per built-in function call and per bare column of a grouped select list.

Parity differences

From the parity job of this PR, against the base (f4a75e7) with the test files of this PR. 78,622 statements aligned in 589 files.

Kind Count
result 0
error 0
params 0
generated code 0
only in the base, only in this PR, diverged 0
API paths (1,720) 0 changed, 0 allowed

There is no difference to list.

Sizes: alasql.fs.js 954,074 to 957,663 bytes (+0.38%), alasql.min.js 543,544 to 544,683 bytes (+0.21%), gzip +403 bytes on the minified file. About half of the added source is the comments of the rules.

Supply chain

No change to package.json, the lockfiles, the workflows, the grammar or the generated parser.

The next step, not started

Step 2 of the plan: the grouped select list writes its code from bind.read, with the lookup kept as the check in the other direction.

@mathiasrw
mathiasrw merged commit c7eacfb into main Nov 22, 2026
28 checks passed
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