@perspective-dev/client 5.6.1 → 5.7.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 +664 -35
- 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 +37 -0
- 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 +14 -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/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 +121 -54
- package/dist/wasm/perspective-js.js +123 -20
- package/dist/wasm/perspective-js.wasm +0 -0
- package/dist/wasm/perspective-js.wasm.d.ts +10 -6
- package/dist/wasm/perspective-migrate-js.d.ts +41 -0
- package/dist/wasm/perspective-migrate-js.js +524 -0
- package/dist/wasm/perspective-migrate-js.wasm +0 -0
- package/dist/wasm/perspective-migrate-js.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/ViewOnUpdateResp.ts → dist/ts-rs/ViewOnUpdateResp.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
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
/**
|
|
3
|
+
* The public types of the `perspective-js` wasm-bindgen module, without its
|
|
4
|
+
* loader internals.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
export type { Client, ExpressionError, GenericSQLVirtualServerModel, ProxySession, Table, TableDescription, View, VirtualDataSlice, VirtualHostedTable, VirtualServer, VirtualServerHandler, } from "../../dist/wasm/perspective-js.d.ts";
|
|
9
|
+
export type * from "../../dist/ts-rs/AggSpec.d.ts";
|
|
10
|
+
export type * from "../../dist/ts-rs/ColumnType.d.ts";
|
|
11
|
+
export type * from "../../dist/ts-rs/ColumnWindow.d.ts";
|
|
12
|
+
export type * from "../../dist/ts-rs/DeleteOptions.d.ts";
|
|
13
|
+
export type * from "../../dist/ts-rs/Features.d.ts";
|
|
14
|
+
export type * from "../../dist/ts-rs/Filter.d.ts";
|
|
15
|
+
export type * from "../../dist/ts-rs/JoinOptions.d.ts";
|
|
16
|
+
export type * from "../../dist/ts-rs/JoinType.d.ts";
|
|
17
|
+
export type * from "../../dist/ts-rs/OnRemoveData.d.ts";
|
|
18
|
+
export type * from "../../dist/ts-rs/OnUpdateData.d.ts";
|
|
19
|
+
export type * from "../../dist/ts-rs/OnUpdateOptions.d.ts";
|
|
20
|
+
export type * from "../../dist/ts-rs/Scalar.d.ts";
|
|
21
|
+
export type * from "../../dist/ts-rs/SortDir.d.ts";
|
|
22
|
+
export type * from "../../dist/ts-rs/SystemInfo.d.ts";
|
|
23
|
+
export type * from "../../dist/ts-rs/TableInitOptions.d.ts";
|
|
24
|
+
export type * from "../../dist/ts-rs/TypedArrayWindow.d.ts";
|
|
25
|
+
export type * from "../../dist/ts-rs/UpdateOptions.d.ts";
|
|
26
|
+
export type * from "../../dist/ts-rs/ViewConfig.d.ts";
|
|
27
|
+
export type * from "../../dist/ts-rs/ViewConfigUpdate.d.ts";
|
|
28
|
+
export type * from "../../dist/ts-rs/ViewOnRemoveResp.d.ts";
|
|
29
|
+
export type * from "../../dist/ts-rs/ViewOnUpdateResp.d.ts";
|
|
30
|
+
export type * from "../../dist/ts-rs/ViewWindow.d.ts";
|
|
31
|
+
export type * from "../../dist/ts-rs/WindowAggSpec.d.ts";
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An implementation of a Perspective Virtual Server for ClickHouse.
|
|
3
|
+
*
|
|
4
|
+
* This import is optional, and so must be imported manually from
|
|
5
|
+
* `@perspective-dev/client/virtual_servers/clickhouse`, it is not exported from
|
|
6
|
+
* the package root `@perspective-dev/client`
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import type * as perspective from "@perspective-dev/client";
|
|
11
|
+
import type { ColumnType } from "../../../dist/ts-rs/ColumnType.d.ts";
|
|
12
|
+
import type { ViewConfig } from "../../../dist/ts-rs/ViewConfig.d.ts";
|
|
13
|
+
import type { TableDescription } from "@perspective-dev/client";
|
|
14
|
+
import type { ViewConfigUpdate } from "../../../dist/ts-rs/ViewConfigUpdate.d.ts";
|
|
15
|
+
import type { ViewWindow } from "../../../dist/ts-rs/ViewWindow.d.ts";
|
|
16
|
+
import type * as clickhouse from "@clickhouse/client-web";
|
|
17
|
+
/**
|
|
18
|
+
* An implementation of Perspective's Virtual Server for `@duckdb/duckdb-wasm`.
|
|
19
|
+
*/
|
|
20
|
+
export declare class ClickhouseHandler implements perspective.VirtualServerHandler {
|
|
21
|
+
private db;
|
|
22
|
+
private sqlBuilder;
|
|
23
|
+
constructor(db: clickhouse.ClickHouseClient, mod?: typeof perspective);
|
|
24
|
+
getFeatures(): perspective.Features;
|
|
25
|
+
getHostedTables(): Promise<string[]>;
|
|
26
|
+
tableSchema(tableId: string, config?: ViewConfig): Promise<Record<string, perspective.ColumnType>>;
|
|
27
|
+
viewColumnSize(viewId: string, config: ViewConfig): Promise<number>;
|
|
28
|
+
tableSize(tableId: string): Promise<number>;
|
|
29
|
+
tableMakeView(tableId: string, viewId: string, config: ViewConfigUpdate): Promise<void>;
|
|
30
|
+
tableDescribe(tableId: string, config: ViewConfig): Promise<TableDescription>;
|
|
31
|
+
/** The planned result columns of one `DESCRIBE` query. */
|
|
32
|
+
private describeQuery;
|
|
33
|
+
/** Plans each expression on its own to name the ones at fault. */
|
|
34
|
+
private attributeExpressionErrors;
|
|
35
|
+
viewDelete(viewId: string): Promise<void>;
|
|
36
|
+
viewGetData(viewId: string, config: ViewConfig, schema: Record<string, ColumnType>, viewport: ViewWindow, dataSlice: perspective.VirtualDataSlice): Promise<void>;
|
|
37
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
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"],y=["count","any_value","arbitrary","first","countif","last","string_agg"],l=["rows","range","cumulative"],
|
|
1
|
+
import{client_runtime as N}from"../wasm/registry.js";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"],y=["count","any_value","arbitrary","first","countif","last","string_agg"],l=["rows","range","cumulative"],k=[{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"},{name:"row_number",result_type:"float"}],h=[{name:"count",frames:l,result_type:"float"},{name:"min",frames:l},{name:"max",frames:l},{name:"lag",offset:!0},{name:"lead",offset:!0},{name:"row_number",result_type:"float"}],g=["==","!=","IS DISTINCT FROM","IS NOT DISTINCT FROM",">=","<=",">","<","is null","is not null"],q=[...g,"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 w(n){if(n.startsWith("Nullable")&&(n=n.match(/Nullable\((.+?)\)/)[1]),n.startsWith("Array"))return"string";if(n==="Int64"||n==="UInt64"||n==="Float64")return"float";if(n==="String")return"string";if(n==="DateTime")return"datetime";if(n==="Date")return"date";throw new Error(`Unknown type '${n}'`)}function D(n,e){if(!(n instanceof Uint32Array||n instanceof Int32Array))return n;let t=BigInt(0);for(let s=0;s<n.length;s++)t|=BigInt(n[s])<<BigInt(s*32);let r=e.match(/Decimal\[\d+e(\d+)\]/);if(r){let s=parseInt(r[1]);return Number(t)/Math.pow(10,s)}else return Number(t)}var C=class{lockPromise;constructor(){this.lockPromise=Promise.resolve()}acquire(){let e,t=new Promise(s=>{e=s}),r=this.lockPromise.then(()=>e);return this.lockPromise=t,r}},O=new C;async function u(n,e,t={}){e=e.replace(/\s+/g," ").trim();let r=await O.acquire();try{let s=await n.query({query:e});if(!t.execute){let{data:i,meta:o}=await s.json();return t.columns?{rows:i,columns:o.map(a=>a.name),dtypes:o.map(a=>a.type)}:i}}catch(s){throw console.error("Query error:",s),console.error("Query:",e),s}finally{r()}}var E=class{db;sqlBuilder;constructor(e,t){if(t??=N()?.glue,t===void 0)throw new Error("perspective-js is not initialized yet: construct a ClickhouseHandler once <perspective-viewer> is ready or a Client exists, or pass the wasm module");this.db=e,this.sqlBuilder=new t.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:k,float:k,string:h,date:h,datetime:h,boolean:h},group_rollup_mode:["rollup","flat","total"],filter_ops:{integer:g,float:g,string:q,boolean:g,date:g,datetime:g},aggregates:{integer:S,float:S,string:y,boolean:y,date:y,datetime:y}}}async getHostedTables(){return(await u(this.db,"SHOW TABLES")).map(r=>`${r.name}`)}async tableSchema(e,t){let r=this.sqlBuilder.tableSchema(e),s=await u(this.db,r),i={};for(let o of s){let a=o.name;a.startsWith("__")||(i[a]=w(o.type))}return i}async viewColumnSize(e,t){let r=`SELECT COUNT() FROM system.columns WHERE table = '${e}'`,s=await u(this.db,r),i=t.group_by?.length||0,o=Number(s[0]["COUNT()"]);return console.log(o),o-(i===0?0:i+(t.split_by?.length===0?1:0))}async tableSize(e){let t=this.sqlBuilder.tableSize(e),r=await u(this.db,t);return Number(r[0]["COUNT()"])}async tableMakeView(e,t,r){let s=Object.keys(r.windows??{}).length?await this.tableSchema(e):void 0,i=this.sqlBuilder.tableMakeView(e,t,r,s);await u(this.db,i,{execute:!0})}async tableDescribe(e,t){let r={},s=this.sqlBuilder.expressionsDescribe(e,t);if(s!==void 0)try{r=await this.describeQuery(s)}catch(m){return await this.attributeExpressionErrors(e,t,m)}let i=Object.keys(t.windows??{}).length?await this.tableSchema(e):void 0,o=this.sqlBuilder.tableDescribe(e,t,i),a={};if(o!==void 0)try{a=await this.describeQuery(o)}catch(m){return{config_error:_(m)}}return{expression_schema:r,view_schema:a}}async describeQuery(e){let t=await u(this.db,e),r={};for(let s of t)s.name.startsWith("__")||(r[s.name]=w(s.type));return r}async attributeExpressionErrors(e,t,r){let s={},i={};for(let[o,a]of Object.entries(t.expressions)){let m=this.sqlBuilder.expressionDescribe(e,a);try{let p=await u(this.db,m);s[o]=w(p[0].type)}catch(p){i[o]={error_message:_(p),line:0,column:0}}}return Object.keys(i).length===0?{config_error:_(r)}:{expression_schema:s,expression_errors:i}}async viewDelete(e){let t=this.sqlBuilder.viewDelete(e);await u(this.db,t,{execute:!0})}async viewGetData(e,t,r,s,i){let o=this.sqlBuilder.viewGetData(e,t,s,r),{rows:a,columns:m,dtypes:p}=await u(this.db,o,{columns:!0});for(let f=0;f<m.length;f++){let v=m[f],b=w(p[f]),x=p[f].startsWith("Decimal");for(let d=0;d<a.length;d++){let T=a[d],I=T.__GROUPING_ID__,c=T[v];x&&(c=D(c,p[f])),typeof c=="bigint"&&(c=Number(c)),b==="datetime"&&typeof c=="string"&&(c=+new Date(c)),b==="string"&&typeof c!="string"&&(c=`${c}`),i.setCol(b,v,d,c,I)}}}};function _(n){return n instanceof Error?n.message:String(n)}export{E 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 {\n ExpressionError,\n TableDescription,\n} from \"@perspective-dev/client\";\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 { name: \"row_number\", 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 { name: \"row_number\", result_type: \"float\" },\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 // Window order keys need column types for `range` frame emission.\n const schema = Object.keys(config.windows ?? {}).length\n ? await this.tableSchema(tableId)\n : undefined;\n\n const query = this.sqlBuilder.tableMakeView(\n tableId,\n viewId,\n config,\n schema,\n );\n\n await runQuery(this.db, query, { execute: true });\n }\n\n async tableDescribe(\n tableId: string,\n config: ViewConfig,\n ): Promise<TableDescription> {\n let expression_schema = {} as Record<string, ColumnType>;\n const expressions_query = this.sqlBuilder.expressionsDescribe(\n tableId,\n config,\n );\n\n if (expressions_query !== undefined) {\n try {\n expression_schema = await this.describeQuery(expressions_query);\n } catch (error) {\n return await this.attributeExpressionErrors(\n tableId,\n config,\n error,\n );\n }\n }\n\n const schema = Object.keys(config.windows ?? {}).length\n ? await this.tableSchema(tableId)\n : undefined;\n\n const view_query = this.sqlBuilder.tableDescribe(\n tableId,\n config,\n schema,\n );\n\n let view_schema = {} as Record<string, ColumnType>;\n if (view_query !== undefined) {\n try {\n view_schema = await this.describeQuery(view_query);\n } catch (error) {\n return { config_error: errorMessage(error) };\n }\n }\n\n return { expression_schema, view_schema };\n }\n\n /** The planned result columns of one `DESCRIBE` query. */\n private async describeQuery(query: string) {\n const results = await runQuery(this.db, query);\n const schema = {} as Record<string, ColumnType>;\n for (const result of results) {\n if (!result.name.startsWith(\"__\")) {\n schema[result.name] = duckdbTypeToPsp(\n result.type,\n ) as ColumnType;\n }\n }\n\n return schema;\n }\n\n /** Plans each expression on its own to name the ones at fault. */\n private async attributeExpressionErrors(\n tableId: string,\n config: ViewConfig,\n error: unknown,\n ): Promise<TableDescription> {\n const expression_schema = {} as Record<string, ColumnType>;\n const expression_errors = {} as Record<string, ExpressionError>;\n for (const [name, expression] of Object.entries(config.expressions)) {\n const query = this.sqlBuilder.expressionDescribe(\n tableId,\n expression,\n );\n\n try {\n const results = await runQuery(this.db, query);\n expression_schema[name] = duckdbTypeToPsp(\n results[0][\"type\"],\n ) as ColumnType;\n } catch (error) {\n expression_errors[name] = {\n error_message: errorMessage(error),\n line: 0,\n column: 0,\n };\n }\n }\n\n if (Object.keys(expression_errors).length === 0) {\n return { config_error: errorMessage(error) };\n }\n\n return { expression_schema, expression_errors };\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\nfunction errorMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n"],
|
|
5
|
-
"mappings": "
|
|
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", "
|
|
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 ClickHouse.\n *\n * This import is optional, and so must be imported manually from\n * `@perspective-dev/client/virtual_servers/clickhouse`, it is not exported from\n * the package root `@perspective-dev/client`\n *\n * @module\n */\n\nimport type * as perspective from \"@perspective-dev/client\";\nimport { client_runtime } from \"../wasm/registry.ts\";\nimport type { ColumnType } from \"../../../dist/ts-rs/ColumnType.d.ts\";\nimport type { ViewConfig } from \"../../../dist/ts-rs/ViewConfig.d.ts\";\nimport type {\n ExpressionError,\n TableDescription,\n} from \"@perspective-dev/client\";\nimport type { ViewConfigUpdate } from \"../../../dist/ts-rs/ViewConfigUpdate.d.ts\";\nimport type { ViewWindow } from \"../../../dist/ts-rs/ViewWindow.d.ts\";\nimport type { WindowAggSpec } from \"../../../dist/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 { name: \"row_number\", 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 { name: \"row_number\", result_type: \"float\" },\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 mod ??= client_runtime()?.glue as unknown as\n | typeof perspective\n | undefined;\n if (mod === undefined) {\n throw new Error(\n \"perspective-js is not initialized yet: construct a ClickhouseHandler once <perspective-viewer> is ready or a Client exists, or pass the wasm module\",\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 // Window order keys need column types for `range` frame emission.\n const schema = Object.keys(config.windows ?? {}).length\n ? await this.tableSchema(tableId)\n : undefined;\n\n const query = this.sqlBuilder.tableMakeView(\n tableId,\n viewId,\n config,\n schema,\n );\n\n await runQuery(this.db, query, { execute: true });\n }\n\n async tableDescribe(\n tableId: string,\n config: ViewConfig,\n ): Promise<TableDescription> {\n let expression_schema = {} as Record<string, ColumnType>;\n const expressions_query = this.sqlBuilder.expressionsDescribe(\n tableId,\n config,\n );\n\n if (expressions_query !== undefined) {\n try {\n expression_schema = await this.describeQuery(expressions_query);\n } catch (error) {\n return await this.attributeExpressionErrors(\n tableId,\n config,\n error,\n );\n }\n }\n\n const schema = Object.keys(config.windows ?? {}).length\n ? await this.tableSchema(tableId)\n : undefined;\n\n const view_query = this.sqlBuilder.tableDescribe(\n tableId,\n config,\n schema,\n );\n\n let view_schema = {} as Record<string, ColumnType>;\n if (view_query !== undefined) {\n try {\n view_schema = await this.describeQuery(view_query);\n } catch (error) {\n return { config_error: errorMessage(error) };\n }\n }\n\n return { expression_schema, view_schema };\n }\n\n /** The planned result columns of one `DESCRIBE` query. */\n private async describeQuery(query: string) {\n const results = await runQuery(this.db, query);\n const schema = {} as Record<string, ColumnType>;\n for (const result of results) {\n if (!result.name.startsWith(\"__\")) {\n schema[result.name] = duckdbTypeToPsp(\n result.type,\n ) as ColumnType;\n }\n }\n\n return schema;\n }\n\n /** Plans each expression on its own to name the ones at fault. */\n private async attributeExpressionErrors(\n tableId: string,\n config: ViewConfig,\n error: unknown,\n ): Promise<TableDescription> {\n const expression_schema = {} as Record<string, ColumnType>;\n const expression_errors = {} as Record<string, ExpressionError>;\n for (const [name, expression] of Object.entries(config.expressions)) {\n const query = this.sqlBuilder.expressionDescribe(\n tableId,\n expression!,\n );\n\n try {\n const results = await runQuery(this.db, query);\n expression_schema[name] = duckdbTypeToPsp(\n results[0][\"type\"],\n ) as ColumnType;\n } catch (error) {\n expression_errors[name] = {\n error_message: errorMessage(error),\n line: 0,\n column: 0,\n };\n }\n }\n\n if (Object.keys(expression_errors).length === 0) {\n return { config_error: errorMessage(error) };\n }\n\n return { expression_schema, expression_errors };\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\nfunction errorMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);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ER_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", "row", "tableId", "config", "results", "schema", "colName", "viewId", "gs", "count", "expression_schema", "expressions_query", "view_query", "view_schema", "errorMessage", "expression_errors", "expression", "viewport", "dataSlice", "rows", "columns", "dtypes", "cidx", "col", "dtype", "isDecimal", "ridx", "grouping_id"]
|
|
7
7
|
}
|
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* An implementation of a Perspective Virtual Server for DuckDB.
|
|
3
3
|
*
|
|
4
|
-
* This import is optional, and so must be imported manually from
|
|
5
|
-
* `@perspective-dev/client/
|
|
6
|
-
* `@perspective-dev/client
|
|
7
|
-
* exported from the package root `@perspective-dev/client`
|
|
4
|
+
* This import is optional, and so must be imported manually from
|
|
5
|
+
* `@perspective-dev/client/virtual_servers/duckdb`, it is not exported from
|
|
6
|
+
* the package root `@perspective-dev/client`
|
|
8
7
|
*
|
|
9
8
|
* @module
|
|
10
9
|
*/
|
|
11
10
|
import type * as perspective from "@perspective-dev/client";
|
|
12
|
-
import type { ColumnType } from "
|
|
13
|
-
import type { ViewConfig } from "
|
|
11
|
+
import type { ColumnType } from "../../../dist/ts-rs/ColumnType.d.ts";
|
|
12
|
+
import type { ViewConfig } from "../../../dist/ts-rs/ViewConfig.d.ts";
|
|
14
13
|
import type { TableDescription } from "@perspective-dev/client";
|
|
15
|
-
import type { ViewConfigUpdate } from "
|
|
16
|
-
import type { ViewWindow } from "
|
|
17
|
-
import type { Scalar } from "
|
|
14
|
+
import type { ViewConfigUpdate } from "../../../dist/ts-rs/ViewConfigUpdate.d.ts";
|
|
15
|
+
import type { ViewWindow } from "../../../dist/ts-rs/ViewWindow.d.ts";
|
|
16
|
+
import type { Scalar } from "../../../dist/ts-rs/Scalar.d.ts";
|
|
18
17
|
import type * as duckdb from "@duckdb/duckdb-wasm";
|
|
19
18
|
/**
|
|
20
19
|
* An implementation of Perspective's Virtual Server for `@duckdb/duckdb-wasm`.
|
|
@@ -27,6 +26,12 @@ export declare class DuckDBHandler implements perspective.VirtualServerHandler {
|
|
|
27
26
|
getHostedTables(): Promise<string[]>;
|
|
28
27
|
tableSchema(tableId: string, config?: ViewConfig): Promise<Record<string, perspective.ColumnType>>;
|
|
29
28
|
viewColumnSize(viewId: string, config: ViewConfig): Promise<number>;
|
|
29
|
+
viewSize(viewId: string): Promise<number>;
|
|
30
|
+
viewExpand(viewId: string, config: ViewConfig, rowIndex: number): Promise<number>;
|
|
31
|
+
viewCollapse(viewId: string, config: ViewConfig, rowIndex: number): Promise<number>;
|
|
32
|
+
viewSetDepth(viewId: string, config: ViewConfig, depth: number): Promise<void>;
|
|
33
|
+
/** The single integer a `COUNT(*)` query or an `UPDATE` returns. */
|
|
34
|
+
private runCount;
|
|
30
35
|
tableSize(tableId: string): Promise<number>;
|
|
31
36
|
tableMakeView(tableId: string, viewId: string, config: ViewConfigUpdate): Promise<void>;
|
|
32
37
|
tableDescribe(tableId: string, config: ViewConfig): Promise<TableDescription>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var _=["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"],
|
|
1
|
+
import{client_runtime as h}from"../wasm/registry.js";var _=["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"],d=["count","any_value","arbitrary","first","countif","last","string_agg"],a=["rows","range","cumulative"],w=[{name:"sum",frames:a,result_type:"float"},{name:"avg",frames:a,result_type:"float"},{name:"count",frames:a,result_type:"float"},{name:"min",frames:a},{name:"max",frames:a},{name:"product",frames:a,result_type:"float"},{name:"median",frames:a,result_type:"float"},{name:"stddev_samp",frames:a,result_type:"float"},{name:"stddev_pop",frames:a,result_type:"float"},{name:"var_samp",frames:a,result_type:"float"},{name:"var_pop",frames:a,result_type:"float"},{name:"first_value",frames:a},{name:"last_value",frames:a},{name:"nth_value",frames:a,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:a,result_type:"float"},{name:"min",frames:a},{name:"max",frames:a},{name:"first_value",frames:a},{name:"last_value",frames:a},{name:"nth_value",frames:a,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"],v=[...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 y(r){return r=r.toLowerCase(),r.startsWith("bool")?"boolean":["tinyint","smallint","integer","utinyint","usmallint"].includes(r)||["int8","int16","int32","uint8","uint16"].includes(r)?"integer":["bigint","hugeint","uhugeint","uinteger","ubigint"].includes(r)||["float","real","double","varint","bignum"].includes(r)||["int64","uint32","uint64","float32","float64"].includes(r)||r.startsWith("decimal")||r.startsWith("numeric")?"float":r.startsWith("date")?"date":r.startsWith("time")?"datetime":(r.startsWith("varchar")||r==="utf8"||r.startsWith("enum")||["json","uuid","blob","bit","interval"].includes(r)||r.startsWith("struct")||r.startsWith("map")||r.startsWith("union")||r.endsWith("[]")||console.warn(`Unknown type '${r}'`),"string")}async function c(r,e,t={}){e=e.replace(/\s+/g," ").trim();try{let s=await r.query(e);return t.columns?{rows:s.toArray(),columns:s.schema.fields.map(n=>n.name),dtypes:s.schema.fields.map(n=>n.type.toString())}:s.toArray()}catch(s){throw console.error("Query error:",s),console.error("Query:",e),s}}var b=class{db;sqlBuilder;constructor(e,t){if(t??=h()?.glue,t===void 0)throw new Error("perspective-js is not initialized yet: construct a DuckDBHandler once <perspective-viewer> is ready or a Client exists, or pass the wasm module");this.db=e,this.sqlBuilder=new t.GenericSQLVirtualServerModel({column_separator:"|",like_escape_clause:"\\",regex_fn:"regexp_matches",expand_collapse:!0})}getFeatures(){return{group_by:!0,split_by:!0,sort:!0,expressions:!0,view_derivations:!0,expand_collapse:!0,window_aggregates:{integer:w,float:w,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:v,boolean:m,date:m,datetime:m},aggregates:{integer:_,float:_,string:d,boolean:d,date:d,datetime:d}}}async getHostedTables(){let e=this.sqlBuilder.getHostedTables();return(await c(this.db,e)).map(s=>{let n=s.toJSON();return`${n.database||"memory"}.${n.name}`})}async tableSchema(e,t){let s=this.sqlBuilder.tableSchema(e),n=await c(this.db,s),i={};for(let u of n){let o=u.toJSON(),l=o.column_name;l.startsWith("__")||(i[l]=y(o.column_type))}return i}async viewColumnSize(e,t){let s=this.sqlBuilder.viewColumnSize(e),n=await c(this.db,s),i=Number(Object.values(n[0].toJSON())[0]),u=t.group_by?.length||0,o=t.group_rollup_mode==="flat";return i-(u===0?0:o?u:u+1)-1}async viewSize(e){return await this.runCount(this.sqlBuilder.viewSize(e))}async viewExpand(e,t,s){return await this.runCount(this.sqlBuilder.viewExpand(e,t,s))}async viewCollapse(e,t,s){return await this.runCount(this.sqlBuilder.viewCollapse(e,t,s))}async viewSetDepth(e,t,s){await c(this.db,this.sqlBuilder.viewSetDepth(e,t,s))}async runCount(e){let t=await c(this.db,e);return Number(Object.values(t[0].toJSON())[0])}async tableSize(e){let t=this.sqlBuilder.tableSize(e),s=await c(this.db,t);return Number(s[0].toJSON()["count_star()"])}async tableMakeView(e,t,s){let n=Object.keys(s.windows??{}).length?await this.tableSchema(e):void 0,i=this.sqlBuilder.tableMakeView(e,t,s,n);await c(this.db,i)}async tableDescribe(e,t){let s={},n=this.sqlBuilder.expressionsDescribe(e,t);if(n!==void 0)try{s=await this.describeQuery(n)}catch(l){return await this.attributeExpressionErrors(e,t,l)}let i=Object.keys(t.windows??{}).length?await this.tableSchema(e):void 0,u=this.sqlBuilder.tableDescribe(e,t,i),o={};if(u!==void 0)try{o=await this.describeQuery(u)}catch(l){return{config_error:g(l)}}return{expression_schema:s,view_schema:o}}async describeQuery(e){let t=await c(this.db,e),s={};for(let n of t){let i=n.toJSON();i.column_name.startsWith("__")||(s[i.column_name]=y(i.column_type))}return s}async attributeExpressionErrors(e,t,s){let n={},i={};for(let[u,o]of Object.entries(t.expressions)){let l=this.sqlBuilder.expressionDescribe(e,o);try{let p=await c(this.db,l);n[u]=y(p[0].toJSON().column_type)}catch(p){i[u]={error_message:g(p),line:0,column:0}}}return Object.keys(i).length===0?{config_error:g(s)}:{expression_schema:n,expression_errors:i}}async viewMakeTable(e,t,s,n){let i=this.sqlBuilder.viewMakeTable(e,t,s,await this.tableSchema(e),n);await c(this.db,i)}async viewDelete(e){let t=this.sqlBuilder.viewDelete(e);await c(this.db,t)}async viewGetMinMax(e,t,s){let n=this.sqlBuilder.viewGetMinMax(e,t,s),u=(await c(this.db,n))[0].toJSON(),[o,l]=Object.values(u);return typeof o=="bigint"&&(o=Number(o)),typeof l=="bigint"&&(l=Number(l)),{min:o??null,max:l??null}}async viewGetData(e,t,s,n,i){let u=this.sqlBuilder.viewGetData(e,t,n,s),o=await this.db.useUnsafe((l,p)=>l.runQuery(p,u));i.fromArrowIpc(o)}};function g(r){return r instanceof Error?r.message:String(r)}export{b as DuckDBHandler};
|
|
2
2
|
//# sourceMappingURL=duckdb.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/ts/virtual_servers/duckdb.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 {\n ExpressionError,\n TableDescription,\n} from \"@perspective-dev/client\";\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 { Scalar } from \"@perspective-dev/client/dist/esm/ts-rs/Scalar.d.ts\";\nimport type * as duckdb from \"@duckdb/duckdb-wasm\";\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 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: \"product\", frames: FRAMES, result_type: \"float\" },\n { name: \"median\", frames: FRAMES, result_type: \"float\" },\n // DuckDB spells sample and population variants separately, so both are\n // offered rather than one being picked on the user's behalf.\n { name: \"stddev_samp\", frames: FRAMES, result_type: \"float\" },\n { name: \"stddev_pop\", frames: FRAMES, result_type: \"float\" },\n { name: \"var_samp\", frames: FRAMES, result_type: \"float\" },\n { name: \"var_pop\", frames: FRAMES, result_type: \"float\" },\n // Navigation.\n { name: \"first_value\", frames: FRAMES },\n { name: \"last_value\", frames: FRAMES },\n { name: \"nth_value\", frames: FRAMES, offset: true },\n { name: \"lag\", offset: true },\n { name: \"lead\", offset: true },\n // Ranking. These take no source column - the window's `order_by` is their\n // input - but Perspective requires one, so the choice of source is\n // immaterial for them.\n { name: \"row_number\", result_type: \"float\" },\n { name: \"rank\", result_type: \"float\" },\n { name: \"dense_rank\", result_type: \"float\" },\n { name: \"percent_rank\", result_type: \"float\" },\n { name: \"cume_dist\", result_type: \"float\" },\n // `ntile`'s argument is a bucket count rather than a row offset.\n { name: \"ntile\", offset: true, result_type: \"float\" },\n // Perspective's own, with no DuckDB equivalent - the SQL translation\n // synthesizes them from `lag` and `first_value`.\n { name: \"diff\", offset: true, result_type: \"float\" },\n { name: \"rate\", frames: [\"range\"], result_type: \"float\" },\n];\n\n// Arithmetic is undefined for the non-numeric types; ordering and navigation\n// are not.\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: \"first_value\", frames: FRAMES },\n { name: \"last_value\", frames: FRAMES },\n { name: \"nth_value\", frames: FRAMES, offset: true },\n { name: \"lag\", offset: true },\n { name: \"lead\", offset: true },\n { name: \"row_number\", result_type: \"float\" },\n { name: \"rank\", result_type: \"float\" },\n { name: \"dense_rank\", result_type: \"float\" },\n { name: \"percent_rank\", result_type: \"float\" },\n { name: \"cume_dist\", result_type: \"float\" },\n { name: \"ntile\", offset: true, result_type: \"float\" },\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` / `regexp_matches`\n// by the SQL builder), plus DuckDB'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\n/**\n * Convert a DuckDB `dtype` to a Perspective `ColumnType`.\n */\nfunction duckdbTypeToPsp(name: string): ColumnType {\n name = name.toLowerCase();\n\n if (name.startsWith(\"bool\")) {\n return \"boolean\";\n }\n\n // 32-bit and narrower - `coerce_column` widens these to `Int32`.\n if (\n [\"tinyint\", \"smallint\", \"integer\", \"utinyint\", \"usmallint\"].includes(\n name,\n ) ||\n [\"int8\", \"int16\", \"int32\", \"uint8\", \"uint16\"].includes(name)\n ) {\n return \"integer\";\n }\n\n // Wider than `Int32`, or fractional - all coerce to `Float64`.\n if (\n [\"bigint\", \"hugeint\", \"uhugeint\", \"uinteger\", \"ubigint\"].includes(\n name,\n ) ||\n [\"float\", \"real\", \"double\", \"varint\", \"bignum\"].includes(name) ||\n [\"int64\", \"uint32\", \"uint64\", \"float32\", \"float64\"].includes(name) ||\n name.startsWith(\"decimal\") ||\n name.startsWith(\"numeric\")\n ) {\n return \"float\";\n }\n\n if (name.startsWith(\"date\")) {\n return \"date\";\n }\n\n // `timestamp`, `timestamptz`, `timestamp_ns`, and `time`/`timetz`,\n // which coerce to `Timestamp(Millisecond)` rather than to a number.\n if (name.startsWith(\"time\")) {\n return \"datetime\";\n }\n\n // Everything else renders as text: `varchar`, `enum(...)` (which\n // arrives dictionary-encoded), `json`, `uuid`, `blob`, `interval`,\n // and the nested types.\n if (\n !(\n name.startsWith(\"varchar\") ||\n name === \"utf8\" ||\n name.startsWith(\"enum\") ||\n [\"json\", \"uuid\", \"blob\", \"bit\", \"interval\"].includes(name) ||\n name.startsWith(\"struct\") ||\n name.startsWith(\"map\") ||\n name.startsWith(\"union\") ||\n name.endsWith(\"[]\")\n )\n ) {\n // Unknown, not fatal - the column still renders, as text.\n console.warn(`Unknown type '${name}'`);\n }\n\n return \"string\";\n}\n\nasync function runQuery(\n db: duckdb.AsyncDuckDBConnection,\n query: string,\n options: { columns: true },\n): Promise<{\n rows: any[];\n columns: string[];\n dtypes: string[];\n}>;\n\nasync function runQuery(\n db: duckdb.AsyncDuckDBConnection,\n query: string,\n options?: { columns: false },\n): Promise<any[]>;\n\nasync function runQuery(\n db: duckdb.AsyncDuckDBConnection,\n query: string,\n options: { columns?: boolean } = {},\n) {\n query = query.replace(/\\s+/g, \" \").trim();\n try {\n const result = await db.query(query);\n if (options.columns) {\n return {\n rows: result.toArray(),\n columns: result.schema.fields.map((f) => f.name),\n dtypes: result.schema.fields.map((f) => f.type.toString()),\n };\n }\n\n return result.toArray();\n } catch (error) {\n console.error(\"Query error:\", error);\n console.error(\"Query:\", query);\n throw error;\n }\n}\n\n/**\n * An implementation of Perspective's Virtual Server for `@duckdb/duckdb-wasm`.\n */\nexport class DuckDBHandler implements perspective.VirtualServerHandler {\n private db: duckdb.AsyncDuckDBConnection;\n private sqlBuilder: perspective.GenericSQLVirtualServerModel;\n constructor(db: duckdb.AsyncDuckDBConnection, 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 column_separator: \"|\",\n like_escape_clause: \"\\\\\",\n regex_fn: \"regexp_matches\",\n });\n }\n\n getFeatures(): perspective.Features {\n return {\n group_by: true,\n split_by: true,\n sort: true,\n expressions: true,\n view_derivations: 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 split_rollup_mode: [\"flat\", \"rollup\"],\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 = this.sqlBuilder.getHostedTables();\n const results = await runQuery(this.db, query);\n return results.map((row) => {\n const json = row.toJSON();\n return `${json.database || \"memory\"}.${json.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 res = result.toJSON();\n const colName = res.column_name;\n if (!colName.startsWith(\"__\")) {\n schema[colName] = duckdbTypeToPsp(\n res.column_type,\n ) as ColumnType;\n }\n }\n\n return schema;\n }\n\n async viewColumnSize(viewId: string, config: ViewConfig) {\n const query = this.sqlBuilder.viewColumnSize(viewId);\n const results = await runQuery(this.db, query);\n const count = Number(Object.values(results[0].toJSON())[0]);\n const gs = config.group_by?.length || 0;\n const is_flat = config.group_rollup_mode === \"flat\";\n return count - (gs === 0 ? 0 : is_flat ? gs : gs + 1);\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].toJSON()[\"count_star()\"]);\n }\n\n async tableMakeView(\n tableId: string,\n viewId: string,\n config: ViewConfigUpdate,\n ) {\n // Window order keys need column types for `range` frame emission.\n const schema = Object.keys(config.windows ?? {}).length\n ? await this.tableSchema(tableId)\n : undefined;\n\n const query = this.sqlBuilder.tableMakeView(\n tableId,\n viewId,\n config,\n schema,\n );\n\n await runQuery(this.db, query);\n }\n\n async tableDescribe(\n tableId: string,\n config: ViewConfig,\n ): Promise<TableDescription> {\n let expression_schema = {} as Record<string, ColumnType>;\n const expressions_query = this.sqlBuilder.expressionsDescribe(\n tableId,\n config,\n );\n\n if (expressions_query !== undefined) {\n try {\n expression_schema = await this.describeQuery(expressions_query);\n } catch (error) {\n return await this.attributeExpressionErrors(\n tableId,\n config,\n error,\n );\n }\n }\n\n const schema = Object.keys(config.windows ?? {}).length\n ? await this.tableSchema(tableId)\n : undefined;\n\n const view_query = this.sqlBuilder.tableDescribe(\n tableId,\n config,\n schema,\n );\n\n let view_schema = {} as Record<string, ColumnType>;\n if (view_query !== undefined) {\n try {\n view_schema = await this.describeQuery(view_query);\n } catch (error) {\n return { config_error: errorMessage(error) };\n }\n }\n\n return { expression_schema, view_schema };\n }\n\n /** The planned result columns of one `DESCRIBE` query. */\n private async describeQuery(query: string) {\n const results = await runQuery(this.db, query);\n const schema = {} as Record<string, ColumnType>;\n for (const result of results) {\n const res = result.toJSON();\n if (!res.column_name.startsWith(\"__\")) {\n schema[res.column_name] = duckdbTypeToPsp(\n res.column_type,\n ) as ColumnType;\n }\n }\n\n return schema;\n }\n\n /** Plans each expression on its own to name the ones at fault. */\n private async attributeExpressionErrors(\n tableId: string,\n config: ViewConfig,\n error: unknown,\n ): Promise<TableDescription> {\n const expression_schema = {} as Record<string, ColumnType>;\n const expression_errors = {} as Record<string, ExpressionError>;\n for (const [name, expression] of Object.entries(config.expressions)) {\n const query = this.sqlBuilder.expressionDescribe(\n tableId,\n expression!,\n );\n\n try {\n const results = await runQuery(this.db, query);\n expression_schema[name] = duckdbTypeToPsp(\n results[0].toJSON()[\"column_type\"],\n ) as ColumnType;\n } catch (error) {\n expression_errors[name] = {\n error_message: errorMessage(error),\n line: 0,\n column: 0,\n };\n }\n }\n\n if (Object.keys(expression_errors).length === 0) {\n return { config_error: errorMessage(error) };\n }\n\n return { expression_schema, expression_errors };\n }\n\n async viewMakeTable(\n viewId: string,\n tableId: string,\n config: ViewConfig,\n schema?: Record<string, ColumnType>,\n ) {\n const query = this.sqlBuilder.viewMakeTable(\n viewId,\n tableId,\n config,\n await this.tableSchema(viewId),\n schema,\n );\n\n await runQuery(this.db, query);\n }\n\n async viewDelete(viewId: string) {\n const query = this.sqlBuilder.viewDelete(viewId);\n await runQuery(this.db, query);\n }\n\n async viewGetMinMax(\n viewId: string,\n columnName: string,\n config: ViewConfig,\n ) {\n const query = this.sqlBuilder.viewGetMinMax(viewId, columnName, config);\n const results = await runQuery(this.db, query);\n const row = results[0].toJSON();\n let [min, max] = Object.values(row);\n if (typeof min === \"bigint\") min = Number(min);\n if (typeof max === \"bigint\") max = Number(max);\n return { min: (min ?? null) as Scalar, max: (max ?? null) as Scalar };\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 ipc = await this.db.useUnsafe((bindings, conn) =>\n bindings.runQuery(conn, query),\n );\n\n dataSlice.fromArrowIpc(ipc);\n }\n}\n\nfunction errorMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["NUMBER_AGGS", "STRING_AGGS", "FRAMES", "WINDOW_AGGREGATES", "WINDOW_AGGREGATES_ANY", "FILTER_OPS", "STRING_FILTER_OPS", "duckdbTypeToPsp", "name", "runQuery", "db", "query", "options", "result", "f", "error", "DuckDBHandler", "mod", "
|
|
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\n * `@perspective-dev/client/virtual_servers/duckdb`, it is not exported from\n * the package root `@perspective-dev/client`\n *\n * @module\n */\n\nimport type * as perspective from \"@perspective-dev/client\";\nimport { client_runtime } from \"../wasm/registry.ts\";\nimport type { ColumnType } from \"../../../dist/ts-rs/ColumnType.d.ts\";\nimport type { ViewConfig } from \"../../../dist/ts-rs/ViewConfig.d.ts\";\nimport type {\n ExpressionError,\n TableDescription,\n} from \"@perspective-dev/client\";\nimport type { ViewConfigUpdate } from \"../../../dist/ts-rs/ViewConfigUpdate.d.ts\";\nimport type { ViewWindow } from \"../../../dist/ts-rs/ViewWindow.d.ts\";\nimport type { WindowAggSpec } from \"../../../dist/ts-rs/WindowAggSpec.d.ts\";\nimport type { Scalar } from \"../../../dist/ts-rs/Scalar.d.ts\";\nimport type * as duckdb from \"@duckdb/duckdb-wasm\";\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 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: \"product\", frames: FRAMES, result_type: \"float\" },\n { name: \"median\", frames: FRAMES, result_type: \"float\" },\n // DuckDB spells sample and population variants separately, so both are\n // offered rather than one being picked on the user's behalf.\n { name: \"stddev_samp\", frames: FRAMES, result_type: \"float\" },\n { name: \"stddev_pop\", frames: FRAMES, result_type: \"float\" },\n { name: \"var_samp\", frames: FRAMES, result_type: \"float\" },\n { name: \"var_pop\", frames: FRAMES, result_type: \"float\" },\n // Navigation.\n { name: \"first_value\", frames: FRAMES },\n { name: \"last_value\", frames: FRAMES },\n { name: \"nth_value\", frames: FRAMES, offset: true },\n { name: \"lag\", offset: true },\n { name: \"lead\", offset: true },\n // Ranking. These take no source column - the window's `order_by` is their\n // input - but Perspective requires one, so the choice of source is\n // immaterial for them.\n { name: \"row_number\", result_type: \"float\" },\n { name: \"rank\", result_type: \"float\" },\n { name: \"dense_rank\", result_type: \"float\" },\n { name: \"percent_rank\", result_type: \"float\" },\n { name: \"cume_dist\", result_type: \"float\" },\n // `ntile`'s argument is a bucket count rather than a row offset.\n { name: \"ntile\", offset: true, result_type: \"float\" },\n // Perspective's own, with no DuckDB equivalent - the SQL translation\n // synthesizes them from `lag` and `first_value`.\n { name: \"diff\", offset: true, result_type: \"float\" },\n { name: \"rate\", frames: [\"range\"], result_type: \"float\" },\n];\n\n// Arithmetic is undefined for the non-numeric types; ordering and navigation\n// are not.\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: \"first_value\", frames: FRAMES },\n { name: \"last_value\", frames: FRAMES },\n { name: \"nth_value\", frames: FRAMES, offset: true },\n { name: \"lag\", offset: true },\n { name: \"lead\", offset: true },\n { name: \"row_number\", result_type: \"float\" },\n { name: \"rank\", result_type: \"float\" },\n { name: \"dense_rank\", result_type: \"float\" },\n { name: \"percent_rank\", result_type: \"float\" },\n { name: \"cume_dist\", result_type: \"float\" },\n { name: \"ntile\", offset: true, result_type: \"float\" },\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` / `regexp_matches`\n// by the SQL builder), plus DuckDB'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\n/**\n * Convert a DuckDB `dtype` to a Perspective `ColumnType`.\n */\nfunction duckdbTypeToPsp(name: string): ColumnType {\n name = name.toLowerCase();\n\n if (name.startsWith(\"bool\")) {\n return \"boolean\";\n }\n\n // 32-bit and narrower - `coerce_column` widens these to `Int32`.\n if (\n [\"tinyint\", \"smallint\", \"integer\", \"utinyint\", \"usmallint\"].includes(\n name,\n ) ||\n [\"int8\", \"int16\", \"int32\", \"uint8\", \"uint16\"].includes(name)\n ) {\n return \"integer\";\n }\n\n // Wider than `Int32`, or fractional - all coerce to `Float64`.\n if (\n [\"bigint\", \"hugeint\", \"uhugeint\", \"uinteger\", \"ubigint\"].includes(\n name,\n ) ||\n [\"float\", \"real\", \"double\", \"varint\", \"bignum\"].includes(name) ||\n [\"int64\", \"uint32\", \"uint64\", \"float32\", \"float64\"].includes(name) ||\n name.startsWith(\"decimal\") ||\n name.startsWith(\"numeric\")\n ) {\n return \"float\";\n }\n\n if (name.startsWith(\"date\")) {\n return \"date\";\n }\n\n // `timestamp`, `timestamptz`, `timestamp_ns`, and `time`/`timetz`,\n // which coerce to `Timestamp(Millisecond)` rather than to a number.\n if (name.startsWith(\"time\")) {\n return \"datetime\";\n }\n\n // Everything else renders as text: `varchar`, `enum(...)` (which\n // arrives dictionary-encoded), `json`, `uuid`, `blob`, `interval`,\n // and the nested types.\n if (\n !(\n name.startsWith(\"varchar\") ||\n name === \"utf8\" ||\n name.startsWith(\"enum\") ||\n [\"json\", \"uuid\", \"blob\", \"bit\", \"interval\"].includes(name) ||\n name.startsWith(\"struct\") ||\n name.startsWith(\"map\") ||\n name.startsWith(\"union\") ||\n name.endsWith(\"[]\")\n )\n ) {\n // Unknown, not fatal - the column still renders, as text.\n console.warn(`Unknown type '${name}'`);\n }\n\n return \"string\";\n}\n\nasync function runQuery(\n db: duckdb.AsyncDuckDBConnection,\n query: string,\n options: { columns: true },\n): Promise<{\n rows: any[];\n columns: string[];\n dtypes: string[];\n}>;\n\nasync function runQuery(\n db: duckdb.AsyncDuckDBConnection,\n query: string,\n options?: { columns: false },\n): Promise<any[]>;\n\nasync function runQuery(\n db: duckdb.AsyncDuckDBConnection,\n query: string,\n options: { columns?: boolean } = {},\n) {\n query = query.replace(/\\s+/g, \" \").trim();\n try {\n const result = await db.query(query);\n if (options.columns) {\n return {\n rows: result.toArray(),\n columns: result.schema.fields.map((f) => f.name),\n dtypes: result.schema.fields.map((f) => f.type.toString()),\n };\n }\n\n return result.toArray();\n } catch (error) {\n console.error(\"Query error:\", error);\n console.error(\"Query:\", query);\n throw error;\n }\n}\n\n/**\n * An implementation of Perspective's Virtual Server for `@duckdb/duckdb-wasm`.\n */\nexport class DuckDBHandler implements perspective.VirtualServerHandler {\n private db: duckdb.AsyncDuckDBConnection;\n private sqlBuilder: perspective.GenericSQLVirtualServerModel;\n constructor(db: duckdb.AsyncDuckDBConnection, mod?: typeof perspective) {\n mod ??= client_runtime()?.glue as unknown as\n | typeof perspective\n | undefined;\n if (mod === undefined) {\n throw new Error(\n \"perspective-js is not initialized yet: construct a DuckDBHandler once <perspective-viewer> is ready or a Client exists, or pass the wasm module\",\n );\n }\n\n this.db = db;\n this.sqlBuilder = new mod.GenericSQLVirtualServerModel({\n column_separator: \"|\",\n like_escape_clause: \"\\\\\",\n regex_fn: \"regexp_matches\",\n expand_collapse: true,\n });\n }\n\n getFeatures(): perspective.Features {\n return {\n group_by: true,\n split_by: true,\n sort: true,\n expressions: true,\n view_derivations: true,\n expand_collapse: 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 split_rollup_mode: [\"flat\", \"rollup\"],\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 = this.sqlBuilder.getHostedTables();\n const results = await runQuery(this.db, query);\n return results.map((row) => {\n const json = row.toJSON();\n return `${json.database || \"memory\"}.${json.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 res = result.toJSON();\n const colName = res.column_name;\n if (!colName.startsWith(\"__\")) {\n schema[colName] = duckdbTypeToPsp(\n res.column_type,\n ) as ColumnType;\n }\n }\n\n return schema;\n }\n\n async viewColumnSize(viewId: string, config: ViewConfig) {\n const query = this.sqlBuilder.viewColumnSize(viewId);\n const results = await runQuery(this.db, query);\n const count = Number(Object.values(results[0].toJSON())[0]);\n const gs = config.group_by?.length || 0;\n const is_flat = config.group_rollup_mode === \"flat\";\n return count - (gs === 0 ? 0 : is_flat ? gs : gs + 1) - 1;\n }\n\n async viewSize(viewId: string) {\n return await this.runCount(this.sqlBuilder.viewSize(viewId));\n }\n\n async viewExpand(viewId: string, config: ViewConfig, rowIndex: number) {\n return await this.runCount(\n this.sqlBuilder.viewExpand(viewId, config, rowIndex),\n );\n }\n\n async viewCollapse(viewId: string, config: ViewConfig, rowIndex: number) {\n return await this.runCount(\n this.sqlBuilder.viewCollapse(viewId, config, rowIndex),\n );\n }\n\n async viewSetDepth(viewId: string, config: ViewConfig, depth: number) {\n await runQuery(\n this.db,\n this.sqlBuilder.viewSetDepth(viewId, config, depth),\n );\n }\n\n /** The single integer a `COUNT(*)` query or an `UPDATE` returns. */\n private async runCount(query: string) {\n const results = await runQuery(this.db, query);\n return Number(Object.values(results[0].toJSON())[0]);\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].toJSON()[\"count_star()\"]);\n }\n\n async tableMakeView(\n tableId: string,\n viewId: string,\n config: ViewConfigUpdate,\n ) {\n // Window order keys need column types for `range` frame emission.\n const schema = Object.keys(config.windows ?? {}).length\n ? await this.tableSchema(tableId)\n : undefined;\n\n const query = this.sqlBuilder.tableMakeView(\n tableId,\n viewId,\n config,\n schema,\n );\n\n await runQuery(this.db, query);\n }\n\n async tableDescribe(\n tableId: string,\n config: ViewConfig,\n ): Promise<TableDescription> {\n let expression_schema = {} as Record<string, ColumnType>;\n const expressions_query = this.sqlBuilder.expressionsDescribe(\n tableId,\n config,\n );\n\n if (expressions_query !== undefined) {\n try {\n expression_schema = await this.describeQuery(expressions_query);\n } catch (error) {\n return await this.attributeExpressionErrors(\n tableId,\n config,\n error,\n );\n }\n }\n\n const schema = Object.keys(config.windows ?? {}).length\n ? await this.tableSchema(tableId)\n : undefined;\n\n const view_query = this.sqlBuilder.tableDescribe(\n tableId,\n config,\n schema,\n );\n\n let view_schema = {} as Record<string, ColumnType>;\n if (view_query !== undefined) {\n try {\n view_schema = await this.describeQuery(view_query);\n } catch (error) {\n return { config_error: errorMessage(error) };\n }\n }\n\n return { expression_schema, view_schema };\n }\n\n /** The planned result columns of one `DESCRIBE` query. */\n private async describeQuery(query: string) {\n const results = await runQuery(this.db, query);\n const schema = {} as Record<string, ColumnType>;\n for (const result of results) {\n const res = result.toJSON();\n if (!res.column_name.startsWith(\"__\")) {\n schema[res.column_name] = duckdbTypeToPsp(\n res.column_type,\n ) as ColumnType;\n }\n }\n\n return schema;\n }\n\n /** Plans each expression on its own to name the ones at fault. */\n private async attributeExpressionErrors(\n tableId: string,\n config: ViewConfig,\n error: unknown,\n ): Promise<TableDescription> {\n const expression_schema = {} as Record<string, ColumnType>;\n const expression_errors = {} as Record<string, ExpressionError>;\n for (const [name, expression] of Object.entries(config.expressions)) {\n const query = this.sqlBuilder.expressionDescribe(\n tableId,\n expression!,\n );\n\n try {\n const results = await runQuery(this.db, query);\n expression_schema[name] = duckdbTypeToPsp(\n results[0].toJSON()[\"column_type\"],\n ) as ColumnType;\n } catch (error) {\n expression_errors[name] = {\n error_message: errorMessage(error),\n line: 0,\n column: 0,\n };\n }\n }\n\n if (Object.keys(expression_errors).length === 0) {\n return { config_error: errorMessage(error) };\n }\n\n return { expression_schema, expression_errors };\n }\n\n async viewMakeTable(\n viewId: string,\n tableId: string,\n config: ViewConfig,\n schema?: Record<string, ColumnType>,\n ) {\n const query = this.sqlBuilder.viewMakeTable(\n viewId,\n tableId,\n config,\n await this.tableSchema(viewId),\n schema,\n );\n\n await runQuery(this.db, query);\n }\n\n async viewDelete(viewId: string) {\n const query = this.sqlBuilder.viewDelete(viewId);\n await runQuery(this.db, query);\n }\n\n async viewGetMinMax(\n viewId: string,\n columnName: string,\n config: ViewConfig,\n ) {\n const query = this.sqlBuilder.viewGetMinMax(viewId, columnName, config);\n const results = await runQuery(this.db, query);\n const row = results[0].toJSON();\n let [min, max] = Object.values(row);\n if (typeof min === \"bigint\") min = Number(min);\n if (typeof max === \"bigint\") max = Number(max);\n return { min: (min ?? null) as Scalar, max: (max ?? null) as Scalar };\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 ipc = await this.db.useUnsafe((bindings, conn) =>\n bindings.runQuery(conn, query),\n );\n\n dataSlice.fromArrowIpc(ipc);\n }\n}\n\nfunction errorMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n"],
|
|
5
|
+
"mappings": "AAuBA,OAAS,kBAAAA,MAAsB,sBAa/B,IAAMC,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,EAEM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", "runQuery", "db", "query", "options", "result", "f", "error", "DuckDBHandler", "mod", "row", "json", "tableId", "config", "results", "schema", "res", "colName", "viewId", "count", "gs", "is_flat", "rowIndex", "depth", "expression_schema", "expressions_query", "view_query", "view_schema", "errorMessage", "expression_errors", "expression", "columnName", "min", "max", "viewport", "dataSlice", "ipc", "bindings", "conn"]
|
|
7
7
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve the URL of a WASM binary shipped in the `wasm_package` Perspective
|
|
3
|
+
* package, relative to the URL of the `bundle_package` CDN bundle which loads
|
|
4
|
+
* it.
|
|
5
|
+
*/
|
|
6
|
+
export declare function resolve_package_wasm_url(bundle_url: string, bundle_package: string, wasm_package: string, filename: string): string;
|
|
7
|
+
/**
|
|
8
|
+
* Resolve the URL of one of `@perspective-dev/server`'s engine WASM binaries
|
|
9
|
+
* relative to the URL of the `@perspective-dev/client` CDN bundle which loads
|
|
10
|
+
* it.
|
|
11
|
+
*/
|
|
12
|
+
export declare function resolve_server_wasm_url(client_url: string, filename?: string): string;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The kind of persisted config `migrate()` upgrades: an engine `ViewConfig`,
|
|
3
|
+
* a `<perspective-viewer>` `save()` token, or a `saveWorkspace()` token.
|
|
4
|
+
*/
|
|
5
|
+
export type MigrateKind = "view" | "viewer" | "workspace";
|
|
6
|
+
/**
|
|
7
|
+
* Information `migrate()` could not carry into the current schema exactly.
|
|
8
|
+
*/
|
|
9
|
+
export type MigrateNotice = {
|
|
10
|
+
kind: "Dropped";
|
|
11
|
+
step: string;
|
|
12
|
+
pointer: string;
|
|
13
|
+
value: unknown;
|
|
14
|
+
} | {
|
|
15
|
+
kind: "Approximated";
|
|
16
|
+
step: string;
|
|
17
|
+
pointer: string;
|
|
18
|
+
reason: string;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* A config upgraded by `migrate()`, with what the upgrade dropped or
|
|
22
|
+
* approximated and the ids of the schema changes it applied.
|
|
23
|
+
*/
|
|
24
|
+
export interface MigrateResult<T = Record<string, unknown>> {
|
|
25
|
+
value: T;
|
|
26
|
+
notices: MigrateNotice[];
|
|
27
|
+
steps: string[];
|
|
28
|
+
}
|
|
29
|
+
type MigrateBinary = BufferSource | WebAssembly.Module;
|
|
30
|
+
/**
|
|
31
|
+
* Instantiates the `perspective-migrate` WebAssembly module from its
|
|
32
|
+
* registered source on the first `migrate()` call.
|
|
33
|
+
*/
|
|
34
|
+
export declare class MigrateLoader {
|
|
35
|
+
private source?;
|
|
36
|
+
private module?;
|
|
37
|
+
register(source: () => Promise<MigrateBinary>): void;
|
|
38
|
+
migrate<T = Record<string, unknown>>(kind: MigrateKind, config: unknown, from_version?: string): Promise<MigrateResult<T>>;
|
|
39
|
+
}
|
|
40
|
+
export {};
|