Repository navigation
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.
Closes #2705. The second step of #2723, and only that step:
bindfordb.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 (tordb.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 ownthis.table.databaseid || databaseid.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 TABLEsrc/graph/SEARCH ... FROM db.tsrc/select/FROMandJOIN(from.js,join.js,defcols.js),SELECT INTO, theFROM DUALcheckOne statement looked in the wrong database,
SEARCH ... FROM db.t, and its result changes (see the parity differences). No other result changes.bindis oninternal, not on thealasqlobject, so the API snapshot has no new path.Two choices to look at.
yy.Tablenode. A parsed statement is cached and can be run in another database, andROLLBACKreplaces the database object, so a node that remembered its database could go stale. It also keepsalasql.parse()output as it is.bind.tabledoes 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
Column.toJSand thefromAnySourcefallback (Write the per-row read of a column from a source without column definitions only in the binding #2680) are untouched.src/select/having.jsis untouched (Let a source column win over a SELECT alias of the same name in HAVING to close #2878 #3095).DROP INDEX,REINDEXandDROP TRIGGERtake a name without a table, so they have nodb.tableto resolve.Lookups of a table's database left outside
bind.jsA grep over
src/fordatabases[...].tablesanddatabaseid ||after this PR. Two places still resolve the database of a name themselves, on purpose.src/select/columns.jsline 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.jsline 49,SHOW TABLES FROM db, names a database and no table, so there is nodb.tableto resolve.The other hits do not resolve a reference. They read a
source.databaseidthatbind.tablealready resolved (from.js,join.js,queryfn.js,pivot.js, andcolumns.jslines 315 to 320), or they work in the current database by design (WITH,CREATE GRAPH, theSEARCHselector atsearch.jsline 73, the storage engines, the dialect helpers,yy.js,core.js). Theshow.jslines 78, 114 and 136 passthis.databaseid || databaseidtobind.tableas the fallback ofSHOW ... FROM t FROM db. The lookup inwith.jsline 247 is inside a comment.Parity differences
Against the base (bdb847c), recorded with the test files of this PR, so
test/test2705.jsran on both builds. The numbers are from the parity job of this PR. 78,512 statements aligned in 586 files.The result change is the fix. For
SEARCH ... FROM db.tthe parser makes aColumnwith the database intableidand the table incolumnid, so the old lookup never saw the database and read the table of that name in the current database.search / a from test2705b.s, with a tablesin both databases[1], from the current database[7,8], fromtest2705bWhen the current database has no table of that name, the statement threw a
TypeErrorbefore and answers from the named database now. Code that relied ondb.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})gives46316.50400071759on the base and46316.50400075231here. 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
TypeErrorthat 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 isCannot read properties of undefined (reading 'tables')before and after.select * from nodb2705.tundefined 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
autocommitoff. Withautocommiton, the default, a probe on both builds gives it.select 1 into nodb2705.tundefined 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.jsthe compare was empty before theSEARCHfix: 78,461 statements, 0 differences. No test file other thantest/test2705.jsusesSEARCH ... FROM db.t, as the CI compare shows.Sizes in the parity job:
alasql.fs.js945,443 to 946,119 bytes (+0.07%),alasql.min.js539,301 to 539,600 bytes (+0.06%), gzip +173 bytes.Tests
test/test2705.js, in the named databasestest2705andtest2705b. It runs every statement above asdb.tablewhile 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 theSEARCHcase. 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-parserpasses. No change topackage.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 eachColumnthe source and the column it refers to, with today's rules for a name found in two sources.Column.toJSthen reads what was recorded. The source list would come frombind.table, sodefcols.jsand thealiasesbuilt infrom.jsandjoin.jsare the first things it replaces. ThefromAnySourcefallback and the alias rules are separate steps after it.