@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,388 @@
1
+ import { type MosaicClient } from './MosaicClient.js';
2
+ import { type ExprNode, FilterExpr, literal, MaybeArray, or } from '@uwdata/mosaic-sql';
3
+ import { Param } from './Param.js';
4
+ import { ClauseSource, SelectionClause } from './SelectionClause.js';
5
+
6
+ export interface SelectionOptions {
7
+ /** Boolean flag indicating cross-filtered resolution. If true, selection clauses will not be applied to the clients they are associated with. */
8
+ cross?: boolean;
9
+ /** Boolean flag indicating if a lack of clauses should correspond to an empty selection with no records. This setting determines the default selection state. */
10
+ empty?: boolean;
11
+ /** Upstream selections whose clauses should be included as part of the new selection. Any clauses published to upstream selections will be relayed to the new selection. */
12
+ include?: Selection | Selection[];
13
+ }
14
+
15
+ export interface SelectionResolverOptions extends Pick<SelectionOptions, "empty" | "cross"> {
16
+ /** Boolean flag to indicate a union strategy. If false, an intersection strategy is used. */
17
+ union?: boolean;
18
+ /** Boolean flag to indicate single clauses only. */
19
+ single?: boolean;
20
+ }
21
+
22
+ /**
23
+ * Test if a value is a Selection instance.
24
+ * @param x The value to test.
25
+ * @returns True if the input is a Selection, false otherwise.
26
+ */
27
+ export function isSelection(x: unknown): x is Selection {
28
+ return x instanceof Selection;
29
+ }
30
+
31
+ function create(options: SelectionResolverOptions, include?: Selection | Selection[]): Selection {
32
+ return new Selection(
33
+ new SelectionResolver(options),
34
+ include ? [include].flat() : undefined
35
+ );
36
+ }
37
+
38
+ type SelectionClauseArray = SelectionClause[] & { active?: SelectionClause };
39
+ type ResolvedPredicate = MaybeArray<string | boolean | ExprNode> | undefined;
40
+
41
+ /**
42
+ * Represents a dynamic set of query filter predicates.
43
+ */
44
+ export class Selection extends Param<SelectionClauseArray> {
45
+ _resolved: SelectionClauseArray;
46
+ _resolver: SelectionResolver;
47
+ _relay: Set<Selection>;
48
+
49
+ /**
50
+ * Create a new Selection instance with an
51
+ * intersect (conjunction) resolution strategy.
52
+ * @param options The selection options.
53
+ * @returns The new Selection instance.
54
+ */
55
+ static intersect({ cross = false, empty = false, include = [] }: SelectionOptions = {}): Selection {
56
+ return create({ cross, empty }, include);
57
+ }
58
+
59
+ /**
60
+ * Create a new Selection instance with a
61
+ * union (disjunction) resolution strategy.
62
+ * @param options The selection options.
63
+ * @returns The new Selection instance.
64
+ */
65
+ static union({ cross = false, empty = false, include = [] }: SelectionOptions = {}): Selection {
66
+ return create({ cross, empty, union: true }, include);
67
+ }
68
+
69
+ /**
70
+ * Create a new Selection instance with a singular resolution strategy
71
+ * that keeps only the most recent selection clause.
72
+ * @param options The selection options.
73
+ * @returns The new Selection instance.
74
+ */
75
+ static single({ cross = false, empty = false, include = [] }: SelectionOptions = {}): Selection {
76
+ return create({ cross, empty, single: true }, include);
77
+ }
78
+
79
+ /**
80
+ * Create a new Selection instance with a
81
+ * cross-filtered intersect resolution strategy.
82
+ * @param options The selection options.
83
+ * @returns The new Selection instance.
84
+ */
85
+ static crossfilter({ empty = false, include = [] }: Omit<SelectionOptions, 'cross'> = {}): Selection {
86
+ return create({ cross: true, empty }, include);
87
+ }
88
+
89
+ /**
90
+ * Create a new Selection instance.
91
+ * @param resolver The selection resolution
92
+ * strategy to apply.
93
+ * @param include Upstream selections whose clauses
94
+ * should be included as part of this selection. Any clauses published
95
+ * to these upstream selections will be relayed to this selection.
96
+ */
97
+ constructor(resolver = new SelectionResolver(), include: Selection[] = []) {
98
+ super([]);
99
+ this._resolved = this._value!;
100
+ this._resolver = resolver;
101
+ this._relay = new Set();
102
+ if (Array.isArray(include)) {
103
+ for (const sel of include) {
104
+ sel._relay.add(this);
105
+ }
106
+ }
107
+ }
108
+
109
+ /**
110
+ * Create a cloned copy of this Selection instance.
111
+ * @returns A clone of this selection.
112
+ */
113
+ clone(): Selection {
114
+ const s = new Selection(this._resolver);
115
+ s._value = s._resolved = this._value!;
116
+ return s;
117
+ }
118
+
119
+ /**
120
+ * Create a clone of this Selection with clauses corresponding
121
+ * to the provided source removed.
122
+ * @param source The clause source to remove.
123
+ * @returns A cloned and updated Selection.
124
+ */
125
+ remove(source: ClauseSource): Selection {
126
+ const s = this.clone();
127
+ s._value = s._resolved = s._resolver.resolve(
128
+ this._resolved,
129
+ { source } as SelectionClause
130
+ );
131
+ s._value.active = { source } as SelectionClause;
132
+ return s;
133
+ }
134
+
135
+ /**
136
+ * The selection clause resolver.
137
+ */
138
+ get resolver(): SelectionResolver {
139
+ return this._resolver;
140
+ }
141
+
142
+ /**
143
+ * Indicate if this selection has a single resolution strategy.
144
+ */
145
+ get single(): boolean {
146
+ return this._resolver.single;
147
+ }
148
+
149
+ /**
150
+ * The current array of selection clauses.
151
+ */
152
+ get clauses(): SelectionClauseArray {
153
+ return super.value!;
154
+ }
155
+
156
+ /**
157
+ * The current active (most recently updated) selection clause.
158
+ */
159
+ get active(): SelectionClause {
160
+ return this.clauses.active!;
161
+ }
162
+
163
+ /**
164
+ * The value corresponding to the current active selection clause.
165
+ * This method ensures compatibility where a normal Param is expected.
166
+ */
167
+ // @ts-expect-error return type differs from Param parent class
168
+ get value(): unknown {
169
+ return this.active?.value;
170
+ }
171
+
172
+ /**
173
+ * The value corresponding to a given source. Returns undefined if
174
+ * this selection does not include a clause from this source.
175
+ * @param source The clause source to look up the value for.
176
+ */
177
+ valueFor(source: unknown): unknown {
178
+ return this.clauses.find(c => c.source === source)?.value;
179
+ }
180
+
181
+ /**
182
+ * Emit an activate event with the given selection clause.
183
+ * @param clause The clause representing the potential activation.
184
+ */
185
+ activate(clause: SelectionClause): void {
186
+ // @ts-expect-error selection operates differently than scalar param
187
+ this.emit('activate', clause);
188
+ this._relay.forEach(sel => sel.activate(clause));
189
+ }
190
+
191
+ /**
192
+ * Update the selection with a new selection clause.
193
+ * @param clause The selection clause to add.
194
+ * @returns This Selection instance.
195
+ */
196
+ // @ts-expect-error selection and param use differing value types
197
+ update(clause: SelectionClause): this {
198
+ // we maintain an up-to-date list of all resolved clauses
199
+ // this ensures consistent clause state across unemitted event values
200
+ this._resolved = this._resolver.resolve(this._resolved, clause, true);
201
+ this._resolved.active = clause;
202
+ this._relay.forEach(sel => sel.update(clause));
203
+ return super.update(this._resolved);
204
+ }
205
+
206
+ /**
207
+ * Reset the selection state by removing all provided clauses. If no clause
208
+ * array is provided as an argument, all current clauses are removed. The
209
+ * reset method (if defined) is invoked on all corresponding clause sources.
210
+ * The reset is relayed to downstream selections that include this selection.
211
+ * @param clauses The clauses to remove. If unspecified, all current clauses are removed.
212
+ * @returns This selection instance.
213
+ */
214
+ reset(clauses?: SelectionClause[]): this {
215
+ clauses ??= this._resolved;
216
+ clauses.forEach(c => c.source?.reset?.());
217
+ this._resolved = this._resolved.filter(c => clauses!.includes(c));
218
+ this._relay.forEach(sel => sel.reset(clauses));
219
+ return super.update(this._resolved = []);
220
+ }
221
+
222
+ /**
223
+ * Upon value-typed updates, sets the current clause list to the
224
+ * input value and returns the active clause value.
225
+ * @param type The event type.
226
+ * @param value The input event value.
227
+ * @returns For value-typed events, returns the active clause
228
+ * values. Otherwise returns the input event value as-is.
229
+ */
230
+ // @ts-expect-error selection and param use differing value types
231
+ willEmit(type: string, value: unknown): unknown {
232
+ if (type === 'value') {
233
+ this._value = value as SelectionClauseArray;
234
+ return this.value;
235
+ }
236
+ return value;
237
+ }
238
+
239
+ /**
240
+ * Upon value-typed updates, returns a dispatch queue filter function.
241
+ * The return value depends on the selection resolution strategy.
242
+ * @param type The event type.
243
+ * @param value The new event value that will be enqueued.
244
+ * @returns A dispatch queue filter function. For non-value events,
245
+ * returns a function that always returns null (no filtering).
246
+ */
247
+ // @ts-expect-error selection and param use differing value types
248
+ emitQueueFilter(
249
+ type: string,
250
+ value: SelectionClauseArray
251
+ ): ((value: SelectionClauseArray) => boolean) | null {
252
+ return type === 'value'
253
+ ? this._resolver.queueFilter(value)
254
+ : null;
255
+ }
256
+
257
+ /**
258
+ * Indicates if a selection clause should not be applied to a given client.
259
+ * The return value depends on the selection resolution strategy.
260
+ * @param client The client to test.
261
+ * @param clause The selection clause.
262
+ * @returns True if the client should be skipped, false otherwise.
263
+ */
264
+ skip(client: MosaicClient, clause: SelectionClause): boolean {
265
+ return this._resolver.skip(client, clause);
266
+ }
267
+
268
+ /**
269
+ * Return a selection query predicate for the given client.
270
+ * @param client The client whose data may be filtered.
271
+ * @param noSkip Disable skipping of active
272
+ * cross-filtered sources. If set true, the source of the active
273
+ * clause in a cross-filtered selection will not be skipped.
274
+ * @returns The query predicate for filtering client data,
275
+ * based on the current state of this selection.
276
+ */
277
+ predicate(client: MosaicClient, noSkip: boolean = false): ResolvedPredicate {
278
+ const { clauses } = this;
279
+ const active = noSkip ? null : clauses.active;
280
+ return this._resolver.predicate(clauses, active!, client);
281
+ }
282
+ }
283
+
284
+ /**
285
+ * Implements selection clause resolution strategies.
286
+ */
287
+ export class SelectionResolver {
288
+ union: boolean;
289
+ cross: boolean;
290
+ single: boolean;
291
+ empty: boolean;
292
+
293
+ /**
294
+ * Create a new selection resolved instance.
295
+ * @param options The resolution strategy options.
296
+ * @param options.union Boolean flag to indicate a union strategy.
297
+ * If false, an intersection strategy is used.
298
+ * @param options.cross Boolean flag to indicate cross-filtering.
299
+ * @param options.single Boolean flag to indicate single clauses only.
300
+ * @param options.empty Boolean flag indicating if a lack
301
+ * of clauses should correspond to an empty selection with no records. This
302
+ * setting determines the default selection state.
303
+ */
304
+ constructor({ union, cross, single, empty }: SelectionResolverOptions = {}) {
305
+ this.union = !!union;
306
+ this.cross = !!cross;
307
+ this.single = !!single;
308
+ this.empty = !!empty;
309
+ }
310
+
311
+ /**
312
+ * Resolve a list of selection clauses according to the resolution strategy.
313
+ * @param clauseList An array of selection clauses.
314
+ * @param clause A new selection clause to add.
315
+ * @returns An updated array of selection clauses.
316
+ */
317
+ resolve(
318
+ clauseList: SelectionClause[],
319
+ clause: SelectionClause,
320
+ reset: boolean = false
321
+ ): SelectionClause[] {
322
+ const { source, predicate } = clause;
323
+ const filtered = clauseList.filter(c => source !== c.source);
324
+ const clauses = this.single ? [] : filtered;
325
+ if (this.single && reset) filtered.forEach(c => c.source?.reset?.());
326
+ if (predicate) clauses.push(clause);
327
+ return clauses;
328
+ }
329
+
330
+ /**
331
+ * Indicates if a selection clause should not be applied to a given client.
332
+ * The return value depends on the resolution strategy.
333
+ * @param client The selection clause.
334
+ * @param clause The client to test.
335
+ * @returns True if the client should be skipped, false otherwise.
336
+ */
337
+ skip(client: MosaicClient, clause: SelectionClause): boolean {
338
+ return Boolean(this.cross && clause?.clients?.has(client));
339
+ }
340
+
341
+ /**
342
+ * Return a selection query predicate for the given client.
343
+ * @param clauseList An array of selection clauses.
344
+ * @param active The current active selection clause.
345
+ * @param client The client whose data may be filtered.
346
+ * @returns The query predicate for filtering client data,
347
+ * based on the current state of this selection.
348
+ */
349
+ predicate(
350
+ clauseList: SelectionClause[],
351
+ active: SelectionClause,
352
+ client: MosaicClient
353
+ ): ResolvedPredicate {
354
+ const { empty, union } = this;
355
+
356
+ if (empty && !clauseList.length) {
357
+ return [literal(false)];
358
+ }
359
+
360
+ // do nothing if cross-filtering and client is currently active
361
+ if (this.skip(client, active)) return undefined;
362
+
363
+ // remove client-specific predicates if cross-filtering
364
+ const predicates: FilterExpr = clauseList
365
+ .filter(clause => !this.skip(client, clause))
366
+ .map(clause => clause.predicate!);
367
+
368
+ // return appropriate conjunction or disjunction
369
+ // an array of predicates is implicitly conjunctive
370
+ return union && predicates.length > 1 ? or(predicates) : predicates;
371
+ }
372
+
373
+ /**
374
+ * Returns a filter function for queued selection updates.
375
+ * @param value The new event value that will be enqueued.
376
+ * @returns A dispatch queue filter
377
+ * function, or null if all unemitted event values should be filtered.
378
+ */
379
+ queueFilter(
380
+ value: SelectionClauseArray
381
+ ): ((value: SelectionClauseArray) => boolean) | null {
382
+ if (this.cross) {
383
+ const source = value.active?.source;
384
+ return value => value.active?.source !== source;
385
+ }
386
+ return null;
387
+ }
388
+ }
@@ -0,0 +1,275 @@
1
+
2
+ import { isMosaicClient, MosaicClient } from './MosaicClient.js';
3
+ import { type ExprNode, type ExprValue, type ScaleOptions, type ScaleDomain, and, contains, isBetween, isIn, isNotDistinct, literal, or, prefix, regexp_matches, suffix } from '@uwdata/mosaic-sql';
4
+
5
+ /**
6
+ * Selection clause metadata to guide possible query optimizations.
7
+ * Sub-interfaces provide more information about the specifics of a
8
+ * given selection based on the selection type.
9
+ */
10
+ export interface ClauseMetadata {
11
+ /**
12
+ * The selection type, such as `'point'`, `'interval'`, or `'match'`.
13
+ */
14
+ type: string;
15
+ }
16
+
17
+ /**
18
+ * Selection clause metadata indicating selection of one or more discrete
19
+ * point values, typically based on equality or is distinctiveness checks.
20
+ */
21
+ export interface PointMetadata extends ClauseMetadata {
22
+ type: 'point';
23
+ }
24
+
25
+ /**
26
+ * Selection clause metadata indicating text search matching.
27
+ */
28
+ export interface MatchMetadata extends ClauseMetadata {
29
+ type: MatchMethod;
30
+ /** The text search matching method used. */
31
+ method?: 'contains' | 'prefix' | 'suffix' | 'regexp' | (string & {});
32
+ }
33
+
34
+ /** A binning method name. */
35
+ export type BinMethod = 'floor' | 'ceil' | 'round';
36
+
37
+ /**
38
+ * Selection clause metadata for one or more selected intervals. This
39
+ * metadata can be used to determine appropriate data-space binning
40
+ * schemes that correspond to pixel-level bins in screen space.
41
+ */
42
+ export interface IntervalMetadata extends ClauseMetadata {
43
+ type: 'interval';
44
+ /**
45
+ * The interactive pixel size used by the generating component.
46
+ * Values larger than one indicate intervals that "snap-to" values
47
+ * greater than a single pixel. If unspecified, assumed to be `1`.
48
+ */
49
+ pixelSize?: number;
50
+ /**
51
+ * An array of one or more scale descriptors that describe the
52
+ * mapping from data values to screen pixels.
53
+ */
54
+ scales?: ScaleOptions[];
55
+ /**
56
+ * A hint for the binning method to use when discretizing the
57
+ * interval domain. If unspecified, the default is `'floor'`.
58
+ */
59
+ bin?: BinMethod
60
+ }
61
+
62
+ export interface ClauseSource {
63
+ reset?: () => void;
64
+ }
65
+
66
+ /**
67
+ * A selection clause representing filtering criteria
68
+ * to apply within a Mosaic Selection.
69
+ */
70
+ export interface SelectionClause {
71
+ /**
72
+ * A unique identifier (according to object equality) for the source
73
+ * component that generated this clause. In many cases, this is a
74
+ * reference to the originating component itself.
75
+ */
76
+ source: ClauseSource;
77
+ /**
78
+ * A set of Mosaic clients associated with this clause that should not
79
+ * be updated when this clause is applied in a cross-filtering context.
80
+ */
81
+ clients?: Set<MosaicClient>;
82
+ /**
83
+ * A selected value associated with this clause. For example, for a 1D
84
+ * interval selection clause the value may be a [lo, hi] array.
85
+ */
86
+ value: unknown;
87
+ /**
88
+ * A predicate SQL expression suitable for use in a query WHERE clause.
89
+ * The predicate should apply filtering criteria consistent with this
90
+ * clause's *value* property.
91
+ */
92
+ predicate: ExprNode | null;
93
+ /**
94
+ * Optional clause metadata that varies based on the selection type.
95
+ * The metadata can be used to optimize selection queries, for example
96
+ * by creating materialized views of pre-aggregated data when applicable.
97
+ */
98
+ meta?: ClauseMetadata;
99
+ }
100
+
101
+ /**
102
+ * Generate a selection clause for a single selected point value.
103
+ * @param field The table column or expression to select.
104
+ * @param value The selected value.
105
+ * @param options Additional clause properties.
106
+ * @param options.source The source component generating this clause.
107
+ * @param options.clients The Mosaic clients associated
108
+ * with this clause. These clients are not filtered by this clause in
109
+ * cross-filtering contexts.
110
+ * @returns The generated selection clause.
111
+ */
112
+ export function clausePoint(field: ExprValue, value: unknown, {
113
+ source,
114
+ clients = isMosaicClient(source) ? new Set([source]) : undefined
115
+ }: {
116
+ source: ClauseSource;
117
+ clients?: Set<MosaicClient>;
118
+ }): SelectionClause {
119
+ const predicate: ExprNode | null = value !== undefined
120
+ ? isIn(field, [literal(value)])
121
+ : null;
122
+ return {
123
+ meta: { type: 'point' },
124
+ source,
125
+ clients,
126
+ value,
127
+ predicate
128
+ };
129
+ }
130
+
131
+ /**
132
+ * Generate a selection clause for multiple selected point values.
133
+ * @param fields The table columns or expressions to select.
134
+ * @param value The selected values, as an array of
135
+ * arrays. Each subarray contains values for each *fields* entry.
136
+ * @param options Additional clause properties.
137
+ * @param options.source The source component generating this clause.
138
+ * @param options.clients The Mosaic clients associated
139
+ * with this clause. These clients are not filtered by this clause in
140
+ * cross-filtering contexts.
141
+ * @returns The generated selection clause.
142
+ */
143
+ export function clausePoints(fields: ExprValue[], value: unknown[][] | null | undefined, {
144
+ source,
145
+ clients = isMosaicClient(source) ? new Set([source]) : undefined
146
+ }: {
147
+ source: ClauseSource;
148
+ clients?: Set<MosaicClient>;
149
+ }): SelectionClause {
150
+ let predicate: ExprNode | null = null;
151
+ if (value) {
152
+ const clauses = value.length && fields.length === 1
153
+ ? [isIn(fields[0], value.map(v => literal(v[0])))]
154
+ : value.map(v => and(v.map((_, i) => isNotDistinct(fields[i], literal(_)))));
155
+ predicate = value.length === 0 ? literal(false)
156
+ : clauses.length > 1 ? or(clauses)
157
+ : clauses[0];
158
+ }
159
+ return {
160
+ meta: { type: 'point' },
161
+ source,
162
+ clients,
163
+ value,
164
+ predicate
165
+ };
166
+ }
167
+
168
+ /**
169
+ * Generate a selection clause for a selected 1D interval.
170
+ * @param field The table column or expression to select.
171
+ * @param value The selected interval as a [lo, hi] array.
172
+ * @param options Additional clause properties.
173
+ * @param options.source The source component generating this clause.
174
+ * @param options.clients The Mosaic clients associated
175
+ * with this clause. These clients are not filtered by this clause in
176
+ * cross-filtering contexts.
177
+ * @param options.scale The scale mapping descriptor.
178
+ * @param options.bin A binning method hint.
179
+ * @param options.pixelSize The interactive pixel size.
180
+ * @returns The generated selection clause.
181
+ */
182
+ export function clauseInterval(field: ExprValue, value: ScaleDomain | null | undefined, {
183
+ source,
184
+ clients = isMosaicClient(source) ? new Set([source]) : undefined,
185
+ bin,
186
+ scale,
187
+ pixelSize = 1
188
+ }: {
189
+ source: ClauseSource;
190
+ clients?: Set<MosaicClient>;
191
+ scale?: ScaleOptions;
192
+ bin?: BinMethod;
193
+ pixelSize?: number;
194
+ }): SelectionClause {
195
+ const predicate = value != null ? isBetween(field, value) : null;
196
+ const meta: IntervalMetadata = {
197
+ type: 'interval',
198
+ scales: scale && [scale],
199
+ bin,
200
+ pixelSize
201
+ };
202
+ return { meta, source, clients, value, predicate };
203
+ }
204
+
205
+ /**
206
+ * Generate a selection clause for multiple selected intervals.
207
+ * @param fields The table columns or expressions to select.
208
+ * @param value The selected intervals, as an array of extents.
209
+ * @param options Additional clause properties.
210
+ * @param options.source The source component generating this clause.
211
+ * @param options.clients The Mosaic clients associated
212
+ * with this clause. These clients are not filtered by this clause in
213
+ * cross-filtering contexts.
214
+ * @param options.scales The scale mapping descriptors,
215
+ * in an order matching the given *fields* and *value* extents.
216
+ * @param options.bin A binning method hint.
217
+ * @param options.pixelSize The interactive pixel size.
218
+ * @returns The generated selection clause.
219
+ */
220
+ export function clauseIntervals(fields: ExprValue[], value: ScaleDomain[] | null | undefined, {
221
+ source,
222
+ clients = isMosaicClient(source) ? new Set([source]) : undefined,
223
+ bin,
224
+ scales = [],
225
+ pixelSize = 1
226
+ }: {
227
+ source: ClauseSource;
228
+ clients?: Set<MosaicClient>;
229
+ scales?: ScaleOptions[];
230
+ bin?: BinMethod;
231
+ pixelSize?: number;
232
+ }): SelectionClause {
233
+ const predicate = value != null
234
+ ? and(fields.map((f, i) => isBetween(f, value[i])))
235
+ : null;
236
+ const meta: IntervalMetadata = {
237
+ type: 'interval',
238
+ scales,
239
+ bin,
240
+ pixelSize
241
+ };
242
+ return { meta, source, clients, value, predicate };
243
+ }
244
+
245
+ const MATCH_METHODS = { contains, prefix, suffix, regexp: regexp_matches };
246
+
247
+ /** Text search matching methods. */
248
+ export type MatchMethod = keyof typeof MATCH_METHODS | (string & {});
249
+
250
+ /**
251
+ * Generate a selection clause for text search matching.
252
+ * @param field The table column or expression to select.
253
+ * @param value The selected text search query string.
254
+ * @param options Additional clause properties.
255
+ * @param options.source The source component generating this clause.
256
+ * @param options.clients The Mosaic clients associated
257
+ * with this clause. These clients are not filtered by this clause in
258
+ * cross-filtering contexts.
259
+ * @param options.method The text matching method to use. Defaults to `'contains'`.
260
+ * @returns The generated selection clause.
261
+ */
262
+ export function clauseMatch(field: ExprValue, value: string | null | undefined, {
263
+ source,
264
+ clients = undefined,
265
+ method = 'contains'
266
+ }: {
267
+ source: ClauseSource;
268
+ clients?: Set<MosaicClient>;
269
+ method?: MatchMethod;
270
+ }): SelectionClause {
271
+ const fn = MATCH_METHODS[method as keyof typeof MATCH_METHODS];
272
+ const predicate: ExprNode | null = value ? fn(field, literal(value)) : null;
273
+ const meta: MatchMetadata = { type: 'match', method };
274
+ return { meta, source, clients, value, predicate };
275
+ }
@@ -1,23 +1,23 @@
1
1
  import type { Table } from '@uwdata/flechette';
2
2
 
3
- export interface QueryRequest {
3
+ export interface ConnectorQueryRequest {
4
4
  /** The query type. */
5
5
  type?: string;
6
6
  /** A SQL query string. */
7
7
  sql: string;
8
8
  }
9
9
 
10
- export interface ArrowQueryRequest extends QueryRequest {
10
+ export interface ArrowQueryRequest extends ConnectorQueryRequest {
11
11
  /** The query type. */
12
12
  type?: 'arrow';
13
13
  }
14
14
 
15
- export interface ExecQueryRequest extends QueryRequest {
15
+ export interface ExecQueryRequest extends ConnectorQueryRequest {
16
16
  /** The query type. */
17
17
  type: 'exec';
18
18
  }
19
19
 
20
- export interface JSONQueryRequest extends QueryRequest {
20
+ export interface JSONQueryRequest extends ConnectorQueryRequest {
21
21
  /** The query type. */
22
22
  type: 'json';
23
23
  }
@@ -26,5 +26,5 @@ export interface Connector {
26
26
  /** Issue a query and return the result. */
27
27
  query(query: ArrowQueryRequest): Promise<Table>;
28
28
  query(query: ExecQueryRequest): Promise<void>;
29
- query(query: JSONQueryRequest): Promise<Record<string, any>[]>;
30
- }
29
+ query(query: JSONQueryRequest): Promise<Record<string, unknown>[]>;
30
+ }