Skip to content

Resolve db.table in one place with bind.table to close #2705 - #3099

Merged
mathiasrw merged 2 commits into
mainfrom
fix/2705
Nov 22, 2026
Merged

mathiasrw merged 2 commits into
mainfrom
fix/2705

Conversation

@mathiasrw

@mathiasrw mathiasrw commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Closes #2705. The second step of #2723, and only that step: bind for db.table.

What changes

A new module, src/engine/bind.js, with one function for now. bind.table(ref, databaseid) takes a table reference as parsed (t or db.t) and the database the statement runs in, and returns {databaseid, tableid, db}. Every statement that takes a table asks it, where each used to write its own this.table.databaseid || databaseid.

Where Statements
src/statements/ CREATE TABLE (the table, the four foreign key lookups, the view's database), DROP TABLE, TRUNCATE TABLE, ALTER TABLE, INSERT, UPDATE, DELETE, CREATE INDEX, CREATE TRIGGER, SHOW COLUMNS, SHOW INDEX, SHOW CREATE TABLE
src/graph/ SEARCH ... FROM db.t
src/select/ the sources of FROM and JOIN (from.js, join.js, defcols.js), SELECT INTO, the FROM DUAL check

One statement looked in the wrong database, SEARCH ... FROM db.t, and its result changes (see the parity differences). No other result changes. bind is on internal, not on the alasql object, so the API snapshot has no new path.

Two choices to look at.

  • The result is returned, not kept on the yy.Table node. A parsed statement is cached and can be run in another database, and ROLLBACK replaces the database object, so a node that remembered its database could go stale. It also keeps alasql.parse() output as it is.
  • bind.table does not look up the table and does not throw. Review #2201 for code improvements: each statement resolves db.table on its own #2705 suggests a clear error when the database or the table is missing. That changes error texts people can see today, and each statement has its own rule for a missing table (IF EXISTS, dropifnotexists, its own message), so it is left for a PR that names it.

Not in this PR

Lookups of a table's database left outside bind.js

A grep over src/ for databases[...].tables and databaseid || after this PR. Two places still resolve the database of a name themselves, on purpose.

  • src/select/columns.js line 224 resolves the database of a column (col.databaseid || query.sources[0].databaseid || ...). That is column binding and stays for that step.
  • src/statements/show.js line 49, SHOW TABLES FROM db, names a database and no table, so there is no db.table to resolve.

The other hits do not resolve a reference. They read a source.databaseid that bind.table already resolved (from.js, join.js, queryfn.js, pivot.js, and columns.js lines 315 to 320), or they work in the current database by design (WITH, CREATE GRAPH, the SEARCH selector at search.js line 73, the storage engines, the dialect helpers, yy.js, core.js). The show.js lines 78, 114 and 136 pass this.databaseid || databaseid to bind.table as the fallback of SHOW ... FROM t FROM db. The lookup in with.js line 247 is inside a comment.

Parity differences

Against the base (bdb847c), recorded with the test files of this PR, so test/test2705.js ran on both builds. The numbers are from the parity job of this PR. 78,512 statements aligned in 586 files.

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

The result change is the fix. For SEARCH ... FROM db.t the parser makes a Column with the database in tableid and the table in columnid, so the old lookup never saw the database and read the table of that name in the current database.

Statement Before After
search / a from test2705b.s, with a table s in both databases [1], from the current database [7,8], from test2705b

When the current database has no table of that name, the statement threw a TypeError before and answers from the named database now. Code that relied on db. being ignored gets another result, so this is the one change in the PR that a user can see. The three statements only in this PR are the rest of that test, which stops at the failing assert on the base build.

The second result difference is not from this PR. SELECT * FROM xlsx("test/test815.xlsx", {cellDates: false}) gives 46316.50400071759 on the base and 46316.50400075231 here. This PR does not touch that path, and the value differs in the eighth digit between two runs of the suite. I did not look into why.

The one error difference is the text of a TypeError that the JavaScript engine writes, for a database that does not exist. JavaScriptCore (Bun, Safari) quotes the source of the failing expression, so the text follows the code. Under V8 (Node, Chrome) the text is Cannot read properties of undefined (reading 'tables') before and after.

Statement Before (Bun) After (Bun)
select * from nodb2705.t undefined is not an object (evaluating 'alasql.databases[fr.databaseid || databaseid].tables') undefined is not an object (evaluating 'db.tables')

One more of the same kind does not show in the recording, because an earlier test file leaves autocommit off. With autocommit on, the default, a probe on both builds gives it.

Statement Before (Bun) After (Bun)
select 1 into nodb2705.t undefined is not an object (evaluating 'alasql.databases[this.into.databaseid || databaseid].engineid') undefined is not an object (evaluating 'into.db.engineid')

The probe ran 44 statements with db.table, 12 of them on a database that does not exist, on the base build and on this build. Under Node all 44 answers are the same. Under Bun 42 are the same and the two above differ.

Without test/test2705.js the compare was empty before the SEARCH fix: 78,461 statements, 0 differences. No test file other than test/test2705.js uses SEARCH ... FROM db.t, as the CI compare shows.

Sizes in the parity job: alasql.fs.js 945,443 to 946,119 bytes (+0.07%), alasql.min.js 539,301 to 539,600 bytes (+0.06%), gzip +173 bytes.

Tests

test/test2705.js, in the named databases test2705 and test2705b. It runs every statement above as db.table while the other database is the current one, checks that nothing was made in the current database, and checks the missing table and missing database cases. Test G is the SEARCH case. All tests but G pass on the base build too, which is the proof that the other results are as in 4.x.

Suite: 2723 pass, 213 skip, 0 fail. Dialect test: all cases as expected. yarn test-parser passes. No change to package.json, the lockfiles, the grammar or the workflows.

The next step, not started

Column binding. bind(select, databaseid) builds one scope per SELECT (its sources, with the columns of each where the table has a schema, and a link to the outer SELECT) and records on each Column the source and the column it refers to, with today's rules for a name found in two sources. Column.toJS then reads what was recorded. The source list would come from bind.table, so defcols.js and the aliases built in from.js and join.js are the first things it replaces. The fromAnySource fallback and the alias rules are separate steps after it.

@mathiasrw
mathiasrw merged commit 32ee928 into main Nov 22, 2026
24 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.

Review #2201 for code improvements: each statement resolves db.table on its own

1 participant