You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
An IN subquery inside an EXISTS subquery throws a TypeError #3097
An IN (SELECT ...) inside an EXISTS (SELECT ...) throws, also when nothing is correlated.
vardb=newalasql.Database();db.exec('CREATE TABLE t (id INT, g INT); CREATE TABLE w (wn INT); CREATE TABLE z (zn INT, zg INT)');db.exec('INSERT INTO t VALUES (1,1),(2,2),(3,3); INSERT INTO w VALUES (5); INSERT INTO z VALUES (5,1),(5,2)');db.exec('SELECT id FROM t WHERE EXISTS (SELECT 1 FROM w WHERE w.wn IN (SELECT z.zn FROM z))');// TypeError: Cannot read properties of undefined (reading '0')// SQLite: 1, 2, 3
The same happens with the EXISTS in the select list, in JOIN ... ON, and with a correlated inner subquery (WHERE z.zg = t.g). It throws the same way on 4.6.0, 4.20.0, 4.21.0 and on main (e534707), so it is not a regression. The dialect test lists 12 of these cases in test/dialect/expected-differences.
The inner subquery is looked up by its position in the statement's subquery list, and the EXISTS subquery does not have that list (#2666, #2717). #2879 is the same cause with another shape.
Reproduced on 4.23.1 (latest npm release), same TypeError: Cannot read properties of undefined (reading '0') from your snippet. The plain IN on its own and the plain EXISTS on its own both work — it's specifically the IN nested inside the EXISTS that throws. Your correlated variant (WHERE z.zg = t.g) and the select-list EXISTS throw the same way.
The stack trace points at the EXISTS subquery's compiled wherefn, and the mechanism matches what you described: alasql.precompile compiles statement.exists into existsfn separately from statement.queries, but only the queries path in Select.compile runs the shareQueriesfn step that hands the compiled subquery list down the tree (there's even a comment there naming exactly this error for multi-level scalar subqueries). So when the inner IN subquery's generated code evaluates this.queriesfn[queriesidx - 1], the EXISTS subquery's query object never got a queriesfn at all.
Looks like the fix is to run the same sharing for the existsfn compilations — or to make sure an EXISTS subquery's own nested queries get compiled into its queriesfn the way a plain subquery's do. Either way, this isn't a corner case in the dialect tests only: any IN/ALL/ANY inside EXISTS hits it, correlated or not.
I would like to work on this. I reproduced it on 4.23.1 (see my comment above) — the plain IN and the plain EXISTS both work on their own, it is specifically the IN nested inside the EXISTS that throws, including the correlated variant and the select-list EXISTS. I will put together a fix with a regression test.
Activity
abhi478jeetur-rgb commentedon Nov 22, 2026
Reproduced on 4.23.1 (latest npm release), same
TypeError: Cannot read properties of undefined (reading '0')from your snippet. The plainINon its own and the plainEXISTSon its own both work — it's specifically theINnested inside theEXISTSthat throws. Your correlated variant (WHERE z.zg = t.g) and the select-listEXISTSthrow the same way.The stack trace points at the
EXISTSsubquery's compiledwherefn, and the mechanism matches what you described:alasql.precompilecompilesstatement.existsintoexistsfnseparately fromstatement.queries, but only thequeriespath inSelect.compileruns theshareQueriesfnstep that hands the compiled subquery list down the tree (there's even a comment there naming exactly this error for multi-level scalar subqueries). So when the innerINsubquery's generated code evaluatesthis.queriesfn[queriesidx - 1], theEXISTSsubquery's query object never got aqueriesfnat all.Looks like the fix is to run the same sharing for the
existsfncompilations — or to make sure anEXISTSsubquery's own nestedqueriesget compiled into itsqueriesfnthe way a plain subquery's do. Either way, this isn't a corner case in the dialect tests only: anyIN/ALL/ANYinsideEXISTShits it, correlated or not.abhi478jeetur-rgb commentedon Nov 22, 2026
I would like to work on this. I reproduced it on 4.23.1 (see my comment above) — the plain
INand the plainEXISTSboth work on their own, it is specifically theINnested inside theEXISTSthat throws, including the correlated variant and the select-listEXISTS. I will put together a fix with a regression test.