sqllang 0.0.1 → 0.0.2
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/dist/cjs/index.js +67 -4
- package/dist/esm/index.js +67 -4
- package/dist/esm/index.min.js +5 -5
- package/package.json +1 -1
- package/src/compile.js +25 -1
- package/src/execute.js +53 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sqllang",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
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
|
@@ -216,7 +216,27 @@ function planSelect (sel, outer, cteNames, cx, orderBy) {
|
|
|
216
216
|
for (const j of term.joins) addItem(j.item, j.joinType, j.on, j.using)
|
|
217
217
|
})
|
|
218
218
|
|
|
219
|
-
|
|
219
|
+
// WHERE conjuncts that read a single table are applied to that table's rows
|
|
220
|
+
// before any join. Only where the table is never the nullable side of an
|
|
221
|
+
// outer join (a later RIGHT/FULL, or its own LEFT), so the answer is unchanged.
|
|
222
|
+
let where = null
|
|
223
|
+
if (sel.where != null) {
|
|
224
|
+
const parts = steps.length > 1 ? splitAnd(sel.where) : [sel.where]
|
|
225
|
+
const residual = []
|
|
226
|
+
for (const part of parts) {
|
|
227
|
+
const deps = new Set()
|
|
228
|
+
const logic = expr(part, scope, cx, { deps })
|
|
229
|
+
const alias = deps.size === 1 ? [...deps][0] : null
|
|
230
|
+
const i = alias === null ? -1 : steps.findIndex((s) => s.alias === alias)
|
|
231
|
+
const step = steps[i]
|
|
232
|
+
const pushable = steps.length > 1 && step && step.source.kind !== 'expr' &&
|
|
233
|
+
(step.join === 'FROM' || step.join === 'CROSS' || step.join === 'INNER') &&
|
|
234
|
+
!steps.slice(i + 1).some((s) => s.join === 'RIGHT' || s.join === 'FULL')
|
|
235
|
+
if (pushable) step.filter = step.filter ? { AND: [step.filter, logic] } : logic
|
|
236
|
+
else residual.push(logic)
|
|
237
|
+
}
|
|
238
|
+
where = residual.length === 0 ? null : residual.length === 1 ? residual[0] : { AND: residual }
|
|
239
|
+
}
|
|
220
240
|
|
|
221
241
|
// Select list. Expression columns are compiled first so GROUP BY and ORDER BY
|
|
222
242
|
// can refer to them by alias or position.
|
|
@@ -281,6 +301,10 @@ function planSelect (sel, outer, cteNames, cx, orderBy) {
|
|
|
281
301
|
having,
|
|
282
302
|
windows: scope.windows,
|
|
283
303
|
columns,
|
|
304
|
+
// The select list as one expression, when it is all expressions with safe names.
|
|
305
|
+
row: columns.length && columns.every((c) => c.kind === 'expr' && c.name !== '__proto__')
|
|
306
|
+
? { ROW: columns.flatMap((c) => [c.name, c.expr]) }
|
|
307
|
+
: null,
|
|
284
308
|
orderBy: order,
|
|
285
309
|
needsOutv
|
|
286
310
|
}
|
package/src/execute.js
CHANGED
|
@@ -20,6 +20,24 @@ import {
|
|
|
20
20
|
let active = null
|
|
21
21
|
|
|
22
22
|
const internal = {
|
|
23
|
+
// One output row from `[name, value, name, value, ...]`. Compiled, it is a single
|
|
24
|
+
// object literal, so a whole select list costs one call per row. Not
|
|
25
|
+
// deterministic: each row needs its own object, never a folded shared one.
|
|
26
|
+
ROW: {
|
|
27
|
+
method: (args) => {
|
|
28
|
+
const out = {}
|
|
29
|
+
for (let i = 0; i < args.length; i += 2) setKey(out, args[i], args[i + 1] === undefined ? null : args[i + 1])
|
|
30
|
+
return out
|
|
31
|
+
},
|
|
32
|
+
compile: (args, bs) => {
|
|
33
|
+
let res = bs.compile`({`
|
|
34
|
+
for (let i = 0; i < args.length; i += 2) {
|
|
35
|
+
res = bs.compile`${res}${i ? bs.compile`,` : bs.compile``}${args[i]}: (${args[i + 1]}) ?? null`
|
|
36
|
+
}
|
|
37
|
+
return bs.compile`${res}})`
|
|
38
|
+
},
|
|
39
|
+
deterministic: false
|
|
40
|
+
},
|
|
23
41
|
PARAM: {
|
|
24
42
|
method: ([key]) => {
|
|
25
43
|
const params = active?.params
|
|
@@ -458,6 +476,7 @@ function runSelect (p, state, outer, keep = null) {
|
|
|
458
476
|
// With `*` over rows of differing shape, give every row every column.
|
|
459
477
|
for (let i = 0; i < items.length; i++) {
|
|
460
478
|
const o = items[i].row
|
|
479
|
+
if (sameKeys(o, columns)) continue
|
|
461
480
|
const r = {}
|
|
462
481
|
for (let j = 0; j < columns.length; j++) {
|
|
463
482
|
const c = columns[j]
|
|
@@ -521,7 +540,10 @@ function makeProjector (p, state) {
|
|
|
521
540
|
// Names are fixed per query; only `__proto__` needs the careful path.
|
|
522
541
|
const plain = cols.map((c) => c.kind === 'expr' && c.name !== '__proto__')
|
|
523
542
|
|
|
524
|
-
|
|
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
|
+
const project = rowFn ?? ((env) => {
|
|
525
547
|
const out = {}
|
|
526
548
|
for (let j = 0; j < cols.length; j++) {
|
|
527
549
|
const c = cols[j]
|
|
@@ -536,6 +558,16 @@ function makeProjector (p, state) {
|
|
|
536
558
|
const row = env[alias]
|
|
537
559
|
if (row !== null && typeof row === 'object' && !Array.isArray(row)) {
|
|
538
560
|
const keys = Object.keys(row)
|
|
561
|
+
if (c.aliases.length === 1) {
|
|
562
|
+
// One table: its keys cannot collide with each other.
|
|
563
|
+
for (let k = 0; k < keys.length; k++) {
|
|
564
|
+
const key = keys[k]
|
|
565
|
+
const v = row[key]
|
|
566
|
+
if (key === '__proto__') setKey(out, key, nullish(v))
|
|
567
|
+
else out[key] = v === undefined ? null : v
|
|
568
|
+
}
|
|
569
|
+
continue
|
|
570
|
+
}
|
|
539
571
|
for (let k = 0; k < keys.length; k++) {
|
|
540
572
|
const key = keys[k]
|
|
541
573
|
const name = Object.prototype.hasOwnProperty.call(out, key) ? `${alias}.${key}` : key
|
|
@@ -550,7 +582,7 @@ function makeProjector (p, state) {
|
|
|
550
582
|
}
|
|
551
583
|
if (hasStar) for (const k in out) if (!columnSet.has(k)) addColumn(k)
|
|
552
584
|
return out
|
|
553
|
-
}
|
|
585
|
+
})
|
|
554
586
|
return { project, columns, hasStar }
|
|
555
587
|
}
|
|
556
588
|
|
|
@@ -669,6 +701,13 @@ function readsOutput (orderBy) {
|
|
|
669
701
|
}
|
|
670
702
|
|
|
671
703
|
const nullish = (v) => (v === undefined ? null : v)
|
|
704
|
+
/** Whether `o` has exactly `columns`, in that order: it needs no padding. */
|
|
705
|
+
function sameKeys (o, columns) {
|
|
706
|
+
const keys = Object.keys(o)
|
|
707
|
+
if (keys.length !== columns.length) return false
|
|
708
|
+
for (let i = 0; i < keys.length; i++) if (keys[i] !== columns[i]) return false
|
|
709
|
+
return true
|
|
710
|
+
}
|
|
672
711
|
|
|
673
712
|
// -- FROM and JOIN --------------------------------------------------------------------
|
|
674
713
|
|
|
@@ -676,7 +715,18 @@ function joinStep (step, envs, base, state) {
|
|
|
676
715
|
const { alias, source, join, leftAliases } = step
|
|
677
716
|
const lateral = source.kind === 'expr'
|
|
678
717
|
const sourceFn = lateral ? fnOf(state, source.expr) : null
|
|
679
|
-
|
|
718
|
+
let right = lateral ? null : materialize(source, state)
|
|
719
|
+
if (step.filter) {
|
|
720
|
+
const keep = fnOf(state, step.filter)
|
|
721
|
+
const e = snapshot(base)
|
|
722
|
+
const kept = []
|
|
723
|
+
for (let j = 0; j < right.length; j++) {
|
|
724
|
+
tick()
|
|
725
|
+
e[alias] = right[j]
|
|
726
|
+
if (isTrue(keep(e))) kept.push(right[j])
|
|
727
|
+
}
|
|
728
|
+
right = kept
|
|
729
|
+
}
|
|
680
730
|
const on = step.on === null ? null : fnOf(state, step.on)
|
|
681
731
|
const out = []
|
|
682
732
|
|