Repository navigation
Description
What happens today
The parser collects subqueries into lists of the statement, not of the SELECT they are written in. The generated code finds a subquery by its position in such a list. A place that compiles a SELECT on its own needs the list handed to it, and where no hand-over exists the query throws.
- In
src/alasqlparser.jison, a scalar subquery is pushed ontoyy.queries(queriesidxcounts from 1). IN, NOT IN and ALL/ANY get their own list throughSubqueryStartandyy.queriesStack(counts from 0). A set-operation branch gets its own list through eight pushes inUnionOpand the pop inUnionableSelect. EXISTS goes ontoyy.exists, and every SELECT rule copies that whole list ($$.exists = yy.exists.slice()). Select.compileQueries(src/select/select.js) compilesthis.queries, andshareQueriesfnhands the list down.compileWhereExistscompilesexistsfn. More hand-overs are inalasql.precompile(src/select/exists.js, for CREATE VIEW),subqueryDatafn(src/select/from.js) andInsert.compile(src/statements/insert.js).- The readers are
this.queriesfn[queriesidx - 1](Select.toJS,src/graph/create.js,src/graph/search.js,src/statements/insert.js),this.queriesfn[queriesidx](IN, NOT IN inOp.toJS),this.query.queriesfn[queriesidx](ALL, SOME, ANY) andthis.existsfn[existsidx](ExistsValue.toJS).
This also fixes #3132, #3097 and #3133. In the dialect test, 74 entries of test/dialect/expected-differences stop at this.queriesfn[0].
What to change
- Open and close a list per SELECT. Add
yy.openQueries(), which pushes the currentyy.queriesontoyy.queriesStackand starts an empty one, andyy.closeQueries(select), which gives the list to the SELECT when it is not empty and pops the outer one. Put them next toyy.notOutsideinsrc/expression/expression.jsor insrc/engine/yy.js. CallopenQueriesin the actions ofSelectModifier. CallcloseQueriesin the first alternative ofSelectand inSelectWithoutOrderOrLimit. - Remove the eight pushes in
UnionOp, the pops inUnionableSelect,SubqueryStartand its pops in the IN, NOT IN and ALL/ANY rules. These rules and the scalar rule only push their SELECT ontoyy.queriesand keep its position. - EXISTS uses the same list.
ExistsValuepushes ontoyy.queries, andExistsValue.toJSreadsthis.queriesfn[i](params, null, ctx).data.length. Removeyy.exists, the$$.exists = ... slice()copies,compileWhereExists,existsfn(insrc/select/select.js,copyQuery,src/statements/update.js,delete.js,insert.js,set.js,alasql.precompile) and theexistscopies inIf,WhileandCreateTrigger. compileQueriescompiles onlythis.queries. RemoveshareQueriesfn, the block that compilesq.queriesof a nested query, the CREATE VIEW case inalasql.precompile, and the hand-overs insubqueryDatafnandInsert.compile.isCorrelatedstays.- Subqueries outside any SELECT stay in
yy.queriesbelow the SELECT lists, andAStatementgives them to the statement. These are UPDATE SET and WHERE, DELETE WHERE, INSERT VALUES,SET @v = (SELECT ...), IF and WHILE conditions, and PRINT.AStatementmust not overwrite the list of a statement that is a SELECT. Remove thequeriescopy of a nested SELECT inIf,WhileandCreateTrigger.- In
LPAR Select RPAR UnionClause OrderClause LimitClause, the inner SELECT is closed before the outer ORDER BY is parsed, but it compiles that ORDER BY. Move the last entries of the current list (the ORDER BY subqueries) to the inner SELECT's list with new positions.
- Positions may count from 0 everywhere. If so, change the
- 1in the readers in the same pull request. Keepthisandthis.queryas each reader has them.
If #3107 is merged first, remove its hand-over to EXISTS subqueries and keep test/test3097.js passing.
If one pull request is too large, split it in two. First every SELECT owns its scalar, IN and ALL/SOME subqueries and the hand-overs go. Then EXISTS joins the same list, and yy.exists, existsfn and the doubled compiles go.
What stays the same
- Every result, column name and error text, except queries that throw today because a subquery cannot find its list (A scalar or IN subquery anywhere in a WITH statement throws a TypeError #3132, An IN subquery inside an EXISTS subquery throws a TypeError #3097, An IN subquery inside an EXISTS subquery throws a TypeError #3097, the dialect test entries above).
- A correlation written without a table name (
WHERE zg = g) still gives a wrong value (Correlated subquery with an unqualified outer column returns no rows #2644). - The queries of A subquery before IN (SELECT ...) is evaluated as the wrong subquery #2707 keep SQLite's answers.
- These keep working, and the new test file runs them. Nested scalar subqueries, a scalar subquery in a subquery in FROM, a view with scalar, IN and EXISTS subqueries, a subquery in a UNION branch, a subquery in ON (LEFT JOIN with a subquery in ON throws a TypeError #2795), outer subqueries of IN and ALL (A JOIN subquery with its own subquery, next to a subquery in the SELECT list, returns wrong values #2879),
(SELECT a FROM A WHERE a IN (SELECT 1)) ORDER BY a * (SELECT -1),IF (SELECT COUNT(*) FROM A) > 0 SELECT ...with a subquery in the nested SELECT, UPDATE and DELETE with subqueries in WHERE, andSET @x = (SELECT MAX(a) FROM A).
Done when
- A new test file named after this issue covers scalar, IN, NOT IN, ALL and EXISTS subqueries in the select list, WHERE, HAVING and ON. Each is also placed inside EXISTS, a subquery in FROM, a UNION branch, a view, a WITH (in the WITH part and in the main SELECT) and three levels deep. Results are checked against SQLite.
- A test counts the calls of
Select.compilefor a statement with 12 EXISTS conditions and expects one per SELECT. shareQueriesfn,existsfn,yy.existsandSubqueryStartno longer exist, andqueriesidxis a position in the SELECT's own list.yarn jisonhas regeneratedsrc/alasqlparser.js,yarn test-parserpasses, and jison prints no more grammar conflicts than before.- The dialect test entries that now match SQLite are removed with
bun test/dialect/run.js --update. - The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.
Fixes
Depends on
Nothing, can start now.
Activity
mathiasrw commentedon Oct 20, 2026
This is step 1 of the subquery design in #2717.
[-]Review #2518 for code improvements: nested SELECTs borrow the statement's subquery list[/-][+]Give each SELECT its own list of subqueries from parsing on[/+]