Skip to content

Support Qualified Table Names in ON Clauses #2186

@mathiasrw

Description

@mathiasrw

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:

  1. Running bun run jison to rebuild the parser.
  2. Running bun run build to build the project.
  3. Running bun test test/test407.test.js (passed).
  4. Running bun test test/test2186.test.js (passed).
  5. Running the full test suite bun run tst (passed with no regressions).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions