Skip to content

Fix UNION with nested SELECT - #2518

Open
pratyushsinghal7 wants to merge 1 commit into
AlaSQL:developfrom
pratyushsinghal7:issue-1264
Open

Fix UNION with nested SELECT#2518
pratyushsinghal7 wants to merge 1 commit into
AlaSQL:developfrom
pratyushsinghal7:issue-1264

Conversation

@pratyushsinghal7

Copy link
Copy Markdown

Fixes #1264

Summary

Queries like SELECT * FROM (SELECT *, (SELECT COUNT(*) FROM ?) AS c FROM ? UNION CORRESPONDING SELECT * FROM ?) crashed with Cannot read property '0' of undefined, as did scalar subqueries nested inside another subquery's WHERE clause.

The parser hoists every parenthesized scalar subquery into a single statement-level queries list, and the compiled expressions reference this.queriesfn[queriesidx - 1] on their own query object — but only the outermost query ever received the compiled list, so any subquery reference evaluated inside a FROM subselect or inside another subquery found this.queriesfn undefined.

  • share the compiled statement-level queriesfn list with nested compiled queries in compileQueries and with FROM subselects, so queriesidx references resolve at any nesting depth
  • treat a FROM subselect's column list as unknown when it was compiled with dirtyColumns (e.g. SELECT * over parameter data), so the outer SELECT * expands from the data instead of projecting only the statically known columns
  • add regression coverage for the queries from the issue

Testing

  • bun test --preload ./test/bun-setup.js ./test/test1264.js (6 passed)
  • bun run test-only (2194 passed, 213 skipped, 0 failed)
  • bun x prettier --check src/40select.js src/420from.js test/test1264.js

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.

UNION fails with nested SELECT: Cannot read property '0' of undefined

1 participant