Skip to content

Give each run of a statement a run context for its parameters, LIMIT, OFFSET and subquery caches #2667

Open
0 of 3 issues completed

Description

@mathiasrw

What happens today

A compiled statement is shared by the statement cache and by alasql.compile(). Some values of one run are still written to shared objects.

  • The statement function from compileInScope (src/select/select.js) writes run.limit and run.offset from params. unionBranchHasOrder in queryfn3 (src/select/queryfn.js) reads fn.query.limit on the compiled branch, so it does not see a parameter LIMIT.
  • queryfn sets query.subqueryCache = {} on each run, and the code for IN (SELECT ...) and NOT IN (SELECT ...) in src/expression/expression.js uses this.subqueryCache. In UPDATE and DELETE, wherefn is bound to the parsed statement (src/statements/update.js, src/statements/delete.js), so the cache lives on the parsed node and is never cleared.
  • src/statements/update.js writes self.params = params on the parsed statement, so that the subquery call this.queriesfn[i](this.params, null, r) (Select.toJS, src/statements/insert.js) finds the parameters. DELETE and INSERT do not set it.

This also fixes #3137, #3138 and #3139.

What to change

Add one run context per run of a statement and pass it down. Nothing of a run is written to the compiled query or to the parsed statement.

  1. Add src/engine/runcontext.js with a class, not Object.create.

    export class RunContext {
      constructor(params, db, cb) {
        this.params = params;
        this.db = db;
        this.outer = null;     // filled by later steps
        this.caches = new Map(); // subquery results of this run
        this.cb = cb;
        this.limit = undefined;
        this.offset = undefined;
      }
    }
  2. SELECT. In the statement function in src/select/select.js, create the context and put it on the run copy as run.ctx. Add ctx to copyQuery. Resolve limitParam and offsetParam into ctx.limit and ctx.offset, and stop writing run.limit and run.offset. doLimit in src/select/queryfn.js reads ctx.limit and ctx.offset when the query has a limitParam or offsetParam, and query.limit and query.offset otherwise. Do the same where queryfn3 asks whether the first SELECT has a LIMIT. unionBranchHasOrder counts a branch as having a LIMIT when fn.query.limit or fn.query.limitParam is set. Remove query.subqueryCache = {} from queryfn.

  3. IN and NOT IN. In src/expression/expression.js, the generated cached lookup uses this.ctx.caches, a Map keyed by 'in' + queriesidx or 'notIn' + queriesidx, instead of this.subqueryCache. Keep the per-row isCorrelated test.

  4. Subquery parameters. In Select.toJS and Insert.toJS, pass the params argument that every generated row function has, instead of this.params. Where a generated function has no params argument (the ORDER BY comparator in src/select/orderby.js, the graph code in src/graph/), keep this.params.

  5. UPDATE, DELETE and INSERT. Remove self.params = params from src/statements/update.js. Stop binding wherefn and the INSERT insertfn to the parsed statement. Each statement function builds one run object of a fixed shape and calls the generated function with it.

    const run = {queriesfn: self.queriesfn, existsfn: self.existsfn, ctx: new RunContext(params, db, cb)};
    if (!wherefn || wherefn.call(run, table.data[i], params, alasql)) { ... }

What stays the same

Done when

  • A new test file named after this issue covers the three bugs, each run twice with different parameters.
  • The same file runs an overlapping pair of the same SELECT with LIMIT ? OFFSET ?, once through a user function that calls the same SQL inside the outer run, and once through two alasql.promise calls over the SLOW source from test/test2596.js. Each run gets its own LIMIT and OFFSET. Both cases pass on main today.
  • After a run, neither statement.query nor the parsed UPDATE, DELETE or INSERT node has a subqueryCache or params property.
  • test/performance/ shows no slowdown on Node for a correlated subquery.
  • 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 #3137, #3138, #3139.

Depends on

Nothing, can start now.

Sub-issues

Sub-issues

0 of 3 Issues completed
Loading 3 sub-issues

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions