Skip to content

A correlated subquery that is a UNION throws a TypeError #2856

Closed

Description

@mathiasrw

A correlated subquery that is a UNION throws a TypeError when a branch refers to the outer table. The same subquery without UNION works.

Seen in the MySQL server tests run (alasql-mysqltest): 84 statements in 10 files (subquery_all.test and the files that include subquery.inc), for example select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2. Reproduced with alasql 4.21.0:

alasql('CREATE TABLE t1 (a INT)');
alasql('INSERT INTO t1 VALUES (1), (2), (3)');
alasql('CREATE TABLE t2 (a INT, c INT)');
alasql('INSERT INTO t2 VALUES (1, 5), (2, 6)');
alasql('SELECT a, (SELECT c FROM t2 WHERE t2.a = t1.a) AS x FROM t1');   // works
alasql('SELECT a, (SELECT c FROM t2 WHERE t2.a = t1.a UNION SELECT c FROM t2 WHERE t2.a = t1.a) AS x FROM t1');
// TypeError: undefined is not an object (evaluating 'p['t1']['a']')
alasql('SELECT a FROM t1 WHERE EXISTS (SELECT c FROM t2 WHERE c = 1 UNION SELECT c FROM t2 WHERE t2.a = t1.a)');
// the same TypeError

Expected: x is 5, 6 and NULL; the EXISTS query gives 1 and 2.

Breaking?

No. Today it throws.

Related: #2644, #2718 (set operations as a tree).

Activity

  1. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I'd like to work on this one.

  2. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I've opened a PR for this: #2990

  3. added this to the v4.x Focus milestone on Nov 15, 2026
  4. added a commit that references this issue on Nov 16, 2026
    baa4334
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