Repository navigation
Description
Several bugs come from where a query keeps the state of one run and from having separate synchronous and asynchronous code paths. Values that belong to one run (parameters used as LIMIT, subquery caches) are written to the compiled statement that the cache shares between runs, and each kind of source (tables, ?, subqueries, files) reads its rows and reports its errors in its own way.
What this issue is about
This issue asks for a change in structure, not for a fix of each bug listed below. The bugs show where the current structure fails, and they serve as test cases. Three changes, in this order.
-
A run context. A compiled statement does not change after it is compiled. Each run gets its own object that carries the parameters, the database, the outer row of a subquery, the caches of that run and the callback, and it is passed down instead of being written onto the statement.
const ctx = {params, db, outer: null, caches: new Map(), cb}; plan.run(ctx); // nothing is written to plan
-
One source interface. Tables,
?, subqueries, files and table functions such asRANGEall give their rows the same way, and a file format only parses text into rows. Reading, errors and async handling are written once.interface Source { columns: Column[] | null; // null when known only once rows arrive open(ctx): Iterable<Row> | AsyncIterable<Row>; }
-
One runner for sync and async. When every source is synchronous the runner iterates, otherwise it awaits, along the same path. An error always reaches the caller, so a promise always settles. The cache key is built from the SQL text and every option that affects compiling.
Before you start
#2596 (two runs of the same statement at once) is the first part of this issue and comes first. It has its own three steps: find every write to a cached statement with Object.freeze in a test build, give each run its own object, add the tests it lists.
Ground rules
- Results stay as in 4.x. Only the bugs a pull request names may change results.
- Small steps. A good first pull request introduces the run context and moves the values that depend on parameters into it:
query.limitandquery.offset(Give each run of a statement a run context for its parameters, LIMIT, OFFSET and subquery caches #2667),subqueryCacheandself.params, with a test that runs the same SQL twice, overlapping, with different parameters. The source interface follows, file sources first (Give file sources one source interface that opens rows for a run and reports errors in one place #2677). - Each pull request passes the full test suite and the parity check in
test/parity/against the build before it. - The code this touches moves to ES6 as part of the work, for example iterables in place of counting callbacks.
Bugs traced to this
- Run state on the cached statement: Cached statements hold per-run state, so two runs of the same SQL at once overwrite each other #2596, Set operation with ORDER BY / LIMIT on the first SELECT gives a different result from the second run #2605, Review #2534 for code improvements: parameters for subqueries in UPDATE #2632, Review #2281 for code improvements: IN subquery caching decided per row #2645, Give each run of a statement a run context for its parameters, LIMIT, OFFSET and subquery caches #2667
- Options left out of the cache key: Run a statement through one runner so every error reaches the caller, and key the statement cache on every compile option #2652
- Synchronous and asynchronous paths that differ: Read a subquery in JOIN the same way as in FROM, so it can read a file #2676, Run UPDATE, DELETE and INSERT on a parameter array without a temporary table #2682, Let BREAK and CONTINUE work in a WHILE loop run asynchronously #2701
- File sources that report errors differently: Fix JSON file error handling #2513, Give file sources one source interface that opens rows for a run and reports errors in one place #2677
Activity