Skip to content

Compile the subqueries in the selectors of SEARCH to close #3180 - #3185

Open
MikeFTV wants to merge 2 commits into
AlaSQL:mainfrom
MikeFTV:fix/search-subquery-in-where
Open

MikeFTV wants to merge 2 commits into
AlaSQL:mainfrom
MikeFTV:fix/search-subquery-in-where

Conversation

@MikeFTV

@MikeFTV MikeFTV commented Nov 28, 2026

Copy link
Copy Markdown

Why

A subquery in a selector of SEARCH throws instead of running (#3180):

alasql('SEARCH / WHERE(k IN (SELECT k FROM u)) k FROM t');        // TypeError: Cannot read properties of undefined (reading '0')
alasql('SEARCH / WHERE(k = (SELECT MIN(k) FROM u)) k FROM t');    // same

The parser puts the subquery on the list of the statement, but Search.compile never compiled that list, so the code of the subquery found this.queriesfn undefined. DELETE, UPDATE and SET already compile it this way.

What changes

  • Search.compile compiles this.queries into this.queriesfn, as Delete.compile does.
  • #doSearch calls each selector with a this that holds queriesfn and the params of the run, and the selectors that compile an expression (EQ, LIKE, WHERE, EX, RETURN, SET, ROW) call that expression with it. The callback of RETURN becomes an arrow function so it keeps this.
  • Not covered: = ANY (SELECT ...) and = ALL (SELECT ...) read this.query.queriesfn, and fail the same way in DELETE FROM t WHERE k = ANY (SELECT ...) on main. That is a separate gap and I left it alone.

Tests

test/test3180.js, 8 cases: IN, NOT IN, scalar subquery, EXISTS (true and false), a ? parameter read by the subquery, EX and RETURN, two subqueries in one SEARCH, and a SEARCH without a subquery.

  • bun test --preload ./test/bun-setup.js ./test/test3180.js: 8 pass, 0 fail.
  • Same command with src/ stashed: 1 pass, 7 fail.
  • bun test --dots --preload ./test/bun-setup.js ./test/test[0-9]*.js ./test/test-*.js: 3005 pass, 213 skip, 1 fail. The failure is test2149 (CLI -v); I built with node build.js and skipped the PACKAGE_VERSION replacement of yarn build-only (bun was not installed). After running that replacement, test2149 passes 15/15.
  • prettier --list-different on the changed files: clean.

Closes #3180

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.

SEARCH with a subquery in WHERE throws a TypeError

1 participant