databonk 0.0.4 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +491 -96
- package/dist/dataframe-Bz1B7bIr.d.cts +1001 -0
- package/dist/dataframe-Bz1B7bIr.d.ts +1001 -0
- package/dist/index.cjs +5 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +366 -0
- package/dist/index.d.ts +354 -31
- package/dist/index.js +4 -40
- package/dist/index.js.map +1 -1
- package/dist/parallel-fv5h4BkA.d.cts +152 -0
- package/dist/parallel-fv5h4BkA.d.ts +152 -0
- package/dist/parquet.cjs +3 -0
- package/dist/parquet.cjs.map +1 -0
- package/dist/parquet.d.cts +57 -0
- package/dist/parquet.d.ts +57 -0
- package/dist/parquet.js +3 -0
- package/dist/parquet.js.map +1 -0
- package/dist/scalar.wasm +0 -0
- package/dist/simd-threads.wasm +0 -0
- package/dist/simd.wasm +0 -0
- package/dist/workers.cjs +102 -0
- package/dist/workers.cjs.map +1 -0
- package/dist/workers.d.cts +74 -0
- package/dist/workers.d.ts +74 -0
- package/dist/workers.js +102 -0
- package/dist/workers.js.map +1 -0
- package/package.json +93 -32
- package/build/release.d.ts +0 -719
- package/build/release.js +0 -774
- package/build/release.wasm +0 -0
- package/build/release.wasm.map +0 -1
- package/build/release.wat +0 -22633
- package/dist/dataframe.d.ts +0 -82
- package/dist/dataframe.d.ts.map +0 -1
- package/dist/dataframe.js +0 -318
- package/dist/dataframe.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/loader.d.ts +0 -86
- package/dist/loader.d.ts.map +0 -1
- package/dist/loader.js +0 -147
- package/dist/loader.js.map +0 -1
- package/dist/shared-memory.d.ts +0 -64
- package/dist/shared-memory.d.ts.map +0 -1
- package/dist/shared-memory.js +0 -113
- package/dist/shared-memory.js.map +0 -1
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* enableThreads — opt-in shared-memory parallel mode (ADR-006 / P5.1).
|
|
3
|
+
*
|
|
4
|
+
* Detection:
|
|
5
|
+
* - Browser: requires `crossOriginIsolated === true` (COOP+COEP headers).
|
|
6
|
+
* - Node.js: `SharedArrayBuffer` is always available (no isolation needed).
|
|
7
|
+
* If isolation is absent → console.warn + returns `false`.
|
|
8
|
+
*
|
|
9
|
+
* When enabled:
|
|
10
|
+
* - Loads `simd-threads.wasm` with a SAB-backed `WebAssembly.Memory`.
|
|
11
|
+
* - Calls `__wasm_init_memory()` (if exported) on the main instance to apply
|
|
12
|
+
* any passive data segments. Workers instantiate the same binary with the
|
|
13
|
+
* same shared memory but skip `__wasm_init_memory` — the arena statics are
|
|
14
|
+
* already zero-initialised by wasm's memory-zero guarantee, so re-applying
|
|
15
|
+
* the data section from workers is neither needed nor safe.
|
|
16
|
+
* - Spawns a pool of `workers` (default 4) kernel workers.
|
|
17
|
+
* - Provides parallel chunked dispatch for elementwise and reduction kernels.
|
|
18
|
+
*
|
|
19
|
+
* Allocator invariant (ADR-006):
|
|
20
|
+
* Workers NEVER call alloc/free/realloc. All memory allocation is done by
|
|
21
|
+
* the main thread before dispatching work. This means:
|
|
22
|
+
* • Workers only read from data buffers (reductions) or write to pre-allocated
|
|
23
|
+
* output buffers (elementwise).
|
|
24
|
+
* • There are no data races on the arena state (HEAP_TOP / FREE_HEAD / GENERATION).
|
|
25
|
+
*
|
|
26
|
+
* Bit-exactness note:
|
|
27
|
+
* f64 sum/mean with parallel dispatch is NOT bit-identical to single-thread.
|
|
28
|
+
* Single-thread uses 2-stripe accumulation over all elements; parallel dispatch
|
|
29
|
+
* computes per-chunk partial sums (also 2-striped within each chunk) and then
|
|
30
|
+
* combines them left-to-right. The combination order differs, producing
|
|
31
|
+
* different floating-point rounding. Integer sums are always exact (no FP).
|
|
32
|
+
* This is an ADR-006-scope deviation; results are still IEEE-754 correct and
|
|
33
|
+
* deterministic for a fixed worker count. See docs/threads.md for details.
|
|
34
|
+
*
|
|
35
|
+
* Chunk boundary alignment:
|
|
36
|
+
* All chunk boundaries are multiples of 8 elements so that validity-bitmap byte
|
|
37
|
+
* offsets are integer-aligned. The sub-bitmap pointer for chunk starting at
|
|
38
|
+
* element `s` (a multiple of 8) is `vpPtr + s/8`, which correctly addresses
|
|
39
|
+
* the first byte of that chunk's portion of the Arrow-LSB bitmap.
|
|
40
|
+
*/
|
|
41
|
+
/** Configuration for {@link enableThreads}. */
|
|
42
|
+
interface ThreadsConfig {
|
|
43
|
+
/** Number of kernel workers (default 4). */
|
|
44
|
+
workers?: number;
|
|
45
|
+
/**
|
|
46
|
+
* Directory containing `simd-threads.wasm`.
|
|
47
|
+
* Node: filesystem path. Browser: base URL.
|
|
48
|
+
* Default: resolved relative to the loader module (same as scalar/simd.wasm).
|
|
49
|
+
*/
|
|
50
|
+
wasmDir?: string | URL;
|
|
51
|
+
/** Worker request timeout in ms (default 30 000). 0 = no timeout. */
|
|
52
|
+
timeoutMs?: number;
|
|
53
|
+
/** Initial shared memory pages (64 KiB each; default 16 = 1 MiB). */
|
|
54
|
+
initialPages?: number;
|
|
55
|
+
/** Maximum shared memory pages (default 16384 = 1 GiB). Must match --max-memory in build.sh. */
|
|
56
|
+
maxPages?: number;
|
|
57
|
+
}
|
|
58
|
+
/** Handle returned by a successful {@link enableThreads} call. */
|
|
59
|
+
interface ThreadsHandle {
|
|
60
|
+
/** Always `true` — threads are active. */
|
|
61
|
+
readonly enabled: true;
|
|
62
|
+
/** Number of active worker slots. */
|
|
63
|
+
readonly workers: number;
|
|
64
|
+
/**
|
|
65
|
+
* The SAB-backed shared WebAssembly.Memory used by all workers.
|
|
66
|
+
* Column data for parallel operations must reside in this memory.
|
|
67
|
+
* Use {@link alloc} to allocate buffers here.
|
|
68
|
+
*/
|
|
69
|
+
readonly memory: WebAssembly.Memory;
|
|
70
|
+
/**
|
|
71
|
+
* Allocate `bytes` in the shared memory arena (main-thread-only).
|
|
72
|
+
* Returns a 16-byte-aligned pointer, or throws on OOM.
|
|
73
|
+
* Workers NEVER call alloc — only the main thread does (ADR-006 invariant).
|
|
74
|
+
*/
|
|
75
|
+
alloc(bytes: number): number;
|
|
76
|
+
/** Free a pointer previously returned by {@link alloc}. */
|
|
77
|
+
free(ptr: number): void;
|
|
78
|
+
/**
|
|
79
|
+
* Call a kernel function synchronously on the main thread (single-thread path).
|
|
80
|
+
* Useful for correctness comparison: `callKernel('sum_f64_null', ptr, 0, len)`.
|
|
81
|
+
*/
|
|
82
|
+
callKernel(fn: string, ...args: number[]): number;
|
|
83
|
+
/**
|
|
84
|
+
* Parallel f64 sum over elements [0, len). Null lanes (vpPtr ≠ 0) are skipped.
|
|
85
|
+
* Returns the sum of non-null values, or 0 if all null.
|
|
86
|
+
* NOTE: result may differ from single-thread due to FP accumulation order; see
|
|
87
|
+
* docs/threads.md for the formal deviation note.
|
|
88
|
+
*/
|
|
89
|
+
sumF64(dataPtr: number, vpPtr: number, len: number): Promise<number>;
|
|
90
|
+
/**
|
|
91
|
+
* Parallel f64 mean. Returns NaN if len == 0 or all null.
|
|
92
|
+
* Computed as totalSum / totalCount (exact weights via count_null per chunk).
|
|
93
|
+
*/
|
|
94
|
+
meanF64(dataPtr: number, vpPtr: number, len: number): Promise<number>;
|
|
95
|
+
/**
|
|
96
|
+
* Parallel f64 min (NaN-aware, null-skipping).
|
|
97
|
+
* Returns NaN if no valid non-NaN elements exist.
|
|
98
|
+
*/
|
|
99
|
+
minF64(dataPtr: number, vpPtr: number, len: number): Promise<number>;
|
|
100
|
+
/**
|
|
101
|
+
* Parallel f64 max (NaN-aware, null-skipping).
|
|
102
|
+
* Returns NaN if no valid non-NaN elements exist.
|
|
103
|
+
*/
|
|
104
|
+
maxF64(dataPtr: number, vpPtr: number, len: number): Promise<number>;
|
|
105
|
+
/**
|
|
106
|
+
* Parallel elementwise add_f64 over pre-allocated buffers in shared memory.
|
|
107
|
+
* Workers write directly to the shared output buffer (zero-copy).
|
|
108
|
+
*/
|
|
109
|
+
addF64(aPtr: number, bPtr: number, outPtr: number, len: number): Promise<void>;
|
|
110
|
+
/** Parallel elementwise sub_f64 (shared memory). */
|
|
111
|
+
subF64(aPtr: number, bPtr: number, outPtr: number, len: number): Promise<void>;
|
|
112
|
+
/** Parallel elementwise mul_f64 (shared memory). */
|
|
113
|
+
mulF64(aPtr: number, bPtr: number, outPtr: number, len: number): Promise<void>;
|
|
114
|
+
/**
|
|
115
|
+
* Run a generic parallel elementwise binary kernel by name.
|
|
116
|
+
* The kernel must have signature (i32 a, i32 b, i32 out, i32 len) -> ().
|
|
117
|
+
*/
|
|
118
|
+
parallelElementwiseBinary(fn: string, aPtr: number, bPtr: number, outPtr: number, len: number, elemBytes: number): Promise<void>;
|
|
119
|
+
/**
|
|
120
|
+
* Run a generic parallel reduction kernel by name.
|
|
121
|
+
* The kernel must have signature (i32 data, i32 vp, i32 len) -> <scalar>.
|
|
122
|
+
* Returns an array of per-chunk partial results (caller combines).
|
|
123
|
+
*/
|
|
124
|
+
parallelReduce(fn: string, dataPtr: number, vpPtr: number, len: number, elemBytes: number): Promise<number[]>;
|
|
125
|
+
/** Terminate all workers. The handle must not be used after this. */
|
|
126
|
+
terminate(): void;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Split [0, len) into at most `numWorkers` chunks, each starting at a
|
|
130
|
+
* multiple of 8 elements (for Arrow-LSB validity-bitmap byte alignment).
|
|
131
|
+
* Returns an array of [start, end) pairs.
|
|
132
|
+
*/
|
|
133
|
+
declare function splitChunks(len: number, numWorkers: number): Array<[number, number]>;
|
|
134
|
+
/**
|
|
135
|
+
* Opt-in parallel mode (ADR-006).
|
|
136
|
+
*
|
|
137
|
+
* Returns a {@link ThreadsHandle} when threads are available and enabled,
|
|
138
|
+
* or `false` (with a console.warn) when cross-origin isolation is absent.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* const th = await enableThreads({ workers: 4 });
|
|
143
|
+
* if (th) {
|
|
144
|
+
* const sum = await th.sumF64(ptr, vp, len);
|
|
145
|
+
* // ...
|
|
146
|
+
* th.terminate();
|
|
147
|
+
* }
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
declare function enableThreads(config?: ThreadsConfig): Promise<ThreadsHandle | false>;
|
|
151
|
+
|
|
152
|
+
export { type ThreadsConfig as T, type ThreadsHandle as a, enableThreads as e, splitChunks as s };
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* enableThreads — opt-in shared-memory parallel mode (ADR-006 / P5.1).
|
|
3
|
+
*
|
|
4
|
+
* Detection:
|
|
5
|
+
* - Browser: requires `crossOriginIsolated === true` (COOP+COEP headers).
|
|
6
|
+
* - Node.js: `SharedArrayBuffer` is always available (no isolation needed).
|
|
7
|
+
* If isolation is absent → console.warn + returns `false`.
|
|
8
|
+
*
|
|
9
|
+
* When enabled:
|
|
10
|
+
* - Loads `simd-threads.wasm` with a SAB-backed `WebAssembly.Memory`.
|
|
11
|
+
* - Calls `__wasm_init_memory()` (if exported) on the main instance to apply
|
|
12
|
+
* any passive data segments. Workers instantiate the same binary with the
|
|
13
|
+
* same shared memory but skip `__wasm_init_memory` — the arena statics are
|
|
14
|
+
* already zero-initialised by wasm's memory-zero guarantee, so re-applying
|
|
15
|
+
* the data section from workers is neither needed nor safe.
|
|
16
|
+
* - Spawns a pool of `workers` (default 4) kernel workers.
|
|
17
|
+
* - Provides parallel chunked dispatch for elementwise and reduction kernels.
|
|
18
|
+
*
|
|
19
|
+
* Allocator invariant (ADR-006):
|
|
20
|
+
* Workers NEVER call alloc/free/realloc. All memory allocation is done by
|
|
21
|
+
* the main thread before dispatching work. This means:
|
|
22
|
+
* • Workers only read from data buffers (reductions) or write to pre-allocated
|
|
23
|
+
* output buffers (elementwise).
|
|
24
|
+
* • There are no data races on the arena state (HEAP_TOP / FREE_HEAD / GENERATION).
|
|
25
|
+
*
|
|
26
|
+
* Bit-exactness note:
|
|
27
|
+
* f64 sum/mean with parallel dispatch is NOT bit-identical to single-thread.
|
|
28
|
+
* Single-thread uses 2-stripe accumulation over all elements; parallel dispatch
|
|
29
|
+
* computes per-chunk partial sums (also 2-striped within each chunk) and then
|
|
30
|
+
* combines them left-to-right. The combination order differs, producing
|
|
31
|
+
* different floating-point rounding. Integer sums are always exact (no FP).
|
|
32
|
+
* This is an ADR-006-scope deviation; results are still IEEE-754 correct and
|
|
33
|
+
* deterministic for a fixed worker count. See docs/threads.md for details.
|
|
34
|
+
*
|
|
35
|
+
* Chunk boundary alignment:
|
|
36
|
+
* All chunk boundaries are multiples of 8 elements so that validity-bitmap byte
|
|
37
|
+
* offsets are integer-aligned. The sub-bitmap pointer for chunk starting at
|
|
38
|
+
* element `s` (a multiple of 8) is `vpPtr + s/8`, which correctly addresses
|
|
39
|
+
* the first byte of that chunk's portion of the Arrow-LSB bitmap.
|
|
40
|
+
*/
|
|
41
|
+
/** Configuration for {@link enableThreads}. */
|
|
42
|
+
interface ThreadsConfig {
|
|
43
|
+
/** Number of kernel workers (default 4). */
|
|
44
|
+
workers?: number;
|
|
45
|
+
/**
|
|
46
|
+
* Directory containing `simd-threads.wasm`.
|
|
47
|
+
* Node: filesystem path. Browser: base URL.
|
|
48
|
+
* Default: resolved relative to the loader module (same as scalar/simd.wasm).
|
|
49
|
+
*/
|
|
50
|
+
wasmDir?: string | URL;
|
|
51
|
+
/** Worker request timeout in ms (default 30 000). 0 = no timeout. */
|
|
52
|
+
timeoutMs?: number;
|
|
53
|
+
/** Initial shared memory pages (64 KiB each; default 16 = 1 MiB). */
|
|
54
|
+
initialPages?: number;
|
|
55
|
+
/** Maximum shared memory pages (default 16384 = 1 GiB). Must match --max-memory in build.sh. */
|
|
56
|
+
maxPages?: number;
|
|
57
|
+
}
|
|
58
|
+
/** Handle returned by a successful {@link enableThreads} call. */
|
|
59
|
+
interface ThreadsHandle {
|
|
60
|
+
/** Always `true` — threads are active. */
|
|
61
|
+
readonly enabled: true;
|
|
62
|
+
/** Number of active worker slots. */
|
|
63
|
+
readonly workers: number;
|
|
64
|
+
/**
|
|
65
|
+
* The SAB-backed shared WebAssembly.Memory used by all workers.
|
|
66
|
+
* Column data for parallel operations must reside in this memory.
|
|
67
|
+
* Use {@link alloc} to allocate buffers here.
|
|
68
|
+
*/
|
|
69
|
+
readonly memory: WebAssembly.Memory;
|
|
70
|
+
/**
|
|
71
|
+
* Allocate `bytes` in the shared memory arena (main-thread-only).
|
|
72
|
+
* Returns a 16-byte-aligned pointer, or throws on OOM.
|
|
73
|
+
* Workers NEVER call alloc — only the main thread does (ADR-006 invariant).
|
|
74
|
+
*/
|
|
75
|
+
alloc(bytes: number): number;
|
|
76
|
+
/** Free a pointer previously returned by {@link alloc}. */
|
|
77
|
+
free(ptr: number): void;
|
|
78
|
+
/**
|
|
79
|
+
* Call a kernel function synchronously on the main thread (single-thread path).
|
|
80
|
+
* Useful for correctness comparison: `callKernel('sum_f64_null', ptr, 0, len)`.
|
|
81
|
+
*/
|
|
82
|
+
callKernel(fn: string, ...args: number[]): number;
|
|
83
|
+
/**
|
|
84
|
+
* Parallel f64 sum over elements [0, len). Null lanes (vpPtr ≠ 0) are skipped.
|
|
85
|
+
* Returns the sum of non-null values, or 0 if all null.
|
|
86
|
+
* NOTE: result may differ from single-thread due to FP accumulation order; see
|
|
87
|
+
* docs/threads.md for the formal deviation note.
|
|
88
|
+
*/
|
|
89
|
+
sumF64(dataPtr: number, vpPtr: number, len: number): Promise<number>;
|
|
90
|
+
/**
|
|
91
|
+
* Parallel f64 mean. Returns NaN if len == 0 or all null.
|
|
92
|
+
* Computed as totalSum / totalCount (exact weights via count_null per chunk).
|
|
93
|
+
*/
|
|
94
|
+
meanF64(dataPtr: number, vpPtr: number, len: number): Promise<number>;
|
|
95
|
+
/**
|
|
96
|
+
* Parallel f64 min (NaN-aware, null-skipping).
|
|
97
|
+
* Returns NaN if no valid non-NaN elements exist.
|
|
98
|
+
*/
|
|
99
|
+
minF64(dataPtr: number, vpPtr: number, len: number): Promise<number>;
|
|
100
|
+
/**
|
|
101
|
+
* Parallel f64 max (NaN-aware, null-skipping).
|
|
102
|
+
* Returns NaN if no valid non-NaN elements exist.
|
|
103
|
+
*/
|
|
104
|
+
maxF64(dataPtr: number, vpPtr: number, len: number): Promise<number>;
|
|
105
|
+
/**
|
|
106
|
+
* Parallel elementwise add_f64 over pre-allocated buffers in shared memory.
|
|
107
|
+
* Workers write directly to the shared output buffer (zero-copy).
|
|
108
|
+
*/
|
|
109
|
+
addF64(aPtr: number, bPtr: number, outPtr: number, len: number): Promise<void>;
|
|
110
|
+
/** Parallel elementwise sub_f64 (shared memory). */
|
|
111
|
+
subF64(aPtr: number, bPtr: number, outPtr: number, len: number): Promise<void>;
|
|
112
|
+
/** Parallel elementwise mul_f64 (shared memory). */
|
|
113
|
+
mulF64(aPtr: number, bPtr: number, outPtr: number, len: number): Promise<void>;
|
|
114
|
+
/**
|
|
115
|
+
* Run a generic parallel elementwise binary kernel by name.
|
|
116
|
+
* The kernel must have signature (i32 a, i32 b, i32 out, i32 len) -> ().
|
|
117
|
+
*/
|
|
118
|
+
parallelElementwiseBinary(fn: string, aPtr: number, bPtr: number, outPtr: number, len: number, elemBytes: number): Promise<void>;
|
|
119
|
+
/**
|
|
120
|
+
* Run a generic parallel reduction kernel by name.
|
|
121
|
+
* The kernel must have signature (i32 data, i32 vp, i32 len) -> <scalar>.
|
|
122
|
+
* Returns an array of per-chunk partial results (caller combines).
|
|
123
|
+
*/
|
|
124
|
+
parallelReduce(fn: string, dataPtr: number, vpPtr: number, len: number, elemBytes: number): Promise<number[]>;
|
|
125
|
+
/** Terminate all workers. The handle must not be used after this. */
|
|
126
|
+
terminate(): void;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Split [0, len) into at most `numWorkers` chunks, each starting at a
|
|
130
|
+
* multiple of 8 elements (for Arrow-LSB validity-bitmap byte alignment).
|
|
131
|
+
* Returns an array of [start, end) pairs.
|
|
132
|
+
*/
|
|
133
|
+
declare function splitChunks(len: number, numWorkers: number): Array<[number, number]>;
|
|
134
|
+
/**
|
|
135
|
+
* Opt-in parallel mode (ADR-006).
|
|
136
|
+
*
|
|
137
|
+
* Returns a {@link ThreadsHandle} when threads are available and enabled,
|
|
138
|
+
* or `false` (with a console.warn) when cross-origin isolation is absent.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* const th = await enableThreads({ workers: 4 });
|
|
143
|
+
* if (th) {
|
|
144
|
+
* const sum = await th.sumF64(ptr, vp, len);
|
|
145
|
+
* // ...
|
|
146
|
+
* th.terminate();
|
|
147
|
+
* }
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
declare function enableThreads(config?: ThreadsConfig): Promise<ThreadsHandle | false>;
|
|
151
|
+
|
|
152
|
+
export { type ThreadsConfig as T, type ThreadsHandle as a, enableThreads as e, splitChunks as s };
|
package/dist/parquet.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
'use strict';var index_js=require('hyparquet/src/index.js'),index_js$1=require('hyparquet-writer/src/index.js');var x={f64:{name:"f64",size:8,view:"f64",ctor:Float64Array,wasm:"f64",float:true},f32:{name:"f32",size:4,view:"f32",ctor:Float32Array,wasm:"f32",float:true},i32:{name:"i32",size:4,view:"i32",ctor:Int32Array,wasm:"i32",float:false},u32:{name:"u32",size:4,view:"u32",ctor:Uint32Array,wasm:"u32",float:false},bool:{name:"bool",size:1,view:"bool",ctor:Uint8Array,wasm:"bool",float:false},utf8:{name:"utf8",size:4,view:"i32",ctor:Int32Array,wasm:"utf8",float:false},i64:{name:"i64",size:8,view:"i64",ctor:BigInt64Array,wasm:"i64",float:false},date32:{name:"date32",size:4,view:"i32",ctor:Int32Array,wasm:"i32",float:false},timestamp:{name:"timestamp",size:8,view:"i64",ctor:BigInt64Array,wasm:"i64",float:false}};function A(t){return t+7>>3}function v(t,e){return (t[e>>3]&1<<(e&7))!==0}function S(t,e){let n=e>>3;t[n]=(t[n]??0)|1<<(e&7);}var Je=new TextEncoder,Ze=new TextDecoder,oe=new WeakMap;function Xe(t){let e=oe.get(t);return e===void 0&&(e={slots:new Array(t.count),stats:{hits:0,misses:0}},oe.set(t,e)),e}function pt(t,e){let n=e.length,r=e.map(c=>Je.encode(c)),o=0;for(let c of r)o+=c.length;let i=t.mod.alloc((n+1)*4),a=t.mod.alloc(o),s=t.viewOf({ptr:i,length:n+1,dtype:"i32"}),l=t.viewOf({ptr:a,length:o,dtype:"u8"});s[0]=0;let u=0;for(let c=0;c<n;c++){let m=r[c];l.set(m,u),u+=m.length,s[c+1]=u;}return {count:n,offsetsPtr:i,bytesPtr:a,bytesLen:o}}function j(t,e,n){let r=Xe(e),o=r.slots[n];if(o!==void 0)return r.stats.hits++,o;r.stats.misses++;let i=t.viewOf({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"}),a=i[n],s=i[n+1],l;if(s<=a)l="";else {let u=t.viewOf({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"});l=Ze.decode(u.subarray(a,s));}return r.slots[n]=l,l}function Z(t,e){let n=new Array(e.count);for(let r=0;r<e.count;r++)n[r]=j(t,e,r);return n}function ie(t,e){t.viewOf.forget({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"}),t.viewOf.forget({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"}),t.mod.free(e.offsetsPtr),t.mod.free(e.bytesPtr);}function Y(t){return t==null}function W(t,e,n,r,o,i){return {dtype:t,length:e,dataPtr:n,validityPtr:r,validityBitOffset:0,dict:o,owned:true,...i!==void 0?{tz:i}:{}}}function D(t,e,n,r){return e==="utf8"?on(t,n):e==="bool"?en(t,n):e==="i64"?nn(t,n):e==="timestamp"||e==="date32"?rn(t,e,n,r):tn(t,e,n)}function Qe(t,e){switch(t){case "f64":return e instanceof Float64Array;case "f32":return e instanceof Float32Array;case "i32":return e instanceof Int32Array;case "u32":return e instanceof Uint32Array;default:return false}}function tn(t,e,n){let r=x[e],o=n.length;if(Qe(e,n)){let c=t.mod.alloc(o*r.size);return t.viewOf({ptr:c,length:o,dtype:r.view}).set(n),W(e,o,c,0,null)}let i=0;for(let c=0;c<o;c++)Y(n[c])&&i++;let a=t.mod.alloc(o*r.size),s=i>0?t.mod.alloc(A(o)):0,l=t.viewOf({ptr:a,length:o,dtype:r.view}),u=null;s!==0&&(u=t.viewOf({ptr:s,length:A(o),dtype:"u8"}),u.fill(0));for(let c=0;c<o;c++){let m=n[c];Y(m)?l[c]=0:(l[c]=m,u&&S(u,c));}return W(e,o,a,s,null)}function en(t,e){let n=e.length;if(e instanceof Uint8Array){let l=t.mod.alloc(n);return t.viewOf({ptr:l,length:n,dtype:"bool"}).set(e),W("bool",n,l,0,null)}let r=0;for(let l=0;l<n;l++)Y(e[l])&&r++;let o=t.mod.alloc(n),i=r>0?t.mod.alloc(A(n)):0,a=t.viewOf({ptr:o,length:n,dtype:"bool"}),s=null;i!==0&&(s=t.viewOf({ptr:i,length:A(n),dtype:"u8"}),s.fill(0));for(let l=0;l<n;l++){let u=e[l];Y(u)?a[l]=0:(a[l]=u?1:0,s&&S(s,l));}return W("bool",n,o,i,null)}function nn(t,e){let n=e.length;if(e instanceof BigInt64Array){let l=t.mod.alloc(n*8);return t.viewOf({ptr:l,length:n,dtype:"i64"}).set(e),W("i64",n,l,0,null)}let r=0;for(let l=0;l<n;l++){let u=e[l];if(Y(u)){r++;continue}if(typeof u=="number"){if(!Number.isInteger(u))throw new RangeError(`i64 column: non-integer number ${u} at index ${l} \u2014 use BigInt or null`);if(!Number.isSafeInteger(u))throw new RangeError(`i64 column: unsafe integer ${u} at index ${l} \u2014 use BigInt (e.g. ${u}n)`)}}let o=t.mod.alloc(n*8),i=r>0?t.mod.alloc(A(n)):0,a=t.viewOf({ptr:o,length:n,dtype:"i64"}),s=null;i!==0&&(s=t.viewOf({ptr:i,length:A(n),dtype:"u8"}),s.fill(0));for(let l=0;l<n;l++){let u=e[l];Y(u)?a[l]=0n:(a[l]=typeof u=="bigint"?u:BigInt(u),s&&S(s,l));}return W("i64",n,o,i,null)}function rn(t,e,n,r){let o=e==="timestamp",i=o?8:4,a=n.length,s=o?"i64":"i32";if(o?n instanceof BigInt64Array:n instanceof Int32Array){let p=t.mod.alloc(Math.max(a*i,1));return t.viewOf({ptr:p,length:a,dtype:s}).set(n),W(e,a,p,0,null,r)}let l=0;for(let p=0;p<a;p++)Y(n[p])&&l++;let u=t.mod.alloc(Math.max(a*i,1)),c=l>0?t.mod.alloc(A(a)):0,m=t.viewOf({ptr:u,length:a,dtype:s}),f=null;c&&(f=t.viewOf({ptr:c,length:A(a),dtype:"u8"}),f.fill(0));for(let p=0;p<a;p++){let d=n[p];Y(d)?m[p]=o?0n:0:(m[p]=o?typeof d=="bigint"?d:BigInt(d):d,f&&S(f,p));}return W(e,a,u,c,null,r)}function on(t,e){let n=e.length,r=new Map,o=[],i=new Int32Array(n),a=new Uint8Array(n),s=0;for(let f=0;f<n;f++){let p=e[f];if(Y(p))s++,a[f]=1,i[f]=0;else {let d=p,h=r.get(d);h===void 0&&(h=o.length,o.push(d),r.set(d,h)),i[f]=h;}}let l=t.mod.alloc(n*4),u=s>0?t.mod.alloc(A(n)):0,c=pt(t,o);if(t.viewOf({ptr:l,length:n,dtype:"i32"}).set(i),u!==0){let f=t.viewOf({ptr:u,length:A(n),dtype:"u8"});f.fill(0);for(let p=0;p<n;p++)a[p]===0&&S(f,p);}return W("utf8",n,l,u,c)}function H(t,e){let{length:n,validityPtr:r,validityBitOffset:o}=e,i=r===0?null:t.viewOf({ptr:r,length:A(o+n),dtype:"u8"}),a=new Array(n);if(e.dtype==="utf8"){let c=t.viewOf({ptr:e.dataPtr,length:n,dtype:"i32"}),m=e.dict;for(let f=0;f<n;f++)a[f]=i&&!v(i,o+f)?null:j(t,m,c[f]);return a}if(e.dtype==="i64"||e.dtype==="timestamp"){let c=t.viewOf({ptr:e.dataPtr,length:n,dtype:"i64"});for(let m=0;m<n;m++)a[m]=i&&!v(i,o+m)?null:c[m];return a}if(e.dtype==="date32"){let c=t.viewOf({ptr:e.dataPtr,length:n,dtype:"i32"});for(let m=0;m<n;m++)a[m]=i&&!v(i,o+m)?null:c[m];return a}let s=x[e.dtype],l=t.viewOf({ptr:e.dataPtr,length:n,dtype:s.view}),u=e.dtype==="bool";for(let c=0;c<n;c++)i&&!v(i,o+c)?a[c]=null:a[c]=u?l[c]!==0:l[c];return a}function Ft(t,e,n){let r=Math.max(0,Math.min(e,t.length)),i=Math.max(r,Math.min(n,t.length))-r,a=x[t.dtype].size;return {dtype:t.dtype,length:i,dataPtr:t.dataPtr+r*a,validityPtr:t.validityPtr,validityBitOffset:t.validityBitOffset+r,dict:t.dict,owned:false,...t.tz!==void 0?{tz:t.tz}:{}}}function Q(t,e){if(!e.owned)return;let n=x[e.dtype];t.viewOf.forget({ptr:e.dataPtr,length:e.length,dtype:n.view}),t.mod.free(e.dataPtr),e.validityPtr!==0&&(t.viewOf.forget({ptr:e.validityPtr,length:A(e.validityBitOffset+e.length),dtype:"u8"}),t.mod.free(e.validityPtr)),e.dict!==null&&ie(t,e.dict);}function dt(t){return t instanceof M?t:ae(t)}var M=class t{node;constructor(e){this.node=Object.freeze(e),Object.freeze(this);}add(e){return ft("add",this,e)}sub(e){return ft("sub",this,e)}mul(e){return ft("mul",this,e)}div(e){return ft("div",this,e)}mod(e){return ft("mod",this,e)}neg(){return new t({kind:"neg",operand:this})}gt(e){return rt("gt",this,e)}ge(e){return rt("ge",this,e)}lt(e){return rt("lt",this,e)}le(e){return rt("le",this,e)}eq(e){return rt("eq",this,e)}ne(e){return rt("ne",this,e)}and(e){return new t({kind:"bool",op:"and",left:this,right:dt(e)})}or(e){return new t({kind:"bool",op:"or",left:this,right:dt(e)})}not(){return new t({kind:"not",operand:this})}isNull(){return new t({kind:"isNull",operand:this})}notNull(){return this.isNull().not()}fillNull(e){return new t({kind:"fillNull",operand:this,value:e})}cast(e){return new t({kind:"cast",operand:this,to:e})}sum(){return q("sum",this)}mean(){return q("mean",this)}min(){return q("min",this)}max(){return q("max",this)}count(){return q("count",this)}nunique(){return q("nunique",this)}std(){return q("std",this)}var(){return q("var",this)}first(){return q("first",this)}last(){return q("last",this)}get dt(){return new Et(this)}get str(){return new Tt(this)}toString(){return R(this.node)}},Et=class{constructor(e){this.operand=e;}operand;year(){return z("year",this.operand)}month(){return z("month",this.operand)}day(){return z("day",this.operand)}hour(){return z("hour",this.operand)}minute(){return z("minute",this.operand)}second(){return z("second",this.operand)}millisecond(){return z("millisecond",this.operand)}weekday(){return z("weekday",this.operand)}dayOfYear(){return z("dayOfYear",this.operand)}quarter(){return z("quarter",this.operand)}},Tt=class{constructor(e){this.operand=e;}operand;slice(e,n){return new M({kind:"strSlice",operand:this.operand,start:e,end:n})}};function ae(t,e){return new M({kind:"lit",value:t,dtype:null})}function ft(t,e,n){return new M({kind:"arith",op:t,left:e,right:dt(n)})}function rt(t,e,n){return new M({kind:"compare",op:t,left:e,right:dt(n)})}function q(t,e){return new M({kind:"agg",op:t,operand:e})}function z(t,e){return new M({kind:"dt",component:t,operand:e})}var an={add:"+",sub:"-",mul:"*",div:"/",mod:"%"};function $t(t){return typeof t=="string"?JSON.stringify(t):String(t)}function R(t){switch(t.kind){case "col":return `col(${JSON.stringify(t.name)})`;case "lit":return t.dtype?`lit(${$t(t.value)}, ${t.dtype})`:`lit(${$t(t.value)})`;case "arith":return `(${R(t.left.node)} ${an[t.op]} ${R(t.right.node)})`;case "neg":return `(-${R(t.operand.node)})`;case "compare":return `${R(t.left.node)}.${t.op}(${R(t.right.node)})`;case "bool":return `${R(t.left.node)}.${t.op}(${R(t.right.node)})`;case "not":return `${R(t.operand.node)}.not()`;case "isNull":return `${R(t.operand.node)}.isNull()`;case "fillNull":return `${R(t.operand.node)}.fillNull(${$t(t.value)})`;case "cast":return `${R(t.operand.node)}.cast(${t.to})`;case "agg":return `${R(t.operand.node)}.${t.op}()`;case "dt":return `${R(t.operand.node)}.dt.${t.component}()`;case "strSlice":return t.end===void 0?`${R(t.operand.node)}.str.slice(${t.start})`:`${R(t.operand.node)}.str.slice(${t.start}, ${t.end})`}}var _=class extends Error{constructor(e){super(e),this.name="ExprError";}};function tt(t,e,n,r){let o=r?` ${r}`:"";return new _(`dtype mismatch in '${t}': cannot combine ${e} and ${n}.${o}`)}function P(t,e,n){let r=n?` ${n}`:"";return new _(`operation '${t}' is not supported for dtype ${e}.${r}`)}function Lt(t,e){return new _(`cast from ${t} to ${e} is not supported in v1`+(t==="utf8"||e==="utf8"?" (numeric\u2194utf8 conversion is not a kernel cast; use CSV inference / toString).":"."))}function Ut(t,e){let n=It(t,e),r=n?` Did you mean '${n}'?`:"",o=e.length?` Known columns: ${e.map(i=>`'${i}'`).join(", ")}.`:"";return new _(`unknown column '${t}'.${r}${o}`)}function X(t,e,n){return new _(`literal ${JSON.stringify(t)} is not a valid ${e} value for '${n}'.`)}function It(t,e){let n=null,r=1/0;for(let i of e){let a=sn(t,i);a<r&&(r=a,n=i);}let o=Math.max(2,Math.ceil(t.length/3));return n!==null&&r<=o?n:null}function sn(t,e){let n=t.length,r=e.length;if(n===0)return r;if(r===0)return n;let o=new Array(r+1),i=new Array(r+1);for(let a=0;a<=r;a++)o[a]=a;for(let a=1;a<=n;a++){i[0]=a;let s=t.charCodeAt(a-1);for(let l=1;l<=r;l++){let u=s===e.charCodeAt(l-1)?0:1;i[l]=Math.min(o[l]+1,i[l-1]+1,o[l-1]+u);}[o,i]=[i,o];}return o[r]}var ln=new Set(["f64","f32","i32","u32","i64"]),se=new Set(["i32","u32","i64"]),gt=new Set(["f64","f32"]),ot=new Set(["date32","timestamp"]);function G(t){return ln.has(t)}function jt(t,e){return Number.isInteger(t)?e==="i32"?t>=-2147483648&&t<=2147483647:t>=0&&t<=4294967295:false}function Rt(t){let e=t.node;return e.kind==="lit"&&e.dtype===null&&typeof e.value=="number"?e.value:null}function B(t,e){return {kind:"lit",value:t,dtype:e}}function Ot(t,e){return t.dtype===e?t:{kind:"cast",dtype:e,from:t.dtype,operand:t}}function wt(t,e){return E(t,e)}function qt(t,e){return wt(t,e).dtype}function E(t,e){let n=t.node;switch(n.kind){case "col":{let r=e.dtypeOf(n.name);if(r===void 0)throw Ut(n.name,e.columnNames());return {kind:"col",name:n.name,dtype:r}}case "lit":return n.dtype!==null?B(n.value,n.dtype):typeof n.value=="bigint"?B(n.value,"i64"):typeof n.value=="number"?B(n.value,Number.isInteger(n.value)?"i32":"f64"):B(n.value,typeof n.value=="string"?"utf8":"bool");case "arith":return un(n.op,n.left,n.right,e);case "neg":return fn(n.operand,e);case "compare":return hn(n.op,n.left,n.right,e);case "bool":return wn(n.op,n.left,n.right,e);case "not":return bn(n.operand,e);case "isNull":return {kind:"isNull",dtype:"bool",operand:E(n.operand,e)};case "fillNull":return xn(n.operand,n.value,e);case "cast":return Dn(n.operand,n.to,e);case "agg":return Cn(n.op,n.operand,e);case "dt":return dn(n.component,n.operand,e);case "strSlice":return yn(n.operand,n.start,n.end,e)}}function un(t,e,n,r){let o=Rt(e),i=Rt(n);if(o!==null&&i!==null){let u=Number.isInteger(o)?"i32":"f64",c=Number.isInteger(i)?"i32":"f64",m=u===c?u:"f64";return {kind:"arith",op:t,dtype:m,left:B(o,m),right:B(i,m)}}if(o!==null||i!==null){let u=o??i,c=E(o!==null?n:e,r),{opDtype:m,colTarget:f}=cn(t,u,c.dtype),p=Ot(c,f),d=B(u,m);return {kind:"arith",op:t,dtype:m,left:o!==null?d:p,right:o!==null?p:d}}let a=E(e,r),s=E(n,r),l=mn(t,a.dtype,s.dtype);return {kind:"arith",op:t,dtype:l,left:Ot(a,l),right:Ot(s,l)}}function cn(t,e,n){if(n==="timestamp")return {opDtype:"timestamp",colTarget:"timestamp"};if(n==="date32")return {opDtype:"date32",colTarget:"date32"};if(!G(n))throw P(t,n,"arithmetic");return gt.has(n)?{opDtype:n,colTarget:n}:n==="i64"?Number.isSafeInteger(e)?{opDtype:"i64",colTarget:"i64"}:{opDtype:"f64",colTarget:"f64"}:jt(e,n)?{opDtype:n,colTarget:n}:{opDtype:"f64",colTarget:"f64"}}function mn(t,e,n){if(ot.has(e)||ot.has(n))return pn(t,e,n);if(!G(e))throw P(t,e,"arithmetic");if(!G(n))throw P(t,n,"arithmetic");if(e===n)return e;if(e==="i64"&&(n==="i32"||n==="u32")||n==="i64"&&(e==="i32"||e==="u32"))return "i64";if(e==="i64"&>.has(n)||n==="i64"&>.has(e)||(e==="i32"||e==="u32")&&n==="f64"||(n==="i32"||n==="u32")&&e==="f64")return "f64";if((e==="i32"||e==="u32")&&n==="f32"||(n==="i32"||n==="u32")&&e==="f32")return "f32";throw tt(t,e,n,"insert an explicit .cast() (only int\u2192float widening is implicit).")}function pn(t,e,n){if(t==="sub"&&e==="timestamp"&&n==="timestamp")return "i64";if((t==="add"||t==="sub")&&e==="timestamp"&&se.has(n)||t==="add"&&se.has(e)&&n==="timestamp")return "timestamp";if(t==="sub"&&e==="date32"&&n==="date32")return "i32";if((t==="add"||t==="sub")&&e==="date32"&&(n==="i32"||n==="u32")||t==="add"&&(e==="i32"||e==="u32")&&n==="date32")return "date32";throw new _(`${t}(${e},${n})`)}function fn(t,e){let n=E(t,e);if(!G(n.dtype))throw P("neg",n.dtype);return {kind:"neg",dtype:n.dtype,operand:n}}function dn(t,e,n){let r=E(e,n);if(!ot.has(r.dtype))throw P("dt",r.dtype);return {kind:"dt",component:t,dtype:"i32",operand:r}}function yn(t,e,n,r){let o=E(t,r);if(o.dtype!=="utf8")throw P("str.slice",o.dtype,`str namespace requires a utf8 column; got ${o.dtype}`);return {kind:"strSlice",dtype:"utf8",operand:o,start:e,end:n}}function hn(t,e,n,r){let o=t==="eq"||t==="ne",i=yt(e)??yt(n),a=ht(e)??ht(n),s=Rt(e),l=Rt(n);if(s!==null&&l!==null){let f=Number.isInteger(s)&&Number.isInteger(l)?"i32":"f64";return et(t,f,B(s,f),B(l,f))}if(s!==null||l!==null){let f=s??l,p=E(s!==null?n:e,r);if(p.dtype==="date32"){let w=B(f,"date32");return et(t,"date32",s!==null?w:p,s!==null?p:w)}if(p.dtype==="timestamp"){let w=B(f,"timestamp");return et(t,"timestamp",s!==null?w:p,s!==null?p:w)}if(!G(p.dtype))throw tt(t,s!==null?"f64":p.dtype,s!==null?p.dtype:"f64","cannot compare a number to a non-numeric column.");let d=gn(f,p.dtype),h=Ot(p,d),y=B(f,d);return et(t,d,s!==null?y:h,s!==null?h:y)}if(i!==null){let f=E(yt(e)!==null?n:e,r);if(f.dtype!=="utf8")throw tt(t,"utf8",f.dtype,"cannot compare a string to a non-utf8 column.");if(!o)throw P(t,"utf8","string ordering");let p=B(i,"utf8");return et(t,"utf8",yt(e)!==null?p:f,yt(e)!==null?f:p)}if(a!==null){let f=E(ht(e)!==null?n:e,r);if(f.dtype!=="bool")throw tt(t,"bool",f.dtype,"cannot compare a boolean to a non-bool column.");if(!o)throw P(t,"bool");let p=B(a,"bool");return et(t,"bool",ht(e)!==null?p:f,ht(e)!==null?f:p)}let u=E(e,r),c=E(n,r);if(u.dtype!==c.dtype)throw tt(t,u.dtype,c.dtype,"cast");let m=u.dtype;if(m==="utf8"||m==="bool")throw P(t,m,"column-vs-column");return et(t,m,u,c)}function et(t,e,n,r){return {kind:"compare",op:t,dtype:"bool",operandDtype:e,left:n,right:r}}function gn(t,e){return gt.has(e)?e:e==="i64"?Number.isSafeInteger(t)?"i64":"f64":jt(t,e)?e:"f64"}function yt(t){let e=t.node;return e.kind==="lit"&&typeof e.value=="string"?e.value:null}function ht(t){let e=t.node;return e.kind==="lit"&&typeof e.value=="boolean"?e.value:null}function wn(t,e,n,r){let o=E(e,r),i=E(n,r);if(o.dtype!=="bool")throw P(t,o.dtype,"boolean");if(i.dtype!=="bool")throw P(t,i.dtype,"boolean");return {kind:"bool",op:t,dtype:"bool",left:o,right:i}}function bn(t,e){let n=E(t,e);if(n.dtype!=="bool")throw P("not",n.dtype,"boolean");return {kind:"not",dtype:"bool",operand:n}}function xn(t,e,n){let r=E(t,n);return An(e,r.dtype),{kind:"fillNull",dtype:r.dtype,operand:r,value:e}}function An(t,e){if(e==="utf8"){if(typeof t!="string")throw X(t,e,"fillNull");return}if(e==="bool"){if(typeof t!="boolean")throw X(t,e,"fillNull");return}if(e==="i64"||e==="timestamp"){if(typeof t!="bigint"&&typeof t!="number")throw X(t,e,"fillNull");if(typeof t=="number"&&!Number.isSafeInteger(t))throw X(t,e,"fillNull");return}if(typeof t!="number")throw X(t,e,"fillNull");if((e==="i32"||e==="u32")&&!jt(t,e))throw X(t,e,"fillNull")}var vn={f64:new Set(["f64","f32","i32","u32","bool","i64"]),f32:new Set(["f64","f32","i32","u32","bool","i64"]),i32:new Set(["f64","f32","i32","u32","bool","i64","date32"]),u32:new Set(["f64","f32","i32","u32","bool","i64"]),bool:new Set(["f64","f32","i32","u32","bool","i64"]),utf8:new Set(["utf8"]),i64:new Set(["f64","f32","i32","u32","bool","i64","timestamp"]),date32:new Set(["i32","timestamp","date32"]),timestamp:new Set(["i64","date32","timestamp"])};function Dn(t,e,n){let r=E(t,n),o=r.dtype;if(!vn[o]?.has(e))throw Lt(o,e);return {kind:"cast",dtype:e,from:o,operand:r}}function Cn(t,e,n){let r=E(e,n),o=bt(t,r.dtype);return {kind:"agg",op:t,dtype:o,operandDtype:r.dtype,operand:r}}function bt(t,e){switch(t){case "count":return "i32";case "nunique":if(G(e)||e==="utf8"||ot.has(e))return "i32";throw P("nunique",e);case "sum":if(gt.has(e))return e;if(e==="i64")return "i64";if(e==="i32"||e==="u32")return "f64";throw P("sum",e);case "mean":case "std":case "var":if(G(e))return "f64";throw P(t,e);case "min":case "max":if(G(e)||ot.has(e))return e;throw P(t,e);case "first":case "last":if(G(e)||e==="utf8"||ot.has(e))return e;throw P(t,e)}}function le(t,e,n){let r=t[e];if(typeof r!="function")throw new Error(`kernel export not found: ${e}`);return r(...n)}function ue(t,e,n){let r=t[e];if(typeof r!="function")throw new Error(`kernel export not found: ${e}`);return r(...n)}function St(t){let e=0,n=0;for(let r of t.allocs)r.purpose==="data"?e++:r.purpose==="mask"&&n++;return {kernelCalls:t.kernels.length,kernels:t.kernels.slice(),allocations:t.allocs.length,dataAllocations:e,maskAllocations:n,frees:t.frees}}var xt=class{ctx;wasm;len;owned=new Set;trace={kernels:[],allocs:[],frees:0};viewed=[];constructor(e){this.ctx=e.ctx,this.wasm=e.wasm,this.len=e.length;}get validityBytes(){return A(this.len)}alloc(e,n){let r=this.ctx.mod.alloc(e);if(r===0)throw new Error(`out of wasm memory allocating ${e} bytes`);return this.owned.add(r),this.trace.allocs.push({bytes:e,purpose:n}),r}track(e,n,r){e!==0&&(this.owned.add(e),this.trace.allocs.push({bytes:n,purpose:r}));}free(e){e!==0&&this.owned.delete(e)&&(this.forgetViews(e),this.ctx.mod.free(e),this.trace.frees++);}transfer(e){this.owned.delete(e),this.forgetViews(e);}freeAll(){for(let e of this.viewed)this.ctx.viewOf.forget(e);this.viewed.length=0;for(let e of this.owned)this.ctx.mod.free(e),this.trace.frees++;this.owned.clear();}call(e,...n){return this.trace.kernels.push(e),le(this.wasm,e,n)}callBigInt(e,...n){return this.trace.kernels.push(e),ue(this.wasm,e,n)}view(e,n,r){let o={ptr:e,length:n,dtype:r};return this.viewed.push(o),this.ctx.viewOf(o)}forgetViews(e){for(let n=this.viewed.length-1;n>=0;n--){let r=this.viewed[n];r.ptr===e&&(this.ctx.viewOf.forget(r),this.viewed.splice(n,1));}}},N={ptr:0,owns:false};function I(t,e){e.owns&&t.free(e.ptr);}function ce(t,e,n){if(e===0)return N;if((n&7)===0)return {ptr:e+(n>>3),owns:false};let r=t.validityBytes,o=t.alloc(r,"validity"),i=t.view(e,A(n+t.len),"u8"),a=t.view(o,r,"u8");a.fill(0);for(let s=0;s<t.len;s++)v(i,n+s)&&S(a,s);return {ptr:o,owns:true}}function me(t,e,n){if(n===0)return 0;let r=t.view(e,A(n),"u8"),o=0;for(let i=0;i<n;i++)r[i>>3]>>(i&7)&1&&o++;return o}var zt=86400000n,kn=[0,31,59,90,120,151,181,212,243,273,304,334];function pe(t){let e=t+719468,n=Math.floor(e/146097),r=e-n*146097,o=Math.floor((r-Math.floor(r/1460)+Math.floor(r/36524)-Math.floor(r/146096))/365),i=o+n*400,a=r-(365*o+Math.floor(o/4)-Math.floor(o/100)),s=Math.floor((5*a+2)/153),l=a-Math.floor((153*s+2)/5)+1,u=s<10?s+3:s-9;return {year:i+(u<=2?1:0),month:u,day:l}}function fe(t,e,n){let r=t-(e<=2?1:0),o=Math.floor(r/400),i=r-o*400,a=Math.floor((153*(e>2?e-3:e+9)+2)/5)+n-1,s=i*365+Math.floor(i/4)-Math.floor(i/100)+a;return o*146097+s-719468}function Pn(t){return t%4===0&&t%100!==0||t%400===0}function Mt(t,e,n){let r=(kn[e-1]??0)+n;return e>2&&Pn(t)?r+1:r}function _t(t){return ((t+3)%7+7)%7+1}function En(t){let e=t/zt;return t%zt!==0n&&t<0n&&(e-=1n),Number(e)}function de(t){let{year:e,month:n,day:r}=pe(t);return {year:e,month:n,day:r,hour:0,minute:0,second:0,millisecond:0,weekday:_t(t),dayOfYear:Mt(e,n,r),quarter:Math.ceil(n/3)}}function ye(t){let e=typeof t=="bigint"?t:BigInt(Math.trunc(t)),n=En(e),r=Number(e-BigInt(n)*zt),{year:o,month:i,day:a}=pe(n),s=Math.floor(r/36e5),l=r%36e5,u=Math.floor(l/6e4),c=l%6e4,m=Math.floor(c/1e3),f=c%1e3;return {year:o,month:i,day:a,hour:s,minute:u,second:m,millisecond:f,weekday:_t(n),dayOfYear:Mt(o,i,a),quarter:Math.ceil(i/3)}}var he=new Map;function Tn(t){let e=he.get(t);if(e!==void 0)return e;let n;try{n=new Intl.DateTimeFormat("en",{timeZone:t,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric",hourCycle:"h23"});}catch(r){throw new RangeError(`Invalid IANA timezone: "${t}". ${r instanceof Error?r.message:String(r)}`)}return he.set(t,n),n}function ge(t,e){let r=Tn(e).formatToParts(t),o=0,i=0,a=0,s=0,l=0,u=0;for(let f of r)switch(f.type){case "year":o=parseInt(f.value,10);break;case "month":i=parseInt(f.value,10);break;case "day":a=parseInt(f.value,10);break;case "hour":s=parseInt(f.value,10);break;case "minute":l=parseInt(f.value,10);break;case "second":u=parseInt(f.value,10);break}let c=(t%1e3+1e3)%1e3,m=fe(o,i,a);return {year:o,month:i,day:a,hour:s,minute:l,second:u,millisecond:c,weekday:_t(m),dayOfYear:Mt(o,i,a),quarter:Math.ceil(i/3)}}function In(t,e){return ((t[e>>3]??0)&1<<(e&7))!==0}function it(t,e,n,r){let o=t.length,i=new Int32Array(o),a=t instanceof Int32Array,s=typeof r=="string"&&r.length>0;for(let l=0;l<o;l++){if(e!==null&&!In(e,l))continue;let u;if(a)u=de(t[l]??0);else if(s){let c=Number(t[l]??0n);u=ge(c,r);}else {let c=t[l]??0n;u=ye(c);}i[l]=u[n];}return {data:i,validity:e}}function vt(t,e){let n=wt(t,e),r=n.kind==="agg"?"scalar":"column";return {dtype:n.dtype,resultKind:r,execute(){let o=new xt(e);try{if(n.kind==="agg")return {kind:"scalar",scalar:Gt(o,e,n),stats:St(o.trace)};let i=U(o,e,n);return {kind:"column",column:De(o,i),stats:St(o.trace)}}finally{o.freeAll();}}}}function Ht(t,e){let n=wt(t,e);if(n.dtype!=="bool")throw new _(`filter predicate must be boolean, got ${n.dtype}. Use a comparison / boolean expression.`);return {execute(){let r=new xt(e),o=Zn(r,U(r,e,n)),i;if(o.validity.ptr===0)i=o.maskPtr;else if(o.ownsMask)r.call("validity_and",o.maskPtr,o.validity.ptr,o.maskPtr,r.len),I(r,o.validity),i=o.maskPtr;else {let s=r.alloc(r.validityBytes,"mask");r.call("validity_and",o.maskPtr,o.validity.ptr,s,r.len),I(r,o.validity),i=s;}let a=me(r,i,r.len);return On(r,e,i,a)}}}function On(t,e,n,r){let o=null,i=()=>{if(o)return o;let a=new Int32Array(r),s=t.view(n,A(t.len),"u8"),l=0;for(let u=0;u<t.len;u++)s[u>>3]>>(u&7)&1&&(a[l++]=u);return o=a,a};return {count:r,compact(a){let s=x[a.dtype],l=e.ctx.mod.alloc(Math.max(r*s.size,1)),u=t.call(`filter_${Xn(a.dtype)}`,a.dataPtr,n,l,t.len);if(u!==r)throw new Error(`filter count mismatch: kernel ${u}, expected ${r}`);let c=0;if(a.validityPtr!==0){let f=i(),p=t.view(a.validityPtr,A(a.validityBitOffset+t.len),"u8"),d=0,h=A(r),y=e.ctx.mod.alloc(Math.max(h,1)),w=t.view(y,h,"u8");w.fill(0);for(let g=0;g<r;g++)v(p,a.validityBitOffset+f[g])?S(w,g):d++;d===0?(e.ctx.viewOf.forget({ptr:y,length:h,dtype:"u8"}),e.ctx.mod.free(y)):c=y;}let m=a.dict?Ce(e.ctx,a.dict):null;return {dtype:a.dtype,length:r,dataPtr:l,validityPtr:c,validityBitOffset:0,dict:m,owned:true}},free(){t.freeAll();},get stats(){return St(t.trace)}}}var Rn={gt:"lt",lt:"gt",ge:"le",le:"ge",eq:"eq",ne:"ne"},Sn=new Set(["add","mul"]),Mn=new Set(["f64_i32","f64_u32","f64_bool","f32_i32","f32_u32","f32_bool","i32_u32","u32_i32","f64_i64","f32_i64"]),Kt=86400000n;function U(t,e,n){switch(n.kind){case "col":return _n(t,e,n.name,n.dtype);case "lit":return Bt(t,n.value,n.dtype);case "arith":return Bn(t,e,n);case "neg":return Vn(t,e,n.dtype,n.operand);case "compare":return Fn(t,e,n);case "bool":return Un(t,e,n.op,n.left,n.right);case "not":return jn(t,e,n.operand);case "isNull":return qn(t,e,n.operand);case "fillNull":return zn(t,e,n.dtype,n.operand,n.value);case "cast":return Wn(t,e,n.from,n.dtype,n.operand);case "agg":{let r=Gt(t,e,n);return Bt(t,r.value,n.dtype)}case "dt":return Hn(t,e,n.component,n.operand);case "strSlice":return Gn(t,e,n.operand,n.start,n.end)}}function _n(t,e,n,r){let o=e.getColumn(n);if(!o)throw new _(`column '${n}' vanished during execution`);let i=ce(t,o.validityPtr,o.validityBitOffset);return r==="bool"?{rep:"boolcol",dataPtr:o.dataPtr,ownsData:false,validity:i}:{rep:"column",dtype:r,dataPtr:o.dataPtr,ownsData:false,validity:i,dict:o.dict,ownsDict:false}}function V(t,e,n){let r=U(t,e,n);if(r.rep!=="column")throw new Error(`internal: expected numeric column, got ${r.rep}`);return r}function At(t){return t.kind==="lit"||t.kind==="agg"||t.kind==="cast"&&At(t.operand)}function Bn(t,e,n){let r=n.dtype,o=At(n.left),i=At(n.right);if(o&&i){let l=Bt(t,K(t,e,n.left),r);return Yt(t,e,n.op,l,K(t,e,n.right),r)}if(i){let l=V(t,e,n.left);return Yt(t,e,n.op,l,K(t,e,n.right),r)}if(o){let l=V(t,e,n.right);return Nn(t,e,n.op,K(t,e,n.left),l,r)}let a=V(t,e,n.left),s=V(t,e,n.right);return xe(t,e,n.op,a,s,r)}function Yt(t,e,n,r,o,i){let a=x[i].size,s=r.ownsData?r.dataPtr:t.alloc(t.len*a,"data");if(o===null||(n==="div"||n==="mod")&&((i==="i32"||i==="u32")&&o===0||i==="i64"&&o===0n))return I(t,r.validity),nr(t,s,t.len*a),F(i,s,nt(t),null,false);let u=x[i].wasm;if(u==="i64"){let c=typeof o=="bigint"?o:BigInt(o);t.callBigInt(`${n}_i64_scalar`,r.dataPtr,c,s,t.len);}else t.call(`${n}_${u}_scalar`,r.dataPtr,o,s,t.len);return F(i,s,r.validity,null,false)}function Nn(t,e,n,r,o,i){if(Sn.has(n))return Yt(t,e,n,o,r,i);let a=Bt(t,r,i);return xe(t,e,n,a,o,i)}function xe(t,e,n,r,o,i){let a=x[i].size,s=(n==="div"||n==="mod")&&(i==="i32"||i==="u32"||i==="i64"),l=N;if(s){let f=t.alloc(t.validityBytes,"mask");i==="i64"?t.callBigInt("ne_i64_scalar_mask",o.dataPtr,0n,f,t.len):t.call(`ne_${i}_scalar_mask`,o.dataPtr,0,f,t.len),l={ptr:f,owns:true};}let u=r.ownsData?r.dataPtr:o.ownsData?o.dataPtr:t.alloc(t.len*a,"data"),c=x[i].wasm;t.call(`${n}_${c}`,r.dataPtr,o.dataPtr,u,t.len),u!==r.dataPtr&&r.ownsData&&t.free(r.dataPtr),u!==o.dataPtr&&o.ownsData&&t.free(o.dataPtr);let m=Wt(t,r.validity,o.validity);return s&&(m=Wt(t,m,l)),F(i,u,m,null,false)}function Vn(t,e,n,r){let o=V(t,e,r),i=x[n].size,a=o.ownsData?o.dataPtr:t.alloc(t.len*i,"data");return t.call(`neg_${n}`,o.dataPtr,a,t.len),F(n,a,o.validity,null,false)}function Fn(t,e,n){let r=n.operandDtype,o=At(n.left),i=At(n.right);if(o&&i){let c=K(t,e,n.left),m=K(t,e,n.right);return er(t,tr(n.op,c,m))}if(r==="bool")return Ln(t,e,n,o);if(r==="utf8")return $n(t,e,n,o);let a=x[r].wasm,s=t.alloc(t.validityBytes,"mask");if(o||i){let c=V(t,e,o?n.right:n.left),m=K(t,e,o?n.left:n.right);if(m===null)return I(t,c.validity),t.free(c.ownsData?c.dataPtr:0),t.view(s,t.validityBytes,"u8").fill(0),{rep:"mask",maskPtr:s,ownsMask:true,validity:nt(t)};let f=o?Rn[n.op]:n.op;if(a==="i64"){let p=typeof m=="bigint"?m:BigInt(m);t.callBigInt(`${f}_i64_scalar_mask`,c.dataPtr,p,s,t.len);}else t.call(`${f}_${a}_scalar_mask`,c.dataPtr,m,s,t.len);return c.ownsData&&t.free(c.dataPtr),{rep:"mask",maskPtr:s,ownsMask:true,validity:c.validity}}let l=V(t,e,n.left),u=V(t,e,n.right);return t.call(`${n.op}_${a}_mask`,l.dataPtr,u.dataPtr,s,t.len),l.ownsData&&t.free(l.dataPtr),u.ownsData&&t.free(u.dataPtr),{rep:"mask",maskPtr:s,ownsMask:true,validity:Wt(t,l.validity,u.validity)}}function $n(t,e,n,r){let o=V(t,e,r?n.right:n.left),i=K(t,e,r?n.left:n.right),a=t.alloc(t.validityBytes,"mask"),s=t.view(a,t.validityBytes,"u8");if(i===null)return s.fill(0),o.ownsData&&t.free(o.dataPtr),I(t,o.validity),o.ownsDict&&st(e,o.dict),{rep:"mask",maskPtr:a,ownsMask:true,validity:nt(t)};let u=(o.dict?Z(e.ctx,o.dict):[]).indexOf(i);return u>=0?t.call(`${n.op}_i32_scalar_mask`,o.dataPtr,u,a,t.len):s.fill(n.op==="ne"?255:0),o.ownsData&&t.free(o.dataPtr),o.ownsDict&&st(e,o.dict),{rep:"mask",maskPtr:a,ownsMask:true,validity:o.validity}}function Ln(t,e,n,r){let o=K(t,e,r?n.left:n.right),i=at(t,U(t,e,r?n.right:n.left));return (n.op==="eq"?o===false:o===true)?Ae(t,i):i}function Un(t,e,n,r,o){let i=at(t,U(t,e,r)),a=at(t,U(t,e,o)),s=n==="and"?"and_kleene":"or_kleene",l=i.ownsData?i.dataPtr:a.ownsData?a.dataPtr:t.alloc(t.len,"data"),u=i.validity.ptr===0&&a.validity.ptr===0,c;if(u){let m=t.alloc(t.validityBytes,"scratch");t.call(s,i.dataPtr,0,a.dataPtr,0,l,m,t.len),t.free(m),c=N;}else {let m=t.alloc(t.validityBytes,"validity");t.call(s,i.dataPtr,i.validity.ptr,a.dataPtr,a.validity.ptr,l,m,t.len),I(t,i.validity),I(t,a.validity),c={ptr:m,owns:true};}return l!==i.dataPtr&&i.ownsData&&t.free(i.dataPtr),l!==a.dataPtr&&a.ownsData&&t.free(a.dataPtr),{rep:"boolcol",dataPtr:l,ownsData:true,validity:c}}function jn(t,e,n){return Ae(t,at(t,U(t,e,n)))}function Ae(t,e){let n=e.ownsData?e.dataPtr:t.alloc(t.len,"data"),r;if(e.validity.ptr===0){let o=t.alloc(t.validityBytes,"scratch");t.call("not_bool",e.dataPtr,0,n,o,t.len),t.free(o),r=N;}else {let o=t.alloc(t.validityBytes,"validity");t.call("not_bool",e.dataPtr,e.validity.ptr,n,o,t.len),I(t,e.validity),r={ptr:o,owns:true};}return n!==e.dataPtr&&e.ownsData&&t.free(e.dataPtr),{rep:"boolcol",dataPtr:n,ownsData:true,validity:r}}function qn(t,e,n){let r=U(t,e,n),o=Jt(r),i=t.alloc(t.len,"data");return t.call("is_null",o.ptr,i,t.len),ve(t,e,r),{rep:"boolcol",dataPtr:i,ownsData:true,validity:N}}function zn(t,e,n,r,o){if(n==="bool")return Kn(t,e,r,o);if(n==="utf8")return Yn(t,e,r,o);let i=V(t,e,r);if(i.validity.ptr===0)return i;let a=x[n].size,s=i.ownsData?i.dataPtr:t.alloc(t.len*a,"data"),l=x[n].wasm;if(l==="i64"){let u=typeof o=="bigint"?o:BigInt(o);t.callBigInt("fill_null_i64",i.dataPtr,i.validity.ptr,u,s,t.len);}else t.call(`fill_null_${l}`,i.dataPtr,i.validity.ptr,o,s,t.len);return I(t,i.validity),F(n,s,N,null,false)}function Kn(t,e,n,r){let o=at(t,U(t,e,n));if(o.validity.ptr===0)return o;let i=o.ownsData?o.dataPtr:t.alloc(t.len,"data"),a=t.view(o.dataPtr,t.len,"u8"),s=t.view(o.validity.ptr,t.validityBytes,"u8"),l=t.view(i,t.len,"u8"),u=r?1:0;for(let c=0;c<t.len;c++)l[c]=v(s,c)?a[c]:u;return I(t,o.validity),{rep:"boolcol",dataPtr:i,ownsData:true,validity:N}}function Yn(t,e,n,r){let o=V(t,e,n);if(o.validity.ptr===0)return o;let i=o.dict?Z(e.ctx,o.dict):[],a=i.indexOf(r),s=o.dict,l=o.ownsDict;a<0&&(a=i.length,s=pt(e.ctx,[...i,r]),t.track(s.offsetsPtr,(s.count+1)*4,"dict"),t.track(s.bytesPtr,Math.max(s.bytesLen,1),"dict"),o.ownsDict&&st(e,o.dict),l=true);let u=o.ownsData?o.dataPtr:t.alloc(t.len*4,"data"),c=t.view(o.dataPtr,t.len,"i32"),m=t.view(o.validity.ptr,t.validityBytes,"u8"),f=t.view(u,t.len,"i32");for(let p=0;p<t.len;p++)f[p]=v(m,p)?c[p]:a;return I(t,o.validity),F("utf8",u,N,s,l)}function Wn(t,e,n,r,o){let i=U(t,e,o);if(n===r)return i;let a=(i.rep==="boolcol",i.dataPtr),s=(i.rep==="boolcol",i.ownsData),l=Jt(i);if(n==="date32"&&r==="i32"||n==="i32"&&r==="date32"||n==="timestamp"&&r==="i64"||n==="i64"&&r==="timestamp")return {...i,dtype:r};if(n==="date32"&&r==="timestamp"){let d=t.alloc(t.len*8,"data"),h=t.alloc(t.validityBytes,"scratch");return t.call("cast_i32_i64",a,l.ptr,d,h,t.len),t.free(h),t.callBigInt("mul_i64_scalar",d,Kt,d,t.len),s&&t.free(a),F("timestamp",d,l,null,false)}if(n==="timestamp"&&r==="date32"){let d=t.view(a,t.len,"i64"),h=t.alloc(t.len*4,"data"),y=t.view(h,t.len,"i32");for(let w=0;w<t.len;w++){let g=d[w],b=g/Kt;g%Kt!==0n&&g<0n&&(b-=1n),y[w]=Number(b);}return s&&t.free(a),F("date32",h,l,null,false)}let u=x[r].size,m=s&&x[n].size===u?a:t.alloc(t.len*u,"data"),f=Mn.has(`${n}_${r}`),p;if(f){let d=t.alloc(t.validityBytes,"validity");t.call(`cast_${n}_${r}`,a,l.ptr,m,d,t.len),I(t,l),p={ptr:d,owns:true};}else {let d=t.alloc(t.validityBytes,"scratch");t.call(`cast_${n}_${r}`,a,l.ptr,m,d,t.len),t.free(d),p=l;}return m!==a&&s&&t.free(a),r==="bool"?{rep:"boolcol",dataPtr:m,ownsData:true,validity:p}:F(r,m,p,null,false)}function Hn(t,e,n,r){let o=V(t,e,r),i=t.len,a;if(r.kind==="col"){let m=e.getColumn(r.name);m?.tz&&(a=m.tz);}let s=null;o.validity.ptr!==0&&(s=t.view(o.validity.ptr,t.validityBytes,"u8"));let l;if(o.dtype==="date32"){let m=t.view(o.dataPtr,i,"i32");l=it(m,s,n);}else {let m=t.view(o.dataPtr,i,"i64");l=it(m,s,n,a);}let u=t.alloc(i*4,"data");return t.view(u,i,"i32").set(l.data),o.ownsData&&t.free(o.dataPtr),o.ownsDict&&o.dict&&st(e,o.dict),F("i32",u,o.validity,null,false)}function Gn(t,e,n,r,o){let i=V(t,e,n),a=t.len,s=i.dict?Z(e.ctx,i.dict):[],l=s.length,u=new Int32Array(l),c=[],m=new Map;for(let y=0;y<l;y++){let w=o===void 0?s[y].slice(r):s[y].slice(r,o),g=m.get(w);g===void 0&&(g=c.length,c.push(w),m.set(w,g)),u[y]=g;}let f=pt(e.ctx,c);t.track(f.offsetsPtr,(f.count+1)*4,"dict"),t.track(f.bytesPtr,Math.max(f.bytesLen,1),"dict");let p=i.ownsData?i.dataPtr:t.alloc(a*4,"data"),d=t.view(i.dataPtr,a,"i32"),h=t.view(p,a,"i32");for(let y=0;y<a;y++)h[y]=u[d[y]];return p!==i.dataPtr&&i.ownsData&&t.free(i.dataPtr),i.ownsDict&&st(e,i.dict),F("utf8",p,i.validity,f,true)}function Gt(t,e,n){let r=U(t,e,n.operand),o=Jt(r),i=r.rep==="mask"?0:(r.rep==="boolcol",r.dataPtr),a=r.rep==="column"?r.dict:null,s=n.operandDtype,l=s==="utf8"?"i32":s,u=t.len,c=o.ptr===0?u:t.call("count_null",o.ptr,u),m=Jn(t,e,n.op,s,l,i,o.ptr,u,c,a);return ve(t,e,r),{value:m,dtype:n.dtype}}function Jn(t,e,n,r,o,i,a,s,l,u){let c=r==="utf8"?"i32":x[r].wasm,m=f=>o==="u32"?f>>>0:f;switch(n){case "count":return l;case "nunique":return t.call(`nunique_${c}_null`,i,a,s);case "sum":return c==="i64"?t.callBigInt("sum_i64_null",i,a,s):t.call(`sum_${r}_null`,i,a,s);case "mean":return l>=1?t.call(`mean_${r}_null`,i,a,s):null;case "std":return l>=2?t.call(`std_${r}_null`,i,a,s):null;case "var":return l>=2?t.call(`var_${r}_null`,i,a,s):null;case "min":case "max":return l?c==="i64"?t.callBigInt(`${n}_i64_null`,i,a,s):m(t.call(`${n}_${c}_null`,i,a,s)):null;case "first":case "last":{let f=t.alloc(4,"scratch");t.view(f,1,"i32")[0]=0;let p;c==="i64"?p=t.callBigInt(`${n}_i64_null`,i,a,s,f):p=t.call(`${n}_${c}_null`,i,a,s,f);let d=t.view(f,1,"i32")[0]!==0;return t.free(f),d?c==="i64"?p:r==="utf8"?j(e.ctx,u,p):m(p):null}}}function at(t,e){if(e.rep==="boolcol")return e;if(e.rep==="mask"){let n=t.alloc(t.len,"data");return t.call("expand_mask_bool",e.maskPtr,n,t.len),e.ownsMask&&t.free(e.maskPtr),{rep:"boolcol",dataPtr:n,ownsData:true,validity:e.validity}}throw new Error("internal: expected a boolean value")}function Zn(t,e){if(e.rep==="mask")return e;if(e.rep==="boolcol"){let n=t.alloc(t.validityBytes,"mask"),r=t.view(e.dataPtr,t.len,"u8"),o=t.view(n,t.validityBytes,"u8");o.fill(0);for(let i=0;i<t.len;i++)r[i]&&S(o,i);return e.ownsData&&t.free(e.dataPtr),{rep:"mask",maskPtr:n,ownsMask:true,validity:e.validity}}throw new Error("internal: expected a boolean value")}function Jt(t){return t.validity}function Xn(t){return t==="bool"?"u8":t==="utf8"?"i32":x[t].wasm}function K(t,e,n){if(n.kind==="lit")return n.dtype==="i64"&&typeof n.value=="number"?BigInt(n.value):n.value;if(n.kind==="agg")return Gt(t,e,n).value;if(n.kind==="cast")return Qn(K(t,e,n.operand),n.from,n.dtype);throw new Error(`internal: ${n.kind} is not a scalar operand`)}var we=9223372036854776e3;function Qn(t,e,n){if(t===null)return null;if(e===n)return t;if(e==="i64"){let i=t;return n==="f64"?Number(i):n==="f32"?Math.fround(Number(i)):n==="i32"?Number(BigInt.asIntN(32,i)):n==="u32"?Number(BigInt.asUintN(32,i)):n==="bool"?i!==0n:null}if(n==="i64"){if(typeof t=="boolean")return t?1n:0n;let i=t;if(!Number.isFinite(i))return null;let a=Math.trunc(i);return a>=we||a<-we?null:BigInt(a)}if(n==="bool")return typeof t=="number"&&Number.isNaN(t)?null:t!==0&&t!==false;let r=typeof t=="boolean"?t?1:0:t;if(n==="f64")return r;if(n==="f32")return Math.fround(r);if(!Number.isFinite(r))return null;let o=Math.trunc(r);return n==="i32"?o>=-2147483648&&o<=2147483647?o:null:r<0?null:o<=4294967295?o:null}function tr(t,e,n){if(e===null||n===null)return null;let r=e,o=n;switch(t){case "gt":return r>o;case "ge":return r>=o;case "lt":return r<o;case "le":return r<=o;case "eq":return r===o;case "ne":return r!==o}}function Bt(t,e,n){if(n==="utf8")throw new _("broadcasting a utf8 scalar to a column is not supported in P3.1.");if(n==="bool"){let s=t.alloc(t.len,"data");return t.view(s,t.len,"u8").fill(e===null?0:e?1:0),{rep:"boolcol",dataPtr:s,ownsData:true,validity:e===null?nt(t):N}}let r=x[n].size,o=t.alloc(t.len*r,"data");if(n==="i64"||n==="timestamp"){let s=t.view(o,t.len,"i64"),l=e===null?0n:typeof e=="bigint"?e:BigInt(e);for(let u=0;u<t.len;u++)s[u]=l;return F(n,o,e===null?nt(t):N,null,false)}let i=t.view(o,t.len,x[n].view),a=e===null?0:e;for(let s=0;s<t.len;s++)i[s]=a;return F(n,o,e===null?nt(t):N,null,false)}function er(t,e){let n=t.alloc(t.validityBytes,"mask");return t.view(n,t.validityBytes,"u8").fill(e===true?255:0),{rep:"mask",maskPtr:n,ownsMask:true,validity:e===null?nt(t):N}}function Wt(t,e,n){if(e.ptr===0&&n.ptr===0)return N;if(n.ptr===0)return e;if(e.ptr===0)return n;let r=t.alloc(t.validityBytes,"validity");return t.call("validity_and",e.ptr,n.ptr,r,t.len),I(t,e),I(t,n),{ptr:r,owns:true}}function nt(t){let e=t.alloc(t.validityBytes,"validity");return t.view(e,t.validityBytes,"u8").fill(0),{ptr:e,owns:true}}function nr(t,e,n){t.view(e,n,"u8").fill(0);}function F(t,e,n,r,o){return {rep:"column",dtype:t,dataPtr:e,ownsData:true,validity:n,dict:r,ownsDict:o}}function ve(t,e,n){if(n.rep==="mask"){n.ownsMask&&t.free(n.maskPtr),I(t,n.validity);return}if(n.rep==="boolcol"){n.ownsData&&t.free(n.dataPtr),I(t,n.validity);return}n.ownsData&&t.free(n.dataPtr),I(t,n.validity),n.ownsDict&&st(e,n.dict);}function st(t,e){e&&(t.ctx.viewOf.forget({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"}),t.ctx.viewOf.forget({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"}),t.ctx.mod.free(e.offsetsPtr),t.ctx.mod.free(e.bytesPtr));}function De(t,e){if(e.rep==="mask")return De(t,at(t,e));let n=t.ctx,r=e.rep==="boolcol"?"bool":e.dtype,o=x[r],i;e.ownsData?(i=e.dataPtr,t.transfer(e.dataPtr)):(i=n.mod.alloc(Math.max(t.len*o.size,1)),be(t,e.dataPtr,i,t.len*o.size));let a=e.validity,s=0;a.ptr!==0&&(a.owns?(s=a.ptr,t.transfer(a.ptr)):(s=n.mod.alloc(Math.max(t.validityBytes,1)),be(t,a.ptr,s,t.validityBytes)));let l=null;return e.rep==="column"&&e.dict&&(e.ownsDict?(l=e.dict,t.transfer(e.dict.offsetsPtr),t.transfer(e.dict.bytesPtr)):l=Ce(n,e.dict)),{dtype:r,length:t.len,dataPtr:i,validityPtr:s,validityBitOffset:0,dict:l,owned:true}}function be(t,e,n,r){if(r<=0)return;let o=t.view(e,r,"u8");t.view(n,r,"u8").set(o);}function Ce(t,e){let n=(e.count+1)*4,r=t.mod.alloc(Math.max(n,1)),o=t.mod.alloc(Math.max(e.bytesLen,1));return t.viewOf({ptr:r,length:e.count+1,dtype:"i32"}).set(t.viewOf({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"})),e.bytesLen>0&&t.viewOf({ptr:o,length:e.bytesLen,dtype:"u8"}).set(t.viewOf({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"})),{count:e.count,offsetsPtr:r,bytesPtr:o,bytesLen:e.bytesLen}}var O=class extends Error{constructor(e){super(e),this.name="FrameError";}};function J(t,e){let n=It(t,e),r=n?` Did you mean '${n}'?`:"",o=e.length?` Available columns: ${e.map(i=>`'${i}'`).join(", ")}.`:"";return new O(`unknown column '${t}'.${r}${o}`)}function ke(t,e,n,r){let o=r?` ${r}`:"";return new O(`dtype mismatch in '${t}': ${e} vs ${n}.${o}`)}function C(t,e){let n=t[e];if(typeof n!="function")throw new O(`kernel export not found: ${e}`);return n}typeof process<"u"&&process.versions!=null&&process.versions.node!=null;function Zt(){throw new O("no DataFrame runtime is loaded \u2014 call `await init()` first (or pass a runtime).");}var Dt=class{col;ctx;refs;constructor(e,n){this.ctx=e,this.col=n,this.refs=1;}retain(){return this.refs++,this}release(){this.refs<=0||--this.refs===0&&Q(this.ctx,this.col);}};function Xt(t){if(t instanceof BigInt64Array)return "i64";if(t instanceof Float64Array)return "f64";if(t instanceof Float32Array)return "f32";if(t instanceof Int32Array)return "i32";if(t instanceof Uint32Array)return "u32";if(t instanceof Uint8Array)return "bool";let e=t;for(let n=0;n<e.length;n++){let r=e[n];if(r!=null){if(typeof r=="bigint")return "i64";if(typeof r=="number")return "f64";if(typeof r=="boolean")return "bool";if(typeof r=="string")return "utf8"}}return "f64"}var rr=new Set(["year","month","day","weekday","dayOfYear","quarter"]),Qt=class{constructor(e){this.series=e;}series;extract(e){let{dtype:n}=this.series;if(n==="date32"){if(!rr.has(e))throw new TypeError(`dt.${e} is not supported for date32 columns (time-of-day fields are always 0 for date32; use year/month/day/weekday/dayOfYear/quarter, or cast to timestamp first).`)}else if(n!=="timestamp")throw new TypeError(`dt accessor requires a date32 or timestamp column, got ${n}.`);let r=this.series.col,o=this.series.ctx,i=r.length,a=n==="timestamp"&&r.tz?r.tz:void 0,s=null;if(r.validityPtr!==0){let m=r.validityBitOffset,f=Math.ceil((m+i)/8),p=o.viewOf({ptr:r.validityPtr,length:f,dtype:"u8"});if(m===0)s=p;else {let d=new Uint8Array(Math.ceil(i/8));for(let h=0;h<i;h++){let y=m+h;p[y>>3]>>(y&7)&1&&(d[h>>3]|=1<<(h&7));}s=d;}}let l;if(n==="date32"){let m=o.viewOf({ptr:r.dataPtr,length:i,dtype:"i32"});({data:l}=it(m,s,e));}else {let m=o.viewOf({ptr:r.dataPtr,length:i,dtype:"i64"});({data:l}=it(m,s,e,a));}let u=new Array(i);if(s!==null)for(let m=0;m<i;m++){let f=(s[m>>3]??0)>>(m&7)&1;u[m]=f?l[m]??0:null;}else for(let m=0;m<i;m++)u[m]=l[m]??0;let c=D(o,"i32",u);return new lt(o,`${this.series.name}.dt.${e}`,c)}year(){return this.extract("year")}month(){return this.extract("month")}day(){return this.extract("day")}hour(){return this.extract("hour")}minute(){return this.extract("minute")}second(){return this.extract("second")}millisecond(){return this.extract("millisecond")}weekday(){return this.extract("weekday")}dayOfYear(){return this.extract("dayOfYear")}quarter(){return this.extract("quarter")}},te=class{constructor(e){this.series=e;}series;slice(e,n){let{dtype:r,col:o,name:i}=this.series,a=this.series.ctx;if(r!=="utf8")throw new TypeError(`str.slice requires a utf8 column, got ${r}.`);let s=o.length,l=o.dict?Z(a,o.dict):[],u=l.length,c=new Int32Array(u),m=[],f=new Map;for(let y=0;y<u;y++){let w=n===void 0?l[y].slice(e):l[y].slice(e,n),g=f.get(w);g===void 0&&(g=m.length,m.push(w),f.set(w,g)),c[y]=g;}let p=a.viewOf({ptr:o.dataPtr,length:s,dtype:"i32"}),d=new Array(s);if(o.validityPtr===0)for(let y=0;y<s;y++)d[y]=m[c[p[y]]];else {let y=o.validityBitOffset,w=A(y+s),g=a.viewOf({ptr:o.validityPtr,length:w,dtype:"u8"});for(let b=0;b<s;b++)v(g,y+b)?d[b]=m[c[p[b]]]:d[b]=null;}let h=D(a,"utf8",d);return new lt(a,`${i}.str.slice(${e}${n===void 0?"":`, ${n}`})`,h)}},lt=class{name;dtype;length;ctx;column;constructor(e,n,r){this.ctx=e,this.name=n,this.dtype=r.dtype,this.length=r.length,this.column=r;}get col(){return this.column}toArray(){return H(this.ctx,this.column)}get(e){return e<0||e>=this.length?null:this.toArray()[e]??null}values(){return this.ctx.viewOf({ptr:this.column.dataPtr,length:this.length,dtype:x[this.dtype].view})}get dt(){if(this.dtype!=="date32"&&this.dtype!=="timestamp")throw new TypeError(`dt accessor requires a date32 or timestamp column, got ${this.dtype}.`);return new Qt(this)}get str(){if(this.dtype!=="utf8")throw new TypeError(`str accessor requires a utf8 column, got ${this.dtype}.`);return new te(this)}[Symbol.iterator](){return this.toArray()[Symbol.iterator]()}toString(){return `Series '${this.name}' (${this.dtype}, ${this.length} rows)`}};function Ee(t){return t==="bool"?"u8":t==="utf8"?"i32":x[t].wasm}function ut(t,e){if(e.validityPtr===0)return {ptr:0,owns:false};let n=e.validityBitOffset;if((n&7)===0)return {ptr:e.validityPtr+(n>>3),owns:false};let r=e.length,o=A(r),i=t.mod.alloc(Math.max(o,1)),a=t.viewOf({ptr:e.validityPtr,length:A(n+r),dtype:"u8"}),s=t.viewOf({ptr:i,length:o,dtype:"u8"});s.fill(0);for(let l=0;l<r;l++)v(a,n+l)&&S(s,l);return {ptr:i,owns:true}}function ct(t,e){e.owns&&e.ptr!==0&&(t.viewOf.forget({ptr:e.ptr,length:1,dtype:"u8"}),t.mod.free(e.ptr));}function Te(t,e){let n=(e.count+1)*4,r=t.mod.alloc(Math.max(n,1)),o=t.mod.alloc(Math.max(e.bytesLen,1));return t.viewOf({ptr:r,length:e.count+1,dtype:"i32"}).set(t.viewOf({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"})),e.bytesLen>0&&t.viewOf({ptr:o,length:e.bytesLen,dtype:"u8"}).set(t.viewOf({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"})),{count:e.count,offsetsPtr:r,bytesPtr:o,bytesLen:e.bytesLen}}function Ie(t,e){return Math.max(e*x[t].size,1)}function ee(t,e,n,r){let{ctx:o,wasm:i}=t,a=Ee(e.dtype),s=o.mod.alloc(Ie(e.dtype,r));C(i,`gather_${a}`)(e.dataPtr,n,r,s);let l=0;if(e.validityPtr!==0){let c=ut(o,e),m=A(r),f=o.mod.alloc(Math.max(m,1));o.viewOf({ptr:f,length:Math.max(m,1),dtype:"u8"}).fill(0),C(i,"gather_validity")(c.ptr,n,r,f),ct(o,c),l=f;}let u=e.dict?Te(o,e.dict):null;return {dtype:e.dtype,length:r,dataPtr:s,validityPtr:l,validityBitOffset:0,dict:u,owned:true}}function Oe(t,e,n){let{ctx:r,wasm:o}=t,i=e.length>0?e[0].length:0,a=r.mod.alloc(Math.max(i*4,1)),s=r.viewOf({ptr:a,length:i,dtype:"i32"});for(let c=0;c<i;c++)s[c]=c;let l=C(o,"argsort_i32").length>=6,u=l?r.mod.alloc(Math.max(i*4,1)):0;try{for(let c=e.length-1;c>=0;c--)or(t,e[c],n[c]===!0,a,i,u,l);}finally{u!==0&&(r.viewOf.forget({ptr:u,length:i,dtype:"i32"}),r.mod.free(u));}return {ptr:a,len:i}}function ne(t,e,n,r,o,i,a,s,l){let u=C(t,e);l?u(n,r,o,i,a,s):u(n,r,o,i,a);}function or(t,e,n,r,o,i,a){let{ctx:s,wasm:l}=t,u=n?1:0,c=ut(s,e);try{if(e.dtype==="utf8"){let m=ir(t,e,o);try{ne(l,"argsort_i32",m,c.ptr,r,o,u,i,a);}finally{s.viewOf.forget({ptr:m,length:o,dtype:"i32"}),s.mod.free(m);}}else if(e.dtype==="bool"){let m=ar(t,e,o);try{ne(l,"argsort_i32",m,c.ptr,r,o,u,i,a);}finally{s.viewOf.forget({ptr:m,length:o,dtype:"i32"}),s.mod.free(m);}}else {let m=x[e.dtype].wasm;ne(l,`argsort_${m}`,e.dataPtr,c.ptr,r,o,u,i,a);}}finally{ct(s,c);}}function ir(t,e,n){let{ctx:r}=t,o=e.dict?Z(r,e.dict):[],i=o.map((c,m)=>m).sort((c,m)=>{let f=o[c],p=o[m];return f<p?-1:f>p?1:0}),a=new Int32Array(o.length);for(let c=0;c<i.length;c++)a[i[c]]=c;let s=r.mod.alloc(Math.max(n*4,1)),l=r.viewOf({ptr:e.dataPtr,length:n,dtype:"i32"}),u=r.viewOf({ptr:s,length:n,dtype:"i32"});for(let c=0;c<n;c++){let m=l[c];u[c]=m>=0&&m<a.length?a[m]:0;}return s}function ar(t,e,n){let{ctx:r}=t,o=r.mod.alloc(Math.max(n*4,1)),i=r.viewOf({ptr:e.dataPtr,length:n,dtype:"bool"}),a=r.viewOf({ptr:o,length:n,dtype:"i32"});for(let s=0;s<n;s++)a[s]=i[s];return o}function re(t,e){let n=0,r={};for(let{name:o,col:i}of e){let a=i.length,s=i.validityBitOffset,l=i.validityPtr===0?null:t.viewOf({ptr:i.validityPtr,length:A(s+a),dtype:"u8"});if(i.dtype==="utf8"){let u=t.viewOf({ptr:i.dataPtr,length:a,dtype:"i32"}),c=i.dict;Object.defineProperty(r,o,{enumerable:true,get(){return l&&!v(l,s+n)?null:j(t,c,u[n])}});}else if(i.dtype==="bool"){let u=t.viewOf({ptr:i.dataPtr,length:a,dtype:"bool"});Object.defineProperty(r,o,{enumerable:true,get(){return l&&!v(l,s+n)?null:u[n]!==0}});}else if(i.dtype==="i64"){let u=t.viewOf({ptr:i.dataPtr,length:a,dtype:"i64"});Object.defineProperty(r,o,{enumerable:true,get(){return l&&!v(l,s+n)?null:u[n]}});}else {let u=t.viewOf({ptr:i.dataPtr,length:a,dtype:x[i.dtype].view});Object.defineProperty(r,o,{enumerable:true,get(){return l&&!v(l,s+n)?null:u[n]}});}}return {at(o){return n=o,r}}}function Re(t){if(t<=1)return 1;let e=1;for(;e<t;)e<<=1;return e}var Se=4,Ct=16;function kt(t,e){if(e===0)return t.alloc(0);let n=t.alloc(e);if(n===0)throw new Error(`[hash stubs] OOM: alloc(${e}) returned 0`);return new Uint8Array(t.memory.buffer,n,e).fill(0),n}function Me(t,e,n,r,o){let i=Math.max(Se,Re(2*Math.max(n,1))),a=0;try{for(a=kt(t,i*Ct);;){let s=t.group_build(e,n,a,i,r);if(s!==-1)return s;t.free(a),a=0,i*=2,a=kt(t,i*Ct);}}finally{a!==0&&t.free(a);}}function _e(t,e,n,r,o,i,a,s,l){let u=Math.max(Se,Re(2*Math.max(s,1))),c=Math.max(1,o+s),m=0,f=0,p=0;try{if(m=kt(t,u*Ct),f=t.alloc(c*4),f===0)throw new Error("[hash stubs] OOM: out_l_idx alloc failed");if(p=t.alloc(c*4),p===0)throw new Error("[hash stubs] OOM: out_r_idx alloc failed");for(;;){let d=e(n,r,o,i,a,s,m,u,f,p,c);if(d===-1){t.free(m),m=0,u*=2,m=kt(t,u*Ct);continue}if(d>c){if(t.free(f),f=0,t.free(p),p=0,c=d,f=t.alloc(c*4),f===0)throw new Error("[hash stubs] OOM: out_l_idx resize failed");if(p=t.alloc(c*4),p===0)throw new Error("[hash stubs] OOM: out_r_idx resize failed");t.free(m),m=0,m=kt(t,u*Ct);continue}let h=new Int32Array(t.memory.buffer,f,d).slice(),y=new Int32Array(t.memory.buffer,p,d).slice();return {count:d,lIdx:h,rIdx:y}}}finally{m!==0&&t.free(m),f!==0&&t.free(f),p!==0&&t.free(p);}}function Be(t,e,n,r,o,i,a,s){return _e(t,(l,u,c,m,f,p,d,h,y,w,g)=>t.join_hash_inner(l,u,c,m,f,p,d,h,y,w,g),e,n,r,o,i,a)}function Ne(t,e,n,r,o,i,a,s){return _e(t,(l,u,c,m,f,p,d,h,y,w,g)=>t.join_hash_left(l,u,c,m,f,p,d,h,y,w,g),e,n,r,o,i,a)}var sr=new Set(["sum","mean","min","max","count","size","nunique","std","var","first","last"]),Nt=class{src;keys;constructor(e,n){this.src=e,this.keys=n;for(let r of n)if(e.dtypeOf(r)===void 0)throw J(r,e.columnNames())}agg(e){let{rt:n}=this.src,{ctx:r}=n,o=this.src.length,i=this.keys.map(u=>this.src.getColumn(u)),a=this.buildPlan(e),s=ur(n,i),l=[];try{let u=s.firstRow,c=[];for(let p=0;p<this.keys.length;p++){let d=mr(n,i[p],u);l.push(d),c.push({name:this.keys[p],col:d});}let m=r.viewOf({ptr:s.groupIdsPtr,length:o,dtype:"i32"});for(let p of a){let d=pr(n,p,m,s.groupCount,o);l.push(d),c.push({name:p.outName,col:d});}let f=this.src.buildResult(c);return l.length=0,f}finally{for(let u of l)Q(r,u);for(let u of a)u.ownsOperand&&u.operand&&Q(r,u.operand);r.viewOf.forget({ptr:s.groupIdsPtr,length:o,dtype:"i32"}),r.mod.free(s.groupIdsPtr);}}buildPlan(e){let n=[];for(let[r,o]of Object.entries(e))if(o instanceof M)n.push(this.exprPlan(r,o));else if(Array.isArray(o))for(let i of o)n.push(this.columnPlan(`${r}_${i}`,r,i));else n.push(this.columnPlan(r,r,o));return n}columnPlan(e,n,r){if(!sr.has(r))throw new O(`unknown aggregation '${r}' for '${n}'.`);if(r==="size")return {outName:e,op:r,operand:null,operandDtype:"i32",ownsOperand:false,resultDtype:"i32"};let o=this.src.getColumn(n);if(!o)throw J(n,this.src.columnNames());let i=lr(r,o.dtype);return {outName:e,op:r,operand:o,operandDtype:o.dtype,ownsOperand:false,resultDtype:i}}exprPlan(e,n){let r=n.node;if(r.kind!=="agg")throw new O(`agg value for '${e}' must be a top-level aggregation (e.g. col('a').sum()).`);let o=r.op,i=qt(r.operand,this.src),a=bt(o,i),s=vt(r.operand,this.src).execute();if(s.kind!=="column"||!s.column)throw new O(`aggregation operand for '${e}' did not produce a column.`);return {outName:e,op:o,operand:s.column,operandDtype:i,ownsOperand:true,resultDtype:a}}};function lr(t,e){return t==="size"?"i32":bt(t,e)}function ur(t,e){let{ctx:n,wasm:r}=t,o=e.length>0?e[0].length:0,i=n.mod.alloc(Math.max(o*8,1));try{for(let m=0;m<e.length;m++){let f=e[m],p=cr(t,f,o),d=ut(n,f);try{if(m===0)C(r,`hash_${p.tok}`)(p.ptr,d.ptr,i,o);else {let h=n.mod.alloc(Math.max(o*8,1));try{C(r,`hash_${p.tok}`)(p.ptr,d.ptr,h,o),C(r,"hash_combine")(i,h,o);}finally{n.viewOf.forget({ptr:h,length:o,dtype:"u8"}),n.mod.free(h);}}}finally{ct(n,d),p.free();}}let a=n.mod.alloc(Math.max(o*4,1)),s=Me(r,i,o,a),l=n.viewOf({ptr:a,length:o,dtype:"i32"}),u=new Int32Array(s),c=0;for(let m=0;m<o&&c<s;m++)l[m]===c&&(u[c++]=m);return {groupIdsPtr:a,groupCount:s,firstRow:u}}finally{n.viewOf.forget({ptr:i,length:o,dtype:"u8"}),n.mod.free(i);}}function cr(t,e,n){let{ctx:r}=t;if(e.dtype==="utf8")return {ptr:e.dataPtr,tok:"i32",free(){}};if(e.dtype==="bool"){let o=r.mod.alloc(Math.max(n*4,1)),i=r.viewOf({ptr:e.dataPtr,length:n,dtype:"bool"}),a=r.viewOf({ptr:o,length:n,dtype:"i32"});for(let s=0;s<n;s++)a[s]=i[s];return {ptr:o,tok:"i32",free(){r.viewOf.forget({ptr:o,length:n,dtype:"i32"}),r.mod.free(o);}}}return {ptr:e.dataPtr,tok:x[e.dtype].wasm,free(){}}}function mr(t,e,n){let{ctx:r}=t,o=n.length,i=e.validityPtr===0?null:r.viewOf({ptr:e.validityPtr,length:A(e.validityBitOffset+e.length),dtype:"u8"}),a=e.validityBitOffset;if(e.dtype==="utf8"){let c=r.viewOf({ptr:e.dataPtr,length:e.length,dtype:"i32"}),m=e.dict,f=new Array(o);for(let p=0;p<o;p++){let d=n[p];f[p]=i&&!v(i,a+d)?null:j(r,m,c[d]);}return D(r,"utf8",f)}if(e.dtype==="i64"){let c=r.viewOf({ptr:e.dataPtr,length:e.length,dtype:"i64"}),m=new Array(o);for(let f=0;f<o;f++){let p=n[f];m[f]=i&&!v(i,a+p)?null:c[p];}return D(r,"i64",m)}let s=e.dtype==="bool",l=r.viewOf({ptr:e.dataPtr,length:e.length,dtype:x[e.dtype].view}),u=new Array(o);for(let c=0;c<o;c++){let m=n[c];i&&!v(i,a+m)?u[c]=null:u[c]=s?l[m]!==0:l[m];}return D(r,e.dtype,u)}function pr(t,e,n,r,o){let{ctx:i}=t,{op:a}=e;if(a==="size"){let w=new Array(r).fill(0);for(let g=0;g<o;g++){let b=n[g];w[b]=w[b]+1;}return D(i,"i32",w)}let s=e.operand,l=s.validityPtr===0?null:i.viewOf({ptr:s.validityPtr,length:A(s.validityBitOffset+o),dtype:"u8"}),u=s.validityBitOffset,c=w=>l===null||v(l,u+w);if(a==="count"){let w=new Array(r).fill(0);for(let g=0;g<o;g++)if(c(g)){let b=n[g];w[b]=w[b]+1;}return D(i,"i32",w)}if(a==="first"||a==="last")return yr(t,e,n,r,o,c);if(a==="nunique")return dr(t,e,n,r,o,c);if(a==="std"||a==="var")return fr(t,e,n,r,o,c);if(s.dtype==="i64"){let w=i.viewOf({ptr:s.dataPtr,length:o,dtype:"i64"});if(a==="sum"){let k=new Array(r).fill(0n);for(let T=0;T<o;T++)if(c(T)){let L=n[T];k[L]=k[L]+w[T];}return D(i,"i64",k)}if(a==="mean"){let k=new Float64Array(r),T=new Int32Array(r);for(let $=0;$<o;$++){if(!c($))continue;let Pt=n[$];k[Pt]=k[Pt]+Number(w[$]),T[Pt]=T[Pt]+1;}let L=new Array(r);for(let $=0;$<r;$++)L[$]=T[$]>0?k[$]/T[$]:null;return D(i,"f64",L)}let g=new Array(r).fill(null),b=a==="min";for(let k=0;k<o;k++){if(!c(k))continue;let T=n[k],L=w[k];g[T]===null?g[T]=L:g[T]=b?L<g[T]?L:g[T]:L>g[T]?L:g[T];}return D(i,"i64",g)}let m=i.viewOf({ptr:s.dataPtr,length:o,dtype:x[s.dtype].view}),f=new Array(r).fill(null);if(a==="sum"){let w=new Float64Array(r);for(let g=0;g<o;g++)if(c(g)){let b=n[g];w[b]=w[b]+m[g];}for(let g=0;g<r;g++)f[g]=w[g];return D(i,e.resultDtype,f)}if(a==="mean"){let w=new Float64Array(r),g=new Int32Array(r);for(let b=0;b<o;b++){if(!c(b))continue;let k=n[b];w[k]=w[k]+m[b],g[k]=g[k]+1;}for(let b=0;b<r;b++)f[b]=g[b]>0?w[b]/g[b]:null;return D(i,"f64",f)}let p=new Int32Array(r),d=new Int32Array(r),h=new Float64Array(r),y=a==="min";for(let w=0;w<o;w++){if(!c(w))continue;let g=n[w];p[g]=p[g]+1;let b=m[w];Number.isNaN(b)||(d[g]===0?h[g]=b:h[g]=y?Math.min(h[g],b):Math.max(h[g],b),d[g]=d[g]+1);}for(let w=0;w<r;w++)f[w]=p[w]===0?null:d[w]===0?NaN:h[w];return D(i,e.resultDtype,f)}function fr(t,e,n,r,o,i){let{ctx:a}=t,s=e.operand,l=s.dtype==="i64",u=l?a.viewOf({ptr:s.dataPtr,length:o,dtype:"i64"}):a.viewOf({ptr:s.dataPtr,length:o,dtype:x[s.dtype].view}),c=new Float64Array(r),m=new Int32Array(r);for(let y=0;y<o;y++){if(!i(y))continue;let w=n[y];c[w]=c[w]+(l?Number(u[y]):u[y]),m[w]=m[w]+1;}let f=new Float64Array(r);for(let y=0;y<r;y++)f[y]=m[y]>0?c[y]/m[y]:0;let p=new Float64Array(r);for(let y=0;y<o;y++){if(!i(y))continue;let w=n[y],b=(l?Number(u[y]):u[y])-f[w];p[w]=p[w]+b*b;}let d=new Array(r),h=e.op==="std";for(let y=0;y<r;y++)if(m[y]<2)d[y]=null;else {let w=p[y]/(m[y]-1);d[y]=h?Math.sqrt(w):w;}return D(a,"f64",d)}function dr(t,e,n,r,o,i){let{ctx:a}=t,s=e.operand,l=new Array(r),u=s.dtype==="utf8"||s.dtype==="bool"?a.viewOf({ptr:s.dataPtr,length:o,dtype:s.dtype==="utf8"?"i32":"bool"}):s.dtype==="i64"?a.viewOf({ptr:s.dataPtr,length:o,dtype:"i64"}):a.viewOf({ptr:s.dataPtr,length:o,dtype:x[s.dtype].view});for(let m=0;m<o;m++){if(!i(m))continue;let f=n[m];(l[f]??=new Set).add(u[m]);}let c=new Array(r);for(let m=0;m<r;m++)c[m]=l[m]?l[m].size:0;return D(a,"i32",c)}function yr(t,e,n,r,o,i){let{ctx:a}=t,s=e.operand,l=e.op==="first",u=new Int32Array(r).fill(-1);for(let p=0;p<o;p++){if(!i(p))continue;let d=n[p];l?u[d]===-1&&(u[d]=p):u[d]=p;}let c=new Array(r);if(s.dtype==="utf8"){let p=a.viewOf({ptr:s.dataPtr,length:o,dtype:"i32"}),d=s.dict;for(let h=0;h<r;h++)c[h]=u[h]===-1?null:j(a,d,p[u[h]]);return D(a,"utf8",c)}if(s.dtype==="i64"){let p=a.viewOf({ptr:s.dataPtr,length:o,dtype:"i64"});for(let d=0;d<r;d++){let h=u[d];c[d]=h===-1?null:p[h];}return D(a,"i64",c)}let m=s.dtype==="bool",f=a.viewOf({ptr:s.dataPtr,length:o,dtype:x[s.dtype].view});for(let p=0;p<r;p++){let d=u[p];c[p]=d===-1?null:m?f[d]!==0:f[d];}return D(a,s.dtype,c)}function Ke(t,e,n){let r=n.how??"inner",o=Array.isArray(n.on)?n.on:[n.on];if(o.length===0)throw new O("join requires at least one key in `on`.");let i=t.rt,{ctx:a}=i,s=t.length,l=e.length;for(let c of o){let m=t.dtypeOf(c);if(m===void 0)throw J(c,t.columnNames());let f=e.dtypeOf(c);if(f===void 0)throw J(c,e.columnNames());if(m!==f)throw ke("join",m,f,`join key '${c}' must have one dtype.`)}let u=o.map(c=>Ar(i,t.getColumn(c),e.getColumn(c),s,l));try{let c=qe(i,u.map(h=>({ptr:h.leftPtr,tok:h.tok})),s),m=qe(i,u.map(h=>({ptr:h.rightPtr,tok:h.tok})),l),f=ze(i,o.map(h=>t.getColumn(h)),s),p=ze(i,o.map(h=>e.getColumn(h)),l),d;try{let h=i.wasm;d=r==="left"?Ne(h,c,f.ptr,s,m,p.ptr,l):Be(h,c,f.ptr,s,m,p.ptr,l);}finally{a.viewOf.forget({ptr:c,length:s,dtype:"u8"}),a.mod.free(c),a.viewOf.forget({ptr:m,length:l,dtype:"u8"}),a.mod.free(m),mt(i,f.ptr,f.owns),mt(i,p.ptr,p.owns);}return hr(t,e,o,d,r)}finally{for(let c of u)c.free();}}function hr(t,e,n,r,o){let i=t.rt,{ctx:a}=i,s=r.count,l=new Set(n),u=t.columnNames(),c=e.columnNames().filter(y=>!l.has(y)),m=new Set(u),f=Ve(i,r.lIdx,s),p,d=null;o==="left"?(d=wr(r.rIdx,s),p=gr(i,r.rIdx,s)):p=Ve(i,r.rIdx,s);let h=[];try{let y=[];for(let g of u){let b=Le(i,t.getColumn(g),f,s,null);h.push(b),y.push({name:g,col:b});}for(let g of c){let b=Le(i,e.getColumn(g),p,s,d);h.push(b);let k=m.has(g)?`${g}_right`:g;y.push({name:k,col:b});}let w=t.buildResult(y);return h.length=0,w}finally{for(let y of h)Q(a,y);a.mod.free(f),a.mod.free(p);}}function Ve(t,e,n){let r=t.ctx.mod.alloc(Math.max(n*4,1));return n>0&&new Int32Array(t.wasm.memory.buffer,r,n).set(e),r}function gr(t,e,n){let r=t.ctx.mod.alloc(Math.max(n*4,1));if(n>0){let o=new Int32Array(t.wasm.memory.buffer,r,n);for(let i=0;i<n;i++)o[i]=e[i]<0?0:e[i];}return r}function wr(t,e){let n=false;for(let i=0;i<e;i++)if(t[i]<0){n=true;break}if(!n)return null;let r=A(e),o=new Uint8Array(r).fill(255);for(let i=0;i<e;i++)if(t[i]<0){let a=i>>3;o[a]=o[a]&~(1<<(i&7));}return e&7&&(o[r-1]=o[r-1]&(1<<(e&7))-1),o}function Fe(t,e,n,r,o){return {dtype:t,length:e,dataPtr:n,validityPtr:r,validityBitOffset:0,dict:o,owned:true}}function br(t,e){let{count:n,offsetsPtr:r,bytesPtr:o,bytesLen:i}=e,a=(n+1)*4,s=t.ctx.mod.alloc(a),l=t.ctx.mod.alloc(Math.max(i,1)),u=t.wasm.memory.buffer;return new Uint8Array(u,s,a).set(new Uint8Array(u,r,a)),i>0&&new Uint8Array(u,l,i).set(new Uint8Array(u,o,i)),{count:n,offsetsPtr:s,bytesPtr:l,bytesLen:i}}function $e(t,e,n,r,o){let i=e.validityPtr!==0,a=o!==null;if(!i&&!a)return 0;let s=A(r);if(!a){let u=t.ctx.mod.alloc(Math.max(s,1));return C(t.wasm,"gather_validity")(e.validityPtr,n,r,u),u}let l=t.ctx.mod.alloc(Math.max(s,1));if(new Uint8Array(t.wasm.memory.buffer,l,s).set(o),i){let u=t.ctx.mod.alloc(Math.max(s,1));try{C(t.wasm,"gather_validity")(e.validityPtr,n,r,u);let c=new Uint8Array(t.wasm.memory.buffer,l,s),m=new Uint8Array(t.wasm.memory.buffer,u,s);for(let f=0;f<s;f++)c[f]=c[f]&m[f];}finally{t.ctx.mod.free(u);}}return l}var xr={bool:"u8"};function Le(t,e,n,r,o){let{ctx:i}=t;if(e.dtype==="utf8"){let u=i.mod.alloc(Math.max(r*4,1));r>0&&C(t.wasm,"gather_i32")(e.dataPtr,n,r,u);let c=br(t,e.dict);return Fe("utf8",r,u,$e(t,e,n,r,o),c)}let a=x[e.dtype],s=xr[a.wasm]??a.wasm,l=i.mod.alloc(Math.max(r*a.size,1));return r>0&&C(t.wasm,`gather_${s}`)(e.dataPtr,n,r,l),Fe(e.dtype,r,l,$e(t,e,n,r,o),null)}var Ye="i64_direct";function Ar(t,e,n,r,o){if(e.dtype==="utf8"){let i=Ue(t,e,r),a=Ue(t,n,o);return {leftPtr:i,rightPtr:a,tok:Ye,free(){mt(t,i,true),mt(t,a,true);}}}if(e.dtype==="bool"){let i=je(t,e,r),a=je(t,n,o);return {leftPtr:i,rightPtr:a,tok:"i32",free(){mt(t,i,true),mt(t,a,true);}}}return {leftPtr:e.dataPtr,rightPtr:n.dataPtr,tok:x[e.dtype].wasm,free(){}}}function Ue(t,e,n){let{ctx:r,wasm:o}=t,i=e.dict,a=i.count,s=r.mod.alloc(Math.max(a*8,1));try{C(o,"hash_utf8_dict")(i.offsetsPtr,i.bytesPtr,a,s);let l=r.mod.alloc(Math.max(n*8,1));return a===0||n===0||C(o,"gather_i64")(s,e.dataPtr,n,l),l}finally{r.mod.free(s);}}function je(t,e,n){let{ctx:r}=t,o=r.mod.alloc(Math.max(n*4,1)),i=r.viewOf({ptr:e.dataPtr,length:n,dtype:"bool"}),a=r.viewOf({ptr:o,length:n,dtype:"i32"});for(let s=0;s<n;s++)a[s]=i[s];return o}function qe(t,e,n){let{ctx:r,wasm:o}=t,i=r.mod.alloc(Math.max(n*8,1)),a=n*8;for(let s=0;s<e.length;s++){let l=e[s];if(l.tok===Ye)if(s===0){if(a>0){let u=o.memory.buffer;new Uint8Array(u,i,a).set(new Uint8Array(u,l.ptr,a));}}else C(o,"hash_combine")(i,l.ptr,n);else if(s===0)C(o,`hash_${l.tok}`)(l.ptr,0,i,n);else {let u=r.mod.alloc(Math.max(n*8,1));try{C(o,`hash_${l.tok}`)(l.ptr,0,u,n),C(o,"hash_combine")(i,u,n);}finally{r.viewOf.forget({ptr:u,length:n,dtype:"u8"}),r.mod.free(u);}}}return i}function ze(t,e,n){let{ctx:r}=t,o=e.filter(u=>u.validityPtr!==0);if(o.length===0)return {ptr:0,owns:false};let i=A(n),a=r.mod.alloc(Math.max(i,1)),s=r.viewOf({ptr:a,length:Math.max(i,1),dtype:"u8"});s.fill(0);let l=o.map(u=>({view:r.viewOf({ptr:u.validityPtr,length:A(u.validityBitOffset+n),dtype:"u8"}),bitOff:u.validityBitOffset}));for(let u=0;u<n;u++){let c=true;for(let m of l)if(!v(m.view,m.bitOff+u)){c=false;break}c&&S(s,u);}return {ptr:a,owns:true}}function mt(t,e,n){n&&e!==0&&(t.ctx.viewOf.forget({ptr:e,length:1,dtype:"u8"}),t.ctx.mod.free(e));}function We(t,e){if(t===null)return "null";if(typeof t=="boolean")return t?"true":"false";if(typeof t=="string")return t.length>24?`${t.slice(0,23)}\u2026`:t;if(e==="date32"&&typeof t=="number")return new Date(t*864e5).toISOString().slice(0,10);if(typeof t=="bigint")return e==="timestamp"?new Date(Number(t)).toISOString():String(t);if(Number.isNaN(t))return "NaN";if(t===1/0)return "inf";if(t===-1/0)return "-inf";if(Number.isInteger(t))return String(t);if(e==="f32"||e==="f64"){let n=t.toPrecision(6);return n.includes(".")&&!n.includes("e")?n.replace(/0+$/,"").replace(/\.$/,""):n}return String(t)}function He(t){return t==="utf8"||t==="bool"?"left":"right"}function vr(t,e,n){let r=e-t.length;return r<=0?t:n==="right"?" ".repeat(r)+t:t+" ".repeat(r)}function Ge(t,e,n,r,o){let i=t.length,a=new Array(i);for(let c=0;c<i;c++){let m=Math.max(t[c].length,e[c].length);for(let f of n)m=Math.max(m,f[c].length);a[c]=m;}let s=c=>c.map((m,f)=>vr(m,a[f],r[f])).join(" "),l=a.map(c=>"\u2500".repeat(c)).join(" "),u=[];u.push(s(t)),u.push(s(e)),u.push(l);for(let c of n)u.push(s(c));return o&&u.push(o),u.join(`
|
|
2
|
+
`)}var Cr=new Set(["f64","f32","i32","u32","i64"]),Vt=class t{length;_rt;entries;byName;disposed=false;constructor(e,n,r){this._rt=e,this.entries=n,this.length=r,this.byName=new Map(n.map(o=>[o.name,o]));}get ctx(){return this._rt.ctx}get wasm(){return this._rt.wasm}get rt(){return this._rt}getColumn(e){return this.byName.get(e)?.col}dtypeOf(e){return this.byName.get(e)?.col.dtype}columnNames(){return this.entries.map(e=>e.name)}buildResult(e){let n=e.length>0?e[0].col.length:this.length;return t.fromRoots(this._rt,e,n)}static fromColumns(e,n={}){let r=n.runtime??Zt(),o=[],i=-1;for(let[a,s]of Object.entries(e)){let l=n.dtypes?.[a]??Xt(s),u=l==="timestamp"?n.tzs?.[a]:void 0,c=D(r.ctx,l,s,u);if(i===-1)i=c.length;else if(c.length!==i)throw new O(`column '${a}' has length ${c.length}, expected ${i}.`);o.push({name:a,col:c});}return t.fromRoots(r,o,i===-1?0:i)}static fromRecords(e,n={}){n.runtime??Zt();let o=[],i=new Set;for(let s of e)for(let l of Object.keys(s))i.has(l)||(i.add(l),o.push(l));let a={};for(let s of o){let l=new Array(e.length);for(let u=0;u<e.length;u++){let c=e[u][s];l[u]=c===void 0?null:c;}a[s]=l;}return t.fromColumns(a,n)}static fromRoots(e,n,r){let o=n.map(i=>({name:i.name,col:i.col,owner:new Dt(e.ctx,i.col)}));return new t(e,o,r)}static _adoptColumns(e,n,r){return t.fromRoots(e,n,r)}get shape(){return [this.length,this.entries.length]}get columns(){return this.columnNames()}get dtypes(){let e={};for(let n of this.entries)e[n.name]=n.col.dtype;return e}col(e){let n=this.entryOf(e);return new lt(this._rt.ctx,e,n.col)}select(e){return new t(this._rt,this.shareEntries(e),this.length)}drop(e){let n=new Set(e);for(let o of e)if(!this.byName.has(o))throw J(o,this.columnNames());let r=this.entries.filter(o=>!n.has(o.name)).map(o=>o.name);return new t(this._rt,this.shareEntries(r),this.length)}withColumn(e,n,r={}){let o=this.materializeColumn(n,r),i=new Dt(this._rt.ctx,o),a={name:e,col:o,owner:i},s=[],l=false;for(let u of this.entries)u.name===e?(s.push(a),l=true):s.push(this.retainEntry(u));return l||s.push(a),new t(this._rt,s,this.length)}assign(e,n,r){return this.withColumn(e,n,r)}filter(e){let n=Ht(e,this).execute();try{let r=this.entries.map(o=>({name:o.name,col:n.compact(o.col)}));return t.fromRoots(this._rt,r,n.count)}finally{n.free();}}filterFn(e){let n=re(this._rt.ctx,this.entries),r=[];for(let o=0;o<this.length;o++)e(n.at(o))&&r.push(o);return this.gatherRows(r)}mapFn(e){let n=re(this._rt.ctx,this.entries),r=new Array(this.length);for(let o=0;o<this.length;o++)r[o]=e(n.at(o));return r}sortValues(e,n={}){let r=typeof e=="string"?[e]:[...e];if(r.length===0)throw new O("sortValues requires at least one key.");let o=r.map(s=>this.entryOf(s).col),i=Array.isArray(n.descending)?r.map((s,l)=>n.descending[l]===true):r.map(()=>n.descending===true),a=Oe(this._rt,o,i);try{let s=this.entries.map(l=>({name:l.name,col:ee(this._rt,l.col,a.ptr,a.len)}));return t.fromRoots(this._rt,s,a.len)}finally{this._rt.ctx.viewOf.forget({ptr:a.ptr,length:a.len,dtype:"i32"}),this._rt.ctx.mod.free(a.ptr);}}groupby(e){let n=typeof e=="string"?[e]:[...e];if(n.length===0)throw new O("groupby requires at least one key.");return new Nt(this,n)}join(e,n){return Ke(this,e,n)}head(e=5){return this.sliceRange(0,e)}tail(e=5){return this.sliceRange(Math.max(0,this.length-e),this.length)}slice(e,n=this.length){return this.sliceRange(e,n)}sliceRange(e,n){let r=Math.max(0,Math.min(e,this.length)),o=Math.max(r,Math.min(n,this.length)),i=this.entries.map(a=>({name:a.name,col:Ft(a.col,r,o),owner:a.owner.retain()}));return new t(this._rt,i,o-r)}toColumns(){let e={};for(let n of this.entries)e[n.name]=H(this._rt.ctx,n.col);return e}toRecords(){let e=this.toColumns(),n=this.columnNames(),r=new Array(this.length);for(let o=0;o<this.length;o++){let i={};for(let a of n)i[a]=e[a][o];r[o]=i;}return r}describe(){let e=["count","mean","std","min","25%","50%","75%","max"],n=this.entries.filter(i=>Cr.has(i.col.dtype)),r={statistic:e},o={statistic:"utf8"};for(let i of n)r[i.name]=kr(H(this._rt.ctx,i.col)),o[i.name]="f64";return t.fromColumns(r,{runtime:this._rt,dtypes:o})}dispose(){if(!this.disposed){this.disposed=true;for(let e of this.entries)e.owner.release();}}toString(){return this.render()}[Symbol.for("nodejs.util.inspect.custom")](){return this.render()}entryOf(e){let n=this.byName.get(e);if(!n)throw J(e,this.columnNames());return n}retainEntry(e){return e.owner.retain(),{name:e.name,col:e.col,owner:e.owner}}shareEntries(e){return e.map(n=>this.retainEntry(this.entryOf(n)))}materializeColumn(e,n){if(e instanceof M){let o=vt(e,this).execute();if(o.kind==="column"&&o.column)return o.column;let i=o.scalar,a=new Array(this.length).fill(i.value);return D(this._rt.ctx,i.dtype,a)}let r=n.dtype??Xt(e);if(e.length!==this.length)throw new O(`withColumn value has length ${e.length}, expected ${this.length}.`);return D(this._rt.ctx,r,e)}gatherRows(e){let{ctx:n}=this._rt,r=e.length,o=n.mod.alloc(Math.max(r*4,1));try{let i=n.viewOf({ptr:o,length:r,dtype:"i32"});for(let s=0;s<r;s++)i[s]=e[s];let a=this.entries.map(s=>({name:s.name,col:ee(this._rt,s.col,o,r)}));return t.fromRoots(this._rt,a,r)}finally{n.viewOf.forget({ptr:o,length:r,dtype:"i32"}),n.mod.free(o);}}render(){let e=this.columnNames();if(e.length===0)return `Empty DataFrame [${this.length} rows \xD7 0 columns]`;let n=this.entries.map(c=>c.col.dtype),r=n.map(He),o=5,i=5,a=[],s=this.length>o+i,l=(c,m)=>{let f=this.entries.map(p=>H(this._rt.ctx,Ft(p.col,c,m)));for(let p=0;p<m-c;p++)a.push(f.map((d,h)=>We(d[p],n[h])));};s?(l(0,o),a.push(e.map(()=>"\u2026")),l(this.length-i,this.length)):l(0,this.length);let u=`[${this.length} rows \xD7 ${e.length} columns]`;return Ge(e,n,a,r,u)}};function kr(t){let e=[];for(let l of t){if(l===null)continue;let u=typeof l=="bigint"?Number(l):l;Number.isFinite(u)&&e.push(u);}let n=e.length;if(n===0)return [0,null,null,null,null,null,null,null];let r=e.slice().sort((l,u)=>l-u),i=e.reduce((l,u)=>l+u,0)/n,a=n>=2?Math.sqrt(e.reduce((l,u)=>l+(u-i)*(u-i),0)/(n-1)):null,s=l=>{if(r.length===1)return r[0];let u=l*(r.length-1),c=Math.floor(u),m=Math.ceil(u),f=u-c;return r[c]+(r[m]-r[c])*f};return [n,i,a,r[0],s(.25),s(.5),s(.75),r[r.length-1]]}var Or=new Set(["UNCOMPRESSED","SNAPPY",void 0]),Rr={timestampFromMilliseconds:t=>t,timestampFromMicroseconds:t=>t/1000n,timestampFromNanoseconds:t=>t/1000000n,dateFromDays:t=>t,stringFromBytes:t=>t!=null?new TextDecoder().decode(t):null,jsonFromBytes:t=>t!=null?JSON.parse(new TextDecoder().decode(t)):null,bsonFromBytes:t=>t,geometryFromBytes:t=>t,geographyFromBytes:t=>t,uuidFromBytes:t=>t};function Sr(t){let e=t instanceof ArrayBuffer?t:t.buffer.slice(t.byteOffset,t.byteOffset+t.byteLength);return {byteLength:e.byteLength,slice:(n,r)=>Promise.resolve(e.slice(n,r))}}function Mr(t,e){let n=t.name,r=t.type,o=t.logical_type,i=t.converted_type,a=t.num_children,s=t.repetition_type;if(a!=null&&a>0){let l=o?.type;throw l==="LIST"?new Error(`unsupported Parquet feature: LIST column '${n}' \u2014 nested/repeated types are not supported by databonk/parquet (ADR-011)`):l==="MAP"?new Error(`unsupported Parquet feature: MAP column '${n}' \u2014 nested types are not supported (ADR-011)`):new Error(`unsupported Parquet feature: nested/group column '${n}' \u2014 databonk/parquet only supports flat columns (ADR-011)`)}if(s==="REPEATED")throw new Error(`unsupported Parquet feature: REPEATED field '${n}' \u2014 only OPTIONAL/REQUIRED are supported (ADR-011)`);switch(r){case "DOUBLE":return {name:n,dtype:"f64"};case "FLOAT":return {name:n,dtype:"f32"};case "BOOLEAN":return {name:n,dtype:"bool"};case "INT32":{let l=o?.type;if(l==="DATE"||i==="DATE")return {name:n,dtype:"date32"};if(l==="DECIMAL"||i==="DECIMAL")throw new Error(`unsupported Parquet feature: DECIMAL column '${n}' (ADR-011 \u2014 only the databonk numeric dtypes are supported)`);if(l==="INTEGER"){let u=o?.isSigned;return {name:n,dtype:u===false?"u32":"i32"}}return i==="UINT_32"?{name:n,dtype:"u32"}:{name:n,dtype:"i32"}}case "INT64":{let l=o?.type;if(l==="DECIMAL"||i==="DECIMAL")throw new Error(`unsupported Parquet feature: DECIMAL column '${n}' (ADR-011)`);if(l==="TIMESTAMP"||i==="TIMESTAMP_MILLIS"){let u=o?.unit;if(u!=null&&u!=="MILLIS")throw new Error(`unsupported Parquet feature: TIMESTAMP with unit '${u}' in column '${n}' \u2014 only MILLIS is supported (ADR-011)`);if(i==="TIMESTAMP_MICROS")throw new Error(`unsupported Parquet feature: TIMESTAMP_MICROS column '${n}' \u2014 only MILLIS is supported (ADR-011)`);let c=e.get(n);return c!==void 0?{name:n,dtype:"timestamp",tz:c}:{name:n,dtype:"timestamp"}}return {name:n,dtype:"i64"}}case "INT96":throw new Error(`unsupported Parquet feature: INT96 timestamp column '${n}' \u2014 only TIMESTAMP(INT64, MILLIS) is supported (ADR-011)`);case "FIXED_LEN_BYTE_ARRAY":throw new Error(`unsupported Parquet feature: FIXED_LEN_BYTE_ARRAY column '${n}' (ADR-011)`);case "BYTE_ARRAY":{let l=o?.type;if(l==="STRING"||i==="UTF8")return {name:n,dtype:"utf8"};throw l==="DECIMAL"||i==="DECIMAL"?new Error(`unsupported Parquet feature: DECIMAL column '${n}' (ADR-011)`):new Error(`unsupported Parquet feature: BYTE_ARRAY column '${n}' without STRING/UTF8 annotation is not supported (ADR-011)`)}default:throw new Error(`unsupported Parquet feature: unknown physical type '${String(r??"GROUP")}' in column '${n}' (ADR-011)`)}}function _r(t){if(t.length===0)return [];if(t.length===1)return t[0];let e=t.reduce((o,i)=>o+i.length,0),n=new Array(e),r=0;for(let o of t){let i=o.length;for(let a=0;a<i;a++)n[r++]=o[a];}return n}async function _i(t,e){let n=Sr(t),r=await index_js.parquetMetadataAsync(n);for(let p of r.row_groups??[])for(let d of p.columns??[]){let h=d.meta_data?.codec;if(!Or.has(h))throw new Error(`unsupported Parquet compression codec: '${h}'. databonk/parquet supports only SNAPPY and UNCOMPRESSED (ADR-011). For GZIP/ZSTD/BROTLI/LZ4, re-compress the file first.`)}let o=new Map,i=r.key_value_metadata??[],a="databonk:tz:";for(let{key:p,value:d}of i)p.startsWith(a)&&d&&o.set(p.slice(a.length),d);let l=index_js.parquetSchema(r).children.map(p=>Mr(p.element,o)),u=new Map;for(let{name:p}of l)u.set(p,[]);await index_js.parquetRead({file:n,metadata:r,parsers:Rr,onChunk(p){let d=u.get(p.columnName);d&&d.push(p.columnData);}});let c=[];for(let{name:p,dtype:d,tz:h}of l){let y=u.get(p)??[],w=_r(y),g=D(e.ctx,d,w,h);c.push({name:p,col:g});}let m=Vt.fromColumns({},{runtime:e}),f=m.buildResult(c);return m.dispose(),f}function Br(t,e,n,r){let o=H(t,n),i,a;switch(n.dtype){case "f64":i={name:e,type:"DOUBLE",repetition_type:"OPTIONAL"};break;case "f32":i={name:e,type:"FLOAT",repetition_type:"OPTIONAL"};break;case "i32":i={name:e,type:"INT32",repetition_type:"OPTIONAL"};break;case "u32":i={name:e,type:"INT32",repetition_type:"OPTIONAL",converted_type:"UINT_32"};break;case "i64":i={name:e,type:"INT64",repetition_type:"OPTIONAL"};break;case "bool":i={name:e,type:"BOOLEAN",repetition_type:"OPTIONAL"};break;case "utf8":i={name:e,type:"BYTE_ARRAY",repetition_type:"OPTIONAL",logical_type:{type:"STRING"},converted_type:"UTF8"};break;case "date32":i={name:e,type:"INT32",repetition_type:"OPTIONAL",logical_type:{type:"DATE"}};break;case "timestamp":{i={name:e,type:"INT64",repetition_type:"OPTIONAL",logical_type:{type:"TIMESTAMP",isAdjustedToUTC:true,unit:{MILLIS:{}}}},n.tz&&(a={key:`databonk:tz:${e}`,value:n.tz});break}default:{let s=n.dtype;throw new Error(`unsupported databonk dtype for Parquet write: '${String(s)}'`)}}return a!==void 0?{el:i,data:o,tzKey:a}:{el:i,data:o}}function Bi(t,e={}){let{compression:n="uncompressed"}=e;if(n!=="snappy"&&n!=="uncompressed")throw new Error(`unsupported Parquet compression codec: '${n}'. databonk/parquet writeParquet only accepts 'snappy' or 'uncompressed' (ADR-011).`);let r=n==="snappy"?"SNAPPY":"UNCOMPRESSED",o=t.columns;t.length;let a=t.ctx,s=[{name:"root",num_children:o.length}],l=[],u=[];for(let f of o){let p=t.getColumn(f),{el:d,data:h,tzKey:y}=Br(a,f,p);s.push(d),l.push({name:f,data:h}),y&&u.push(y);}let c={schema:s,columnData:l,codec:r};u.length>0&&(c.kvMetadata=u);let m=index_js$1.parquetWriteBuffer(c);return new Uint8Array(m)}exports.readParquet=_i;exports.writeParquet=Bi;//# sourceMappingURL=parquet.cjs.map
|
|
3
|
+
//# sourceMappingURL=parquet.cjs.map
|