squirreling 0.12.22 → 0.12.23
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/README.md +2 -2
- package/package.json +1 -1
- package/src/ast.d.ts +1 -0
- package/src/expression/evaluate.js +20 -1
- package/src/parse/joins.js +18 -4
- package/src/plan/columns.js +7 -0
- package/src/plan/plan.js +36 -2
- package/src/types.d.ts +1 -1
- package/src/validation/functions.js +3 -1
- package/src/validation/keywords.js +3 -3
package/README.md
CHANGED
|
@@ -141,7 +141,7 @@ Squirreling mostly follows the SQL standard. The following features are supporte
|
|
|
141
141
|
- `SELECT` statements with `DISTINCT`, `WHERE`, `ORDER BY`, `LIMIT`, `OFFSET`
|
|
142
142
|
- `WITH` clause for Common Table Expressions (CTEs)
|
|
143
143
|
- Subqueries in `SELECT`, `FROM`, `WHERE`, and correlated subqueries
|
|
144
|
-
- `JOIN` operations: `INNER JOIN`, `LEFT JOIN`, `RIGHT JOIN`, `FULL JOIN`, `CROSS JOIN`, `POSITIONAL JOIN`, `LATERAL VIEW [OUTER] EXPLODE(...)`
|
|
144
|
+
- `JOIN` operations: `INNER JOIN`, `LEFT JOIN`, `RIGHT JOIN`, `FULL JOIN`, `CROSS JOIN`, `POSITIONAL JOIN`, `LATERAL VIEW [OUTER] EXPLODE(...)`, with `ON` or `USING (col, ...)` conditions
|
|
145
145
|
- `GROUP BY` and `HAVING` clauses
|
|
146
146
|
- Set operations: `UNION`, `UNION ALL`, `INTERSECT`, `INTERSECT ALL`, `EXCEPT`, `EXCEPT ALL`
|
|
147
147
|
- Expressions: `CASE`, `CAST`, `BETWEEN`, `IN`, `LIKE`, `IS NULL`, `IS NOT NULL`
|
|
@@ -160,7 +160,7 @@ Squirreling mostly follows the SQL standard. The following features are supporte
|
|
|
160
160
|
- Math: `ABS`, `SIGN`, `CEIL`, `FLOOR`, `ROUND`, `MOD`, `RAND`, `RANDOM`, `LN`, `LOG10`, `EXP`, `POWER`, `SQRT`
|
|
161
161
|
- Trig: `SIN`, `COS`, `TAN`, `COT`, `ASIN`, `ACOS`, `ATAN`, `ATAN2`, `DEGREES`, `RADIANS`, `PI`
|
|
162
162
|
- Date: `CURRENT_DATE`, `CURRENT_TIME`, `CURRENT_TIMESTAMP`, `DATE_DIFF`, `DATEDIFF`, `DATE_PART`, `DATE_TRUNC`, `EPOCH`, `EXTRACT`, `INTERVAL`
|
|
163
|
-
- Json: `JSON_VALUE`, `JSON_QUERY`, `JSON_EXTRACT`, `JSON_OBJECT`, `JSON_ARRAY_LENGTH`, `JSON_VALID`, `JSON_TYPE`
|
|
163
|
+
- Json: `JSON_VALUE`, `JSON_QUERY`, `JSON_EXTRACT`, `JSON_OBJECT`, `JSON_ARRAY_LENGTH`, `JSON_VALID`, `JSON_TYPE`, `JSON_KEYS`
|
|
164
164
|
- Array: `ARRAY_LENGTH`, `ARRAY_POSITION`, `ARRAY_CONTAINS`, `ARRAY_SORT`, `ARRAY_APPEND`, `ARRAY_CONCAT`, `LEN`, `CARDINALITY`, `SIZE`
|
|
165
165
|
- Table functions: `UNNEST`, `EXPLODE`, `JSON_EACH`
|
|
166
166
|
- Regex: `REGEXP_SUBSTR`, `REGEXP_EXTRACT`, `REGEXP_REPLACE`, `REGEXP_MATCHES`
|
package/package.json
CHANGED
package/src/ast.d.ts
CHANGED
|
@@ -332,7 +332,7 @@ export async function evaluateExpr({ node, row, rowIndex, rows, context }) {
|
|
|
332
332
|
return values[lower] + (values[upper] - values[lower]) * (pos - lower)
|
|
333
333
|
}
|
|
334
334
|
|
|
335
|
-
if (funcName === 'JSON_ARRAYAGG' || funcName === 'ARRAY_AGG') {
|
|
335
|
+
if (funcName === 'JSON_ARRAYAGG' || funcName === 'ARRAY_AGG' || funcName === 'LIST') {
|
|
336
336
|
const allValues = await evaluateAll(argNode, filteredRows, context)
|
|
337
337
|
if (node.distinct) {
|
|
338
338
|
/** @type {SqlPrimitive[]} */
|
|
@@ -516,6 +516,25 @@ export async function evaluateExpr({ node, row, rowIndex, rows, context }) {
|
|
|
516
516
|
return typeof value
|
|
517
517
|
}
|
|
518
518
|
|
|
519
|
+
if (funcName === 'JSON_KEYS') {
|
|
520
|
+
let value = args[0]
|
|
521
|
+
if (value == null) return null
|
|
522
|
+
if (typeof value === 'string') {
|
|
523
|
+
try {
|
|
524
|
+
value = JSON.parse(value)
|
|
525
|
+
} catch {
|
|
526
|
+
throw new ArgValueError({
|
|
527
|
+
...node,
|
|
528
|
+
message: 'invalid JSON string',
|
|
529
|
+
hint: 'Argument must be valid JSON.',
|
|
530
|
+
rowIndex,
|
|
531
|
+
})
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value) || value instanceof Date) return null
|
|
535
|
+
return Object.keys(value)
|
|
536
|
+
}
|
|
537
|
+
|
|
519
538
|
if (funcName === 'JSON_ARRAY_LENGTH') {
|
|
520
539
|
let arr = args[0]
|
|
521
540
|
if (arr == null) return null
|
package/src/parse/joins.js
CHANGED
|
@@ -222,13 +222,26 @@ export function parseJoins(state) {
|
|
|
222
222
|
const tableTok = expect(state, 'identifier')
|
|
223
223
|
const tableAlias = parseTableAlias(state)
|
|
224
224
|
|
|
225
|
-
// Parse ON condition (not for POSITIONAL joins)
|
|
225
|
+
// Parse ON condition or USING column list (not for POSITIONAL joins)
|
|
226
226
|
/** @type {ExprNode | undefined} */
|
|
227
227
|
let condition
|
|
228
|
+
/** @type {string[] | undefined} */
|
|
229
|
+
let using
|
|
228
230
|
if (joinType !== 'POSITIONAL') {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
231
|
+
if (match(state, 'keyword', 'USING')) {
|
|
232
|
+
expect(state, 'paren', '(')
|
|
233
|
+
using = []
|
|
234
|
+
while (true) {
|
|
235
|
+
const colTok = expect(state, 'identifier')
|
|
236
|
+
using.push(colTok.value)
|
|
237
|
+
if (!match(state, 'comma')) break
|
|
238
|
+
}
|
|
239
|
+
expect(state, 'paren', ')')
|
|
240
|
+
} else {
|
|
241
|
+
expect(state, 'keyword', 'ON')
|
|
242
|
+
condition = parseExpression(state)
|
|
243
|
+
expectNoAggregate(condition, 'JOIN ON')
|
|
244
|
+
}
|
|
232
245
|
}
|
|
233
246
|
|
|
234
247
|
joins.push({
|
|
@@ -236,6 +249,7 @@ export function parseJoins(state) {
|
|
|
236
249
|
table: tableTok.value,
|
|
237
250
|
alias: tableAlias,
|
|
238
251
|
on: condition,
|
|
252
|
+
using,
|
|
239
253
|
positionStart: tok.positionStart,
|
|
240
254
|
positionEnd: tableTok.positionEnd,
|
|
241
255
|
})
|
package/src/plan/columns.js
CHANGED
|
@@ -153,6 +153,13 @@ export function extractColumns({ select, parentColumns }) {
|
|
|
153
153
|
if (sourceAlias !== undefined) visibleLateralAliases.push(sourceAlias)
|
|
154
154
|
for (const join of select.joins) {
|
|
155
155
|
collectColumnsFromExpr(join.on, identifiers)
|
|
156
|
+
// USING columns are equi-join keys on both sides; keep them in every
|
|
157
|
+
// table's needed set so projection pushdown can't prune the join key.
|
|
158
|
+
if (join.using) {
|
|
159
|
+
for (const col of join.using) {
|
|
160
|
+
for (const [, set] of perTable) set?.add(col)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
156
163
|
const joinAlias = join.alias ?? join.table
|
|
157
164
|
if (join.fromFunction) {
|
|
158
165
|
/** @type {IdentifierNode[]} */
|
package/src/plan/plan.js
CHANGED
|
@@ -463,7 +463,10 @@ function planJoin({ left, joins, leftTable, ctePlans, cteColumns, perTableColumn
|
|
|
463
463
|
if (join.joinType === 'POSITIONAL') {
|
|
464
464
|
plan = { type: 'PositionalJoin', leftAlias: currentLeftTable, rightAlias: rightTable, left: plan, right: rightScan }
|
|
465
465
|
} else {
|
|
466
|
-
|
|
466
|
+
// `USING (cols)` desugars to an equi-condition `left.col = right.col` per
|
|
467
|
+
// column, which routes through the hash-join path like any other ON.
|
|
468
|
+
const condition = join.on ?? (join.using && buildUsingCondition(join.using, join))
|
|
469
|
+
const keys = condition && extractEquiKeys({ condition, leftTable: currentLeftTable, rightTable })
|
|
467
470
|
if (keys) {
|
|
468
471
|
/** @type {HashJoinNode} */
|
|
469
472
|
const hashJoin = {
|
|
@@ -484,7 +487,7 @@ function planJoin({ left, joins, leftTable, ctePlans, cteColumns, perTableColumn
|
|
|
484
487
|
joinType: join.joinType,
|
|
485
488
|
leftAlias: currentLeftTable,
|
|
486
489
|
rightAlias: rightTable,
|
|
487
|
-
condition
|
|
490
|
+
condition,
|
|
488
491
|
left: plan,
|
|
489
492
|
right: rightScan,
|
|
490
493
|
}
|
|
@@ -623,6 +626,37 @@ function normalizeIdentifiers(node, sourceColumns) {
|
|
|
623
626
|
return node
|
|
624
627
|
}
|
|
625
628
|
|
|
629
|
+
/**
|
|
630
|
+
* Builds the join condition for a `JOIN ... USING (cols)` clause: an AND of
|
|
631
|
+
* `col = col` equalities using unprefixed identifiers. The hash-join path
|
|
632
|
+
* evaluates the left key against the left row and the right key against the
|
|
633
|
+
* right row, so each unqualified name resolves unambiguously on its own side.
|
|
634
|
+
*
|
|
635
|
+
* @param {string[]} using - shared column names from the USING clause
|
|
636
|
+
* @param {{ positionStart: number, positionEnd: number }} pos - position info for the synthesized exprs
|
|
637
|
+
* @returns {ExprNode | undefined}
|
|
638
|
+
*/
|
|
639
|
+
function buildUsingCondition(using, pos) {
|
|
640
|
+
const { positionStart, positionEnd } = pos
|
|
641
|
+
/** @type {ExprNode | undefined} */
|
|
642
|
+
let condition
|
|
643
|
+
for (const col of using) {
|
|
644
|
+
/** @type {ExprNode} */
|
|
645
|
+
const eq = {
|
|
646
|
+
type: 'binary',
|
|
647
|
+
op: '=',
|
|
648
|
+
left: { type: 'identifier', name: col, positionStart, positionEnd },
|
|
649
|
+
right: { type: 'identifier', name: col, positionStart, positionEnd },
|
|
650
|
+
positionStart,
|
|
651
|
+
positionEnd,
|
|
652
|
+
}
|
|
653
|
+
condition = condition === undefined
|
|
654
|
+
? eq
|
|
655
|
+
: { type: 'binary', op: 'AND', left: condition, right: eq, positionStart, positionEnd }
|
|
656
|
+
}
|
|
657
|
+
return condition
|
|
658
|
+
}
|
|
659
|
+
|
|
626
660
|
/**
|
|
627
661
|
* Splits a join ON expression into equi-key pairs and a residual predicate so
|
|
628
662
|
* the planner can route AND-of-equis (with optional range/inequality
|
package/src/types.d.ts
CHANGED
|
@@ -129,7 +129,7 @@ export interface UserDefinedFunction {
|
|
|
129
129
|
arguments: FunctionSignature
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
export type AggregateFunc = 'COUNT' | 'COUNTIF' | 'SUM' | 'AVG' | 'MIN' | 'MAX' | 'ARRAY_AGG' | 'JSON_ARRAYAGG' | 'STDDEV_SAMP' | 'STDDEV_POP' | 'MEDIAN' | 'PERCENTILE_CONT' | 'APPROX_QUANTILE' | 'STRING_AGG'
|
|
132
|
+
export type AggregateFunc = 'COUNT' | 'COUNTIF' | 'SUM' | 'AVG' | 'MIN' | 'MAX' | 'ARRAY_AGG' | 'LIST' | 'JSON_ARRAYAGG' | 'STDDEV_SAMP' | 'STDDEV_POP' | 'MEDIAN' | 'PERCENTILE_CONT' | 'APPROX_QUANTILE' | 'STRING_AGG'
|
|
133
133
|
|
|
134
134
|
export type RegExpFunction = 'REGEXP_SUBSTR' | 'REGEXP_EXTRACT' | 'REGEXP_REPLACE' | 'REGEXP_MATCHES'
|
|
135
135
|
|
|
@@ -11,7 +11,7 @@ export const niladicFuncs = ['CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP'
|
|
|
11
11
|
* @returns {name is AggregateFunc}
|
|
12
12
|
*/
|
|
13
13
|
export function isAggregateFunc(name) {
|
|
14
|
-
return ['COUNT', 'COUNTIF', 'SUM', 'AVG', 'MIN', 'MAX', 'ARRAY_AGG', 'JSON_ARRAYAGG', 'STDDEV_SAMP', 'STDDEV_POP', 'MEDIAN', 'PERCENTILE_CONT', 'APPROX_QUANTILE', 'STRING_AGG'].includes(name)
|
|
14
|
+
return ['COUNT', 'COUNTIF', 'SUM', 'AVG', 'MIN', 'MAX', 'ARRAY_AGG', 'LIST', 'JSON_ARRAYAGG', 'STDDEV_SAMP', 'STDDEV_POP', 'MEDIAN', 'PERCENTILE_CONT', 'APPROX_QUANTILE', 'STRING_AGG'].includes(name)
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/**
|
|
@@ -179,8 +179,10 @@ export const FUNCTION_SIGNATURES = {
|
|
|
179
179
|
JSON_ARRAY_LENGTH: { min: 1, max: 1, signature: 'array' },
|
|
180
180
|
JSON_VALID: { min: 1, max: 1, signature: 'value' },
|
|
181
181
|
JSON_TYPE: { min: 1, max: 1, signature: 'value' },
|
|
182
|
+
JSON_KEYS: { min: 1, max: 1, signature: 'value' },
|
|
182
183
|
JSON_ARRAYAGG: { min: 1, max: 1, signature: 'expression' },
|
|
183
184
|
ARRAY_AGG: { min: 1, max: 1, signature: 'expression' },
|
|
185
|
+
LIST: { min: 1, max: 1, signature: 'expression' },
|
|
184
186
|
|
|
185
187
|
// Array functions
|
|
186
188
|
ARRAY_LENGTH: { min: 1, max: 2, signature: 'array[, dimension]' },
|
|
@@ -3,7 +3,7 @@ export const KEYWORDS = new Set([
|
|
|
3
3
|
'HAVING', 'ORDER', 'ASC', 'DESC', 'NULLS', 'LIMIT', 'OFFSET', 'AS', 'ALL',
|
|
4
4
|
'DISTINCT', 'TRUE', 'FALSE', 'NULL', 'LIKE', 'IN', 'EXISTS', 'BETWEEN',
|
|
5
5
|
'CASE', 'WHEN', 'THEN', 'ELSE', 'END', 'JOIN', 'INNER', 'LEFT', 'RIGHT',
|
|
6
|
-
'FULL', 'OUTER', 'CROSS', 'POSITIONAL', 'LATERAL', 'VIEW', 'ON', 'INTERVAL', 'DAY', 'MONTH', 'YEAR',
|
|
6
|
+
'FULL', 'OUTER', 'CROSS', 'POSITIONAL', 'LATERAL', 'VIEW', 'ON', 'USING', 'INTERVAL', 'DAY', 'MONTH', 'YEAR',
|
|
7
7
|
'HOUR', 'MINUTE', 'SECOND', 'FILTER', 'WITHIN',
|
|
8
8
|
'UNION', 'INTERSECT', 'EXCEPT',
|
|
9
9
|
])
|
|
@@ -17,7 +17,7 @@ export const RESERVED_KEYWORDS = new Set([
|
|
|
17
17
|
'EXISTS', 'CASE', 'WHEN', 'THEN', 'ELSE', 'END', 'INTERVAL',
|
|
18
18
|
'GROUP', 'BY', 'HAVING', 'ORDER', 'LIMIT', 'OFFSET',
|
|
19
19
|
'AS', 'ALL', 'DISTINCT',
|
|
20
|
-
'JOIN', 'INNER', 'LEFT', 'RIGHT', 'FULL', 'OUTER', 'ON',
|
|
20
|
+
'JOIN', 'INNER', 'LEFT', 'RIGHT', 'FULL', 'OUTER', 'ON', 'USING',
|
|
21
21
|
'UNION', 'INTERSECT', 'EXCEPT',
|
|
22
22
|
])
|
|
23
23
|
|
|
@@ -30,6 +30,6 @@ export const RESERVED_AFTER_COLUMN = new Set([
|
|
|
30
30
|
// Keywords that cannot be used as table aliases
|
|
31
31
|
export const RESERVED_AFTER_TABLE = new Set([
|
|
32
32
|
'WHERE', 'GROUP', 'HAVING', 'ORDER', 'LIMIT', 'OFFSET', 'JOIN', 'INNER',
|
|
33
|
-
'LEFT', 'RIGHT', 'FULL', 'CROSS', 'ON', 'POSITIONAL', 'LATERAL',
|
|
33
|
+
'LEFT', 'RIGHT', 'FULL', 'CROSS', 'ON', 'USING', 'POSITIONAL', 'LATERAL',
|
|
34
34
|
'UNION', 'INTERSECT', 'EXCEPT',
|
|
35
35
|
])
|