sqllang 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +282 -2
- package/dist/cjs/index.js +13715 -0
- package/dist/cjs/package.json +3 -0
- package/dist/esm/index.js +13675 -0
- package/dist/esm/index.min.js +6 -0
- package/dist/esm/package.json +3 -0
- package/dist/types/index.d.ts +221 -0
- package/package.json +58 -4
- package/src/aggregates.js +129 -0
- package/src/compile.js +603 -0
- package/src/errors.js +16 -0
- package/src/execute.js +1412 -0
- package/src/functions.js +691 -0
- package/src/grammar.gram +581 -0
- package/src/guard.js +79 -0
- package/src/index.js +78 -0
- package/src/parser.js +5481 -0
- package/src/values.js +196 -0
package/src/execute.js
ADDED
|
@@ -0,0 +1,1412 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs a plan from compile.js against data.
|
|
3
|
+
*
|
|
4
|
+
* Per-row work — every WHERE, ON, select-list and ORDER BY expression — is JSON
|
|
5
|
+
* Logic built once by json-logic-engine. What JSON Logic cannot express (joins,
|
|
6
|
+
* grouping, aggregation, windows, sorting, set operations) is this file.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { LogicEngine } from 'json-logic-engine'
|
|
10
|
+
import { operators, functions } from './functions.js'
|
|
11
|
+
import { aggregates } from './aggregates.js'
|
|
12
|
+
import { SqlError } from './errors.js'
|
|
13
|
+
import { enter, normalizeLimits, tick, rows as checkRows } from './guard.js'
|
|
14
|
+
import {
|
|
15
|
+
isNull, isTrue, setKey, keyOf, rowKey, sortCompare, equals, toNumber, maybeJson, walk, isNumericString
|
|
16
|
+
} from './values.js'
|
|
17
|
+
|
|
18
|
+
// The run in progress. Execution is synchronous, so one slot is enough for the
|
|
19
|
+
// operators that need it (parameters, subqueries); nested runs save and restore it.
|
|
20
|
+
let active = null
|
|
21
|
+
|
|
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
|
+
},
|
|
41
|
+
PARAM: {
|
|
42
|
+
method: ([key]) => {
|
|
43
|
+
const params = active?.params
|
|
44
|
+
// Own keys only: `:constructor` must not find Object.prototype.constructor.
|
|
45
|
+
const v = params == null || !Object.prototype.hasOwnProperty.call(params, key) ? undefined : params[key]
|
|
46
|
+
if (v === undefined) throw new SqlError(`No value bound for parameter ${typeof key === 'number' ? key + 1 : ':' + key}`)
|
|
47
|
+
return v
|
|
48
|
+
},
|
|
49
|
+
deterministic: false
|
|
50
|
+
},
|
|
51
|
+
SUBQUERY: {
|
|
52
|
+
method: ([id], env) => {
|
|
53
|
+
const { rows, columns } = runSubquery(id, env)
|
|
54
|
+
if (!rows.length) return null
|
|
55
|
+
return rows[0][columns[0]] ?? null
|
|
56
|
+
},
|
|
57
|
+
deterministic: false
|
|
58
|
+
},
|
|
59
|
+
EXISTS: {
|
|
60
|
+
method: ([id], env) => runSubquery(id, env).rows.length > 0,
|
|
61
|
+
deterministic: false
|
|
62
|
+
},
|
|
63
|
+
IN_SUB: {
|
|
64
|
+
method: ([value, id], env) => {
|
|
65
|
+
if (isNull(value)) return null
|
|
66
|
+
const plan = active.plan.subqueries[id]
|
|
67
|
+
// Uncorrelated: the subquery's values are the same for every row, so they
|
|
68
|
+
// are hashed once and each row is a lookup instead of a scan.
|
|
69
|
+
if (!plan.correlated) return inLookup(lookupFor(id), value)
|
|
70
|
+
const { rows, columns } = runSubquery(id, env)
|
|
71
|
+
return inScan(rows, columns[0], value)
|
|
72
|
+
},
|
|
73
|
+
deterministic: false
|
|
74
|
+
},
|
|
75
|
+
SERIES: {
|
|
76
|
+
method: ([from, to, by = 1]) => {
|
|
77
|
+
const a = toNumber(from); const b = toNumber(to); const s = toNumber(by)
|
|
78
|
+
if (a === null || b === null || !s) return []
|
|
79
|
+
checkRows(Math.floor((b - a) / s) + 1)
|
|
80
|
+
const out = []
|
|
81
|
+
if (s > 0) for (let x = a; x <= b; x += s) out.push(x)
|
|
82
|
+
else for (let x = a; x >= b; x += s) out.push(x)
|
|
83
|
+
return out
|
|
84
|
+
},
|
|
85
|
+
deterministic: true
|
|
86
|
+
},
|
|
87
|
+
ENTRIES: {
|
|
88
|
+
method: ([v]) => {
|
|
89
|
+
v = maybeJson(v)
|
|
90
|
+
const out = []
|
|
91
|
+
if (Array.isArray(v)) {
|
|
92
|
+
for (let i = 0; i < v.length; i++) out.push({ key: i, value: v[i] })
|
|
93
|
+
} else if (v && typeof v === 'object') {
|
|
94
|
+
const keys = Object.keys(v)
|
|
95
|
+
for (let i = 0; i < keys.length; i++) out.push({ key: keys[i], value: v[keys[i]] })
|
|
96
|
+
}
|
|
97
|
+
return out
|
|
98
|
+
},
|
|
99
|
+
deterministic: true
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* An engine with the SQL operators and function library loaded.
|
|
105
|
+
*
|
|
106
|
+
* @param {{ functions?: Record<string, Function | object>, [k: string]: any }} [options]
|
|
107
|
+
* `functions` adds scalar functions, callable from SQL by name (case-insensitive).
|
|
108
|
+
* A plain function receives the evaluated arguments as an array.
|
|
109
|
+
*/
|
|
110
|
+
export function createEngine (options = {}) {
|
|
111
|
+
const { functions: extra = {}, ...engineOptions } = options
|
|
112
|
+
const engine = new LogicEngine(undefined, engineOptions)
|
|
113
|
+
for (const [name, method] of Object.entries({ ...operators, ...functions, ...internal })) engine.addMethod(name, method)
|
|
114
|
+
const names = new Set()
|
|
115
|
+
for (const [name, method] of Object.entries(extra)) {
|
|
116
|
+
const upper = name.toUpperCase()
|
|
117
|
+
engine.addMethod(upper, typeof method === 'function' ? { method, deterministic: false } : method)
|
|
118
|
+
names.add(upper)
|
|
119
|
+
}
|
|
120
|
+
engine.sqlFunctions = names
|
|
121
|
+
return engine
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export const defaultEngine = createEngine()
|
|
125
|
+
|
|
126
|
+
// -- running ---------------------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
const caches = new WeakMap()
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Execute every statement of a compiled script.
|
|
132
|
+
*
|
|
133
|
+
* @returns {{ results: Array<{columns: string[], rows: object[]}>, tables: Map<string, object[]> }}
|
|
134
|
+
*/
|
|
135
|
+
export function executePlan (plan, data, options = {}) {
|
|
136
|
+
const engine = options.engine ?? defaultEngine
|
|
137
|
+
let cache = caches.get(engine)
|
|
138
|
+
if (!cache) { cache = new WeakMap(); caches.set(engine, cache) }
|
|
139
|
+
|
|
140
|
+
const state = {
|
|
141
|
+
plan,
|
|
142
|
+
engine,
|
|
143
|
+
cache,
|
|
144
|
+
interpreted: Boolean(options.interpreted),
|
|
145
|
+
root: Array.isArray(data) ? { data } : (data ?? {}),
|
|
146
|
+
params: options.params ?? null,
|
|
147
|
+
tables: new Map(Object.entries(options.tables ?? {})),
|
|
148
|
+
cteResults: new Map(),
|
|
149
|
+
subqueryResults: new Map(),
|
|
150
|
+
inLookups: new Map()
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const previous = active
|
|
154
|
+
active = state
|
|
155
|
+
const leave = enter(normalizeLimits(options.limits))
|
|
156
|
+
try {
|
|
157
|
+
const results = []
|
|
158
|
+
for (const statement of plan.statements) {
|
|
159
|
+
if (statement.kind === 'insert') results.push(runInsert(statement, state))
|
|
160
|
+
else results.push(runQuery(statement, state, null))
|
|
161
|
+
// CTE results do not outlive their statement — a later INSERT may change their inputs.
|
|
162
|
+
state.cteResults.clear()
|
|
163
|
+
state.subqueryResults.clear()
|
|
164
|
+
state.inLookups.clear()
|
|
165
|
+
}
|
|
166
|
+
return { results, tables: state.tables }
|
|
167
|
+
} finally {
|
|
168
|
+
leave()
|
|
169
|
+
active = previous
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function fnOf (state, logic) {
|
|
174
|
+
if (logic === null || typeof logic !== 'object') return () => logic
|
|
175
|
+
let f = state.cache.get(logic)
|
|
176
|
+
if (!f) {
|
|
177
|
+
if (state.interpreted) {
|
|
178
|
+
const engine = state.engine
|
|
179
|
+
f = (env) => engine.run(logic, env)
|
|
180
|
+
} else {
|
|
181
|
+
f = state.engine.build(logic)
|
|
182
|
+
}
|
|
183
|
+
state.cache.set(logic, f)
|
|
184
|
+
}
|
|
185
|
+
return f
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function inScan (rows, column, value) {
|
|
189
|
+
let sawNull = false
|
|
190
|
+
for (let i = 0; i < rows.length; i++) {
|
|
191
|
+
const e = equals(value, rows[i][column])
|
|
192
|
+
if (e) return true
|
|
193
|
+
if (e === null) sawNull = true
|
|
194
|
+
}
|
|
195
|
+
return sawNull ? null : false
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* The hashed form of an uncorrelated IN subquery, built once per run.
|
|
200
|
+
*
|
|
201
|
+
* exact every number and string, as is — most probes end here, hit or miss
|
|
202
|
+
* numeric the numbers, plus the numeric reading of numeric strings, for the
|
|
203
|
+
* loose matches equals() allows (1 = '1', '1.0' = 1); only consulted
|
|
204
|
+
* when a cross-type match is possible at all
|
|
205
|
+
*
|
|
206
|
+
* Values hashing cannot place (dates, objects, booleans) fall back to a scan.
|
|
207
|
+
*/
|
|
208
|
+
function lookupFor (id) {
|
|
209
|
+
const state = active
|
|
210
|
+
let lookup = state.inLookups.get(id)
|
|
211
|
+
if (lookup) return lookup
|
|
212
|
+
const { rows, columns } = runSubquery(id, null)
|
|
213
|
+
const column = columns[0]
|
|
214
|
+
const exact = new Set()
|
|
215
|
+
const numeric = new Map()
|
|
216
|
+
let hasNull = false
|
|
217
|
+
let hashable = true
|
|
218
|
+
let hasNumbers = false
|
|
219
|
+
let hasStrings = false
|
|
220
|
+
for (let i = 0; i < rows.length; i++) {
|
|
221
|
+
const v = rows[i][column]
|
|
222
|
+
if (v === null || v === undefined) { hasNull = true; continue }
|
|
223
|
+
if (typeof v === 'number') { hasNumbers = true; exact.add(v); addNumeric(numeric, v, v); continue }
|
|
224
|
+
if (typeof v !== 'string') { hashable = false; break }
|
|
225
|
+
hasStrings = true
|
|
226
|
+
exact.add(v)
|
|
227
|
+
if (isNumericString(v)) addNumeric(numeric, Number(v), v)
|
|
228
|
+
}
|
|
229
|
+
lookup = hashable
|
|
230
|
+
? { exact, numeric, hasNull, hasNumbers, hasNumericStrings: hasStrings && numeric.size > 0 }
|
|
231
|
+
: { rows, column }
|
|
232
|
+
state.inLookups.set(id, lookup)
|
|
233
|
+
return lookup
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function addNumeric (map, n, v) {
|
|
237
|
+
const list = map.get(n)
|
|
238
|
+
if (list === undefined) map.set(n, [v])
|
|
239
|
+
else list.push(v)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function inLookup (lookup, value) {
|
|
243
|
+
if (lookup.exact === undefined) return inScan(lookup.rows, lookup.column, value)
|
|
244
|
+
const miss = lookup.hasNull ? null : false
|
|
245
|
+
if (typeof value === 'number') {
|
|
246
|
+
if (lookup.exact.has(value)) return true
|
|
247
|
+
// A number can still equal a numeric string: 1 = '1.0'.
|
|
248
|
+
if (!lookup.hasNumericStrings) return miss
|
|
249
|
+
return confirm(lookup.numeric.get(value), value) ? true : miss
|
|
250
|
+
}
|
|
251
|
+
if (typeof value === 'string') {
|
|
252
|
+
if (lookup.exact.has(value)) return true
|
|
253
|
+
// A string can only equal a number now ('1' = 1); strings compare as strings.
|
|
254
|
+
if (!lookup.hasNumbers || !isNumericString(value)) return miss
|
|
255
|
+
return confirm(lookup.numeric.get(Number(value)), value) ? true : miss
|
|
256
|
+
}
|
|
257
|
+
// A date or an object against numbers and strings: compare the slow way.
|
|
258
|
+
for (const v of lookup.exact) if (equals(value, v)) return true
|
|
259
|
+
return miss
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function confirm (candidates, value) {
|
|
263
|
+
if (candidates === undefined) return false
|
|
264
|
+
for (let i = 0; i < candidates.length; i++) if (equals(value, candidates[i])) return true
|
|
265
|
+
return false
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function runSubquery (id, env) {
|
|
269
|
+
const state = active
|
|
270
|
+
const plan = state.plan.subqueries[id]
|
|
271
|
+
if (!plan.correlated) {
|
|
272
|
+
let hit = state.subqueryResults.get(id)
|
|
273
|
+
if (!hit) { hit = runQuery(plan, state, null); state.subqueryResults.set(id, hit) }
|
|
274
|
+
return hit
|
|
275
|
+
}
|
|
276
|
+
return runQuery(plan, state, env)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function runQuery (plan, state, outer) {
|
|
280
|
+
tick()
|
|
281
|
+
const limited = plan.limit !== null || plan.offset !== null
|
|
282
|
+
const offset = plan.offset === null ? 0 : Math.max(0, toNumber(fnOf(state, plan.offset)({})) ?? 0)
|
|
283
|
+
const limit = plan.limit === null ? Infinity : Math.max(0, toNumber(fnOf(state, plan.limit)({})) ?? 0)
|
|
284
|
+
// ORDER BY ... LIMIT on a select only needs the first offset + limit rows in order.
|
|
285
|
+
const keep = limited && plan.body.kind === 'select' && plan.body.orderBy && Number.isFinite(limit) ? offset + limit : null
|
|
286
|
+
let result = runBody(plan.body, state, outer, keep)
|
|
287
|
+
checkRows(result.rows.length)
|
|
288
|
+
if (plan.orderBy) {
|
|
289
|
+
const keys = plan.orderBy.map((o) => fnOf(state, o.expr))
|
|
290
|
+
const { rows, columns } = result
|
|
291
|
+
const decorated = new Array(rows.length)
|
|
292
|
+
for (let i = 0; i < rows.length; i++) {
|
|
293
|
+
const row = rows[i]
|
|
294
|
+
const env = { $out: row, $outv: valuesOf(row, columns) }
|
|
295
|
+
const k = new Array(keys.length)
|
|
296
|
+
for (let j = 0; j < keys.length; j++) k[j] = keys[j](env)
|
|
297
|
+
decorated[i] = { row, keys: k }
|
|
298
|
+
}
|
|
299
|
+
sortDecorated(decorated, plan.orderBy)
|
|
300
|
+
const sorted = new Array(decorated.length)
|
|
301
|
+
for (let i = 0; i < decorated.length; i++) sorted[i] = decorated[i].row
|
|
302
|
+
result = { columns, rows: sorted }
|
|
303
|
+
}
|
|
304
|
+
if (limited) result = { columns: result.columns, rows: result.rows.slice(offset, offset + limit) }
|
|
305
|
+
return result
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function runBody (body, state, outer, keep = null) {
|
|
309
|
+
switch (body.kind) {
|
|
310
|
+
case 'select': return runSelect(body, state, outer, keep)
|
|
311
|
+
case 'query': return runQuery(body, state, outer)
|
|
312
|
+
case 'values': return runValues(body, state, outer)
|
|
313
|
+
case 'setop': return runSetOp(body, state, outer)
|
|
314
|
+
default: throw new SqlError(`Unknown plan node ${body.kind}`)
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function runValues (body, state, outer) {
|
|
319
|
+
const env = outer ? Object.create(outer) : {}
|
|
320
|
+
const width = body.rows[0].length
|
|
321
|
+
const columns = Array.from({ length: width }, (_, i) => `column${i + 1}`)
|
|
322
|
+
const rows = new Array(body.rows.length)
|
|
323
|
+
for (let i = 0; i < body.rows.length; i++) {
|
|
324
|
+
const r = body.rows[i]
|
|
325
|
+
if (r.length !== width) throw new SqlError('VALUES rows must all have the same number of columns')
|
|
326
|
+
const row = {}
|
|
327
|
+
for (let j = 0; j < r.length; j++) setKey(row, columns[j], nullish(fnOf(state, r[j])(env)))
|
|
328
|
+
rows[i] = row
|
|
329
|
+
}
|
|
330
|
+
return { columns, rows }
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function runSetOp (body, state, outer) {
|
|
334
|
+
const left = runBody(body.left, state, outer)
|
|
335
|
+
const right = runBody(body.right, state, outer)
|
|
336
|
+
if (left.columns.length !== right.columns.length) {
|
|
337
|
+
throw new SqlError(`${body.op} needs both sides to have the same number of columns (${left.columns.length} vs ${right.columns.length})`)
|
|
338
|
+
}
|
|
339
|
+
const columns = left.columns
|
|
340
|
+
const toRow = (vals) => { const o = {}; for (let i = 0; i < columns.length; i++) setKey(o, columns[i], vals[i]); return o }
|
|
341
|
+
const leftVals = new Array(left.rows.length)
|
|
342
|
+
for (let i = 0; i < left.rows.length; i++) leftVals[i] = valuesOf(left.rows[i], columns)
|
|
343
|
+
const rightVals = new Array(right.rows.length)
|
|
344
|
+
for (let i = 0; i < right.rows.length; i++) rightVals[i] = valuesOf(right.rows[i], right.columns)
|
|
345
|
+
|
|
346
|
+
let out
|
|
347
|
+
if (body.op === 'UNION') {
|
|
348
|
+
out = leftVals.concat(rightVals)
|
|
349
|
+
if (!body.all) out = dedupe(out)
|
|
350
|
+
} else {
|
|
351
|
+
const counts = new Map()
|
|
352
|
+
for (let i = 0; i < rightVals.length; i++) { const k = rowKey(rightVals[i]); counts.set(k, (counts.get(k) || 0) + 1) }
|
|
353
|
+
const source = body.all ? leftVals : dedupe(leftVals)
|
|
354
|
+
out = []
|
|
355
|
+
for (let i = 0; i < source.length; i++) {
|
|
356
|
+
const v = source[i]
|
|
357
|
+
const k = rowKey(v)
|
|
358
|
+
const n = counts.get(k) || 0
|
|
359
|
+
if (body.op === 'INTERSECT' ? n > 0 : n === 0) out.push(v)
|
|
360
|
+
if (body.all && n > 0) counts.set(k, n - 1)
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
checkRows(out.length)
|
|
364
|
+
const rows = new Array(out.length)
|
|
365
|
+
for (let i = 0; i < out.length; i++) rows[i] = toRow(out[i])
|
|
366
|
+
return { columns, rows }
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function dedupe (rows) {
|
|
370
|
+
if (!rows.length) return rows
|
|
371
|
+
const add = tupleSet(rows[0].length)
|
|
372
|
+
const out = []
|
|
373
|
+
for (let i = 0; i < rows.length; i++) if (add(rows[i])) out.push(rows[i])
|
|
374
|
+
return out
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function valuesOf (row, columns) {
|
|
378
|
+
const out = new Array(columns.length)
|
|
379
|
+
for (let i = 0; i < columns.length; i++) out[i] = row[columns[i]]
|
|
380
|
+
return out
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function filterTrue (envs, fn) {
|
|
384
|
+
const out = []
|
|
385
|
+
for (let i = 0; i < envs.length; i++) {
|
|
386
|
+
tick()
|
|
387
|
+
if (isTrue(fn(envs[i]))) out.push(envs[i])
|
|
388
|
+
}
|
|
389
|
+
return out
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// -- SELECT ------------------------------------------------------------------------
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* A new context: `env` plus one more alias. `aliases` are the aliases `env`
|
|
396
|
+
* already holds (the join step knows them), so nothing is enumerated per row.
|
|
397
|
+
*/
|
|
398
|
+
function extend (env, aliases, alias, row) {
|
|
399
|
+
const proto = Object.getPrototypeOf(env)
|
|
400
|
+
const e = proto === Object.prototype ? {} : Object.create(proto)
|
|
401
|
+
for (let i = 0; i < aliases.length; i++) e[aliases[i]] = env[aliases[i]]
|
|
402
|
+
e[alias] = row
|
|
403
|
+
return e
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function runSelect (p, state, outer, keep = null) {
|
|
407
|
+
const base = outer ? Object.create(outer) : {}
|
|
408
|
+
const projector = makeProjector(p, state)
|
|
409
|
+
const orderFns = p.orderBy ? p.orderBy.map((o) => fnOf(state, o.expr)) : null
|
|
410
|
+
// DISTINCT must see every row before the first k are known, so it sorts fully.
|
|
411
|
+
const top = orderFns && keep !== null && !p.distinct ? topK(keep, comparatorFor(p.orderBy)) : null
|
|
412
|
+
|
|
413
|
+
// `items` are { row, keys }: the projected row and its ORDER BY keys (or null).
|
|
414
|
+
let items
|
|
415
|
+
const single = p.steps.length === 1 && p.steps[0].join === 'FROM' && !p.windows.length
|
|
416
|
+
|
|
417
|
+
if (single && !p.grouped && p.having === null) {
|
|
418
|
+
if (top !== null && !readsOutput(p.orderBy)) {
|
|
419
|
+
// The keys come from the input row, so a row is only projected once it
|
|
420
|
+
// has made the cut.
|
|
421
|
+
const probe = { row: null, keys: null }
|
|
422
|
+
streamRows(p, state, base, (env) => {
|
|
423
|
+
const keys = new Array(orderFns.length)
|
|
424
|
+
for (let k = 0; k < orderFns.length; k++) keys[k] = orderFns[k](env)
|
|
425
|
+
probe.keys = keys
|
|
426
|
+
if (!top.admits(probe)) return
|
|
427
|
+
top.add({ row: projector.project(env), keys })
|
|
428
|
+
})
|
|
429
|
+
items = top.best
|
|
430
|
+
} else {
|
|
431
|
+
items = []
|
|
432
|
+
streamRows(p, state, base, (env) => {
|
|
433
|
+
const row = projector.project(env)
|
|
434
|
+
const item = { row, keys: orderFns ? orderKeys(p, env, row, projector, orderFns) : null }
|
|
435
|
+
if (top !== null) { if (top.admits(item)) top.add(item) } else items.push(item)
|
|
436
|
+
})
|
|
437
|
+
if (top !== null) items = top.best
|
|
438
|
+
}
|
|
439
|
+
} else {
|
|
440
|
+
let envs
|
|
441
|
+
if (single && p.grouped) {
|
|
442
|
+
envs = group(p, state, base, (visit) => streamRows(p, state, base, visit))
|
|
443
|
+
} else {
|
|
444
|
+
envs = [base]
|
|
445
|
+
for (const step of p.steps) envs = joinStep(step, envs, base, state)
|
|
446
|
+
if (p.where !== null) {
|
|
447
|
+
const where = fnOf(state, p.where)
|
|
448
|
+
envs = filterTrue(envs, where)
|
|
449
|
+
}
|
|
450
|
+
if (p.grouped) {
|
|
451
|
+
const rows = envs
|
|
452
|
+
envs = group(p, state, base, (visit) => { for (let i = 0; i < rows.length; i++) { tick(); visit(rows[i], false) } })
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (p.having !== null) {
|
|
457
|
+
const having = fnOf(state, p.having)
|
|
458
|
+
envs = filterTrue(envs, having)
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (p.windows.length) computeWindows(p.windows, envs, state)
|
|
462
|
+
|
|
463
|
+
items = top !== null ? null : new Array(envs.length)
|
|
464
|
+
for (let i = 0; i < envs.length; i++) {
|
|
465
|
+
tick()
|
|
466
|
+
const env = envs[i]
|
|
467
|
+
const row = projector.project(env)
|
|
468
|
+
const item = { row, keys: orderFns ? orderKeys(p, env, row, projector, orderFns) : null }
|
|
469
|
+
if (top !== null) { if (top.admits(item)) top.add(item) } else items[i] = item
|
|
470
|
+
}
|
|
471
|
+
if (top !== null) items = top.best
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
const columns = projector.columns
|
|
475
|
+
if (projector.hasStar) {
|
|
476
|
+
// With `*` over rows of differing shape, give every row every column.
|
|
477
|
+
for (let i = 0; i < items.length; i++) {
|
|
478
|
+
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
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
if (p.distinct) {
|
|
490
|
+
const add = tupleSet(columns.length)
|
|
491
|
+
const kept = []
|
|
492
|
+
for (let i = 0; i < items.length; i++) if (add(valuesOf(items[i].row, columns))) kept.push(items[i])
|
|
493
|
+
items = kept
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// A top-k collector already holds its rows in order.
|
|
497
|
+
if (orderFns && top === null) sortDecorated(items, p.orderBy)
|
|
498
|
+
const rows = new Array(items.length)
|
|
499
|
+
for (let i = 0; i < items.length; i++) rows[i] = items[i].row
|
|
500
|
+
return { columns, rows }
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* One table and no windows: the common case, and the one where per-row
|
|
505
|
+
* allocation shows most. Rows that pass WHERE stream through a single reused
|
|
506
|
+
* context object; `visit(env, true)` must copy anything it keeps.
|
|
507
|
+
*/
|
|
508
|
+
function streamRows (p, state, base, visit) {
|
|
509
|
+
const step = p.steps[0]
|
|
510
|
+
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)
|
|
513
|
+
const where = p.where === null ? null : fnOf(state, p.where)
|
|
514
|
+
const env = snapshot(base)
|
|
515
|
+
for (let i = 0; i < rows.length; i++) {
|
|
516
|
+
tick()
|
|
517
|
+
env[alias] = rows[i]
|
|
518
|
+
if (where !== null && !isTrue(where(env))) continue
|
|
519
|
+
visit(env, true)
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function orderKeys (p, env, row, projector, orderFns) {
|
|
524
|
+
env.$out = row
|
|
525
|
+
if (p.needsOutv) env.$outv = valuesOf(row, projector.columns)
|
|
526
|
+
const keys = new Array(orderFns.length)
|
|
527
|
+
for (let k = 0; k < orderFns.length; k++) keys[k] = orderFns[k](env)
|
|
528
|
+
return keys
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** Builds output rows from contexts, and collects the column list as it goes. */
|
|
532
|
+
function makeProjector (p, state) {
|
|
533
|
+
const cols = p.columns
|
|
534
|
+
const exprFns = cols.map((c) => (c.kind === 'expr' ? fnOf(state, c.expr) : null))
|
|
535
|
+
const hasStar = cols.some((c) => c.kind === 'star')
|
|
536
|
+
const columnSet = new Set()
|
|
537
|
+
const columns = []
|
|
538
|
+
const addColumn = (name) => { if (!columnSet.has(name)) { columnSet.add(name); columns.push(name) } }
|
|
539
|
+
if (!hasStar) for (const c of cols) addColumn(c.name)
|
|
540
|
+
// Names are fixed per query; only `__proto__` needs the careful path.
|
|
541
|
+
const plain = cols.map((c) => c.kind === 'expr' && c.name !== '__proto__')
|
|
542
|
+
|
|
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) => {
|
|
547
|
+
const out = {}
|
|
548
|
+
for (let j = 0; j < cols.length; j++) {
|
|
549
|
+
const c = cols[j]
|
|
550
|
+
if (c.kind === 'expr') {
|
|
551
|
+
const v = exprFns[j](env)
|
|
552
|
+
if (plain[j]) out[c.name] = v === undefined ? null : v
|
|
553
|
+
else setKey(out, c.name, nullish(v))
|
|
554
|
+
continue
|
|
555
|
+
}
|
|
556
|
+
for (let a = 0; a < c.aliases.length; a++) {
|
|
557
|
+
const alias = c.aliases[a]
|
|
558
|
+
const row = env[alias]
|
|
559
|
+
if (row !== null && typeof row === 'object' && !Array.isArray(row)) {
|
|
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
|
+
}
|
|
571
|
+
for (let k = 0; k < keys.length; k++) {
|
|
572
|
+
const key = keys[k]
|
|
573
|
+
const name = Object.prototype.hasOwnProperty.call(out, key) ? `${alias}.${key}` : key
|
|
574
|
+
setKey(out, name, nullish(row[key]))
|
|
575
|
+
}
|
|
576
|
+
} else if (row !== undefined && row !== null) {
|
|
577
|
+
// (A NULL row is the missing side of an outer join: no columns to add.)
|
|
578
|
+
// A plain value (UNNEST of numbers) is the column `value`.
|
|
579
|
+
out[Object.prototype.hasOwnProperty.call(out, 'value') ? `${alias}.value` : 'value'] = nullish(row)
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
if (hasStar) for (const k in out) if (!columnSet.has(k)) addColumn(k)
|
|
584
|
+
return out
|
|
585
|
+
})
|
|
586
|
+
return { project, columns, hasStar }
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
const comparators = new WeakMap()
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* A comparator for `{ keys }` items under an ORDER BY, built once per ORDER BY.
|
|
593
|
+
* Numbers against numbers and strings against strings — nearly every
|
|
594
|
+
* comparison — never reach the general compare(). NULL placement is not
|
|
595
|
+
* flipped by DESC; NULLS FIRST defaults to DESC, as in Postgres.
|
|
596
|
+
*/
|
|
597
|
+
function comparatorFor (orderBy) {
|
|
598
|
+
let cmp = comparators.get(orderBy)
|
|
599
|
+
if (cmp) return cmp
|
|
600
|
+
const n = orderBy.length
|
|
601
|
+
const desc = orderBy.map((o) => o.desc)
|
|
602
|
+
const nullsFirst = orderBy.map((o) => o.nullsFirst ?? o.desc)
|
|
603
|
+
cmp = (a, b) => {
|
|
604
|
+
const ka = a.keys; const kb = b.keys
|
|
605
|
+
for (let i = 0; i < n; i++) {
|
|
606
|
+
const x = ka[i]; const y = kb[i]
|
|
607
|
+
let c
|
|
608
|
+
if ((typeof x === 'number' && typeof y === 'number') || (typeof x === 'string' && typeof y === 'string')) {
|
|
609
|
+
c = x < y ? -1 : x > y ? 1 : 0
|
|
610
|
+
} else {
|
|
611
|
+
const xn = x === null || x === undefined
|
|
612
|
+
const yn = y === null || y === undefined
|
|
613
|
+
if (xn || yn) {
|
|
614
|
+
if (xn && yn) continue
|
|
615
|
+
return xn ? (nullsFirst[i] ? -1 : 1) : (nullsFirst[i] ? 1 : -1)
|
|
616
|
+
}
|
|
617
|
+
c = sortCompare(x, y, false, null)
|
|
618
|
+
}
|
|
619
|
+
if (c !== 0) return desc[i] ? -c : c
|
|
620
|
+
}
|
|
621
|
+
return 0
|
|
622
|
+
}
|
|
623
|
+
comparators.set(orderBy, cmp)
|
|
624
|
+
return cmp
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
function sortDecorated (decorated, orderBy) {
|
|
628
|
+
// One numeric key with no NULLs (the usual window ORDER BY): plain subtraction.
|
|
629
|
+
if (orderBy.length === 1 && decorated.length > 16) {
|
|
630
|
+
let numeric = true
|
|
631
|
+
for (let i = 0; i < decorated.length; i++) {
|
|
632
|
+
const k = decorated[i].keys[0]
|
|
633
|
+
if (typeof k !== 'number' || k !== k) { numeric = false; break }
|
|
634
|
+
}
|
|
635
|
+
if (numeric) {
|
|
636
|
+
decorated.sort(orderBy[0].desc ? (a, b) => b.keys[0] - a.keys[0] : (a, b) => a.keys[0] - b.keys[0])
|
|
637
|
+
return
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
decorated.sort(comparatorFor(orderBy))
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Keeps the k smallest items under `cmp`, in order, as they stream past —
|
|
645
|
+
* O(n log k) instead of sorting everything for ORDER BY ... LIMIT k. An item
|
|
646
|
+
* lands after any it ties with, so the result matches a stable sort.
|
|
647
|
+
*/
|
|
648
|
+
function topK (k, cmp) {
|
|
649
|
+
const best = []
|
|
650
|
+
return {
|
|
651
|
+
best,
|
|
652
|
+
/** Would this item make the cut? (Ask before paying to build it.) */
|
|
653
|
+
admits (item) { return k > 0 && (best.length < k || cmp(item, best[best.length - 1]) < 0) },
|
|
654
|
+
add (item) {
|
|
655
|
+
let lo = 0; let hi = best.length
|
|
656
|
+
while (lo < hi) { const mid = (lo + hi) >> 1; if (cmp(item, best[mid]) < 0) hi = mid; else lo = mid + 1 }
|
|
657
|
+
if (lo >= k) return
|
|
658
|
+
best.splice(lo, 0, item)
|
|
659
|
+
if (best.length > k) best.pop()
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/** A set of value tuples in nested Maps — no key strings built per row. */
|
|
665
|
+
function tupleSet (width) {
|
|
666
|
+
const root = new Map()
|
|
667
|
+
const last = width - 1
|
|
668
|
+
/** Adds the tuple; returns false if it was already present. */
|
|
669
|
+
return (values) => {
|
|
670
|
+
if (width === 0) { if (root.has(true)) return false; root.set(true, true); return true }
|
|
671
|
+
let level = root
|
|
672
|
+
for (let d = 0; d < last; d++) {
|
|
673
|
+
const k = groupKey(values[d])
|
|
674
|
+
let next = level.get(k)
|
|
675
|
+
if (next === undefined) { next = new Map(); level.set(k, next) }
|
|
676
|
+
level = next
|
|
677
|
+
}
|
|
678
|
+
const k = groupKey(values[last])
|
|
679
|
+
if (level.has(k)) return false
|
|
680
|
+
level.set(k, true)
|
|
681
|
+
return true
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
const outRefs = new WeakMap()
|
|
686
|
+
|
|
687
|
+
/** Does this ORDER BY read the projected row ($out / $outv)? */
|
|
688
|
+
function readsOutput (orderBy) {
|
|
689
|
+
let r = outRefs.get(orderBy)
|
|
690
|
+
if (r === undefined) {
|
|
691
|
+
const visit = (l) => {
|
|
692
|
+
if (l === null || typeof l !== 'object') return false
|
|
693
|
+
if (Array.isArray(l)) return l.some(visit)
|
|
694
|
+
if (Array.isArray(l.val) && (l.val[0] === '$out' || l.val[0] === '$outv')) return true
|
|
695
|
+
return visit(l[Object.keys(l)[0]])
|
|
696
|
+
}
|
|
697
|
+
r = orderBy.some((o) => visit(o.expr))
|
|
698
|
+
outRefs.set(orderBy, r)
|
|
699
|
+
}
|
|
700
|
+
return r
|
|
701
|
+
}
|
|
702
|
+
|
|
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
|
+
}
|
|
711
|
+
|
|
712
|
+
// -- FROM and JOIN --------------------------------------------------------------------
|
|
713
|
+
|
|
714
|
+
function joinStep (step, envs, base, state) {
|
|
715
|
+
const { alias, source, join, leftAliases } = step
|
|
716
|
+
const lateral = source.kind === 'expr'
|
|
717
|
+
const sourceFn = lateral ? fnOf(state, source.expr) : null
|
|
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
|
+
}
|
|
730
|
+
const on = step.on === null ? null : fnOf(state, step.on)
|
|
731
|
+
const out = []
|
|
732
|
+
|
|
733
|
+
if (join === 'FROM' || join === 'CROSS') {
|
|
734
|
+
for (let i = 0; i < envs.length; i++) {
|
|
735
|
+
const env = envs[i]
|
|
736
|
+
const rows = lateral ? toRows(sourceFn(env), false) : right
|
|
737
|
+
for (let j = 0; j < rows.length; j++) {
|
|
738
|
+
tick()
|
|
739
|
+
const e = extend(env, leftAliases, alias, rows[j])
|
|
740
|
+
if (on !== null && !isTrue(on(e))) continue
|
|
741
|
+
out.push(e)
|
|
742
|
+
checkRows(out.length)
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
return out
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
if (step.hash) return hashJoin(step, envs, right, base, on, state)
|
|
749
|
+
|
|
750
|
+
if (join === 'INNER' || join === 'LEFT') {
|
|
751
|
+
for (let i = 0; i < envs.length; i++) {
|
|
752
|
+
const env = envs[i]
|
|
753
|
+
const rows = lateral ? toRows(sourceFn(env), false) : right
|
|
754
|
+
let matched = false
|
|
755
|
+
for (let j = 0; j < rows.length; j++) {
|
|
756
|
+
tick()
|
|
757
|
+
const e = extend(env, leftAliases, alias, rows[j])
|
|
758
|
+
if (on === null || isTrue(on(e))) { out.push(e); matched = true; checkRows(out.length) }
|
|
759
|
+
}
|
|
760
|
+
if (!matched && join === 'LEFT') out.push(extend(env, leftAliases, alias, null))
|
|
761
|
+
}
|
|
762
|
+
checkRows(out.length)
|
|
763
|
+
return out
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
// RIGHT and FULL: track which right rows found a partner.
|
|
767
|
+
if (lateral) throw new SqlError(`${join} JOIN cannot be lateral`)
|
|
768
|
+
const rows = right
|
|
769
|
+
const matchedRight = new Uint8Array(rows.length)
|
|
770
|
+
for (let i = 0; i < envs.length; i++) {
|
|
771
|
+
const env = envs[i]
|
|
772
|
+
let matched = false
|
|
773
|
+
for (let j = 0; j < rows.length; j++) {
|
|
774
|
+
tick()
|
|
775
|
+
const e = extend(env, leftAliases, alias, rows[j])
|
|
776
|
+
if (on === null || isTrue(on(e))) { out.push(e); matched = true; matchedRight[j] = 1; checkRows(out.length) }
|
|
777
|
+
}
|
|
778
|
+
if (!matched && join === 'FULL') out.push(extend(env, leftAliases, alias, null))
|
|
779
|
+
}
|
|
780
|
+
const nullLeft = nullEnv(base, leftAliases)
|
|
781
|
+
for (let j = 0; j < rows.length; j++) if (!matchedRight[j]) out.push(extend(nullLeft, leftAliases, alias, rows[j]))
|
|
782
|
+
checkRows(out.length)
|
|
783
|
+
return out
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
function nullEnv (base, aliases) {
|
|
787
|
+
const e = Object.create(Object.getPrototypeOf(base))
|
|
788
|
+
for (let i = 0; i < aliases.length; i++) e[aliases[i]] = null
|
|
789
|
+
return e
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
/**
|
|
793
|
+
* Single-column equi-join. Matching follows equals(), so 1 = '1' and
|
|
794
|
+
* '1.0' = 1 still join, but the common case is one Map lookup on the raw value:
|
|
795
|
+
*
|
|
796
|
+
* exact raw value -> right rows (numbers, strings, keyOf for the rest)
|
|
797
|
+
* numeric numeric reading -> right rows whose value is a number or a numeric
|
|
798
|
+
* string, consulted only for a cross-type match (number vs string)
|
|
799
|
+
*
|
|
800
|
+
* Within one left row, matches come out in right-table order, as a scan would give.
|
|
801
|
+
*/
|
|
802
|
+
function hashJoin1 (step, envs, right, base, on, state) {
|
|
803
|
+
const { alias, join, leftAliases } = step
|
|
804
|
+
const rightKey = fnOf(state, step.hash.right[0])
|
|
805
|
+
const leftKey = fnOf(state, step.hash.left[0])
|
|
806
|
+
const exact = new Map()
|
|
807
|
+
const numeric = new Map()
|
|
808
|
+
let rightNumbers = false
|
|
809
|
+
let rightNumericStrings = false
|
|
810
|
+
const probe = snapshot(base)
|
|
811
|
+
for (let i = 0; i < right.length; i++) {
|
|
812
|
+
tick()
|
|
813
|
+
probe[alias] = right[i]
|
|
814
|
+
const v = rightKey(probe)
|
|
815
|
+
if (v === null || v === undefined) continue
|
|
816
|
+
const entry = { i, row: right[i], v }
|
|
817
|
+
const k = groupKey(v)
|
|
818
|
+
const bucket = exact.get(k)
|
|
819
|
+
if (bucket === undefined) exact.set(k, [entry])
|
|
820
|
+
else bucket.push(entry)
|
|
821
|
+
let n = null
|
|
822
|
+
if (typeof v === 'number') { rightNumbers = true; n = v } else if (typeof v === 'string' && isNumericString(v)) { rightNumericStrings = true; n = Number(v) }
|
|
823
|
+
if (n !== null) {
|
|
824
|
+
const list = numeric.get(n)
|
|
825
|
+
if (list === undefined) numeric.set(n, [entry])
|
|
826
|
+
else list.push(entry)
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
const out = []
|
|
830
|
+
for (let i = 0; i < envs.length; i++) {
|
|
831
|
+
const env = envs[i]
|
|
832
|
+
const v = leftKey(env)
|
|
833
|
+
let matched = false
|
|
834
|
+
if (v !== null && v !== undefined) {
|
|
835
|
+
let candidates = exact.get(groupKey(v))
|
|
836
|
+
// Same-type matches are exact; a cross-type one needs the numeric map.
|
|
837
|
+
let cross
|
|
838
|
+
if (typeof v === 'number' && rightNumericStrings) cross = numeric.get(v)
|
|
839
|
+
else if (typeof v === 'string' && rightNumbers && isNumericString(v)) cross = numeric.get(Number(v))
|
|
840
|
+
else if (typeof v === 'object' || typeof v === 'boolean') candidates = undefined
|
|
841
|
+
if (cross !== undefined) {
|
|
842
|
+
const extra = []
|
|
843
|
+
for (let c = 0; c < cross.length; c++) if (typeof cross[c].v !== typeof v && equals(v, cross[c].v)) extra.push(cross[c])
|
|
844
|
+
if (extra.length) candidates = candidates === undefined ? extra : candidates.concat(extra).sort((x, y) => x.i - y.i)
|
|
845
|
+
}
|
|
846
|
+
if (candidates === undefined && (typeof v === 'object' || typeof v === 'boolean')) {
|
|
847
|
+
// Dates, objects, booleans: no shortcut — compare against every key.
|
|
848
|
+
candidates = []
|
|
849
|
+
for (const bucket of exact.values()) for (let c = 0; c < bucket.length; c++) if (equals(v, bucket[c].v)) candidates.push(bucket[c])
|
|
850
|
+
candidates.sort((x, y) => x.i - y.i)
|
|
851
|
+
}
|
|
852
|
+
if (candidates !== undefined) {
|
|
853
|
+
for (let c = 0; c < candidates.length; c++) {
|
|
854
|
+
tick()
|
|
855
|
+
const e = extend(env, leftAliases, alias, candidates[c].row)
|
|
856
|
+
if (on === null || isTrue(on(e))) { out.push(e); matched = true; checkRows(out.length) }
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
if (!matched && join === 'LEFT') out.push(extend(env, leftAliases, alias, null))
|
|
861
|
+
}
|
|
862
|
+
return out
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/** Equality-keyed join. Buckets are coarse (1 and '1' share one); EQ confirms. */
|
|
866
|
+
function hashJoin (step, envs, right, base, on, state) {
|
|
867
|
+
if (step.hash.right.length === 1) return hashJoin1(step, envs, right, base, on, state)
|
|
868
|
+
const { alias, join, leftAliases } = step
|
|
869
|
+
const rightKeys = step.hash.right.map((l) => fnOf(state, l))
|
|
870
|
+
const leftKeys = step.hash.left.map((l) => fnOf(state, l))
|
|
871
|
+
const width = rightKeys.length
|
|
872
|
+
const buckets = new Map()
|
|
873
|
+
// Right-side keys are evaluated against one reused context.
|
|
874
|
+
const probe = snapshot(base)
|
|
875
|
+
for (let i = 0; i < right.length; i++) {
|
|
876
|
+
tick()
|
|
877
|
+
const row = right[i]
|
|
878
|
+
probe[alias] = row
|
|
879
|
+
const values = new Array(width)
|
|
880
|
+
for (let k = 0; k < width; k++) values[k] = rightKeys[k](probe)
|
|
881
|
+
const key = hashKey(values)
|
|
882
|
+
if (key === null) continue
|
|
883
|
+
const bucket = buckets.get(key)
|
|
884
|
+
if (bucket === undefined) buckets.set(key, [{ row, values }])
|
|
885
|
+
else bucket.push({ row, values })
|
|
886
|
+
}
|
|
887
|
+
const out = []
|
|
888
|
+
const values = new Array(width)
|
|
889
|
+
for (let i = 0; i < envs.length; i++) {
|
|
890
|
+
const env = envs[i]
|
|
891
|
+
for (let k = 0; k < width; k++) values[k] = leftKeys[k](env)
|
|
892
|
+
const key = hashKey(values)
|
|
893
|
+
let matched = false
|
|
894
|
+
const bucket = key === null ? undefined : buckets.get(key)
|
|
895
|
+
if (bucket !== undefined) {
|
|
896
|
+
for (let b = 0; b < bucket.length; b++) {
|
|
897
|
+
tick()
|
|
898
|
+
const candidate = bucket[b]
|
|
899
|
+
let same = true
|
|
900
|
+
for (let k = 0; k < width; k++) if (equals(values[k], candidate.values[k]) !== true) { same = false; break }
|
|
901
|
+
if (!same) continue
|
|
902
|
+
const e = extend(env, leftAliases, alias, candidate.row)
|
|
903
|
+
if (on === null || isTrue(on(e))) { out.push(e); matched = true; checkRows(out.length) }
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
if (!matched && join === 'LEFT') out.push(extend(env, leftAliases, alias, null))
|
|
907
|
+
}
|
|
908
|
+
return out
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
function hashKey (values) {
|
|
912
|
+
let key = ''
|
|
913
|
+
for (let i = 0; i < values.length; i++) {
|
|
914
|
+
const v = values[i]
|
|
915
|
+
if (v === null || v === undefined) return null
|
|
916
|
+
if (typeof v === 'number') key += 'n' + v
|
|
917
|
+
else if (isNumericString(v)) key += 'n' + Number(v)
|
|
918
|
+
else key += keyOf(v)
|
|
919
|
+
key += '\u0000'
|
|
920
|
+
}
|
|
921
|
+
return key
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
function materialize (source, state) {
|
|
925
|
+
switch (source.kind) {
|
|
926
|
+
case 'table': return toRows(resolveTable(source.path, state), true)
|
|
927
|
+
case 'cte': {
|
|
928
|
+
let rows = state.cteResults.get(source.id)
|
|
929
|
+
if (!rows) {
|
|
930
|
+
rows = runQuery(state.plan.ctes[source.id].plan, state, null).rows
|
|
931
|
+
state.cteResults.set(source.id, rows)
|
|
932
|
+
}
|
|
933
|
+
return rows
|
|
934
|
+
}
|
|
935
|
+
case 'subquery': return runQuery(source.plan, state, null).rows
|
|
936
|
+
default: throw new SqlError(`Unknown source ${source.kind}`)
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
const own = (o, k) => o !== null && typeof o === 'object' && Object.prototype.hasOwnProperty.call(o, k)
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* A table name is a path into the data, or a table created by INSERT. Keys may
|
|
944
|
+
* themselves contain dots (`analytics.events`), so the longest matching
|
|
945
|
+
* prefix wins: `a.b.c` tries the key `a.b.c`, then `a.b` + `.c`, then `a` + `.b.c`.
|
|
946
|
+
*/
|
|
947
|
+
function resolveTable (path, state) {
|
|
948
|
+
for (let i = path.length; i >= 1; i--) {
|
|
949
|
+
const head = path.slice(0, i)
|
|
950
|
+
if (head.some((s) => typeof s !== 'string')) continue
|
|
951
|
+
const key = head.join('.')
|
|
952
|
+
if (state.tables.has(key)) return walk(state.tables.get(key), path, i)
|
|
953
|
+
if (own(state.root, key)) return walk(state.root[key], path, i)
|
|
954
|
+
}
|
|
955
|
+
// Fall back to a case-insensitive match on the first segment.
|
|
956
|
+
const first = String(path[0]).toLowerCase()
|
|
957
|
+
for (const key of state.tables.keys()) if (key.toLowerCase() === first) return walk(state.tables.get(key), path, 1)
|
|
958
|
+
for (const key of Object.keys(state.root)) if (key.toLowerCase() === first) return walk(state.root[key], path, 1)
|
|
959
|
+
throw new SqlError(`Unknown table '${path.join('.')}'`)
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
/**
|
|
963
|
+
* The rows a value stands for. An array is its elements; an object is one row.
|
|
964
|
+
* For a table (`shapes`), two more layouts are recognised:
|
|
965
|
+
* - columnar: `{ id: [1, 2], name: ['a', 'b'] }` — every value an array, same length
|
|
966
|
+
* - header row: `[['id', 'name'], [1, 'a'], [2, 'b']]`
|
|
967
|
+
*/
|
|
968
|
+
export function toRows (value, shapes) {
|
|
969
|
+
value = maybeJson(value)
|
|
970
|
+
if (isNull(value)) return []
|
|
971
|
+
if (Array.isArray(value)) {
|
|
972
|
+
if (shapes && value.length > 1 && value.every(Array.isArray) && value[0].length && value[0].every((h) => typeof h === 'string')) {
|
|
973
|
+
const header = value[0]
|
|
974
|
+
const rows = new Array(value.length - 1)
|
|
975
|
+
for (let i = 1; i < value.length; i++) {
|
|
976
|
+
const r = value[i]
|
|
977
|
+
const o = {}
|
|
978
|
+
for (let j = 0; j < header.length; j++) setKey(o, header[j], r[j] === undefined ? null : r[j])
|
|
979
|
+
rows[i - 1] = o
|
|
980
|
+
}
|
|
981
|
+
return rows
|
|
982
|
+
}
|
|
983
|
+
return value
|
|
984
|
+
}
|
|
985
|
+
if (typeof value === 'object' && shapes) {
|
|
986
|
+
const keys = Object.keys(value)
|
|
987
|
+
if (keys.length > 1 && keys.every((k) => Array.isArray(value[k]))) {
|
|
988
|
+
const n = value[keys[0]].length
|
|
989
|
+
if (keys.every((k) => value[k].length === n)) {
|
|
990
|
+
const rows = new Array(n)
|
|
991
|
+
const cols = new Array(keys.length)
|
|
992
|
+
for (let k = 0; k < keys.length; k++) cols[k] = value[keys[k]]
|
|
993
|
+
for (let i = 0; i < n; i++) {
|
|
994
|
+
const o = {}
|
|
995
|
+
for (let k = 0; k < keys.length; k++) setKey(o, keys[k], cols[k][i])
|
|
996
|
+
rows[i] = o
|
|
997
|
+
}
|
|
998
|
+
return rows
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
return [value]
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
// -- GROUP BY and aggregates ---------------------------------------------------------------
|
|
1006
|
+
|
|
1007
|
+
// Aggregates are fed one row at a time, so grouping never holds on to rows:
|
|
1008
|
+
// COUNT/SUM/AVG/MIN/MAX keep running totals, the rest collect their argument
|
|
1009
|
+
// values, and a group keeps only its first row (for the non-aggregated columns).
|
|
1010
|
+
|
|
1011
|
+
const INCREMENTAL = new Set(['COUNT', 'SUM', 'AVG', 'MIN', 'MAX'])
|
|
1012
|
+
const COUNT = 0; const SUM = 1; const AVG = 2; const MIN = 3; const MAX = 4
|
|
1013
|
+
const KIND = { COUNT, SUM, AVG, MIN, MAX }
|
|
1014
|
+
|
|
1015
|
+
function makeAggregators (p, state) {
|
|
1016
|
+
return p.aggregates.map((spec) => {
|
|
1017
|
+
const impl = aggregates[spec.name]
|
|
1018
|
+
return {
|
|
1019
|
+
spec,
|
|
1020
|
+
impl,
|
|
1021
|
+
args: spec.args.map((a) => fnOf(state, a)),
|
|
1022
|
+
filter: spec.filter === null ? null : fnOf(state, spec.filter),
|
|
1023
|
+
order: spec.orderBy ? spec.orderBy.map((o) => fnOf(state, o.expr)) : null,
|
|
1024
|
+
arity: impl.pairs ? 2 : (impl.arity ?? 1),
|
|
1025
|
+
incremental: INCREMENTAL.has(spec.name) && !spec.distinct && !spec.orderBy,
|
|
1026
|
+
kind: KIND[spec.name] ?? -1
|
|
1027
|
+
}
|
|
1028
|
+
})
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
const newAccumulator = (a) => (a.incremental ? { count: 0, sum: 0, numeric: 0, best: null } : { values: [], extra: null })
|
|
1032
|
+
|
|
1033
|
+
function accumulate (a, acc, env) {
|
|
1034
|
+
if (a.filter !== null && !isTrue(a.filter(env))) return
|
|
1035
|
+
if (a.incremental) {
|
|
1036
|
+
if (a.spec.star) { acc.count++; return }
|
|
1037
|
+
const v = a.args[0](env)
|
|
1038
|
+
if (v === null || v === undefined) return
|
|
1039
|
+
acc.count++
|
|
1040
|
+
switch (a.kind) {
|
|
1041
|
+
case SUM: case AVG: {
|
|
1042
|
+
const x = typeof v === 'number' ? v : toNumber(v)
|
|
1043
|
+
if (x !== null) { acc.sum += x; acc.numeric++ }
|
|
1044
|
+
return
|
|
1045
|
+
}
|
|
1046
|
+
case MIN:
|
|
1047
|
+
if (acc.best === null || (typeof v === 'number' && typeof acc.best === 'number' ? v < acc.best : sortCompare(v, acc.best, false, null) < 0)) acc.best = v
|
|
1048
|
+
return
|
|
1049
|
+
case MAX:
|
|
1050
|
+
if (acc.best === null || (typeof v === 'number' && typeof acc.best === 'number' ? v > acc.best : sortCompare(v, acc.best, false, null) > 0)) acc.best = v
|
|
1051
|
+
}
|
|
1052
|
+
return
|
|
1053
|
+
}
|
|
1054
|
+
// Constant extra arguments (STRING_AGG's separator) come from the first row.
|
|
1055
|
+
if (acc.extra === null) {
|
|
1056
|
+
acc.extra = []
|
|
1057
|
+
for (let i = a.arity; i < a.args.length; i++) acc.extra.push(a.args[i](env))
|
|
1058
|
+
}
|
|
1059
|
+
let v
|
|
1060
|
+
if (a.spec.star) v = 1
|
|
1061
|
+
else if (a.impl.pairs) v = [a.args[0](env), a.args[1](env)]
|
|
1062
|
+
else {
|
|
1063
|
+
v = a.args[0](env)
|
|
1064
|
+
if ((v === null || v === undefined) && !a.impl.keepNulls) return
|
|
1065
|
+
if (v === undefined) v = null
|
|
1066
|
+
}
|
|
1067
|
+
if (a.order === null) { acc.values.push(v); return }
|
|
1068
|
+
const keys = new Array(a.order.length)
|
|
1069
|
+
for (let i = 0; i < a.order.length; i++) keys[i] = a.order[i](env)
|
|
1070
|
+
acc.values.push({ v, keys })
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
function finish (a, acc) {
|
|
1074
|
+
if (a.incremental) {
|
|
1075
|
+
switch (a.kind) {
|
|
1076
|
+
case COUNT: return acc.count
|
|
1077
|
+
case SUM: return acc.numeric ? acc.sum : null
|
|
1078
|
+
case AVG: return acc.numeric ? acc.sum / acc.numeric : null
|
|
1079
|
+
default: return acc.best
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
let list = acc.values
|
|
1083
|
+
if (a.order !== null) {
|
|
1084
|
+
sortDecorated(list, a.spec.orderBy)
|
|
1085
|
+
const plain = new Array(list.length)
|
|
1086
|
+
for (let i = 0; i < list.length; i++) plain[i] = list[i].v
|
|
1087
|
+
list = plain
|
|
1088
|
+
}
|
|
1089
|
+
if (a.spec.distinct) list = distinctValues(list)
|
|
1090
|
+
let extra = acc.extra
|
|
1091
|
+
if (extra === null) {
|
|
1092
|
+
extra = []
|
|
1093
|
+
for (let i = a.arity; i < a.args.length; i++) extra.push(a.args[i]({}))
|
|
1094
|
+
}
|
|
1095
|
+
return nullish(a.impl.reduce(list, ...extra))
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
function distinctValues (list) {
|
|
1099
|
+
const seen = new Set()
|
|
1100
|
+
const out = []
|
|
1101
|
+
for (let i = 0; i < list.length; i++) {
|
|
1102
|
+
const k = keyOf(list[i])
|
|
1103
|
+
if (!seen.has(k)) { seen.add(k); out.push(list[i]) }
|
|
1104
|
+
}
|
|
1105
|
+
return out
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
function snapshot (env) {
|
|
1109
|
+
const proto = Object.getPrototypeOf(env)
|
|
1110
|
+
const e = proto === Object.prototype ? {} : Object.create(proto)
|
|
1111
|
+
for (const k in env) if (Object.prototype.hasOwnProperty.call(env, k)) e[k] = env[k]
|
|
1112
|
+
return e
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
function newAccumulators (aggs) {
|
|
1116
|
+
const accs = new Array(aggs.length)
|
|
1117
|
+
for (let i = 0; i < aggs.length; i++) accs[i] = newAccumulator(aggs[i])
|
|
1118
|
+
return accs
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
/**
|
|
1122
|
+
* GROUP BY over rows produced by `each(visit)`. `visit(env, reused)`: when the
|
|
1123
|
+
* producer reuses one context object, a new group copies it.
|
|
1124
|
+
*/
|
|
1125
|
+
/**
|
|
1126
|
+
* A grouping key part as a Map key. Primitives are their own keys — Map's
|
|
1127
|
+
* SameValueZero already keeps 1 and '1' apart, as keyOf does — so the common
|
|
1128
|
+
* case builds no string at all; anything else (dates, objects) goes through keyOf.
|
|
1129
|
+
*/
|
|
1130
|
+
function groupKey (v) {
|
|
1131
|
+
if (v === undefined) return null
|
|
1132
|
+
if (v !== null && typeof v === 'object') return keyOf(v)
|
|
1133
|
+
return v
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
/**
|
|
1137
|
+
* GROUP BY over rows produced by `each(visit)`. `visit(env, reused)`: when the
|
|
1138
|
+
* producer reuses one context object, a new group copies it.
|
|
1139
|
+
*
|
|
1140
|
+
* Groups live in nested Maps, one level per GROUP BY expression, so a
|
|
1141
|
+
* multi-column key is a chain of lookups rather than a concatenated string.
|
|
1142
|
+
*/
|
|
1143
|
+
function group (p, state, base, each) {
|
|
1144
|
+
const keyFns = (p.groupBy ?? []).map((g) => fnOf(state, g))
|
|
1145
|
+
const aggs = makeAggregators(p, state)
|
|
1146
|
+
const depth = keyFns.length
|
|
1147
|
+
const root = new Map()
|
|
1148
|
+
const list = []
|
|
1149
|
+
|
|
1150
|
+
const open = (env, reused) => {
|
|
1151
|
+
const g = { first: reused ? snapshot(env) : env, accs: newAccumulators(aggs) }
|
|
1152
|
+
list.push(g)
|
|
1153
|
+
checkRows(list.length)
|
|
1154
|
+
return g
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
if (depth === 0) {
|
|
1158
|
+
let g = null
|
|
1159
|
+
// (Row producers tick; the visitors here do not tick again.)
|
|
1160
|
+
each((env, reused) => {
|
|
1161
|
+
if (g === null) g = open(env, reused)
|
|
1162
|
+
for (let i = 0; i < aggs.length; i++) accumulate(aggs[i], g.accs[i], env)
|
|
1163
|
+
})
|
|
1164
|
+
// An aggregate over no rows at all still produces one row: SELECT COUNT(*) FROM empty.
|
|
1165
|
+
if (g === null) list.push({ first: base, accs: newAccumulators(aggs) })
|
|
1166
|
+
} else {
|
|
1167
|
+
const last = depth - 1
|
|
1168
|
+
each((env, reused) => {
|
|
1169
|
+
let level = root
|
|
1170
|
+
for (let d = 0; d < last; d++) {
|
|
1171
|
+
const k = groupKey(keyFns[d](env))
|
|
1172
|
+
let next = level.get(k)
|
|
1173
|
+
if (next === undefined) { next = new Map(); level.set(k, next) }
|
|
1174
|
+
level = next
|
|
1175
|
+
}
|
|
1176
|
+
const k = groupKey(keyFns[last](env))
|
|
1177
|
+
let g = level.get(k)
|
|
1178
|
+
if (g === undefined) { g = open(env, reused); level.set(k, g) }
|
|
1179
|
+
for (let i = 0; i < aggs.length; i++) accumulate(aggs[i], g.accs[i], env)
|
|
1180
|
+
})
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
const out = new Array(list.length)
|
|
1184
|
+
for (let gi = 0; gi < list.length; gi++) {
|
|
1185
|
+
const g = list[gi]
|
|
1186
|
+
const env = snapshot(g.first)
|
|
1187
|
+
const results = new Array(aggs.length)
|
|
1188
|
+
for (let i = 0; i < aggs.length; i++) results[i] = finish(aggs[i], g.accs[i])
|
|
1189
|
+
env.$agg = results
|
|
1190
|
+
out[gi] = env
|
|
1191
|
+
}
|
|
1192
|
+
return out
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
function reduceValues (spec, impl, args, rows) {
|
|
1196
|
+
const values = []
|
|
1197
|
+
const arity = impl.pairs ? 2 : (impl.arity ?? 1)
|
|
1198
|
+
for (let i = 0; i < rows.length; i++) {
|
|
1199
|
+
const env = rows[i]
|
|
1200
|
+
if (impl.pairs) {
|
|
1201
|
+
values.push([args[0](env), args[1](env)])
|
|
1202
|
+
continue
|
|
1203
|
+
}
|
|
1204
|
+
const v = args[0](env)
|
|
1205
|
+
if (isNull(v) && !impl.keepNulls) continue
|
|
1206
|
+
values.push(v === undefined ? null : v)
|
|
1207
|
+
}
|
|
1208
|
+
const list = spec.distinct ? distinctValues(values) : values
|
|
1209
|
+
const extra = []
|
|
1210
|
+
for (let i = arity; i < args.length; i++) extra.push(rows.length ? args[i](rows[0]) : args[i]({}))
|
|
1211
|
+
return nullish(impl.reduce(list, ...extra))
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
// -- window functions ------------------------------------------------------------------------
|
|
1215
|
+
|
|
1216
|
+
function computeWindows (windows, envs, state) {
|
|
1217
|
+
for (let i = 0; i < envs.length; i++) envs[i].$win = new Array(windows.length)
|
|
1218
|
+
for (let wi = 0; wi < windows.length; wi++) {
|
|
1219
|
+
const w = windows[wi]
|
|
1220
|
+
const partFns = w.partitionBy.map((p) => fnOf(state, p))
|
|
1221
|
+
const orderFns = w.orderBy.map((o) => fnOf(state, o.expr))
|
|
1222
|
+
const argFns = w.args.map((a) => fnOf(state, a))
|
|
1223
|
+
// Partitions in nested Maps, one level per PARTITION BY expression.
|
|
1224
|
+
const root = new Map()
|
|
1225
|
+
const parts = []
|
|
1226
|
+
const last = partFns.length - 1
|
|
1227
|
+
for (let i = 0; i < envs.length; i++) {
|
|
1228
|
+
tick()
|
|
1229
|
+
const env = envs[i]
|
|
1230
|
+
let part
|
|
1231
|
+
if (last < 0) {
|
|
1232
|
+
part = parts[0]
|
|
1233
|
+
if (part === undefined) { part = []; parts.push(part) }
|
|
1234
|
+
} else {
|
|
1235
|
+
let level = root
|
|
1236
|
+
for (let d = 0; d < last; d++) {
|
|
1237
|
+
const k = groupKey(partFns[d](env))
|
|
1238
|
+
let next = level.get(k)
|
|
1239
|
+
if (next === undefined) { next = new Map(); level.set(k, next) }
|
|
1240
|
+
level = next
|
|
1241
|
+
}
|
|
1242
|
+
const k = groupKey(partFns[last](env))
|
|
1243
|
+
part = level.get(k)
|
|
1244
|
+
if (part === undefined) { part = []; level.set(k, part); parts.push(part) }
|
|
1245
|
+
}
|
|
1246
|
+
const keys = new Array(orderFns.length)
|
|
1247
|
+
for (let j = 0; j < orderFns.length; j++) keys[j] = orderFns[j](env)
|
|
1248
|
+
part.push({ env, keys })
|
|
1249
|
+
}
|
|
1250
|
+
for (let pi = 0; pi < parts.length; pi++) {
|
|
1251
|
+
const part = parts[pi]
|
|
1252
|
+
if (orderFns.length) sortDecorated(part, w.orderBy)
|
|
1253
|
+
windowPartition(w, wi, part, argFns, orderFns.length > 0)
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
function samePeers (a, b) {
|
|
1259
|
+
for (let j = 0; j < a.length; j++) if (sortCompare(a[j], b[j], false, null) !== 0) return false
|
|
1260
|
+
return true
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
function peerGroups (part, ordered) {
|
|
1264
|
+
// Rows with equal ORDER BY keys are peers: they share a rank and a frame end.
|
|
1265
|
+
const starts = []
|
|
1266
|
+
const ends = []
|
|
1267
|
+
if (!ordered) { starts.push(0); ends.push(part.length); return { starts, ends } }
|
|
1268
|
+
let start = 0
|
|
1269
|
+
for (let i = 1; i <= part.length; i++) {
|
|
1270
|
+
if (i === part.length || !samePeers(part[i].keys, part[start].keys)) {
|
|
1271
|
+
starts.push(start)
|
|
1272
|
+
ends.push(i)
|
|
1273
|
+
start = i
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
return { starts, ends }
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
const INCREMENTAL_WINDOW = new Set(['COUNT', 'SUM', 'AVG', 'MIN', 'MAX'])
|
|
1280
|
+
|
|
1281
|
+
function windowPartition (w, wi, part, argFns, ordered) {
|
|
1282
|
+
const n = part.length
|
|
1283
|
+
const set = (i, v) => { part[i].env.$win[wi] = v === undefined ? null : v }
|
|
1284
|
+
const arg = (i, k) => argFns[k](part[i].env)
|
|
1285
|
+
// Peer groups as parallel start/end arrays: rows [starts[g], ends[g]) are peers.
|
|
1286
|
+
const { starts, ends } = peerGroups(part, ordered)
|
|
1287
|
+
const groupCount = starts.length
|
|
1288
|
+
|
|
1289
|
+
if (w.aggregate) {
|
|
1290
|
+
// The default frame: the whole partition without ORDER BY, otherwise
|
|
1291
|
+
// everything up to and including the current row's peers.
|
|
1292
|
+
if (INCREMENTAL_WINDOW.has(w.name) && !w.distinct) {
|
|
1293
|
+
let count = 0; let sum = 0; let numeric = 0; let best = null
|
|
1294
|
+
for (let g = 0; g < groupCount; g++) {
|
|
1295
|
+
const s = starts[g]; const e = ends[g]
|
|
1296
|
+
for (let i = s; i < e; i++) {
|
|
1297
|
+
if (w.star) { count++; continue }
|
|
1298
|
+
const v = arg(i, 0)
|
|
1299
|
+
if (v === null || v === undefined) continue
|
|
1300
|
+
count++
|
|
1301
|
+
const x = typeof v === 'number' ? v : toNumber(v)
|
|
1302
|
+
if (x !== null) { sum += x; numeric++ }
|
|
1303
|
+
if (w.name === 'MIN' && (best === null || sortCompare(v, best, false, null) < 0)) best = v
|
|
1304
|
+
else if (w.name === 'MAX' && (best === null || sortCompare(v, best, false, null) > 0)) best = v
|
|
1305
|
+
}
|
|
1306
|
+
const value = w.name === 'COUNT'
|
|
1307
|
+
? count
|
|
1308
|
+
: w.name === 'SUM'
|
|
1309
|
+
? (numeric ? sum : null)
|
|
1310
|
+
: w.name === 'AVG' ? (numeric ? sum / numeric : null) : best
|
|
1311
|
+
for (let i = s; i < e; i++) set(i, value)
|
|
1312
|
+
}
|
|
1313
|
+
return
|
|
1314
|
+
}
|
|
1315
|
+
// Anything else re-reduces the growing frame once per peer group.
|
|
1316
|
+
const impl = aggregates[w.name]
|
|
1317
|
+
const envs = new Array(n)
|
|
1318
|
+
for (let i = 0; i < n; i++) envs[i] = part[i].env
|
|
1319
|
+
for (let g = 0; g < groupCount; g++) {
|
|
1320
|
+
const e = ends[g]
|
|
1321
|
+
let value
|
|
1322
|
+
if (w.star) value = impl.reduce(new Array(e).fill(1))
|
|
1323
|
+
else value = reduceValues(w, impl, argFns, envs.slice(0, e))
|
|
1324
|
+
for (let i = starts[g]; i < e; i++) set(i, value)
|
|
1325
|
+
}
|
|
1326
|
+
return
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
switch (w.name) {
|
|
1330
|
+
case 'ROW_NUMBER':
|
|
1331
|
+
for (let i = 0; i < n; i++) set(i, i + 1)
|
|
1332
|
+
return
|
|
1333
|
+
case 'RANK':
|
|
1334
|
+
for (let g = 0; g < groupCount; g++) for (let i = starts[g]; i < ends[g]; i++) set(i, starts[g] + 1)
|
|
1335
|
+
return
|
|
1336
|
+
case 'DENSE_RANK':
|
|
1337
|
+
for (let g = 0; g < groupCount; g++) for (let i = starts[g]; i < ends[g]; i++) set(i, g + 1)
|
|
1338
|
+
return
|
|
1339
|
+
case 'PERCENT_RANK':
|
|
1340
|
+
for (let g = 0; g < groupCount; g++) for (let i = starts[g]; i < ends[g]; i++) set(i, n > 1 ? starts[g] / (n - 1) : 0)
|
|
1341
|
+
return
|
|
1342
|
+
case 'CUME_DIST':
|
|
1343
|
+
for (let g = 0; g < groupCount; g++) for (let i = starts[g]; i < ends[g]; i++) set(i, ends[g] / n)
|
|
1344
|
+
return
|
|
1345
|
+
case 'NTILE': {
|
|
1346
|
+
const buckets = Math.max(1, toNumber(n ? arg(0, 0) : 1) ?? 1)
|
|
1347
|
+
const size = Math.floor(n / buckets); const extra = n % buckets
|
|
1348
|
+
let i = 0
|
|
1349
|
+
for (let b = 1; b <= buckets && i < n; b++) {
|
|
1350
|
+
const len = size + (b <= extra ? 1 : 0)
|
|
1351
|
+
for (let j = 0; j < len; j++) set(i++, b)
|
|
1352
|
+
}
|
|
1353
|
+
return
|
|
1354
|
+
}
|
|
1355
|
+
case 'LAG':
|
|
1356
|
+
case 'LEAD': {
|
|
1357
|
+
const sign = w.name === 'LAG' ? -1 : 1
|
|
1358
|
+
for (let i = 0; i < n; i++) {
|
|
1359
|
+
const offset = argFns[1] ? toNumber(arg(i, 1)) ?? 1 : 1
|
|
1360
|
+
const j = i + sign * offset
|
|
1361
|
+
set(i, j >= 0 && j < n ? arg(j, 0) : (argFns[2] ? arg(i, 2) : null))
|
|
1362
|
+
}
|
|
1363
|
+
return
|
|
1364
|
+
}
|
|
1365
|
+
case 'FIRST_VALUE': {
|
|
1366
|
+
const first = n ? arg(0, 0) : null
|
|
1367
|
+
for (let i = 0; i < n; i++) set(i, first)
|
|
1368
|
+
return
|
|
1369
|
+
}
|
|
1370
|
+
case 'LAST_VALUE':
|
|
1371
|
+
for (let g = 0; g < groupCount; g++) {
|
|
1372
|
+
const value = arg(ends[g] - 1, 0)
|
|
1373
|
+
for (let i = starts[g]; i < ends[g]; i++) set(i, value)
|
|
1374
|
+
}
|
|
1375
|
+
return
|
|
1376
|
+
case 'NTH_VALUE':
|
|
1377
|
+
for (let g = 0; g < groupCount; g++) {
|
|
1378
|
+
const e = ends[g]
|
|
1379
|
+
for (let i = starts[g]; i < e; i++) {
|
|
1380
|
+
const k = (toNumber(arg(i, 1)) ?? 1) - 1
|
|
1381
|
+
set(i, k >= 0 && k < e ? arg(k, 0) : null)
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
return
|
|
1385
|
+
default:
|
|
1386
|
+
throw new SqlError(`Unknown window function ${w.name}`)
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
// -- INSERT ------------------------------------------------------------------------------------
|
|
1391
|
+
|
|
1392
|
+
function runInsert (statement, state) {
|
|
1393
|
+
const source = runQuery(statement.source, state, null)
|
|
1394
|
+
const name = statement.table
|
|
1395
|
+
let existing = state.tables.get(name)
|
|
1396
|
+
if (!existing) {
|
|
1397
|
+
existing = own(state.root, name) ? toRows(state.root[name], true).slice() : []
|
|
1398
|
+
state.tables.set(name, existing)
|
|
1399
|
+
}
|
|
1400
|
+
const columns = statement.columns ??
|
|
1401
|
+
(existing.length && typeof existing[0] === 'object' ? Object.keys(existing[0]) : source.columns)
|
|
1402
|
+
if (columns.length !== source.columns.length) {
|
|
1403
|
+
throw new SqlError(`INSERT INTO ${name} has ${columns.length} columns but the values have ${source.columns.length}`)
|
|
1404
|
+
}
|
|
1405
|
+
for (let r = 0; r < source.rows.length; r++) {
|
|
1406
|
+
const row = source.rows[r]
|
|
1407
|
+
const o = {}
|
|
1408
|
+
for (let i = 0; i < columns.length; i++) setKey(o, columns[i], row[source.columns[i]])
|
|
1409
|
+
existing.push(o)
|
|
1410
|
+
}
|
|
1411
|
+
return { columns: [], rows: [], inserted: source.rows.length, table: name }
|
|
1412
|
+
}
|