Repository navigation
Fix database.table.column references throwing to close #2186 - #2578
joshuablac wants to merge 3 commits into
Conversation
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]>
|
@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) |
|
@joshuablac I looked a little deeper. That the grammar change from the issue breaks I tried it on top of current
I think the rest share one root cause: the rewrite happens late, inside the code generation for A suggestion: resolve it once on the parsed query, before compiling. Turn |
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]>
|
Thanks for the detailed review. I followed your suggestion in f00aaac (the latest commit).
🤖 Addressed by Claude Code |
Closes #2186
Problem
A three-part
database.table.columnreference throwsCannot read properties of undefined (reading '<table>')everywhere it is used, not only inON. That includesSELECT,WHERE,ORDER BY, functions and correlated subqueries.Cause
db.t1.idis 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 fixesdb.t1.id, but it breaksalias.column.property. For example,SELECT t.obj.x FROM ? tworks ondevelopand 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,
resolveDatabaseColumnsinsrc/40select.jsruns once on the parsed query, including nested SELECTs. It replaces(db.t1) -> idwith a realyy.Column(databaseid: 'db',tableid: 't1',columnid: 'id') when all of these hold:dbis an existing databasedbis not the name of a source in the query or an enclosing onet1is a table of databasedbin the query or an enclosing one, without a different aliasAfter that the reference goes through the normal column code. Naming, functions,
GROUP BYand correlated subqueries behave as they do fort1.id. The grammar is unchanged.Otherwise:
dbstill makes italias.column.property, a property lookup as before.Table db.t1 has the alias x in this query, so use x.id instead of db.t1.id.t1from another database givesCannot resolve db.t1.id: t1 in this query is not a table of database db.Cannot resolve db.t1.id: table db.t1 is not used in this query.The cases from the review:
developSELECT db.t1.id AS i FROM other.t1t1is not a table of databasedbSELECT db.t1.id FROM db.t1[{id: 1}, {id: 2}]SELECT UPPER(db.t1.v) AS u FROM db.t1[{u: 'A'}, {u: 'B'}]... WHERE EXISTS (SELECT * FROM db.t2 WHERE db.t2.id = db.t1.id)[{id: 1}]SELECT db.t1.id AS i FROM db.t1 AS xx.idTests
test/test2186.jscovers:ON, theSELECTlist,WHEREandORDER BY, andUSE <db>GROUP BYalias.column.property, including a source named like a databasedevelop. The 2alias.column.propertychecks pass on both.developplus this PR (bun test, same command asyarn test-only): 2566 pass, 213 skip. The only failure is CLI test 10 intest2149.js, which fails the same way on unmodifieddevelopin 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