@perspective-dev/client 5.6.1 → 5.8.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.
- package/README.md +5 -1
- package/dist/cdn/perspective.js +3 -2
- package/dist/cdn/perspective.js.map +4 -4
- package/dist/esm/perspective.browser.d.ts +71 -17
- package/dist/esm/perspective.js +2 -2
- package/dist/esm/perspective.js.map +4 -4
- package/dist/esm/perspective.node.d.ts +20 -3
- package/dist/esm/perspective.node.js +734 -39
- package/dist/esm/perspective.node.js.map +4 -4
- package/dist/esm/public_types.d.ts +31 -0
- package/dist/esm/virtual_servers/clickhouse.d.ts +41 -0
- package/dist/esm/virtual_servers/clickhouse.js +6 -1
- package/dist/esm/virtual_servers/clickhouse.js.map +3 -3
- package/dist/esm/virtual_servers/duckdb.d.ts +19 -9
- package/dist/esm/virtual_servers/duckdb.js +1 -1
- package/dist/esm/virtual_servers/duckdb.js.map +3 -3
- package/dist/esm/wasm/cdn.d.ts +12 -0
- package/dist/esm/wasm/migrate.d.ts +40 -0
- package/dist/esm/wasm/registry.d.ts +123 -0
- package/dist/esm/wasm/registry.js +3 -0
- package/dist/esm/wasm/registry.js.map +7 -0
- package/{src/ts/ts-rs/AggSpec.ts → dist/ts-rs/AggSpec.d.ts} +1 -1
- package/{src/ts/ts-rs/Features.ts → dist/ts-rs/Features.d.ts} +16 -6
- package/{src/ts/ts-rs/Filter.ts → dist/ts-rs/Filter.d.ts} +1 -1
- package/{src/ts/ts-rs/FilterTerm.ts → dist/ts-rs/FilterTerm.d.ts} +1 -1
- package/{src/ts/ts-rs/JoinOptions.ts → dist/ts-rs/JoinOptions.d.ts} +1 -1
- package/dist/ts-rs/ListFlatten.d.ts +3 -0
- package/dist/ts-rs/MapFlatten.d.ts +3 -0
- package/{src/ts/ts-rs/OnRemoveData.ts → dist/ts-rs/OnRemoveData.d.ts} +1 -1
- package/{src/ts/ts-rs/OnUpdateData.ts → dist/ts-rs/OnUpdateData.d.ts} +1 -1
- package/{src/ts/ts-rs/OnUpdateOptions.ts → dist/ts-rs/OnUpdateOptions.d.ts} +1 -1
- package/{src/ts/ts-rs/Sort.ts → dist/ts-rs/Sort.d.ts} +1 -1
- package/dist/ts-rs/StructFlatten.d.ts +3 -0
- package/dist/ts-rs/TableIndex.d.ts +9 -0
- package/dist/ts-rs/TableInitOptions.d.ts +65 -0
- package/{src/ts/ts-rs/UpdateOptions.ts → dist/ts-rs/UpdateOptions.d.ts} +1 -1
- package/{src/ts/ts-rs/ViewConfig.ts → dist/ts-rs/ViewConfig.d.ts} +8 -8
- package/{src/ts/ts-rs/ViewConfigUpdate.ts → dist/ts-rs/ViewConfigUpdate.d.ts} +8 -8
- package/{src/ts/ts-rs/ViewOnUpdateResp.ts → dist/ts-rs/ViewOnUpdateResp.d.ts} +1 -1
- package/{src/ts/ts-rs/WindowAggSpec.ts → dist/ts-rs/WindowAggSpec.d.ts} +1 -1
- package/{src/ts/ts-rs/WindowSort.ts → dist/ts-rs/WindowSort.d.ts} +1 -1
- package/{src/ts/ts-rs/WindowSpec.ts → dist/ts-rs/WindowSpec.d.ts} +1 -1
- package/{src/ts/ts-rs/Windows.ts → dist/ts-rs/Windows.d.ts} +1 -1
- package/dist/wasm/perspective-js.d.ts +136 -58
- package/dist/wasm/perspective-js.js +190 -24
- package/dist/wasm/perspective-js.wasm +0 -0
- package/dist/wasm/perspective-js.wasm.d.ts +14 -8
- package/dist/wasm/perspective-migrate.d.ts +41 -0
- package/dist/wasm/perspective-migrate.js +524 -0
- package/dist/wasm/perspective-migrate.wasm +0 -0
- package/dist/wasm/perspective-migrate.wasm.d.ts +8 -0
- package/package.json +30 -15
- package/dist/esm/ts-rs/AggSpec.d.ts +0 -8
- package/dist/esm/ts-rs/Aggregate.d.ts +0 -1
- package/dist/esm/ts-rs/ColumnType.d.ts +0 -4
- package/dist/esm/ts-rs/ColumnWindow.d.ts +0 -4
- package/dist/esm/ts-rs/DeleteOptions.d.ts +0 -6
- package/dist/esm/ts-rs/Expressions.d.ts +0 -3
- package/dist/esm/ts-rs/Features.d.ts +0 -71
- package/dist/esm/ts-rs/Filter.d.ts +0 -2
- package/dist/esm/ts-rs/FilterReducer.d.ts +0 -1
- package/dist/esm/ts-rs/FilterTerm.d.ts +0 -2
- package/dist/esm/ts-rs/GroupRollupMode.d.ts +0 -1
- package/dist/esm/ts-rs/JoinOptions.d.ts +0 -9
- package/dist/esm/ts-rs/JoinType.d.ts +0 -1
- package/dist/esm/ts-rs/ListFlatten.d.ts +0 -1
- package/dist/esm/ts-rs/OnRemoveData.d.ts +0 -6
- package/dist/esm/ts-rs/OnUpdateData.d.ts +0 -6
- package/dist/esm/ts-rs/OnUpdateMode.d.ts +0 -9
- package/dist/esm/ts-rs/OnUpdateOptions.d.ts +0 -7
- package/dist/esm/ts-rs/Scalar.d.ts +0 -5
- package/dist/esm/ts-rs/Sort.d.ts +0 -2
- package/dist/esm/ts-rs/SortDir.d.ts +0 -1
- package/dist/esm/ts-rs/SplitRollupMode.d.ts +0 -9
- package/dist/esm/ts-rs/SystemInfo.d.ts +0 -40
- package/dist/esm/ts-rs/TableInitOptions.d.ts +0 -45
- package/dist/esm/ts-rs/TableReadFormat.d.ts +0 -7
- package/dist/esm/ts-rs/TypedArrayWindow.d.ts +0 -33
- package/dist/esm/ts-rs/UpdateOptions.d.ts +0 -8
- package/dist/esm/ts-rs/ViewConfig.d.ts +0 -24
- package/dist/esm/ts-rs/ViewConfigUpdate.d.ts +0 -116
- package/dist/esm/ts-rs/ViewOnRemoveResp.d.ts +0 -4
- package/dist/esm/ts-rs/ViewOnUpdateResp.d.ts +0 -4
- package/dist/esm/ts-rs/ViewWindow.d.ts +0 -30
- package/dist/esm/ts-rs/WindowAggSpec.d.ts +0 -25
- package/dist/esm/ts-rs/WindowSort.d.ts +0 -7
- package/dist/esm/ts-rs/WindowSortDir.d.ts +0 -6
- package/dist/esm/ts-rs/WindowSpec.d.ts +0 -47
- package/dist/esm/ts-rs/Windows.d.ts +0 -10
- package/src/rust/client.rs +0 -590
- package/src/rust/generic_sql_model.rs +0 -276
- package/src/rust/lib.rs +0 -99
- package/src/rust/table.rs +0 -397
- package/src/rust/table_data.rs +0 -164
- package/src/rust/typed_array.rs +0 -386
- package/src/rust/utils/browser.rs +0 -35
- package/src/rust/utils/console_logger.rs +0 -237
- package/src/rust/utils/errors.rs +0 -274
- package/src/rust/utils/futures.rs +0 -274
- package/src/rust/utils/json.rs +0 -252
- package/src/rust/utils/local_poll_loop.rs +0 -63
- package/src/rust/utils/mod.rs +0 -32
- package/src/rust/utils/serde.rs +0 -46
- package/src/rust/utils/trace_allocator.rs +0 -98
- package/src/rust/view.rs +0 -463
- package/src/rust/virtual_server.rs +0 -991
- package/src/ts/perspective-server.worker.ts +0 -74
- package/src/ts/perspective.browser.ts +0 -380
- package/src/ts/perspective.cdn.ts +0 -28
- package/src/ts/perspective.node.ts +0 -556
- package/src/ts/ts-rs/ListFlatten.ts +0 -3
- package/src/ts/ts-rs/TableInitOptions.ts +0 -42
- package/src/ts/virtual_server.ts +0 -83
- package/src/ts/virtual_servers/clickhouse.ts +0 -533
- package/src/ts/virtual_servers/duckdb.ts +0 -549
- package/src/ts/wasm/browser.ts +0 -134
- package/src/ts/wasm/cdn.ts +0 -32
- package/src/ts/wasm/decompress.ts +0 -83
- package/src/ts/wasm/emscripten_api.ts +0 -91
- package/src/ts/wasm/engine.ts +0 -309
- package/src/ts/wasm/features.ts +0 -31
- package/src/ts/wasm/perspective-server.poly.ts +0 -460
- package/src/ts/websocket.ts +0 -95
- package/tsconfig.json +0 -22
- /package/{src/ts/ts-rs/Aggregate.ts → dist/ts-rs/Aggregate.d.ts} +0 -0
- /package/{src/ts/ts-rs/ColumnType.ts → dist/ts-rs/ColumnType.d.ts} +0 -0
- /package/{src/ts/ts-rs/ColumnWindow.ts → dist/ts-rs/ColumnWindow.d.ts} +0 -0
- /package/{src/ts/ts-rs/DeleteOptions.ts → dist/ts-rs/DeleteOptions.d.ts} +0 -0
- /package/{src/ts/ts-rs/Expressions.ts → dist/ts-rs/Expressions.d.ts} +0 -0
- /package/{src/ts/ts-rs/FilterReducer.ts → dist/ts-rs/FilterReducer.d.ts} +0 -0
- /package/{src/ts/ts-rs/GroupRollupMode.ts → dist/ts-rs/GroupRollupMode.d.ts} +0 -0
- /package/{src/ts/ts-rs/JoinType.ts → dist/ts-rs/JoinType.d.ts} +0 -0
- /package/{src/ts/ts-rs/OnUpdateMode.ts → dist/ts-rs/OnUpdateMode.d.ts} +0 -0
- /package/{src/ts/ts-rs/Scalar.ts → dist/ts-rs/Scalar.d.ts} +0 -0
- /package/{src/ts/ts-rs/SortDir.ts → dist/ts-rs/SortDir.d.ts} +0 -0
- /package/{src/ts/ts-rs/SplitRollupMode.ts → dist/ts-rs/SplitRollupMode.d.ts} +0 -0
- /package/{src/ts/ts-rs/SystemInfo.ts → dist/ts-rs/SystemInfo.d.ts} +0 -0
- /package/{src/ts/ts-rs/TableReadFormat.ts → dist/ts-rs/TableReadFormat.d.ts} +0 -0
- /package/{src/ts/ts-rs/TypedArrayWindow.ts → dist/ts-rs/TypedArrayWindow.d.ts} +0 -0
- /package/{src/ts/ts-rs/ViewOnRemoveResp.ts → dist/ts-rs/ViewOnRemoveResp.d.ts} +0 -0
- /package/{src/ts/ts-rs/ViewWindow.ts → dist/ts-rs/ViewWindow.d.ts} +0 -0
- /package/{src/ts/ts-rs/WindowSortDir.ts → dist/ts-rs/WindowSortDir.d.ts} +0 -0
|
@@ -1,533 +0,0 @@
|
|
|
1
|
-
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
|
|
2
|
-
// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
|
|
3
|
-
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
|
|
4
|
-
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
|
|
5
|
-
// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
|
|
6
|
-
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
|
|
7
|
-
// ┃ Copyright (c) 2017, the Perspective Authors. ┃
|
|
8
|
-
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
|
|
9
|
-
// ┃ This file is part of the Perspective library, distributed under the terms ┃
|
|
10
|
-
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
|
|
11
|
-
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* An implementation of a Perspective Virtual Server for DuckDB.
|
|
15
|
-
*
|
|
16
|
-
* This import is optional, and so must be imported manually from either
|
|
17
|
-
* `@perspective-dev/client/dist/esm/virtual_servers/duckdb.js` or
|
|
18
|
-
* `@perspective-dev/client/src/ts/virtual_servers/duckdb.ts`, it is not
|
|
19
|
-
* exported from the package root `@perspective-dev/client`
|
|
20
|
-
*
|
|
21
|
-
* @module
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
import type * as perspective from "@perspective-dev/client";
|
|
25
|
-
import type { ColumnType } from "@perspective-dev/client/dist/esm/ts-rs/ColumnType.d.ts";
|
|
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";
|
|
31
|
-
import type { ViewConfigUpdate } from "@perspective-dev/client/dist/esm/ts-rs/ViewConfigUpdate.d.ts";
|
|
32
|
-
import type { ViewWindow } from "@perspective-dev/client/dist/esm/ts-rs/ViewWindow.d.ts";
|
|
33
|
-
import type { WindowAggSpec } from "@perspective-dev/client/dist/esm/ts-rs/WindowAggSpec.d.ts";
|
|
34
|
-
import type * as clickhouse from "@clickhouse/client-web";
|
|
35
|
-
|
|
36
|
-
const NUMBER_AGGS = [
|
|
37
|
-
"sum",
|
|
38
|
-
"count",
|
|
39
|
-
"any_value",
|
|
40
|
-
"arbitrary",
|
|
41
|
-
"array_agg",
|
|
42
|
-
"avg",
|
|
43
|
-
"bit_and",
|
|
44
|
-
"bit_or",
|
|
45
|
-
"bit_xor",
|
|
46
|
-
"bitstring_agg",
|
|
47
|
-
"bool_and",
|
|
48
|
-
"bool_or",
|
|
49
|
-
"countif",
|
|
50
|
-
"favg",
|
|
51
|
-
"fsum",
|
|
52
|
-
"geomean",
|
|
53
|
-
"kahan_sum",
|
|
54
|
-
"last",
|
|
55
|
-
"max",
|
|
56
|
-
"min",
|
|
57
|
-
"product",
|
|
58
|
-
"string_agg",
|
|
59
|
-
"sumkahan",
|
|
60
|
-
];
|
|
61
|
-
|
|
62
|
-
const STRING_AGGS = [
|
|
63
|
-
"count",
|
|
64
|
-
"any_value",
|
|
65
|
-
"arbitrary",
|
|
66
|
-
"first",
|
|
67
|
-
"countif",
|
|
68
|
-
"last",
|
|
69
|
-
"string_agg",
|
|
70
|
-
];
|
|
71
|
-
|
|
72
|
-
// Window functions. Renamed from Perspective's `stddev`/`var` to the SQL
|
|
73
|
-
// standard spellings DuckDB and ClickHouse both accept, since the advertised
|
|
74
|
-
// name is now emitted verbatim.
|
|
75
|
-
//
|
|
76
|
-
// NOTE: this set is inherited from the DuckDB handler and has NOT been audited
|
|
77
|
-
// against a live ClickHouse - see the aggregate lists below, which have the
|
|
78
|
-
// same problem. ClickHouse's own navigation functions are `lagInFrame` /
|
|
79
|
-
// `leadInFrame`, and its ranking set differs; both need verifying before being
|
|
80
|
-
// advertised here.
|
|
81
|
-
const FRAMES = ["rows", "range", "cumulative"];
|
|
82
|
-
|
|
83
|
-
const WINDOW_AGGREGATES: WindowAggSpec[] = [
|
|
84
|
-
{ name: "sum", frames: FRAMES, result_type: "float" },
|
|
85
|
-
{ name: "avg", frames: FRAMES, result_type: "float" },
|
|
86
|
-
{ name: "count", frames: FRAMES, result_type: "float" },
|
|
87
|
-
{ name: "min", frames: FRAMES },
|
|
88
|
-
{ name: "max", frames: FRAMES },
|
|
89
|
-
{ name: "stddev_samp", frames: FRAMES, result_type: "float" },
|
|
90
|
-
{ name: "var_samp", frames: FRAMES, result_type: "float" },
|
|
91
|
-
{ name: "lag", offset: true },
|
|
92
|
-
{ name: "lead", offset: true },
|
|
93
|
-
{ name: "diff", offset: true, result_type: "float" },
|
|
94
|
-
{ name: "rate", frames: ["range"], result_type: "float" },
|
|
95
|
-
{ name: "row_number", result_type: "float" },
|
|
96
|
-
];
|
|
97
|
-
|
|
98
|
-
const WINDOW_AGGREGATES_ANY: WindowAggSpec[] = [
|
|
99
|
-
{ name: "count", frames: FRAMES, result_type: "float" },
|
|
100
|
-
{ name: "min", frames: FRAMES },
|
|
101
|
-
{ name: "max", frames: FRAMES },
|
|
102
|
-
{ name: "lag", offset: true },
|
|
103
|
-
{ name: "lead", offset: true },
|
|
104
|
-
{ name: "row_number", result_type: "float" },
|
|
105
|
-
];
|
|
106
|
-
|
|
107
|
-
const FILTER_OPS = [
|
|
108
|
-
"==",
|
|
109
|
-
"!=",
|
|
110
|
-
"IS DISTINCT FROM",
|
|
111
|
-
"IS NOT DISTINCT FROM",
|
|
112
|
-
">=",
|
|
113
|
-
"<=",
|
|
114
|
-
">",
|
|
115
|
-
"<",
|
|
116
|
-
"is null",
|
|
117
|
-
"is not null",
|
|
118
|
-
];
|
|
119
|
-
|
|
120
|
-
// Perspective's canonical string ops (translated to `ILIKE` / `match` by the
|
|
121
|
-
// SQL builder), plus ClickHouse's raw infix pattern ops spliced verbatim.
|
|
122
|
-
const STRING_FILTER_OPS = [
|
|
123
|
-
...FILTER_OPS,
|
|
124
|
-
"begins with",
|
|
125
|
-
"not begins with",
|
|
126
|
-
"contains",
|
|
127
|
-
"not contains",
|
|
128
|
-
"ends with",
|
|
129
|
-
"not ends with",
|
|
130
|
-
"matches",
|
|
131
|
-
"not matches",
|
|
132
|
-
"in",
|
|
133
|
-
"not in",
|
|
134
|
-
"LIKE",
|
|
135
|
-
"NOT LIKE",
|
|
136
|
-
"ILIKE",
|
|
137
|
-
"NOT ILIKE",
|
|
138
|
-
];
|
|
139
|
-
|
|
140
|
-
function duckdbTypeToPsp(name: string): ColumnType {
|
|
141
|
-
if (name.startsWith("Nullable")) {
|
|
142
|
-
name = name.match(/Nullable\((.+?)\)/)![1];
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (name.startsWith("Array")) {
|
|
146
|
-
return "string";
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (name === "Int64" || name === "UInt64" || name === "Float64") {
|
|
150
|
-
return "float";
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
if (name === "String") {
|
|
154
|
-
return "string";
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
if (name === "DateTime") {
|
|
158
|
-
return "datetime";
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if (name === "Date") {
|
|
162
|
-
return "date";
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
throw new Error(`Unknown type '${name}'`);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function convertDecimalToNumber(value: any, dtypeString: string) {
|
|
169
|
-
if (!(value instanceof Uint32Array || value instanceof Int32Array)) {
|
|
170
|
-
return value;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
let bigIntValue = BigInt(0);
|
|
174
|
-
for (let i = 0; i < value.length; i++) {
|
|
175
|
-
bigIntValue |= BigInt(value[i]) << BigInt(i * 32);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const scaleMatch = dtypeString.match(/Decimal\[\d+e(\d+)\]/);
|
|
179
|
-
if (scaleMatch) {
|
|
180
|
-
const scale = parseInt(scaleMatch[1]);
|
|
181
|
-
return Number(bigIntValue) / Math.pow(10, scale);
|
|
182
|
-
} else {
|
|
183
|
-
return Number(bigIntValue);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
class Lock {
|
|
188
|
-
lockPromise: Promise<void>;
|
|
189
|
-
constructor() {
|
|
190
|
-
this.lockPromise = Promise.resolve();
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
acquire() {
|
|
194
|
-
let releaseLock: (value: void) => void;
|
|
195
|
-
const newLockPromise: Promise<void> = new Promise((resolve) => {
|
|
196
|
-
releaseLock = resolve;
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
const acquirePromise = this.lockPromise.then(() => releaseLock);
|
|
200
|
-
this.lockPromise = newLockPromise;
|
|
201
|
-
return acquirePromise;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
const LOCK = new Lock();
|
|
206
|
-
|
|
207
|
-
async function runQuery(
|
|
208
|
-
db: clickhouse.ClickHouseClient,
|
|
209
|
-
query: string,
|
|
210
|
-
options: { columns?: true; execute?: boolean },
|
|
211
|
-
): Promise<{
|
|
212
|
-
rows: any[];
|
|
213
|
-
columns: string[];
|
|
214
|
-
dtypes: string[];
|
|
215
|
-
}>;
|
|
216
|
-
|
|
217
|
-
async function runQuery(
|
|
218
|
-
db: clickhouse.ClickHouseClient,
|
|
219
|
-
query: string,
|
|
220
|
-
options?: { columns?: false; execute?: boolean },
|
|
221
|
-
): Promise<any[]>;
|
|
222
|
-
|
|
223
|
-
async function runQuery(
|
|
224
|
-
db: clickhouse.ClickHouseClient,
|
|
225
|
-
query: string,
|
|
226
|
-
options: { columns?: boolean; execute?: boolean } = {},
|
|
227
|
-
) {
|
|
228
|
-
query = query.replace(/\s+/g, " ").trim();
|
|
229
|
-
const release = await LOCK.acquire();
|
|
230
|
-
try {
|
|
231
|
-
const result = await db.query({ query });
|
|
232
|
-
if (!options.execute) {
|
|
233
|
-
const { data, meta } =
|
|
234
|
-
(await result.json()) as clickhouse.ResponseJSON<unknown>;
|
|
235
|
-
|
|
236
|
-
if (options.columns) {
|
|
237
|
-
return {
|
|
238
|
-
rows: data,
|
|
239
|
-
columns: meta!.map((f) => f.name),
|
|
240
|
-
dtypes: meta!.map((f) => f.type),
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
return data;
|
|
245
|
-
}
|
|
246
|
-
} catch (error) {
|
|
247
|
-
console.error("Query error:", error);
|
|
248
|
-
console.error("Query:", query);
|
|
249
|
-
throw error;
|
|
250
|
-
} finally {
|
|
251
|
-
release();
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* An implementation of Perspective's Virtual Server for `@duckdb/duckdb-wasm`.
|
|
257
|
-
*/
|
|
258
|
-
export class ClickhouseHandler implements perspective.VirtualServerHandler {
|
|
259
|
-
private db: clickhouse.ClickHouseClient;
|
|
260
|
-
private sqlBuilder: perspective.GenericSQLVirtualServerModel;
|
|
261
|
-
constructor(db: clickhouse.ClickHouseClient, mod?: typeof perspective) {
|
|
262
|
-
if (!mod) {
|
|
263
|
-
if (customElements) {
|
|
264
|
-
const viewer_class: any =
|
|
265
|
-
customElements.get("perspective-viewer");
|
|
266
|
-
if (viewer_class) {
|
|
267
|
-
mod = viewer_class.__wasm_module__;
|
|
268
|
-
} else {
|
|
269
|
-
throw new Error("Missing perspective-client.wasm");
|
|
270
|
-
}
|
|
271
|
-
} else {
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
this.db = db;
|
|
276
|
-
this.sqlBuilder = new mod!.GenericSQLVirtualServerModel({
|
|
277
|
-
create_entity: "VIEW",
|
|
278
|
-
grouping_fn: "GROUPING",
|
|
279
|
-
column_separator: "|",
|
|
280
|
-
backslash_escaped_literals: true,
|
|
281
|
-
regex_fn: "match",
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
getFeatures(): perspective.Features {
|
|
286
|
-
return {
|
|
287
|
-
group_by: true,
|
|
288
|
-
split_by: false,
|
|
289
|
-
sort: true,
|
|
290
|
-
expressions: true,
|
|
291
|
-
// ClickHouse has no stable `rowid`, so natural-order windows
|
|
292
|
-
// are unsupported.
|
|
293
|
-
unordered: true,
|
|
294
|
-
window_aggregates: {
|
|
295
|
-
// `ema` is recursive and has no SQL window translation.
|
|
296
|
-
integer: WINDOW_AGGREGATES,
|
|
297
|
-
float: WINDOW_AGGREGATES,
|
|
298
|
-
string: WINDOW_AGGREGATES_ANY,
|
|
299
|
-
date: WINDOW_AGGREGATES_ANY,
|
|
300
|
-
datetime: WINDOW_AGGREGATES_ANY,
|
|
301
|
-
boolean: WINDOW_AGGREGATES_ANY,
|
|
302
|
-
},
|
|
303
|
-
group_rollup_mode: ["rollup", "flat", "total"],
|
|
304
|
-
filter_ops: {
|
|
305
|
-
integer: FILTER_OPS,
|
|
306
|
-
float: FILTER_OPS,
|
|
307
|
-
string: STRING_FILTER_OPS,
|
|
308
|
-
boolean: FILTER_OPS,
|
|
309
|
-
date: FILTER_OPS,
|
|
310
|
-
datetime: FILTER_OPS,
|
|
311
|
-
},
|
|
312
|
-
aggregates: {
|
|
313
|
-
integer: NUMBER_AGGS,
|
|
314
|
-
float: NUMBER_AGGS,
|
|
315
|
-
string: STRING_AGGS,
|
|
316
|
-
boolean: STRING_AGGS,
|
|
317
|
-
date: STRING_AGGS,
|
|
318
|
-
datetime: STRING_AGGS,
|
|
319
|
-
},
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
async getHostedTables() {
|
|
324
|
-
const query = "SHOW TABLES";
|
|
325
|
-
const results = await runQuery(this.db, query);
|
|
326
|
-
return results.map((row) => {
|
|
327
|
-
return `${row.name}`;
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
async tableSchema(tableId: string, config?: ViewConfig) {
|
|
332
|
-
const query = this.sqlBuilder.tableSchema(tableId);
|
|
333
|
-
const results = await runQuery(this.db, query);
|
|
334
|
-
const schema = {} as Record<string, ColumnType>;
|
|
335
|
-
for (const result of results) {
|
|
336
|
-
const colName = result.name;
|
|
337
|
-
if (!colName.startsWith("__")) {
|
|
338
|
-
schema[colName] = duckdbTypeToPsp(result.type) as ColumnType;
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
return schema;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
async viewColumnSize(viewId: string, config: ViewConfig) {
|
|
346
|
-
const query = `SELECT COUNT() FROM system.columns WHERE table = '${viewId}'`;
|
|
347
|
-
const results = await runQuery(this.db, query);
|
|
348
|
-
const gs = config.group_by?.length || 0;
|
|
349
|
-
const count = Number(results[0]["COUNT()"]);
|
|
350
|
-
console.log(count);
|
|
351
|
-
return (
|
|
352
|
-
count -
|
|
353
|
-
(gs === 0 ? 0 : gs + (config.split_by?.length === 0 ? 1 : 0))
|
|
354
|
-
);
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
async tableSize(tableId: string) {
|
|
358
|
-
const query = this.sqlBuilder.tableSize(tableId);
|
|
359
|
-
const results = await runQuery(this.db, query);
|
|
360
|
-
return Number(results[0]["COUNT()"]);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
async tableMakeView(
|
|
364
|
-
tableId: string,
|
|
365
|
-
viewId: string,
|
|
366
|
-
config: ViewConfigUpdate,
|
|
367
|
-
) {
|
|
368
|
-
// Window order keys need column types for `range` frame emission.
|
|
369
|
-
const schema = Object.keys(config.windows ?? {}).length
|
|
370
|
-
? await this.tableSchema(tableId)
|
|
371
|
-
: undefined;
|
|
372
|
-
|
|
373
|
-
const query = this.sqlBuilder.tableMakeView(
|
|
374
|
-
tableId,
|
|
375
|
-
viewId,
|
|
376
|
-
config,
|
|
377
|
-
schema,
|
|
378
|
-
);
|
|
379
|
-
|
|
380
|
-
await runQuery(this.db, query, { execute: true });
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
async tableDescribe(
|
|
384
|
-
tableId: string,
|
|
385
|
-
config: ViewConfig,
|
|
386
|
-
): Promise<TableDescription> {
|
|
387
|
-
let expression_schema = {} as Record<string, ColumnType>;
|
|
388
|
-
const expressions_query = this.sqlBuilder.expressionsDescribe(
|
|
389
|
-
tableId,
|
|
390
|
-
config,
|
|
391
|
-
);
|
|
392
|
-
|
|
393
|
-
if (expressions_query !== undefined) {
|
|
394
|
-
try {
|
|
395
|
-
expression_schema = await this.describeQuery(expressions_query);
|
|
396
|
-
} catch (error) {
|
|
397
|
-
return await this.attributeExpressionErrors(
|
|
398
|
-
tableId,
|
|
399
|
-
config,
|
|
400
|
-
error,
|
|
401
|
-
);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
const schema = Object.keys(config.windows ?? {}).length
|
|
406
|
-
? await this.tableSchema(tableId)
|
|
407
|
-
: undefined;
|
|
408
|
-
|
|
409
|
-
const view_query = this.sqlBuilder.tableDescribe(
|
|
410
|
-
tableId,
|
|
411
|
-
config,
|
|
412
|
-
schema,
|
|
413
|
-
);
|
|
414
|
-
|
|
415
|
-
let view_schema = {} as Record<string, ColumnType>;
|
|
416
|
-
if (view_query !== undefined) {
|
|
417
|
-
try {
|
|
418
|
-
view_schema = await this.describeQuery(view_query);
|
|
419
|
-
} catch (error) {
|
|
420
|
-
return { config_error: errorMessage(error) };
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
return { expression_schema, view_schema };
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
/** The planned result columns of one `DESCRIBE` query. */
|
|
428
|
-
private async describeQuery(query: string) {
|
|
429
|
-
const results = await runQuery(this.db, query);
|
|
430
|
-
const schema = {} as Record<string, ColumnType>;
|
|
431
|
-
for (const result of results) {
|
|
432
|
-
if (!result.name.startsWith("__")) {
|
|
433
|
-
schema[result.name] = duckdbTypeToPsp(
|
|
434
|
-
result.type,
|
|
435
|
-
) as ColumnType;
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
return schema;
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
/** Plans each expression on its own to name the ones at fault. */
|
|
443
|
-
private async attributeExpressionErrors(
|
|
444
|
-
tableId: string,
|
|
445
|
-
config: ViewConfig,
|
|
446
|
-
error: unknown,
|
|
447
|
-
): Promise<TableDescription> {
|
|
448
|
-
const expression_schema = {} as Record<string, ColumnType>;
|
|
449
|
-
const expression_errors = {} as Record<string, ExpressionError>;
|
|
450
|
-
for (const [name, expression] of Object.entries(config.expressions)) {
|
|
451
|
-
const query = this.sqlBuilder.expressionDescribe(
|
|
452
|
-
tableId,
|
|
453
|
-
expression,
|
|
454
|
-
);
|
|
455
|
-
|
|
456
|
-
try {
|
|
457
|
-
const results = await runQuery(this.db, query);
|
|
458
|
-
expression_schema[name] = duckdbTypeToPsp(
|
|
459
|
-
results[0]["type"],
|
|
460
|
-
) as ColumnType;
|
|
461
|
-
} catch (error) {
|
|
462
|
-
expression_errors[name] = {
|
|
463
|
-
error_message: errorMessage(error),
|
|
464
|
-
line: 0,
|
|
465
|
-
column: 0,
|
|
466
|
-
};
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
if (Object.keys(expression_errors).length === 0) {
|
|
471
|
-
return { config_error: errorMessage(error) };
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
return { expression_schema, expression_errors };
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
async viewDelete(viewId: string) {
|
|
478
|
-
const query = this.sqlBuilder.viewDelete(viewId);
|
|
479
|
-
await runQuery(this.db, query, { execute: true });
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
async viewGetData(
|
|
483
|
-
viewId: string,
|
|
484
|
-
config: ViewConfig,
|
|
485
|
-
schema: Record<string, ColumnType>,
|
|
486
|
-
viewport: ViewWindow,
|
|
487
|
-
dataSlice: perspective.VirtualDataSlice,
|
|
488
|
-
) {
|
|
489
|
-
const query = this.sqlBuilder.viewGetData(
|
|
490
|
-
viewId,
|
|
491
|
-
config,
|
|
492
|
-
viewport,
|
|
493
|
-
schema,
|
|
494
|
-
);
|
|
495
|
-
|
|
496
|
-
const { rows, columns, dtypes } = await runQuery(this.db, query, {
|
|
497
|
-
columns: true,
|
|
498
|
-
});
|
|
499
|
-
|
|
500
|
-
for (let cidx = 0; cidx < columns.length; cidx++) {
|
|
501
|
-
const col = columns[cidx];
|
|
502
|
-
const dtype = duckdbTypeToPsp(dtypes[cidx]) as ColumnType;
|
|
503
|
-
|
|
504
|
-
const isDecimal = dtypes[cidx].startsWith("Decimal");
|
|
505
|
-
for (let ridx = 0; ridx < rows.length; ridx++) {
|
|
506
|
-
const row = rows[ridx];
|
|
507
|
-
const grouping_id = row["__GROUPING_ID__"];
|
|
508
|
-
let value = row[col];
|
|
509
|
-
if (isDecimal) {
|
|
510
|
-
value = convertDecimalToNumber(value, dtypes[cidx]);
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
if (typeof value === "bigint") {
|
|
514
|
-
value = Number(value);
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
if (dtype === "datetime" && typeof value === "string") {
|
|
518
|
-
value = +new Date(value);
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
if (dtype === "string" && typeof value !== "string") {
|
|
522
|
-
value = `${value}`;
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
dataSlice.setCol(dtype, col, ridx, value, grouping_id);
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
function errorMessage(error: unknown): string {
|
|
532
|
-
return error instanceof Error ? error.message : String(error);
|
|
533
|
-
}
|