Skip to content

Fix database.table.column references throwing to close #2186 - #2578

Open
joshuablac wants to merge 1 commit into
AlaSQL:developfrom
joshuablac:fix/qualified-on-2186
Open

joshuablac wants to merge 1 commit into
AlaSQL:developfrom
joshuablac:fix/qualified-on-2186

Conversation

@joshuablac

Copy link
Copy Markdown

Closes #2186

Problem

A three-part database.table.column reference throws Cannot read properties of undefined (reading '<table>') wherever it is used, not only in ON:

SELECT * FROM db.t1 JOIN db.t2 ON db.t1.id = db.t2.id   -- throws
SELECT db.t1.id FROM db.t1                               -- throws
SELECT * FROM db.t1 WHERE db.t1.id = 1                   -- throws
SELECT * FROM db.t1 ORDER BY db.t1.id                    -- throws

Cause

As the issue describes, db.t1.id is parsed as (db.t1) -> id: a two-part column (tableid: 'db', columnid: 't1') followed by a property access. The compiled code reads p['db']['t1'], which is undefined.

Why this doesn't use the grammar change from the issue

I tried the change the issue proposes (%nonassoc LOW_PREC plus Literal DOT Literal %prec LOW_PREC). It fixes db.table.column, and the existing suite still passes. But it breaks alias.column.property, which works on develop and has no test:

alasql('SELECT t.obj.x AS v FROM ? t', [[{obj: {x: 10}}]]);
// develop:            [{v: 10}]
// with grammar change: TypeError: Cannot read properties of undefined (reading 'x')

The parser can't tell db.table.col from alias.col.prop, because the difference depends on what the names refer to. So I resolve it at compile time instead.

Fix

In Op.toJS for ->, a left side a.b is compiled as the column a.b.<right> only when all of these hold:

  • a is not a source alias of the query
  • a is an existing database
  • b is a source of the query

Every other -> compiles exactly as before. The only expressions whose behavior changes are ones that throw today. No grammar change.

Note: an unaliased SELECT db.t1.id FROM db.t1 gets the key db.t1->id, the same automatic naming any -> expression gets. Use AS for a clean name.

Tests

  • Added test/test2186.js (named after the A_2186 / B_2186 tables from the issue):
    • database.table.column in ON, in the SELECT list, WHERE and ORDER BY, and with USE <db>
    • a regression check that alias.column.property (t.obj.x, t.obj.y.z, and the t.obj->x key name) behaves as before
  • The 3 database-qualified cases fail on develop and pass with this change.
  • Full suite (bun test, same command as yarn test-only): 2253 pass, 213 skip. The only failures are the 2 CLI tests in test2149.js, which fail the same way on unmodified develop in my local setup (a space in my checkout path, and the version-injection step of build.sh not running).

This fix was written with AI assistance (Claude Code) and verified locally as described above.

🤖 Generated with Claude Code

A reference like db.table.column was parsed as (db.table)->column, so the
compiled code read p['db']['table'] and threw "Cannot read properties of
undefined" in ON, WHERE, ORDER BY and the SELECT list.

When compiling the -> operator, a left side "a.b" where "a" is a database
and not a source alias, and "b" is a source table, is now compiled as the
column a.b.column. alias.column.property keeps working as a property
lookup, so the grammar is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 12, 2026 16:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

Support Qualified Table Names in ON Clauses

2 participants