Skip to content

fix: resolve IN subquery inside EXISTS subquery - #3107

Open
abhi478jeetur-rgb wants to merge 1 commit into
AlaSQL:developfrom
abhi478jeetur-rgb:fix/in-subquery-in-exists
Open

abhi478jeetur-rgb wants to merge 1 commit into
AlaSQL:developfrom
abhi478jeetur-rgb:fix/in-subquery-in-exists

Conversation

@abhi478jeetur-rgb

Copy link
Copy Markdown
Contributor

An IN (SELECT ...) nested inside an EXISTS (SELECT ...) threw
TypeError: Cannot read properties of undefined (reading '0'), even
when nothing was correlated:

SELECT id FROM t
WHERE EXISTS (SELECT 1 FROM w WHERE w.wn IN (SELECT z.zn FROM z));

The inner IN subquery is compiled into the statement-level
queriesfn, but the EXISTS subquery is compiled separately
(compileWhereExists) and never received that list, so its generated
code failed when it evaluated this.queriesfn[queriesidx]. This extends
the existing shareQueriesfn step in compileQueries — which already
hands the list down to nested scalar subqueries — to the EXISTS
subqueries as well, including EXISTS nested inside another EXISTS
and EXISTS inside a UNION branch (which keeps its own subquery
list). No other compilation path is touched.

Fixes #3097

Tests

  • New regression test test/test3097.js (7 tests): the reported query,
    EXISTS in the select list, EXISTS in JOIN ... ON, the correlated
    variant (WHERE z.zg = t.g), a doubly nested IN inside EXISTS,
    the UNION ALL variant, and plain EXISTS / plain IN sanity checks.
    Before the fix: 1 pass / 6 fail; after: 7 pass.
  • Full suite (sh build.sh && bun test): 2673 pass, 213 skip, 0 fail.
  • npx prettier --check clean on both changed files.

Dialect test note

bun test/dialect/run.js (not run by the default suite) now reports 48
cases newly matching SQLite across the EXISTS positions in the
subqueries matrix (select list, WHERE, NOT EXISTS, HAVING,
GROUP BY, JOIN ... ON, and their UNION ALL variants). Of the 12
cases the issue mentions in test/dialect/expected-differences, 8 now
return SQLite's answer. The remaining 4 use an unqualified correlation
(WHERE wg = g / WHERE zg = g); they no longer throw, but still return
the wrong value — the pre-existing unqualified-column correlation bug,
the same one the already-listed plain EXISTS ... WHERE ug = g cases
show. expected-differences itself is left untouched for the
maintainer's own regeneration (it already had 46 stale entries on
develop before this change).

@mathiasrw

mathiasrw commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

The default branch has been changed to main after we transitioned to having the source as es6 modules.

Please change the target branch to main and consider the new file structure.

@abhi478jeetur-rgb

Copy link
Copy Markdown
Contributor Author

Thanks for the note — I will retarget this to main and adapt the fix to the new module structure.

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.

2 participants