@perspective-dev/client 5.0.0 → 5.2.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/dist/cdn/perspective.js +2 -2
- package/dist/cdn/perspective.js.map +3 -3
- package/dist/esm/perspective.inline.js +2 -2
- package/dist/esm/perspective.inline.js.map +3 -3
- package/dist/esm/perspective.js +2 -2
- package/dist/esm/perspective.js.map +3 -3
- package/dist/esm/perspective.node.js +10 -10
- package/dist/esm/perspective.node.js.map +2 -2
- package/dist/esm/ts-rs/AggSpec.d.ts +8 -0
- package/dist/esm/ts-rs/Features.d.ts +66 -0
- package/dist/esm/ts-rs/ListFlatten.d.ts +1 -0
- package/dist/esm/ts-rs/SplitRollupMode.d.ts +9 -0
- package/dist/esm/ts-rs/TableInitOptions.d.ts +9 -0
- package/dist/esm/ts-rs/ViewConfig.d.ts +4 -0
- package/dist/esm/ts-rs/ViewConfigUpdate.d.ts +24 -0
- package/dist/esm/ts-rs/WindowAggSpec.d.ts +25 -0
- package/dist/esm/ts-rs/WindowSort.d.ts +7 -0
- package/dist/esm/ts-rs/WindowSortDir.d.ts +6 -0
- package/dist/esm/ts-rs/WindowSpec.d.ts +47 -0
- package/dist/esm/ts-rs/Windows.d.ts +10 -0
- package/dist/esm/virtual_server.d.ts +8 -34
- package/dist/esm/virtual_servers/clickhouse.js +1 -1
- package/dist/esm/virtual_servers/clickhouse.js.map +3 -3
- package/dist/esm/virtual_servers/duckdb.d.ts +6 -26
- package/dist/esm/virtual_servers/duckdb.js +1 -1
- package/dist/esm/virtual_servers/duckdb.js.map +3 -3
- package/dist/wasm/perspective-js.d.ts +96 -6
- package/dist/wasm/perspective-js.js +11 -11
- package/dist/wasm/perspective-js.wasm +0 -0
- package/dist/wasm/perspective-js.wasm.d.ts +5 -5
- package/package.json +1 -1
- package/src/rust/lib.rs +6 -0
- package/src/rust/typed_array.rs +22 -3
- package/src/rust/virtual_server.rs +98 -2
- package/src/ts/ts-rs/AggSpec.ts +10 -0
- package/src/ts/ts-rs/Features.ts +61 -0
- package/src/ts/ts-rs/ListFlatten.ts +3 -0
- package/src/ts/ts-rs/SplitRollupMode.ts +11 -0
- package/src/ts/ts-rs/TableInitOptions.ts +10 -1
- package/src/ts/ts-rs/ViewConfig.ts +3 -1
- package/src/ts/ts-rs/ViewConfigUpdate.ts +24 -1
- package/src/ts/ts-rs/WindowAggSpec.ts +25 -0
- package/src/ts/ts-rs/WindowSort.ts +9 -0
- package/src/ts/ts-rs/WindowSortDir.ts +8 -0
- package/src/ts/ts-rs/WindowSpec.ts +47 -0
- package/src/ts/ts-rs/Windows.ts +10 -0
- package/src/ts/virtual_server.ts +17 -55
- package/src/ts/virtual_servers/clickhouse.ts +78 -4
- package/src/ts/virtual_servers/duckdb.ts +145 -31
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ColumnType } from "./ColumnType.js";
|
|
2
|
+
/**
|
|
3
|
+
* Specification for an aggregate function.
|
|
4
|
+
*
|
|
5
|
+
* Aggregates can either take no additional arguments ([`AggSpec::Single`])
|
|
6
|
+
* or require column type arguments ([`AggSpec::Multiple`]).
|
|
7
|
+
*/
|
|
8
|
+
export type AggSpec = string | [string, Array<ColumnType>];
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { AggSpec } from "./AggSpec.js";
|
|
2
|
+
import type { ColumnType } from "./ColumnType.js";
|
|
3
|
+
import type { GroupRollupMode } from "./GroupRollupMode.js";
|
|
4
|
+
import type { SplitRollupMode } from "./SplitRollupMode.js";
|
|
5
|
+
import type { WindowAggSpec } from "./WindowAggSpec.js";
|
|
6
|
+
/**
|
|
7
|
+
* Describes the capabilities supported by a virtual server handler.
|
|
8
|
+
*
|
|
9
|
+
* This struct is returned by
|
|
10
|
+
* [`VirtualServerHandler::get_features`](super::VirtualServerHandler::get_features)
|
|
11
|
+
* to inform clients about which operations are available.
|
|
12
|
+
*/
|
|
13
|
+
export type Features = {
|
|
14
|
+
/**
|
|
15
|
+
* Whether group-by aggregation is supported.
|
|
16
|
+
*/
|
|
17
|
+
group_by?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Which `group_by_rollup_mode` options are supported
|
|
20
|
+
*/
|
|
21
|
+
group_rollup_mode?: Array<GroupRollupMode>;
|
|
22
|
+
/**
|
|
23
|
+
* Whether split-by (pivot) operations are supported.
|
|
24
|
+
*/
|
|
25
|
+
split_by?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Which `split_rollup_mode` options are supported. Empty (the default)
|
|
28
|
+
* means `["flat"]` - a server must opt in to `"rollup"` explicitly, as
|
|
29
|
+
* it requires emitting subtotal and grand-total column groups.
|
|
30
|
+
*/
|
|
31
|
+
split_rollup_mode?: Array<SplitRollupMode>;
|
|
32
|
+
/**
|
|
33
|
+
* Available filter operators per column type.
|
|
34
|
+
*/
|
|
35
|
+
filter_ops?: {
|
|
36
|
+
[key in ColumnType]?: Array<string>;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Available aggregate functions per column type.
|
|
40
|
+
*/
|
|
41
|
+
aggregates?: {
|
|
42
|
+
[key in ColumnType]?: Array<AggSpec>;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Whether sorting is supported.
|
|
46
|
+
*/
|
|
47
|
+
sort?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Whether computed expressions are supported.
|
|
50
|
+
*/
|
|
51
|
+
expressions?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Available window aggregates per column type.
|
|
54
|
+
*/
|
|
55
|
+
window_aggregates?: {
|
|
56
|
+
[key in ColumnType]?: Array<WindowAggSpec>;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Whether update callbacks are supported.
|
|
60
|
+
*/
|
|
61
|
+
on_update?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* The data store has no reliable natural row order
|
|
64
|
+
*/
|
|
65
|
+
unordered?: boolean;
|
|
66
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type ListFlatten = "zip" | "cartesian" | "stringify";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `split_by` corollary to [`GroupRollupMode`]. `Flat` (the default,
|
|
3
|
+
* matching this crate's historical behavior) emits only full-depth split
|
|
4
|
+
* combinations as columns; `Rollup` additionally emits grand-total and
|
|
5
|
+
* subtotal column groups in "totals before" order. There is no `Total`
|
|
6
|
+
* variant - an empty `split_by` already expresses a single grand-total
|
|
7
|
+
* column group.
|
|
8
|
+
*/
|
|
9
|
+
export type SplitRollupMode = "flat" | "rollup";
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ListFlatten } from "./ListFlatten.js";
|
|
1
2
|
import type { TableReadFormat } from "./TableReadFormat.js";
|
|
2
3
|
/**
|
|
3
4
|
* Options which impact the behavior of [`Client::table`], as well as
|
|
@@ -25,4 +26,12 @@ export type TableInitOptions = {
|
|
|
25
26
|
* WASM it is OPFS (Worker only). Defaults to in-memory.
|
|
26
27
|
*/
|
|
27
28
|
page_to_disk?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* How Arrow `LIST` and JSON `Array` columns are ingested. `zip` (the
|
|
31
|
+
* default) and `cartesian` expand a row into one row per list element,
|
|
32
|
+
* and are incompatible with `index`, as the rows of an expansion
|
|
33
|
+
* repeat their index. `stringify` encodes each list as a JSON array in
|
|
34
|
+
* a single string column instead.
|
|
35
|
+
*/
|
|
36
|
+
list_flatten?: ListFlatten;
|
|
28
37
|
};
|
|
@@ -4,14 +4,18 @@ import type { Filter } from "./Filter.js";
|
|
|
4
4
|
import type { FilterReducer } from "./FilterReducer.js";
|
|
5
5
|
import type { GroupRollupMode } from "./GroupRollupMode.js";
|
|
6
6
|
import type { Sort } from "./Sort.js";
|
|
7
|
+
import type { SplitRollupMode } from "./SplitRollupMode.js";
|
|
8
|
+
import type { Windows } from "./Windows.js";
|
|
7
9
|
export type ViewConfig = {
|
|
8
10
|
group_by: Array<string>;
|
|
9
11
|
split_by: Array<string>;
|
|
10
12
|
sort: Array<Sort>;
|
|
11
13
|
filter: Array<Filter>;
|
|
12
14
|
group_rollup_mode: GroupRollupMode;
|
|
15
|
+
split_rollup_mode: SplitRollupMode;
|
|
13
16
|
filter_op?: FilterReducer;
|
|
14
17
|
expressions: Expressions;
|
|
18
|
+
windows?: Windows;
|
|
15
19
|
columns: Array<string | null>;
|
|
16
20
|
aggregates: {
|
|
17
21
|
[key in string]?: Aggregate;
|
|
@@ -4,6 +4,8 @@ import type { Filter } from "./Filter.js";
|
|
|
4
4
|
import type { FilterReducer } from "./FilterReducer.js";
|
|
5
5
|
import type { GroupRollupMode } from "./GroupRollupMode.js";
|
|
6
6
|
import type { Sort } from "./Sort.js";
|
|
7
|
+
import type { SplitRollupMode } from "./SplitRollupMode.js";
|
|
8
|
+
import type { Windows } from "./Windows.js";
|
|
7
9
|
export type ViewConfigUpdate = {
|
|
8
10
|
/**
|
|
9
11
|
* A group by _groups_ the dataset by the unique values of each column used
|
|
@@ -60,6 +62,12 @@ export type ViewConfigUpdate = {
|
|
|
60
62
|
* name and a string sort direction. When `column-pivots` are applied,
|
|
61
63
|
* the additional sort directions `"col asc"` and `"col desc"` will
|
|
62
64
|
* determine the order of pivot columns groups.
|
|
65
|
+
*
|
|
66
|
+
* `sort` is the ONLY thing that orders a `View`'s rows — without it
|
|
67
|
+
* they keep the `Table`'s natural (insertion) order, which any
|
|
68
|
+
* consumer reading rows sequentially will reflect. Not to be
|
|
69
|
+
* confused with a window column's `order_by`, which orders rows
|
|
70
|
+
* WITHIN a window frame and does not reorder the `View`.
|
|
63
71
|
*/
|
|
64
72
|
sort?: Array<Sort>;
|
|
65
73
|
/**
|
|
@@ -70,6 +78,14 @@ export type ViewConfigUpdate = {
|
|
|
70
78
|
* panel.
|
|
71
79
|
*/
|
|
72
80
|
expressions?: Expressions;
|
|
81
|
+
/**
|
|
82
|
+
* The `windows` property declares ordered, partitioned rolling
|
|
83
|
+
* computations (moving aggregates, cumulative sums) as _new_ columns
|
|
84
|
+
* keyed by output alias (`{"name": {...spec}}`, symmetric with
|
|
85
|
+
* `expressions`), analogous to SQL window functions. See
|
|
86
|
+
* [`crate::config::WindowSpec`].
|
|
87
|
+
*/
|
|
88
|
+
windows?: Windows;
|
|
73
89
|
/**
|
|
74
90
|
* Aggregates perform a calculation over an entire column, and are
|
|
75
91
|
* displayed when one or more [Group By](#group-by) are applied to the
|
|
@@ -82,6 +98,13 @@ export type ViewConfigUpdate = {
|
|
|
82
98
|
* Perspective provides a selection of aggregate functions that can be
|
|
83
99
|
* applied to columns in the `View` constructor using a dictionary of
|
|
84
100
|
* column name to aggregate function name.
|
|
101
|
+
*
|
|
102
|
+
* An aggregate also determines the column's RESULT TYPE, which need
|
|
103
|
+
* not match the input: `"count"` yields an `integer` whatever it
|
|
104
|
+
* counts, so a `date` column left on the default `"count"` is an
|
|
105
|
+
* `integer` in the resulting `View` — no longer a date. Set an
|
|
106
|
+
* aggregate that preserves the type (e.g. `"any"`, `"last"`) when
|
|
107
|
+
* the original type matters, such as a date used as a chart axis.
|
|
85
108
|
*/
|
|
86
109
|
aggregates?: {
|
|
87
110
|
[key in string]?: Aggregate;
|
|
@@ -89,4 +112,5 @@ export type ViewConfigUpdate = {
|
|
|
89
112
|
group_by_depth?: number;
|
|
90
113
|
filter_op?: FilterReducer;
|
|
91
114
|
group_rollup_mode?: GroupRollupMode;
|
|
115
|
+
split_rollup_mode?: SplitRollupMode;
|
|
92
116
|
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ColumnType } from "./ColumnType.js";
|
|
2
|
+
/**
|
|
3
|
+
* Specification for a window aggregate.
|
|
4
|
+
*/
|
|
5
|
+
export type WindowAggSpec = {
|
|
6
|
+
name: string;
|
|
7
|
+
/**
|
|
8
|
+
* Frame kinds this aggregate accepts (`rows`, `range`, `cumulative`).
|
|
9
|
+
* Empty means it takes no frame at all.
|
|
10
|
+
*/
|
|
11
|
+
frames?: Array<string>;
|
|
12
|
+
/**
|
|
13
|
+
* Takes a row offset, as `lag` / `lead` / `diff` do.
|
|
14
|
+
*/
|
|
15
|
+
offset?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Takes a smoothing factor, as `ema` does.
|
|
18
|
+
*/
|
|
19
|
+
alpha?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* The output column type. `None` means the source column's type, which
|
|
22
|
+
* is what `min` / `max` / `lag` do.
|
|
23
|
+
*/
|
|
24
|
+
result_type?: ColumnType;
|
|
25
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { WindowSortDir } from "./WindowSortDir.js";
|
|
2
|
+
/**
|
|
3
|
+
* The `Table` column which orders each partition, with its direction -
|
|
4
|
+
* serialized as a two-element array (`["ts", "desc"]`), symmetric with the
|
|
5
|
+
* `ViewConfig`'s `sort` field.
|
|
6
|
+
*/
|
|
7
|
+
export type WindowSort = [string, WindowSortDir];
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A window's order direction. A dedicated two-variant enum rather than
|
|
3
|
+
* [`crate::config::SortDir`] - the column-sort extras (`col asc`, `abs`,
|
|
4
|
+
* `none`) are meaningless inside a window frame and unrepresentable here.
|
|
5
|
+
*/
|
|
6
|
+
export type WindowSortDir = "asc" | "desc";
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { WindowSort } from "./WindowSort.js";
|
|
2
|
+
/**
|
|
3
|
+
* The serialized form of [`WindowSpec`].
|
|
4
|
+
*/
|
|
5
|
+
export type WindowSpec = {
|
|
6
|
+
/**
|
|
7
|
+
* The input column - either a real `Table` column or an expression
|
|
8
|
+
* alias from the same `ViewConfig`.
|
|
9
|
+
*/
|
|
10
|
+
column: string;
|
|
11
|
+
aggregate: string;
|
|
12
|
+
/**
|
|
13
|
+
* Columns whose distinct value tuples partition the rows; empty
|
|
14
|
+
* partitions the whole `Table` as one group.
|
|
15
|
+
*/
|
|
16
|
+
partition_by?: Array<string>;
|
|
17
|
+
/**
|
|
18
|
+
* The `Table` column which orders each partition, and the direction.
|
|
19
|
+
* This orders rows WITHIN the window frame only — it does not
|
|
20
|
+
* reorder the `View`, which is what the view-level `sort` does.
|
|
21
|
+
*/
|
|
22
|
+
order_by?: WindowSort | null;
|
|
23
|
+
/**
|
|
24
|
+
* A frame of the `rows` preceding each row, plus the row itself.
|
|
25
|
+
* Mutually exclusive with `range` and `cumulative`.
|
|
26
|
+
*/
|
|
27
|
+
rows?: number | null;
|
|
28
|
+
/**
|
|
29
|
+
* A frame of the rows whose `order_by` value lies within `range` of
|
|
30
|
+
* each row's, requiring a numeric or temporal `order_by`. Mutually
|
|
31
|
+
* exclusive with `rows` and `cumulative`.
|
|
32
|
+
*/
|
|
33
|
+
range?: number | null;
|
|
34
|
+
/**
|
|
35
|
+
* `true` frames all rows from the partition start through each row.
|
|
36
|
+
* Default can be omitted.
|
|
37
|
+
*/
|
|
38
|
+
cumulative?: boolean | null;
|
|
39
|
+
/**
|
|
40
|
+
* Row offset for `lag`/`lead` (default 1).
|
|
41
|
+
*/
|
|
42
|
+
offset?: number | null;
|
|
43
|
+
/**
|
|
44
|
+
* Smoothing factor in `(0, 1]` for `ema`.
|
|
45
|
+
*/
|
|
46
|
+
alpha?: number | null;
|
|
47
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { WindowSpec } from "./WindowSpec.js";
|
|
2
|
+
/**
|
|
3
|
+
* The window columns of a `ViewConfig`, keyed by output column alias -
|
|
4
|
+
* symmetric with `expressions` (`{"name": {...spec}}`). An alias must not
|
|
5
|
+
* collide with a `Table` column, expression alias, or another window's
|
|
6
|
+
* key.
|
|
7
|
+
*/
|
|
8
|
+
export type Windows = {
|
|
9
|
+
[key in string]?: WindowSpec;
|
|
10
|
+
};
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { ColumnType } from "./ts-rs/ColumnType.ts";
|
|
2
|
-
import { ViewConfig } from "./ts-rs/ViewConfig.ts";
|
|
3
|
-
import { ViewWindow } from "./ts-rs/ViewWindow.ts";
|
|
4
1
|
import type * as perspective from "../../dist/wasm/perspective-js.js";
|
|
5
2
|
/**
|
|
6
3
|
* VirtualServer API for implementing custom data sources in JavaScript/WASM.
|
|
@@ -12,36 +9,13 @@ import type * as perspective from "../../dist/wasm/perspective-js.js";
|
|
|
12
9
|
* - Implementing custom aggregation or transformation logic
|
|
13
10
|
* - Creating data adapters without copying data into Perspective tables
|
|
14
11
|
*
|
|
15
|
-
*
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Handler interface that you implement to provide custom data sources.
|
|
12
|
+
* The `VirtualServerHandler` interface and `Features` struct are declared
|
|
13
|
+
* in Rust alongside the bridge that invokes them (`perspective-js`'s
|
|
14
|
+
* `virtual_server.rs` and `perspective-client`'s `features.rs`
|
|
15
|
+
* respectively) and re-exported here. `ServerFeatures` is a legacy alias
|
|
16
|
+
* for `Features`.
|
|
22
17
|
*
|
|
23
|
-
*
|
|
24
|
-
* messages from Perspective clients. Methods can return values directly or
|
|
25
|
-
* return Promises for asynchronous operations (e.g., database queries).
|
|
18
|
+
* @module virtual_server
|
|
26
19
|
*/
|
|
27
|
-
export
|
|
28
|
-
|
|
29
|
-
tableSchema(tableId: string): Record<string, ColumnType> | Promise<Record<string, ColumnType>>;
|
|
30
|
-
tableSize(tableId: string): number | Promise<number>;
|
|
31
|
-
tableMakeView(tableId: string, viewId: string, config: ViewConfig): void | Promise<void>;
|
|
32
|
-
viewDelete(viewId: string): void | Promise<void>;
|
|
33
|
-
viewGetData(viewId: string, config: ViewConfig, schema: Record<string, ColumnType>, viewport: ViewWindow, dataSlice: perspective.VirtualDataSlice): void | Promise<void>;
|
|
34
|
-
viewSchema?(viewId: string, config?: ViewConfig): Record<string, ColumnType> | Promise<Record<string, ColumnType>>;
|
|
35
|
-
viewSize?(viewId: string): number | Promise<number>;
|
|
36
|
-
tableValidateExpression?(tableId: string, expression: string): ColumnType | Promise<ColumnType>;
|
|
37
|
-
viewGetMinMax?(viewId: string, columnName: string, config: ViewConfig): {
|
|
38
|
-
min: any;
|
|
39
|
-
max: any;
|
|
40
|
-
} | Promise<{
|
|
41
|
-
min: any;
|
|
42
|
-
max: any;
|
|
43
|
-
}>;
|
|
44
|
-
getFeatures?(): ServerFeatures | Promise<ServerFeatures>;
|
|
45
|
-
makeTable?(tableId: string, data: string | Uint8Array): void | Promise<void>;
|
|
46
|
-
}
|
|
47
|
-
export declare function createMessageHandler(mod: typeof perspective, handler: VirtualServerHandler): MessagePort;
|
|
20
|
+
export type { VirtualServerHandler, VirtualHostedTable, Features, Features as ServerFeatures, } from "../../dist/wasm/perspective-js.js";
|
|
21
|
+
export declare function createMessageHandler(mod: typeof perspective, handler: perspective.VirtualServerHandler): MessagePort;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var S=["sum","count","any_value","arbitrary","array_agg","avg","bit_and","bit_or","bit_xor","bitstring_agg","bool_and","bool_or","countif","favg","fsum","geomean","kahan_sum","last","max","min","product","string_agg","sumkahan"],f=["count","any_value","arbitrary","first","countif","last","string_agg"],l=["rows","range","cumulative"],T=[{name:"sum",frames:l,result_type:"float"},{name:"avg",frames:l,result_type:"float"},{name:"count",frames:l,result_type:"float"},{name:"min",frames:l},{name:"max",frames:l},{name:"stddev_samp",frames:l,result_type:"float"},{name:"var_samp",frames:l,result_type:"float"},{name:"lag",offset:!0},{name:"lead",offset:!0},{name:"diff",offset:!0,result_type:"float"},{name:"rate",frames:["range"],result_type:"float"}],d=[{name:"count",frames:l,result_type:"float"},{name:"min",frames:l},{name:"max",frames:l},{name:"lag",offset:!0},{name:"lead",offset:!0}],p=["==","!=","IS DISTINCT FROM","IS NOT DISTINCT FROM",">=","<=",">","<","is null","is not null"],E=[...p,"begins with","not begins with","contains","not contains","ends with","not ends with","matches","not matches","in","not in","LIKE","NOT LIKE","ILIKE","NOT ILIKE"];function _(i){if(i.startsWith("Nullable")&&(i=i.match(/Nullable\((.+?)\)/)[1]),i.startsWith("Array"))return"string";if(i==="Int64"||i==="UInt64"||i==="Float64")return"float";if(i==="String")return"string";if(i==="DateTime")return"datetime";if(i==="Date")return"date";throw new Error(`Unknown type '${i}'`)}function V(i,t){if(!(i instanceof Uint32Array||i instanceof Int32Array))return i;let e=BigInt(0);for(let r=0;r<i.length;r++)e|=BigInt(i[r])<<BigInt(r*32);let s=t.match(/Decimal\[\d+e(\d+)\]/);if(s){let r=parseInt(s[1]);return Number(e)/Math.pow(10,r)}else return Number(e)}var h=class{lockPromise;constructor(){this.lockPromise=Promise.resolve()}acquire(){let t,e=new Promise(r=>{t=r}),s=this.lockPromise.then(()=>t);return this.lockPromise=e,s}},q=new h;async function u(i,t,e={}){t=t.replace(/\s+/g," ").trim();let s=await q.acquire();try{let r=await i.query({query:t});if(!e.execute){let{data:o,meta:a}=await r.json();return e.columns?{rows:o,columns:a.map(c=>c.name),dtypes:a.map(c=>c.type)}:o}}catch(r){throw console.error("Query error:",r),console.error("Query:",t),r}finally{s()}}var I=class{db;sqlBuilder;constructor(t,e){if(!e&&customElements){let s=customElements.get("perspective-viewer");if(s)e=s.__wasm_module__;else throw new Error("Missing perspective-client.wasm")}this.db=t,this.sqlBuilder=new e.GenericSQLVirtualServerModel({create_entity:"VIEW",grouping_fn:"GROUPING",column_separator:"|",backslash_escaped_literals:!0,regex_fn:"match"})}getFeatures(){return{group_by:!0,split_by:!1,sort:!0,expressions:!0,unordered:!0,window_aggregates:{integer:T,float:T,string:d,date:d,datetime:d,boolean:d},group_rollup_mode:["rollup","flat","total"],filter_ops:{integer:p,float:p,string:E,boolean:p,date:p,datetime:p},aggregates:{integer:S,float:S,string:f,boolean:f,date:f,datetime:f}}}async getHostedTables(){return(await u(this.db,"SHOW TABLES")).map(s=>`${s.name}`)}async tableSchema(t,e){let s=this.sqlBuilder.tableSchema(t),r=await u(this.db,s),o={};for(let a of r){let c=a.name;c.startsWith("__")||(o[c]=_(a.type))}return o}async viewColumnSize(t,e){let s=`SELECT COUNT() FROM system.columns WHERE table = '${t}'`,r=await u(this.db,s),o=e.group_by?.length||0,a=Number(r[0]["COUNT()"]);return console.log(a),a-(o===0?0:o+(e.split_by?.length===0?1:0))}async tableSize(t){let e=this.sqlBuilder.tableSize(t),s=await u(this.db,e);return Number(s[0]["COUNT()"])}async tableMakeView(t,e,s){let r=this.sqlBuilder.tableMakeView(t,e,s);await u(this.db,r,{execute:!0})}async tableValidateExpression(t,e){let s=this.sqlBuilder.tableValidateExpression(t,e),r=await u(this.db,s);return _(r[0].type)}async viewDelete(t){let e=this.sqlBuilder.viewDelete(t);await u(this.db,e,{execute:!0})}async viewGetData(t,e,s,r,o){let a=this.sqlBuilder.viewGetData(t,e,r,s),{rows:c,columns:b,dtypes:y}=await u(this.db,a,{columns:!0});for(let m=0;m<b.length;m++){let v=b[m],w=_(y[m]),N=y[m].startsWith("Decimal");for(let g=0;g<c.length;g++){let C=c[g],k=C.__GROUPING_ID__,n=C[v];N&&(n=V(n,y[m])),typeof n=="bigint"&&(n=Number(n)),w==="datetime"&&typeof n=="string"&&(n=+new Date(n)),w==="string"&&typeof n!="string"&&(n=`${n}`),o.setCol(w,v,g,n,k)}}}};export{I as ClickhouseHandler};
|
|
2
2
|
//# sourceMappingURL=clickhouse.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/ts/virtual_servers/clickhouse.ts"],
|
|
4
|
-
"sourcesContent": ["// \u250F\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513\n// \u2503 \u2588\u2588\u2588\u2588\u2588\u2588 \u2588\u2588\u2588\u2588\u2588\u2588 \u2588\u2588\u2588\u2588\u2588\u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2588\u2584 \u2580\u2588\u2588\u2588 \u2588 \u2503\n// \u2503 \u2584\u2584\u2584\u2584\u2584\u2588 \u2588\u2584\u2584\u2584\u2584\u2584 \u2584\u2584\u2584\u2584\u2584\u2588 \u2580\u2580\u2580\u2580\u2580\u2588\u2580\u2580\u2580\u2580\u2580 \u2588 \u2580\u2580\u2580\u2580\u2580\u2588 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u258C\u2590\u2588\u2588\u2588 \u2588\u2588\u2588\u2584 \u2580\u2588 \u2588 \u2580\u2580\u2580\u2580\u2580 \u2503\n// \u2503 \u2588\u2580\u2580\u2580\u2580\u2580 \u2588\u2580\u2580\u2580\u2580\u2580 \u2588\u2580\u2588\u2588\u2580\u2580 \u2584\u2584\u2584\u2584\u2584 \u2588 \u2584\u2584\u2584\u2584\u2584\u2588 \u2584\u2584\u2584\u2584\u2584\u2588 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u258C\u2590\u2588\u2588\u2588 \u2588\u2588\u2588\u2588\u2588\u2584 \u2588 \u2584\u2584\u2584\u2584\u2584 \u2503\n// \u2503 \u2588 \u2588\u2588\u2588\u2588\u2588\u2588 \u2588 \u2580\u2588\u2584 \u2588 \u2588\u2588\u2588\u2588\u2588\u2588 \u2588 \u2588\u2588\u2588\u258C\u2590\u2588\u2588\u2588 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2584 \u2588 \u2503\n// \u2523\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252B\n// \u2503 Copyright (c) 2017, the Perspective Authors. \u2503\n// \u2503 \u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C \u2503\n// \u2503 This file is part of the Perspective library, distributed under the terms \u2503\n// \u2503 of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). \u2503\n// \u2517\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u251B\n\n/**\n * An implementation of a Perspective Virtual Server for DuckDB.\n *\n * This import is optional, and so must be imported manually from either\n * `@perspective-dev/client/dist/esm/virtual_servers/duckdb.js` or\n * `@perspective-dev/client/src/ts/virtual_servers/duckdb.ts`, it is not\n * exported from the package root `@perspective-dev/client`\n *\n * @module\n */\n\nimport type * as perspective from \"@perspective-dev/client\";\nimport type { ColumnType } from \"@perspective-dev/client/dist/esm/ts-rs/ColumnType.d.ts\";\nimport type { ViewConfig } from \"@perspective-dev/client/dist/esm/ts-rs/ViewConfig.d.ts\";\nimport type { ViewWindow } from \"@perspective-dev/client/dist/esm/ts-rs/ViewWindow.d.ts\";\nimport type * as clickhouse from \"@clickhouse/client-web\";\n\nconst NUMBER_AGGS = [\n \"sum\",\n \"count\",\n \"any_value\",\n \"arbitrary\",\n \"array_agg\",\n \"avg\",\n \"bit_and\",\n \"bit_or\",\n \"bit_xor\",\n \"bitstring_agg\",\n \"bool_and\",\n \"bool_or\",\n \"countif\",\n \"favg\",\n \"fsum\",\n \"geomean\",\n \"kahan_sum\",\n \"last\",\n \"max\",\n \"min\",\n \"product\",\n \"string_agg\",\n \"sumkahan\",\n];\n\nconst STRING_AGGS = [\n \"count\",\n \"any_value\",\n \"arbitrary\",\n \"first\",\n \"countif\",\n \"last\",\n \"string_agg\",\n];\n\nconst FILTER_OPS = [\n \"==\",\n \"!=\",\n \"LIKE\",\n \"IS DISTINCT FROM\",\n \"IS NOT DISTINCT FROM\",\n \">=\",\n \"<=\",\n \">\",\n \"<\",\n];\n\nfunction duckdbTypeToPsp(name: string): ColumnType {\n if (name.startsWith(\"Nullable\")) {\n name = name.match(/Nullable\\((.+?)\\)/)![1];\n }\n\n if (name.startsWith(\"Array\")) {\n return \"string\";\n }\n\n if (name === \"Int64\" || name === \"UInt64\" || name === \"Float64\") {\n return \"float\";\n }\n\n if (name === \"String\") {\n return \"string\";\n }\n\n if (name === \"DateTime\") {\n return \"datetime\";\n }\n\n if (name === \"Date\") {\n return \"date\";\n }\n\n throw new Error(`Unknown type '${name}'`);\n}\n\nfunction convertDecimalToNumber(value: any, dtypeString: string) {\n if (!(value instanceof Uint32Array || value instanceof Int32Array)) {\n return value;\n }\n\n let bigIntValue = BigInt(0);\n for (let i = 0; i < value.length; i++) {\n bigIntValue |= BigInt(value[i]) << BigInt(i * 32);\n }\n\n const scaleMatch = dtypeString.match(/Decimal\\[\\d+e(\\d+)\\]/);\n if (scaleMatch) {\n const scale = parseInt(scaleMatch[1]);\n return Number(bigIntValue) / Math.pow(10, scale);\n } else {\n return Number(bigIntValue);\n }\n}\n\nclass Lock {\n lockPromise: Promise<void>;\n constructor() {\n this.lockPromise = Promise.resolve();\n }\n\n acquire() {\n let releaseLock: (value: void) => void;\n const newLockPromise: Promise<void> = new Promise((resolve) => {\n releaseLock = resolve;\n });\n\n const acquirePromise = this.lockPromise.then(() => releaseLock);\n this.lockPromise = newLockPromise;\n return acquirePromise;\n }\n}\n\nconst LOCK = new Lock();\n\nasync function runQuery(\n db: clickhouse.ClickHouseClient,\n query: string,\n options: { columns?: true; execute?: boolean },\n): Promise<{\n rows: any[];\n columns: string[];\n dtypes: string[];\n}>;\n\nasync function runQuery(\n db: clickhouse.ClickHouseClient,\n query: string,\n options?: { columns?: false; execute?: boolean },\n): Promise<any[]>;\n\nasync function runQuery(\n db: clickhouse.ClickHouseClient,\n query: string,\n options: { columns?: boolean; execute?: boolean } = {},\n) {\n query = query.replace(/\\s+/g, \" \").trim();\n const release = await LOCK.acquire();\n try {\n const result = await db.query({ query });\n if (!options.execute) {\n const { data, meta } =\n (await result.json()) as clickhouse.ResponseJSON<unknown>;\n\n if (options.columns) {\n return {\n rows: data,\n columns: meta!.map((f) => f.name),\n dtypes: meta!.map((f) => f.type),\n };\n }\n\n return data;\n }\n } catch (error) {\n console.error(\"Query error:\", error);\n console.error(\"Query:\", query);\n throw error;\n } finally {\n release();\n }\n}\n\n/**\n * An implementation of Perspective's Virtual Server for `@duckdb/duckdb-wasm`.\n */\nexport class ClickhouseHandler implements perspective.VirtualServerHandler {\n private db: clickhouse.ClickHouseClient;\n private sqlBuilder: perspective.GenericSQLVirtualServerModel;\n constructor(db: clickhouse.ClickHouseClient, mod?: typeof perspective) {\n if (!mod) {\n if (customElements) {\n const viewer_class: any =\n customElements.get(\"perspective-viewer\");\n if (viewer_class) {\n mod = viewer_class.__wasm_module__;\n } else {\n throw new Error(\"Missing perspective-client.wasm\");\n }\n } else {\n }\n }\n\n this.db = db;\n this.sqlBuilder = new mod!.GenericSQLVirtualServerModel({\n create_entity: \"VIEW\",\n grouping_fn: \"GROUPING\",\n column_separator: \"|\",\n });\n }\n\n getFeatures() {\n return {\n group_by: true,\n split_by: false,\n sort: true,\n expressions: true,\n group_rollup_mode: [\"rollup\", \"flat\", \"total\"],\n filter_ops: {\n integer: FILTER_OPS,\n float: FILTER_OPS,\n string: FILTER_OPS,\n boolean: FILTER_OPS,\n date: FILTER_OPS,\n datetime: FILTER_OPS,\n },\n aggregates: {\n integer: NUMBER_AGGS,\n float: NUMBER_AGGS,\n string: STRING_AGGS,\n boolean: STRING_AGGS,\n date: STRING_AGGS,\n datetime: STRING_AGGS,\n },\n };\n }\n\n async getHostedTables() {\n const query = \"SHOW TABLES\";\n const results = await runQuery(this.db, query);\n return results.map((row) => {\n return `${row.name}`;\n });\n }\n\n async tableSchema(tableId: string, config?: ViewConfig) {\n const query = this.sqlBuilder.tableSchema(tableId);\n const results = await runQuery(this.db, query);\n const schema = {} as Record<string, ColumnType>;\n for (const result of results) {\n const colName = result.name;\n if (!colName.startsWith(\"__\")) {\n schema[colName] = duckdbTypeToPsp(result.type) as ColumnType;\n }\n }\n\n return schema;\n }\n\n async viewColumnSize(viewId: string, config: ViewConfig) {\n const query = `SELECT COUNT() FROM system.columns WHERE table = '${viewId}'`;\n const results = await runQuery(this.db, query);\n const gs = config.group_by?.length || 0;\n const count = Number(results[0][\"COUNT()\"]);\n console.log(count);\n return (\n count -\n (gs === 0 ? 0 : gs + (config.split_by?.length === 0 ? 1 : 0))\n );\n }\n\n async tableSize(tableId: string) {\n const query = this.sqlBuilder.tableSize(tableId);\n const results = await runQuery(this.db, query);\n return Number(results[0][\"COUNT()\"]);\n }\n\n async tableMakeView(tableId: string, viewId: string, config: ViewConfig) {\n const query = this.sqlBuilder.tableMakeView(tableId, viewId, config);\n await runQuery(this.db, query, { execute: true });\n }\n\n async tableValidateExpression(tableId: string, expression: string) {\n const query = this.sqlBuilder.tableValidateExpression(\n tableId,\n expression,\n );\n const results = await runQuery(this.db, query);\n return duckdbTypeToPsp(results[0][\"type\"]) as ColumnType;\n }\n\n async viewDelete(viewId: string) {\n const query = this.sqlBuilder.viewDelete(viewId);\n await runQuery(this.db, query, { execute: true });\n }\n\n async viewGetData(\n viewId: string,\n config: ViewConfig,\n schema: Record<string, ColumnType>,\n viewport: ViewWindow,\n dataSlice: perspective.VirtualDataSlice,\n ) {\n const query = this.sqlBuilder.viewGetData(\n viewId,\n config,\n viewport,\n schema,\n );\n\n const { rows, columns, dtypes } = await runQuery(this.db, query, {\n columns: true,\n });\n\n for (let cidx = 0; cidx < columns.length; cidx++) {\n const col = columns[cidx];\n const dtype = duckdbTypeToPsp(dtypes[cidx]) as ColumnType;\n\n const isDecimal = dtypes[cidx].startsWith(\"Decimal\");\n for (let ridx = 0; ridx < rows.length; ridx++) {\n const row = rows[ridx];\n const grouping_id = row[\"__GROUPING_ID__\"];\n let value = row[col];\n if (isDecimal) {\n value = convertDecimalToNumber(value, dtypes[cidx]);\n }\n\n if (typeof value === \"bigint\") {\n value = Number(value);\n }\n\n if (dtype === \"datetime\" && typeof value === \"string\") {\n value = +new Date(value);\n }\n\n if (dtype === \"string\" && typeof value !== \"string\") {\n value = `${value}`;\n }\n\n dataSlice.setCol(dtype, col, ridx, value, grouping_id);\n }\n }\n }\n}\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["NUMBER_AGGS", "STRING_AGGS", "FILTER_OPS", "duckdbTypeToPsp", "name", "convertDecimalToNumber", "value", "dtypeString", "bigIntValue", "i", "scaleMatch", "scale", "Lock", "releaseLock", "newLockPromise", "resolve", "acquirePromise", "LOCK", "runQuery", "db", "query", "options", "release", "result", "data", "meta", "f", "error", "ClickhouseHandler", "mod", "viewer_class", "row", "tableId", "config", "results", "schema", "colName", "viewId", "gs", "count", "expression", "viewport", "dataSlice", "rows", "columns", "dtypes", "cidx", "col", "dtype", "isDecimal", "ridx", "grouping_id"]
|
|
4
|
+
"sourcesContent": ["// \u250F\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513\n// \u2503 \u2588\u2588\u2588\u2588\u2588\u2588 \u2588\u2588\u2588\u2588\u2588\u2588 \u2588\u2588\u2588\u2588\u2588\u2588 \u2588 \u2588 \u2588 \u2588 \u2588 \u2588\u2584 \u2580\u2588\u2588\u2588 \u2588 \u2503\n// \u2503 \u2584\u2584\u2584\u2584\u2584\u2588 \u2588\u2584\u2584\u2584\u2584\u2584 \u2584\u2584\u2584\u2584\u2584\u2588 \u2580\u2580\u2580\u2580\u2580\u2588\u2580\u2580\u2580\u2580\u2580 \u2588 \u2580\u2580\u2580\u2580\u2580\u2588 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u258C\u2590\u2588\u2588\u2588 \u2588\u2588\u2588\u2584 \u2580\u2588 \u2588 \u2580\u2580\u2580\u2580\u2580 \u2503\n// \u2503 \u2588\u2580\u2580\u2580\u2580\u2580 \u2588\u2580\u2580\u2580\u2580\u2580 \u2588\u2580\u2588\u2588\u2580\u2580 \u2584\u2584\u2584\u2584\u2584 \u2588 \u2584\u2584\u2584\u2584\u2584\u2588 \u2584\u2584\u2584\u2584\u2584\u2588 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u258C\u2590\u2588\u2588\u2588 \u2588\u2588\u2588\u2588\u2588\u2584 \u2588 \u2584\u2584\u2584\u2584\u2584 \u2503\n// \u2503 \u2588 \u2588\u2588\u2588\u2588\u2588\u2588 \u2588 \u2580\u2588\u2584 \u2588 \u2588\u2588\u2588\u2588\u2588\u2588 \u2588 \u2588\u2588\u2588\u258C\u2590\u2588\u2588\u2588 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2584 \u2588 \u2503\n// \u2523\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252B\n// \u2503 Copyright (c) 2017, the Perspective Authors. \u2503\n// \u2503 \u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C\u254C \u2503\n// \u2503 This file is part of the Perspective library, distributed under the terms \u2503\n// \u2503 of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). \u2503\n// \u2517\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u251B\n\n/**\n * An implementation of a Perspective Virtual Server for DuckDB.\n *\n * This import is optional, and so must be imported manually from either\n * `@perspective-dev/client/dist/esm/virtual_servers/duckdb.js` or\n * `@perspective-dev/client/src/ts/virtual_servers/duckdb.ts`, it is not\n * exported from the package root `@perspective-dev/client`\n *\n * @module\n */\n\nimport type * as perspective from \"@perspective-dev/client\";\nimport type { ColumnType } from \"@perspective-dev/client/dist/esm/ts-rs/ColumnType.d.ts\";\nimport type { ViewConfig } from \"@perspective-dev/client/dist/esm/ts-rs/ViewConfig.d.ts\";\nimport type { ViewConfigUpdate } from \"@perspective-dev/client/dist/esm/ts-rs/ViewConfigUpdate.d.ts\";\nimport type { ViewWindow } from \"@perspective-dev/client/dist/esm/ts-rs/ViewWindow.d.ts\";\nimport type { WindowAggSpec } from \"@perspective-dev/client/dist/esm/ts-rs/WindowAggSpec.d.ts\";\nimport type * as clickhouse from \"@clickhouse/client-web\";\n\nconst NUMBER_AGGS = [\n \"sum\",\n \"count\",\n \"any_value\",\n \"arbitrary\",\n \"array_agg\",\n \"avg\",\n \"bit_and\",\n \"bit_or\",\n \"bit_xor\",\n \"bitstring_agg\",\n \"bool_and\",\n \"bool_or\",\n \"countif\",\n \"favg\",\n \"fsum\",\n \"geomean\",\n \"kahan_sum\",\n \"last\",\n \"max\",\n \"min\",\n \"product\",\n \"string_agg\",\n \"sumkahan\",\n];\n\nconst STRING_AGGS = [\n \"count\",\n \"any_value\",\n \"arbitrary\",\n \"first\",\n \"countif\",\n \"last\",\n \"string_agg\",\n];\n\n// Window functions. Renamed from Perspective's `stddev`/`var` to the SQL\n// standard spellings DuckDB and ClickHouse both accept, since the advertised\n// name is now emitted verbatim.\n//\n// NOTE: this set is inherited from the DuckDB handler and has NOT been audited\n// against a live ClickHouse - see the aggregate lists below, which have the\n// same problem. ClickHouse's own navigation functions are `lagInFrame` /\n// `leadInFrame`, and its ranking set differs; both need verifying before being\n// advertised here.\nconst FRAMES = [\"rows\", \"range\", \"cumulative\"];\n\nconst WINDOW_AGGREGATES: WindowAggSpec[] = [\n { name: \"sum\", frames: FRAMES, result_type: \"float\" },\n { name: \"avg\", frames: FRAMES, result_type: \"float\" },\n { name: \"count\", frames: FRAMES, result_type: \"float\" },\n { name: \"min\", frames: FRAMES },\n { name: \"max\", frames: FRAMES },\n { name: \"stddev_samp\", frames: FRAMES, result_type: \"float\" },\n { name: \"var_samp\", frames: FRAMES, result_type: \"float\" },\n { name: \"lag\", offset: true },\n { name: \"lead\", offset: true },\n { name: \"diff\", offset: true, result_type: \"float\" },\n { name: \"rate\", frames: [\"range\"], result_type: \"float\" },\n];\n\nconst WINDOW_AGGREGATES_ANY: WindowAggSpec[] = [\n { name: \"count\", frames: FRAMES, result_type: \"float\" },\n { name: \"min\", frames: FRAMES },\n { name: \"max\", frames: FRAMES },\n { name: \"lag\", offset: true },\n { name: \"lead\", offset: true },\n];\n\nconst FILTER_OPS = [\n \"==\",\n \"!=\",\n \"IS DISTINCT FROM\",\n \"IS NOT DISTINCT FROM\",\n \">=\",\n \"<=\",\n \">\",\n \"<\",\n \"is null\",\n \"is not null\",\n];\n\n// Perspective's canonical string ops (translated to `ILIKE` / `match` by the\n// SQL builder), plus ClickHouse's raw infix pattern ops spliced verbatim.\nconst STRING_FILTER_OPS = [\n ...FILTER_OPS,\n \"begins with\",\n \"not begins with\",\n \"contains\",\n \"not contains\",\n \"ends with\",\n \"not ends with\",\n \"matches\",\n \"not matches\",\n \"in\",\n \"not in\",\n \"LIKE\",\n \"NOT LIKE\",\n \"ILIKE\",\n \"NOT ILIKE\",\n];\n\nfunction duckdbTypeToPsp(name: string): ColumnType {\n if (name.startsWith(\"Nullable\")) {\n name = name.match(/Nullable\\((.+?)\\)/)![1];\n }\n\n if (name.startsWith(\"Array\")) {\n return \"string\";\n }\n\n if (name === \"Int64\" || name === \"UInt64\" || name === \"Float64\") {\n return \"float\";\n }\n\n if (name === \"String\") {\n return \"string\";\n }\n\n if (name === \"DateTime\") {\n return \"datetime\";\n }\n\n if (name === \"Date\") {\n return \"date\";\n }\n\n throw new Error(`Unknown type '${name}'`);\n}\n\nfunction convertDecimalToNumber(value: any, dtypeString: string) {\n if (!(value instanceof Uint32Array || value instanceof Int32Array)) {\n return value;\n }\n\n let bigIntValue = BigInt(0);\n for (let i = 0; i < value.length; i++) {\n bigIntValue |= BigInt(value[i]) << BigInt(i * 32);\n }\n\n const scaleMatch = dtypeString.match(/Decimal\\[\\d+e(\\d+)\\]/);\n if (scaleMatch) {\n const scale = parseInt(scaleMatch[1]);\n return Number(bigIntValue) / Math.pow(10, scale);\n } else {\n return Number(bigIntValue);\n }\n}\n\nclass Lock {\n lockPromise: Promise<void>;\n constructor() {\n this.lockPromise = Promise.resolve();\n }\n\n acquire() {\n let releaseLock: (value: void) => void;\n const newLockPromise: Promise<void> = new Promise((resolve) => {\n releaseLock = resolve;\n });\n\n const acquirePromise = this.lockPromise.then(() => releaseLock);\n this.lockPromise = newLockPromise;\n return acquirePromise;\n }\n}\n\nconst LOCK = new Lock();\n\nasync function runQuery(\n db: clickhouse.ClickHouseClient,\n query: string,\n options: { columns?: true; execute?: boolean },\n): Promise<{\n rows: any[];\n columns: string[];\n dtypes: string[];\n}>;\n\nasync function runQuery(\n db: clickhouse.ClickHouseClient,\n query: string,\n options?: { columns?: false; execute?: boolean },\n): Promise<any[]>;\n\nasync function runQuery(\n db: clickhouse.ClickHouseClient,\n query: string,\n options: { columns?: boolean; execute?: boolean } = {},\n) {\n query = query.replace(/\\s+/g, \" \").trim();\n const release = await LOCK.acquire();\n try {\n const result = await db.query({ query });\n if (!options.execute) {\n const { data, meta } =\n (await result.json()) as clickhouse.ResponseJSON<unknown>;\n\n if (options.columns) {\n return {\n rows: data,\n columns: meta!.map((f) => f.name),\n dtypes: meta!.map((f) => f.type),\n };\n }\n\n return data;\n }\n } catch (error) {\n console.error(\"Query error:\", error);\n console.error(\"Query:\", query);\n throw error;\n } finally {\n release();\n }\n}\n\n/**\n * An implementation of Perspective's Virtual Server for `@duckdb/duckdb-wasm`.\n */\nexport class ClickhouseHandler implements perspective.VirtualServerHandler {\n private db: clickhouse.ClickHouseClient;\n private sqlBuilder: perspective.GenericSQLVirtualServerModel;\n constructor(db: clickhouse.ClickHouseClient, mod?: typeof perspective) {\n if (!mod) {\n if (customElements) {\n const viewer_class: any =\n customElements.get(\"perspective-viewer\");\n if (viewer_class) {\n mod = viewer_class.__wasm_module__;\n } else {\n throw new Error(\"Missing perspective-client.wasm\");\n }\n } else {\n }\n }\n\n this.db = db;\n this.sqlBuilder = new mod!.GenericSQLVirtualServerModel({\n create_entity: \"VIEW\",\n grouping_fn: \"GROUPING\",\n column_separator: \"|\",\n backslash_escaped_literals: true,\n regex_fn: \"match\",\n });\n }\n\n getFeatures(): perspective.Features {\n return {\n group_by: true,\n split_by: false,\n sort: true,\n expressions: true,\n // ClickHouse has no stable `rowid`, so natural-order windows\n // are unsupported.\n unordered: true,\n window_aggregates: {\n // `ema` is recursive and has no SQL window translation.\n integer: WINDOW_AGGREGATES,\n float: WINDOW_AGGREGATES,\n string: WINDOW_AGGREGATES_ANY,\n date: WINDOW_AGGREGATES_ANY,\n datetime: WINDOW_AGGREGATES_ANY,\n boolean: WINDOW_AGGREGATES_ANY,\n },\n group_rollup_mode: [\"rollup\", \"flat\", \"total\"],\n filter_ops: {\n integer: FILTER_OPS,\n float: FILTER_OPS,\n string: STRING_FILTER_OPS,\n boolean: FILTER_OPS,\n date: FILTER_OPS,\n datetime: FILTER_OPS,\n },\n aggregates: {\n integer: NUMBER_AGGS,\n float: NUMBER_AGGS,\n string: STRING_AGGS,\n boolean: STRING_AGGS,\n date: STRING_AGGS,\n datetime: STRING_AGGS,\n },\n };\n }\n\n async getHostedTables() {\n const query = \"SHOW TABLES\";\n const results = await runQuery(this.db, query);\n return results.map((row) => {\n return `${row.name}`;\n });\n }\n\n async tableSchema(tableId: string, config?: ViewConfig) {\n const query = this.sqlBuilder.tableSchema(tableId);\n const results = await runQuery(this.db, query);\n const schema = {} as Record<string, ColumnType>;\n for (const result of results) {\n const colName = result.name;\n if (!colName.startsWith(\"__\")) {\n schema[colName] = duckdbTypeToPsp(result.type) as ColumnType;\n }\n }\n\n return schema;\n }\n\n async viewColumnSize(viewId: string, config: ViewConfig) {\n const query = `SELECT COUNT() FROM system.columns WHERE table = '${viewId}'`;\n const results = await runQuery(this.db, query);\n const gs = config.group_by?.length || 0;\n const count = Number(results[0][\"COUNT()\"]);\n console.log(count);\n return (\n count -\n (gs === 0 ? 0 : gs + (config.split_by?.length === 0 ? 1 : 0))\n );\n }\n\n async tableSize(tableId: string) {\n const query = this.sqlBuilder.tableSize(tableId);\n const results = await runQuery(this.db, query);\n return Number(results[0][\"COUNT()\"]);\n }\n\n async tableMakeView(\n tableId: string,\n viewId: string,\n config: ViewConfigUpdate,\n ) {\n const query = this.sqlBuilder.tableMakeView(tableId, viewId, config);\n await runQuery(this.db, query, { execute: true });\n }\n\n async tableValidateExpression(tableId: string, expression: string) {\n const query = this.sqlBuilder.tableValidateExpression(\n tableId,\n expression,\n );\n const results = await runQuery(this.db, query);\n return duckdbTypeToPsp(results[0][\"type\"]) as ColumnType;\n }\n\n async viewDelete(viewId: string) {\n const query = this.sqlBuilder.viewDelete(viewId);\n await runQuery(this.db, query, { execute: true });\n }\n\n async viewGetData(\n viewId: string,\n config: ViewConfig,\n schema: Record<string, ColumnType>,\n viewport: ViewWindow,\n dataSlice: perspective.VirtualDataSlice,\n ) {\n const query = this.sqlBuilder.viewGetData(\n viewId,\n config,\n viewport,\n schema,\n );\n\n const { rows, columns, dtypes } = await runQuery(this.db, query, {\n columns: true,\n });\n\n for (let cidx = 0; cidx < columns.length; cidx++) {\n const col = columns[cidx];\n const dtype = duckdbTypeToPsp(dtypes[cidx]) as ColumnType;\n\n const isDecimal = dtypes[cidx].startsWith(\"Decimal\");\n for (let ridx = 0; ridx < rows.length; ridx++) {\n const row = rows[ridx];\n const grouping_id = row[\"__GROUPING_ID__\"];\n let value = row[col];\n if (isDecimal) {\n value = convertDecimalToNumber(value, dtypes[cidx]);\n }\n\n if (typeof value === \"bigint\") {\n value = Number(value);\n }\n\n if (dtype === \"datetime\" && typeof value === \"string\") {\n value = +new Date(value);\n }\n\n if (dtype === \"string\" && typeof value !== \"string\") {\n value = `${value}`;\n }\n\n dataSlice.setCol(dtype, col, ridx, value, grouping_id);\n }\n }\n }\n}\n"],
|
|
5
|
+
"mappings": "AA+BA,IAAMA,EAAc,CAChB,MACA,QACA,YACA,YACA,YACA,MACA,UACA,SACA,UACA,gBACA,WACA,UACA,UACA,OACA,OACA,UACA,YACA,OACA,MACA,MACA,UACA,aACA,UACJ,EAEMC,EAAc,CAChB,QACA,YACA,YACA,QACA,UACA,OACA,YACJ,EAWMC,EAAS,CAAC,OAAQ,QAAS,YAAY,EAEvCC,EAAqC,CACvC,CAAE,KAAM,MAAO,OAAQD,EAAQ,YAAa,OAAQ,EACpD,CAAE,KAAM,MAAO,OAAQA,EAAQ,YAAa,OAAQ,EACpD,CAAE,KAAM,QAAS,OAAQA,EAAQ,YAAa,OAAQ,EACtD,CAAE,KAAM,MAAO,OAAQA,CAAO,EAC9B,CAAE,KAAM,MAAO,OAAQA,CAAO,EAC9B,CAAE,KAAM,cAAe,OAAQA,EAAQ,YAAa,OAAQ,EAC5D,CAAE,KAAM,WAAY,OAAQA,EAAQ,YAAa,OAAQ,EACzD,CAAE,KAAM,MAAO,OAAQ,EAAK,EAC5B,CA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AAQD,EAAuBC,EAAOuC,EAAOC,CAAI,CAAC,GAGlD,OAAOxC,GAAU,WACjBA,EAAQ,OAAOA,CAAK,GAGpB0C,IAAU,YAAc,OAAO1C,GAAU,WACzCA,EAAQ,CAAC,IAAI,KAAKA,CAAK,GAGvB0C,IAAU,UAAY,OAAO1C,GAAU,WACvCA,EAAQ,GAAGA,CAAK,IAGpBoC,EAAU,OAAOM,EAAOD,EAAKG,EAAM5C,EAAO6C,CAAW,CACzD,CACJ,CACJ,CACJ",
|
|
6
|
+
"names": ["NUMBER_AGGS", "STRING_AGGS", "FRAMES", "WINDOW_AGGREGATES", "WINDOW_AGGREGATES_ANY", "FILTER_OPS", "STRING_FILTER_OPS", "duckdbTypeToPsp", "name", "convertDecimalToNumber", "value", "dtypeString", "bigIntValue", "i", "scaleMatch", "scale", "Lock", "releaseLock", "newLockPromise", "resolve", "acquirePromise", "LOCK", "runQuery", "db", "query", "options", "release", "result", "data", "meta", "f", "error", "ClickhouseHandler", "mod", "viewer_class", "row", "tableId", "config", "results", "schema", "colName", "viewId", "gs", "count", "expression", "viewport", "dataSlice", "rows", "columns", "dtypes", "cidx", "col", "dtype", "isDecimal", "ridx", "grouping_id"]
|
|
7
7
|
}
|
|
@@ -11,7 +11,9 @@
|
|
|
11
11
|
import type * as perspective from "@perspective-dev/client";
|
|
12
12
|
import type { ColumnType } from "@perspective-dev/client/dist/esm/ts-rs/ColumnType.d.ts";
|
|
13
13
|
import type { ViewConfig } from "@perspective-dev/client/dist/esm/ts-rs/ViewConfig.d.ts";
|
|
14
|
+
import type { ViewConfigUpdate } from "@perspective-dev/client/dist/esm/ts-rs/ViewConfigUpdate.d.ts";
|
|
14
15
|
import type { ViewWindow } from "@perspective-dev/client/dist/esm/ts-rs/ViewWindow.d.ts";
|
|
16
|
+
import type { Scalar } from "@perspective-dev/client/dist/esm/ts-rs/Scalar.d.ts";
|
|
15
17
|
import type * as duckdb from "@duckdb/duckdb-wasm";
|
|
16
18
|
/**
|
|
17
19
|
* An implementation of Perspective's Virtual Server for `@duckdb/duckdb-wasm`.
|
|
@@ -20,39 +22,17 @@ export declare class DuckDBHandler implements perspective.VirtualServerHandler {
|
|
|
20
22
|
private db;
|
|
21
23
|
private sqlBuilder;
|
|
22
24
|
constructor(db: duckdb.AsyncDuckDBConnection, mod?: typeof perspective);
|
|
23
|
-
getFeatures():
|
|
24
|
-
group_by: boolean;
|
|
25
|
-
split_by: boolean;
|
|
26
|
-
sort: boolean;
|
|
27
|
-
expressions: boolean;
|
|
28
|
-
group_rollup_mode: string[];
|
|
29
|
-
filter_ops: {
|
|
30
|
-
integer: string[];
|
|
31
|
-
float: string[];
|
|
32
|
-
string: string[];
|
|
33
|
-
boolean: string[];
|
|
34
|
-
date: string[];
|
|
35
|
-
datetime: string[];
|
|
36
|
-
};
|
|
37
|
-
aggregates: {
|
|
38
|
-
integer: string[];
|
|
39
|
-
float: string[];
|
|
40
|
-
string: string[];
|
|
41
|
-
boolean: string[];
|
|
42
|
-
date: string[];
|
|
43
|
-
datetime: string[];
|
|
44
|
-
};
|
|
45
|
-
};
|
|
25
|
+
getFeatures(): perspective.Features;
|
|
46
26
|
getHostedTables(): Promise<string[]>;
|
|
47
27
|
tableSchema(tableId: string, config?: ViewConfig): Promise<Record<string, perspective.ColumnType>>;
|
|
48
28
|
viewColumnSize(viewId: string, config: ViewConfig): Promise<number>;
|
|
49
29
|
tableSize(tableId: string): Promise<number>;
|
|
50
|
-
tableMakeView(tableId: string, viewId: string, config:
|
|
30
|
+
tableMakeView(tableId: string, viewId: string, config: ViewConfigUpdate): Promise<void>;
|
|
51
31
|
tableValidateExpression(tableId: string, expression: string): Promise<perspective.ColumnType>;
|
|
52
32
|
viewDelete(viewId: string): Promise<void>;
|
|
53
33
|
viewGetMinMax(viewId: string, columnName: string, config: ViewConfig): Promise<{
|
|
54
|
-
min:
|
|
55
|
-
max:
|
|
34
|
+
min: Scalar;
|
|
35
|
+
max: Scalar;
|
|
56
36
|
}>;
|
|
57
37
|
viewGetData(viewId: string, config: ViewConfig, schema: Record<string, ColumnType>, viewport: ViewWindow, dataSlice: perspective.VirtualDataSlice): Promise<void>;
|
|
58
38
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var d=["sum","count","any_value","arbitrary","array_agg","avg","bit_and","bit_or","bit_xor","bitstring_agg","bool_and","bool_or","countif","favg","fsum","geomean","kahan_sum","last","max","min","product","string_agg","sumkahan"],p=["count","any_value","arbitrary","first","countif","last","string_agg"],n=["rows","range","cumulative"],y=[{name:"sum",frames:n,result_type:"float"},{name:"avg",frames:n,result_type:"float"},{name:"count",frames:n,result_type:"float"},{name:"min",frames:n},{name:"max",frames:n},{name:"product",frames:n,result_type:"float"},{name:"median",frames:n,result_type:"float"},{name:"stddev_samp",frames:n,result_type:"float"},{name:"stddev_pop",frames:n,result_type:"float"},{name:"var_samp",frames:n,result_type:"float"},{name:"var_pop",frames:n,result_type:"float"},{name:"first_value",frames:n},{name:"last_value",frames:n},{name:"nth_value",frames:n,offset:!0},{name:"lag",offset:!0},{name:"lead",offset:!0},{name:"row_number",result_type:"float"},{name:"rank",result_type:"float"},{name:"dense_rank",result_type:"float"},{name:"percent_rank",result_type:"float"},{name:"cume_dist",result_type:"float"},{name:"ntile",offset:!0,result_type:"float"},{name:"diff",offset:!0,result_type:"float"},{name:"rate",frames:["range"],result_type:"float"}],f=[{name:"count",frames:n,result_type:"float"},{name:"min",frames:n},{name:"max",frames:n},{name:"first_value",frames:n},{name:"last_value",frames:n},{name:"nth_value",frames:n,offset:!0},{name:"lag",offset:!0},{name:"lead",offset:!0},{name:"row_number",result_type:"float"},{name:"rank",result_type:"float"},{name:"dense_rank",result_type:"float"},{name:"percent_rank",result_type:"float"},{name:"cume_dist",result_type:"float"},{name:"ntile",offset:!0,result_type:"float"}],m=["==","!=","IS DISTINCT FROM","IS NOT DISTINCT FROM",">=","<=",">","<","is null","is not null"],w=[...m,"begins with","not begins with","contains","not contains","ends with","not ends with","matches","not matches","in","not in","LIKE","NOT LIKE","ILIKE","NOT ILIKE"];function g(t){return t=t.toLowerCase(),t.startsWith("bool")?"boolean":["tinyint","smallint","integer","utinyint","usmallint"].includes(t)||["int8","int16","int32","uint8","uint16"].includes(t)?"integer":["bigint","hugeint","uhugeint","uinteger","ubigint"].includes(t)||["float","real","double","varint"].includes(t)||["int64","uint32","uint64","float32","float64"].includes(t)||t.startsWith("decimal")||t.startsWith("numeric")?"float":t.startsWith("date")?"date":t.startsWith("time")?"datetime":(t.startsWith("varchar")||t==="utf8"||t.startsWith("enum")||["json","uuid","blob","bit","interval"].includes(t)||t.startsWith("struct")||t.startsWith("map")||t.startsWith("union")||t.endsWith("[]")||console.warn(`Unknown type '${t}'`),"string")}async function u(t,s,r={}){s=s.replace(/\s+/g," ").trim();try{let e=await t.query(s);return r.columns?{rows:e.toArray(),columns:e.schema.fields.map(i=>i.name),dtypes:e.schema.fields.map(i=>i.type.toString())}:e.toArray()}catch(e){throw console.error("Query error:",e),console.error("Query:",s),e}}var _=class{db;sqlBuilder;constructor(s,r){if(!r&&customElements){let e=customElements.get("perspective-viewer");if(e)r=e.__wasm_module__;else throw new Error("Missing perspective-client.wasm")}this.db=s,this.sqlBuilder=new r.GenericSQLVirtualServerModel({column_separator:"|",like_escape_clause:"\\",regex_fn:"regexp_matches"})}getFeatures(){return{group_by:!0,split_by:!0,sort:!0,expressions:!0,window_aggregates:{integer:y,float:y,string:f,date:f,datetime:f,boolean:f},group_rollup_mode:["rollup","flat","total"],split_rollup_mode:["flat","rollup"],filter_ops:{integer:m,float:m,string:w,boolean:m,date:m,datetime:m},aggregates:{integer:d,float:d,string:p,boolean:p,date:p,datetime:p}}}async getHostedTables(){let s=this.sqlBuilder.getHostedTables();return(await u(this.db,s)).map(e=>{let i=e.toJSON();return`${i.database||"memory"}.${i.name}`})}async tableSchema(s,r){let e=this.sqlBuilder.tableSchema(s),i=await u(this.db,e),c={};for(let o of i){let a=o.toJSON(),l=a.column_name;l.startsWith("__")||(c[l]=g(a.column_type))}return c}async viewColumnSize(s,r){let e=this.sqlBuilder.viewColumnSize(s),i=await u(this.db,e),c=Number(Object.values(i[0].toJSON())[0]),o=r.group_by?.length||0,a=r.group_rollup_mode==="flat";return c-(o===0?0:a?o:o+1)}async tableSize(s){let r=this.sqlBuilder.tableSize(s),e=await u(this.db,r);return Number(e[0].toJSON()["count_star()"])}async tableMakeView(s,r,e){let i=this.sqlBuilder.tableMakeView(s,r,e);await u(this.db,i)}async tableValidateExpression(s,r){let e=this.sqlBuilder.tableValidateExpression(s,r),i=await u(this.db,e);return g(i[0].toJSON().column_type)}async viewDelete(s){let r=this.sqlBuilder.viewDelete(s);await u(this.db,r)}async viewGetMinMax(s,r,e){let i=this.sqlBuilder.viewGetMinMax(s,r,e),o=(await u(this.db,i))[0].toJSON(),[a,l]=Object.values(o);return typeof a=="bigint"&&(a=Number(a)),typeof l=="bigint"&&(l=Number(l)),{min:a??null,max:l??null}}async viewGetData(s,r,e,i,c){let o=this.sqlBuilder.viewGetData(s,r,i,e),a=await this.db.useUnsafe((l,b)=>l.runQuery(b,o));c.fromArrowIpc(a)}};export{_ as DuckDBHandler};
|
|
2
2
|
//# sourceMappingURL=duckdb.js.map
|