Skip to content

Give each SELECT its own list of subqueries from parsing on #2666

Open
0 of 3 issues completed
Open

Description

@mathiasrw

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 onto yy.queries (queriesidx counts from 1). IN, NOT IN and ALL/ANY get their own list through SubqueryStart and yy.queriesStack (counts from 0). A set-operation branch gets its own list through eight pushes in UnionOp and the pop in UnionableSelect. EXISTS goes onto yy.exists, and every SELECT rule copies that whole list ($$.exists = yy.exists.slice()).
  • Select.compileQueries (src/select/select.js) compiles this.queries, and shareQueriesfn hands the list down. compileWhereExists compiles existsfn. More hand-overs are in alasql.precompile (src/select/exists.js, for CREATE VIEW), subqueryDatafn (src/select/from.js) and Insert.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 in Op.toJS), this.query.queriesfn[queriesidx] (ALL, SOME, ANY) and this.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

  1. Open and close a list per SELECT. Add yy.openQueries(), which pushes the current yy.queries onto yy.queriesStack and starts an empty one, and yy.closeQueries(select), which gives the list to the SELECT when it is not empty and pops the outer one. Put them next to yy.notOutside in src/expression/expression.js or in src/engine/yy.js. Call openQueries in the actions of SelectModifier. Call closeQueries in the first alternative of Select and in SelectWithoutOrderOrLimit.
  2. Remove the eight pushes in UnionOp, the pops in UnionableSelect, SubqueryStart and its pops in the IN, NOT IN and ALL/ANY rules. These rules and the scalar rule only push their SELECT onto yy.queries and keep its position.
  3. EXISTS uses the same list. ExistsValue pushes onto yy.queries, and ExistsValue.toJS reads this.queriesfn[i](params, null, ctx).data.length. Remove yy.exists, the $$.exists = ... slice() copies, compileWhereExists, existsfn (in src/select/select.js, copyQuery, src/statements/update.js, delete.js, insert.js, set.js, alasql.precompile) and the exists copies in If, While and CreateTrigger.
  4. compileQueries compiles only this.queries. Remove shareQueriesfn, the block that compiles q.queries of a nested query, the CREATE VIEW case in alasql.precompile, and the hand-overs in subqueryDatafn and Insert.compile. isCorrelated stays.
  5. Subqueries outside any SELECT stay in yy.queries below the SELECT lists, and AStatement gives them to the statement. These are UPDATE SET and WHERE, DELETE WHERE, INSERT VALUES, SET @v = (SELECT ...), IF and WHILE conditions, and PRINT.
    • AStatement must not overwrite the list of a statement that is a SELECT. Remove the queries copy of a nested SELECT in If, While and CreateTrigger.
    • 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.
  6. Positions may count from 0 everywhere. If so, change the - 1 in the readers in the same pull request. Keep this and this.query as 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

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.compile for a statement with 12 EXISTS conditions and expects one per SELECT.
  • shareQueriesfn, existsfn, yy.exists and SubqueryStart no longer exist, and queriesidx is a position in the SELECT's own list.
  • yarn jison has regenerated src/alasqlparser.js, yarn test-parser passes, 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

Fixes #3132, #3097, #3133.

Depends on

Nothing, can start now.

Sub-issues

Sub-issues

0 of 3 Issues completed
Loading 3 sub-issues

Activity

  1. mathiasrw commented on Oct 20, 2026

    @mathiasrw
    MemberAuthor

    This is step 1 of the subquery design in #2717.

  2. added this to the v4.x Focus milestone on Nov 26, 2026
  3. changed the title [-]Review #2518 for code improvements: nested SELECTs borrow the statement's subquery list[/-] [+]Give each SELECT its own list of subqueries from parsing on[/+] on Nov 26, 2026
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