@perspective-dev/client 5.5.1 → 5.6.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 (40) hide show
  1. package/README.md +66 -2
  2. package/dist/cdn/perspective-server.worker.js +1 -1
  3. package/dist/cdn/perspective-server.worker.js.map +3 -3
  4. package/dist/cdn/perspective.js +2 -2
  5. package/dist/cdn/perspective.js.map +4 -4
  6. package/dist/esm/perspective.js +2 -2
  7. package/dist/esm/perspective.js.map +4 -4
  8. package/dist/esm/perspective.node.js +232 -67
  9. package/dist/esm/perspective.node.js.map +2 -2
  10. package/dist/esm/ts-rs/Features.d.ts +5 -0
  11. package/dist/esm/ts-rs/TableInitOptions.d.ts +8 -0
  12. package/dist/esm/ts-rs/TypedArrayWindow.d.ts +5 -4
  13. package/dist/esm/ts-rs/ViewWindow.d.ts +5 -4
  14. package/dist/esm/virtual_servers/clickhouse.js +1 -1
  15. package/dist/esm/virtual_servers/clickhouse.js.map +3 -3
  16. package/dist/esm/virtual_servers/duckdb.d.ts +7 -1
  17. package/dist/esm/virtual_servers/duckdb.js +1 -1
  18. package/dist/esm/virtual_servers/duckdb.js.map +3 -3
  19. package/dist/esm/wasm/decompress.d.ts +1 -0
  20. package/dist/wasm/perspective-js.d.ts +97 -19
  21. package/dist/wasm/perspective-js.js +172 -45
  22. package/dist/wasm/perspective-js.wasm +0 -0
  23. package/dist/wasm/perspective-js.wasm.d.ts +11 -7
  24. package/package.json +21 -16
  25. package/src/rust/client.rs +2 -1
  26. package/src/rust/generic_sql_model.rs +70 -6
  27. package/src/rust/table.rs +33 -1
  28. package/src/rust/view.rs +14 -5
  29. package/src/rust/virtual_server.rs +101 -23
  30. package/src/ts/ts-rs/Features.ts +6 -1
  31. package/src/ts/ts-rs/TableInitOptions.ts +7 -1
  32. package/src/ts/ts-rs/TypedArrayWindow.ts +5 -4
  33. package/src/ts/ts-rs/ViewWindow.ts +5 -4
  34. package/src/ts/virtual_servers/clickhouse.ts +99 -4
  35. package/src/ts/virtual_servers/duckdb.ts +117 -7
  36. package/src/ts/wasm/decompress.ts +42 -23
  37. package/src/ts/wasm/engine.ts +26 -35
  38. package/dist/esm/perspective.inline.js +0 -3
  39. package/dist/esm/perspective.inline.js.map +0 -7
  40. package/src/ts/perspective.inline.ts +0 -27
@@ -24,6 +24,10 @@
24
24
  import type * as perspective from "@perspective-dev/client";
25
25
  import type { ColumnType } from "@perspective-dev/client/dist/esm/ts-rs/ColumnType.d.ts";
26
26
  import type { ViewConfig } from "@perspective-dev/client/dist/esm/ts-rs/ViewConfig.d.ts";
27
+ import type {
28
+ ExpressionError,
29
+ TableDescription,
30
+ } from "@perspective-dev/client";
27
31
  import type { ViewConfigUpdate } from "@perspective-dev/client/dist/esm/ts-rs/ViewConfigUpdate.d.ts";
28
32
  import type { ViewWindow } from "@perspective-dev/client/dist/esm/ts-rs/ViewWindow.d.ts";
29
33
  import type { WindowAggSpec } from "@perspective-dev/client/dist/esm/ts-rs/WindowAggSpec.d.ts";
@@ -181,7 +185,7 @@ function duckdbTypeToPsp(name: string): ColumnType {
181
185
  ["bigint", "hugeint", "uhugeint", "uinteger", "ubigint"].includes(
182
186
  name,
183
187
  ) ||
184
- ["float", "real", "double", "varint"].includes(name) ||
188
+ ["float", "real", "double", "varint", "bignum"].includes(name) ||
185
189
  ["int64", "uint32", "uint64", "float32", "float64"].includes(name) ||
186
190
  name.startsWith("decimal") ||
187
191
  name.startsWith("numeric")
@@ -295,6 +299,7 @@ export class DuckDBHandler implements perspective.VirtualServerHandler {
295
299
  split_by: true,
296
300
  sort: true,
297
301
  expressions: true,
302
+ view_derivations: true,
298
303
  window_aggregates: {
299
304
  // `ema` is recursive and has no SQL window translation.
300
305
  integer: WINDOW_AGGREGATES,
@@ -386,15 +391,116 @@ export class DuckDBHandler implements perspective.VirtualServerHandler {
386
391
  await runQuery(this.db, query);
387
392
  }
388
393
 
389
- async tableValidateExpression(tableId: string, expression: string) {
390
- const query = this.sqlBuilder.tableValidateExpression(
394
+ async tableDescribe(
395
+ tableId: string,
396
+ config: ViewConfig,
397
+ ): Promise<TableDescription> {
398
+ let expression_schema = {} as Record<string, ColumnType>;
399
+ const expressions_query = this.sqlBuilder.expressionsDescribe(
400
+ tableId,
401
+ config,
402
+ );
403
+
404
+ if (expressions_query !== undefined) {
405
+ try {
406
+ expression_schema = await this.describeQuery(expressions_query);
407
+ } catch (error) {
408
+ return await this.attributeExpressionErrors(
409
+ tableId,
410
+ config,
411
+ error,
412
+ );
413
+ }
414
+ }
415
+
416
+ const schema = Object.keys(config.windows ?? {}).length
417
+ ? await this.tableSchema(tableId)
418
+ : undefined;
419
+
420
+ const view_query = this.sqlBuilder.tableDescribe(
391
421
  tableId,
392
- expression,
422
+ config,
423
+ schema,
393
424
  );
425
+
426
+ let view_schema = {} as Record<string, ColumnType>;
427
+ if (view_query !== undefined) {
428
+ try {
429
+ view_schema = await this.describeQuery(view_query);
430
+ } catch (error) {
431
+ return { config_error: errorMessage(error) };
432
+ }
433
+ }
434
+
435
+ return { expression_schema, view_schema };
436
+ }
437
+
438
+ /** The planned result columns of one `DESCRIBE` query. */
439
+ private async describeQuery(query: string) {
394
440
  const results = await runQuery(this.db, query);
395
- return duckdbTypeToPsp(
396
- results[0].toJSON()["column_type"],
397
- ) as ColumnType;
441
+ const schema = {} as Record<string, ColumnType>;
442
+ for (const result of results) {
443
+ const res = result.toJSON();
444
+ if (!res.column_name.startsWith("__")) {
445
+ schema[res.column_name] = duckdbTypeToPsp(
446
+ res.column_type,
447
+ ) as ColumnType;
448
+ }
449
+ }
450
+
451
+ return schema;
452
+ }
453
+
454
+ /** Plans each expression on its own to name the ones at fault. */
455
+ private async attributeExpressionErrors(
456
+ tableId: string,
457
+ config: ViewConfig,
458
+ error: unknown,
459
+ ): Promise<TableDescription> {
460
+ const expression_schema = {} as Record<string, ColumnType>;
461
+ const expression_errors = {} as Record<string, ExpressionError>;
462
+ for (const [name, expression] of Object.entries(config.expressions)) {
463
+ const query = this.sqlBuilder.expressionDescribe(
464
+ tableId,
465
+ expression!,
466
+ );
467
+
468
+ try {
469
+ const results = await runQuery(this.db, query);
470
+ expression_schema[name] = duckdbTypeToPsp(
471
+ results[0].toJSON()["column_type"],
472
+ ) as ColumnType;
473
+ } catch (error) {
474
+ expression_errors[name] = {
475
+ error_message: errorMessage(error),
476
+ line: 0,
477
+ column: 0,
478
+ };
479
+ }
480
+ }
481
+
482
+ if (Object.keys(expression_errors).length === 0) {
483
+ return { config_error: errorMessage(error) };
484
+ }
485
+
486
+ return { expression_schema, expression_errors };
487
+ }
488
+
489
+ async viewMakeTable(
490
+ viewId: string,
491
+ tableId: string,
492
+ config: ViewConfig,
493
+ schema?: Record<string, ColumnType>,
494
+ ) {
495
+ const query = this.sqlBuilder.viewMakeTable(
496
+ viewId,
497
+ tableId,
498
+ config,
499
+ await this.tableSchema(viewId),
500
+ schema,
501
+ );
502
+
503
+ await runQuery(this.db, query);
398
504
  }
399
505
 
400
506
  async viewDelete(viewId: string) {
@@ -437,3 +543,7 @@ export class DuckDBHandler implements perspective.VirtualServerHandler {
437
543
  dataSlice.fromArrowIpc(ipc);
438
544
  }
439
545
  }
546
+
547
+ function errorMessage(error: unknown): string {
548
+ return error instanceof Error ? error.message : String(error);
549
+ }
@@ -10,8 +10,44 @@
10
10
  // ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11
11
  // ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
12
 
13
- // @ts-ignore
14
- import { extract } from "pro_self_extracting_wasm";
13
+ type ModType = {
14
+ resize: (x: number) => number;
15
+ memory: WebAssembly.Memory;
16
+ compile: (x: number, y: number) => number;
17
+ };
18
+
19
+ export async function extract(input: any) {
20
+ let bin: WebAssembly.WebAssemblyInstantiatedSource;
21
+ if (input instanceof Response) {
22
+ bin = await WebAssembly.instantiateStreaming(input);
23
+ } else if (typeof input === "string") {
24
+ bin = await WebAssembly.instantiateStreaming(fetch(input));
25
+ } else if (input instanceof ArrayBuffer) {
26
+ bin = await WebAssembly.instantiate(input);
27
+ } else {
28
+ bin = input;
29
+ }
30
+
31
+ const inst = bin.instance.exports as ModType;
32
+ const [payload] = WebAssembly.Module.customSections(
33
+ bin.module,
34
+ "psp-runtime",
35
+ );
36
+
37
+ const [raw_length_view] = WebAssembly.Module.customSections(
38
+ bin.module,
39
+ "psp-len",
40
+ );
41
+
42
+ const raw_length = new DataView(raw_length_view).getUint32(0, true);
43
+ const ptr = inst.resize(payload.byteLength);
44
+ new Uint8Array(inst.memory.buffer).set(new Uint8Array(payload), ptr);
45
+ const offset = inst.compile(payload.byteLength, raw_length);
46
+ return new Uint8Array(inst.memory.buffer).slice(
47
+ offset,
48
+ raw_length + offset,
49
+ );
50
+ }
15
51
 
16
52
  if (typeof WebAssembly === "undefined") {
17
53
  throw new Error("WebAssembly not supported.");
@@ -23,27 +59,6 @@ interface Module extends WebAssembly.Exports {
23
59
  memory: WebAssembly.Memory;
24
60
  }
25
61
 
26
- // Perform a silly dance to deal with the different ways webpack and esbuild
27
- // load binary, as this may either be an `ArrayBuffer` or `URL` depending
28
- // on whether `inline` option was specified to `perspective-esbuild-plugin`.
29
- async function compile(
30
- buffer: ArrayBuffer | Response | WebAssembly.Module | WebAssembly.Exports,
31
- ): Promise<Module> {
32
- if (buffer instanceof Response) {
33
- return (await WebAssembly.instantiateStreaming(buffer)).instance
34
- .exports as Module;
35
- } else if (buffer instanceof WebAssembly.Module) {
36
- return (await WebAssembly.instantiate(buffer)).exports as Module;
37
- } else if (buffer instanceof WebAssembly.Instance) {
38
- return buffer.exports as Module;
39
- } else if (buffer instanceof ArrayBuffer) {
40
- return (await WebAssembly.instantiate(buffer as BufferSource)).instance
41
- .exports as Module;
42
- } else {
43
- return buffer as Module;
44
- }
45
- }
46
-
47
62
  export async function load_wasm_stage_0(
48
63
  wasm:
49
64
  | ArrayBuffer
@@ -55,6 +70,10 @@ export async function load_wasm_stage_0(
55
70
  wasm = await wasm();
56
71
  }
57
72
 
73
+ if (wasm instanceof Promise) {
74
+ wasm = await wasm;
75
+ }
76
+
58
77
  try {
59
78
  return await extract(wasm as ArrayBuffer);
60
79
  } catch (e) {
@@ -247,7 +247,8 @@ async function convert_typed_array_to_pointer(
247
247
  *
248
248
  * @param callback A callback to which is passed the responses. THe responses
249
249
  * must be fully processed or copied before the callback returns, as it
250
- * references memory on the wasm stack.
250
+ * references memory on the wasm stack, which a `callback` that allocates may
251
+ * also detach - hence the message descriptors are read before dispatching.
251
252
  */
252
253
  async function decode_api_responses(
253
254
  core: MainModule,
@@ -255,28 +256,32 @@ async function decode_api_responses(
255
256
  callback: (_: ApiResponse) => Promise<void>,
256
257
  ) {
257
258
  const is_64 = core._psp_is_memory64();
259
+ const response_ptr = is_64 ? Number(ptr) : Number(ptr) >>> 0;
258
260
  const response = new DataView(
259
261
  core.HEAPU8.buffer,
260
- is_64 ? Number(ptr) : Number(ptr) >>> 0,
262
+ response_ptr,
261
263
  is_64 ? 12 : 8,
262
264
  );
263
265
 
264
266
  const num_msgs = response.getUint32(0, true);
265
- const msgs_ptr = is_64
266
- ? response.getBigInt64(4, true)
267
- : response.getUint32(4, true);
267
+ const msgs_ptr = Number(
268
+ is_64 ? response.getBigInt64(4, true) : response.getUint32(4, true),
269
+ );
268
270
 
269
271
  const messages = new DataView(
270
272
  core.HEAPU8.buffer,
271
- Number(msgs_ptr),
273
+ msgs_ptr,
272
274
  num_msgs * (is_64 ? 16 : 12),
273
275
  );
274
276
 
275
- try {
276
- for (let i = 0; i < num_msgs; i++) {
277
- const [data_ptr, data_len, client_id] = is_64
277
+ const free = (x: number) => core._psp_free((is_64 ? BigInt(x) : x) as any);
278
+
279
+ const descriptors: [number, number, number][] = [];
280
+ for (let i = 0; i < num_msgs; i++) {
281
+ descriptors.push(
282
+ is_64
278
283
  ? [
279
- messages.getBigInt64(i * 16, true),
284
+ Number(messages.getBigInt64(i * 16, true)),
280
285
  messages.getInt32(i * 16 + 8, true),
281
286
  messages.getInt32(i * 16 + 12, true),
282
287
  ]
@@ -284,35 +289,21 @@ async function decode_api_responses(
284
289
  messages.getUint32(i * 12, true),
285
290
  messages.getUint32(i * 12 + 4, true),
286
291
  messages.getInt32(i * 12 + 8, true),
287
- ];
288
-
289
- const data = new Uint8Array(
290
- core.HEAPU8.buffer,
291
- Number(data_ptr),
292
- data_len,
293
- );
292
+ ],
293
+ );
294
+ }
294
295
 
295
- const resp = { client_id, data };
296
- await callback(resp);
296
+ try {
297
+ for (const [data_ptr, data_len, client_id] of descriptors) {
298
+ const data = new Uint8Array(core.HEAPU8.buffer, data_ptr, data_len);
299
+ await callback({ client_id, data });
297
300
  }
298
301
  } finally {
299
- for (let i = 0; i < num_msgs; i++) {
300
- const data_ptr = is_64
301
- ? messages.getBigInt64(i * 16, true)
302
- : messages.getInt32(i * 12, true);
303
-
304
- core._psp_free(data_ptr as any);
302
+ for (const [data_ptr] of descriptors) {
303
+ free(data_ptr);
305
304
  }
306
305
 
307
- core._psp_free(
308
- is_64
309
- ? (BigInt(messages.byteOffset) as any as number)
310
- : (messages.byteOffset as any),
311
- );
312
- core._psp_free(
313
- is_64
314
- ? (BigInt(response.byteOffset) as any as number)
315
- : (response.byteOffset as any),
316
- );
306
+ free(msgs_ptr);
307
+ free(response_ptr);
317
308
  }
318
309
  }