Skip to content

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

Open
joshuablac wants to merge 3 commits into
AlaSQL:developfrom
joshuablac:fix/qualified-on-2186
Open

joshuablac wants to merge 3 commits into
AlaSQL:developfrom
joshuablac:fix/qualified-on-2186

Conversation

@joshuablac

@joshuablac joshuablac commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2186

Problem

A three-part database.table.column reference throws Cannot read properties of undefined (reading '<table>') everywhere it is used, not only in ON. That includes SELECT, WHERE, ORDER BY, functions and correlated subqueries.

Cause

db.t1.id is parsed as (db.t1) -> id: a two-part column (tableid: 'db', columnid: 't1'), followed by a property lookup. The grammar change suggested in the issue fixes db.t1.id, but it breaks alias.column.property. For example, SELECT t.obj.x FROM ? t works on develop and has no test. The parser can't tell the two apart, because the difference is in what the names refer to.

Fix

Before a SELECT is compiled, resolveDatabaseColumns in src/40select.js runs once on the parsed query, including nested SELECTs. It replaces (db.t1) -> id with a real yy.Column (databaseid: 'db', tableid: 't1', columnid: 'id') when all of these hold:

  • db is an existing database
  • db is not the name of a source in the query or an enclosing one
  • t1 is a table of database db in the query or an enclosing one, without a different alias

After that the reference goes through the normal column code. Naming, functions, GROUP BY and correlated subqueries behave as they do for t1.id. The grammar is unchanged.

Otherwise:

  • A source named db still makes it alias.column.property, a property lookup as before.
  • A table that has an alias gives Table db.t1 has the alias x in this query, so use x.id instead of db.t1.id.
  • A t1 from another database gives Cannot resolve db.t1.id: t1 in this query is not a table of database db.
  • A table that is not in the query gives Cannot resolve db.t1.id: table db.t1 is not used in this query.

The cases from the review:

Query develop This PR
SELECT db.t1.id AS i FROM other.t1 throws (TypeError) error: t1 is not a table of database db
SELECT db.t1.id FROM db.t1 throws (TypeError) [{id: 1}, {id: 2}]
SELECT UPPER(db.t1.v) AS u FROM db.t1 throws (TypeError) [{u: 'A'}, {u: 'B'}]
... WHERE EXISTS (SELECT * FROM db.t2 WHERE db.t2.id = db.t1.id) throws (TypeError) [{id: 1}]
SELECT db.t1.id AS i FROM db.t1 AS x throws (TypeError) error: use x.id

Tests

  • test/test2186.js covers:
    • ON, the SELECT list, WHERE and ORDER BY, and USE <db>
    • the result column name, a correlated subquery, a function argument and GROUP BY
    • the three errors
    • alias.column.property, including a source named like a database
  • 9 of its 11 tests fail on develop. The 2 alias.column.property checks pass on both.
  • Full suite on the current develop plus this PR (bun test, same command as yarn test-only): 2566 pass, 213 skip. The only failure is CLI test 10 in test2149.js, which fails the same way on unmodified develop in my local setup because my checkout path contains a space.

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 <[email protected]>
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.

@mathiasrw

mathiasrw commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

@joshuablac Tank you for this.

It does solve some cases. I was wondering if you feel like taking a stab at these cases as well in this PR that seems to be the same family of problems?

alasql('CREATE DATABASE db');
alasql('CREATE TABLE db.t1 (id INT, v STRING)');
alasql('CREATE TABLE db.t2 (id INT, w STRING)');
alasql("INSERT INTO db.t1 VALUES (1,'a'),(2,'b')");
alasql("INSERT INTO db.t2 VALUES (1,'x'),(3,'y')");

// 1. Argument of a built-in function
alasql('SELECT UPPER(db.t1.v) AS u FROM db.t1');
// throws: Cannot read properties of undefined (reading 't1')
// expected: [{u:'A'}, {u:'B'}]

// 2. Correlated subquery that refers to the outer table
alasql('SELECT id FROM db.t1 WHERE EXISTS (SELECT * FROM db.t2 WHERE db.t2.id = db.t1.id)');
// throws: Cannot read properties of undefined (reading 't1')
// expected: [{id:1}]

// 3. Table name used after the table has an alias
alasql('SELECT db.t1.id AS i FROM db.t1 AS x');
// throws: Cannot read properties of undefined (reading 't1')
// expected: a clear error saying to use the alias x (SQL does not allow the table name here)

@mathiasrw

Copy link
Copy Markdown
Member

@joshuablac I looked a little deeper. That the grammar change from the issue breaks t.obj.x was a great catch, and I agree that this has to be decided by looking at the names, not by the parser.

I tried it on top of current develop (full suite green) and found a few things:

Query Result Should be
SELECT db.t1.id AS i FROM other.t1 [{i: 7}] - read from other.t1 an error
SELECT db.t1.id FROM db.t1 key db.t1->id key id, the same as t1.id gives
SELECT UPPER(db.t1.v) FROM db.t1 throws A, B
correlated ... WHERE db.t2.id = db.t1.id throws [{id: 1}]
SELECT db.t1.id FROM db.t1 AS x TypeError a clear "use the alias x" error
  • The first one is the important one: the check matches the table name but not its database, so a query that throws today would silently read another table.
  • The key name matters because once this is released, people will build on db.t1->id.
  • The UPPER case is a separate bug: built-in functions don't pass defcols to their arguments. I opened Built-in functions read an unqualified column from the first table in a JOIN #2615 for it, so you don't need to handle that here.

I think the rest share one root cause: the rewrite happens late, inside the code generation for -> in Op.toJS. At that point it only knows the current query's sources, and the expression stays an Op, so it isn't named or treated like a column.

A suggestion: resolve it once on the parsed query, before compiling. Turn (db.t1)->id into a real yy.Column (database db, table t1, column id) when db is a database and t1 is a source from database db in this query or an enclosing one. Then naming, GROUP BY, functions and correlated subqueries all go through the normal column code, and a table name that has an alias can raise a clear error.

joshuablac and others added 2 commits November 10, 2026 20:03
Following the review on AlaSQL#2578, `(db.t1)->id` is now turned into a real
column (database db, table t1, column id) once, on the parsed query,
instead of in the code generation for `->`. It applies when `db` is a
database and not a source name, and `t1` is a table of database `db` in
this query or an enclosing one. So:

- the result column is named `id`, like `t1.id`
- it works in functions, GROUP BY and correlated subqueries
- a table of another database with the same name is an error instead of
  being read silently
- a table that has an alias gives an error that says to use the alias
- a table that is not in the query gives a clear error

alias.column.property, including a source named like a database, is still
a property lookup.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@joshuablac

joshuablac commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. I followed your suggestion in f00aaac (the latest commit). (db.t1)->id is now resolved once on the parsed query, before compiling, into a real yy.Column. That happens only when db is a database (not a source name) and t1 is a table from database db in this query or an enclosing one. The late rewrite in Op.toJS is gone, so src/50expression.js is unchanged from develop.

Query Now
SELECT db.t1.id AS i FROM other.t1 error: t1 in this query is not a table of database db
SELECT db.t1.id FROM db.t1 key id, the same as t1.id
SELECT UPPER(db.t1.v) AS u FROM db.t1 A, B
correlated ... WHERE db.t2.id = db.t1.id [{id: 1}]
SELECT db.t1.id AS i FROM db.t1 AS x error: use x.id instead of db.t1.id

t.obj.x and other alias.column.property lookups are unchanged, including when a source has the same name as a database. test2186.js now has 11 cases, and 9 of them fail on develop. I also merged the current develop into the branch.

🤖 Addressed by Claude Code

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

3 participants