squirreling 0.12.27 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "squirreling",
3
- "version": "0.12.27",
3
+ "version": "0.13.0",
4
4
  "description": "Squirreling Async SQL Engine",
5
5
  "author": "Hyperparam",
6
6
  "homepage": "https://hyperparam.app",
@@ -42,7 +42,7 @@
42
42
  "@types/node": "26.1.0",
43
43
  "@vitest/coverage-v8": "4.1.9",
44
44
  "eslint": "9.39.4",
45
- "eslint-plugin-jsdoc": "63.0.10",
45
+ "eslint-plugin-jsdoc": "63.0.11",
46
46
  "typescript": "6.0.3",
47
47
  "vitest": "4.1.9"
48
48
  }
@@ -101,7 +101,7 @@ export function executeHashAggregate(plan, context) {
101
101
  for await (const row of child.rows()) {
102
102
  if (++collectCount % YIELD_INTERVAL === 0) {
103
103
  await yieldToEventLoop()
104
- if (context.signal?.aborted) return
104
+ context.signal?.throwIfAborted()
105
105
  }
106
106
  allRows.push(row)
107
107
  }
@@ -121,7 +121,7 @@ export function executeHashAggregate(plan, context) {
121
121
  for (let chunkStart = 0; chunkStart < allRows.length; chunkStart += YIELD_INTERVAL) {
122
122
  if (chunkStart > 0) {
123
123
  await yieldToEventLoop()
124
- if (context.signal?.aborted) return
124
+ context.signal?.throwIfAborted()
125
125
  }
126
126
  const chunkEnd = Math.min(chunkStart + YIELD_INTERVAL, allRows.length)
127
127
  const chunkLen = chunkEnd - chunkStart
@@ -227,7 +227,7 @@ export function executeScalarAggregate(plan, context) {
227
227
  for await (const row of child.rows()) {
228
228
  if (++collectCount % YIELD_INTERVAL === 0) {
229
229
  await yieldToEventLoop()
230
- if (context.signal?.aborted) return
230
+ context.signal?.throwIfAborted()
231
231
  }
232
232
  group.push(row)
233
233
  }
@@ -174,7 +174,7 @@ function executeUnnest(plan, context) {
174
174
  const value = await evaluateExpr({ node: plan.args[0], row, rowIndex: 1, context })
175
175
  if (!Array.isArray(value)) return
176
176
  for (const element of value) {
177
- if (context.signal?.aborted) return
177
+ context.signal?.throwIfAborted()
178
178
  yield {
179
179
  columns,
180
180
  cells: { [columnName]: () => Promise.resolve(element) },
@@ -209,7 +209,7 @@ function executeJsonEach(plan, context) {
209
209
  }
210
210
  if (Array.isArray(parsed)) {
211
211
  for (let i = 0; i < parsed.length; i++) {
212
- if (context.signal?.aborted) return
212
+ context.signal?.throwIfAborted()
213
213
  const k = i
214
214
  const v = parsed[i]
215
215
  yield {
@@ -224,7 +224,7 @@ function executeJsonEach(plan, context) {
224
224
  }
225
225
  if (typeof parsed === 'object' && parsed !== null) {
226
226
  for (const [k, v] of Object.entries(parsed)) {
227
- if (context.signal?.aborted) return
227
+ context.signal?.throwIfAborted()
228
228
  yield {
229
229
  columns,
230
230
  cells: {
@@ -294,7 +294,7 @@ function executeScan(plan, context) {
294
294
  async *rows() {
295
295
  const columns = [column]
296
296
  for await (const chunk of chunks) {
297
- if (signal?.aborted) return
297
+ signal?.throwIfAborted()
298
298
  for (let i = 0; i < chunk.length; i++) {
299
299
  const value = chunk[i]
300
300
  yield {
@@ -303,6 +303,9 @@ function executeScan(plan, context) {
303
303
  }
304
304
  }
305
305
  }
306
+ // A data source may end its stream cooperatively on abort; surface
307
+ // the abort so a truncated scan is not mistaken for a complete one
308
+ signal?.throwIfAborted()
306
309
  },
307
310
  }
308
311
  }
@@ -335,6 +338,10 @@ function executeScan(plan, context) {
335
338
  }
336
339
 
337
340
  yield* result
341
+
342
+ // A data source may end its stream cooperatively on abort; surface
343
+ // the abort so a truncated scan is not mistaken for a complete one
344
+ signal?.throwIfAborted()
338
345
  },
339
346
  }
340
347
  }
@@ -420,10 +427,10 @@ async function* filterRows(rows, condition, context, limit) {
420
427
  let buffer = []
421
428
 
422
429
  for await (const row of rows) {
423
- if (context.signal?.aborted) return
430
+ context.signal?.throwIfAborted()
424
431
  if (++innerCount % YIELD_INTERVAL === 0) {
425
432
  await yieldToEventLoop()
426
- if (context.signal?.aborted) return
433
+ context.signal?.throwIfAborted()
427
434
  }
428
435
  rowIndex++
429
436
  buffer.push({ row, rowIndex })
@@ -466,10 +473,10 @@ async function* limitRows(rows, limit = Infinity, offset = 0, signal) {
466
473
  let yielded = 0
467
474
  let innerCount = 0
468
475
  for await (const row of rows) {
469
- if (signal?.aborted) return
476
+ signal?.throwIfAborted()
470
477
  if (++innerCount % YIELD_INTERVAL === 0) {
471
478
  await yieldToEventLoop()
472
- if (signal?.aborted) return
479
+ signal?.throwIfAborted()
473
480
  }
474
481
  if (skipped < offset) {
475
482
  skipped++
@@ -521,10 +528,10 @@ function executeProject(plan, context) {
521
528
  let innerCount = 0
522
529
 
523
530
  for await (const row of child.rows()) {
524
- if (context.signal?.aborted) return
531
+ context.signal?.throwIfAborted()
525
532
  if (++innerCount % YIELD_INTERVAL === 0) {
526
533
  await yieldToEventLoop()
527
- if (context.signal?.aborted) return
534
+ context.signal?.throwIfAborted()
528
535
  }
529
536
  rowIndex++
530
537
  const currentRowIndex = rowIndex
@@ -614,10 +621,10 @@ function executeDistinct(plan, context) {
614
621
  let innerCount = 0
615
622
 
616
623
  for await (const row of child.rows()) {
617
- if (signal?.aborted) return
624
+ signal?.throwIfAborted()
618
625
  if (++innerCount % YIELD_INTERVAL === 0) {
619
626
  await yieldToEventLoop()
620
- if (signal?.aborted) return
627
+ signal?.throwIfAborted()
621
628
  }
622
629
  buffer.push(row)
623
630
 
@@ -701,10 +708,10 @@ function executeSetOperation(plan, context) {
701
708
  const seen = new Set()
702
709
  let count = 0
703
710
  for await (const row of left.rows()) {
704
- if (signal?.aborted) return
711
+ signal?.throwIfAborted()
705
712
  if (++count % YIELD_INTERVAL === 0) {
706
713
  await yieldToEventLoop()
707
- if (signal?.aborted) return
714
+ signal?.throwIfAborted()
708
715
  }
709
716
  const key = await stableRowKey(row)
710
717
  if (!seen.has(key)) {
@@ -713,10 +720,10 @@ function executeSetOperation(plan, context) {
713
720
  }
714
721
  }
715
722
  for await (const row of right.rows()) {
716
- if (signal?.aborted) return
723
+ signal?.throwIfAborted()
717
724
  if (++count % YIELD_INTERVAL === 0) {
718
725
  await yieldToEventLoop()
719
- if (signal?.aborted) return
726
+ signal?.throwIfAborted()
720
727
  }
721
728
  const key = await stableRowKey(row)
722
729
  if (!seen.has(key)) {
@@ -739,10 +746,10 @@ function executeSetOperation(plan, context) {
739
746
  const rightKeys = new Map()
740
747
  let tick = 0
741
748
  for await (const row of right.rows()) {
742
- if (signal?.aborted) return
749
+ signal?.throwIfAborted()
743
750
  if (++tick % YIELD_INTERVAL === 0) {
744
751
  await yieldToEventLoop()
745
- if (signal?.aborted) return
752
+ signal?.throwIfAborted()
746
753
  }
747
754
  const key = await stableRowKey(row)
748
755
  rightKeys.set(key, (rightKeys.get(key) ?? 0) + 1)
@@ -751,10 +758,10 @@ function executeSetOperation(plan, context) {
751
758
  if (plan.all) {
752
759
  // INTERSECT ALL: yield each left row that matches, consuming right counts
753
760
  for await (const row of left.rows()) {
754
- if (signal?.aborted) return
761
+ signal?.throwIfAborted()
755
762
  if (++tick % YIELD_INTERVAL === 0) {
756
763
  await yieldToEventLoop()
757
- if (signal?.aborted) return
764
+ signal?.throwIfAborted()
758
765
  }
759
766
  const key = await stableRowKey(row)
760
767
  const count = rightKeys.get(key)
@@ -767,10 +774,10 @@ function executeSetOperation(plan, context) {
767
774
  // INTERSECT: yield deduplicated rows present in both
768
775
  const seen = new Set()
769
776
  for await (const row of left.rows()) {
770
- if (signal?.aborted) return
777
+ signal?.throwIfAborted()
771
778
  if (++tick % YIELD_INTERVAL === 0) {
772
779
  await yieldToEventLoop()
773
- if (signal?.aborted) return
780
+ signal?.throwIfAborted()
774
781
  }
775
782
  const key = await stableRowKey(row)
776
783
  if (rightKeys.has(key) && !seen.has(key)) {
@@ -794,10 +801,10 @@ function executeSetOperation(plan, context) {
794
801
  const rightKeys = new Map()
795
802
  let tick = 0
796
803
  for await (const row of right.rows()) {
797
- if (signal?.aborted) return
804
+ signal?.throwIfAborted()
798
805
  if (++tick % YIELD_INTERVAL === 0) {
799
806
  await yieldToEventLoop()
800
- if (signal?.aborted) return
807
+ signal?.throwIfAborted()
801
808
  }
802
809
  const key = await stableRowKey(row)
803
810
  rightKeys.set(key, (rightKeys.get(key) ?? 0) + 1)
@@ -806,10 +813,10 @@ function executeSetOperation(plan, context) {
806
813
  if (plan.all) {
807
814
  // EXCEPT ALL: yield left rows, consuming right counts
808
815
  for await (const row of left.rows()) {
809
- if (signal?.aborted) return
816
+ signal?.throwIfAborted()
810
817
  if (++tick % YIELD_INTERVAL === 0) {
811
818
  await yieldToEventLoop()
812
- if (signal?.aborted) return
819
+ signal?.throwIfAborted()
813
820
  }
814
821
  const key = await stableRowKey(row)
815
822
  const count = rightKeys.get(key)
@@ -823,10 +830,10 @@ function executeSetOperation(plan, context) {
823
830
  // EXCEPT: yield deduplicated left rows not in right
824
831
  const seen = new Set()
825
832
  for await (const row of left.rows()) {
826
- if (signal?.aborted) return
833
+ signal?.throwIfAborted()
827
834
  if (++tick % YIELD_INTERVAL === 0) {
828
835
  await yieldToEventLoop()
829
- if (signal?.aborted) return
836
+ signal?.throwIfAborted()
830
837
  }
831
838
  const key = await stableRowKey(row)
832
839
  if (!rightKeys.has(key) && !seen.has(key)) {
@@ -56,7 +56,7 @@ export function executeNestedLoopJoin(plan, context) {
56
56
  /** @type {AsyncRow[]} */
57
57
  const innerRows = []
58
58
  for await (const row of inner.rows()) {
59
- if (context.signal?.aborted) return
59
+ context.signal?.throwIfAborted()
60
60
  innerRows.push(row)
61
61
  }
62
62
 
@@ -69,7 +69,7 @@ export function executeNestedLoopJoin(plan, context) {
69
69
 
70
70
  let innerCount = 0
71
71
  for await (const outerRow of outer.rows()) {
72
- if (context.signal?.aborted) return
72
+ context.signal?.throwIfAborted()
73
73
 
74
74
  if (!outerCols) {
75
75
  outerCols = outerRow.columns
@@ -80,7 +80,7 @@ export function executeNestedLoopJoin(plan, context) {
80
80
  for (const innerRow of innerRows) {
81
81
  if (++innerCount % YIELD_INTERVAL === 0) {
82
82
  await yieldToEventLoop()
83
- if (context.signal?.aborted) return
83
+ context.signal?.throwIfAborted()
84
84
  }
85
85
  const tempMerged = merge(outerRow, innerRow)
86
86
  const matches = await evaluateExpr({
@@ -101,7 +101,7 @@ export function executeNestedLoopJoin(plan, context) {
101
101
  }
102
102
  }
103
103
 
104
- if (context.signal?.aborted) return
104
+ context.signal?.throwIfAborted()
105
105
 
106
106
  // Unmatched inner rows for outer joins on the buffered side
107
107
  if (matchedInnerRows) {
@@ -134,7 +134,7 @@ function executeLateralJoin(plan, context) {
134
134
  const rightTable = plan.rightAlias
135
135
 
136
136
  for await (const leftRow of left.rows()) {
137
- if (context.signal?.aborted) return
137
+ context.signal?.throwIfAborted()
138
138
 
139
139
  // When nested inside a correlated subquery, preserve the enclosing
140
140
  // outer row so UNNEST args can reference its columns (e.g. o.arr).
@@ -146,7 +146,7 @@ function executeLateralJoin(plan, context) {
146
146
 
147
147
  let hasMatch = false
148
148
  for await (const rightRow of right.rows()) {
149
- if (context.signal?.aborted) return
149
+ context.signal?.throwIfAborted()
150
150
  const merged = mergeRows(leftRow, rightRow, leftTable, rightTable)
151
151
  const matches = plan.condition === undefined
152
152
  ? true
@@ -199,10 +199,10 @@ export function executePositionalJoin(plan, context) {
199
199
  while (true) {
200
200
  const [leftResult, rightResult] = await Promise.all([leftRows.next(), rightRows.next()])
201
201
  if (leftResult.done && rightResult.done) return
202
- if (signal?.aborted) return
202
+ signal?.throwIfAborted()
203
203
  if (++tick % YIELD_INTERVAL === 0) {
204
204
  await yieldToEventLoop()
205
- if (signal?.aborted) return
205
+ signal?.throwIfAborted()
206
206
  }
207
207
  if (!leftResult.done && !leftCols.length) leftCols = leftResult.value.columns
208
208
  if (!rightResult.done && !rightCols.length) rightCols = rightResult.value.columns
@@ -266,10 +266,10 @@ export function executeHashJoin(plan, context) {
266
266
  let buildCols = []
267
267
  let innerCount = 0
268
268
  for await (const buildRow of build.rows()) {
269
- if (context.signal?.aborted) return
269
+ context.signal?.throwIfAborted()
270
270
  if (++innerCount % YIELD_INTERVAL === 0) {
271
271
  await yieldToEventLoop()
272
- if (context.signal?.aborted) return
272
+ context.signal?.throwIfAborted()
273
273
  }
274
274
  if (!buildCols.length) {
275
275
  buildCols = buildRow.columns
@@ -297,7 +297,7 @@ export function executeHashJoin(plan, context) {
297
297
 
298
298
  // Probe phase: stream the other side
299
299
  for await (const probeRow of probe.rows()) {
300
- if (context.signal?.aborted) return
300
+ context.signal?.throwIfAborted()
301
301
 
302
302
  if (!probeCols) {
303
303
  probeCols = probeRow.columns
@@ -314,7 +314,7 @@ export function executeHashJoin(plan, context) {
314
314
  for (const buildRow of candidates) {
315
315
  if (++innerCount % YIELD_INTERVAL === 0) {
316
316
  await yieldToEventLoop()
317
- if (context.signal?.aborted) return
317
+ context.signal?.throwIfAborted()
318
318
  }
319
319
  const merged = merge(probeRow, buildRow)
320
320
  if (residual) {
@@ -333,7 +333,7 @@ export function executeHashJoin(plan, context) {
333
333
  }
334
334
  }
335
335
 
336
- if (context.signal?.aborted) return
336
+ context.signal?.throwIfAborted()
337
337
 
338
338
  // Unmatched build rows for outer joins on the build side
339
339
  if (buildRows && matchedBuildRows) {
@@ -146,7 +146,7 @@ export function executeSort(plan, context) {
146
146
  /** @type {SortEntry[]} */
147
147
  let entries = []
148
148
  for await (const row of child.rows()) {
149
- if (context.signal?.aborted) return
149
+ context.signal?.throwIfAborted()
150
150
  entries.push({ row })
151
151
  if (entries.length >= bufferLimit) {
152
152
  // Sort keys are cached on the rows, so survivors of one truncation
@@ -409,8 +409,8 @@ async function accumulateChunk({ chunk, groupBy, specs, groups, needsRow, contex
409
409
 
410
410
  /**
411
411
  * Consumes the child rows into per-group accumulators, holding at most one
412
- * chunk of rows at a time. Returns undefined when aborted mid-collection so
413
- * callers end the row stream silently, matching the buffered path.
412
+ * chunk of rows at a time. Throws when aborted so partial accumulators are
413
+ * never finalized into results.
414
414
  *
415
415
  * @param {object} options
416
416
  * @param {QueryResults} options.child
@@ -418,7 +418,7 @@ async function accumulateChunk({ chunk, groupBy, specs, groups, needsRow, contex
418
418
  * @param {StreamingAggSpec[]} options.specs
419
419
  * @param {boolean} options.needsRow
420
420
  * @param {ExecuteContext} options.context
421
- * @returns {Promise<Map<unknown, StreamingGroup> | undefined>}
421
+ * @returns {Promise<Map<unknown, StreamingGroup>>}
422
422
  */
423
423
  async function accumulateGroups({ child, groupBy, specs, needsRow, context }) {
424
424
  /** @type {Map<unknown, StreamingGroup>} */
@@ -431,14 +431,11 @@ async function accumulateGroups({ child, groupBy, specs, needsRow, context }) {
431
431
  await accumulateChunk({ chunk, groupBy, specs, groups, needsRow, context })
432
432
  chunk = []
433
433
  await yieldToEventLoop()
434
- if (context.signal?.aborted) return
434
+ context.signal?.throwIfAborted()
435
435
  }
436
436
  }
437
437
  if (chunk.length) {
438
438
  await accumulateChunk({ chunk, groupBy, specs, groups, needsRow, context })
439
- // An abort during the final partial chunk ends the stream silently,
440
- // consistent with the full-chunk check above
441
- if (context.signal?.aborted) return
442
439
  }
443
440
  context.signal?.throwIfAborted()
444
441
  return groups
@@ -531,7 +528,6 @@ export function streamingHashAggregateRows({ plan, streaming, child, context })
531
528
  const { specs, keyRefs, needsRow } = streaming
532
529
  return async function* () {
533
530
  const groups = await accumulateGroups({ child, groupBy: plan.groupBy, specs, needsRow, context })
534
- if (!groups) return
535
531
  const { orderBy, having } = plan
536
532
 
537
533
  // Without ORDER BY, groups finalize and yield one at a time so output
@@ -585,7 +581,6 @@ export function streamingScalarAggregateRows({ plan, streaming, child, context }
585
581
  const { specs, keyRefs, needsRow } = streaming
586
582
  return async function* () {
587
583
  const groups = await accumulateGroups({ child, groupBy: [], specs, needsRow, context })
588
- if (!groups) return
589
584
  /** @type {StreamingGroup} */
590
585
  const group = groups.get(true) ?? { firstRow: undefined, keyValues: [], accumulators: specs.map(spec => newAccumulator(spec.funcName, spec.node.distinct)) }
591
586
 
@@ -42,7 +42,7 @@ export function executeWindow(plan, context) {
42
42
  for await (const row of child.rows()) {
43
43
  if (++i % YIELD_INTERVAL === 0) {
44
44
  await yieldToEventLoop()
45
- if (context.signal?.aborted) return
45
+ context.signal?.throwIfAborted()
46
46
  }
47
47
  const cells = { ...row.cells }
48
48
  for (const w of plan.windows) {
@@ -69,7 +69,7 @@ export function executeWindow(plan, context) {
69
69
  for await (const row of child.rows()) {
70
70
  if (++collectCount % YIELD_INTERVAL === 0) {
71
71
  await yieldToEventLoop()
72
- if (context.signal?.aborted) return
72
+ context.signal?.throwIfAborted()
73
73
  }
74
74
  rows.push(row)
75
75
  }
@@ -81,14 +81,14 @@ export function executeWindow(plan, context) {
81
81
 
82
82
  for (let w = 0; w < plan.windows.length; w++) {
83
83
  await computeWindow(plan.windows[w], rows, windowValues[w], context)
84
- if (context.signal?.aborted) return
84
+ context.signal?.throwIfAborted()
85
85
  }
86
86
 
87
87
  let emitCount = 0
88
88
  for (let i = 0; i < rows.length; i++) {
89
89
  if (++emitCount % YIELD_INTERVAL === 0) {
90
90
  await yieldToEventLoop()
91
- if (context.signal?.aborted) return
91
+ context.signal?.throwIfAborted()
92
92
  }
93
93
  const row = rows[i]
94
94
  const cells = { ...row.cells }
@@ -122,7 +122,7 @@ async function computeWindow(spec, rows, output, context) {
122
122
  for (let chunkStart = 0; chunkStart < rows.length; chunkStart += YIELD_INTERVAL) {
123
123
  if (chunkStart > 0) {
124
124
  await yieldToEventLoop()
125
- if (context.signal?.aborted) return
125
+ context.signal?.throwIfAborted()
126
126
  }
127
127
  const chunkEnd = Math.min(chunkStart + YIELD_INTERVAL, rows.length)
128
128
  const chunkKeys = await Promise.all(
@@ -142,7 +142,7 @@ async function computeWindow(spec, rows, output, context) {
142
142
  }
143
143
 
144
144
  for (const bucket of partitions.values()) {
145
- if (context.signal?.aborted) return
145
+ context.signal?.throwIfAborted()
146
146
 
147
147
  // Order within the partition. Empty ORDER BY → input order.
148
148
  /** @type {number[]} */
@@ -153,7 +153,7 @@ async function computeWindow(spec, rows, output, context) {
153
153
  for (let chunkStart = 0; chunkStart < bucket.length; chunkStart += YIELD_INTERVAL) {
154
154
  if (chunkStart > 0) {
155
155
  await yieldToEventLoop()
156
- if (context.signal?.aborted) return
156
+ context.signal?.throwIfAborted()
157
157
  }
158
158
  const chunkEnd = Math.min(chunkStart + YIELD_INTERVAL, bucket.length)
159
159
  const chunkValues = await Promise.all(
@@ -205,7 +205,7 @@ async function applyWindowFunction(spec, ordered, rows, output, context) {
205
205
  for (let k = 0; k < ordered.length; k++) {
206
206
  if (++tick % YIELD_INTERVAL === 0) {
207
207
  await yieldToEventLoop()
208
- if (context.signal?.aborted) return
208
+ context.signal?.throwIfAborted()
209
209
  }
210
210
  const idx = ordered[k]
211
211
  const row = rows[idx]
package/src/index.d.ts CHANGED
@@ -28,7 +28,7 @@ export type {
28
28
  * @param options.tables - source data as a list of objects or an AsyncDataSource
29
29
  * @param options.query - SQL query string
30
30
  * @param options.functions - user-defined functions available in the SQL context
31
- * @param options.signal - AbortSignal to cancel the query
31
+ * @param options.signal - AbortSignal to cancel the query; an aborted query rejects with the signal's reason
32
32
  * @returns async generator yielding rows matching the query
33
33
  */
34
34
  export function executeSql(options: ExecuteSqlOptions): QueryResults