sqllang 0.0.2 → 0.0.4

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": "sqllang",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "SQL over your own data: query arrays of rows, columnar tables, nested documents and JSON-text columns with a SQL dialect — joins, CTEs, window functions, subqueries. Compiles to JSON Logic and runs on json-logic-engine.",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",
package/src/compile.js CHANGED
@@ -243,7 +243,7 @@ function planSelect (sel, outer, cteNames, cx, orderBy) {
243
243
  const columns = []
244
244
  const outputs = new Map()
245
245
  const taken = new Set()
246
- sel.columns.forEach((col, i) => {
246
+ expandPivots(sel.columns).forEach((col, i) => {
247
247
  if (col.type === 'Star') {
248
248
  if (col.table !== null && !scope.aliases.includes(col.table)) throw new SqlError(`Unknown table '${col.table}' in ${col.table}.*`)
249
249
  const aliases = col.table !== null ? [col.table] : [...scope.aliases]
@@ -310,6 +310,26 @@ function planSelect (sel, outer, cteNames, cx, orderBy) {
310
310
  }
311
311
  }
312
312
 
313
+ /**
314
+ * `PIVOT(agg FOR col IN (a, b))` is one column per value: `agg FILTER (WHERE col = a)
315
+ * AS a, ...`. The values are literals, so the columns are known at compile time.
316
+ */
317
+ function expandPivots (columns) {
318
+ if (!columns.some((c) => c.expr && c.expr.$pivot)) return columns
319
+ return columns.flatMap((c) => {
320
+ if (!c.expr || !c.expr.$pivot) return [c]
321
+ const { agg, col, values } = c.expr.$pivot
322
+ if (c.alias != null) throw new SqlError('PIVOT makes one column per value, so it cannot take an alias')
323
+ const call = agg && agg.$call
324
+ if (!call || call.over || call.filter) throw new SqlError('PIVOT needs a plain aggregate, such as PIVOT(SUM(x) FOR col IN (...))')
325
+ return values.map((v) => {
326
+ if (v !== null && typeof v === 'object') throw new SqlError('PIVOT values must be literals')
327
+ const match = v === null ? { IS_NULL: [col] } : { EQ: [col, v] }
328
+ return { type: 'Column', alias: v === null ? 'null' : String(v), expr: { $call: { ...call, filter: match } } }
329
+ })
330
+ })
331
+ }
332
+
313
333
  function direction (item) {
314
334
  const desc = item.dir === 'DESC'
315
335
  let nullsFirst = null
@@ -392,6 +412,7 @@ function expr (node, scope, cx, mode) {
392
412
  const value = node[key]
393
413
  switch (key) {
394
414
  case '$ref': return ref(value, scope, cx, mode)
415
+ case '$pivot': throw new SqlError('PIVOT can only be a column in the select list')
395
416
  case '$param':
396
417
  return { PARAM: [value === null ? cx.positional++ : typeof value === 'number' ? value - 1 : value] }
397
418
  case '$call': return call(value, scope, cx, mode)
package/src/execute.js CHANGED
@@ -19,10 +19,86 @@ import {
19
19
  // operators that need it (parameters, subqueries); nested runs save and restore it.
20
20
  let active = null
21
21
 
22
+ const SYNC = Symbol.for('json_logic_sync')
23
+ const sync = (f) => { f[SYNC] = true; return f }
24
+
22
25
  const internal = {
23
26
  // One output row from `[name, value, name, value, ...]`. Compiled, it is a single
24
27
  // object literal, so a whole select list costs one call per row. Not
25
28
  // deterministic: each row needs its own object, never a folded shared one.
29
+ // A one-table GROUP BY as one function: [alias, where | null, keys, aggregates], each
30
+ // aggregate { kind, star, arg, filter }. The loop, the group lookup and the running
31
+ // totals are one piece of code. Rows come in as `context.$scan`, the group table as
32
+ // `context.$grp`. COUNT, SUM, AVG, MIN and MAX only: the incremental ones.
33
+ GROUPSCAN: {
34
+ method: () => { throw new SqlError('GROUPSCAN runs compiled only') },
35
+ compile: ([alias, where, keys, aggs], bs) => {
36
+ const raw = (text) => bs.compile([text])
37
+ // `alias.column` reads straight off the row, with the same guard the engine puts on
38
+ // a column read; anything else reads the context, so the row is stored there too.
39
+ const simple = (logic) => {
40
+ const v = logic !== null && typeof logic === 'object' ? logic.val : null
41
+ return Array.isArray(v) && v.length === 2 && v[0] === alias && typeof v[1] === 'string' ? v[1] : null
42
+ }
43
+ const read = (logic) => {
44
+ const column = simple(logic)
45
+ return column === null ? logic : raw(`(typeof (p = (r?.[${JSON.stringify(column)}] ?? null)) === "function" ? null : p)`)
46
+ }
47
+ const exprs = [...keys, ...aggs.flatMap((a) => [a.arg, a.filter])].filter((e) => e !== null)
48
+ const needsContext = where !== null || exprs.some((e) => simple(e) === null)
49
+
50
+ let res = raw('(() => { const rows = context["$scan"], grp = context["$grp"], root = grp.root, open = grp.open; let single = null, p, prev; const ints = []; for (let i = 0; i < rows.length; i++) { ')
51
+ res = bs.compile`${res}if ((i & 1023) === 0) ${sync(tick)}(Math.min(1024, rows.length - i)); const r = rows[i]; `
52
+ if (needsContext) res = bs.compile`${res}context[${alias}] = r; `
53
+ if (where !== null) res = bs.compile`${res}if (!${sync(isTrue)}(${where})) continue; `
54
+ const fresh = raw(`{ ${aggs.map((_, j) => `c${j}: 0, s${j}: 0, n${j}: 0, b${j}: null`).join(', ')}${aggs.length ? ', ' : ''}first: null }`)
55
+ if (!keys.length) {
56
+ res = bs.compile`${res}if (single === null) { single = ${fresh}; open(single, r) } const g = single; `
57
+ } else {
58
+ const key = (logic) => bs.compile`(p = ${read(logic)}) === undefined ? null : (p !== null && typeof p === "object") ? ${sync(keyOf)}(p) : p`
59
+ if (keys.length === 1) {
60
+ // One key: small non-negative integers index an array, which V8 reads faster than a Map.
61
+ res = bs.compile`${res}let g; { const k = ${key(keys[0])}; if (typeof k === "number" && k >= 0 && k < 1048576 && (k | 0) === k) { g = ints[k]; if (g === undefined) { g = ${fresh}; open(g, r); ints[k] = g } } else { g = root.get(k); if (g === undefined) { g = ${fresh}; open(g, r); root.set(k, g) } } } `
62
+ }
63
+ res = bs.compile`${res}${raw(keys.length === 1 ? '' : 'let level = root; ')}`
64
+ for (let d = 0; keys.length > 1 && d < keys.length - 1; d++) {
65
+ res = bs.compile`${res}{ const k = ${key(keys[d])}; let next = level.get(k); if (next === undefined) { next = new Map(); level.set(k, next) } level = next } `
66
+ }
67
+ if (keys.length > 1) res = bs.compile`${res}let g; { const k = ${key(keys[keys.length - 1])}; g = level.get(k); if (g === undefined) { g = ${fresh}; open(g, r); level.set(k, g) } } `
68
+ }
69
+ aggs.forEach((a, j) => {
70
+ let body
71
+ if (a.star) body = raw(`g.c${j}++; `)
72
+ else {
73
+ const kindCode = a.kind === SUM || a.kind === AVG
74
+ ? bs.compile`${raw('const x = typeof v === "number" ? v : ')}${sync(toNumber)}(v); if (x !== null) ${raw(`{ g.s${j} += x; g.n${j}++ }`)} `
75
+ : a.kind === MIN || a.kind === MAX
76
+ ? bs.compile`${raw(`if (g.b${j} === null || (typeof v === "number" && typeof g.b${j} === "number" ? v ${a.kind === MIN ? '<' : '>'} g.b${j} : `)}${sync(sortCompare)}(v, ${raw(`g.b${j}`)}, false, null) ${raw(a.kind === MIN ? '< 0' : '> 0')})) ${raw(`g.b${j} = v; `)}`
77
+ : raw('')
78
+ body = bs.compile`${raw('const v = ')}${read(a.arg)}; if (v !== null && v !== undefined) { ${raw(`g.c${j}++; `)}${kindCode} } `
79
+ }
80
+ if (a.filter !== null) body = bs.compile`if (${sync(isTrue)}(${read(a.filter)})) { ${body} } `
81
+ res = bs.compile`${res}{ ${body} } `
82
+ })
83
+ return bs.compile`${res}} })()`
84
+ },
85
+ traverse: false,
86
+ deterministic: false
87
+ },
88
+ // (Both scans declare their own `prev`, the engine's scratch variable, so the snippets inside
89
+ // them use a local instead of the enclosing function's, which a closure would keep in memory.)
90
+ // A whole single-table scan as one function: [alias, where | null, row]. Compiled, the
91
+ // loop, the filter and the projection are one piece of code, with the rows passed in
92
+ // as `context.$scan`. Only used compiled; the interpreted path runs the loop itself.
93
+ SCAN: {
94
+ method: () => { throw new SqlError('SCAN runs compiled only') },
95
+ compile: ([alias, where, row], bs) => {
96
+ const skip = where === null ? bs.compile`` : bs.compile`if (!${sync(isTrue)}(${where})) continue;`
97
+ return bs.compile`(() => { let prev; const rows = context["$scan"]; const out = []; for (let i = 0; i < rows.length; i++) { if ((i & 1023) === 0) ${sync(tick)}(Math.min(1024, rows.length - i)); context[${alias}] = rows[i]; ${skip} out.push(${row}) } return out })()`
98
+ },
99
+ traverse: false,
100
+ deterministic: false
101
+ },
26
102
  ROW: {
27
103
  method: (args) => {
28
104
  const out = {}
@@ -405,13 +481,17 @@ function extend (env, aliases, alias, row) {
405
481
 
406
482
  function runSelect (p, state, outer, keep = null) {
407
483
  const base = outer ? Object.create(outer) : {}
408
- const projector = makeProjector(p, state)
409
484
  const orderFns = p.orderBy ? p.orderBy.map((o) => fnOf(state, o.expr)) : null
485
+ // A plain one-table scan runs as one compiled function, which has its own copy of
486
+ // the select list, so the projector need not build (and eval) another.
487
+ const scan = orderFns === null && !p.grouped && p.having === null && scannable(p, state)
488
+ const projector = makeProjector(p, state, scan)
410
489
  // DISTINCT must see every row before the first k are known, so it sorts fully.
411
490
  const top = orderFns && keep !== null && !p.distinct ? topK(keep, comparatorFor(p.orderBy)) : null
412
491
 
413
492
  // `items` are { row, keys }: the projected row and its ORDER BY keys (or null).
414
493
  let items
494
+ let plain = null // the projected rows themselves, when there is no ORDER BY
415
495
  const single = p.steps.length === 1 && p.steps[0].join === 'FROM' && !p.windows.length
416
496
 
417
497
  if (single && !p.grouped && p.having === null) {
@@ -427,11 +507,18 @@ function runSelect (p, state, outer, keep = null) {
427
507
  top.add({ row: projector.project(env), keys })
428
508
  })
429
509
  items = top.best
510
+ } else if (orderFns === null) {
511
+ // No ORDER BY: nothing needs the `{ row, keys }` wrapper, so keep the rows.
512
+ if (scan) plain = scanRows(p, state, base)
513
+ else {
514
+ plain = []
515
+ streamRows(p, state, base, (env) => { plain.push(projector.project(env)) })
516
+ }
430
517
  } else {
431
518
  items = []
432
519
  streamRows(p, state, base, (env) => {
433
520
  const row = projector.project(env)
434
- const item = { row, keys: orderFns ? orderKeys(p, env, row, projector, orderFns) : null }
521
+ const item = { row, keys: orderKeys(p, env, row, projector, orderFns) }
435
522
  if (top !== null) { if (top.admits(item)) top.add(item) } else items.push(item)
436
523
  })
437
524
  if (top !== null) items = top.best
@@ -439,7 +526,9 @@ function runSelect (p, state, outer, keep = null) {
439
526
  } else {
440
527
  let envs
441
528
  if (single && p.grouped) {
442
- envs = group(p, state, base, (visit) => streamRows(p, state, base, visit))
529
+ envs = groupScannable(p, state)
530
+ ? groupScan(p, state, base)
531
+ : group(p, state, base, (visit) => streamRows(p, state, base, visit))
443
532
  } else {
444
533
  envs = [base]
445
534
  for (const step of p.steps) envs = joinStep(step, envs, base, state)
@@ -472,17 +561,19 @@ function runSelect (p, state, outer, keep = null) {
472
561
  }
473
562
 
474
563
  const columns = projector.columns
564
+ if (plain !== null) {
565
+ if (projector.hasStar) for (let i = 0; i < plain.length; i++) plain[i] = padRow(plain[i], columns)
566
+ if (p.distinct) {
567
+ const add = tupleSet(columns.length)
568
+ plain = plain.filter((row) => add(valuesOf(row, columns)))
569
+ }
570
+ return { columns, rows: plain }
571
+ }
475
572
  if (projector.hasStar) {
476
573
  // With `*` over rows of differing shape, give every row every column.
477
574
  for (let i = 0; i < items.length; i++) {
478
575
  const o = items[i].row
479
- if (sameKeys(o, columns)) continue
480
- const r = {}
481
- for (let j = 0; j < columns.length; j++) {
482
- const c = columns[j]
483
- setKey(r, c, Object.prototype.hasOwnProperty.call(o, c) ? o[c] : null)
484
- }
485
- items[i].row = r
576
+ items[i].row = padRow(o, columns)
486
577
  }
487
578
  }
488
579
 
@@ -505,15 +596,74 @@ function runSelect (p, state, outer, keep = null) {
505
596
  * allocation shows most. Rows that pass WHERE stream through a single reused
506
597
  * context object; `visit(env, true)` must copy anything it keeps.
507
598
  */
599
+ function sourceRows (p, state, base) {
600
+ const source = p.steps[0].source
601
+ return source.kind === 'expr' ? toRows(fnOf(state, source.expr)(base), false) : materialize(source, state)
602
+ }
603
+
604
+ const groupLogics = new WeakMap()
605
+
606
+ /** Whether a one-table GROUP BY can run as one compiled function. */
607
+ function groupScannable (p, state) {
608
+ return !state.interpreted && p.aggregates.every((spec) => INCREMENTAL.has(spec.name) && !spec.distinct && !spec.orderBy)
609
+ }
610
+
611
+ /** The groups of a one-table GROUP BY, as the contexts `group()` would give. */
612
+ function groupScan (p, state, base) {
613
+ let logic = groupLogics.get(p)
614
+ if (!logic) {
615
+ const aggs = p.aggregates.map((spec) => ({
616
+ kind: KIND[spec.name], star: spec.star, arg: spec.star ? null : spec.args[0], filter: spec.filter
617
+ }))
618
+ groupLogics.set(p, logic = { GROUPSCAN: [p.steps[0].alias, p.where, p.groupBy ?? [], aggs] })
619
+ }
620
+ const alias = p.steps[0].alias
621
+ const aggs = p.aggregates.map((spec) => ({ incremental: true, kind: KIND[spec.name] }))
622
+ const list = []
623
+ const open = (g, row) => {
624
+ g.first = snapshot(base)
625
+ g.first[alias] = row
626
+ list.push(g)
627
+ checkRows(list.length)
628
+ }
629
+ const env = snapshot(base)
630
+ env.$scan = sourceRows(p, state, base)
631
+ env.$grp = { root: new Map(), open }
632
+ fnOf(state, logic)(env)
633
+ if (!(p.groupBy && p.groupBy.length) && list.length === 0) list.push({ first: base, accs: newAccumulators(aggs) })
634
+ // The generated code keeps each total as a field on its group; `finish` reads accumulator objects.
635
+ for (let i = 0; i < list.length; i++) {
636
+ const g = list[i]
637
+ if (g.accs) continue
638
+ g.accs = aggs.map((_, j) => ({ count: g[`c${j}`], sum: g[`s${j}`], numeric: g[`n${j}`], best: g[`b${j}`] }))
639
+ }
640
+ return finishGroups(list, aggs)
641
+ }
642
+
643
+ const scanLogics = new WeakMap()
644
+
645
+ /** Whether a one-table select can run as one compiled scan. */
646
+ function scannable (p, state) {
647
+ return !state.interpreted && p.row !== null && p.steps.length === 1 && p.steps[0].join === 'FROM' && !p.windows.length
648
+ }
649
+
650
+ /** The projected rows of a one-table scan, run as one compiled function. */
651
+ function scanRows (p, state, base) {
652
+ let logic = scanLogics.get(p)
653
+ if (!logic) scanLogics.set(p, logic = { SCAN: [p.steps[0].alias, p.where, p.row] })
654
+ const env = snapshot(base)
655
+ env.$scan = sourceRows(p, state, base)
656
+ return fnOf(state, logic)(env)
657
+ }
658
+
508
659
  function streamRows (p, state, base, visit) {
509
660
  const step = p.steps[0]
510
661
  const alias = step.alias
511
- const source = step.source
512
- const rows = source.kind === 'expr' ? toRows(fnOf(state, source.expr)(base), false) : materialize(source, state)
662
+ const rows = sourceRows(p, state, base)
513
663
  const where = p.where === null ? null : fnOf(state, p.where)
514
664
  const env = snapshot(base)
515
665
  for (let i = 0; i < rows.length; i++) {
516
- tick()
666
+ if ((i & 1023) === 0) tick(Math.min(1024, rows.length - i))
517
667
  env[alias] = rows[i]
518
668
  if (where !== null && !isTrue(where(env))) continue
519
669
  visit(env, true)
@@ -529,10 +679,13 @@ function orderKeys (p, env, row, projector, orderFns) {
529
679
  }
530
680
 
531
681
  /** Builds output rows from contexts, and collects the column list as it goes. */
532
- function makeProjector (p, state) {
682
+ function makeProjector (p, state, skipBuild = false) {
533
683
  const cols = p.columns
534
- const exprFns = cols.map((c) => (c.kind === 'expr' ? fnOf(state, c.expr) : null))
535
684
  const hasStar = cols.some((c) => c.kind === 'star')
685
+ // No `*` and no `__proto__`: the whole select list is one compiled object literal.
686
+ const rowFn = !skipBuild && !hasStar && p.row ? fnOf(state, p.row) : null
687
+ // Per-column functions are only the fallback for that, so they are built only when needed.
688
+ const exprFns = skipBuild || rowFn ? [] : cols.map((c) => (c.kind === 'expr' ? fnOf(state, c.expr) : null))
536
689
  const columnSet = new Set()
537
690
  const columns = []
538
691
  const addColumn = (name) => { if (!columnSet.has(name)) { columnSet.add(name); columns.push(name) } }
@@ -540,9 +693,6 @@ function makeProjector (p, state) {
540
693
  // Names are fixed per query; only `__proto__` needs the careful path.
541
694
  const plain = cols.map((c) => c.kind === 'expr' && c.name !== '__proto__')
542
695
 
543
- // No `*` and no `__proto__`: the whole select list is one compiled object literal.
544
- const rowFn = !hasStar && p.row ? fnOf(state, p.row) : null
545
-
546
696
  const project = rowFn ?? ((env) => {
547
697
  const out = {}
548
698
  for (let j = 0; j < cols.length; j++) {
@@ -701,6 +851,17 @@ function readsOutput (orderBy) {
701
851
  }
702
852
 
703
853
  const nullish = (v) => (v === undefined ? null : v)
854
+ /** `o` with every column present, in column order (the row itself when it already is). */
855
+ function padRow (o, columns) {
856
+ if (sameKeys(o, columns)) return o
857
+ const r = {}
858
+ for (let j = 0; j < columns.length; j++) {
859
+ const c = columns[j]
860
+ setKey(r, c, Object.prototype.hasOwnProperty.call(o, c) ? o[c] : null)
861
+ }
862
+ return r
863
+ }
864
+
704
865
  /** Whether `o` has exactly `columns`, in that order: it needs no padding. */
705
866
  function sameKeys (o, columns) {
706
867
  const keys = Object.keys(o)
@@ -1180,6 +1341,11 @@ function group (p, state, base, each) {
1180
1341
  })
1181
1342
  }
1182
1343
 
1344
+ return finishGroups(list, aggs)
1345
+ }
1346
+
1347
+ /** One context per group: its first row, with the finished aggregates as `$agg`. */
1348
+ function finishGroups (list, aggs) {
1183
1349
  const out = new Array(list.length)
1184
1350
  for (let gi = 0; gi < list.length; gi++) {
1185
1351
  const g = list[gi]
package/src/functions.js CHANGED
@@ -267,6 +267,28 @@ const numeric = (f) => op((x, y) => {
267
267
  return Number.isFinite(r) ? r : null
268
268
  })
269
269
 
270
+ // -- short-circuit AND / OR -----------------------------------------------------
271
+ //
272
+ // Measured: inlining comparisons and arithmetic with a literal operand was no
273
+ // faster than the engine calling the small operator functions, so only the
274
+ // short-circuit is compiled. `prev` is the engine's scratch variable; it is
275
+ // read again before anything else can overwrite it.
276
+
277
+ /** Raw code. */
278
+ const code = (bs, text) => bs.compile([text])
279
+
280
+ /** AND stops at the first FALSE, OR at the first TRUE; anything undecided goes to the full function. */
281
+ function shortCircuit (impl, decisive) {
282
+ return (args, bs) => {
283
+ if (!Array.isArray(args) || args.length < 2) return callCompiled(bs, impl, args)
284
+ let acc = args[args.length - 1]
285
+ for (let i = args.length - 2; i >= 0; i--) {
286
+ acc = bs.compile`((prev = ${args[i]}) === ${code(bs, String(decisive))} ? ${code(bs, String(decisive))} : ${sync(impl)}(prev, ${acc}))`
287
+ }
288
+ return acc
289
+ }
290
+ }
291
+
270
292
  // Each comparison written out: numbers against numbers and strings against
271
293
  // strings compare directly; everything else goes through compare().
272
294
  const isNil = (v) => v === null || v === undefined
@@ -306,11 +328,14 @@ function or () {
306
328
  return sawNull ? null : false
307
329
  }
308
330
 
331
+ // `x IN (:ids)` with an array bound to `:ids` is membership in that array, as most database
332
+ // drivers expand it. A single non-array operand is the ordinary one-element list.
309
333
  function inList (v) {
310
334
  if (isNull(v)) return null
335
+ const list = arguments.length === 2 && Array.isArray(arguments[1]) ? arguments[1] : Array.prototype.slice.call(arguments, 1)
311
336
  let sawNull = false
312
- for (let i = 1; i < arguments.length; i++) {
313
- const e = equals(v, arguments[i])
337
+ for (let i = 0; i < list.length; i++) {
338
+ const e = equals(v, list[i])
314
339
  if (e) return true
315
340
  if (e === null) sawNull = true
316
341
  }
@@ -373,8 +398,8 @@ export const operators = {
373
398
  return !equals(a, b)
374
399
  }),
375
400
 
376
- AND: op(and),
377
- OR: op(or),
401
+ AND: { ...op(and), compile: shortCircuit(and, false) },
402
+ OR: { ...op(or), compile: shortCircuit(or, true) },
378
403
  NOT: op((v) => (v === null || v === undefined ? null : !isTrue(v))),
379
404
 
380
405
  ADD: numeric((a, b) => a + b),
package/src/grammar.gram CHANGED
@@ -76,6 +76,9 @@ token Extract pattern "EXTRACT(?![A-Za-z0-9_])" ignoreCase;
76
76
  token Over pattern "OVER(?![A-Za-z0-9_])" ignoreCase;
77
77
  token Partition pattern "PARTITION(?![A-Za-z0-9_])" ignoreCase;
78
78
  token Filter pattern "FILTER(?![A-Za-z0-9_])" ignoreCase;
79
+ token For pattern "FOR(?![A-Za-z0-9_])" ignoreCase;
80
+ // Only a keyword when a parenthesis follows, so `pivot` stays a usable column name.
81
+ token Pivot pattern "PIVOT(?=\\s*\\()" ignoreCase;
79
82
 
80
83
  token Number pattern "(?:[0-9]+\\.?[0-9]*|\\.[0-9]+)(?:[eE][+-]?[0-9]+)?";
81
84
  token String pattern "'(?:''|[^'])*'";
@@ -311,7 +314,7 @@ rule AnyName = alt(
311
314
  as(oneOf(Select, From, Where, As, Distinct, All, And, Or, Not, In, Is, Null, Like, Ilike, Between,
312
315
  Inner, Left, Right, Full, Outer, Cross, Join, On, Using, Group, By, Order, Asc, Desc, Nulls,
313
316
  Having, Limit, Offset, Insert, Into, Values, True, False, Case, When, Then, Else, End, With,
314
- Union, Except, Intersect, Exists, Cast, Extract, Over, Partition, Filter),
317
+ Union, Except, Intersect, Exists, Cast, Extract, Over, Partition, Filter, For),
315
318
  action({"val":"image"}))
316
319
  );
317
320
 
@@ -444,6 +447,7 @@ rule Primary = alt(
444
447
  subrule(CaseExpr),
445
448
  subrule(CastExpr),
446
449
  subrule(ExtractExpr),
450
+ subrule(PivotExpr),
447
451
  subrule(FunctionCall),
448
452
  subrule(NumberLiteral),
449
453
  subrule(StringLiteral),
@@ -495,6 +499,16 @@ rule ExtractExpr = seq(
495
499
  action({"obj":["DATE_PART",[{"val":"field"},{"val":"e"}]]})
496
500
  );
497
501
 
502
+ // PIVOT(SUM(sales) FOR region IN ('north', 'south')) — a select-list item that
503
+ // stands for one column per listed value; the compiler expands it.
504
+ rule PivotExpr = seq(
505
+ consume(Pivot), consume(LParen),
506
+ label(agg, subrule(Expression)), consume(For), label(col, subrule(ConcatExpr)),
507
+ consume(In), consume(LParen), label(values, many1Sep(subrule(Expression), Comma)), consume(RParen),
508
+ consume(RParen),
509
+ action({"obj":["$pivot",{"obj":["agg",{"val":"agg"},"col",{"val":"col"},"values",{"val":"values"}]}]})
510
+ );
511
+
498
512
  rule FunctionCall = seq(
499
513
  label(name, alt(subrule(Ident), as(oneOf(Left, Right), action({"val":"image"})))),
500
514
  consume(LParen),
package/src/guard.js CHANGED
@@ -51,9 +51,9 @@ export function enter (limits) {
51
51
  return () => { current = saved.current; deadline = saved.deadline }
52
52
  }
53
53
 
54
- /** Call once per unit of work in a loop; checks the clock every 1024 calls. */
55
- export function tick () {
56
- if ((++ticks & 1023) === 0 && performance.now() > deadline) {
54
+ /** Count `n` units of work (one by default); checks the clock each time 1024 are crossed. */
55
+ export function tick (n = 1) {
56
+ if (((ticks += n) & 1023) < n && performance.now() > deadline) {
57
57
  throw new SqlLimitError(`Query exceeded its time limit of ${current.timeoutMs} ms`, 'timeoutMs')
58
58
  }
59
59
  }
package/src/index.js CHANGED
@@ -47,14 +47,40 @@ export function prepare (sql, options = {}) {
47
47
  * @returns {object[] & { columns: string[] }}
48
48
  */
49
49
  export function query (sql, data, options = {}) {
50
- return prepare(sql, options).query(data, options.params)
50
+ const engine = options.engine ?? defaultEngine
51
+ return asRows(last(executePlan(planFor(sql, engine, options), data, { ...options, engine }).results))
51
52
  }
52
53
 
53
54
  /**
54
55
  * Run a script and get every statement's result, and the tables it created.
55
56
  */
56
57
  export function execute (sql, data, options = {}) {
57
- return prepare(sql, options).execute(data, options.params)
58
+ const engine = options.engine ?? defaultEngine
59
+ return executePlan(planFor(sql, engine, options), data, { ...options, engine })
60
+ }
61
+
62
+ // The same SQL text compiles to the same plan, and a plan carries the functions the
63
+ // engine built for it, so `query()` keeps the most recent ones. `limits` shape the
64
+ // plan at compile time, so a call that passes them compiles its own.
65
+ const plans = new WeakMap()
66
+ const PLAN_CACHE_SIZE = 256
67
+ const PLAN_CACHE_MAX_SQL = 8192
68
+
69
+ function planFor (sql, engine, options) {
70
+ const fresh = () => compile(sql, { functions: engine.sqlFunctions, limits: options.limits })
71
+ if (options.limits || typeof sql !== 'string' || sql.length > PLAN_CACHE_MAX_SQL) return fresh()
72
+ let cache = plans.get(engine)
73
+ if (!cache) plans.set(engine, cache = new Map())
74
+ let plan = cache.get(sql)
75
+ if (plan !== undefined) {
76
+ cache.delete(sql) // least recently used first
77
+ cache.set(sql, plan)
78
+ return plan
79
+ }
80
+ plan = fresh()
81
+ cache.set(sql, plan)
82
+ if (cache.size > PLAN_CACHE_SIZE) cache.delete(cache.keys().next().value)
83
+ return plan
58
84
  }
59
85
 
60
86
  function last (results) {