@uwdata/mosaic-sql 0.11.0 → 0.12.1

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.
Files changed (143) hide show
  1. package/README.md +2 -0
  2. package/dist/mosaic-sql.js +2242 -1064
  3. package/dist/mosaic-sql.min.js +1 -1
  4. package/dist/types/ast/aggregate.d.ts +70 -0
  5. package/dist/types/ast/between-op.d.ts +46 -0
  6. package/dist/types/ast/binary-op.d.ts +28 -0
  7. package/dist/types/ast/case.d.ts +68 -0
  8. package/dist/types/ast/cast.d.ts +21 -0
  9. package/dist/types/ast/column-param.d.ts +17 -0
  10. package/dist/types/ast/column-ref.d.ts +39 -0
  11. package/dist/types/ast/fragment.d.ts +14 -0
  12. package/dist/types/ast/from.d.ts +21 -0
  13. package/dist/types/ast/function.d.ts +21 -0
  14. package/dist/types/ast/in-op.d.ts +21 -0
  15. package/dist/types/ast/interval.d.ts +21 -0
  16. package/dist/types/ast/literal.d.ts +15 -0
  17. package/dist/types/ast/logical-op.d.ts +46 -0
  18. package/dist/types/ast/node.d.ts +24 -0
  19. package/dist/types/ast/order-by.d.ts +29 -0
  20. package/dist/types/ast/param.d.ts +19 -0
  21. package/dist/types/ast/query.d.ts +268 -0
  22. package/dist/types/ast/sample.d.ts +42 -0
  23. package/dist/types/ast/select.d.ts +22 -0
  24. package/dist/types/ast/table-ref.d.ts +25 -0
  25. package/dist/types/ast/unary-op.d.ts +39 -0
  26. package/dist/types/ast/verbatim.d.ts +9 -0
  27. package/dist/types/ast/window.d.ts +177 -0
  28. package/dist/types/ast/with.d.ts +22 -0
  29. package/dist/types/constants.d.ts +38 -0
  30. package/dist/types/functions/aggregate.d.ts +229 -0
  31. package/dist/types/functions/case.d.ts +15 -0
  32. package/dist/types/functions/cast.d.ts +26 -0
  33. package/dist/types/functions/column.d.ts +9 -0
  34. package/dist/types/functions/datetime.d.ts +44 -0
  35. package/dist/types/functions/literal.d.ts +16 -0
  36. package/dist/types/functions/numeric.d.ts +93 -0
  37. package/dist/types/functions/operators.d.ts +198 -0
  38. package/dist/types/functions/order-by.d.ts +17 -0
  39. package/dist/types/functions/spatial.d.ts +37 -0
  40. package/dist/types/functions/sql-template-tag.d.ts +16 -0
  41. package/dist/types/functions/string.d.ts +55 -0
  42. package/dist/types/functions/table-ref.d.ts +9 -0
  43. package/dist/types/functions/window.d.ts +87 -0
  44. package/dist/types/index-types.d.ts +2 -0
  45. package/dist/types/index.d.ts +53 -0
  46. package/dist/types/load/create.d.ts +8 -0
  47. package/dist/types/load/extension.d.ts +1 -0
  48. package/dist/types/load/load.d.ts +12 -0
  49. package/dist/types/load/sql-from.d.ts +11 -0
  50. package/dist/types/transforms/bin-1d.d.ts +14 -0
  51. package/dist/types/transforms/bin-2d.d.ts +18 -0
  52. package/dist/types/transforms/bin-linear-1d.d.ts +9 -0
  53. package/dist/types/transforms/bin-linear-2d.d.ts +18 -0
  54. package/dist/types/transforms/line-density.d.ts +23 -0
  55. package/dist/types/transforms/m4.d.ts +18 -0
  56. package/dist/types/transforms/scales.d.ts +1 -0
  57. package/dist/types/types.d.ts +59 -0
  58. package/dist/types/util/ast.d.ts +60 -0
  59. package/dist/types/util/function.d.ts +54 -0
  60. package/dist/types/util/string.d.ts +3 -0
  61. package/dist/types/util/type-check.d.ts +18 -0
  62. package/dist/types/visit/recurse.d.ts +28 -0
  63. package/dist/types/visit/rewrite.d.ts +10 -0
  64. package/dist/types/visit/visitors.d.ts +33 -0
  65. package/dist/types/visit/walk.d.ts +7 -0
  66. package/jsconfig.json +11 -0
  67. package/package.json +6 -4
  68. package/src/ast/aggregate.js +164 -0
  69. package/src/ast/between-op.js +75 -0
  70. package/src/ast/binary-op.js +40 -0
  71. package/src/ast/case.js +105 -0
  72. package/src/ast/cast.js +34 -0
  73. package/src/ast/column-param.js +29 -0
  74. package/src/ast/column-ref.js +72 -0
  75. package/src/ast/fragment.js +26 -0
  76. package/src/ast/from.js +40 -0
  77. package/src/ast/function.js +34 -0
  78. package/src/ast/in-op.js +33 -0
  79. package/src/ast/interval.js +33 -0
  80. package/src/ast/literal.js +55 -0
  81. package/src/ast/logical-op.js +67 -0
  82. package/src/ast/node.js +29 -0
  83. package/src/ast/order-by.js +48 -0
  84. package/src/ast/param.js +35 -0
  85. package/src/ast/query.js +578 -0
  86. package/src/ast/sample.js +53 -0
  87. package/src/ast/select.js +44 -0
  88. package/src/ast/table-ref.js +44 -0
  89. package/src/ast/unary-op.js +64 -0
  90. package/src/ast/verbatim.js +26 -0
  91. package/src/ast/window.js +290 -0
  92. package/src/ast/with.js +30 -0
  93. package/src/constants.js +44 -0
  94. package/src/functions/aggregate.js +335 -0
  95. package/src/functions/case.js +21 -0
  96. package/src/functions/cast.js +39 -0
  97. package/src/functions/column.js +20 -0
  98. package/src/functions/datetime.js +65 -0
  99. package/src/functions/literal.js +22 -0
  100. package/src/functions/numeric.js +139 -0
  101. package/src/functions/operators.js +298 -0
  102. package/src/functions/order-by.js +24 -0
  103. package/src/functions/spatial.js +56 -0
  104. package/src/functions/sql-template-tag.js +51 -0
  105. package/src/functions/string.js +82 -0
  106. package/src/functions/table-ref.js +14 -0
  107. package/src/functions/window.js +121 -0
  108. package/src/index-types.ts +2 -0
  109. package/src/index.js +57 -155
  110. package/src/load/create.js +10 -2
  111. package/src/load/load.js +4 -4
  112. package/src/load/sql-from.js +7 -6
  113. package/src/transforms/bin-1d.js +21 -0
  114. package/src/transforms/bin-2d.js +29 -0
  115. package/src/transforms/bin-linear-1d.js +26 -0
  116. package/src/transforms/bin-linear-2d.js +71 -0
  117. package/src/transforms/line-density.js +113 -0
  118. package/src/transforms/m4.js +38 -0
  119. package/src/{scales.js → transforms/scales.js} +31 -17
  120. package/src/types.ts +96 -0
  121. package/src/util/ast.js +96 -0
  122. package/src/util/function.js +78 -0
  123. package/src/util/string.js +16 -0
  124. package/src/util/type-check.js +29 -0
  125. package/src/visit/recurse.js +57 -0
  126. package/src/visit/rewrite.js +32 -0
  127. package/src/visit/visitors.js +108 -0
  128. package/src/visit/walk.js +30 -0
  129. package/tsconfig.json +12 -0
  130. package/src/Query.js +0 -593
  131. package/src/aggregates.js +0 -185
  132. package/src/cast.js +0 -19
  133. package/src/datetime.js +0 -31
  134. package/src/desc.js +0 -13
  135. package/src/expression.js +0 -170
  136. package/src/functions.js +0 -25
  137. package/src/literal.js +0 -6
  138. package/src/operators.js +0 -54
  139. package/src/ref.js +0 -109
  140. package/src/repeat.js +0 -3
  141. package/src/spatial.js +0 -10
  142. package/src/to-sql.js +0 -52
  143. package/src/windows.js +0 -239
@@ -0,0 +1,40 @@
1
+ import { FROM_CLAUSE } from '../constants.js';
2
+ import { quoteIdentifier } from '../util/string.js';
3
+ import { SQLNode } from './node.js';
4
+ import { isQuery } from './query.js';
5
+ import { isTableRef } from './table-ref.js';
6
+
7
+ export class FromClauseNode extends SQLNode {
8
+ /**
9
+ * Instantiate a from node.
10
+ * @param {SQLNode} expr The from expression.
11
+ * @param {string} alias The output name.
12
+ */
13
+ constructor(expr, alias) {
14
+ super(FROM_CLAUSE);
15
+ /**
16
+ * The from expression.
17
+ * @type {SQLNode}
18
+ * @readonly
19
+ */
20
+ this.expr = expr;
21
+ /**
22
+ * The output name.
23
+ * @type {string}
24
+ * @readonly
25
+ */
26
+ this.alias = alias;
27
+ }
28
+
29
+ /**
30
+ * Generate a SQL query string for this node.
31
+ * @returns {string}
32
+ */
33
+ toString() {
34
+ const { expr, alias } = this;
35
+ const ref = isQuery(expr) ? `(${expr})` : `${expr}`;
36
+ return alias && !(isTableRef(expr) && expr.table.join('.') === alias)
37
+ ? `${ref} AS ${quoteIdentifier(alias)}`
38
+ : `${ref}`;
39
+ }
40
+ }
@@ -0,0 +1,34 @@
1
+ import { FUNCTION } from '../constants.js';
2
+ import { ExprNode } from './node.js';
3
+
4
+ export class FunctionNode extends ExprNode {
5
+ /**
6
+ * Instantiate a function node.
7
+ * @param {string} name The function name.
8
+ * @param {ExprNode[]} [args=[]] The function arguments.
9
+ */
10
+ constructor(name, args = []) {
11
+ super(FUNCTION);
12
+ /**
13
+ * The function name.
14
+ * @type {string}
15
+ * @readonly
16
+ */
17
+ this.name = name;
18
+ /**
19
+ * The function arguments.
20
+ * @type {ExprNode[]}
21
+ * @readonly
22
+ */
23
+ this.args = args;
24
+ }
25
+
26
+ /**
27
+ * Generate a SQL query string for this node.
28
+ * @returns {string}
29
+ */
30
+ toString() {
31
+ const { name, args } = this;
32
+ return `${name}(${args.join(', ')})`;
33
+ }
34
+ }
@@ -0,0 +1,33 @@
1
+ import { IN_OPERATOR } from '../constants.js';
2
+ import { ExprNode } from './node.js';
3
+
4
+ export class InOpNode extends ExprNode {
5
+ /**
6
+ * Instantiate an in operator node.
7
+ * @param {ExprNode} expr The input expression.
8
+ * @param {ExprNode[]} values The value set.
9
+ */
10
+ constructor(expr, values) {
11
+ super(IN_OPERATOR);
12
+ /**
13
+ * The input expression.
14
+ * @type {ExprNode}
15
+ * @readonly
16
+ */
17
+ this.expr = expr;
18
+ /**
19
+ * The value set.
20
+ * @type {ExprNode[]}
21
+ * @readonly
22
+ */
23
+ this.values = values;
24
+ }
25
+
26
+ /**
27
+ * Generate a SQL query string for this node.
28
+ * @returns {string}
29
+ */
30
+ toString() {
31
+ return `(${this.expr} IN (${this.values.join(', ')}))`;
32
+ }
33
+ }
@@ -0,0 +1,33 @@
1
+ import { INTERVAL } from '../constants.js';
2
+ import { ExprNode } from './node.js';
3
+
4
+ export class IntervalNode extends ExprNode {
5
+ /**
6
+ * Instantiate an interval node.
7
+ * @param {string} name The interval name.
8
+ * @param {number} [steps=1] The interval steps.
9
+ */
10
+ constructor(name, steps = 1) {
11
+ super(INTERVAL);
12
+ /**
13
+ * The interval name.
14
+ * @type {string}
15
+ * @readonly
16
+ */
17
+ this.name = name;
18
+ /**
19
+ * The interval steps.
20
+ * @type {number}
21
+ * @readonly
22
+ */
23
+ this.steps = steps;
24
+ }
25
+
26
+ /**
27
+ * Generate a SQL query string for this node.
28
+ * @returns {string}
29
+ */
30
+ toString() {
31
+ return `INTERVAL ${this.steps} ${this.name}`;
32
+ }
33
+ }
@@ -0,0 +1,55 @@
1
+ import { LITERAL } from '../constants.js';
2
+ import { ExprNode } from './node.js';
3
+
4
+ export class LiteralNode extends ExprNode {
5
+ /**
6
+ * Instantiate an literal node.
7
+ * @param {*} value The literal value.
8
+ */
9
+ constructor(value) {
10
+ super(LITERAL);
11
+ /**
12
+ * The literal value.
13
+ * @type {any}
14
+ * @readonly
15
+ */
16
+ this.value = value;
17
+ }
18
+
19
+ /**
20
+ * Generate a SQL query string for this node.
21
+ * @returns {string}
22
+ */
23
+ toString() {
24
+ return literalToSQL(this.value);
25
+ }
26
+ }
27
+
28
+ export function literalToSQL(value) {
29
+ switch (typeof value) {
30
+ case 'number':
31
+ return Number.isFinite(value) ? `${value}` : 'NULL';
32
+ case 'string':
33
+ return `'${value.replace(`'`, `''`)}'`;
34
+ case 'boolean':
35
+ return value ? 'TRUE' : 'FALSE';
36
+ default:
37
+ if (value == null) {
38
+ return 'NULL';
39
+ } else if (value instanceof Date) {
40
+ const ts = +value;
41
+ if (Number.isNaN(ts)) return 'NULL';
42
+ const y = value.getUTCFullYear();
43
+ const m = value.getUTCMonth();
44
+ const d = value.getUTCDate();
45
+ return ts === Date.UTC(y, m, d)
46
+ ? `DATE '${y}-${m+1}-${d}'` // utc date
47
+ : `epoch_ms(${ts})`; // timestamp
48
+ } else if (value instanceof RegExp) {
49
+ return `'${value.source}'`;
50
+ } else {
51
+ // otherwise rely on string coercion
52
+ return `${value}`;
53
+ }
54
+ }
55
+ }
@@ -0,0 +1,67 @@
1
+ import { LOGICAL_OPERATOR } from '../constants.js';
2
+ import { ExprNode } from './node.js';
3
+
4
+ /**
5
+ * @template {ExprNode} T
6
+ */
7
+ export class LogicalOpNode extends ExprNode {
8
+ /**
9
+ * Instantiate a logical operator node.
10
+ * @param {string} op The logical operation.
11
+ * @param {T[]} clauses The input clause expressions.
12
+ */
13
+ constructor(op, clauses) {
14
+ super(LOGICAL_OPERATOR);
15
+ /**
16
+ * The logical operator.
17
+ * @type {string}
18
+ * @readonly
19
+ */
20
+ this.op = op;
21
+ /**
22
+ * The input clause expressions.
23
+ * @type {T[]}
24
+ * @readonly
25
+ */
26
+ this.clauses = clauses;
27
+ }
28
+
29
+ /**
30
+ * Generate a SQL query string for this node.
31
+ * @returns {string}
32
+ */
33
+ toString() {
34
+ const c = this.clauses;
35
+ return c.length === 0 ? ''
36
+ : c.length === 1 ? `${c[0]}`
37
+ : `(${c.join(` ${this.op} `)})`;
38
+ }
39
+ }
40
+
41
+ /**
42
+ * @template {ExprNode} T
43
+ * @extends {LogicalOpNode<T>}
44
+ */
45
+ export class AndNode extends LogicalOpNode {
46
+ /**
47
+ * Instantiate a logical AND operator node.
48
+ * @param {T[]} clauses The input clause expressions.
49
+ */
50
+ constructor(clauses) {
51
+ super('AND', clauses);
52
+ }
53
+ }
54
+
55
+ /**
56
+ * @template {ExprNode} T
57
+ * @extends {LogicalOpNode<T>}
58
+ */
59
+ export class OrNode extends LogicalOpNode {
60
+ /**
61
+ * Instantiate a logical OR operator node.
62
+ * @param {T[]} clauses The input clause expressions.
63
+ */
64
+ constructor(clauses) {
65
+ super('OR', clauses);
66
+ }
67
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Check if a value is a SQL AST node.
3
+ * @param {*} value The value to check.
4
+ * @returns {value is SQLNode}
5
+ */
6
+ export function isNode(value) {
7
+ return value instanceof SQLNode;
8
+ }
9
+
10
+ export class SQLNode {
11
+ /**
12
+ * Instantiate a SQL AST node.
13
+ * @param {string} type The SQL AST node type.
14
+ */
15
+ constructor(type) {
16
+ /**
17
+ * The SQL AST node type.
18
+ * @type {string}
19
+ * @readonly
20
+ */
21
+ this.type = type;
22
+ }
23
+ }
24
+
25
+ /**
26
+ * AST node corresponding to an individual expression.
27
+ */
28
+ export class ExprNode extends SQLNode {
29
+ }
@@ -0,0 +1,48 @@
1
+ import { ORDER_BY } from '../constants.js';
2
+ import { ExprNode } from './node.js';
3
+
4
+ export class OrderByNode extends ExprNode {
5
+ /**
6
+ * Instantiate an order by entry node.
7
+ * @param {ExprNode} expr The expression to order by.
8
+ * @param {boolean | undefined} [desc] Flag indicating descending order.
9
+ * @param {boolean | undefined} [nullsFirst] Flag indicating if null
10
+ * values should be sorted first.
11
+ */
12
+ constructor(expr, desc, nullsFirst) {
13
+ super(ORDER_BY);
14
+ /**
15
+ * The expression to order by.
16
+ * @type {ExprNode}
17
+ * @readonly
18
+ */
19
+ this.expr = expr;
20
+ /**
21
+ * Flag indicating descending order.
22
+ * @type {boolean | undefined}
23
+ * @readonly
24
+ */
25
+ this.desc = desc;
26
+ /**
27
+ * Flag indicating if null values should be sorted first.
28
+ * @type {boolean | undefined}
29
+ * @readonly
30
+ */
31
+ this.nullsFirst = nullsFirst;
32
+ }
33
+
34
+ /**
35
+ * Generate a SQL query string for this node.
36
+ * @returns {string}
37
+ */
38
+ toString() {
39
+ const { expr, desc, nullsFirst } = this;
40
+ const dir = desc ? ' DESC'
41
+ : desc === false ? ' ASC'
42
+ : '';
43
+ const nf = nullsFirst ? ' NULLS FIRST'
44
+ : nullsFirst === false ? ' NULLS LAST'
45
+ : '';
46
+ return `${expr}${dir}${nf}`;
47
+ }
48
+ }
@@ -0,0 +1,35 @@
1
+ import { PARAM } from '../constants.js';
2
+ import { literalToSQL } from './literal.js';
3
+ import { ExprNode } from './node.js';
4
+
5
+ export class ParamNode extends ExprNode {
6
+ /**
7
+ * Instantiate a param node with a dynamic parameter.
8
+ * @param {import('../types.js').ParamLike} param The dynamic parameter.
9
+ */
10
+ constructor(param) {
11
+ super(PARAM);
12
+ /**
13
+ * The dynamic parameter.
14
+ * @type {import('../types.js').ParamLike}
15
+ * @readonly
16
+ */
17
+ this.param = param;
18
+ }
19
+
20
+ /**
21
+ * Returns the current parameter value.
22
+ * @returns {*}
23
+ */
24
+ get value() {
25
+ return this.param.value;
26
+ }
27
+
28
+ /**
29
+ * Generate a SQL query string for this node.
30
+ * @returns {string}
31
+ */
32
+ toString() {
33
+ return literalToSQL(this.value);
34
+ }
35
+ }