Repository navigation
Description
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) writesrun.limitandrun.offsetfromparams.unionBranchHasOrderinqueryfn3(src/select/queryfn.js) readsfn.query.limiton the compiled branch, so it does not see a parameter LIMIT. queryfnsetsquery.subqueryCache = {}on each run, and the code forIN (SELECT ...)andNOT IN (SELECT ...)insrc/expression/expression.jsusesthis.subqueryCache. In UPDATE and DELETE,wherefnis 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.jswritesself.params = paramson the parsed statement, so that the subquery callthis.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.
-
Add
src/engine/runcontext.jswith a class, notObject.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; } }
-
SELECT. In the statement function in
src/select/select.js, create the context and put it on the run copy asrun.ctx. AddctxtocopyQuery. ResolvelimitParamandoffsetParamintoctx.limitandctx.offset, and stop writingrun.limitandrun.offset.doLimitinsrc/select/queryfn.jsreadsctx.limitandctx.offsetwhen the query has alimitParamoroffsetParam, andquery.limitandquery.offsetotherwise. Do the same wherequeryfn3asks whether the first SELECT has a LIMIT.unionBranchHasOrdercounts a branch as having a LIMIT whenfn.query.limitorfn.query.limitParamis set. Removequery.subqueryCache = {}fromqueryfn. -
IN and NOT IN. In
src/expression/expression.js, the generated cached lookup usesthis.ctx.caches, aMapkeyed by'in' + queriesidxor'notIn' + queriesidx, instead ofthis.subqueryCache. Keep the per-rowisCorrelatedtest. -
Subquery parameters. In
Select.toJSandInsert.toJS, pass theparamsargument that every generated row function has, instead ofthis.params. Where a generated function has noparamsargument (the ORDER BY comparator insrc/select/orderby.js, the graph code insrc/graph/), keepthis.params. -
UPDATE, DELETE and INSERT. Remove
self.params = paramsfromsrc/statements/update.js. Stop bindingwherefnand the INSERTinsertfnto 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
- Every result, column name and error text, except UPDATE and DELETE with an IN subquery reuse the subquery result of the first run on later runs #3137, DELETE and INSERT VALUES throw a TypeError when a scalar subquery reads a parameter #3138 and A LIMIT parameter on a parenthesised part of a set operation is not applied to that part #3139.
- The per-run copy of Refactor: Each run of a compiled SELECT gets its own state #3106 (
copyQuery,copySource,newRun). - The subquery cache lives as long as one call of a statement function.
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 twoalasql.promisecalls over theSLOWsource fromtest/test2596.js. Each run gets its own LIMIT and OFFSET. Both cases pass on main today. - After a run, neither
statement.querynor the parsed UPDATE, DELETE or INSERT node has asubqueryCacheorparamsproperty. 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
Depends on
Nothing, can start now.
Activity
[-]Review #2517 for code improvements: LIMIT and OFFSET parameters written into the compiled query[/-][+]Give each run of a statement a run context for its parameters, LIMIT, OFFSET and subquery caches[/+]