@narrative.io/data-collaboration-sdk-ts 2.50.0 → 2.52.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/build/nql/NqlBuilder.d.ts +9 -8
- package/build/nql/NqlBuilder.js +67 -60
- package/build/queries/types.d.ts +9 -0
- package/package.json +1 -1
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import type { NqlBudget, NqlPlaceholder } from "./Ast";
|
|
2
2
|
import type { CreateMaterializedView, CreateMaterializedViewMetadata, Deduplication, Explain, Expression, Output, Raw, Select, Statement, Table } from "./AstParser";
|
|
3
|
+
export type DatasetIdToNameMap = Record<number, string>;
|
|
3
4
|
export type CompiledNql = string;
|
|
4
|
-
export declare function compileStatement(n: Statement): CompiledNql;
|
|
5
|
-
export declare function compileCreateMaterializedView(n: CreateMaterializedView): CompiledNql;
|
|
5
|
+
export declare function compileStatement(n: Statement, dsMap?: DatasetIdToNameMap): CompiledNql;
|
|
6
|
+
export declare function compileCreateMaterializedView(n: CreateMaterializedView, dsMap?: DatasetIdToNameMap): CompiledNql;
|
|
6
7
|
export declare function compileCreateMaterializedViewMetadata(metadata?: CreateMaterializedViewMetadata): string;
|
|
7
|
-
export declare function compileExplain(n: Explain): CompiledNql;
|
|
8
|
-
export declare function compileSelect(q: Select): CompiledNql;
|
|
8
|
+
export declare function compileExplain(n: Explain, dsMap?: DatasetIdToNameMap): CompiledNql;
|
|
9
|
+
export declare function compileSelect(q: Select, dsMap?: DatasetIdToNameMap): CompiledNql;
|
|
9
10
|
export declare function compileBudget(budget: NqlBudget): CompiledNql;
|
|
10
|
-
export declare function compileDeduplication(d: Deduplication | Raw): CompiledNql;
|
|
11
|
-
export declare function compileFrom(tables: Table[] | Raw): CompiledNql;
|
|
12
|
-
export declare function compileOutput(n: Output): CompiledNql;
|
|
13
|
-
export declare function compileExpression(n: Expression): CompiledNql;
|
|
11
|
+
export declare function compileDeduplication(d: Deduplication | Raw, dsMap?: DatasetIdToNameMap): CompiledNql;
|
|
12
|
+
export declare function compileFrom(tables: Table[] | Raw, dsMap?: DatasetIdToNameMap): CompiledNql;
|
|
13
|
+
export declare function compileOutput(n: Output, dsMap?: DatasetIdToNameMap): CompiledNql;
|
|
14
|
+
export declare function compileExpression(n: Expression, dsMap?: DatasetIdToNameMap): CompiledNql;
|
|
14
15
|
export declare function compilePlaceholder(n: NqlPlaceholder): CompiledNql;
|
package/build/nql/NqlBuilder.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
const quoteChar = '"';
|
|
2
|
-
export function compileStatement(n) {
|
|
2
|
+
export function compileStatement(n, dsMap) {
|
|
3
3
|
switch (n.type) {
|
|
4
4
|
case "create_materialized_view":
|
|
5
|
-
return compileCreateMaterializedView(n);
|
|
5
|
+
return compileCreateMaterializedView(n, dsMap);
|
|
6
6
|
case "explain":
|
|
7
|
-
return compileExplain(n);
|
|
7
|
+
return compileExplain(n, dsMap);
|
|
8
8
|
default:
|
|
9
9
|
return unreachable(n);
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
-
export function compileCreateMaterializedView(n) {
|
|
13
|
-
return `CREATE MATERIALIZE VIEW ${quote(n.name)}${compileCreateMaterializedViewMetadata(n.metadata)} AS (${compileSelect(n.select)})`;
|
|
12
|
+
export function compileCreateMaterializedView(n, dsMap) {
|
|
13
|
+
return `CREATE MATERIALIZE VIEW ${quote(n.name)}${compileCreateMaterializedViewMetadata(n.metadata)} AS (${compileSelect(n.select, dsMap)})`;
|
|
14
14
|
}
|
|
15
15
|
export function compileCreateMaterializedViewMetadata(metadata) {
|
|
16
16
|
function compileDisplayName() {
|
|
@@ -70,17 +70,17 @@ export function compileCreateMaterializedViewMetadata(metadata) {
|
|
|
70
70
|
}
|
|
71
71
|
return ` ${compileDisplayName()}${compileDescription()}${compileTags()}${compileRefreshSchedule()}${compileExpire()}${compileWriteMode()}${compileExtendedStats()}${compilePartitionBy()}`;
|
|
72
72
|
}
|
|
73
|
-
export function compileExplain(n) {
|
|
74
|
-
return `EXPLAIN ${compileSelect(n.query)}`;
|
|
73
|
+
export function compileExplain(n, dsMap) {
|
|
74
|
+
return `EXPLAIN ${compileSelect(n.query, dsMap)}`;
|
|
75
75
|
}
|
|
76
|
-
export function compileSelect(q) {
|
|
77
|
-
const columns = q.columns.map((e) => compileOutput(e));
|
|
76
|
+
export function compileSelect(q, dsMap) {
|
|
77
|
+
const columns = q.columns.map((e) => compileOutput(e, dsMap));
|
|
78
78
|
const select = `SELECT ${columns.join(", ")}`;
|
|
79
79
|
const budget = q.budget !== null ? compileBudget(q.budget) : "";
|
|
80
|
-
const from = `FROM ${compileFrom(q.from)}`;
|
|
81
|
-
const where = q.where !== null ? `WHERE ${compileExpression(q.where)}` : "";
|
|
80
|
+
const from = `FROM ${compileFrom(q.from, dsMap)}`;
|
|
81
|
+
const where = q.where !== null ? `WHERE ${compileExpression(q.where, dsMap)}` : "";
|
|
82
82
|
const qualify = q.deduplication !== null
|
|
83
|
-
? `QUALIFY ${compileDeduplication(q.deduplication)}`
|
|
83
|
+
? `QUALIFY ${compileDeduplication(q.deduplication, dsMap)}`
|
|
84
84
|
: "";
|
|
85
85
|
return `${select} ${from} ${where} ${qualify} ${budget}`;
|
|
86
86
|
}
|
|
@@ -100,24 +100,30 @@ export function compileBudget(budget) {
|
|
|
100
100
|
}
|
|
101
101
|
return `LIMIT ${budget.amount.value} ${budget.amount.currency.toUpperCase()} ${period}`;
|
|
102
102
|
}
|
|
103
|
-
export function compileDeduplication(d) {
|
|
103
|
+
export function compileDeduplication(d, dsMap) {
|
|
104
104
|
if (d.type === "deduplication") {
|
|
105
|
-
const expressions = d.expressions
|
|
105
|
+
const expressions = d.expressions
|
|
106
|
+
.map((e) => compileExpression(e, dsMap))
|
|
107
|
+
.join(", ");
|
|
106
108
|
return `ROW_NUMBER() OVER (PARTITION BY ${expressions} ORDER BY 0) = 1`;
|
|
107
109
|
}
|
|
108
110
|
return raw(d);
|
|
109
111
|
}
|
|
110
|
-
export function compileFrom(tables) {
|
|
112
|
+
export function compileFrom(tables, dsMap) {
|
|
111
113
|
if (isArray(tables)) {
|
|
112
|
-
return compileTableRefs(tables);
|
|
114
|
+
return compileTableRefs(tables, dsMap);
|
|
113
115
|
}
|
|
114
116
|
return compileRawRef(tables.nql, tables.as);
|
|
115
117
|
}
|
|
116
|
-
function compileTableRefs(tables) {
|
|
118
|
+
function compileTableRefs(tables, dsMap) {
|
|
117
119
|
function ref(table) {
|
|
118
120
|
switch (table.type) {
|
|
119
|
-
case "dataset":
|
|
120
|
-
|
|
121
|
+
case "dataset": {
|
|
122
|
+
const datasetName = dsMap
|
|
123
|
+
? dsMap[table.datasetId]
|
|
124
|
+
: table.datasetId.toString();
|
|
125
|
+
return aliased(`company_data.${quote(datasetName ? datasetName : table.datasetId.toString())}`, table.as, false);
|
|
126
|
+
}
|
|
121
127
|
case "rosetta_stone":
|
|
122
128
|
return aliased("narrative.rosetta_stone", table.as, false);
|
|
123
129
|
case "raw_table":
|
|
@@ -128,7 +134,7 @@ function compileTableRefs(tables) {
|
|
|
128
134
|
throw new Error("unreachable");
|
|
129
135
|
}
|
|
130
136
|
}
|
|
131
|
-
function compileTableRef(table) {
|
|
137
|
+
function compileTableRef(table, dsMap) {
|
|
132
138
|
let stmt = "";
|
|
133
139
|
// NB: by convention the first table in list will never have a join associated with it,
|
|
134
140
|
// hence the leading ' ' below does what we want.
|
|
@@ -189,23 +195,23 @@ function compileTableRefs(tables) {
|
|
|
189
195
|
default:
|
|
190
196
|
unreachable(table.join.conditionType);
|
|
191
197
|
}
|
|
192
|
-
stmt += ` ${compileExpression(table.join.condition)}`;
|
|
198
|
+
stmt += ` ${compileExpression(table.join.condition, dsMap)}`;
|
|
193
199
|
}
|
|
194
200
|
return stmt;
|
|
195
201
|
}
|
|
196
202
|
let nql = "";
|
|
197
203
|
for (const table of tables) {
|
|
198
|
-
const stmt = compileTableRef(table);
|
|
204
|
+
const stmt = compileTableRef(table, dsMap);
|
|
199
205
|
nql += stmt;
|
|
200
206
|
}
|
|
201
207
|
return nql;
|
|
202
208
|
}
|
|
203
|
-
export function compileOutput(n) {
|
|
209
|
+
export function compileOutput(n, dsMap) {
|
|
204
210
|
switch (n.type) {
|
|
205
211
|
case "attribute_ref":
|
|
206
212
|
return compileAttributeRef(n.column, n.as, false);
|
|
207
213
|
case "dataset_column_ref":
|
|
208
|
-
return compileDatasetColumnRef(n.datasetId, n.column, n.as,
|
|
214
|
+
return compileDatasetColumnRef(n.datasetId, n.column, n.as, dsMap);
|
|
209
215
|
case "lit":
|
|
210
216
|
return compileLit(n.value, n.value_type, n.as, false);
|
|
211
217
|
case "raw_ref":
|
|
@@ -217,40 +223,40 @@ export function compileOutput(n) {
|
|
|
217
223
|
}
|
|
218
224
|
throw new Error("unreachable");
|
|
219
225
|
}
|
|
220
|
-
export function compileExpression(n) {
|
|
226
|
+
export function compileExpression(n, dsMap) {
|
|
221
227
|
switch (n.type) {
|
|
222
228
|
case "=":
|
|
223
|
-
return compileBinaryOperator("=", [n.left, n.right], n.as);
|
|
229
|
+
return compileBinaryOperator("=", [n.left, n.right], n.as, dsMap);
|
|
224
230
|
case "<>":
|
|
225
|
-
return compileBinaryOperator("<>", [n.left, n.right], n.as);
|
|
231
|
+
return compileBinaryOperator("<>", [n.left, n.right], n.as, dsMap);
|
|
226
232
|
case "<":
|
|
227
|
-
return compileBinaryOperator("<", [n.left, n.right], n.as);
|
|
233
|
+
return compileBinaryOperator("<", [n.left, n.right], n.as, dsMap);
|
|
228
234
|
case "<=":
|
|
229
|
-
return compileBinaryOperator("<=", [n.left, n.right], n.as);
|
|
235
|
+
return compileBinaryOperator("<=", [n.left, n.right], n.as, dsMap);
|
|
230
236
|
case ">":
|
|
231
|
-
return compileBinaryOperator(">", [n.left, n.right], n.as);
|
|
237
|
+
return compileBinaryOperator(">", [n.left, n.right], n.as, dsMap);
|
|
232
238
|
case ">=":
|
|
233
|
-
return compileBinaryOperator(">=", [n.left, n.right], n.as);
|
|
239
|
+
return compileBinaryOperator(">=", [n.left, n.right], n.as, dsMap);
|
|
234
240
|
case "and":
|
|
235
|
-
return compileBinaryOperator("AND", n.operands, n.as);
|
|
241
|
+
return compileBinaryOperator("AND", n.operands, n.as, dsMap);
|
|
236
242
|
case "attribute_ref":
|
|
237
243
|
return compileAttributeRef(n.column, n.as);
|
|
238
244
|
case "between":
|
|
239
|
-
return compileBetween(n.operand, n.lower, n.upper, n.as);
|
|
245
|
+
return compileBetween(n.operand, n.lower, n.upper, n.as, dsMap);
|
|
240
246
|
case "dataset_column_ref":
|
|
241
|
-
return compileDatasetColumnRef(n.datasetId, n.column, n.as);
|
|
247
|
+
return compileDatasetColumnRef(n.datasetId, n.column, n.as, dsMap);
|
|
242
248
|
case "in":
|
|
243
|
-
return compileIn(n.expression, n.values, n.negated, n.as);
|
|
249
|
+
return compileIn(n.expression, n.values, n.negated, n.as, dsMap);
|
|
244
250
|
case "is_null":
|
|
245
|
-
return compileIsNull(n.operand, n.negated, n.as);
|
|
251
|
+
return compileIsNull(n.operand, n.negated, n.as, dsMap);
|
|
246
252
|
case "like":
|
|
247
|
-
return compileLike(n.value, n.pattern, n.negated, n.as);
|
|
253
|
+
return compileLike(n.value, n.pattern, n.negated, n.as, dsMap);
|
|
248
254
|
case "lit":
|
|
249
255
|
return compileLit(n.value, n.value_type, n.as);
|
|
250
256
|
case "not":
|
|
251
|
-
return compileNot(n.operand, n.as);
|
|
257
|
+
return compileNot(n.operand, n.as, dsMap);
|
|
252
258
|
case "or":
|
|
253
|
-
return compileBinaryOperator("OR", n.operands, n.as);
|
|
259
|
+
return compileBinaryOperator("OR", n.operands, n.as, dsMap);
|
|
254
260
|
case "nql":
|
|
255
261
|
return raw(n);
|
|
256
262
|
case "raw_ref":
|
|
@@ -270,16 +276,16 @@ function compileAttributeRef(column, as, aliasParens) {
|
|
|
270
276
|
}, "");
|
|
271
277
|
return aliased(`narrative.rosetta_stone.${quotedSegments}`, as, aliasParens);
|
|
272
278
|
}
|
|
273
|
-
function compileBetween(operand, lower, upper, as) {
|
|
274
|
-
const operandNql = compileExpression(operand);
|
|
275
|
-
const lowerNql = compileExpression(lower);
|
|
276
|
-
const upperNql = compileExpression(upper);
|
|
279
|
+
function compileBetween(operand, lower, upper, as, dsMap) {
|
|
280
|
+
const operandNql = compileExpression(operand, dsMap);
|
|
281
|
+
const lowerNql = compileExpression(lower, dsMap);
|
|
282
|
+
const upperNql = compileExpression(upper, dsMap);
|
|
277
283
|
return aliased(`${operandNql} BETWEEN ${lowerNql} AND ${upperNql}`, as);
|
|
278
284
|
}
|
|
279
|
-
function compileBinaryOperator(op, operands, as) {
|
|
280
|
-
return aliased(`(${operands.map((e) => compileExpression(e)).join(` ${op} `)})`, as);
|
|
285
|
+
function compileBinaryOperator(op, operands, as, dsMap) {
|
|
286
|
+
return aliased(`(${operands.map((e) => compileExpression(e, dsMap)).join(` ${op} `)})`, as);
|
|
281
287
|
}
|
|
282
|
-
function compileDatasetColumnRef(datasetId, column, as,
|
|
288
|
+
function compileDatasetColumnRef(datasetId, column, as, dsMap) {
|
|
283
289
|
const formattedColumn = column
|
|
284
290
|
.split(".")
|
|
285
291
|
.reduce((acc, segment, index) => {
|
|
@@ -288,22 +294,23 @@ function compileDatasetColumnRef(datasetId, column, as, aliasParen) {
|
|
|
288
294
|
}
|
|
289
295
|
return `${acc}.${quote(segment)}`;
|
|
290
296
|
}, "");
|
|
291
|
-
|
|
297
|
+
const datasetName = dsMap ? dsMap[datasetId] : datasetId.toString();
|
|
298
|
+
return aliased(`company_data.${quote(datasetName ? datasetName : datasetId.toString())}.${formattedColumn}`, as, false);
|
|
292
299
|
}
|
|
293
|
-
function compileIn(expression, values, negated, as) {
|
|
300
|
+
function compileIn(expression, values, negated, as, dsMap) {
|
|
294
301
|
const op = !negated ? "IN" : "NOT IN";
|
|
295
302
|
const compiledValues = isArray(values)
|
|
296
|
-
? `(${values.map((e) => compileExpression(e)).join(", ")})`
|
|
303
|
+
? `(${values.map((e) => compileExpression(e, dsMap)).join(", ")})`
|
|
297
304
|
: raw(values);
|
|
298
|
-
return aliased(`${compileExpression(expression)} ${op} ${compiledValues}`, as);
|
|
305
|
+
return aliased(`${compileExpression(expression, dsMap)} ${op} ${compiledValues}`, as);
|
|
299
306
|
}
|
|
300
|
-
function compileIsNull(operand, negated, as) {
|
|
307
|
+
function compileIsNull(operand, negated, as, dsMap) {
|
|
301
308
|
const op = !negated ? "IS NULL" : "IS NOT NULL";
|
|
302
|
-
return compilePostfixUnaryOperator(op, operand, as);
|
|
309
|
+
return compilePostfixUnaryOperator(op, operand, as, dsMap);
|
|
303
310
|
}
|
|
304
|
-
function compileLike(value, pattern, negated, as) {
|
|
311
|
+
function compileLike(value, pattern, negated, as, dsMap) {
|
|
305
312
|
const op = !negated ? "LIKE" : "NOT LIKE";
|
|
306
|
-
return aliased(`${compileExpression(value)} ${op} ${str(pattern)}`, as);
|
|
313
|
+
return aliased(`${compileExpression(value, dsMap)} ${op} ${str(pattern)}`, as);
|
|
307
314
|
}
|
|
308
315
|
function compileLit(value, valueType, as, aliasParens) {
|
|
309
316
|
let valueNql;
|
|
@@ -339,17 +346,17 @@ function compileLit(value, valueType, as, aliasParens) {
|
|
|
339
346
|
}
|
|
340
347
|
return aliased(valueNql, as, aliasParens);
|
|
341
348
|
}
|
|
342
|
-
function compileNot(operand, as) {
|
|
343
|
-
return aliased(`NOT ${compileExpression(operand)}`, as);
|
|
349
|
+
function compileNot(operand, as, dsMap) {
|
|
350
|
+
return aliased(`NOT ${compileExpression(operand, dsMap)}`, as);
|
|
344
351
|
}
|
|
345
|
-
function compilePostfixUnaryOperator(op, operand, as) {
|
|
346
|
-
return aliased(`(${compileExpression(operand)}) ${op}`, as);
|
|
352
|
+
function compilePostfixUnaryOperator(op, operand, as, dsMap) {
|
|
353
|
+
return aliased(`(${compileExpression(operand, dsMap)}) ${op}`, as);
|
|
347
354
|
}
|
|
348
355
|
function compileRawRef(nql, as) {
|
|
349
356
|
return aliased(nql, as);
|
|
350
357
|
}
|
|
351
|
-
function compileUnaryMinus(operand, as) {
|
|
352
|
-
return aliased(`-(${compileExpression(operand)})`, as);
|
|
358
|
+
function compileUnaryMinus(operand, as, dsMap) {
|
|
359
|
+
return aliased(`-(${compileExpression(operand, dsMap)})`, as);
|
|
353
360
|
}
|
|
354
361
|
export function compilePlaceholder(n) {
|
|
355
362
|
switch (n.expectedType) {
|
package/build/queries/types.d.ts
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import type { CollaboratorsConfig } from "src/types";
|
|
2
2
|
import type * as Parser from "../nql/AstParser";
|
|
3
|
+
export interface CondensedNqlDatasetSource {
|
|
4
|
+
type: "dataset";
|
|
5
|
+
datasetName: string;
|
|
6
|
+
}
|
|
7
|
+
export interface CondensedNqlRosettaSource {
|
|
8
|
+
type: "rosetta_stone";
|
|
9
|
+
}
|
|
10
|
+
export type CondensedNqlSource = CondensedNqlDatasetSource | CondensedNqlRosettaSource;
|
|
3
11
|
export interface NqlQueryAst {
|
|
4
12
|
parsed: Parser.Select;
|
|
5
13
|
compiled: string;
|
|
6
14
|
placeholdersWithContext?: unknown[];
|
|
15
|
+
sources?: CondensedNqlSource[];
|
|
7
16
|
ast_metadata: {
|
|
8
17
|
node_type: "multiple";
|
|
9
18
|
};
|