@uwdata/mosaic-sql 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mosaic-sql.js +2242 -1064
- package/dist/mosaic-sql.min.js +1 -1
- package/dist/types/ast/aggregate.d.ts +70 -0
- package/dist/types/ast/between-op.d.ts +46 -0
- package/dist/types/ast/binary-op.d.ts +28 -0
- package/dist/types/ast/case.d.ts +68 -0
- package/dist/types/ast/cast.d.ts +21 -0
- package/dist/types/ast/column-param.d.ts +17 -0
- package/dist/types/ast/column-ref.d.ts +39 -0
- package/dist/types/ast/fragment.d.ts +14 -0
- package/dist/types/ast/from.d.ts +21 -0
- package/dist/types/ast/function.d.ts +21 -0
- package/dist/types/ast/in-op.d.ts +21 -0
- package/dist/types/ast/interval.d.ts +21 -0
- package/dist/types/ast/literal.d.ts +15 -0
- package/dist/types/ast/logical-op.d.ts +46 -0
- package/dist/types/ast/node.d.ts +24 -0
- package/dist/types/ast/order-by.d.ts +29 -0
- package/dist/types/ast/param.d.ts +19 -0
- package/dist/types/ast/query.d.ts +268 -0
- package/dist/types/ast/sample.d.ts +42 -0
- package/dist/types/ast/select.d.ts +22 -0
- package/dist/types/ast/table-ref.d.ts +25 -0
- package/dist/types/ast/unary-op.d.ts +39 -0
- package/dist/types/ast/verbatim.d.ts +9 -0
- package/dist/types/ast/window.d.ts +177 -0
- package/dist/types/ast/with.d.ts +22 -0
- package/dist/types/constants.d.ts +38 -0
- package/dist/types/functions/aggregate.d.ts +229 -0
- package/dist/types/functions/case.d.ts +15 -0
- package/dist/types/functions/cast.d.ts +26 -0
- package/dist/types/functions/column.d.ts +9 -0
- package/dist/types/functions/datetime.d.ts +44 -0
- package/dist/types/functions/literal.d.ts +16 -0
- package/dist/types/functions/numeric.d.ts +93 -0
- package/dist/types/functions/operators.d.ts +198 -0
- package/dist/types/functions/order-by.d.ts +17 -0
- package/dist/types/functions/spatial.d.ts +37 -0
- package/dist/types/functions/sql-template-tag.d.ts +16 -0
- package/dist/types/functions/string.d.ts +55 -0
- package/dist/types/functions/table-ref.d.ts +9 -0
- package/dist/types/functions/window.d.ts +87 -0
- package/dist/types/index-types.d.ts +2 -0
- package/dist/types/index.d.ts +53 -0
- package/dist/types/load/create.d.ts +8 -0
- package/dist/types/load/extension.d.ts +1 -0
- package/dist/types/load/load.d.ts +12 -0
- package/dist/types/load/sql-from.d.ts +11 -0
- package/dist/types/transforms/bin-1d.d.ts +14 -0
- package/dist/types/transforms/bin-2d.d.ts +18 -0
- package/dist/types/transforms/bin-linear-1d.d.ts +9 -0
- package/dist/types/transforms/bin-linear-2d.d.ts +18 -0
- package/dist/types/transforms/line-density.d.ts +23 -0
- package/dist/types/transforms/m4.d.ts +18 -0
- package/dist/types/transforms/scales.d.ts +1 -0
- package/dist/types/types.d.ts +59 -0
- package/dist/types/util/ast.d.ts +60 -0
- package/dist/types/util/function.d.ts +54 -0
- package/dist/types/util/string.d.ts +3 -0
- package/dist/types/util/type-check.d.ts +18 -0
- package/dist/types/visit/recurse.d.ts +28 -0
- package/dist/types/visit/rewrite.d.ts +10 -0
- package/dist/types/visit/visitors.d.ts +33 -0
- package/dist/types/visit/walk.d.ts +7 -0
- package/jsconfig.json +11 -0
- package/package.json +6 -4
- package/src/ast/aggregate.js +164 -0
- package/src/ast/between-op.js +75 -0
- package/src/ast/binary-op.js +40 -0
- package/src/ast/case.js +105 -0
- package/src/ast/cast.js +34 -0
- package/src/ast/column-param.js +29 -0
- package/src/ast/column-ref.js +72 -0
- package/src/ast/fragment.js +26 -0
- package/src/ast/from.js +40 -0
- package/src/ast/function.js +34 -0
- package/src/ast/in-op.js +33 -0
- package/src/ast/interval.js +33 -0
- package/src/ast/literal.js +55 -0
- package/src/ast/logical-op.js +67 -0
- package/src/ast/node.js +29 -0
- package/src/ast/order-by.js +48 -0
- package/src/ast/param.js +35 -0
- package/src/ast/query.js +578 -0
- package/src/ast/sample.js +53 -0
- package/src/ast/select.js +44 -0
- package/src/ast/table-ref.js +44 -0
- package/src/ast/unary-op.js +64 -0
- package/src/ast/verbatim.js +26 -0
- package/src/ast/window.js +290 -0
- package/src/ast/with.js +30 -0
- package/src/constants.js +44 -0
- package/src/functions/aggregate.js +335 -0
- package/src/functions/case.js +21 -0
- package/src/functions/cast.js +39 -0
- package/src/functions/column.js +20 -0
- package/src/functions/datetime.js +65 -0
- package/src/functions/literal.js +22 -0
- package/src/functions/numeric.js +139 -0
- package/src/functions/operators.js +298 -0
- package/src/functions/order-by.js +24 -0
- package/src/functions/spatial.js +56 -0
- package/src/functions/sql-template-tag.js +51 -0
- package/src/functions/string.js +82 -0
- package/src/functions/table-ref.js +14 -0
- package/src/functions/window.js +121 -0
- package/src/index-types.ts +2 -0
- package/src/index.js +57 -155
- package/src/load/create.js +10 -2
- package/src/load/load.js +4 -4
- package/src/load/sql-from.js +7 -6
- package/src/transforms/bin-1d.js +21 -0
- package/src/transforms/bin-2d.js +29 -0
- package/src/transforms/bin-linear-1d.js +26 -0
- package/src/transforms/bin-linear-2d.js +71 -0
- package/src/transforms/line-density.js +113 -0
- package/src/transforms/m4.js +38 -0
- package/src/{scales.js → transforms/scales.js} +31 -17
- package/src/types.ts +96 -0
- package/src/util/ast.js +96 -0
- package/src/util/function.js +78 -0
- package/src/util/string.js +16 -0
- package/src/util/type-check.js +29 -0
- package/src/visit/recurse.js +57 -0
- package/src/visit/rewrite.js +32 -0
- package/src/visit/visitors.js +108 -0
- package/src/visit/walk.js +30 -0
- package/tsconfig.json +12 -0
- package/src/Query.js +0 -593
- package/src/aggregates.js +0 -185
- package/src/cast.js +0 -19
- package/src/datetime.js +0 -31
- package/src/desc.js +0 -13
- package/src/expression.js +0 -170
- package/src/functions.js +0 -25
- package/src/literal.js +0 -6
- package/src/operators.js +0 -54
- package/src/ref.js +0 -109
- package/src/repeat.js +0 -3
- package/src/spatial.js +0 -10
- package/src/to-sql.js +0 -52
- package/src/windows.js +0 -239
package/src/windows.js
DELETED
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
import { SQLExpression, isParamLike, sql } from './expression.js';
|
|
2
|
-
import { functionCall } from './functions.js';
|
|
3
|
-
import { asColumn } from './ref.js';
|
|
4
|
-
import { repeat } from './repeat.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Base class for individual window functions.
|
|
8
|
-
* Most callers should use a dedicated window function
|
|
9
|
-
* rather than instantiate this class.
|
|
10
|
-
*/
|
|
11
|
-
export class WindowFunction extends SQLExpression {
|
|
12
|
-
/**
|
|
13
|
-
* Create a new WindowFunction instance.
|
|
14
|
-
* @param {string} op The window operation indicator.
|
|
15
|
-
* @param {*} func The window function expression.
|
|
16
|
-
* @param {*} [type] The SQL data type to cast to.
|
|
17
|
-
* @param {string} [name] The window definition name.
|
|
18
|
-
* @param {*} [group] Grouping (partition by) expressions.
|
|
19
|
-
* @param {*} [order] Sorting (order by) expressions.
|
|
20
|
-
* @param {*} [frame] The window frame definition.
|
|
21
|
-
*/
|
|
22
|
-
constructor(op, func, type, name, group = '', order = '', frame = '') {
|
|
23
|
-
// build and parse expression
|
|
24
|
-
let expr;
|
|
25
|
-
const noWindowParams = !(group || order || frame);
|
|
26
|
-
if (name && noWindowParams) {
|
|
27
|
-
expr = name ? sql`${func} OVER "${name}"` : sql`${func} OVER ()`;
|
|
28
|
-
} else {
|
|
29
|
-
const s1 = group && order ? ' ' : '';
|
|
30
|
-
const s2 = (group || order) && frame ? ' ' : '';
|
|
31
|
-
expr = sql`${func} OVER (${name ? `"${name}" ` : ''}${group}${s1}${order}${s2}${frame})`;
|
|
32
|
-
}
|
|
33
|
-
if (type) {
|
|
34
|
-
expr = sql`(${expr})::${type}`;
|
|
35
|
-
}
|
|
36
|
-
const { _expr, _deps } = expr;
|
|
37
|
-
super(_expr, _deps);
|
|
38
|
-
this.window = op;
|
|
39
|
-
this.func = func;
|
|
40
|
-
this.type = type;
|
|
41
|
-
this.name = name;
|
|
42
|
-
this.group = group;
|
|
43
|
-
this.order = order;
|
|
44
|
-
this.frame = frame;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
get basis() {
|
|
48
|
-
return this.column;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
get label() {
|
|
52
|
-
const { func } = this;
|
|
53
|
-
return func.label ?? func.toString();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Return an updated window function over a named window definition.
|
|
58
|
-
* @param {string} name The window definition name.
|
|
59
|
-
* @returns {WindowFunction} A new window function.
|
|
60
|
-
*/
|
|
61
|
-
over(name) {
|
|
62
|
-
const { window: op, func, type, group, order, frame } = this;
|
|
63
|
-
return new WindowFunction(op, func, type, name, group, order, frame);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Return an updated window function with the given partitioning.
|
|
68
|
-
* @param {*} expr The grouping (partition by) criteria for the window function.
|
|
69
|
-
* @returns {WindowFunction} A new window function.
|
|
70
|
-
*/
|
|
71
|
-
partitionby(...expr) {
|
|
72
|
-
const exprs = expr.flat().filter(x => x).map(asColumn);
|
|
73
|
-
const group = sql(
|
|
74
|
-
['PARTITION BY ', repeat(exprs.length - 1, ', '), ''],
|
|
75
|
-
...exprs
|
|
76
|
-
);
|
|
77
|
-
const { window: op, func, type, name, order, frame } = this;
|
|
78
|
-
return new WindowFunction(op, func, type, name, group, order, frame);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Return an updated window function with the given ordering.
|
|
83
|
-
* @param {*} expr The sorting (order by) criteria for the window function.
|
|
84
|
-
* @returns {WindowFunction} A new window function.
|
|
85
|
-
*/
|
|
86
|
-
orderby(...expr) {
|
|
87
|
-
const exprs = expr.flat().filter(x => x).map(asColumn);
|
|
88
|
-
const order = sql(
|
|
89
|
-
['ORDER BY ', repeat(exprs.length - 1, ', '), ''],
|
|
90
|
-
...exprs
|
|
91
|
-
);
|
|
92
|
-
const { window: op, func, type, name, group, frame } = this;
|
|
93
|
-
return new WindowFunction(op, func, type, name, group, order, frame);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Return an updated window function with the given rows frame.
|
|
98
|
-
* @param {(number|null)[] | import('./expression.js').ParamLike} expr The row-based window frame.
|
|
99
|
-
* @returns {WindowFunction} A new window function.
|
|
100
|
-
*/
|
|
101
|
-
rows(expr) {
|
|
102
|
-
const frame = windowFrame('ROWS', expr);
|
|
103
|
-
const { window: op, func, type, name, group, order } = this;
|
|
104
|
-
return new WindowFunction(op, func, type, name, group, order, frame);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Return an updated window function with the given range frame.
|
|
109
|
-
* @param {(number|null)[] | import('./expression.js').ParamLike} expr The range-based window frame.
|
|
110
|
-
* @returns {WindowFunction} A new window function.
|
|
111
|
-
*/
|
|
112
|
-
range(expr) {
|
|
113
|
-
const frame = windowFrame('RANGE', expr);
|
|
114
|
-
const { window: op, func, type, name, group, order } = this;
|
|
115
|
-
return new WindowFunction(op, func, type, name, group, order, frame);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function windowFrame(type, frame) {
|
|
120
|
-
if (isParamLike(frame)) {
|
|
121
|
-
const expr = sql`${frame}`;
|
|
122
|
-
expr.toString = () => `${type} ${frameToSQL(frame.value)}`;
|
|
123
|
-
return expr;
|
|
124
|
-
}
|
|
125
|
-
return `${type} ${frameToSQL(frame)}`;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function frameToSQL(frame) {
|
|
129
|
-
const [prev, next] = frame;
|
|
130
|
-
const a = prev === 0 ? 'CURRENT ROW'
|
|
131
|
-
: Number.isFinite(prev) ? `${Math.abs(prev)} PRECEDING`
|
|
132
|
-
: 'UNBOUNDED PRECEDING';
|
|
133
|
-
const b = next === 0 ? 'CURRENT ROW'
|
|
134
|
-
: Number.isFinite(next) ? `${Math.abs(next)} FOLLOWING`
|
|
135
|
-
: 'UNBOUNDED FOLLOWING';
|
|
136
|
-
return `BETWEEN ${a} AND ${b}`;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export function winf(op, type) {
|
|
140
|
-
return (...values) => {
|
|
141
|
-
const func = functionCall(op)(...values);
|
|
142
|
-
return new WindowFunction(op, func, type);
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Create a window function that returns the number of the current row
|
|
148
|
-
* within the partition, counting from 1.
|
|
149
|
-
* @returns {WindowFunction} The generated window function.
|
|
150
|
-
*/
|
|
151
|
-
export const row_number = winf('ROW_NUMBER', 'INTEGER');
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Create a window function that returns the rank of the current row with gaps.
|
|
155
|
-
* This is the same as the row_number of its first peer.
|
|
156
|
-
* @returns {WindowFunction} The generated window function.
|
|
157
|
-
*/
|
|
158
|
-
export const rank = winf('RANK', 'INTEGER');
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Create a window function that returns the rank of the current row without gaps,
|
|
162
|
-
* The function counts peer groups.
|
|
163
|
-
* @returns {WindowFunction} The generated window function.
|
|
164
|
-
*/
|
|
165
|
-
export const dense_rank = winf('DENSE_RANK', 'INTEGER');
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Create a window function that returns the relative rank of the current row.
|
|
169
|
-
* (rank() - 1) / (total partition rows - 1).
|
|
170
|
-
* @returns {WindowFunction} The generated window function.
|
|
171
|
-
*/
|
|
172
|
-
export const percent_rank = winf('PERCENT_RANK');
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Create a window function that returns the cumulative distribution.
|
|
176
|
-
* (number of preceding or peer partition rows) / total partition rows.
|
|
177
|
-
* @returns {WindowFunction} The generated window function.
|
|
178
|
-
*/
|
|
179
|
-
export const cume_dist = winf('CUME_DIST');
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Create a window function that r an integer ranging from 1 to the argument
|
|
183
|
-
* value, dividing the partition as equally as possible.
|
|
184
|
-
* @param {number|SQLExpression} num_buckets The number of quantile buckets.
|
|
185
|
-
* @returns {WindowFunction} The generated window function.
|
|
186
|
-
*/
|
|
187
|
-
export const ntile = winf('NTILE');
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Create a window function that returns the expression evaluated at the row
|
|
191
|
-
* that is offset rows before the current row within the partition.
|
|
192
|
-
* If there is no such row, instead return default (which must be of the same
|
|
193
|
-
* type as the expression). Both offset and default are evaluated with respect
|
|
194
|
-
* to the current row. If omitted, offset defaults to 1 and default to null.
|
|
195
|
-
* @param {string|SQLExpression} expr The expression to evaluate.
|
|
196
|
-
* @param {number|SQLExpression} offset The row offset.
|
|
197
|
-
* @param {*} default The default value.
|
|
198
|
-
* @returns {WindowFunction} The generated window function.
|
|
199
|
-
*/
|
|
200
|
-
export const lag = winf('LAG');
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Create a window function that returns the expression evaluated at the row
|
|
204
|
-
* that is offset rows after the current row within the partition.
|
|
205
|
-
* If there is no such row, instead return default (which must be of the same
|
|
206
|
-
* type as the expression). Both offset and default are evaluated with respect
|
|
207
|
-
* to the current row. If omitted, offset defaults to 1 and default to null.
|
|
208
|
-
* @param {string|SQLExpression} expr The expression to evaluate.
|
|
209
|
-
* @param {number|SQLExpression} offset The row offset.
|
|
210
|
-
* @param {*} default The default value.
|
|
211
|
-
* @returns {WindowFunction} The generated window function.
|
|
212
|
-
*/
|
|
213
|
-
export const lead = winf('LEAD');
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Create a window function that returns the expression evaluated at the row
|
|
217
|
-
* that is the first row of the window frame.
|
|
218
|
-
* @param {string|SQLExpression} expr The expression to evaluate.
|
|
219
|
-
* @returns {WindowFunction} The generated window function.
|
|
220
|
-
*/
|
|
221
|
-
export const first_value = winf('FIRST_VALUE');
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Create a window function that returns the expression evaluated at the row
|
|
225
|
-
* that is the last row of the window frame.
|
|
226
|
-
* @param {string|SQLExpression} expr The expression to evaluate.
|
|
227
|
-
* @returns {WindowFunction} The generated window function.
|
|
228
|
-
*/
|
|
229
|
-
|
|
230
|
-
export const last_value = winf('LAST_VALUE');
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Create a window function that returns the expression evaluated at the
|
|
234
|
-
* nth row of the window frame (counting from 1), or null if no such row.
|
|
235
|
-
* @param {string|SQLExpression} expr The expression to evaluate.
|
|
236
|
-
* @param {number|SQLExpression} nth The 1-based window frame index.
|
|
237
|
-
* @returns {WindowFunction} The generated window function.
|
|
238
|
-
*/
|
|
239
|
-
export const nth_value = winf('NTH_VALUE');
|