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/compile.js
ADDED
|
@@ -0,0 +1,603 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL text -> a query plan.
|
|
3
|
+
*
|
|
4
|
+
* The parser (generated from grammar.gram) produces statement nodes whose
|
|
5
|
+
* expressions are already JSON Logic, apart from a few placeholders that need
|
|
6
|
+
* the query's scope: column references, function calls, subqueries and
|
|
7
|
+
* parameters. This module resolves them, so that a plan is
|
|
8
|
+
*
|
|
9
|
+
* - plain JSON (store it, ship it, diff it), and
|
|
10
|
+
* - every expression in it is JSON Logic that json-logic-engine runs as is.
|
|
11
|
+
*
|
|
12
|
+
* Rows in the executor are "environments": `{ o: orderRow, i: itemRow }`, one
|
|
13
|
+
* key per FROM alias. A reference resolves to a path into that object —
|
|
14
|
+
* `o.total` is `{"val": ["o", "total"]}` — and anything deeper, or inside a
|
|
15
|
+
* JSON-text column, goes through `PATH`.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import * as parser from './parser.js'
|
|
19
|
+
import { SqlError, SqlSyntaxError } from './errors.js'
|
|
20
|
+
import { SqlLimitError, normalizeLimits } from './guard.js'
|
|
21
|
+
|
|
22
|
+
const REGEX_FUNCTIONS = ['REGEXP_LIKE', 'REGEXP_REPLACE', 'REGEXP_EXTRACT']
|
|
23
|
+
import { aggregates, windowFunctions } from './aggregates.js'
|
|
24
|
+
import { functions as scalarFunctions, validateCall } from './functions.js'
|
|
25
|
+
|
|
26
|
+
const TABLE_FUNCTIONS = {
|
|
27
|
+
UNNEST: 'unnest', FLATTEN: 'unnest', EXPLODE: 'unnest',
|
|
28
|
+
GENERATE_SERIES: 'series', RANGE: 'series',
|
|
29
|
+
JSON_EACH: 'entries', ENTRIES: 'entries'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {string} source
|
|
34
|
+
* @param {{ functions?: Iterable<string>, limits?: object }} [options]
|
|
35
|
+
* `functions`: extra scalar function names. `limits`: see guard.js — compile
|
|
36
|
+
* applies `maxQueryLength` and `regex`.
|
|
37
|
+
*/
|
|
38
|
+
export function compile (source, options = {}) {
|
|
39
|
+
if (typeof source !== 'string') throw new SqlError('A query must be a string')
|
|
40
|
+
const limits = normalizeLimits(options.limits)
|
|
41
|
+
if (source.length > limits.maxQueryLength) {
|
|
42
|
+
throw new SqlLimitError(`Query is ${source.length} characters; the limit is ${limits.maxQueryLength}`, 'maxQueryLength')
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
return compileSource(source, options, limits)
|
|
46
|
+
} catch (error) {
|
|
47
|
+
// The parser and planner recurse; pathological nesting runs out of stack.
|
|
48
|
+
if (error instanceof RangeError && /call stack/i.test(error.message)) {
|
|
49
|
+
throw new SqlLimitError('Query is nested too deeply', 'depth')
|
|
50
|
+
}
|
|
51
|
+
throw error
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function compileSource (source, options, limits) {
|
|
56
|
+
let ast
|
|
57
|
+
try {
|
|
58
|
+
ast = parser.parse(source)
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error && (error.name === 'ParseError' || error.name === 'LexError')) throw syntaxError(source, error)
|
|
61
|
+
throw error
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const names = new Set(Object.keys(scalarFunctions))
|
|
65
|
+
for (const name of options.functions ?? []) names.add(String(name).toUpperCase())
|
|
66
|
+
|
|
67
|
+
const disabled = new Set(limits.regex ? [] : REGEX_FUNCTIONS)
|
|
68
|
+
const cx = { subqueries: [], ctes: [], positional: 0, functions: names, disabled, scopes: [] }
|
|
69
|
+
const statements = [ast.head, ...ast.tail].filter(Boolean).map((s) => planStatement(s, cx))
|
|
70
|
+
return {
|
|
71
|
+
type: 'sqllang',
|
|
72
|
+
version: 1,
|
|
73
|
+
statements,
|
|
74
|
+
ctes: cx.ctes,
|
|
75
|
+
subqueries: cx.subqueries,
|
|
76
|
+
positionalParams: cx.positional
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function planStatement (node, cx) {
|
|
81
|
+
if (node.type === 'Insert') {
|
|
82
|
+
return {
|
|
83
|
+
kind: 'insert',
|
|
84
|
+
table: node.table.join('.'),
|
|
85
|
+
columns: node.columns,
|
|
86
|
+
source: planQuery(node.source, null, new Map(), cx)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return planQuery(node, null, new Map(), cx)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// -- scopes ------------------------------------------------------------------
|
|
93
|
+
|
|
94
|
+
function newScope (cx, outer, cteNames) {
|
|
95
|
+
const scope = {
|
|
96
|
+
aliases: [],
|
|
97
|
+
outer,
|
|
98
|
+
cteNames,
|
|
99
|
+
outputs: null,
|
|
100
|
+
aggregates: [],
|
|
101
|
+
aggKeys: new Map(),
|
|
102
|
+
windows: [],
|
|
103
|
+
winKeys: new Map(),
|
|
104
|
+
correlated: false
|
|
105
|
+
}
|
|
106
|
+
cx.scopes.push(scope)
|
|
107
|
+
return scope
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const NO_REFS = { aliases: [], outer: null, cteNames: new Map(), outputs: null, aggregates: [], aggKeys: new Map(), windows: [], winKeys: new Map() }
|
|
111
|
+
|
|
112
|
+
// -- queries --------------------------------------------------------------------
|
|
113
|
+
|
|
114
|
+
function planQuery (q, outer, cteNames, cx) {
|
|
115
|
+
const firstScope = cx.scopes.length
|
|
116
|
+
cteNames = new Map(cteNames)
|
|
117
|
+
for (const cte of q.with ?? []) {
|
|
118
|
+
const id = cx.ctes.length
|
|
119
|
+
cx.ctes.push(null)
|
|
120
|
+
// A CTE sees the CTEs before it, never the row it is used from.
|
|
121
|
+
cx.ctes[id] = { name: cte.name, plan: planQuery(cte.query, null, cteNames, cx) }
|
|
122
|
+
cteNames.set(cte.name, id)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const plan = { kind: 'query', body: null, orderBy: null, limit: null, offset: null, correlated: false }
|
|
126
|
+
if (q.body.type === 'Select') {
|
|
127
|
+
plan.body = planSelect(q.body, outer, cteNames, cx, q.orderBy)
|
|
128
|
+
} else {
|
|
129
|
+
plan.body = planBody(q.body, outer, cteNames, cx)
|
|
130
|
+
if (q.orderBy) {
|
|
131
|
+
// Over a UNION, ORDER BY sees only the result's columns.
|
|
132
|
+
plan.orderBy = q.orderBy.map((item) => orderItem(item, outputOnlyScope(), cx))
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (q.limit) {
|
|
136
|
+
plan.limit = q.limit.limit === null ? null : expr(q.limit.limit, NO_REFS, cx, {})
|
|
137
|
+
plan.offset = q.limit.offset === null ? null : expr(q.limit.offset, NO_REFS, cx, {})
|
|
138
|
+
}
|
|
139
|
+
plan.correlated = cx.scopes.slice(firstScope).some((s) => s.correlated)
|
|
140
|
+
return plan
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function outputOnlyScope () {
|
|
144
|
+
return { ...NO_REFS, outputs: new Map(), outputOnly: true }
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function planBody (node, outer, cteNames, cx) {
|
|
148
|
+
switch (node.type) {
|
|
149
|
+
case 'Select': return planSelect(node, outer, cteNames, cx, null)
|
|
150
|
+
case 'Query': return planQuery(node, outer, cteNames, cx)
|
|
151
|
+
case 'SetOp':
|
|
152
|
+
return {
|
|
153
|
+
kind: 'setop',
|
|
154
|
+
op: node.op,
|
|
155
|
+
all: node.all,
|
|
156
|
+
left: planBody(node.left, outer, cteNames, cx),
|
|
157
|
+
right: planBody(node.right, outer, cteNames, cx)
|
|
158
|
+
}
|
|
159
|
+
case 'Values': {
|
|
160
|
+
const scope = newScope(cx, outer, cteNames)
|
|
161
|
+
return { kind: 'values', rows: node.rows.map((row) => row.map((e) => expr(e, scope, cx, {}))) }
|
|
162
|
+
}
|
|
163
|
+
default: throw new SqlError(`Unexpected ${node.type}`)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function planSelect (sel, outer, cteNames, cx, orderBy) {
|
|
168
|
+
const scope = newScope(cx, outer, cteNames)
|
|
169
|
+
const steps = []
|
|
170
|
+
|
|
171
|
+
const addItem = (item, join, on, using) => {
|
|
172
|
+
const { alias, source } = planSource(item, scope, cx)
|
|
173
|
+
if (alias === '__proto__' || alias.startsWith('$')) throw new SqlError(`'${alias}' cannot be used as a table alias`)
|
|
174
|
+
if (scope.aliases.includes(alias)) throw new SqlError(`Table name '${alias}' specified more than once; give one an alias`)
|
|
175
|
+
const leftAliases = [...scope.aliases]
|
|
176
|
+
scope.aliases.push(alias)
|
|
177
|
+
const step = { alias, source, join, on: null, hash: null, leftAliases }
|
|
178
|
+
|
|
179
|
+
let conjuncts = []
|
|
180
|
+
if (using) {
|
|
181
|
+
for (const column of using) {
|
|
182
|
+
conjuncts.push({ logic: { EQ: [{ COL: [column, ...leftAliases] }, { val: [alias, column] }] }, deps: new Set() })
|
|
183
|
+
}
|
|
184
|
+
} else if (on) {
|
|
185
|
+
conjuncts = splitAnd(on).map((c) => {
|
|
186
|
+
const deps = new Set()
|
|
187
|
+
return { logic: expr(c, scope, cx, { deps }), deps }
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// An equality between something of the new table and something of the
|
|
192
|
+
// tables before it becomes a hash join instead of a nested loop.
|
|
193
|
+
const hashable = source.kind !== 'expr' && (join === 'INNER' || join === 'LEFT')
|
|
194
|
+
const keys = { left: [], right: [] }
|
|
195
|
+
const residual = []
|
|
196
|
+
for (const c of conjuncts) {
|
|
197
|
+
const logic = c.logic
|
|
198
|
+
if (hashable && c.deps && logic.EQ) {
|
|
199
|
+
const [a, b] = logic.EQ
|
|
200
|
+
const da = depsOf(conjuncts, c, 0); const db = depsOf(conjuncts, c, 1)
|
|
201
|
+
if (onlyNew(da, alias) && notNew(db, alias)) { keys.right.push(a); keys.left.push(b); continue }
|
|
202
|
+
if (onlyNew(db, alias) && notNew(da, alias)) { keys.right.push(b); keys.left.push(a); continue }
|
|
203
|
+
}
|
|
204
|
+
residual.push(logic)
|
|
205
|
+
}
|
|
206
|
+
if (keys.left.length) step.hash = keys
|
|
207
|
+
if (residual.length) step.on = residual.length === 1 ? residual[0] : { AND: residual }
|
|
208
|
+
if ((join === 'INNER' || join === 'LEFT' || join === 'RIGHT' || join === 'FULL') && !on && !using) {
|
|
209
|
+
throw new SqlError(`${join} JOIN ${alias} needs an ON or USING condition`)
|
|
210
|
+
}
|
|
211
|
+
steps.push(step)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
;(sel.from ?? []).forEach((term, i) => {
|
|
215
|
+
addItem(term.base, i === 0 ? 'FROM' : 'CROSS', null, null)
|
|
216
|
+
for (const j of term.joins) addItem(j.item, j.joinType, j.on, j.using)
|
|
217
|
+
})
|
|
218
|
+
|
|
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
|
+
}
|
|
240
|
+
|
|
241
|
+
// Select list. Expression columns are compiled first so GROUP BY and ORDER BY
|
|
242
|
+
// can refer to them by alias or position.
|
|
243
|
+
const columns = []
|
|
244
|
+
const outputs = new Map()
|
|
245
|
+
const taken = new Set()
|
|
246
|
+
sel.columns.forEach((col, i) => {
|
|
247
|
+
if (col.type === 'Star') {
|
|
248
|
+
if (col.table !== null && !scope.aliases.includes(col.table)) throw new SqlError(`Unknown table '${col.table}' in ${col.table}.*`)
|
|
249
|
+
const aliases = col.table !== null ? [col.table] : [...scope.aliases]
|
|
250
|
+
if (!aliases.length) throw new SqlError('SELECT * needs a FROM clause')
|
|
251
|
+
columns.push({ kind: 'star', aliases })
|
|
252
|
+
return
|
|
253
|
+
}
|
|
254
|
+
const logic = expr(col.expr, scope, cx, { agg: true, win: true })
|
|
255
|
+
let name = col.alias ?? deriveName(col.expr, i)
|
|
256
|
+
if (taken.has(name)) { let n = 2; while (taken.has(`${name}_${n}`)) n++; name = `${name}_${n}` }
|
|
257
|
+
taken.add(name)
|
|
258
|
+
columns.push({ kind: 'expr', name, expr: logic })
|
|
259
|
+
outputs.set(name, { logic, ast: col.expr, index: i })
|
|
260
|
+
})
|
|
261
|
+
scope.outputs = outputs
|
|
262
|
+
|
|
263
|
+
let groupBy = null
|
|
264
|
+
if (sel.groupBy) {
|
|
265
|
+
groupBy = sel.groupBy.map((g) => {
|
|
266
|
+
if (typeof g === 'number') {
|
|
267
|
+
const col = columns[g - 1]
|
|
268
|
+
if (!col || col.kind !== 'expr') throw new SqlError(`GROUP BY position ${g} is not in the select list`)
|
|
269
|
+
if (containsAggregate(col.expr)) throw new SqlError(`GROUP BY position ${g} refers to an aggregate`)
|
|
270
|
+
return col.expr
|
|
271
|
+
}
|
|
272
|
+
if (g && g.$ref && g.$ref.length === 1 && outputs.has(g.$ref[0])) {
|
|
273
|
+
const out = outputs.get(g.$ref[0])
|
|
274
|
+
const sameColumn = out.ast && out.ast.$ref && out.ast.$ref.length === 1 && out.ast.$ref[0] === g.$ref[0]
|
|
275
|
+
if (!sameColumn && !containsAggregate(out.logic)) return out.logic
|
|
276
|
+
}
|
|
277
|
+
return expr(g, scope, cx, {})
|
|
278
|
+
})
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// HAVING may name a select-list alias (as SQLite allows): `HAVING n > 1`. It runs
|
|
282
|
+
// before projection, so the alias is replaced by its expression.
|
|
283
|
+
const having = sel.having == null ? null : expr(sel.having, scope, cx, { agg: true, inlineOutputs: true })
|
|
284
|
+
let needsOutv = false
|
|
285
|
+
const order = orderBy
|
|
286
|
+
? orderBy.map((item) => {
|
|
287
|
+
const o = orderItem(item, scope, cx)
|
|
288
|
+
if (o.ordinal) needsOutv = true
|
|
289
|
+
return o
|
|
290
|
+
})
|
|
291
|
+
: null
|
|
292
|
+
|
|
293
|
+
return {
|
|
294
|
+
kind: 'select',
|
|
295
|
+
distinct: sel.distinct,
|
|
296
|
+
steps,
|
|
297
|
+
where,
|
|
298
|
+
grouped: groupBy !== null || scope.aggregates.length > 0,
|
|
299
|
+
groupBy,
|
|
300
|
+
aggregates: scope.aggregates,
|
|
301
|
+
having,
|
|
302
|
+
windows: scope.windows,
|
|
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,
|
|
308
|
+
orderBy: order,
|
|
309
|
+
needsOutv
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function direction (item) {
|
|
314
|
+
const desc = item.dir === 'DESC'
|
|
315
|
+
let nullsFirst = null
|
|
316
|
+
if (item.nulls !== null) {
|
|
317
|
+
const n = String(item.nulls).toUpperCase()
|
|
318
|
+
if (n !== 'FIRST' && n !== 'LAST') throw new SqlError(`Expected NULLS FIRST or NULLS LAST, got NULLS ${item.nulls}`)
|
|
319
|
+
nullsFirst = n === 'FIRST'
|
|
320
|
+
}
|
|
321
|
+
return { desc, nullsFirst }
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function orderItem (item, scope, cx) {
|
|
325
|
+
const { desc, nullsFirst } = direction(item)
|
|
326
|
+
if (typeof item.expr === 'number') {
|
|
327
|
+
return { expr: { val: ['$outv', item.expr - 1] }, desc, nullsFirst, ordinal: true }
|
|
328
|
+
}
|
|
329
|
+
return { expr: expr(item.expr, scope, cx, { agg: true, win: true, outputs: true }), desc, nullsFirst }
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function planSource (item, scope, cx) {
|
|
333
|
+
const src = item.source
|
|
334
|
+
switch (src.type) {
|
|
335
|
+
case 'Path': {
|
|
336
|
+
const segs = src.path
|
|
337
|
+
const head = segs[0]
|
|
338
|
+
const defaultAlias = [...segs].reverse().find((s) => typeof s === 'string' && s !== '[*]') ?? 'value'
|
|
339
|
+
const alias = item.alias ?? defaultAlias
|
|
340
|
+
if (segs.length === 1 && scope.cteNames.has(head)) {
|
|
341
|
+
return { alias, source: { kind: 'cte', id: scope.cteNames.get(head) } }
|
|
342
|
+
}
|
|
343
|
+
// A path *into* an alias already in scope is a lateral join: `FROM orders o, o.items i`.
|
|
344
|
+
// A bare name is always a table, so `FROM t, t u` and `(SELECT ... FROM t)` inside a
|
|
345
|
+
// query over t mean what they say.
|
|
346
|
+
if (segs.length > 1 && visibleAlias(scope, head)) {
|
|
347
|
+
return { alias, source: { kind: 'expr', expr: ref(segs, scope, cx, {}) } }
|
|
348
|
+
}
|
|
349
|
+
return { alias, source: { kind: 'table', path: segs } }
|
|
350
|
+
}
|
|
351
|
+
case 'Subquery':
|
|
352
|
+
return {
|
|
353
|
+
alias: item.alias ?? 'subquery',
|
|
354
|
+
source: { kind: 'subquery', plan: planQuery(src.query, null, scope.cteNames, cx) }
|
|
355
|
+
}
|
|
356
|
+
case 'Function': {
|
|
357
|
+
const name = String(src.name).toUpperCase()
|
|
358
|
+
const kind = TABLE_FUNCTIONS[name]
|
|
359
|
+
if (!kind) throw new SqlError(`Unknown table function ${src.name}()`)
|
|
360
|
+
const args = src.args.map((a) => expr(a, scope, cx, {}))
|
|
361
|
+
const alias = item.alias ?? (kind === 'unnest' ? 'unnest' : String(src.name).toLowerCase())
|
|
362
|
+
if (kind === 'unnest') {
|
|
363
|
+
if (args.length !== 1) throw new SqlError(`${name}() takes one argument`)
|
|
364
|
+
return { alias, source: { kind: 'expr', expr: args[0] } }
|
|
365
|
+
}
|
|
366
|
+
if (kind === 'series') return { alias, source: { kind: 'expr', expr: { SERIES: args } } }
|
|
367
|
+
return { alias, source: { kind: 'expr', expr: { ENTRIES: args } } }
|
|
368
|
+
}
|
|
369
|
+
default: throw new SqlError(`Unexpected FROM item ${src.type}`)
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function visibleAlias (scope, name) {
|
|
374
|
+
for (let s = scope; s; s = s.outer) if (s.aliases.includes(name)) return true
|
|
375
|
+
return false
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// -- expressions ------------------------------------------------------------------
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Resolve an expression's placeholders.
|
|
382
|
+
*
|
|
383
|
+
* mode.agg aggregates allowed here (select list, HAVING, ORDER BY)
|
|
384
|
+
* mode.win window functions allowed here
|
|
385
|
+
* mode.outputs a bare name may be a select-list alias (ORDER BY)
|
|
386
|
+
* mode.deps collects the aliases the expression reads (for hash joins)
|
|
387
|
+
*/
|
|
388
|
+
function expr (node, scope, cx, mode) {
|
|
389
|
+
if (node === null || typeof node !== 'object') return node
|
|
390
|
+
if (Array.isArray(node)) return node.map((n) => expr(n, scope, cx, mode))
|
|
391
|
+
const key = Object.keys(node)[0]
|
|
392
|
+
const value = node[key]
|
|
393
|
+
switch (key) {
|
|
394
|
+
case '$ref': return ref(value, scope, cx, mode)
|
|
395
|
+
case '$param':
|
|
396
|
+
return { PARAM: [value === null ? cx.positional++ : typeof value === 'number' ? value - 1 : value] }
|
|
397
|
+
case '$call': return call(value, scope, cx, mode)
|
|
398
|
+
case '$subquery': return { SUBQUERY: [subquery(value, scope, cx, mode)] }
|
|
399
|
+
case '$exists': return { EXISTS: [subquery(value, scope, cx, mode)] }
|
|
400
|
+
case '$inSub': return { IN_SUB: [expr(value[0], scope, cx, mode), subquery(value[1], scope, cx, mode)] }
|
|
401
|
+
default: {
|
|
402
|
+
const problem = validateCall(key, Array.isArray(value) ? value : [value])
|
|
403
|
+
if (problem) throw new SqlError(problem)
|
|
404
|
+
return { [key]: Array.isArray(value) ? value.map((n) => expr(n, scope, cx, mode)) : expr(value, scope, cx, mode) }
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function subquery (q, scope, cx, mode) {
|
|
410
|
+
if (mode.deps) mode.deps.add('*')
|
|
411
|
+
const id = cx.subqueries.length
|
|
412
|
+
cx.subqueries.push(null)
|
|
413
|
+
cx.subqueries[id] = planQuery(q, scope.outputOnly ? null : scope, scope.cteNames ?? new Map(), cx)
|
|
414
|
+
return id
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function ref (segs, scope, cx, mode) {
|
|
418
|
+
const head = segs[0]
|
|
419
|
+
|
|
420
|
+
if (mode.outputs && segs.length === 1 && scope.outputs && scope.outputs.has(head)) {
|
|
421
|
+
return { val: ['$out', head] }
|
|
422
|
+
}
|
|
423
|
+
if (mode.inlineOutputs && segs.length === 1 && scope.outputs && scope.outputs.has(head) && !scope.aliases.includes(head)) {
|
|
424
|
+
const out = scope.outputs.get(head)
|
|
425
|
+
const sameColumn = out.ast && out.ast.$ref && out.ast.$ref.length === 1 && out.ast.$ref[0] === head
|
|
426
|
+
if (!sameColumn) return out.logic
|
|
427
|
+
}
|
|
428
|
+
if (scope.outputOnly) {
|
|
429
|
+
if (segs.length === 1) return { val: ['$out', head] }
|
|
430
|
+
throw new SqlError(`Unknown column '${segs.join('.')}' in ORDER BY`)
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
if (typeof head === 'string' && scope.aliases.includes(head)) {
|
|
434
|
+
mode.deps?.add(head)
|
|
435
|
+
return qualified(head, segs.slice(1))
|
|
436
|
+
}
|
|
437
|
+
for (let s = scope.outer; s; s = s.outer) {
|
|
438
|
+
if (s.aliases.includes(head)) {
|
|
439
|
+
markCorrelated(scope, s)
|
|
440
|
+
mode.deps?.add('*')
|
|
441
|
+
return qualified(head, segs.slice(1))
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// Unqualified. Find the nearest scope with any tables in it.
|
|
446
|
+
let target = scope
|
|
447
|
+
while (target && target.aliases.length === 0) target = target.outer
|
|
448
|
+
if (!target) throw new SqlError(`Unknown column '${segs.join('.')}' — there is no FROM clause to read it from`)
|
|
449
|
+
if (target !== scope) { markCorrelated(scope, target); mode.deps?.add('*') }
|
|
450
|
+
|
|
451
|
+
if (target.aliases.length === 1) {
|
|
452
|
+
mode.deps?.add(target.aliases[0])
|
|
453
|
+
return qualified(target.aliases[0], segs)
|
|
454
|
+
}
|
|
455
|
+
for (const a of target.aliases) mode.deps?.add(a)
|
|
456
|
+
const base = { COL: [head, ...target.aliases] }
|
|
457
|
+
return segs.length > 1 ? { PATH: [base, ...segs.slice(1)] } : base
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function qualified (alias, rest) {
|
|
461
|
+
let base
|
|
462
|
+
let remaining
|
|
463
|
+
if (rest.length && rest[0] === 'value') {
|
|
464
|
+
// `x.value` is the element itself when x ranges over plain values
|
|
465
|
+
// (UNNEST of a list of numbers), and the field `value` otherwise.
|
|
466
|
+
base = { ROW_VALUE: [{ val: [alias] }] }
|
|
467
|
+
remaining = rest.slice(1)
|
|
468
|
+
} else if (rest.length && typeof rest[0] === 'string' && rest[0] !== '[*]') {
|
|
469
|
+
base = { val: [alias, rest[0]] }
|
|
470
|
+
remaining = rest.slice(1)
|
|
471
|
+
} else {
|
|
472
|
+
base = { val: [alias] }
|
|
473
|
+
remaining = rest
|
|
474
|
+
}
|
|
475
|
+
return remaining.length ? { PATH: [base, ...remaining] } : base
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function markCorrelated (from, to) {
|
|
479
|
+
for (let s = from; s && s !== to; s = s.outer) s.correlated = true
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function call (c, scope, cx, mode) {
|
|
483
|
+
const name = String(c.name).toUpperCase()
|
|
484
|
+
const inner = { deps: mode.deps }
|
|
485
|
+
|
|
486
|
+
if (c.over) {
|
|
487
|
+
if (!mode.win) throw new SqlError(`Window function ${name}() is not allowed here`)
|
|
488
|
+
if (!aggregates[name] && !windowFunctions.has(name)) throw new SqlError(`${name}() is not a window function`)
|
|
489
|
+
// Window arguments may use aggregates: SUM(SUM(x)) OVER (...).
|
|
490
|
+
const argMode = { agg: mode.agg, deps: mode.deps }
|
|
491
|
+
const spec = {
|
|
492
|
+
name,
|
|
493
|
+
aggregate: Boolean(aggregates[name]),
|
|
494
|
+
star: c.star || (name === 'COUNT' && c.args.length === 0),
|
|
495
|
+
distinct: c.distinct,
|
|
496
|
+
args: c.args.map((a) => expr(a, scope, cx, argMode)),
|
|
497
|
+
partitionBy: (c.over.partitionBy ?? []).map((p) => expr(p, scope, cx, argMode)),
|
|
498
|
+
orderBy: (c.over.orderBy ?? []).map((item) => {
|
|
499
|
+
if (typeof item.expr === 'number') throw new SqlError('Window ORDER BY does not take positions')
|
|
500
|
+
return { expr: expr(item.expr, scope, cx, argMode), ...direction(item) }
|
|
501
|
+
})
|
|
502
|
+
}
|
|
503
|
+
return { val: ['$win', intern(scope.windows, scope.winKeys, spec)] }
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
if (aggregates[name]) {
|
|
507
|
+
if (!mode.agg) throw new SqlError(`Aggregate ${name}() is not allowed here`)
|
|
508
|
+
const spec = {
|
|
509
|
+
name,
|
|
510
|
+
star: c.star || (name === 'COUNT' && c.args.length === 0),
|
|
511
|
+
distinct: c.distinct,
|
|
512
|
+
args: c.args.map((a) => expr(a, scope, cx, inner)),
|
|
513
|
+
filter: c.filter == null ? null : expr(c.filter, scope, cx, inner),
|
|
514
|
+
orderBy: c.orderBy
|
|
515
|
+
? c.orderBy.map((item) => {
|
|
516
|
+
return { expr: expr(item.expr, scope, cx, inner), ...direction(item) }
|
|
517
|
+
})
|
|
518
|
+
: null
|
|
519
|
+
}
|
|
520
|
+
if (aggregates[name].pairs && spec.args.length !== 2) throw new SqlError(`${name}() takes two arguments`)
|
|
521
|
+
if (!spec.star && spec.args.length === 0) throw new SqlError(`${name}() needs an argument`)
|
|
522
|
+
return { val: ['$agg', intern(scope.aggregates, scope.aggKeys, spec)] }
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
if (windowFunctions.has(name)) throw new SqlError(`${name}() needs an OVER (...) clause`)
|
|
526
|
+
if (c.star || c.distinct || c.filter || c.orderBy) throw new SqlError(`${name}() is not an aggregate`)
|
|
527
|
+
if (cx.disabled.has(name)) throw new SqlLimitError(`${name}() is disabled for this query (limits.regex)`, 'regex')
|
|
528
|
+
if (!cx.functions.has(name)) throw new SqlError(`Unknown function ${c.name}()`)
|
|
529
|
+
const problem = validateCall(name, c.args)
|
|
530
|
+
if (problem) throw new SqlError(problem)
|
|
531
|
+
return { [name]: c.args.map((a) => expr(a, scope, cx, mode)) }
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function intern (list, keys, spec) {
|
|
535
|
+
const key = JSON.stringify(spec)
|
|
536
|
+
let i = keys.get(key)
|
|
537
|
+
if (i === undefined) { i = list.length; list.push(spec); keys.set(key, i) }
|
|
538
|
+
return i
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// -- helpers ------------------------------------------------------------------------
|
|
542
|
+
|
|
543
|
+
function splitAnd (node) {
|
|
544
|
+
if (node && node.AND) return node.AND.flatMap(splitAnd)
|
|
545
|
+
return [node]
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/** Dependencies of one side of an EQ conjunct, recomputed from its logic. */
|
|
549
|
+
function depsOf (_all, c, side) {
|
|
550
|
+
return logicDeps(c.logic.EQ[side])
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function logicDeps (logic, out = new Set()) {
|
|
554
|
+
if (logic === null || typeof logic !== 'object') return out
|
|
555
|
+
if (Array.isArray(logic)) { for (const l of logic) logicDeps(l, out); return out }
|
|
556
|
+
const key = Object.keys(logic)[0]
|
|
557
|
+
const v = logic[key]
|
|
558
|
+
if (key === 'val') { if (Array.isArray(v) && typeof v[0] === 'string') out.add(v[0]); return out }
|
|
559
|
+
if (key === 'COL') { for (const a of v.slice(1)) out.add(a); return out }
|
|
560
|
+
if (key === 'SUBQUERY' || key === 'EXISTS' || key === 'IN_SUB' || key === 'PARAM') out.add('*')
|
|
561
|
+
return logicDeps(v, out)
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
const onlyNew = (deps, alias) => deps.size > 0 && [...deps].every((d) => d === alias)
|
|
565
|
+
const notNew = (deps, alias) => !deps.has(alias) && !deps.has('*')
|
|
566
|
+
|
|
567
|
+
function containsAggregate (logic) {
|
|
568
|
+
if (logic === null || typeof logic !== 'object') return false
|
|
569
|
+
if (Array.isArray(logic)) return logic.some(containsAggregate)
|
|
570
|
+
const key = Object.keys(logic)[0]
|
|
571
|
+
if (key === 'val' && Array.isArray(logic.val) && (logic.val[0] === '$agg' || logic.val[0] === '$win')) return true
|
|
572
|
+
return containsAggregate(logic[key])
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
function deriveName (ast, i) {
|
|
576
|
+
if (ast === null || typeof ast !== 'object') return ast === null ? 'NULL' : String(ast)
|
|
577
|
+
if (ast.$ref) {
|
|
578
|
+
const name = [...ast.$ref].reverse().find((s) => typeof s === 'string' && s !== '[*]')
|
|
579
|
+
return name ?? `col${i + 1}`
|
|
580
|
+
}
|
|
581
|
+
if (ast.$call) return String(ast.$call.name).toLowerCase()
|
|
582
|
+
if (ast.CAST) return deriveName(ast.CAST[0], i)
|
|
583
|
+
if (ast.JSON_TEXT || ast.JSON_GET) {
|
|
584
|
+
const k = (ast.JSON_TEXT || ast.JSON_GET)[1]
|
|
585
|
+
if (typeof k === 'string' || typeof k === 'number') return String(k)
|
|
586
|
+
}
|
|
587
|
+
if (ast.$subquery) return `col${i + 1}`
|
|
588
|
+
return `col${i + 1}`
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function syntaxError (source, error) {
|
|
592
|
+
const offset = error.token ? error.token.start : (error.offset ?? source.length)
|
|
593
|
+
const endOffset = error.token ? error.token.end : Math.min(source.length, offset + (error.name === 'LexError' ? 1 : 0))
|
|
594
|
+
const position = (at) => {
|
|
595
|
+
const before = source.slice(0, at)
|
|
596
|
+
return { offset: at, line: before.split('\n').length, column: at - before.lastIndexOf('\n') }
|
|
597
|
+
}
|
|
598
|
+
const { line, column } = position(offset)
|
|
599
|
+
const text = source.split('\n')[line - 1] ?? source
|
|
600
|
+
const what = error.message.replace(/ at offset \d+$/, '')
|
|
601
|
+
const message = `${what} at line ${line}, column ${column}\n ${text}\n ${' '.repeat(Math.max(0, column - 1))}^`
|
|
602
|
+
return new SqlSyntaxError(message, { start: position(offset), end: position(endOffset) })
|
|
603
|
+
}
|
package/src/errors.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Anything wrong with a query: an unknown column, a misplaced aggregate, a bad table. */
|
|
2
|
+
export class SqlError extends Error {
|
|
3
|
+
constructor (message, details) {
|
|
4
|
+
super(message)
|
|
5
|
+
this.name = 'SqlError'
|
|
6
|
+
if (details) Object.assign(this, details)
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** The query text does not parse. Carries `location` ({ start, end } with offset/line/column). */
|
|
11
|
+
export class SqlSyntaxError extends SqlError {
|
|
12
|
+
constructor (message, location) {
|
|
13
|
+
super(message, { location })
|
|
14
|
+
this.name = 'SqlSyntaxError'
|
|
15
|
+
}
|
|
16
|
+
}
|