Skip to content

An IN subquery inside an EXISTS subquery throws a TypeError #3097

Open

Description

@mathiasrw

An IN (SELECT ...) inside an EXISTS (SELECT ...) throws, also when nothing is correlated.

var db = new alasql.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.

Activity

  1. abhi478jeetur-rgb commented on Nov 22, 2026

    @abhi478jeetur-rgb
    Contributor

    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.

  2. abhi478jeetur-rgb commented on Nov 22, 2026

    @abhi478jeetur-rgb
    Contributor

    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.

  3. added this to the v4.x Focus milestone on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions