@uwdata/mosaic-core 0.17.0 → 0.18.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.
Files changed (178) hide show
  1. package/LICENSE +47 -0
  2. package/README.md +0 -1
  3. package/dist/src/Coordinator.d.ts +147 -0
  4. package/dist/src/Coordinator.d.ts.map +1 -0
  5. package/dist/src/Coordinator.js +269 -0
  6. package/dist/src/Coordinator.js.map +1 -0
  7. package/dist/src/MosaicClient.d.ts +138 -0
  8. package/dist/src/MosaicClient.d.ts.map +1 -0
  9. package/dist/src/MosaicClient.js +213 -0
  10. package/dist/src/MosaicClient.js.map +1 -0
  11. package/dist/src/Param.d.ts +56 -0
  12. package/dist/src/Param.d.ts.map +1 -0
  13. package/dist/src/Param.js +89 -0
  14. package/dist/src/Param.js.map +1 -0
  15. package/dist/src/QueryConsolidator.d.ts +11 -0
  16. package/dist/src/QueryConsolidator.d.ts.map +1 -0
  17. package/dist/src/QueryConsolidator.js +249 -0
  18. package/dist/src/QueryConsolidator.js.map +1 -0
  19. package/dist/src/QueryManager.d.ts +77 -0
  20. package/dist/src/QueryManager.d.ts.map +1 -0
  21. package/dist/src/QueryManager.js +174 -0
  22. package/dist/src/QueryManager.js.map +1 -0
  23. package/dist/src/Selection.d.ts +222 -0
  24. package/dist/src/Selection.d.ts.map +1 -0
  25. package/dist/src/Selection.js +319 -0
  26. package/dist/src/Selection.js.map +1 -0
  27. package/dist/src/SelectionClause.d.ts +192 -0
  28. package/dist/src/SelectionClause.d.ts.map +1 -0
  29. package/dist/src/SelectionClause.js +126 -0
  30. package/dist/src/SelectionClause.js.map +1 -0
  31. package/dist/src/connectors/Connector.d.ts +26 -0
  32. package/dist/src/connectors/Connector.d.ts.map +1 -0
  33. package/dist/src/connectors/Connector.js +2 -0
  34. package/dist/src/connectors/Connector.js.map +1 -0
  35. package/dist/src/connectors/rest.d.ts +24 -0
  36. package/dist/src/connectors/rest.d.ts.map +1 -0
  37. package/dist/src/connectors/rest.js +37 -0
  38. package/dist/src/connectors/rest.js.map +1 -0
  39. package/dist/src/connectors/socket.d.ts +40 -0
  40. package/dist/src/connectors/socket.d.ts.map +1 -0
  41. package/dist/src/connectors/socket.js +115 -0
  42. package/dist/src/connectors/socket.js.map +1 -0
  43. package/dist/src/connectors/wasm.d.ts +53 -0
  44. package/dist/src/connectors/wasm.d.ts.map +1 -0
  45. package/dist/src/connectors/wasm.js +113 -0
  46. package/dist/src/connectors/wasm.js.map +1 -0
  47. package/dist/src/index.d.ts +28 -0
  48. package/dist/src/index.d.ts.map +1 -0
  49. package/dist/src/index.js +25 -0
  50. package/dist/src/index.js.map +1 -0
  51. package/dist/src/make-client.d.ts +35 -0
  52. package/dist/src/make-client.d.ts.map +1 -0
  53. package/dist/src/make-client.js +52 -0
  54. package/dist/src/make-client.js.map +1 -0
  55. package/dist/src/preagg/PreAggregator.d.ts +150 -0
  56. package/dist/src/preagg/PreAggregator.d.ts.map +1 -0
  57. package/dist/src/preagg/PreAggregator.js +382 -0
  58. package/dist/src/preagg/PreAggregator.js.map +1 -0
  59. package/dist/src/preagg/preagg-columns.d.ts +16 -0
  60. package/dist/src/preagg/preagg-columns.d.ts.map +1 -0
  61. package/dist/src/preagg/preagg-columns.js +95 -0
  62. package/dist/src/preagg/preagg-columns.js.map +1 -0
  63. package/dist/src/preagg/sufficient-statistics.d.ts +14 -0
  64. package/dist/src/preagg/sufficient-statistics.d.ts.map +1 -0
  65. package/dist/src/preagg/sufficient-statistics.js +446 -0
  66. package/dist/src/preagg/sufficient-statistics.js.map +1 -0
  67. package/dist/src/types.d.ts +77 -0
  68. package/dist/src/types.d.ts.map +1 -0
  69. package/dist/src/types.js +2 -0
  70. package/dist/src/types.js.map +1 -0
  71. package/dist/src/util/AsyncDispatch.d.ts +121 -0
  72. package/dist/src/util/AsyncDispatch.d.ts.map +1 -0
  73. package/dist/src/util/AsyncDispatch.js +188 -0
  74. package/dist/src/util/AsyncDispatch.js.map +1 -0
  75. package/dist/src/util/cache.d.ts +19 -0
  76. package/dist/src/util/cache.d.ts.map +1 -0
  77. package/dist/src/util/cache.js +66 -0
  78. package/dist/src/util/cache.js.map +1 -0
  79. package/dist/src/util/decode-ipc.d.ts +12 -0
  80. package/dist/src/util/decode-ipc.d.ts.map +1 -0
  81. package/{src → dist/src}/util/decode-ipc.js +5 -6
  82. package/dist/src/util/decode-ipc.js.map +1 -0
  83. package/dist/src/util/distinct.d.ts +3 -0
  84. package/dist/src/util/distinct.d.ts.map +1 -0
  85. package/dist/src/util/distinct.js +16 -0
  86. package/dist/src/util/distinct.js.map +1 -0
  87. package/dist/src/util/field-info.d.ts +26 -0
  88. package/dist/src/util/field-info.d.ts.map +1 -0
  89. package/dist/src/util/field-info.js +91 -0
  90. package/dist/src/util/field-info.js.map +1 -0
  91. package/dist/src/util/hash.d.ts +2 -0
  92. package/dist/src/util/hash.d.ts.map +1 -0
  93. package/dist/src/util/hash.js +26 -0
  94. package/dist/src/util/hash.js.map +1 -0
  95. package/dist/src/util/is-activatable.d.ts +8 -0
  96. package/dist/src/util/is-activatable.d.ts.map +1 -0
  97. package/dist/src/util/is-activatable.js +10 -0
  98. package/dist/src/util/is-activatable.js.map +1 -0
  99. package/dist/src/util/is-arrow-table.d.ts +9 -0
  100. package/dist/src/util/is-arrow-table.d.ts.map +1 -0
  101. package/dist/src/util/is-arrow-table.js +11 -0
  102. package/dist/src/util/is-arrow-table.js.map +1 -0
  103. package/dist/src/util/js-type.d.ts +9 -0
  104. package/dist/src/util/js-type.d.ts.map +1 -0
  105. package/dist/src/util/js-type.js +59 -0
  106. package/dist/src/util/js-type.js.map +1 -0
  107. package/dist/src/util/priority-queue.d.ts +35 -0
  108. package/dist/src/util/priority-queue.d.ts.map +1 -0
  109. package/dist/src/util/priority-queue.js +81 -0
  110. package/dist/src/util/priority-queue.js.map +1 -0
  111. package/dist/src/util/query-result.d.ts +47 -0
  112. package/dist/src/util/query-result.d.ts.map +1 -0
  113. package/dist/src/util/query-result.js +83 -0
  114. package/dist/src/util/query-result.js.map +1 -0
  115. package/dist/src/util/synchronizer.d.ts +36 -0
  116. package/dist/src/util/synchronizer.d.ts.map +1 -0
  117. package/dist/src/util/synchronizer.js +52 -0
  118. package/dist/src/util/synchronizer.js.map +1 -0
  119. package/dist/src/util/throttle.d.ts +12 -0
  120. package/dist/src/util/throttle.d.ts.map +1 -0
  121. package/dist/src/util/throttle.js +51 -0
  122. package/dist/src/util/throttle.js.map +1 -0
  123. package/dist/src/util/to-data-columns.d.ts +22 -0
  124. package/dist/src/util/to-data-columns.d.ts.map +1 -0
  125. package/dist/src/util/to-data-columns.js +51 -0
  126. package/dist/src/util/to-data-columns.js.map +1 -0
  127. package/dist/src/util/void-logger.d.ts +13 -0
  128. package/dist/src/util/void-logger.d.ts.map +1 -0
  129. package/dist/src/util/void-logger.js +13 -0
  130. package/dist/src/util/void-logger.js.map +1 -0
  131. package/package.json +16 -10
  132. package/src/Coordinator.ts +367 -0
  133. package/src/{MosaicClient.js → MosaicClient.ts} +49 -43
  134. package/src/{Param.js → Param.ts} +29 -28
  135. package/src/{QueryConsolidator.js → QueryConsolidator.ts} +81 -58
  136. package/src/{QueryManager.js → QueryManager.ts} +61 -54
  137. package/src/Selection.ts +388 -0
  138. package/src/SelectionClause.ts +275 -0
  139. package/src/connectors/Connector.ts +6 -6
  140. package/src/connectors/rest.ts +56 -0
  141. package/src/connectors/{socket.js → socket.ts} +53 -42
  142. package/src/connectors/{wasm.js → wasm.ts} +46 -62
  143. package/src/{index.js → index.ts} +13 -1
  144. package/src/make-client.ts +93 -0
  145. package/src/preagg/{PreAggregator.js → PreAggregator.ts} +164 -145
  146. package/src/preagg/{preagg-columns.js → preagg-columns.ts} +27 -24
  147. package/src/preagg/{sufficient-statistics.js → sufficient-statistics.ts} +160 -110
  148. package/src/types.ts +24 -9
  149. package/src/util/{AsyncDispatch.js → AsyncDispatch.ts} +62 -43
  150. package/src/util/{cache.js → cache.ts} +25 -15
  151. package/src/util/decode-ipc.ts +15 -0
  152. package/src/util/{distinct.js → distinct.ts} +3 -3
  153. package/src/util/{field-info.js → field-info.ts} +31 -32
  154. package/src/util/{hash.js → hash.ts} +4 -4
  155. package/src/util/is-activatable.ts +11 -0
  156. package/src/util/is-arrow-table.ts +12 -0
  157. package/src/util/{js-type.js → js-type.ts} +7 -5
  158. package/src/util/{priority-queue.js → priority-queue.ts} +32 -20
  159. package/src/util/{query-result.js → query-result.ts} +24 -17
  160. package/src/util/synchronizer.ts +56 -0
  161. package/src/util/throttle.ts +59 -0
  162. package/src/util/to-data-columns.ts +65 -0
  163. package/src/util/void-logger.ts +23 -0
  164. package/src/Coordinator.js +0 -313
  165. package/src/Selection.js +0 -380
  166. package/src/SelectionClause.js +0 -159
  167. package/src/connectors/rest.js +0 -38
  168. package/src/index-types.ts +0 -5
  169. package/src/make-client.js +0 -101
  170. package/src/util/is-activatable.js +0 -8
  171. package/src/util/is-arrow-table.js +0 -10
  172. package/src/util/selection-types.ts +0 -137
  173. package/src/util/synchronizer.js +0 -47
  174. package/src/util/throttle.js +0 -54
  175. package/src/util/to-data-columns.js +0 -60
  176. package/src/util/void-logger.js +0 -13
  177. package/tsconfig.json +0 -9
  178. package/vitest.config.ts +0 -3
@@ -0,0 +1,150 @@
1
+ import { ExprNode, SelectQuery, MaybeArray } from '@uwdata/mosaic-sql';
2
+ import type { Coordinator } from '../Coordinator.js';
3
+ import type { MosaicClient } from '../MosaicClient.js';
4
+ import type { Selection } from '../Selection.js';
5
+ import type { ClauseSource, SelectionClause } from '../SelectionClause.js';
6
+ declare const Skip: {
7
+ skip: boolean;
8
+ result: null;
9
+ };
10
+ export interface PreAggregateOptions {
11
+ /** Database schema (namespace) in which to write pre-aggregated materialized views (default 'mosaic'). */
12
+ schema?: string;
13
+ /** Flag to enable or disable the pre-aggregation. This flag can be updated later via the `enabled` property. */
14
+ enabled?: boolean;
15
+ }
16
+ type ActivePredicate = (p?: ExprNode) => MaybeArray<ExprNode> | undefined;
17
+ interface ActiveColumnsResult {
18
+ source: ClauseSource | null;
19
+ columns?: Record<string, ExprNode>;
20
+ predicate?: ActivePredicate;
21
+ }
22
+ interface PreAggregateInfoOptions {
23
+ table: string;
24
+ create: string;
25
+ active: ActiveColumnsResult;
26
+ select: SelectQuery;
27
+ }
28
+ /**
29
+ * Build and query optimized pre-aggregated materaialized views, for fast
30
+ * computation of groupby aggregate queries over compatible client queries
31
+ * and selections. The materialized views contains pre-aggregated data for a
32
+ * Mosaic client, subdivided by possible query values from an active selection
33
+ * clause. These materialized views are database tables that can be queried
34
+ * for rapid updates.
35
+ *
36
+ * Compatible client queries must consist of only groupby dimensions and
37
+ * supported aggregate functions. Compatible selections must contain an active
38
+ * clause that exposes metadata for an interval or point value predicate.
39
+ *
40
+ * Materialized views are written to a dedicated schema (namespace) that
41
+ * can be set using the *schema* constructor option. This schema acts as a
42
+ * persistent cache, and materialized view tables may be used across sessions.
43
+ * The `dropSchema` method issues a query to remove *all* tables within this
44
+ * schema. This may be needed if the original tables have updated data, but
45
+ * should be used with care.
46
+ */
47
+ export declare class PreAggregator {
48
+ entries: Map<MosaicClient, PreAggregateInfo | typeof Skip | null>;
49
+ private active;
50
+ private mc;
51
+ private _schema;
52
+ private _enabled;
53
+ /**
54
+ * Create a new manager of materialized views of pre-aggregated data.
55
+ * @param coordinator A Mosaic coordinator.
56
+ * @param options Pre-aggregation options.
57
+ */
58
+ constructor(coordinator: Coordinator, { schema, enabled }?: PreAggregateOptions);
59
+ /**
60
+ * Set the enabled state of this manager. If false, any local state is
61
+ * cleared and subsequent request calls will return null until re-enabled.
62
+ * This method has no effect on any pre-aggregated tables already in the
63
+ * database.
64
+ * @param state The enabled state to set.
65
+ */
66
+ set enabled(state: boolean);
67
+ /**
68
+ * Get the enabled state of this manager.
69
+ * @returns The current enabled state.
70
+ */
71
+ get enabled(): boolean;
72
+ /**
73
+ * Set the database schema used for pre-aggregated materialized view tables.
74
+ * Upon changes, any local state is cleared. This method does _not_ drop any
75
+ * existing materialized views, use `dropSchema` before changing the schema
76
+ * to also remove existing materalized views in the database.
77
+ * @param schema The schema name to set.
78
+ */
79
+ set schema(schema: string);
80
+ /**
81
+ * Get the database schema used for pre-aggregated materialized view tables.
82
+ * @returns The current schema name.
83
+ */
84
+ get schema(): string;
85
+ /**
86
+ * Issues a query through the coordinator to drop the current schema for
87
+ * pre-aggregated materialized views. *All* materialized view tables in the
88
+ * schema will be removed and local state is cleared. Call this method if
89
+ * the underlying base tables have been updated, causing materialized view
90
+ * to become stale and inaccurate. Use this method with care! Once dropped,
91
+ * the schema will be repopulated by future pre-aggregation requests.
92
+ * @returns A query result promise.
93
+ */
94
+ dropSchema(): Promise<unknown>;
95
+ /**
96
+ * Clear the cache of pre-aggregation entries for the current active
97
+ * selection clause. This method does _not_ drop any existing materialized
98
+ * views. Use `dropSchema` to remove existing materialized view tables from
99
+ * the database.
100
+ */
101
+ clear(): void;
102
+ /**
103
+ * Return pre-aggregation information for the active state of a
104
+ * client-selection pair, or null if the client has unstable filters.
105
+ * This method has multiple possible side effects, including materialized
106
+ * view creation and updating internal caches.
107
+ * @param client A Mosaic client.
108
+ * @param selection A Mosaic selection to filter the client by.
109
+ * @param activeClause A representative active selection
110
+ * clause for which to generate materialized views of pre-aggregates.
111
+ * @returns Information and query generator
112
+ * for pre-aggregated tables, or null if the client has unstable filters.
113
+ */
114
+ request(client: MosaicClient, selection: Selection, activeClause: SelectionClause | null): PreAggregateInfo | typeof Skip | null;
115
+ }
116
+ /**
117
+ * Metadata and query generator for materialized views of pre-aggregated data.
118
+ * This object provides the information needed to generate and query the
119
+ * materialized views for a client-selection pair relative to a specific
120
+ * active clause and selection state.
121
+ */
122
+ export declare class PreAggregateInfo {
123
+ /** The name of the materialized view. */
124
+ table: string;
125
+ /** The SQL query used to generate the materialized view. */
126
+ create: string;
127
+ /** A result promise returned for the materialized view creation query. */
128
+ result: Promise<unknown> | null;
129
+ /** Definitions and predicate function for the active columns,
130
+ * which are dynamically filtered by the active clause. */
131
+ active: ActiveColumnsResult;
132
+ /** Select query (sans where clause) for materialized views. */
133
+ select: SelectQuery;
134
+ /** Boolean flag indicating a client that should be skipped.
135
+ * This value is always false for a created materialized view. */
136
+ skip: boolean;
137
+ /**
138
+ * Create a new pre-aggregation information instance.
139
+ * @param options Options object.
140
+ */
141
+ constructor({ table, create, active, select }: PreAggregateInfoOptions);
142
+ /**
143
+ * Generate a materialized view query for the given predicate.
144
+ * @param predicate The current active clause predicate.
145
+ * @returns A materialized view query.
146
+ */
147
+ query(predicate: ExprNode): SelectQuery;
148
+ }
149
+ export {};
150
+ //# sourceMappingURL=PreAggregator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PreAggregator.d.ts","sourceRoot":"","sources":["../../../src/preagg/PreAggregator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAgB,WAAW,EAAoB,UAAU,EAAwC,MAAM,oBAAoB,CAAC;AAC7I,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAa,YAAY,EAAoB,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAKxG,QAAA,MAAM,IAAI;;;CAA+B,CAAC;AAE1C,MAAM,WAAW,mBAAmB;IAClC,0GAA0G;IAC1G,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gHAAgH;IAChH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,KAAK,eAAe,GAAG,CAAC,CAAC,CAAC,EAAE,QAAQ,KAAK,UAAU,CAAC,QAAQ,CAAC,GAAG,SAAS,CAAC;AAE1E,UAAU,mBAAmB;IAC3B,MAAM,EAAE,YAAY,GAAG,IAAI,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACnC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C,GAAG,IAAI,CAAC;IAChC;8DAC0D;IAC1D,MAAM,EAAE,mBAAmB,CAAC;IAC5B,+DAA+D;IAC/D,MAAM,EAAE,WAAW,CAAC;IACpB;qEACiE;IACjE,IAAI,EAAE,OAAO,CAAC;IAEd;;;OAGG;gBACS,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,uBAAuB;IAStE;;;;OAIG;IACH,KAAK,CAAC,SAAS,EAAE,QAAQ,GAAG,WAAW;CAGxC"}
@@ -0,0 +1,382 @@
1
+ import { Query as QueryBuilder, and, asNode, ceil, collectColumns, createTable, float64, floor, isBetween, int32, mul, round, scaleTransform, sub, isSelectQuery, isAggregateExpression, ColumnNameRefNode } from '@uwdata/mosaic-sql';
2
+ import { preaggColumns } from './preagg-columns.js';
3
+ import { fnv_hash } from '../util/hash.js';
4
+ const Skip = { skip: true, result: null };
5
+ /**
6
+ * Build and query optimized pre-aggregated materaialized views, for fast
7
+ * computation of groupby aggregate queries over compatible client queries
8
+ * and selections. The materialized views contains pre-aggregated data for a
9
+ * Mosaic client, subdivided by possible query values from an active selection
10
+ * clause. These materialized views are database tables that can be queried
11
+ * for rapid updates.
12
+ *
13
+ * Compatible client queries must consist of only groupby dimensions and
14
+ * supported aggregate functions. Compatible selections must contain an active
15
+ * clause that exposes metadata for an interval or point value predicate.
16
+ *
17
+ * Materialized views are written to a dedicated schema (namespace) that
18
+ * can be set using the *schema* constructor option. This schema acts as a
19
+ * persistent cache, and materialized view tables may be used across sessions.
20
+ * The `dropSchema` method issues a query to remove *all* tables within this
21
+ * schema. This may be needed if the original tables have updated data, but
22
+ * should be used with care.
23
+ */
24
+ export class PreAggregator {
25
+ entries;
26
+ active;
27
+ mc;
28
+ _schema;
29
+ _enabled;
30
+ /**
31
+ * Create a new manager of materialized views of pre-aggregated data.
32
+ * @param coordinator A Mosaic coordinator.
33
+ * @param options Pre-aggregation options.
34
+ */
35
+ constructor(coordinator, { schema = 'mosaic', enabled = true } = {}) {
36
+ this.entries = new Map();
37
+ this.active = null;
38
+ this.mc = coordinator;
39
+ this._schema = schema;
40
+ this._enabled = enabled;
41
+ }
42
+ /**
43
+ * Set the enabled state of this manager. If false, any local state is
44
+ * cleared and subsequent request calls will return null until re-enabled.
45
+ * This method has no effect on any pre-aggregated tables already in the
46
+ * database.
47
+ * @param state The enabled state to set.
48
+ */
49
+ set enabled(state) {
50
+ if (this._enabled !== state) {
51
+ if (!state)
52
+ this.clear();
53
+ this._enabled = state;
54
+ }
55
+ }
56
+ /**
57
+ * Get the enabled state of this manager.
58
+ * @returns The current enabled state.
59
+ */
60
+ get enabled() {
61
+ return this._enabled;
62
+ }
63
+ /**
64
+ * Set the database schema used for pre-aggregated materialized view tables.
65
+ * Upon changes, any local state is cleared. This method does _not_ drop any
66
+ * existing materialized views, use `dropSchema` before changing the schema
67
+ * to also remove existing materalized views in the database.
68
+ * @param schema The schema name to set.
69
+ */
70
+ set schema(schema) {
71
+ if (this._schema !== schema) {
72
+ this.clear();
73
+ this._schema = schema;
74
+ }
75
+ }
76
+ /**
77
+ * Get the database schema used for pre-aggregated materialized view tables.
78
+ * @returns The current schema name.
79
+ */
80
+ get schema() {
81
+ return this._schema;
82
+ }
83
+ /**
84
+ * Issues a query through the coordinator to drop the current schema for
85
+ * pre-aggregated materialized views. *All* materialized view tables in the
86
+ * schema will be removed and local state is cleared. Call this method if
87
+ * the underlying base tables have been updated, causing materialized view
88
+ * to become stale and inaccurate. Use this method with care! Once dropped,
89
+ * the schema will be repopulated by future pre-aggregation requests.
90
+ * @returns A query result promise.
91
+ */
92
+ dropSchema() {
93
+ this.clear();
94
+ return this.mc.exec(`DROP SCHEMA IF EXISTS "${this.schema}" CASCADE`);
95
+ }
96
+ /**
97
+ * Clear the cache of pre-aggregation entries for the current active
98
+ * selection clause. This method does _not_ drop any existing materialized
99
+ * views. Use `dropSchema` to remove existing materialized view tables from
100
+ * the database.
101
+ */
102
+ clear() {
103
+ this.entries.clear();
104
+ this.active = null;
105
+ }
106
+ /**
107
+ * Return pre-aggregation information for the active state of a
108
+ * client-selection pair, or null if the client has unstable filters.
109
+ * This method has multiple possible side effects, including materialized
110
+ * view creation and updating internal caches.
111
+ * @param client A Mosaic client.
112
+ * @param selection A Mosaic selection to filter the client by.
113
+ * @param activeClause A representative active selection
114
+ * clause for which to generate materialized views of pre-aggregates.
115
+ * @returns Information and query generator
116
+ * for pre-aggregated tables, or null if the client has unstable filters.
117
+ */
118
+ request(client, selection, activeClause) {
119
+ // if not enabled, do nothing
120
+ if (!this.enabled || activeClause == null)
121
+ return null;
122
+ const { entries, mc, schema } = this;
123
+ const { source } = activeClause;
124
+ // if there is no clause source to track, do nothing
125
+ if (!source)
126
+ return null;
127
+ // if we have cached active columns, check for updates or exit
128
+ if (this.active) {
129
+ // if the active clause source has changed, clear the state
130
+ // this cancels outstanding requests and clears the local cache
131
+ // a clear also sets this.active to null
132
+ if (this.active.source !== source)
133
+ this.clear();
134
+ // if we've seen this source and it has unstable filters, do nothing
135
+ if (this.active?.source === null)
136
+ return null;
137
+ }
138
+ // the current active columns cache value
139
+ let { active } = this;
140
+ // if cached active columns are unset, analyze the active clause
141
+ if (!active) {
142
+ // if active clause predicate is null, we can't analyze it
143
+ // return null to backoff to standard client query
144
+ // non-null clauses may come later, so don't set active state
145
+ if (activeClause.predicate == null)
146
+ return null;
147
+ // generate active dimension columns to select over
148
+ // will return an object with null source if it has unstable filters
149
+ this.active = active = activeColumns(activeClause);
150
+ // if the active clause has unstable filters, exit now
151
+ if (active.source === null)
152
+ return null;
153
+ }
154
+ // if we have cached pre-aggregate info, return that
155
+ if (entries.has(client)) {
156
+ return entries.get(client);
157
+ }
158
+ // get non-active materialized view columns
159
+ const preaggCols = preaggColumns(client);
160
+ let info;
161
+ if (!preaggCols) {
162
+ // if client is not indexable, record null info
163
+ info = null;
164
+ }
165
+ else if (selection.skip(client, activeClause)) {
166
+ // skip client if untouched by cross-filtering
167
+ info = Skip;
168
+ }
169
+ else {
170
+ // generate materialized view table
171
+ const filter = selection.remove(source).predicate(client);
172
+ info = preaggregateInfo(client.query(filter), active, preaggCols, schema);
173
+ info.result = mc.exec([
174
+ `CREATE SCHEMA IF NOT EXISTS ${schema}`,
175
+ createTable(info.table, info.create, { temp: false })
176
+ ]);
177
+ info.result.catch((e) => mc.logger().error(e));
178
+ }
179
+ entries.set(client, info);
180
+ return info;
181
+ }
182
+ }
183
+ /**
184
+ * Determines the active dimension columns to select over. Returns an object
185
+ * with the clause source, column definitions, and a predicate generator
186
+ * function for the active dimensions of a pre-aggregated materialized view.
187
+ * If the active clause is not indexable or is missing metadata, this method
188
+ * returns an object with a null source property.
189
+ * @param clause The active selection clause to analyze.
190
+ */
191
+ function activeColumns(clause) {
192
+ const { source, meta } = clause;
193
+ const clausePred = clause.predicate;
194
+ const clauseCols = collectColumns(clausePred).map(c => c.column);
195
+ let predicate;
196
+ let columns;
197
+ if (!meta || !clauseCols) {
198
+ return { source: null, columns, predicate };
199
+ }
200
+ switch (meta.type) {
201
+ case 'point':
202
+ predicate = x => x;
203
+ columns = Object.fromEntries(clauseCols.map(col => [`${col}`, asNode(col)]));
204
+ break;
205
+ case 'interval': {
206
+ const { scales, bin, pixelSize = 1 } = meta;
207
+ if (!scales)
208
+ break;
209
+ // determine pixel-level binning
210
+ const bins = scales.map((s) => binInterval(s, pixelSize, bin));
211
+ if (bins.some(b => !b)) {
212
+ // bail if a scale type is unsupported
213
+ }
214
+ else if (bins.length === 1) {
215
+ // selection clause predicate has type BetweenOpNode
216
+ // single interval selection
217
+ predicate = (p) => p
218
+ ? isBetween('active0', p.extent?.map(bins[0]))
219
+ : [];
220
+ columns = { active0: bins[0](clausePred.expr) };
221
+ }
222
+ else {
223
+ // selection clause predicate has type AndNode<BetweenOpNode>
224
+ // multiple interval selection
225
+ predicate = (p) => p
226
+ ? and(p.clauses.map((c, i) => isBetween(`active${i}`, c.extent?.map(bins[i]))))
227
+ : [];
228
+ columns = Object.fromEntries(clausePred.clauses.map((p, i) => [`active${i}`, bins[i](p.expr)]));
229
+ }
230
+ }
231
+ }
232
+ return { source: columns ? source : null, columns, predicate };
233
+ }
234
+ const BIN = { ceil, round };
235
+ /**
236
+ * Returns a bin function generator to discretize a selection interval domain.
237
+ * @param scale A scale that maps domain values to the output range
238
+ * (typically pixels).
239
+ * @param pixelSize The interactive pixel size. This value indicates
240
+ * the bin step size and may be greater than an actual screen pixel.
241
+ * @param bin The binning method to apply, one of `floor`, `ceil', or `round`.
242
+ * @returns A bin function generator.
243
+ */
244
+ function binInterval(scale, pixelSize, bin) {
245
+ const { type, domain, range, apply, sqlApply } = scaleTransform(scale);
246
+ if (!apply)
247
+ return; // unsupported scale type
248
+ const binFn = BIN[`${bin}`.toLowerCase()] || floor;
249
+ const dom = domain.map(x => Number(x));
250
+ const lo = apply(Math.min(...dom));
251
+ const hi = apply(Math.max(...dom));
252
+ const s = (type === 'identity'
253
+ ? 1
254
+ : Math.abs(range[1] - range[0]) / (hi - lo)) / pixelSize;
255
+ const scalar = s === 1
256
+ ? (x) => x
257
+ : (x) => mul(float64(s), x);
258
+ const diff = lo === 0
259
+ ? (x) => x
260
+ : (x) => sub(x, float64(lo));
261
+ return value => int32(binFn(scalar(diff(sqlApply(value)))));
262
+ }
263
+ /**
264
+ * Generate pre-aggregate query information.
265
+ * @param clientQuery The original client query.
266
+ * @param active Active (selected) columns.
267
+ * @param preaggCols Pre-aggregation columns.
268
+ * @param schema Database schema name.
269
+ * @returns Pre-aggregation information.
270
+ */
271
+ function preaggregateInfo(clientQuery, active, preaggCols, schema) {
272
+ const { group, output, preagg } = preaggCols;
273
+ const { columns } = active;
274
+ // build materialized view construction query
275
+ const query = clientQuery
276
+ .setSelect({ ...preagg, ...columns })
277
+ .groupby(Object.keys(columns));
278
+ // ensure active clause columns are selected by subqueries
279
+ const [subq] = query.subqueries;
280
+ if (subq) {
281
+ const cols = Object.values(columns)
282
+ .flatMap(c => collectColumns(c).map(c => c.column));
283
+ subqueryPushdown(subq, cols);
284
+ }
285
+ // push any having or orderby criteria to output queries
286
+ const having = query._having;
287
+ const order = query._orderby;
288
+ query._having = [];
289
+ query._orderby = [];
290
+ // generate creation query string and hash id
291
+ const create = query.toString();
292
+ const id = (fnv_hash(create) >>> 0).toString(16);
293
+ const table = `${schema}.preagg_${id}`;
294
+ // generate preaggregate select query
295
+ const select = QueryBuilder
296
+ .select(group, output)
297
+ .from(table)
298
+ .groupby(group)
299
+ .having(having)
300
+ .orderby(order);
301
+ return new PreAggregateInfo({ table, create, active, select });
302
+ }
303
+ /**
304
+ * Push column selections down to subqueries.
305
+ * @param query The (sub)query to push down to.
306
+ * @param cols The column names to push down.
307
+ */
308
+ function subqueryPushdown(query, cols) {
309
+ const memo = new Set();
310
+ const pushdown = (q) => {
311
+ // it is possible to have duplicate subqueries
312
+ // so we memoize and exit early if already seen
313
+ if (memo.has(q))
314
+ return;
315
+ memo.add(q);
316
+ if (isSelectQuery(q) && q._from.length) {
317
+ // select the pushed down columns
318
+ // note that the select method will deduplicate for us
319
+ q.select(cols);
320
+ if (isAggregateQuery(q)) {
321
+ // if an aggregation query, we need to push to groupby as well
322
+ // we also deduplicate as the column may already be present
323
+ const set = new Set(q._groupby.flatMap(x => x instanceof ColumnNameRefNode ? [x.name] : []));
324
+ q.groupby(cols.filter(c => !set.has(c)));
325
+ }
326
+ }
327
+ q.subqueries.forEach(pushdown);
328
+ };
329
+ pushdown(query);
330
+ }
331
+ /**
332
+ * Test if a query performs aggregation.
333
+ * @param query Select query to test.
334
+ * @returns True if query performs aggregation.
335
+ */
336
+ function isAggregateQuery(query) {
337
+ return query._groupby.length > 0
338
+ || query._select.some(node => isAggregateExpression(node));
339
+ }
340
+ /**
341
+ * Metadata and query generator for materialized views of pre-aggregated data.
342
+ * This object provides the information needed to generate and query the
343
+ * materialized views for a client-selection pair relative to a specific
344
+ * active clause and selection state.
345
+ */
346
+ export class PreAggregateInfo {
347
+ /** The name of the materialized view. */
348
+ table;
349
+ /** The SQL query used to generate the materialized view. */
350
+ create;
351
+ /** A result promise returned for the materialized view creation query. */
352
+ result;
353
+ /** Definitions and predicate function for the active columns,
354
+ * which are dynamically filtered by the active clause. */
355
+ active;
356
+ /** Select query (sans where clause) for materialized views. */
357
+ select;
358
+ /** Boolean flag indicating a client that should be skipped.
359
+ * This value is always false for a created materialized view. */
360
+ skip;
361
+ /**
362
+ * Create a new pre-aggregation information instance.
363
+ * @param options Options object.
364
+ */
365
+ constructor({ table, create, active, select }) {
366
+ this.table = table;
367
+ this.create = create;
368
+ this.result = null;
369
+ this.active = active;
370
+ this.select = select;
371
+ this.skip = false;
372
+ }
373
+ /**
374
+ * Generate a materialized view query for the given predicate.
375
+ * @param predicate The current active clause predicate.
376
+ * @returns A materialized view query.
377
+ */
378
+ query(predicate) {
379
+ return this.select.clone().where(this.active.predicate(predicate));
380
+ }
381
+ }
382
+ //# sourceMappingURL=PreAggregator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PreAggregator.js","sourceRoot":"","sources":["../../../src/preagg/PreAggregator.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,IAAI,YAAY,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,EAAE,WAAW,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,cAAc,EAAE,GAAG,EAAE,aAAa,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvO,OAAO,EAAE,aAAa,EAAuB,MAAM,qBAAqB,CAAC;AACzE,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,MAAM,IAAI,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AAwB1C;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,aAAa;IACjB,OAAO,CAA2D;IACjE,MAAM,CAA6B;IACnC,EAAE,CAAc;IAChB,OAA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C,CAAC;IACF,QAAQ,CAAC,KAAK,CAAC,CAAC;AAClB,CAAC;AAED;;;;GAIG;AACH,SAAS,gBAAgB,CAAC,KAAkB;IAC1C,OAAO,KAAK,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC;WAC3B,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,qBAAqB,CAAC,IAAI,CAAC,CAAC,CAAC;AAC/D,CAAC;AAED;;;;;GAKG;AACH,MAAM,OAAO,gBAAgB;IAC3B,yCAAyC;IACzC,KAAK,CAAS;IACd,4DAA4D;IAC5D,MAAM,CAAS;IACf,0EAA0E;IAC1E,MAAM,CAA0B;IAChC;8DAC0D;IAC1D,MAAM,CAAsB;IAC5B,+DAA+D;IAC/D,MAAM,CAAc;IACpB;qEACiE;IACjE,IAAI,CAAU;IAEd;;;OAGG;IACH,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAA2B;QACpE,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;IACpB,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,SAAmB;QACvB,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,SAAU,CAAC,SAAS,CAAE,CAAC,CAAC;IACvE,CAAC;CACF"}
@@ -0,0 +1,16 @@
1
+ import type { ExprNode } from '@uwdata/mosaic-sql';
2
+ import type { MosaicClient } from '../MosaicClient.js';
3
+ export interface PreAggColumnsResult {
4
+ group: string[];
5
+ preagg: Record<string, ExprNode>;
6
+ output: Record<string, ExprNode>;
7
+ }
8
+ /**
9
+ * Determine pre-aggregation columns for a given Mosaic client.
10
+ * @param client The Mosaic client.
11
+ * @returns An object with necessary column data to generate pre-aggregated
12
+ * columns, or null if the client can't be optimized or the client query
13
+ * contains an invalid or unsupported expression.
14
+ */
15
+ export declare function preaggColumns(client: MosaicClient): PreAggColumnsResult | null;
16
+ //# sourceMappingURL=preagg-columns.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"preagg-columns.d.ts","sourceRoot":"","sources":["../../../src/preagg/preagg-columns.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAgC,QAAQ,EAAsB,MAAM,oBAAoB,CAAC;AACrG,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAIvD,MAAM,WAAW,mBAAmB;IAClC,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACjC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;CAClC;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,YAAY,GAAG,mBAAmB,GAAG,IAAI,CA0D9E"}
@@ -0,0 +1,95 @@
1
+ import { collectAggregates, isAggregateExpression, isSelectQuery, isTableRef, rewrite, sql } from '@uwdata/mosaic-sql';
2
+ import { sufficientStatistics } from './sufficient-statistics.js';
3
+ /**
4
+ * Determine pre-aggregation columns for a given Mosaic client.
5
+ * @param client The Mosaic client.
6
+ * @returns An object with necessary column data to generate pre-aggregated
7
+ * columns, or null if the client can't be optimized or the client query
8
+ * contains an invalid or unsupported expression.
9
+ */
10
+ export function preaggColumns(client) {
11
+ if (!client.filterStable)
12
+ return null;
13
+ const q = client.query();
14
+ // bail if query is not analyzable
15
+ if (!isSelectQuery(q))
16
+ return null;
17
+ // bail if no base table
18
+ const from = getBase(q, q => {
19
+ const ref = q._from[0]?.expr;
20
+ return isTableRef(ref) ? ref.name : ref;
21
+ });
22
+ if (typeof from !== 'string')
23
+ return null;
24
+ const aggrs = new Map();
25
+ const preagg = {};
26
+ const output = {};
27
+ const group = []; // list of grouping dimension columns
28
+ // generate a scalar subquery for a global average
29
+ const avg = (ref) => {
30
+ const name = ref.column;
31
+ const expr = getBase(q, q => q._select.find(c => c.alias === name)?.expr);
32
+ return sql `(SELECT avg(${expr ?? ref}) FROM "${from}")`;
33
+ };
34
+ // iterate over select clauses and analyze expressions
35
+ for (const { alias, expr } of q._select) {
36
+ // bail if there is an aggregate we can't analyze
37
+ // a value > 1 indicates an aggregate in verbatim text
38
+ if (isAggregateExpression(expr) > 1)
39
+ return null;
40
+ const nodes = collectAggregates(expr);
41
+ if (nodes.length === 0) {
42
+ // if no aggregates, expr is a groupby dimension
43
+ group.push(alias);
44
+ preagg[alias] = expr;
45
+ }
46
+ else {
47
+ for (const node of nodes) {
48
+ // bail if distinct aggregate
49
+ if (node.isDistinct)
50
+ return null;
51
+ // bail if aggregate function is unsupported
52
+ // otherwise add output aggregate to rewrite map
53
+ const agg = sufficientStatistics(node, preagg, avg);
54
+ if (!agg)
55
+ return null;
56
+ aggrs.set(node, agg);
57
+ }
58
+ // rewrite original select clause to use preaggregates
59
+ output[alias] = rewrite(expr, aggrs);
60
+ }
61
+ }
62
+ // bail if the query has no aggregates
63
+ if (!aggrs.size)
64
+ return null;
65
+ return { group, preagg, output };
66
+ }
67
+ /**
68
+ * Identify a shared base (source) query and extract a value from it.
69
+ * This method is used to find a shared base table name or extract
70
+ * the original column name within a base table.
71
+ * @param query The input query.
72
+ * @param get A getter function to extract
73
+ * a value from a base query.
74
+ * @returns the base query value, or
75
+ * `undefined` if there is no source table, or `NaN` if the
76
+ * query operates over multiple source tables.
77
+ */
78
+ function getBase(query, get) {
79
+ const subq = query.subqueries;
80
+ // select query
81
+ if (isSelectQuery(query) && subq.length === 0) {
82
+ return get(query);
83
+ }
84
+ // handle set operations / subqueries
85
+ const base = getBase(subq[0], get);
86
+ for (let i = 1; i < subq.length; ++i) {
87
+ const value = getBase(subq[i], get);
88
+ if (value === undefined)
89
+ continue;
90
+ if (value !== base)
91
+ return NaN;
92
+ }
93
+ return base;
94
+ }
95
+ //# sourceMappingURL=preagg-columns.js.map