@perspective-dev/client 5.1.0 → 5.3.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 (41) hide show
  1. package/dist/cdn/perspective.js +2 -2
  2. package/dist/cdn/perspective.js.map +3 -3
  3. package/dist/esm/perspective.inline.js +2 -2
  4. package/dist/esm/perspective.inline.js.map +3 -3
  5. package/dist/esm/perspective.js +2 -2
  6. package/dist/esm/perspective.js.map +3 -3
  7. package/dist/esm/perspective.node.js +36 -22
  8. package/dist/esm/perspective.node.js.map +2 -2
  9. package/dist/esm/ts-rs/Features.d.ts +10 -3
  10. package/dist/esm/ts-rs/ListFlatten.d.ts +1 -0
  11. package/dist/esm/ts-rs/SplitRollupMode.d.ts +9 -0
  12. package/dist/esm/ts-rs/TableInitOptions.d.ts +9 -0
  13. package/dist/esm/ts-rs/ViewConfig.d.ts +2 -0
  14. package/dist/esm/ts-rs/ViewConfigUpdate.d.ts +15 -0
  15. package/dist/esm/ts-rs/WindowAggSpec.d.ts +25 -0
  16. package/dist/esm/ts-rs/WindowSpec.d.ts +3 -2
  17. package/dist/esm/virtual_servers/clickhouse.js +1 -1
  18. package/dist/esm/virtual_servers/clickhouse.js.map +3 -3
  19. package/dist/esm/virtual_servers/duckdb.js +1 -1
  20. package/dist/esm/virtual_servers/duckdb.js.map +3 -3
  21. package/dist/wasm/perspective-js.d.ts +9 -8
  22. package/dist/wasm/perspective-js.js +38 -24
  23. package/dist/wasm/perspective-js.wasm +0 -0
  24. package/dist/wasm/perspective-js.wasm.d.ts +7 -6
  25. package/package.json +1 -1
  26. package/src/rust/generic_sql_model.rs +8 -1
  27. package/src/rust/lib.rs +1 -1
  28. package/src/rust/typed_array.rs +22 -3
  29. package/src/rust/utils/futures.rs +95 -8
  30. package/src/ts/ts-rs/Features.ts +10 -3
  31. package/src/ts/ts-rs/ListFlatten.ts +3 -0
  32. package/src/ts/ts-rs/SplitRollupMode.ts +11 -0
  33. package/src/ts/ts-rs/TableInitOptions.ts +10 -1
  34. package/src/ts/ts-rs/ViewConfig.ts +2 -1
  35. package/src/ts/ts-rs/ViewConfigUpdate.ts +15 -1
  36. package/src/ts/ts-rs/WindowAggSpec.ts +25 -0
  37. package/src/ts/ts-rs/WindowSpec.ts +3 -2
  38. package/src/ts/virtual_servers/clickhouse.ts +67 -24
  39. package/src/ts/virtual_servers/duckdb.ts +135 -50
  40. package/dist/esm/ts-rs/WindowAggregate.d.ts +0 -1
  41. package/src/ts/ts-rs/WindowAggregate.ts +0 -3
@@ -26,7 +26,7 @@ import type { ColumnType } from "@perspective-dev/client/dist/esm/ts-rs/ColumnTy
26
26
  import type { ViewConfig } from "@perspective-dev/client/dist/esm/ts-rs/ViewConfig.d.ts";
27
27
  import type { ViewConfigUpdate } from "@perspective-dev/client/dist/esm/ts-rs/ViewConfigUpdate.d.ts";
28
28
  import type { ViewWindow } from "@perspective-dev/client/dist/esm/ts-rs/ViewWindow.d.ts";
29
- import type { WindowAggregate } from "@perspective-dev/client/dist/esm/ts-rs/WindowAggregate.d.ts";
29
+ import type { WindowAggSpec } from "@perspective-dev/client/dist/esm/ts-rs/WindowAggSpec.d.ts";
30
30
  import type * as clickhouse from "@clickhouse/client-web";
31
31
 
32
32
  const NUMBER_AGGS = [
@@ -65,40 +65,70 @@ const STRING_AGGS = [
65
65
  "string_agg",
66
66
  ];
67
67
 
68
- // The window aggregates the SQL translation supports, per source
69
- // column type (`ema` is recursive - no SQL window equivalent).
70
- const WINDOW_AGGREGATES: WindowAggregate[] = [
71
- "sum",
72
- "avg",
73
- "count",
74
- "min",
75
- "max",
76
- "stddev",
77
- "var",
78
- "lag",
79
- "lead",
80
- "diff",
81
- "rate",
68
+ // Window functions. Renamed from Perspective's `stddev`/`var` to the SQL
69
+ // standard spellings DuckDB and ClickHouse both accept, since the advertised
70
+ // name is now emitted verbatim.
71
+ //
72
+ // NOTE: this set is inherited from the DuckDB handler and has NOT been audited
73
+ // against a live ClickHouse - see the aggregate lists below, which have the
74
+ // same problem. ClickHouse's own navigation functions are `lagInFrame` /
75
+ // `leadInFrame`, and its ranking set differs; both need verifying before being
76
+ // advertised here.
77
+ const FRAMES = ["rows", "range", "cumulative"];
78
+
79
+ const WINDOW_AGGREGATES: WindowAggSpec[] = [
80
+ { name: "sum", frames: FRAMES, result_type: "float" },
81
+ { name: "avg", frames: FRAMES, result_type: "float" },
82
+ { name: "count", frames: FRAMES, result_type: "float" },
83
+ { name: "min", frames: FRAMES },
84
+ { name: "max", frames: FRAMES },
85
+ { name: "stddev_samp", frames: FRAMES, result_type: "float" },
86
+ { name: "var_samp", frames: FRAMES, result_type: "float" },
87
+ { name: "lag", offset: true },
88
+ { name: "lead", offset: true },
89
+ { name: "diff", offset: true, result_type: "float" },
90
+ { name: "rate", frames: ["range"], result_type: "float" },
82
91
  ];
83
92
 
84
- const WINDOW_AGGREGATES_ANY: WindowAggregate[] = [
85
- "count",
86
- "min",
87
- "max",
88
- "lag",
89
- "lead",
93
+ const WINDOW_AGGREGATES_ANY: WindowAggSpec[] = [
94
+ { name: "count", frames: FRAMES, result_type: "float" },
95
+ { name: "min", frames: FRAMES },
96
+ { name: "max", frames: FRAMES },
97
+ { name: "lag", offset: true },
98
+ { name: "lead", offset: true },
90
99
  ];
91
100
 
92
101
  const FILTER_OPS = [
93
102
  "==",
94
103
  "!=",
95
- "LIKE",
96
104
  "IS DISTINCT FROM",
97
105
  "IS NOT DISTINCT FROM",
98
106
  ">=",
99
107
  "<=",
100
108
  ">",
101
109
  "<",
110
+ "is null",
111
+ "is not null",
112
+ ];
113
+
114
+ // Perspective's canonical string ops (translated to `ILIKE` / `match` by the
115
+ // SQL builder), plus ClickHouse's raw infix pattern ops spliced verbatim.
116
+ const STRING_FILTER_OPS = [
117
+ ...FILTER_OPS,
118
+ "begins with",
119
+ "not begins with",
120
+ "contains",
121
+ "not contains",
122
+ "ends with",
123
+ "not ends with",
124
+ "matches",
125
+ "not matches",
126
+ "in",
127
+ "not in",
128
+ "LIKE",
129
+ "NOT LIKE",
130
+ "ILIKE",
131
+ "NOT ILIKE",
102
132
  ];
103
133
 
104
134
  function duckdbTypeToPsp(name: string): ColumnType {
@@ -241,6 +271,8 @@ export class ClickhouseHandler implements perspective.VirtualServerHandler {
241
271
  create_entity: "VIEW",
242
272
  grouping_fn: "GROUPING",
243
273
  column_separator: "|",
274
+ backslash_escaped_literals: true,
275
+ regex_fn: "match",
244
276
  });
245
277
  }
246
278
 
@@ -266,7 +298,7 @@ export class ClickhouseHandler implements perspective.VirtualServerHandler {
266
298
  filter_ops: {
267
299
  integer: FILTER_OPS,
268
300
  float: FILTER_OPS,
269
- string: FILTER_OPS,
301
+ string: STRING_FILTER_OPS,
270
302
  boolean: FILTER_OPS,
271
303
  date: FILTER_OPS,
272
304
  datetime: FILTER_OPS,
@@ -327,7 +359,18 @@ export class ClickhouseHandler implements perspective.VirtualServerHandler {
327
359
  viewId: string,
328
360
  config: ViewConfigUpdate,
329
361
  ) {
330
- const query = this.sqlBuilder.tableMakeView(tableId, viewId, config);
362
+ // Window order keys need column types for `range` frame emission.
363
+ const schema = Object.keys(config.windows ?? {}).length
364
+ ? await this.tableSchema(tableId)
365
+ : undefined;
366
+
367
+ const query = this.sqlBuilder.tableMakeView(
368
+ tableId,
369
+ viewId,
370
+ config,
371
+ schema,
372
+ );
373
+
331
374
  await runQuery(this.db, query, { execute: true });
332
375
  }
333
376
 
@@ -26,7 +26,7 @@ import type { ColumnType } from "@perspective-dev/client/dist/esm/ts-rs/ColumnTy
26
26
  import type { ViewConfig } from "@perspective-dev/client/dist/esm/ts-rs/ViewConfig.d.ts";
27
27
  import type { ViewConfigUpdate } from "@perspective-dev/client/dist/esm/ts-rs/ViewConfigUpdate.d.ts";
28
28
  import type { ViewWindow } from "@perspective-dev/client/dist/esm/ts-rs/ViewWindow.d.ts";
29
- import type { WindowAggregate } from "@perspective-dev/client/dist/esm/ts-rs/WindowAggregate.d.ts";
29
+ import type { WindowAggSpec } from "@perspective-dev/client/dist/esm/ts-rs/WindowAggSpec.d.ts";
30
30
  import type { Scalar } from "@perspective-dev/client/dist/esm/ts-rs/Scalar.d.ts";
31
31
  import type * as duckdb from "@duckdb/duckdb-wasm";
32
32
 
@@ -66,87 +66,158 @@ const STRING_AGGS = [
66
66
  "string_agg",
67
67
  ];
68
68
 
69
- // The window aggregates the SQL translation supports, per source
70
- // column type (`ema` is recursive - no SQL window equivalent).
71
- const WINDOW_AGGREGATES: WindowAggregate[] = [
72
- "sum",
73
- "avg",
74
- "count",
75
- "min",
76
- "max",
77
- "stddev",
78
- "var",
79
- "lag",
80
- "lead",
81
- "diff",
82
- "rate",
69
+ const FRAMES = ["rows", "range", "cumulative"];
70
+
71
+ const WINDOW_AGGREGATES: WindowAggSpec[] = [
72
+ { name: "sum", frames: FRAMES, result_type: "float" },
73
+ { name: "avg", frames: FRAMES, result_type: "float" },
74
+ { name: "count", frames: FRAMES, result_type: "float" },
75
+ { name: "min", frames: FRAMES },
76
+ { name: "max", frames: FRAMES },
77
+ { name: "product", frames: FRAMES, result_type: "float" },
78
+ { name: "median", frames: FRAMES, result_type: "float" },
79
+ // DuckDB spells sample and population variants separately, so both are
80
+ // offered rather than one being picked on the user's behalf.
81
+ { name: "stddev_samp", frames: FRAMES, result_type: "float" },
82
+ { name: "stddev_pop", frames: FRAMES, result_type: "float" },
83
+ { name: "var_samp", frames: FRAMES, result_type: "float" },
84
+ { name: "var_pop", frames: FRAMES, result_type: "float" },
85
+ // Navigation.
86
+ { name: "first_value", frames: FRAMES },
87
+ { name: "last_value", frames: FRAMES },
88
+ { name: "nth_value", frames: FRAMES, offset: true },
89
+ { name: "lag", offset: true },
90
+ { name: "lead", offset: true },
91
+ // Ranking. These take no source column - the window's `order_by` is their
92
+ // input - but Perspective requires one, so the choice of source is
93
+ // immaterial for them.
94
+ { name: "row_number", result_type: "float" },
95
+ { name: "rank", result_type: "float" },
96
+ { name: "dense_rank", result_type: "float" },
97
+ { name: "percent_rank", result_type: "float" },
98
+ { name: "cume_dist", result_type: "float" },
99
+ // `ntile`'s argument is a bucket count rather than a row offset.
100
+ { name: "ntile", offset: true, result_type: "float" },
101
+ // Perspective's own, with no DuckDB equivalent - the SQL translation
102
+ // synthesizes them from `lag` and `first_value`.
103
+ { name: "diff", offset: true, result_type: "float" },
104
+ { name: "rate", frames: ["range"], result_type: "float" },
83
105
  ];
84
106
 
85
- const WINDOW_AGGREGATES_ANY: WindowAggregate[] = [
86
- "count",
87
- "min",
88
- "max",
89
- "lag",
90
- "lead",
107
+ // Arithmetic is undefined for the non-numeric types; ordering and navigation
108
+ // are not.
109
+ const WINDOW_AGGREGATES_ANY: WindowAggSpec[] = [
110
+ { name: "count", frames: FRAMES, result_type: "float" },
111
+ { name: "min", frames: FRAMES },
112
+ { name: "max", frames: FRAMES },
113
+ { name: "first_value", frames: FRAMES },
114
+ { name: "last_value", frames: FRAMES },
115
+ { name: "nth_value", frames: FRAMES, offset: true },
116
+ { name: "lag", offset: true },
117
+ { name: "lead", offset: true },
118
+ { name: "row_number", result_type: "float" },
119
+ { name: "rank", result_type: "float" },
120
+ { name: "dense_rank", result_type: "float" },
121
+ { name: "percent_rank", result_type: "float" },
122
+ { name: "cume_dist", result_type: "float" },
123
+ { name: "ntile", offset: true, result_type: "float" },
91
124
  ];
92
125
 
93
126
  const FILTER_OPS = [
94
127
  "==",
95
128
  "!=",
96
- "LIKE",
97
129
  "IS DISTINCT FROM",
98
130
  "IS NOT DISTINCT FROM",
99
131
  ">=",
100
132
  "<=",
101
133
  ">",
102
134
  "<",
135
+ "is null",
136
+ "is not null",
137
+ ];
138
+
139
+ // Perspective's canonical string ops (translated to `ILIKE` / `regexp_matches`
140
+ // by the SQL builder), plus DuckDB's raw infix pattern ops spliced verbatim.
141
+ const STRING_FILTER_OPS = [
142
+ ...FILTER_OPS,
143
+ "begins with",
144
+ "not begins with",
145
+ "contains",
146
+ "not contains",
147
+ "ends with",
148
+ "not ends with",
149
+ "matches",
150
+ "not matches",
151
+ "in",
152
+ "not in",
153
+ "LIKE",
154
+ "NOT LIKE",
155
+ "ILIKE",
156
+ "NOT ILIKE",
103
157
  ];
104
158
 
159
+ /**
160
+ * Convert a DuckDB `dtype` to a Perspective `ColumnType`.
161
+ */
105
162
  function duckdbTypeToPsp(name: string): ColumnType {
106
163
  name = name.toLowerCase();
107
- if (name === "varchar" || name == "utf8") {
108
- return "string";
164
+
165
+ if (name.startsWith("bool")) {
166
+ return "boolean";
109
167
  }
110
168
 
169
+ // 32-bit and narrower - `coerce_column` widens these to `Int32`.
111
170
  if (
112
- name === "double" ||
113
- name === "bigint" ||
114
- name === "hugeint" ||
115
- name === "float64" ||
116
- name.startsWith("decimal")
171
+ ["tinyint", "smallint", "integer", "utinyint", "usmallint"].includes(
172
+ name,
173
+ ) ||
174
+ ["int8", "int16", "int32", "uint8", "uint16"].includes(name)
117
175
  ) {
118
- return "float";
176
+ return "integer";
119
177
  }
120
178
 
121
- if (name.startsWith("int")) {
122
- return "integer";
179
+ // Wider than `Int32`, or fractional - all coerce to `Float64`.
180
+ if (
181
+ ["bigint", "hugeint", "uhugeint", "uinteger", "ubigint"].includes(
182
+ name,
183
+ ) ||
184
+ ["float", "real", "double", "varint"].includes(name) ||
185
+ ["int64", "uint32", "uint64", "float32", "float64"].includes(name) ||
186
+ name.startsWith("decimal") ||
187
+ name.startsWith("numeric")
188
+ ) {
189
+ return "float";
123
190
  }
124
191
 
125
192
  if (name.startsWith("date")) {
126
193
  return "date";
127
194
  }
128
195
 
129
- if (name.startsWith("bool")) {
130
- return "boolean";
131
- }
132
-
133
- if (name.startsWith("timestamp")) {
196
+ // `timestamp`, `timestamptz`, `timestamp_ns`, and `time`/`timetz`,
197
+ // which coerce to `Timestamp(Millisecond)` rather than to a number.
198
+ if (name.startsWith("time")) {
134
199
  return "datetime";
135
200
  }
136
201
 
137
- if (name.startsWith("json")) {
138
- return "string";
139
- }
140
-
141
- if (name.startsWith("struct")) {
142
- return "string";
143
- }
144
-
145
- if (name.startsWith("time")) {
146
- return "float";
202
+ // Everything else renders as text: `varchar`, `enum(...)` (which
203
+ // arrives dictionary-encoded), `json`, `uuid`, `blob`, `interval`,
204
+ // and the nested types.
205
+ if (
206
+ !(
207
+ name.startsWith("varchar") ||
208
+ name === "utf8" ||
209
+ name.startsWith("enum") ||
210
+ ["json", "uuid", "blob", "bit", "interval"].includes(name) ||
211
+ name.startsWith("struct") ||
212
+ name.startsWith("map") ||
213
+ name.startsWith("union") ||
214
+ name.endsWith("[]")
215
+ )
216
+ ) {
217
+ // Unknown, not fatal - the column still renders, as text.
218
+ console.warn(`Unknown type '${name}'`);
147
219
  }
148
220
 
149
- console.warn(`Unknown type '${name}'`);
150
221
  return "string";
151
222
  }
152
223
 
@@ -213,6 +284,8 @@ export class DuckDBHandler implements perspective.VirtualServerHandler {
213
284
  this.db = db;
214
285
  this.sqlBuilder = new mod!.GenericSQLVirtualServerModel({
215
286
  column_separator: "|",
287
+ like_escape_clause: "\\",
288
+ regex_fn: "regexp_matches",
216
289
  });
217
290
  }
218
291
 
@@ -232,10 +305,11 @@ export class DuckDBHandler implements perspective.VirtualServerHandler {
232
305
  boolean: WINDOW_AGGREGATES_ANY,
233
306
  },
234
307
  group_rollup_mode: ["rollup", "flat", "total"],
308
+ split_rollup_mode: ["flat", "rollup"],
235
309
  filter_ops: {
236
310
  integer: FILTER_OPS,
237
311
  float: FILTER_OPS,
238
- string: FILTER_OPS,
312
+ string: STRING_FILTER_OPS,
239
313
  boolean: FILTER_OPS,
240
314
  date: FILTER_OPS,
241
315
  datetime: FILTER_OPS,
@@ -297,7 +371,18 @@ export class DuckDBHandler implements perspective.VirtualServerHandler {
297
371
  viewId: string,
298
372
  config: ViewConfigUpdate,
299
373
  ) {
300
- const query = this.sqlBuilder.tableMakeView(tableId, viewId, config);
374
+ // Window order keys need column types for `range` frame emission.
375
+ const schema = Object.keys(config.windows ?? {}).length
376
+ ? await this.tableSchema(tableId)
377
+ : undefined;
378
+
379
+ const query = this.sqlBuilder.tableMakeView(
380
+ tableId,
381
+ viewId,
382
+ config,
383
+ schema,
384
+ );
385
+
301
386
  await runQuery(this.db, query);
302
387
  }
303
388
 
@@ -1 +0,0 @@
1
- export type WindowAggregate = "sum" | "avg" | "count" | "min" | "max" | "stddev" | "var" | "first" | "last" | "lag" | "lead" | "diff" | "rate" | "ema";
@@ -1,3 +0,0 @@
1
- // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
2
-
3
- export type WindowAggregate = "sum" | "avg" | "count" | "min" | "max" | "stddev" | "var" | "first" | "last" | "lag" | "lead" | "diff" | "rate" | "ema";