-
Notifications
You must be signed in to change notification settings - Fork 690
Description
Fix: Support Qualified Table Names in ON Clauses
Problem
The Alasql parser was incorrectly parsing three-part column references (e.g., database.table.column) in ON clauses. It treated them as a two-part column reference (database.table) followed by a property access (-> column), leading to runtime errors like TypeError: undefined is not an object.
Solution
We modified the Jison grammar to resolve a shift/reduce conflict, ensuring that database.table.column is parsed as a single Column node.
1. Parser Grammar Update
We added a low-precedence token %nonassoc LOW_PREC and applied it to the Literal DOT Literal rule in Column definition. This forces the parser to shift the second dot when encountering a.b.c, correctly identifying it as a three-part identifier.
// src/alasqlparser.jison
%nonassoc LOW_PREC
%left COMMA
// ...
Column
: Literal DOT Literal DOT Literal
{ $$ = new yy.Column({columnid: $5, tableid: $3, databaseid:$1});}
| Literal DOT Literal %prec LOW_PREC
{ $$ = new yy.Column({columnid: $3, tableid: $1});}
// ...2. Test Refactoring
We refactored test/test407.test.js to use fully qualified table names in all queries, ensuring isolated database contexts and verifying the fix.
// test/test407.test.js
// Before
'SELECT one.id AS a ... FROM one INNER JOIN two ...'
// After
'SELECT test407.one.id AS a ... FROM test407.one INNER JOIN test407.two ...'3. Regression Test
We created a new regression test file test/test2186.test.js to verify that the fix works for both qualified and unqualified table names, with and without USE <DB> statements. We used table names A_2186 and B_2186 to avoid collisions. This ensures that future changes do not break this functionality.
Verification
We verified the fix by:
- Running
bun run jisonto rebuild the parser. - Running
bun run buildto build the project. - Running
bun test test/test407.test.js(passed). - Running
bun test test/test2186.test.js(passed). - Running the full test suite
bun run tst(passed with no regressions).