@ducklings/workers 1.4.4 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -4
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +45 -1
- package/dist/index.d.ts +45 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/wasm/duckdb-workers.js +1 -1
- package/dist/wasm/duckdb-workers.wasm +0 -0
- package/package.json +9 -9
package/dist/index.d.cts
CHANGED
|
@@ -5,7 +5,7 @@ export { Table, tableFromArrays, tableFromIPC, tableToIPC } from '@uwdata/fleche
|
|
|
5
5
|
* Ducklings Workers - Minimal DuckDB for Cloudflare Workers
|
|
6
6
|
*
|
|
7
7
|
* This package provides a lightweight DuckDB binding for WebAssembly,
|
|
8
|
-
* designed for Cloudflare Workers
|
|
8
|
+
* designed for Cloudflare Workers.
|
|
9
9
|
*
|
|
10
10
|
* IMPORTANT: In this build, query() and execute() are async and return Promises.
|
|
11
11
|
* Always use: `await conn.query(...)` or `await conn.execute(...)`
|
|
@@ -324,6 +324,21 @@ declare class StreamingResult implements Iterable<DataChunk> {
|
|
|
324
324
|
reset(): void;
|
|
325
325
|
[Symbol.iterator](): Iterator<DataChunk>;
|
|
326
326
|
toArray<T = Record<string, unknown>>(): T[];
|
|
327
|
+
/**
|
|
328
|
+
* Converts the streaming result into a Flechette Arrow Table.
|
|
329
|
+
*
|
|
330
|
+
* Reads all remaining chunks and aggregates them into a single Arrow table.
|
|
331
|
+
* DuckDB column types are mapped to the corresponding Flechette types.
|
|
332
|
+
*
|
|
333
|
+
* @returns A Flechette {@link Table} containing all result data
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* ```typescript
|
|
337
|
+
* const stream = conn.queryStreaming('SELECT * FROM large_table');
|
|
338
|
+
* const arrowTable = stream.toArrowTable();
|
|
339
|
+
* console.log(arrowTable.numRows);
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
327
342
|
toArrowTable(): Table;
|
|
328
343
|
private getFlechetteType;
|
|
329
344
|
close(): void;
|
|
@@ -420,6 +435,35 @@ declare class Connection {
|
|
|
420
435
|
commit(): Promise<void>;
|
|
421
436
|
rollback(): Promise<void>;
|
|
422
437
|
transaction<T>(fn: () => Promise<T>): Promise<T>;
|
|
438
|
+
/**
|
|
439
|
+
* Insert data from an Arrow IPC stream buffer into a table.
|
|
440
|
+
*
|
|
441
|
+
* Creates a new table with the given name from the Arrow IPC data. If the table
|
|
442
|
+
* already exists, the call is a no-op (uses `CREATE TABLE IF NOT EXISTS`).
|
|
443
|
+
*
|
|
444
|
+
* **Important:** The IPC stream must not contain dictionary-encoded columns.
|
|
445
|
+
* Flechette's `tableFromArrays()` defaults to `dictionary(utf8())` for string
|
|
446
|
+
* columns. Use explicit `utf8()` types to avoid this:
|
|
447
|
+
*
|
|
448
|
+
* @param tableName - The name of the table to create
|
|
449
|
+
* @param ipcBuffer - Arrow IPC stream bytes (use `tableToIPC(table, { format: 'stream' })`)
|
|
450
|
+
* @throws {@link DuckDBError} If the connection is closed or the IPC data is invalid
|
|
451
|
+
*
|
|
452
|
+
* @example
|
|
453
|
+
* ```typescript
|
|
454
|
+
* import { tableFromArrays, tableToIPC, utf8 } from '@ducklings/workers';
|
|
455
|
+
*
|
|
456
|
+
* const table = tableFromArrays(
|
|
457
|
+
* { id: [1, 2, 3], name: ['Alice', 'Bob', 'Charlie'] },
|
|
458
|
+
* { types: { name: utf8() } } // Required for string columns
|
|
459
|
+
* );
|
|
460
|
+
* const ipcBuffer = tableToIPC(table, { format: 'stream' });
|
|
461
|
+
* await conn.insertArrowFromIPCStream('users', ipcBuffer);
|
|
462
|
+
* ```
|
|
463
|
+
*
|
|
464
|
+
* @category Data Insertion
|
|
465
|
+
*/
|
|
466
|
+
insertArrowFromIPCStream(tableName: string, ipcBuffer: Uint8Array): Promise<void>;
|
|
423
467
|
close(): void;
|
|
424
468
|
}
|
|
425
469
|
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export { Table, tableFromArrays, tableFromIPC, tableToIPC } from '@uwdata/fleche
|
|
|
5
5
|
* Ducklings Workers - Minimal DuckDB for Cloudflare Workers
|
|
6
6
|
*
|
|
7
7
|
* This package provides a lightweight DuckDB binding for WebAssembly,
|
|
8
|
-
* designed for Cloudflare Workers
|
|
8
|
+
* designed for Cloudflare Workers.
|
|
9
9
|
*
|
|
10
10
|
* IMPORTANT: In this build, query() and execute() are async and return Promises.
|
|
11
11
|
* Always use: `await conn.query(...)` or `await conn.execute(...)`
|
|
@@ -324,6 +324,21 @@ declare class StreamingResult implements Iterable<DataChunk> {
|
|
|
324
324
|
reset(): void;
|
|
325
325
|
[Symbol.iterator](): Iterator<DataChunk>;
|
|
326
326
|
toArray<T = Record<string, unknown>>(): T[];
|
|
327
|
+
/**
|
|
328
|
+
* Converts the streaming result into a Flechette Arrow Table.
|
|
329
|
+
*
|
|
330
|
+
* Reads all remaining chunks and aggregates them into a single Arrow table.
|
|
331
|
+
* DuckDB column types are mapped to the corresponding Flechette types.
|
|
332
|
+
*
|
|
333
|
+
* @returns A Flechette {@link Table} containing all result data
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* ```typescript
|
|
337
|
+
* const stream = conn.queryStreaming('SELECT * FROM large_table');
|
|
338
|
+
* const arrowTable = stream.toArrowTable();
|
|
339
|
+
* console.log(arrowTable.numRows);
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
327
342
|
toArrowTable(): Table;
|
|
328
343
|
private getFlechetteType;
|
|
329
344
|
close(): void;
|
|
@@ -420,6 +435,35 @@ declare class Connection {
|
|
|
420
435
|
commit(): Promise<void>;
|
|
421
436
|
rollback(): Promise<void>;
|
|
422
437
|
transaction<T>(fn: () => Promise<T>): Promise<T>;
|
|
438
|
+
/**
|
|
439
|
+
* Insert data from an Arrow IPC stream buffer into a table.
|
|
440
|
+
*
|
|
441
|
+
* Creates a new table with the given name from the Arrow IPC data. If the table
|
|
442
|
+
* already exists, the call is a no-op (uses `CREATE TABLE IF NOT EXISTS`).
|
|
443
|
+
*
|
|
444
|
+
* **Important:** The IPC stream must not contain dictionary-encoded columns.
|
|
445
|
+
* Flechette's `tableFromArrays()` defaults to `dictionary(utf8())` for string
|
|
446
|
+
* columns. Use explicit `utf8()` types to avoid this:
|
|
447
|
+
*
|
|
448
|
+
* @param tableName - The name of the table to create
|
|
449
|
+
* @param ipcBuffer - Arrow IPC stream bytes (use `tableToIPC(table, { format: 'stream' })`)
|
|
450
|
+
* @throws {@link DuckDBError} If the connection is closed or the IPC data is invalid
|
|
451
|
+
*
|
|
452
|
+
* @example
|
|
453
|
+
* ```typescript
|
|
454
|
+
* import { tableFromArrays, tableToIPC, utf8 } from '@ducklings/workers';
|
|
455
|
+
*
|
|
456
|
+
* const table = tableFromArrays(
|
|
457
|
+
* { id: [1, 2, 3], name: ['Alice', 'Bob', 'Charlie'] },
|
|
458
|
+
* { types: { name: utf8() } } // Required for string columns
|
|
459
|
+
* );
|
|
460
|
+
* const ipcBuffer = tableToIPC(table, { format: 'stream' });
|
|
461
|
+
* await conn.insertArrowFromIPCStream('users', ipcBuffer);
|
|
462
|
+
* ```
|
|
463
|
+
*
|
|
464
|
+
* @category Data Insertion
|
|
465
|
+
*/
|
|
466
|
+
insertArrowFromIPCStream(tableName: string, ipcBuffer: Uint8Array): Promise<void>;
|
|
423
467
|
close(): void;
|
|
424
468
|
}
|
|
425
469
|
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// Ducklings Workers - Minimal DuckDB for Cloudflare Workers
|
|
2
|
-
var ht=Uint8Array.of(65,82,82,79,87,49),mn=Uint8Array.of(255,255,255,255,0,0,0,0),U={V1:0,V4:3,V5:4},Or={Little:0},P={NONE:0,Schema:1,DictionaryBatch:2,RecordBatch:3,Tensor:4,SparseTensor:5},l={Dictionary:-1,NONE:0,Null:1,Int:2,Float:3,Binary:4,Utf8:5,Bool:6,Decimal:7,Date:8,Time:9,Timestamp:10,Interval:11,List:12,Struct:13,Union:14,FixedSizeBinary:15,FixedSizeList:16,Map:17,Duration:18,LargeBinary:19,LargeUtf8:20,LargeList:21,RunEndEncoded:22,BinaryView:23,Utf8View:24,ListView:25,LargeListView:26},tt={HALF:0,SINGLE:1,DOUBLE:2},H={DAY:0,MILLISECOND:1},w={SECOND:0,MILLISECOND:1,MICROSECOND:2,NANOSECOND:3},N={YEAR_MONTH:0,DAY_TIME:1,MONTH_DAY_NANO:2},j={Sparse:0,Dense:1},pt={LZ4_FRAME:0,ZSTD:1},st={BUFFER:0};var q=Uint8Array,jt=Uint16Array,zt=Uint32Array,Et=BigUint64Array,bt=Int8Array,Br=Int16Array,M=Int32Array,v=BigInt64Array,ue=Float32Array,et=Float64Array;function hn(r){return r instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&r instanceof SharedArrayBuffer}function pn(r,t){let e=Math.log2(r)-3;return (t?[bt,Br,M,v]:[q,jt,zt,Et])[e]}var ds=Object.getPrototypeOf(Int8Array);function Ur(r){return r instanceof ds}function le(r){return Array.isArray(r)||Ur(r)}function _t(r){return r===v||r===Et}function kt(r,t){let e=0,n=r.length;if(n<=2147483648)do{let o=e+n>>>1;r[o]<=t?e=o+1:n=o;}while(e<n);else do{let o=Math.trunc((e+n)/2);r[o]<=t?e=o+1:n=o;}while(e<n);return e}function ms(r,t=1){return (r*t+7&-8)/t}function bn(r,t=r.length){let e=ms(t,r.BYTES_PER_ELEMENT);return r.length>e?r.subarray(0,e):r.length<e?yn(r,e):r}function yn(r,t,e=0){let n=new r.constructor(t);return n.set(r,e),n}function fe(r,t,e){for(;r.length<=t;)r=yn(r,r.length<<1,e?r.length:0);return r}function gn(r){return r instanceof Date}function In(r){return typeof r[Symbol.iterator]=="function"}function de(r,t,e){if(t(r))return r;throw new Error(e(r))}function F(r,t,e){return t=Array.isArray(t)?t:Object.values(t),de(r,n=>t.includes(n),e??(()=>`${r} must be one of ${t}`))}function St(r,t){for(let[e,n]of Object.entries(r))if(n===t)return e;return "<Unknown>"}var C=r=>`Unsupported data type: "${St(l,r)}" (id ${r})`,yt=(r,t,e=true,n=null)=>({name:r,type:t,nullable:e,metadata:n});function wn(r){return Object.hasOwn(r,"name")&&xn(r.type)}function xn(r){return typeof r?.typeId=="number"}function ot(r,t="",e=true){return wn(r)?r:yt(t,de(r,xn,()=>"Data type expected."),e)}var An=r=>({typeId:r}),Gt=(r,t,e=false,n=-1)=>({typeId:l.Dictionary,id:n,dictionary:r,indices:t||G(),ordered:e}),Pr=()=>An(l.Null),z=(r=32,t=true)=>({typeId:l.Int,bitWidth:F(r,[8,16,32,64]),signed:t,values:pn(r,t)}),it=()=>z(8),ct=()=>z(16),G=()=>z(32),at=()=>z(64),Dt=()=>z(8,false),Ot=()=>z(16,false),Bt=()=>z(32,false),Ut=()=>z(64,false),$t=(r=2)=>({typeId:l.Float,precision:F(r,tt),values:[jt,ue,et][r]});var Pt=()=>$t(tt.SINGLE),rt=()=>$t(tt.DOUBLE),Mr=()=>({typeId:l.Binary,offsets:M}),$=()=>({typeId:l.Utf8,offsets:M}),Mt=()=>An(l.Bool),Nr=(r,t,e=128)=>({typeId:l.Decimal,precision:r,scale:t,bitWidth:F(e,[32,64,128,256]),values:e===32?M:Et});var me=r=>({typeId:l.Date,unit:F(r,H),values:r===H.DAY?M:v}),Nt=()=>me(H.DAY);var vr=(r=w.MILLISECOND)=>{r=F(r,w);let t=r===w.SECOND||r===w.MILLISECOND?32:64;return {typeId:l.Time,unit:r,bitWidth:t,values:t===32?M:v}};var ut=(r=w.MILLISECOND,t=null)=>({typeId:l.Timestamp,unit:F(r,w),timezone:t,values:v}),Fr=(r=N.MONTH_DAY_NANO)=>({typeId:l.Interval,unit:F(r,N),values:r===N.MONTH_DAY_NANO?void 0:M}),Yt=r=>({typeId:l.List,children:[ot(r)],offsets:M}),Wt=r=>({typeId:l.Struct,children:Array.isArray(r)&&r.length>0&&wn(r[0])?r:Object.entries(r).map(([t,e])=>yt(t,e))}),Lr=(r,t,e,n)=>(e??=t.map((o,s)=>s),{typeId:l.Union,mode:F(r,j),typeIds:e,typeMap:e.reduce((o,s,c)=>(o[s]=c,o),{}),children:t.map((o,s)=>ot(o,`_${s}`)),typeIdForValue:n,offsets:M}),Cr=r=>({typeId:l.FixedSizeBinary,stride:r}),Zt=(r,t)=>({typeId:l.FixedSizeList,stride:t,children:[ot(r)]}),Tn=(r,t)=>({typeId:l.Map,keysSorted:r,children:[t],offsets:M});var Vr=(r=w.MILLISECOND)=>({typeId:l.Duration,unit:F(r,w),values:v}),Rr=()=>({typeId:l.LargeBinary,offsets:v}),Hr=()=>({typeId:l.LargeUtf8,offsets:v}),jr=r=>({typeId:l.LargeList,children:[ot(r)],offsets:v}),zr=(r,t)=>({typeId:l.RunEndEncoded,children:[de(ot(r,"run_ends"),e=>e.type.typeId===l.Int,()=>"Run-ends must have an integer type."),ot(t,"values")]});var Gr=r=>({typeId:l.ListView,children:[ot(r,"value")],offsets:M}),$r=r=>({typeId:l.LargeListView,children:[ot(r,"value")],offsets:v});var _n=new et(2),he=_n.buffer,hs=new v(he),vt=new zt(he),En=new M(he),ps=new q(he);function bs(r){return r}function Y(r){return BigInt(r)}function pe(r){return _t(r)?Y:bs}function kn(r){return r/864e5|0}function Sn(r){return r===w.SECOND?t=>Y(t/1e3):r===w.MILLISECOND?Y:r===w.MICROSECOND?t=>Y(t*1e3):t=>Y(t*1e6)}function Dn([r,t,e]){return En[0]=r,En[1]=t,hs[1]=Y(e),ps}function X(r){if(r>Number.MAX_SAFE_INTEGER||r<Number.MIN_SAFE_INTEGER)throw Error(`BigInt exceeds integer number representation: ${r}`);return Number(r)}function be(r,t){return Number(r/t)+Number(r%t)/Number(t)}function On(r){return t=>typeof t=="bigint"?Number(t):Math.trunc(t*r)}function Bn(r,t,e,n,o){let s=typeof r=="bigint"?r:Y(Math.trunc(r*o));t[e]=s,n>1&&(t[e+1]=s>>64n,n>2&&(t[e+2]=s>>128n,t[e+3]=s>>192n));}var Ft=r=>BigInt.asUintN(64,r);function Un(r,t){return BigInt.asIntN(64,r[t])}function Pn(r,t){let e=t<<1,n;return BigInt.asIntN(64,r[e+1])<0?(n=Ft(~r[e])|Ft(~r[e+1])<<64n,n=-(n+1n)):n=r[e]|r[e+1]<<64n,n}function Mn(r,t){let e=t<<2,n;return BigInt.asIntN(64,r[e+3])<0?(n=Ft(~r[e])|Ft(~r[e+1])<<64n|Ft(~r[e+2])<<128n|Ft(~r[e+3])<<192n,n=-(n+1n)):n=r[e]|r[e+1]<<64n|r[e+2]<<128n|r[e+3]<<192n,n}function Nn(r){if(r!==r)return 32256;_n[0]=r;let t=(vt[1]&2147483648)>>16&65535,e=vt[1]&2146435072,n=0;return e>=1089470464?vt[0]>0?e=31744:(e=(e&2080374784)>>16,n=(vt[1]&1048575)>>10):e<=1056964608?(n=1048576+(vt[1]&1048575),n=1048576+(n<<(e>>20)-998)>>21,e=0):(e=e-1056964608>>10,n=(vt[1]&1048575)+512>>10),t|e|n&65535}var ys=new TextDecoder("utf-8"),gs=new TextEncoder;function Lt(r){return ys.decode(r)}function ye(r){return gs.encode(r)}function Ct(r){return `${typeof r!="object"||!r?r??null:gn(r)?+r:le(r)?`[${r.map(Ct)}]`:Is(r)}`}function Is(r){let t="",e=-1;for(let n in r)++e>0&&(t+=","),t+=`"${n}":${Ct(r[n])}`;return `{${t}}`}var V=4,ge=2;function Yr(r,t){return (r[t>>3]&1<<t%8)!==0}function B(r,t){let e=t+x(r,t),n=e-x(r,e),o=S(r,n);return (s,c,a=null)=>{if(s<o){let i=S(r,n+s);if(i)return c(r,e+i)}return a}}function Q(r,t){return t}function lt(r,t){return !!Ie(r,t)}function Ie(r,t){return qt(r,t)<<24>>24}function qt(r,t){return r[t]}function S(r,t){return ws(r,t)<<16>>16}function ws(r,t){return r[t]|r[t+1]<<8}function x(r,t){return r[t]|r[t+1]<<8|r[t+2]<<16|r[t+3]<<24}function vn(r,t){return x(r,t)>>>0}function D(r,t){return X(BigInt.asIntN(64,BigInt(vn(r,t))+(BigInt(vn(r,t+V))<<32n)))}function gt(r,t){let e=t+x(r,t),n=x(r,e);return e+=V,Lt(r.subarray(e,e+n))}function L(r,t,e,n){if(!t)return [];let o=t+x(r,t);return Array.from({length:x(r,o)},(s,c)=>n(r,o+V+c*e))}var we=Symbol("rowIndex");function Xt(r,t){class e{constructor(s){this[we]=s;}toJSON(){return Fn(r,t,this[we])}}let n=e.prototype;for(let o=0;o<r.length;++o){if(Object.hasOwn(n,r[o]))continue;let s=t[o];Object.defineProperty(n,r[o],{get(){return s.at(this[we])},enumerable:true});}return o=>new e(o)}function xe(r,t){return e=>Fn(r,t,e)}function Fn(r,t,e){let n={};for(let o=0;o<r.length;++o)n[r[o]]=t[o].at(e);return n}function Ln(r){return r instanceof K}var It=class{static ArrayType=null;constructor({length:t,nullCount:e,type:n,validity:o,values:s,offsets:c,sizes:a,children:i}){this.length=t,this.nullCount=e,this.type=n,this.validity=o,this.values=s,this.offsets=c,this.sizes=a,this.children=i,(!e||!this.validity)&&(this.at=u=>this.value(u));}get[Symbol.toStringTag](){return "Batch"}at(t){return this.isValid(t)?this.value(t):null}isValid(t){return Yr(this.validity,t)}value(t){return this.values[t]}slice(t,e){let n=e-t,o=Array(n);for(let s=0;s<n;++s)o[s]=this.at(t+s);return o}*[Symbol.iterator](){for(let t=0;t<this.length;++t)yield this.at(t);}},K=class extends It{constructor(t){super(t);let{length:e,values:n}=this;this.values=n.subarray(0,e);}slice(t,e){return this.nullCount?super.slice(t,e):this.values.subarray(t,e)}[Symbol.iterator](){return this.nullCount?super[Symbol.iterator]():this.values[Symbol.iterator]()}},Vt=class extends It{static ArrayType=et},T=class extends It{static ArrayType=Array},Rt=class extends T{value(t){return null}},W=class extends Vt{value(t){return X(this.values[t])}},Ae=class extends Vt{value(t){let e=this.values[t],n=(e&31744)>>10,o=(e&1023)/1024,s=(-1)**((e&32768)>>15);switch(n){case 31:return s*(o?Number.NaN:1/0);case 0:return s*(o?6103515625e-14*o:0)}return s*2**(n-15)*(1+o)}},Te=class extends T{value(t){return Yr(this.values,t)}},Ee=class extends Vt{constructor(t){super(t);let{scale:e}=this.type;this.scale=10**e;}value(t){return this.values[t]/this.scale}},_e=class extends It{constructor(t){super(t);let{bitWidth:e,scale:n}=this.type;this.decimal=e===64?Un:e===128?Pn:Mn,this.scale=10n**BigInt(n);}},ke=class extends _e{static ArrayType=et;value(t){return be(this.decimal(this.values,t),this.scale)}},Se=class extends _e{static ArrayType=Array;value(t){return this.decimal(this.values,t)}},Qt=class extends T{constructor(t){super(t),this.source=t;}value(t){return new Date(this.source.value(t))}},De=class extends Vt{value(t){return 864e5*this.values[t]}},Cn=W,Oe=class extends W{value(t){return super.value(t)*1e3}},Vn=W,Be=class extends W{value(t){return be(this.values[t],1000n)}},Ue=class extends W{value(t){return be(this.values[t],1000000n)}},Pe=class extends T{value(t){return this.values.subarray(t<<1,t+1<<1)}},Me=class extends T{value(t){let e=this.values,n=t<<4;return Float64Array.of(x(e,n),x(e,n+4),D(e,n+8))}},Rn=({values:r,offsets:t},e)=>r.subarray(t[e],t[e+1]),Hn=({values:r,offsets:t},e)=>r.subarray(X(t[e]),X(t[e+1])),Ne=class extends T{value(t){return Rn(this,t)}},ve=class extends T{value(t){return Hn(this,t)}},Fe=class extends T{value(t){return Lt(Rn(this,t))}},Le=class extends T{value(t){return Lt(Hn(this,t))}},Ce=class extends T{value(t){let e=this.offsets;return this.children[0].slice(e[t],e[t+1])}},Ve=class extends T{value(t){let e=this.offsets;return this.children[0].slice(X(e[t]),X(e[t+1]))}},Re=class extends T{value(t){let e=this.offsets[t],n=e+this.sizes[t];return this.children[0].slice(e,n)}},He=class extends T{value(t){let e=this.offsets[t],n=e+this.sizes[t];return this.children[0].slice(X(e),X(n))}},je=class extends T{constructor(t){super(t),this.stride=this.type.stride;}},ze=class extends je{value(t){let{stride:e,values:n}=this;return n.subarray(t*e,(t+1)*e)}},Ge=class extends je{value(t){let{children:e,stride:n}=this;return e[0].slice(t*n,(t+1)*n)}};function jn({children:r,offsets:t},e){let[n,o]=r[0].children,s=t[e],c=t[e+1],a=[];for(let i=s;i<c;++i)a.push([n.at(i),o.at(i)]);return a}var $e=class extends T{value(t){return jn(this,t)}},Ye=class extends T{value(t){return new Map(jn(this,t))}},Kt=class extends T{constructor({typeIds:t,...e}){super(e),this.typeIds=t,this.typeMap=this.type.typeMap;}value(t,e=t){let{typeIds:n,children:o,typeMap:s}=this;return o[s[n[t]]].at(e)}},We=class extends Kt{value(t){return super.value(t,this.offsets[t])}},Jt=class extends T{constructor(t,e=xe){super(t),this.names=this.type.children.map(n=>n.name),this.factory=e(this.names,this.children);}value(t){return this.factory(t)}},Ze=class extends Jt{constructor(t){super(t,Xt);}},qe=class extends T{value(t){let[{values:e},n]=this.children;return n.at(kt(e,t))}},Xe=class extends T{setDictionary(t){return this.dictionary=t,this.cache=t.cache(),this}value(t){return this.cache[this.key(t)]}key(t){return this.values[t]}},Qe=class extends T{constructor({data:t,...e}){super(e),this.data=t;}view(t){let{values:e,data:n}=this,o=t<<4,s=o+4,c=e,a=x(c,o);return a>12&&(s=x(c,o+12),c=n[x(c,o+8)]),c.subarray(s,s+a)}},Ke=class extends Qe{value(t){return this.view(t)}},Je=class extends Qe{value(t){return Lt(this.view(t))}};function Wr(r){let t=[];return {add(e){return t.push(e),this},clear:()=>t=[],done:()=>new J(t,r)}}var J=class{constructor(t,e=t[0]?.type){this.type=e,this.length=t.reduce((s,c)=>s+c.length,0),this.nullCount=t.reduce((s,c)=>s+c.nullCount,0),this.data=t;let n=t.length,o=new Int32Array(n+1);if(n===1){let[s]=t;o[1]=s.length,this.at=c=>s.at(c);}else for(let s=0,c=0;s<n;++s)o[s+1]=c+=t[s].length;this.offsets=o;}get[Symbol.toStringTag](){return "Column"}[Symbol.iterator](){let t=this.data;return t.length===1?t[0][Symbol.iterator]():xs(t)}at(t){let{data:e,offsets:n}=this,o=kt(n,t)-1;return e[o]?.at(t-n[o])}get(t){return this.at(t)}toArray(){let{length:t,nullCount:e,data:n}=this,o=!e&&Ln(n[0]),s=n.length;if(o&&s===1)return n[0].values;let c=!s||e>0?Array:n[0].constructor.ArrayType??n[0].values.constructor,a=new c(t);return o?As(a,n):Ts(a,n)}cache(){return this._cache??(this._cache=this.toArray())}};function*xs(r){for(let t=0;t<r.length;++t){let e=r[t][Symbol.iterator]();for(let n=e.next();!n.done;n=e.next())yield n.value;}}function As(r,t){for(let e=0,n=0;e<t.length;++e){let{values:o}=t[e];r.set(o,n),n+=o.length;}return r}function Ts(r,t){let e=-1;for(let n=0;n<t.length;++n){let o=t[n];for(let s=0;s<o.length;++s)r[++e]=o.at(s);}return r}var wt=class r{constructor(t,e,n=false){let o=t.fields.map(c=>c.name);this.schema=t,this.names=o,this.children=e,this.factory=n?Xt:xe;let s=[];this.getFactory=c=>s[c]??(s[c]=this.factory(o,e.map(a=>a.data[c])));}get[Symbol.toStringTag](){return "Table"}get numCols(){return this.names.length}get numRows(){return this.children[0]?.length??0}getChildAt(t){return this.children[t]}getChild(t){let e=this.names.findIndex(n=>n===t);return e>-1?this.children[e]:void 0}selectAt(t,e=[]){let{children:n,factory:o,schema:s}=this,{fields:c}=s;return new r({...s,fields:t.map((a,i)=>Es(c[a],e[i]))},t.map(a=>n[a]),o===Xt)}select(t,e){let n=this.names,o=t.map(s=>n.indexOf(s));return this.selectAt(o,e)}toColumns(){let{children:t,names:e}=this,n={};return e.forEach((o,s)=>n[o]=t[s]?.toArray()??[]),n}toArray(){let{children:t,getFactory:e,numRows:n}=this,o=t[0]?.data??[],s=Array(n);for(let c=0,a=-1;c<o.length;++c){let i=e(c);for(let u=0;u<o[c].length;++u)s[++a]=i(u);}return s}*[Symbol.iterator](){let{children:t,getFactory:e}=this,n=t[0]?.data??[];for(let o=0;o<n.length;++o){let s=e(o);for(let c=0;c<n[o].length;++c)yield s(c);}}at(t){let{children:e,getFactory:n,numRows:o}=this;if(t<0||t>=o)return null;let[{offsets:s}]=e,c=kt(s,t)-1;return n(c)(t-s[c])}get(t){return this.at(t)}};function Es(r,t){return t!=null&&t!==r.name?{...r,name:t}:r}function xt(r,t={}){let{typeId:e,bitWidth:n,mode:o,precision:s,unit:c}=r,{useBigInt:a,useDate:i,useDecimalInt:u,useMap:f,useProxy:m}=t;switch(e){case l.Null:return Rt;case l.Bool:return Te;case l.Int:case l.Time:case l.Duration:return a||n<64?K:W;case l.Float:return s?K:Ae;case l.Date:return zn(c===H.DAY?De:Cn,i&&Qt);case l.Timestamp:return zn(c===w.SECOND?Oe:c===w.MILLISECOND?Vn:c===w.MICROSECOND?Be:Ue,i&&Qt);case l.Decimal:return n===32?u?K:Ee:u?Se:ke;case l.Interval:return c===N.DAY_TIME?Pe:c===N.YEAR_MONTH?K:Me;case l.FixedSizeBinary:return ze;case l.Utf8:return Fe;case l.LargeUtf8:return Le;case l.Binary:return Ne;case l.LargeBinary:return ve;case l.BinaryView:return Ke;case l.Utf8View:return Je;case l.List:return Ce;case l.LargeList:return Ve;case l.Map:return f?Ye:$e;case l.ListView:return Re;case l.LargeListView:return He;case l.FixedSizeList:return Ge;case l.Struct:return m?Ze:Jt;case l.RunEndEncoded:return qe;case l.Dictionary:return Xe;case l.Union:return o?We:Kt}throw new Error(C(e))}function zn(r,t){return t?class extends t{constructor(n){super(new r(n));}}:r}function te(r,t,e){r[t]=e,r[t+1]=e>>8,r[t+2]=e>>16,r[t+3]=e>>24;}function Zr(r,t,e){let n=BigInt(e);te(r,t+4,Number(BigInt.asIntN(32,n>>BigInt(32)))),te(r,t+0,Number(BigInt.asIntN(32,n)));}var tr=1024,er=class{constructor(t){this.sink=t,this.minalign=1,this.buf=new Uint8Array(tr),this.space=tr,this.vtables=[],this.outputBytes=0;}offset(){return this.buf.length-this.space}writeInt8(t){this.buf[this.space-=1]=t;}writeInt16(t){this.buf[this.space-=2]=t,this.buf[this.space+1]=t>>8;}writeInt32(t){te(this.buf,this.space-=4,t);}writeInt64(t){Zr(this.buf,this.space-=8,t);}addInt8(t){nt(this,1,0),this.writeInt8(t);}addInt16(t){nt(this,2,0),this.writeInt16(t);}addInt32(t){nt(this,4,0),this.writeInt32(t);}addInt64(t){nt(this,8,0),this.writeInt64(t);}addOffset(t){nt(this,V,0),this.writeInt32(this.offset()-t+V);}addObject(t,e){let n=_s(this,t);return e?.(n),n.finish()}addVector(t,e,n,o){let s=t?.length;if(!s)return 0;nt(this,V,e*s),nt(this,n,e*s);for(let c=s;--c>=0;)o(this,t[c]);return this.writeInt32(s),this.offset()}addOffsetVector(t){return this.addVector(t,4,4,(e,n)=>e.addOffset(n))}addString(t){if(t==null)return 0;let e=ye(t),n=e.length;return this.addInt8(0),nt(this,V,n),this.buf.set(e,this.space-=n),this.writeInt32(n),this.offset()}finish(t){nt(this,this.minalign,V),this.addOffset(t);}flush(){let{buf:t,sink:e}=this,n=t.subarray(this.space,t.length);e.write(n),this.outputBytes+=n.byteLength,this.minalign=1,this.vtables=[],this.buf=new Uint8Array(tr),this.space=tr;}addBuffer(t){let e=t.byteLength;if(!e)return 0;this.sink.write(t),this.outputBytes+=e;let n=(e+7&-8)-e;return this.addPadding(n),e+n}addPadding(t){t>0&&(this.sink.write(new Uint8Array(t)),this.outputBytes+=t);}};function nt(r,t,e){let{buf:n,space:o,minalign:s}=r;t>s&&(r.minalign=t);let c=n.length,a=c-o+e,i=~a+1&t-1;n=fe(n,a+i+t-1,true),o+=n.length-c;for(let u=0;u<i;++u)n[--o]=0;r.buf=n,r.space=o;}function _s(r,t){let e=Array(t).fill(0),n=r.offset();function o(s){e[s]=r.offset();}return {addInt8(s,c,a){c!=a&&(r.addInt8(c),o(s));},addInt16(s,c,a){c!=a&&(r.addInt16(c),o(s));},addInt32(s,c,a){c!=a&&(r.addInt32(c),o(s));},addInt64(s,c,a){c!=a&&(r.addInt64(c),o(s));},addOffset(s,c,a){c!=a&&(r.addOffset(c),o(s));},finish(){r.addInt32(0);let s=r.offset(),c=t;for(;--c>=0&&e[c]===0;);let a=c+1;for(;c>=0;--c)r.addInt16(e[c]?s-e[c]:0);let i=2;r.addInt16(s-n);let u=(a+i)*ge;r.addInt16(u);let f=0,{buf:m,vtables:b,space:p}=r;t:for(c=0;c<b.length;++c){let g=m.length-b[c];if(u==S(m,g)){for(let A=ge;A<u;A+=ge)if(S(m,p+A)!=S(m,g+A))continue t;f=b[c];break}}if(f)r.space=m.length-s,te(m,r.space,f-s);else {let g=r.offset();b.push(g),te(m,m.length-s,g-s);}return s}}}var Gn=-1,qr=8;function rr(r){return `Missing compression codec "${St(pt,r)}" (id ${r})`}var ks=new Map;function ee(r){return r!=null&&ks.get(r)||null}function $n(r,{offset:t,length:e},n){if(e===0)return {bytes:new Uint8Array(0),offset:0,length:0};let o=D(r,t),s=r.subarray(t+qr,t+e),c=o===Gn?s:n.decode(s);return {bytes:c,offset:0,length:c.length}}function Yn(r,t){let e=t.encode(r),n=e.length<r.length,o=n?e:r,s=new Uint8Array(qr+o.length);return Zr(s,0,n?r.length:Gn),s.set(o,qr),s}function Ss(r,t){return {offset:D(r,t),metadataLength:x(r,t+8),bodyLength:D(r,t+16)}}function Xr(r,t){return L(r,t,24,Ss)}function Wn(r,t){let e=B(r,t);return {codec:e(4,Ie,pt.LZ4_FRAME),method:e(6,Ie,st.BUFFER)}}function nr(r,t,e){let n=B(r,t),o=e<U.V4?8:0;return {length:n(4,D,0),nodes:L(r,n(6,Q),16,(s,c)=>({length:D(s,c),nullCount:D(s,c+8)})),regions:L(r,n(8,Q),16+o,(s,c)=>({offset:D(s,c+o),length:D(s,c+o+8)})),compression:n(10,Wn),variadic:L(r,n(12,Q),8,D)}}function Zn(r,t,e){let n=B(r,t);return {id:n(4,D,0),data:n(6,(o,s)=>nr(o,s,e)),isDelta:n(8,lt,false)}}function Qr(r,t,e,n){F(e,l,C);let o=B(r,t);switch(e){case l.Binary:return Mr();case l.Utf8:return $();case l.LargeBinary:return Rr();case l.LargeUtf8:return Hr();case l.List:return Yt(n[0]);case l.ListView:return Gr(n[0]);case l.LargeList:return jr(n[0]);case l.LargeListView:return $r(n[0]);case l.Struct:return Wt(n);case l.RunEndEncoded:return zr(n[0],n[1]);case l.Int:return z(o(4,x,0),o(6,lt,false));case l.Float:return $t(o(4,S,tt.HALF));case l.Decimal:return Nr(o(4,x,0),o(6,x,0),o(8,x,128));case l.Date:return me(o(4,S,H.MILLISECOND));case l.Time:return vr(o(4,S,w.MILLISECOND));case l.Timestamp:return ut(o(4,S,w.SECOND),o(6,gt));case l.Interval:return Fr(o(4,S,N.YEAR_MONTH));case l.Duration:return Vr(o(4,S,w.MILLISECOND));case l.FixedSizeBinary:return Cr(o(4,x,0));case l.FixedSizeList:return Zt(n[0],o(4,x,0));case l.Map:return Tn(o(4,lt,false),n[0]);case l.Union:return Lr(o(4,S,j.Sparse),n,L(r,o(6,Q),4,x))}return {typeId:e}}function re(r,t){let e=L(r,t,4,(n,o)=>{let s=B(n,o);return [s(4,gt),s(6,gt)]});return e.length?new Map(e):null}function sr(r,t,e){let n=B(r,t);return {version:e,endianness:n(4,S,0),fields:n(6,Ds,[]),metadata:n(8,re)}}function Ds(r,t){return L(r,t,4,qn)}function qn(r,t){let e=B(r,t),n=e(8,qt,l.NONE),o=e(10,Q,0),s=e(12,Bs),c=e(14,Os,[]),a=Qr(r,o,n,c);return s&&(s.dictionary=a,a=s),{name:e(4,gt),type:a,nullable:e(6,lt,false),metadata:e(16,re)}}function Os(r,t){return L(r,t,4,qn)}function Bs(r,t){if(!t)return null;let e=B(r,t);return Gt(null,e(6,Us,G()),e(8,lt,false),e(4,D,0))}function Us(r,t){return Qr(r,t,l.Int)}var Ps=(r,t)=>`Expected to read ${r} metadata bytes, but only read ${t}.`,Ms=(r,t)=>`Expected to read ${r} bytes for message body, but only read ${t}.`,Ns=r=>`Unsupported message type: ${r} (${St(P,r)})`;function or(r,t){let e=x(r,t)||0;if(t+=V,e===-1&&(e=x(r,t)||0,t+=V),e===0)return null;let n=r.subarray(t,t+=e);if(n.byteLength<e)throw new Error(Ps(e,n.byteLength));let o=B(n,0),s=o(4,S,U.V1),c=o(6,qt,P.NONE),a=o(8,Q,0),i=o(10,D,0),u;if(a){let f=c===P.Schema?sr:c===P.DictionaryBatch?Zn:c===P.RecordBatch?nr:null;if(!f)throw new Error(Ns(c));if(u=f(n,a,s),i>0){let m=r.subarray(t,t+=i);if(m.byteLength<i)throw new Error(Ms(i,m.byteLength));u.body=m;}else c!==P.Schema&&(u.body=new Uint8Array(0));}return {version:s,type:c,index:t,content:u}}function Xn(r){let t=hn(r)?new Uint8Array(r):r;return t instanceof Uint8Array&&vs(t)?Ls(t):Fs(t)}function vs(r){if(!r||r.length<4)return false;for(let t=0;t<6;++t)if(ht[t]!==r[t])return false;return true}function Fs(r){let t=[r].flat(),e,n=[],o=[];for(let s of t){if(!(s instanceof Uint8Array))throw new Error("IPC data batch was not a Uint8Array.");let c=0;for(;;){let a=or(s,c);if(a===null)break;if(c=a.index,!!a.content)switch(a.type){case P.Schema:e||(e=a.content);break;case P.RecordBatch:n.push(a.content);break;case P.DictionaryBatch:o.push(a.content);break}}}return {schema:e,dictionaries:o,records:n,metadata:null}}function Ls(r){let t=r.byteLength-(ht.length+4),e=x(r,t),n=B(r,t-e),o=n(4,S,U.V1),s=n(8,Xr,[]),c=n(10,Xr,[]);return {schema:n(6,(a,i)=>sr(a,i,o)),dictionaries:s.map(({offset:a})=>or(r,a).content),records:c.map(({offset:a})=>or(r,a).content),metadata:n(12,re)}}function Qn(r,t){return Cs(Xn(r),t)}function Cs(r,t={}){let{schema:e={fields:[]},dictionaries:n,records:o}=r,{version:s,fields:c}=e,a=new Map,i=Rs(t,s,a),u=new Map;Vs(e,b=>{let p=b.type;p.typeId===l.Dictionary&&u.set(p.id,p.dictionary);});let f=new Map;for(let b of n){let{id:p,data:g,isDelta:A,body:k}=b,Z=u.get(p),Tt=Kr(Z,i({...g,body:k}));if(f.has(p)){let I=f.get(p);A||I.clear(),I.add(Tt);}else {if(A)throw new Error("Delta update can not be first dictionary batch.");f.set(p,Wr(Z).add(Tt));}}f.forEach((b,p)=>a.set(p,b.done()));let m=c.map(b=>Wr(b.type));for(let b of o){let p=i(b);c.forEach((g,A)=>m[A].add(Kr(g.type,p)));}return new wt(e,m.map(b=>b.done()),t.useProxy)}function Vs(r,t){r.fields.forEach(function e(n){t(n),n.type.dictionary?.children?.forEach(e),n.type.children?.forEach(e);});}function Rs(r,t,e){let n={version:t,options:r,dictionary:o=>e.get(o)};return o=>{let{length:s,nodes:c,regions:a,compression:i,variadic:u,body:f}=o,m=-1,b=-1,p=-1;return {...n,length:s,node:()=>c[++m],buffer:g=>{let{bytes:A,length:k,offset:Z}=Hs(f,a[++b],i);return g?new g(A.buffer,A.byteOffset+Z,k/g.BYTES_PER_ELEMENT):A.subarray(Z,Z+k)},variadic:()=>u[++p],visit(g){return g.map(A=>Kr(A.type,this))}}}}function Hs(r,t,e){if(e){if(e.method!==st.BUFFER)throw new Error(`Unknown compression method (${e.method})`);{let n=e.codec,o=ee(n);if(!o)throw new Error(rr(n));return $n(r,t,o)}}else return {bytes:r,...t}}function Kr(r,t){let{typeId:e}=r,{options:n,node:o,buffer:s,variadic:c,version:a}=t,i=xt(r,n),u={...o(),type:r};if(e===l.Null)return new i({...u,nullCount:u.length});switch(e){case l.Bool:case l.Int:case l.Time:case l.Duration:case l.Float:case l.Decimal:case l.Date:case l.Timestamp:case l.Interval:case l.FixedSizeBinary:return new i({...u,validity:s(),values:s(r.values)});case l.Utf8:case l.LargeUtf8:case l.Binary:case l.LargeBinary:return new i({...u,validity:s(),offsets:s(r.offsets),values:s()});case l.BinaryView:case l.Utf8View:return new i({...u,validity:s(),values:s(),data:Array.from({length:c()},()=>s())});case l.List:case l.LargeList:case l.Map:return new i({...u,validity:s(),offsets:s(r.offsets),children:t.visit(r.children)});case l.ListView:case l.LargeListView:return new i({...u,validity:s(),offsets:s(r.offsets),sizes:s(r.offsets),children:t.visit(r.children)});case l.FixedSizeList:case l.Struct:return new i({...u,validity:s(),children:t.visit(r.children)});case l.RunEndEncoded:return new i({...u,children:t.visit(r.children)});case l.Dictionary:{let{id:f,indices:m}=r;return new i({...u,validity:s(),values:s(m.values)}).setDictionary(t.dictionary(f))}case l.Union:return a<U.V5&&s(),new i({...u,typeIds:s(bt),offsets:r.mode===j.Sparse?null:s(r.offsets),children:t.visit(r.children)});default:throw new Error(C(e))}}function ir(r,t,e){let{nodes:n,regions:o,variadic:s}=t,c=r.addVector(n,16,8,(u,f)=>(u.writeInt64(f.nullCount),u.writeInt64(f.length),u.offset())),a=r.addVector(o,16,8,(u,f)=>(u.writeInt64(f.length),u.writeInt64(f.offset),u.offset())),i=r.addVector(s,8,8,(u,f)=>u.addInt64(f));return r.addObject(5,u=>{u.addInt64(0,n[0].length,0),u.addOffset(1,c,0),u.addOffset(2,a,0),u.addOffset(3,js(r,e),0),u.addOffset(4,i,0);})}function js(r,t){if(!t)return 0;let{codec:e,method:n}=t;return r.addObject(2,o=>{o.addInt8(0,e,pt.LZ4_FRAME),o.addInt8(1,n,st.BUFFER);})}function Kn(r,t,e){let n=ir(r,t.data,e);return r.addObject(3,o=>{o.addInt64(0,t.id,0),o.addOffset(1,n,0),o.addInt8(2,+t.isDelta,0);})}function ne(r,t){return t?.size>0?r.addOffsetVector(Array.from(t,([e,n])=>{let o=r.addString(`${e}`),s=r.addString(`${n}`);return r.addObject(2,c=>{c.addOffset(0,o,0),c.addOffset(1,s,0);})})):0}function se(r,t){switch(F(t.typeId,l,C)){case l.Dictionary:return to(r,t);case l.Int:return Zs(r,t);case l.Float:return Ws(r,t);case l.Decimal:return Gs(r,t);case l.Date:return zs(r,t);case l.Time:return Qs(r,t);case l.Timestamp:return Ks(r,t);case l.Interval:return qs(r,t);case l.Duration:return $s(r,t);case l.FixedSizeBinary:case l.FixedSizeList:return Ys(r,t);case l.Map:return Xs(r,t);case l.Union:return Js(r,t)}return r.addObject(0)}function zs(r,t){return r.addObject(1,e=>{e.addInt16(0,t.unit,H.MILLISECOND);})}function Gs(r,t){return r.addObject(3,e=>{e.addInt32(0,t.precision,0),e.addInt32(1,t.scale,0),e.addInt32(2,t.bitWidth,128);})}function $s(r,t){return r.addObject(1,e=>{e.addInt16(0,t.unit,w.MILLISECOND);})}function Ys(r,t){return r.addObject(1,e=>{e.addInt32(0,t.stride,0);})}function Ws(r,t){return r.addObject(1,e=>{e.addInt16(0,t.precision,tt.HALF);})}function Zs(r,t){return r.addObject(2,e=>{e.addInt32(0,t.bitWidth,0),e.addInt8(1,+t.signed,0);})}function qs(r,t){return r.addObject(1,e=>{e.addInt16(0,t.unit,N.YEAR_MONTH);})}function Xs(r,t){return r.addObject(1,e=>{e.addInt8(0,+t.keysSorted,0);})}function Qs(r,t){return r.addObject(2,e=>{e.addInt16(0,t.unit,w.MILLISECOND),e.addInt32(1,t.bitWidth,32);})}function Ks(r,t){let e=r.addString(t.timezone);return r.addObject(2,n=>{n.addInt16(0,t.unit,w.SECOND),n.addOffset(1,e,0);})}function Js(r,t){let e=r.addVector(t.typeIds,4,4,(n,o)=>n.addInt32(o));return r.addObject(2,n=>{n.addInt16(0,t.mode,j.Sparse),n.addOffset(1,e,0);})}function to(r,t){return r.addObject(4,e=>{e.addInt64(0,t.id,0),e.addOffset(1,se(r,t.indices),0),e.addInt8(2,+t.ordered,0);})}var eo=new Uint16Array(new Uint8Array([1,0]).buffer)[0]===1;function cr(r,t){let{fields:e,metadata:n}=t,o=e.map(a=>Jn(r,a)),s=r.addOffsetVector(o),c=ne(r,n);return r.addObject(4,a=>{a.addInt16(0,+!eo,0),a.addOffset(1,s,0),a.addOffset(2,c,0);})}function Jn(r,t){let{name:e,nullable:n,type:o,metadata:s}=t,{typeId:c}=o,a=0,i=0;if(c!==l.Dictionary)a=se(r,o);else {let p=o.dictionary;c=p.typeId,i=se(r,o),a=se(r,p);}let u=(o.children||[]).map(p=>Jn(r,p)),f=r.addOffsetVector(u),m=ne(r,s),b=r.addString(e);return r.addObject(7,p=>{p.addOffset(0,b,0),p.addInt8(1,+n,0),p.addInt8(2,c,l.NONE),p.addOffset(3,a,0),p.addOffset(4,i,0),p.addOffset(5,f,0),p.addOffset(6,m,0);})}function es(r,t,e,n,o){let s=ne(r,o),c=r.addVector(n,24,8,ts),a=r.addVector(e,24,8,ts),i=cr(r,t);r.finish(r.addObject(5,f=>{f.addInt16(0,U.V5,U.V1),f.addOffset(1,i,0),f.addOffset(2,a,0),f.addOffset(3,c,0),f.addOffset(4,s,0);}));let u=r.offset();r.addInt32(0),r.addInt32(-1),r.flush(),r.sink.write(new Uint8Array(Int32Array.of(u).buffer)),r.sink.write(ht);}function ts(r,{offset:t,metadataLength:e,bodyLength:n}){return r.writeInt64(n),r.writeInt32(0),r.writeInt32(e),r.writeInt64(t),r.offset()}function ar(r,t,e,n,o){r.finish(r.addObject(5,i=>{i.addInt16(0,U.V5,U.V1),i.addInt8(1,t,P.NONE),i.addOffset(2,e,0),i.addInt64(3,n,0);}));let s=8,c=r.offset(),a=c+s+7&-8;o?.push({offset:r.outputBytes,metadataLength:a,bodyLength:n}),r.addInt32(a-s),r.addInt32(-1),r.flush(),r.addPadding(a-c-s);}var Jr=class{write(t){}pad(t){this.write(new Uint8Array(t));}finish(){return null}},ur=class extends Jr{constructor(){super(),this.buffers=[];}write(t){this.buffers.push(t);}finish(){let t=this.buffers,e=t.reduce((o,s)=>o+s.byteLength,0),n=new Uint8Array(e);for(let o=0,s=0;o<t.length;++o)n.set(t[o],s),s+=t[o].byteLength;return n}};var rs="stream",ns="file";function os(r,{sink:t,format:e=rs,codec:n}={}){if(e!==rs&&e!==ns)throw new Error(`Unrecognized Arrow IPC format: ${e}`);let{schema:o,dictionaries:s=[],records:c=[],metadata:a}=r,i=new er(t||new ur),u=e===ns,f=[],m=[],b=n!=null?{codec:n,method:st.BUFFER}:null;u&&i.addBuffer(ht),o&&ar(i,P.Schema,cr(i,o),0);for(let p of s){let{data:g}=p;ar(i,P.DictionaryBatch,Kn(i,p,b),g.byteLength,f),ss(i,g.buffers);}for(let p of c)ar(i,P.RecordBatch,ir(i,p,b),p.byteLength,m),ss(i,p.buffers);return i.addBuffer(mn),u&&es(i,o,f,m,a),i.sink}function ss(r,t){for(let e=0;e<t.length;++e)r.addBuffer(t[e]);}function is(r,t){typeof t=="string"&&(t={format:t});let e=t?.codec,n=ee(e);if(e!=null&&!n)throw new Error(rr(e));let o=r.children;ro(o);let{dictionaries:s,idMap:c}=so(o,n),a=io(o,n),u={schema:oo(r.schema,c),dictionaries:s,records:a};return os(u,{...t,codec:e}).finish()}function ro(r){let t=r[0]?.data.map(e=>e.length);r.forEach(({data:e})=>{if(e.length!==t.length||e.some((n,o)=>n.length!==t[o]))throw new Error("Columns have inconsistent batch sizes.")});}function no(r){let t=0,e=[],n=[],o=[],s=[];return {node(c,a){e.push({length:c,nullCount:a});},buffer(c){let a=new Uint8Array(c.buffer,c.byteOffset,c.byteLength),i=r?Yn(a,r):a,u=i.byteLength;n.push({offset:t,length:u}),o.push(i),t+=u+7&-8;},variadic(c){s.push(c);},children(c,a){c.children.forEach((i,u)=>{as(i.type,a.children[u],this);});},done(){return {byteLength:t,nodes:e,regions:n,variadic:s,buffers:o}}}}function so(r,t){let e=[],n=new Map,o=new Map,s=-1,c=a=>{if(n.has(a))o.set(a.type,n.get(a));else {n.set(a,++s);for(let i=0;i<a.data.length;++i)e.push({id:s,isDelta:i>0,data:cs([a],i,t)});o.set(a.type,s);}};return r.forEach(a=>tn(a.data[0],c)),{dictionaries:e,idMap:o}}function tn(r,t){if(r?.type.typeId===l.Dictionary){let e=r.dictionary;t(e),tn(e.data[0],t);}r?.children?.forEach(e=>tn(e,t));}function oo(r,t){if(!t.size)return r;let e=s=>{s.typeId===l.Dictionary&&(s.id=t.get(s.dictionary),o(s)),s.children&&(s.children=s.children.slice()).forEach(n);},n=(s,c,a)=>{let i={...s.type};a[c]={...s,type:i},e(i);},o=s=>{let c={...s.dictionary};s.dictionary=c,e(c);};return r={...r,fields:r.fields.slice()},r.fields.forEach(n),r}function io(r,t){return (r[0]?.data||[]).map((e,n)=>cs(r,n,t))}function cs(r,t=0,e){let n=no(e);return r.forEach(o=>{as(o.type,o.data[t],n);}),n.done()}function as(r,t,e){let{typeId:n}=r;if(e.node(t.length,t.nullCount),n!==l.Null)switch(n){case l.Bool:case l.Int:case l.Time:case l.Duration:case l.Float:case l.Date:case l.Timestamp:case l.Decimal:case l.Interval:case l.FixedSizeBinary:case l.Dictionary:e.buffer(t.validity),e.buffer(t.values);return;case l.Utf8:case l.LargeUtf8:case l.Binary:case l.LargeBinary:e.buffer(t.validity),e.buffer(t.offsets),e.buffer(t.values);return;case l.BinaryView:case l.Utf8View:e.buffer(t.validity),e.buffer(t.values),e.variadic(t.data.length),t.data.forEach(o=>e.buffer(o));return;case l.List:case l.LargeList:case l.Map:e.buffer(t.validity),e.buffer(t.offsets),e.children(r,t);return;case l.ListView:case l.LargeListView:e.buffer(t.validity),e.buffer(t.offsets),e.buffer(t.sizes),e.children(r,t);return;case l.FixedSizeList:case l.Struct:e.buffer(t.validity),e.children(r,t);return;case l.RunEndEncoded:e.children(r,t);return;case l.Union:{e.buffer(t.typeIds),r.mode===j.Dense&&e.buffer(t.offsets),e.children(r,t);return}default:throw new Error(C(n))}}function O(r){return new lr(r)}var lr=class{constructor(t=q){this.buf=new t(512);}array(t){return bn(this.buf,t)}prep(t){t>=this.buf.length&&(this.buf=fe(this.buf,t));}get(t){return this.buf[t]}set(t,e){this.prep(e),this.buf[e]=t;}write(t,e){this.prep(e+t.length),this.buf.set(t,e);}};function fr(){return new en}var en=class extends lr{set(t){let e=t>>3;this.prep(e),this.buf[e]|=1<<t%8;}};var ft=class{constructor(t,e){this.type=t,this.ctx=e,this.batchClass=e.batchType(t);}init(){return this.index=-1,this}set(t,e){return this.index=e,false}done(){return null}batch(){let t=new this.batchClass(this.done());return this.init(),t}};var E=class extends ft{constructor(t,e){super(t,e);}init(){return this.nullCount=0,this.validity=fr(),super.init()}set(t,e){this.index=e;let n=t!=null;return n?this.validity.set(e):this.nullCount++,n}done(){let{index:t,nullCount:e,type:n,validity:o}=this;return {length:t+1,nullCount:e,type:n,validity:e?o.array((t>>3)+1):new q(0)}}};function oe(){let r=new Map,t=new Set;return {get(e,n){let o=e.id;if(o>=0&&r.has(o))return r.get(o);{let s=co(e,n);return o>=0&&r.set(o,s),t.add(s),s}},finish(e){t.forEach(n=>n.finish(e));}}}function co(r,t){let e=Object.create(null),n=t.builder(r.dictionary),o=[];n.init();let s=-1;return {type:r,values:n,add(c){return o.push(c),c},key(c){let a=Ct(c),i=e[a];return i===void 0&&(e[a]=i=++s,n.set(c,i)),i},finish(c){let a=r.dictionary,i=new(xt(a,c))(n.done()),u=new J([i]);o.forEach(f=>f.setDictionary(u));}}}var dr=class extends E{constructor(t,e){super(t,e),this.dict=e.dictionary(t);}init(){return this.values=O(this.type.indices.values),super.init()}set(t,e){super.set(t,e)&&this.values.set(this.dict.key(t),e);}done(){return {...super.done(),values:this.values.array(this.index+1)}}batch(){return this.dict.add(super.batch())}};function us(r){let t=rn();return r(e=>t.add(e)),t.type()}function rn(){let r=0,t=0,e=0,n=0,o=0,s=0,c=0,a=0,i=0,u=0,f=0,m=1/0,b=-1/0,p=1/0,g=-1/0,A,k,Z,Tt={};return {add(I){if(r++,I==null){t++;return}switch(typeof I){case "string":i++;break;case "number":n++,I<m&&(m=I),I>b&&(b=I),Number.isInteger(I)&&o++;break;case "bigint":s++,A===void 0?A=k=I:(I<A&&(A=I),I>k&&(k=I));break;case "boolean":e++;break;case "object":if(I instanceof Date)c++,+I%864e5===0&&a++;else if(le(I)){u++;let R=I.length;R<p&&(p=R),R>g&&(g=R),Z??=rn(),I.forEach(Z.add);}else {f++;for(let R in I)(Tt[R]??(Tt[R]=rn())).add(I[R]);}}},type(){let I=r-t;return I===0?Pr():o===I?uo(m,b):n===I?rt():s===I?lo(A,k):e===I?Mt():a===I?Nt():c===I?ut():i===I?Gt($()):u===I?ao(Z.type(),p,g):f===I?Wt(Object.entries(Tt).map(R=>yt(R[0],R[1].type()))):fo()}}}function ao(r,t,e){return e===t?Zt(r,t):Yt(r)}function uo(r,t){let e=Math.max(Math.abs(r)-1,t);return e<128?it():e<32768?ct():e<2**31?G():rt()}function lo(r,t){let e=-r>t?-r-1n:t;if(e>=2**63)throw new Error(`BigInt exceeds 64 bits: ${e}`);return at()}function fo(){throw new Error("Mixed types detected, please define a union type.")}var Ht=class extends E{constructor(t,e){super(t,e),this.toOffset=pe(t.offsets);}init(){return this.offsets=O(this.type.offsets),this.values=O(),this.pos=0,super.init()}set(t,e){let{offsets:n,values:o,toOffset:s}=this;super.set(t,e)&&(o.write(t,this.pos),this.pos+=t.length),n.set(s(this.pos),e+1);}done(){return {...super.done(),offsets:this.offsets.array(this.index+2),values:this.values.array(this.pos+1)}}};var mr=class extends E{constructor(t,e){super(t,e);}init(){return this.values=fr(),super.init()}set(t,e){super.set(t,e),t&&this.values.set(e);}done(){return {...super.done(),values:this.values.array((this.index>>3)+1)}}};var hr=class extends E{constructor(t,e){super(t,e),this.scale=10**t.scale,this.stride=t.bitWidth>>6;}init(){return this.values=O(this.type.values),super.init()}set(t,e){let{scale:n,stride:o,values:s}=this;super.set(t,e)&&(s.prep((e+1)*o),Bn(t,s.buf,e*o,o,n));}done(){let{index:t,stride:e,values:n}=this;return {...super.done(),values:n.array((t+1)*e)}}};var pr=class extends E{constructor(t,e){super(t,e),this.stride=t.stride;}init(){return this.values=O(),super.init()}set(t,e){super.set(t,e)&&this.values.write(t,e*this.stride);}done(){let{stride:t,values:e}=this;return {...super.done(),values:e.array(t*(this.index+1))}}};var br=class extends E{constructor(t,e){super(t,e),this.child=e.builder(this.type.children[0].type),this.stride=t.stride;}init(){return this.child.init(),super.init()}set(t,e){let{child:n,stride:o}=this,s=e*o;if(super.set(t,e))for(let c=0;c<o;++c)n.set(t[c],s+c);else n.index=s+o;}done(){let{child:t}=this;return {...super.done(),children:[t.batch()]}}};var yr=class extends E{init(){return this.values=O(this.type.values),super.init()}set(t,e){if(super.set(t,e)){let n=e<<1;this.values.set(t[0],n),this.values.set(t[1],n+1);}}done(){return {...super.done(),values:this.values.array(this.index+1<<1)}}},gr=class extends E{init(){return this.values=O(),super.init()}set(t,e){super.set(t,e)&&this.values.write(Dn(t),e<<4);}done(){return {...super.done(),values:this.values.array(this.index+1<<4)}}};var ie=class extends E{constructor(t,e,n){super(t,e),this.child=n;}init(){this.child.init();let t=this.type.offsets;return this.offsets=O(t),this.toOffset=pe(t),this.pos=0,super.init()}done(){return {...super.done(),offsets:this.offsets.array(this.index+2),children:[this.child.batch()]}}},Ir=class extends ie{constructor(t,e){super(t,e,e.builder(t.children[0].type));}set(t,e){let{child:n,offsets:o,toOffset:s}=this;super.set(t,e)&&t.forEach(c=>n.set(c,this.pos++)),o.set(s(this.pos),e+1);}};var ce=class extends E{constructor(t,e){super(t,e),this.children=t.children.map(n=>e.builder(n.type));}init(){return this.children.forEach(t=>t.init()),super.init()}done(){let{children:t}=this;return t.forEach(e=>e.index=this.index),{...super.done(),children:t.map(e=>e.batch())}}},wr=class extends ce{constructor(t,e){super(t,e),this.setters=this.children.map((n,o)=>{let s=t.children[o].name;return (c,a)=>n.set(c?.[s],a)});}set(t,e){super.set(t,e);let n=this.setters;for(let o=0;o<n.length;++o)n[o](t,e);}};var xr=class extends ie{constructor(t,e){super(t,e,new nn(t.children[0].type,e));}set(t,e){let{child:n,offsets:o,toOffset:s}=this;if(super.set(t,e))for(let c of t)n.set(c,this.pos++);o.set(s(this.pos),e+1);}},nn=class extends ce{set(t,e){super.set(t,e);let[n,o]=this.children;n.set(t[0],e),o.set(t[1],e);}};var mo={},Ar=class extends ft{constructor(t,e){super(t,e),this.children=t.children.map(n=>e.builder(n.type));}init(){return this.pos=0,this.key=null,this.value=mo,this.children.forEach(t=>t.init()),super.init()}next(){let[t,e]=this.children;t.set(this.index+1,this.pos),e.set(this.value,this.pos++);}set(t,e){if(t!==this.value){let n=Ct(t);n!==this.key&&(this.key&&this.next(),this.key=n,this.value=t);}this.index=e;}done(){this.next();let{children:t,index:e,type:n}=this;return {length:e+1,nullCount:0,type:n,children:t.map(o=>o.batch())}}};var Tr=class extends ft{constructor(t,e){super(t,e),this.children=t.children.map(n=>e.builder(n.type)),this.typeMap=t.typeMap,this.lookup=t.typeIdForValue;}init(){return this.nullCount=0,this.typeIds=O(bt),this.children.forEach(t=>t.init()),super.init()}set(t,e){let{children:n,lookup:o,typeMap:s,typeIds:c}=this;this.index=e;let a=o(t,e),i=n[s[a]];c.set(a,e),t==null&&++this.nullCount,this.update(t,e,i);}done(){let{children:t,nullCount:e,type:n,typeIds:o}=this,s=this.index+1;return {length:s,nullCount:e,type:n,typeIds:o.array(s),children:t.map(c=>c.batch())}}},Er=class extends Tr{update(t,e,n){n.set(t,e),this.children.forEach(o=>{o!==n&&o.set(null,e);});}},_r=class extends Tr{init(){return this.offsets=O(this.type.offsets),super.init()}update(t,e,n){let o=n.index+1;n.set(t,o),this.offsets.set(o,e);}done(){return {...super.done(),offsets:this.offsets.array(this.index+1)}}};var kr=class extends Ht{set(t,e){super.set(t&&ye(t),e);}};var dt=class extends E{constructor(t,e){super(t,e),this.values=O(t.values);}init(){return this.values=O(this.type.values),super.init()}set(t,e){super.set(t,e)&&this.values.set(t,e);}done(){return {...super.done(),values:this.values.array(this.index+1)}}},Sr=class extends dt{set(t,e){super.set(t==null?t:Y(t),e);}},At=class extends dt{constructor(t,e,n){super(t,e),this.transform=n;}set(t,e){super.set(t==null?t:this.transform(t),e);}};function sn(r={},t=oe()){return {batchType:e=>xt(e,r),builder(e){return on(e,this)},dictionary(e){return t.get(e,this)},finish:()=>t.finish(r)}}function on(r,t=sn()){let{typeId:e}=r;switch(e){case l.Int:case l.Time:case l.Duration:return _t(r.values)?new Sr(r,t):new dt(r,t);case l.Float:return r.precision?new dt(r,t):new At(r,t,Nn);case l.Binary:case l.LargeBinary:return new Ht(r,t);case l.Utf8:case l.LargeUtf8:return new kr(r,t);case l.Bool:return new mr(r,t);case l.Decimal:return r.bitWidth===32?new At(r,t,On(r.scale)):new hr(r,t);case l.Date:return new At(r,t,r.unit?Y:kn);case l.Timestamp:return new At(r,t,Sn(r.unit));case l.Interval:switch(r.unit){case N.DAY_TIME:return new yr(r,t);case N.MONTH_DAY_NANO:return new gr(r,t)}return new dt(r,t);case l.List:case l.LargeList:return new Ir(r,t);case l.Struct:return new wr(r,t);case l.Union:return r.mode?new _r(r,t):new Er(r,t);case l.FixedSizeBinary:return new pr(r,t);case l.FixedSizeList:return new br(r,t);case l.Map:return new xr(r,t);case l.RunEndEncoded:return new Ar(r,t);case l.Dictionary:return new dr(r,t)}throw new Error(C(e))}function cn(r,t,e={},n){let o=In(r)?i=>{for(let u of r)i(u);}:r;t??=us(o);let{maxBatchRows:s=1/0,...c}=e,a;if(t.typeId===l.Null){let i=0;o(()=>++i),a=ho(t,i,s);}else {let i=sn(c,n),u=on(t,i).init(),f=b=>a.push(b.batch());a=[];let m=0;o(b=>{u.set(b,m++),m>=s&&(f(u),m=0);}),m&&f(u),i.finish();}return new J(a,t)}function ho(r,t,e){let n=[],o=a=>new Rt({length:a,nullCount:a,type:r}),s=Math.floor(t/e);for(let a=0;a<s;++a)n.push(o(e));let c=t%e;return c&&n.push(o(c)),n}function an(r,t,e={},n){return !t&&Ur(r)?po(r,e):cn(o=>r.forEach(o),t,e,n)}function po(r,{maxBatchRows:t,useBigInt:e}){let n=r.constructor,o=bo(n),s=r.length,c=Math.min(t||1/0,s),a=Math.floor(s/c),i=[],u=_t(n)&&!e?W:K,f=(b,p)=>i.push(new u({length:p-b,nullCount:0,type:o,validity:new q(0),values:r.subarray(b,p)})),m=0;for(let b=0;b<a;++b)f(m,m+=c);return m<s&&f(m,s),new J(i)}function bo(r){switch(r){case ue:return Pt();case et:return rt();case bt:return it();case Br:return ct();case M:return G();case v:return at();case q:return Dt();case jt:return Ot();case zt:return Bt();case Et:return Ut()}}function un(r,t){let e=[],n=Array.isArray(r)?r:Object.entries(r),o=n[0]?.[1].length,s=n.map(([a,i])=>{if(i.length!==o)throw new Error("All columns must have the same length.");return e.push(yt(a,i.type)),i}),c={version:U.V5,endianness:Or.Little,fields:e,metadata:null};return new wt(c,s,t)}function ae(r,t={}){let{types:e={},...n}=t,o=oe(),c=(Array.isArray(r)?r:Object.entries(r)).map(([a,i])=>[a,an(i,e[a],n,o)]);return un(c,t.useProxy)}var h={INVALID:0,BOOLEAN:1,TINYINT:2,SMALLINT:3,INTEGER:4,BIGINT:5,UTINYINT:6,USMALLINT:7,UINTEGER:8,UBIGINT:9,FLOAT:10,DOUBLE:11,TIMESTAMP:12,DATE:13,TIME:14,INTERVAL:15,HUGEINT:16,UHUGEINT:32,VARCHAR:17,BLOB:18,DECIMAL:19,TIMESTAMP_S:20,TIMESTAMP_MS:21,TIMESTAMP_NS:22,ENUM:23,LIST:24,STRUCT:25,MAP:26,ARRAY:33,UUID:27,UNION:28,BIT:29,TIME_TZ:30,TIMESTAMP_TZ:31},yo=(n=>(n.AUTOMATIC="automatic",n.READ_ONLY="read_only",n.READ_WRITE="read_write",n))(yo||{}),y=class r extends Error{code;query;constructor(t,e,n){super(t),this.name="DuckDBError",this.code=e,this.query=n,Error.captureStackTrace&&Error.captureStackTrace(this,r);}};function go(r){let t=r.replace(/\/\*[\s\S]*?\*\//g," ");return t=t.replace(/--[^\n\r]*/g," "),t=t.replace(/#[^\n\r]*/g," "),t}var mt={secretsFunction:{pattern:/\bduckdb_secrets\s*\(/i,message:"Access to duckdb_secrets() is not allowed"},pragma:{pattern:/^\s*PRAGMA\b/im,message:"PRAGMA statements are not allowed"},copyTo:{pattern:/\bCOPY\b[\s\S]+?\bTO\b\s+['"`]/i,message:"COPY ... TO statements are not allowed"},exportDatabase:{pattern:/\bEXPORT\s+DATABASE\b/i,message:"EXPORT DATABASE statements are not allowed"}};function Io(r,t={}){let e=go(r);return !t.allowSecretsFunction&&mt.secretsFunction.pattern.test(e)?{safe:false,sql:r,reason:mt.secretsFunction.message,matchedPattern:"duckdb_secrets()"}:!t.allowPragma&&mt.pragma.pattern.test(e)?{safe:false,sql:r,reason:mt.pragma.message,matchedPattern:"PRAGMA"}:!t.allowCopyTo&&mt.copyTo.pattern.test(e)?{safe:false,sql:r,reason:mt.copyTo.message,matchedPattern:"COPY ... TO"}:!t.allowExportDatabase&&mt.exportDatabase.pattern.test(e)?{safe:false,sql:r,reason:mt.exportDatabase.message,matchedPattern:"EXPORT DATABASE"}:{safe:true,sql:r}}function ul(r,t={}){let e=Io(r,t);if(!e.safe)throw new y(e.reason,"SANITIZE_ERROR",r);return r}var d=null,Dr=null;function _(){if(!d)throw new y("DuckDB WASM not initialized. Call init() first.");return d}async function ll(r){if(!d){if(Dr){await Dr;return}if(!r?.wasmModule)throw new Error("Workers build requires a pre-compiled WASM module. Import the WASM file and pass it as { wasmModule }.");Dr=(async()=>{let t=(await import('./wasm/duckdb-workers.js')).default;return await t({instantiateWasm:(o,s)=>(WebAssembly.instantiate(r.wasmModule,o).then(c=>{s(c);}),{})})})(),d=await Dr;}}function fl(){if(!d)throw new y("DuckDB WASM not initialized. Call init() first.");let r=d.ccall("duckdb_library_version","number",[],[]);return d.UTF8ToString(r)}function wo(r,t,e,n,o){let s=new Array(n),c=e!==0;switch(o){case h.BOOLEAN:{for(let a=0;a<n;a++)c&&r.HEAPU8[e+a]?s[a]=null:s[a]=r.HEAPU8[t+a]!==0;break}case h.TINYINT:{for(let a=0;a<n;a++)c&&r.HEAPU8[e+a]?s[a]=null:s[a]=r.HEAP8[t+a];break}case h.SMALLINT:{let a=t>>1;for(let i=0;i<n;i++)c&&r.HEAPU8[e+i]?s[i]=null:s[i]=r.HEAP16[a+i];break}case h.INTEGER:{let a=t>>2;for(let i=0;i<n;i++)c&&r.HEAPU8[e+i]?s[i]=null:s[i]=r.HEAP32[a+i];break}case h.BIGINT:{let a=t>>2;for(let i=0;i<n;i++)if(c&&r.HEAPU8[e+i])s[i]=null;else {let u=r.HEAPU32[a+i*2],f=r.HEAP32[a+i*2+1];if(f===0&&u<=9007199254740991)s[i]=u;else if(f===-1&&u>=2147483648)s[i]=f*4294967296+u;else {let m=BigInt(f)*BigInt(4294967296)+BigInt(u>>>0);s[i]=m.toString();}}break}case h.UTINYINT:{for(let a=0;a<n;a++)c&&r.HEAPU8[e+a]?s[a]=null:s[a]=r.HEAPU8[t+a];break}case h.USMALLINT:{let a=t>>1;for(let i=0;i<n;i++)c&&r.HEAPU8[e+i]?s[i]=null:s[i]=r.HEAPU16[a+i];break}case h.UINTEGER:{let a=t>>2;for(let i=0;i<n;i++)c&&r.HEAPU8[e+i]?s[i]=null:s[i]=r.HEAPU32[a+i];break}case h.UBIGINT:{let a=t>>2;for(let i=0;i<n;i++)if(c&&r.HEAPU8[e+i])s[i]=null;else {let u=r.HEAPU32[a+i*2],f=r.HEAPU32[a+i*2+1];if(f===0&&u<=9007199254740991)s[i]=u;else {let m=BigInt(f)*BigInt(4294967296)+BigInt(u);s[i]=m.toString();}}break}case h.FLOAT:{let a=t>>2;for(let i=0;i<n;i++)c&&r.HEAPU8[e+i]?s[i]=null:s[i]=r.HEAPF32[a+i];break}case h.DOUBLE:{let a=t>>3;for(let i=0;i<n;i++)c&&r.HEAPU8[e+i]?s[i]=null:s[i]=r.HEAPF64[a+i];break}case h.DATE:{let a=t>>2;for(let i=0;i<n;i++)if(c&&r.HEAPU8[e+i])s[i]=null;else {let u=r.HEAP32[a+i],f=new Date(u*24*60*60*1e3);s[i]=f.toISOString().split("T")[0];}break}case h.TIMESTAMP:case h.TIMESTAMP_TZ:{let a=t>>2;for(let i=0;i<n;i++)if(c&&r.HEAPU8[e+i])s[i]=null;else {let u=r.HEAPU32[a+i*2],f=r.HEAP32[a+i*2+1],m=BigInt(f)*BigInt(4294967296)+BigInt(u>>>0),b=Number(m/BigInt(1e3)),p=new Date(b);s[i]=p.toISOString();}break}default:for(let a=0;a<n;a++)s[a]=null;break}return s}var ln=class{stmtPtr;closed=false;sql;constructor(t,e,n){this.stmtPtr=t,this.sql=n;}parameterCount(){if(this.closed)throw new y("Statement is closed");return _().ccall("duckdb_nparams","number",["number"],[this.stmtPtr])}bindBoolean(t,e){if(this.closed)throw new y("Statement is closed");if(_().ccall("duckdb_bind_boolean","number",["number","number","number","number"],[this.stmtPtr,t,0,e?1:0])!==0)throw new y(`Failed to bind boolean at index ${t}`);return this}bindInt32(t,e){if(this.closed)throw new y("Statement is closed");if(_().ccall("duckdb_bind_int32","number",["number","number","number","number"],[this.stmtPtr,t,0,e])!==0)throw new y(`Failed to bind int32 at index ${t}`);return this}bindInt64(t,e){if(this.closed)throw new y("Statement is closed");let n=_(),o=BigInt(e),s=Number(o&BigInt(4294967295)),c=Number(o>>BigInt(32)&BigInt(4294967295));if(n.ccall("duckdb_bind_int64","number",["number","number","number","number","number"],[this.stmtPtr,t,0,s,c])!==0)throw new y(`Failed to bind int64 at index ${t}`);return this}bindFloat(t,e){if(this.closed)throw new y("Statement is closed");if(_().ccall("duckdb_bind_float","number",["number","number","number","number"],[this.stmtPtr,t,0,e])!==0)throw new y(`Failed to bind float at index ${t}`);return this}bindDouble(t,e){if(this.closed)throw new y("Statement is closed");if(_().ccall("duckdb_bind_double","number",["number","number","number","number"],[this.stmtPtr,t,0,e])!==0)throw new y(`Failed to bind double at index ${t}`);return this}bindString(t,e){if(this.closed)throw new y("Statement is closed");if(_().ccall("duckdb_bind_varchar","number",["number","number","number","string"],[this.stmtPtr,t,0,e])!==0)throw new y(`Failed to bind string at index ${t}`);return this}bindBlob(t,e){if(this.closed)throw new y("Statement is closed");let n=_(),o=n._malloc(e.length);try{if(n.HEAPU8.set(e,o),n.ccall("duckdb_bind_blob","number",["number","number","number","number","number","number"],[this.stmtPtr,t,0,o,e.length,0])!==0)throw new y(`Failed to bind blob at index ${t}`)}finally{n._free(o);}return this}bindNull(t){if(this.closed)throw new y("Statement is closed");if(_().ccall("duckdb_bind_null","number",["number","number","number"],[this.stmtPtr,t,0])!==0)throw new y(`Failed to bind null at index ${t}`);return this}bindTimestamp(t,e){if(this.closed)throw new y("Statement is closed");let n=_(),o=BigInt(e.getTime())*BigInt(1e3),s=Number(o&BigInt(4294967295)),c=Number(o>>BigInt(32)&BigInt(4294967295));if(n.ccall("duckdb_bind_timestamp","number",["number","number","number","number","number"],[this.stmtPtr,t,0,s,c])!==0)throw new y(`Failed to bind timestamp at index ${t}`);return this}bindDate(t,e){if(this.closed)throw new y("Statement is closed");let n=_(),o=Math.floor(e.getTime()/(1440*60*1e3));if(n.ccall("duckdb_bind_date","number",["number","number","number","number"],[this.stmtPtr,t,0,o])!==0)throw new y(`Failed to bind date at index ${t}`);return this}bind(t,e){return e==null?this.bindNull(t):typeof e=="boolean"?this.bindBoolean(t,e):typeof e=="number"?Number.isInteger(e)&&e>=-2147483648&&e<=2147483647?this.bindInt32(t,e):this.bindDouble(t,e):typeof e=="bigint"?this.bindInt64(t,e):typeof e=="string"?this.bindString(t,e):e instanceof Date?this.bindTimestamp(t,e):e instanceof Uint8Array?this.bindBlob(t,e):this.bindString(t,String(e))}async run(){if(this.closed)throw new y("Statement is closed");let t=_(),e=t._malloc(64);try{if(await t.ccall("duckdb_execute_prepared","number",["number","number"],[this.stmtPtr,e],{async:!0})!==0){let u=t.ccall("duckdb_result_error","number",["number"],[e]),f=u?t.UTF8ToString(u):"Prepared statement execution failed";throw t.ccall("duckdb_destroy_result",null,["number"],[e]),new y(f,void 0,this.sql)}let o=t.ccall("duckdb_column_count","number",["number"],[e]),s=t.ccall("duckdb_row_count","number",["number"],[e]),c=[];for(let u=0;u<o;u++){let f=t.ccall("duckdb_column_name","number",["number","number"],[e,u]),m=t.ccall("duckdb_column_type","number",["number","number"],[e,u]);c.push({name:t.UTF8ToString(f),type:m});}let a=[];for(let u=0;u<o;u++){let f=c[u],m=t.ccall("duckdb_column_data","number",["number","number"],[e,u]),b=t.ccall("duckdb_nullmask_data","number",["number","number"],[e,u]);if(f.type===h.VARCHAR||m===0){let p=new Array(s);for(let g=0;g<s;g++)if(t.ccall("duckdb_value_is_null","number",["number","number","number","number","number"],[e,u,0,g,0]))p[g]=null;else {let k=t.ccall("duckdb_value_varchar","number",["number","number","number","number","number"],[e,u,0,g,0]);k?(p[g]=t.UTF8ToString(k),t._free(k)):p[g]=null;}a.push(p);}else a.push(wo(t,m,b,s,f.type));}let i=new Array(s);for(let u=0;u<s;u++){let f={};for(let m=0;m<o;m++)f[c[m].name]=a[m][u];i[u]=f;}return t.ccall("duckdb_destroy_result",null,["number"],[e]),i}finally{t._free(e);}}async execute(){if(this.closed)throw new y("Statement is closed");let t=_(),e=t._malloc(64);try{if(await t.ccall("duckdb_execute_prepared","number",["number","number"],[this.stmtPtr,e],{async:!0})!==0){let s=t.ccall("duckdb_result_error","number",["number"],[e]),c=s?t.UTF8ToString(s):"Prepared statement execution failed";throw t.ccall("duckdb_destroy_result",null,["number"],[e]),new y(c,void 0,this.sql)}let o=t.ccall("duckdb_rows_changed","number",["number"],[e]);return t.ccall("duckdb_destroy_result",null,["number"],[e]),o}finally{t._free(e);}}close(){if(this.closed)return;let t=_(),e=t._malloc(4);try{t.setValue(e,this.stmtPtr,"i32"),t.ccall("duckdb_destroy_prepare",null,["number"],[e]);}finally{t._free(e);}this.closed=true,this.stmtPtr=0;}},fn=class{chunkPtr;destroyed=false;columns;constructor(t,e){this.chunkPtr=t,this.columns=e;}get rowCount(){return this.destroyed?0:_().ccall("duckdb_data_chunk_get_size","number",["number"],[this.chunkPtr])}get columnCount(){return this.destroyed?0:_().ccall("duckdb_data_chunk_get_column_count","number",["number"],[this.chunkPtr])}getColumnInfo(){return this.columns}getColumn(t){if(this.destroyed)throw new y("DataChunk has been destroyed");let e=_(),n=this.rowCount,o=e.ccall("duckdb_data_chunk_get_vector","number",["number","number"],[this.chunkPtr,t]);if(!o)throw new y(`Failed to get vector for column ${t}`);let s=e.ccall("duckdb_vector_get_data","number",["number"],[o]),c=e.ccall("duckdb_vector_get_validity","number",["number"],[o]),a=this.columns[t]?.type??h.VARCHAR;return this.readVectorData(s,c,n,a)}toArray(){if(this.destroyed)return [];let t=this.rowCount,e=this.columnCount,n=[];for(let s=0;s<e;s++)n.push(this.getColumn(s));let o=new Array(t);for(let s=0;s<t;s++){let c={};for(let a=0;a<e;a++)c[this.columns[a].name]=n[a][s];o[s]=c;}return o}readVectorData(t,e,n,o){let s=_(),c=new Array(n),a=i=>e===0?false:s.ccall("duckdb_validity_row_is_valid","number",["number","number"],[e,i])===0;switch(o){case h.BOOLEAN:for(let i=0;i<n;i++)c[i]=a(i)?null:s.HEAPU8[t+i]!==0;break;case h.TINYINT:for(let i=0;i<n;i++)c[i]=a(i)?null:s.HEAP8[t+i];break;case h.SMALLINT:{let i=t>>1;for(let u=0;u<n;u++)c[u]=a(u)?null:s.HEAP16[i+u];break}case h.INTEGER:{let i=t>>2;for(let u=0;u<n;u++)c[u]=a(u)?null:s.HEAP32[i+u];break}case h.BIGINT:{let i=t>>2;for(let u=0;u<n;u++)if(a(u))c[u]=null;else {let f=s.HEAPU32[i+u*2],m=s.HEAP32[i+u*2+1];c[u]=m*4294967296+f;}break}case h.FLOAT:{let i=t>>2;for(let u=0;u<n;u++)c[u]=a(u)?null:s.HEAPF32[i+u];break}case h.DOUBLE:{let i=t>>3;for(let u=0;u<n;u++)c[u]=a(u)?null:s.HEAPF64[i+u];break}case h.DATE:{let i=t>>2;for(let u=0;u<n;u++)if(a(u))c[u]=null;else {let f=s.HEAP32[i+u],m=new Date(f*24*60*60*1e3);c[u]=m.toISOString().split("T")[0];}break}case h.TIMESTAMP:case h.TIMESTAMP_TZ:{let i=t>>2;for(let u=0;u<n;u++)if(a(u))c[u]=null;else {let f=s.HEAPU32[i+u*2],b=s.HEAP32[i+u*2+1]*4294967296+f,p=new Date(b/1e3);c[u]=p.toISOString();}break}default:for(let i=0;i<n;i++)if(a(i))c[i]=null;else {let u=t+i*16,f=s.HEAP32[u>>2];if(f<=12){let m="";for(let b=0;b<f;b++)m+=String.fromCharCode(s.HEAPU8[u+4+b]);c[i]=m;}else {let m=s.HEAP32[u+8>>2];c[i]=s.UTF8ToString(m);}}break}return c}destroy(){if(this.destroyed)return;let t=_(),e=t._malloc(4);try{t.setValue(e,this.chunkPtr,"i32"),t.ccall("duckdb_destroy_data_chunk",null,["number"],[e]);}finally{t._free(e);}this.destroyed=true,this.chunkPtr=0;}},ls=class{resultPtr;closed=false;columns;currentChunkIndex=0;totalChunks;constructor(t,e){this.resultPtr=t,this.columns=e;let n=_();this.totalChunks=n.ccall("duckdb_result_chunk_count","number",["number"],[t]);}getColumns(){return this.columns}get columnCount(){return this.columns.length}get chunkCount(){return this.totalChunks}nextChunk(){if(this.closed||this.currentChunkIndex>=this.totalChunks)return null;let e=_().ccall("duckdb_result_get_chunk","number",["number","number"],[this.resultPtr,this.currentChunkIndex]);return e?(this.currentChunkIndex++,new fn(e,this.columns)):null}reset(){this.currentChunkIndex=0;}*[Symbol.iterator](){this.reset();let t;for(;(t=this.nextChunk())!==null;)try{yield t;}finally{t.destroy();}}toArray(){let t=[];for(let e of this)t.push(...e.toArray());return t}toArrowTable(){let t={},e={};for(let n of this.columns)t[n.name]=[],e[n.name]=this.getFlechetteType(n.type);for(let n of this)for(let o=0;o<this.columns.length;o++){let s=this.columns[o].name,c=n.getColumn(o);t[s].push(...c);}return ae(t,{types:e})}getFlechetteType(t){switch(t){case h.BOOLEAN:return Mt();case h.TINYINT:return it();case h.SMALLINT:return ct();case h.INTEGER:return G();case h.BIGINT:return at();case h.UTINYINT:return Dt();case h.USMALLINT:return Ot();case h.UINTEGER:return Bt();case h.UBIGINT:return Ut();case h.FLOAT:return Pt();case h.DOUBLE:return rt();case h.VARCHAR:return $();case h.DATE:return Nt();case h.TIMESTAMP:case h.TIMESTAMP_TZ:return ut(w.MICROSECOND);default:return $()}}close(){if(this.closed)return;_().ccall("duckdb_destroy_result",null,["number"],[this.resultPtr]),this.closed=true,this.resultPtr=0;}},fs=class r{dbPtr=0;closed=false;constructor(t={}){if(!d)throw new y("DuckDB WASM not initialized. Call init() first.");let e=d,n={accessMode:t.accessMode??"automatic",enableExternalAccess:t.enableExternalAccess??true,lockConfiguration:t.lockConfiguration??true,customConfig:t.customConfig??{}},o=e._malloc(4);if(e.ccall("duckdb_create_config","number",["number"],[o])!==0)throw e._free(o),new y("Failed to create DuckDB configuration");let c=e.getValue(o,"i32");e._free(o);try{let a=(f,m)=>{if(e.ccall("duckdb_set_config","number",["number","string","string"],[c,f,m])!==0)throw new y(`Failed to set config option: ${f}`)};n.accessMode!=="automatic"&&a("access_mode",n.accessMode),n.enableExternalAccess===!1&&a("enable_external_access","false");for(let[f,m]of Object.entries(n.customConfig))a(f,m);let i=e._malloc(4),u=e._malloc(4);try{if(e.ccall("duckdb_open_ext","number",["string","number","number","number"],[":memory:",i,c,u])!==0){let m=e.getValue(u,"i32"),b=m?e.UTF8ToString(m):"Unknown error";throw new y(`Failed to open database: ${b}`)}this.dbPtr=e.getValue(i,"i32");}finally{e._free(i),e._free(u);}if(n.enableExternalAccess!==!1&&e.ccall("duckdb_wasm_httpfs_init",null,["number"],[this.dbPtr]),n.lockConfiguration!==!1){let f=e._malloc(4);try{if(e.ccall("duckdb_connect","number",["number","number"],[this.dbPtr,f])===0){let b=e.getValue(f,"i32"),p=e._malloc(64);try{e.ccall("duckdb_query","number",["number","string","number"],[b,"SET lock_configuration = true",p]),e.ccall("duckdb_destroy_result",null,["number"],[p]);}finally{e._free(p);}e.ccall("duckdb_disconnect",null,["number"],[f]);}}finally{e._free(f);}}}finally{let a=e._malloc(4);e.setValue(a,c,"i32"),e.ccall("duckdb_destroy_config",null,["number"],[a]),e._free(a);}}static async create(t){return new r(t)}connect(){if(this.closed||!d)throw new y("Database is closed");let t=d._malloc(4);try{if(d.ccall("duckdb_connect","number",["number","number"],[this.dbPtr,t])!==0)throw new y("Failed to create connection");let n=d.getValue(t,"*");return new dn(n)}finally{d._free(t);}}close(){if(this.closed||!d)return;let t=d._malloc(4);try{d.setValue(t,this.dbPtr,"*"),d.ccall("duckdb_close",null,["number"],[t]);}finally{d._free(t);}this.closed=true,this.dbPtr=0;}},dn=class{connPtr;closed=false;constructor(t){this.connPtr=t;}async query(t){if(this.closed||!d)throw new y("Connection is closed");let e=d._malloc(64);try{if(await d.ccall("duckdb_query","number",["number","string","number"],[this.connPtr,t,e],{async:!0})!==0){let u=d.ccall("duckdb_result_error","number",["number"],[e]),f=u?d.UTF8ToString(u):"Query failed";throw d.ccall("duckdb_destroy_result",null,["number"],[e]),new y(f,void 0,t)}let o=d.ccall("duckdb_column_count","number",["number"],[e]),s=[];for(let u=0;u<o;u++){let f=d.ccall("duckdb_column_name","number",["number","number"],[e,u]),m=d.ccall("duckdb_column_type","number",["number","number"],[e,u]);s.push({name:d.UTF8ToString(f),type:m});}let c=d.ccall("duckdb_row_count","number",["number"],[e]),a=[];for(let u=0;u<o;u++){let f=s[u],m=d.ccall("duckdb_column_data","number",["number","number"],[e,u]),b=d.ccall("duckdb_nullmask_data","number",["number","number"],[e,u]);if(f.type===h.VARCHAR||m===0){let p=new Array(c);for(let g=0;g<c;g++)if(d.ccall("duckdb_value_is_null","number",["number","number","number","number","number"],[e,u,0,g,0]))p[g]=null;else {let k=d.ccall("duckdb_value_varchar","number",["number","number","number","number","number"],[e,u,0,g,0]);k?(p[g]=d.UTF8ToString(k),d._free(k)):p[g]=null;}a.push(p);}else a.push(this.readColumnData(m,b,c,f.type));}let i=new Array(c);for(let u=0;u<c;u++){let f={};for(let m=0;m<o;m++)f[s[m].name]=a[m][u];i[u]=f;}return d.ccall("duckdb_destroy_result",null,["number"],[e]),i}finally{d._free(e);}}readColumnData(t,e,n,o){if(!d)return [];let s=new Array(n),c=e!==0;switch(o){case h.BOOLEAN:{let a=t;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAPU8[a+i]!==0;break}case h.TINYINT:{for(let a=0;a<n;a++)c&&d.HEAPU8[e+a]?s[a]=null:s[a]=d.HEAP8[t+a];break}case h.SMALLINT:{let a=t>>1;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAP16[a+i];break}case h.INTEGER:{let a=t>>2;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAP32[a+i];break}case h.BIGINT:{let a=t>>2;for(let i=0;i<n;i++)if(c&&d.HEAPU8[e+i])s[i]=null;else {let u=d.HEAPU32[a+i*2],f=d.HEAP32[a+i*2+1];if(f===0&&u<=9007199254740991)s[i]=u;else if(f===-1&&u>=2147483648)s[i]=f*4294967296+u;else {let m=BigInt(f)*BigInt(4294967296)+BigInt(u>>>0);s[i]=m.toString();}}break}case h.UTINYINT:{for(let a=0;a<n;a++)c&&d.HEAPU8[e+a]?s[a]=null:s[a]=d.HEAPU8[t+a];break}case h.USMALLINT:{let a=t>>1;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAPU16[a+i];break}case h.UINTEGER:{let a=t>>2;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAPU32[a+i];break}case h.UBIGINT:{let a=t>>2;for(let i=0;i<n;i++)if(c&&d.HEAPU8[e+i])s[i]=null;else {let u=d.HEAPU32[a+i*2],f=d.HEAPU32[a+i*2+1];if(f===0&&u<=9007199254740991)s[i]=u;else {let m=BigInt(f)*BigInt(4294967296)+BigInt(u);s[i]=m.toString();}}break}case h.FLOAT:{let a=t>>2;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAPF32[a+i];break}case h.DOUBLE:{let a=t>>3;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAPF64[a+i];break}case h.DATE:{let a=t>>2;for(let i=0;i<n;i++)if(c&&d.HEAPU8[e+i])s[i]=null;else {let u=d.HEAP32[a+i],f=new Date(u*24*60*60*1e3);s[i]=f.toISOString().split("T")[0];}break}case h.TIMESTAMP:case h.TIMESTAMP_TZ:{let a=t>>2;for(let i=0;i<n;i++)if(c&&d.HEAPU8[e+i])s[i]=null;else {let u=d.HEAPU32[a+i*2],f=d.HEAP32[a+i*2+1],m=BigInt(f)*BigInt(4294967296)+BigInt(u>>>0),b=Number(m/BigInt(1e3)),p=new Date(b);s[i]=p.toISOString();}break}default:for(let a=0;a<n;a++)s[a]=null;break}return s}async queryArrow(t){if(this.closed||!d)throw new y("Connection is closed");let e=d._malloc(64);try{if(await d.ccall("duckdb_query","number",["number","string","number"],[this.connPtr,t,e],{async:!0})!==0){let u=d.ccall("duckdb_result_error","number",["number"],[e]),f=u?d.UTF8ToString(u):"Query failed";throw d.ccall("duckdb_destroy_result",null,["number"],[e]),new y(f,void 0,t)}let o=d.ccall("duckdb_column_count","number",["number"],[e]),s=d.ccall("duckdb_row_count","number",["number"],[e]),c=[];for(let u=0;u<o;u++){let f=d.ccall("duckdb_column_name","number",["number","number"],[e,u]),m=d.ccall("duckdb_column_type","number",["number","number"],[e,u]);c.push({name:d.UTF8ToString(f),type:m,flechetteType:this.getFlechetteType(m)||$()});}let a={},i={};for(let u=0;u<o;u++){let f=c[u],m=d.ccall("duckdb_column_data","number",["number","number"],[e,u]),b=d.ccall("duckdb_nullmask_data","number",["number","number"],[e,u]);if(f.type===h.VARCHAR||m===0){let p=new Array(s);for(let g=0;g<s;g++)if(d.ccall("duckdb_value_is_null","number",["number","number","number","number","number"],[e,u,0,g,0]))p[g]=null;else {let k=d.ccall("duckdb_value_varchar","number",["number","number","number","number","number"],[e,u,0,g,0]);k?(p[g]=d.UTF8ToString(k),d._free(k)):p[g]=null;}a[f.name]=p;}else a[f.name]=this.readColumnData(m,b,s,f.type);i[f.name]=f.flechetteType;}return d.ccall("duckdb_destroy_result",null,["number"],[e]),ae(a,{types:i})}finally{d._free(e);}}getFlechetteType(t){switch(t){case h.BOOLEAN:return Mt();case h.TINYINT:return it();case h.SMALLINT:return ct();case h.INTEGER:return G();case h.BIGINT:return at();case h.UTINYINT:return Dt();case h.USMALLINT:return Ot();case h.UINTEGER:return Bt();case h.UBIGINT:return Ut();case h.FLOAT:return Pt();case h.DOUBLE:return rt();case h.VARCHAR:return $();case h.DATE:return Nt();case h.TIMESTAMP:case h.TIMESTAMP_TZ:return ut(w.MICROSECOND);default:return $()}}async execute(t){if(this.closed||!d)throw new y("Connection is closed");let e=d._malloc(64);try{if(await d.ccall("duckdb_query","number",["number","string","number"],[this.connPtr,t,e],{async:!0})!==0){let s=d.ccall("duckdb_result_error","number",["number"],[e]),c=s?d.UTF8ToString(s):"Query failed";throw d.ccall("duckdb_destroy_result",null,["number"],[e]),new y(c,void 0,t)}let o=d.ccall("duckdb_rows_changed","number",["number"],[e]);return d.ccall("duckdb_destroy_result",null,["number"],[e]),o}finally{d._free(e);}}prepare(t){if(this.closed||!d)throw new y("Connection is closed");let e=d._malloc(4);try{if(d.ccall("duckdb_prepare","number",["number","string","number"],[this.connPtr,t,e])!==0){let s=d.getValue(e,"*");if(s){let c=d.ccall("duckdb_prepare_error","number",["number"],[s]),a=c?d.UTF8ToString(c):"Failed to prepare statement";throw d.ccall("duckdb_destroy_prepare",null,["number"],[s]),new y(a,void 0,t)}throw new y("Failed to prepare statement",void 0,t)}let o=d.getValue(e,"*");return new ln(o,this.connPtr,t)}finally{d._free(e);}}async beginTransaction(){await this.execute("BEGIN TRANSACTION");}async commit(){await this.execute("COMMIT");}async rollback(){await this.execute("ROLLBACK");}async transaction(t){await this.beginTransaction();try{let e=await t();return await this.commit(),e}catch(e){throw await this.rollback(),e}}close(){if(this.closed||!d)return;let t=d._malloc(4);try{d.setValue(t,this.connPtr,"*"),d.ccall("duckdb_disconnect",null,["number"],[t]);}finally{d._free(t);}this.closed=true,this.connPtr=0;}};
|
|
2
|
+
var ht=Uint8Array.of(65,82,82,79,87,49),mn=Uint8Array.of(255,255,255,255,0,0,0,0),U={V1:0,V4:3,V5:4},Or={Little:0},P={NONE:0,Schema:1,DictionaryBatch:2,RecordBatch:3,Tensor:4,SparseTensor:5},l={Dictionary:-1,NONE:0,Null:1,Int:2,Float:3,Binary:4,Utf8:5,Bool:6,Decimal:7,Date:8,Time:9,Timestamp:10,Interval:11,List:12,Struct:13,Union:14,FixedSizeBinary:15,FixedSizeList:16,Map:17,Duration:18,LargeBinary:19,LargeUtf8:20,LargeList:21,RunEndEncoded:22,BinaryView:23,Utf8View:24,ListView:25,LargeListView:26},tt={HALF:0,SINGLE:1,DOUBLE:2},H={DAY:0,MILLISECOND:1},w={SECOND:0,MILLISECOND:1,MICROSECOND:2,NANOSECOND:3},v={YEAR_MONTH:0,DAY_TIME:1,MONTH_DAY_NANO:2},j={Sparse:0,Dense:1},pt={LZ4_FRAME:0,ZSTD:1},st={BUFFER:0};var q=Uint8Array,jt=Uint16Array,zt=Uint32Array,Et=BigUint64Array,bt=Int8Array,Br=Int16Array,M=Int32Array,N=BigInt64Array,ue=Float32Array,et=Float64Array;function hn(r){return r instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&r instanceof SharedArrayBuffer}function pn(r,t){let e=Math.log2(r)-3;return (t?[bt,Br,M,N]:[q,jt,zt,Et])[e]}var ds=Object.getPrototypeOf(Int8Array);function Ur(r){return r instanceof ds}function le(r){return Array.isArray(r)||Ur(r)}function _t(r){return r===N||r===Et}function kt(r,t){let e=0,n=r.length;if(n<=2147483648)do{let o=e+n>>>1;r[o]<=t?e=o+1:n=o;}while(e<n);else do{let o=Math.trunc((e+n)/2);r[o]<=t?e=o+1:n=o;}while(e<n);return e}function ms(r,t=1){return (r*t+7&-8)/t}function bn(r,t=r.length){let e=ms(t,r.BYTES_PER_ELEMENT);return r.length>e?r.subarray(0,e):r.length<e?yn(r,e):r}function yn(r,t,e=0){let n=new r.constructor(t);return n.set(r,e),n}function fe(r,t,e){for(;r.length<=t;)r=yn(r,r.length<<1,e?r.length:0);return r}function gn(r){return r instanceof Date}function In(r){return typeof r[Symbol.iterator]=="function"}function de(r,t,e){if(t(r))return r;throw new Error(e(r))}function F(r,t,e){return t=Array.isArray(t)?t:Object.values(t),de(r,n=>t.includes(n),e??(()=>`${r} must be one of ${t}`))}function St(r,t){for(let[e,n]of Object.entries(r))if(n===t)return e;return "<Unknown>"}var C=r=>`Unsupported data type: "${St(l,r)}" (id ${r})`,yt=(r,t,e=true,n=null)=>({name:r,type:t,nullable:e,metadata:n});function wn(r){return Object.hasOwn(r,"name")&&xn(r.type)}function xn(r){return typeof r?.typeId=="number"}function ot(r,t="",e=true){return wn(r)?r:yt(t,de(r,xn,()=>"Data type expected."),e)}var An=r=>({typeId:r}),Gt=(r,t,e=false,n=-1)=>({typeId:l.Dictionary,id:n,dictionary:r,indices:t||G(),ordered:e}),Pr=()=>An(l.Null),z=(r=32,t=true)=>({typeId:l.Int,bitWidth:F(r,[8,16,32,64]),signed:t,values:pn(r,t)}),it=()=>z(8),ct=()=>z(16),G=()=>z(32),at=()=>z(64),Dt=()=>z(8,false),Ot=()=>z(16,false),Bt=()=>z(32,false),Ut=()=>z(64,false),$t=(r=2)=>({typeId:l.Float,precision:F(r,tt),values:[jt,ue,et][r]});var Pt=()=>$t(tt.SINGLE),rt=()=>$t(tt.DOUBLE),Mr=()=>({typeId:l.Binary,offsets:M}),$=()=>({typeId:l.Utf8,offsets:M}),Mt=()=>An(l.Bool),vr=(r,t,e=128)=>({typeId:l.Decimal,precision:r,scale:t,bitWidth:F(e,[32,64,128,256]),values:e===32?M:Et});var me=r=>({typeId:l.Date,unit:F(r,H),values:r===H.DAY?M:N}),vt=()=>me(H.DAY);var Nr=(r=w.MILLISECOND)=>{r=F(r,w);let t=r===w.SECOND||r===w.MILLISECOND?32:64;return {typeId:l.Time,unit:r,bitWidth:t,values:t===32?M:N}};var ut=(r=w.MILLISECOND,t=null)=>({typeId:l.Timestamp,unit:F(r,w),timezone:t,values:N}),Fr=(r=v.MONTH_DAY_NANO)=>({typeId:l.Interval,unit:F(r,v),values:r===v.MONTH_DAY_NANO?void 0:M}),Yt=r=>({typeId:l.List,children:[ot(r)],offsets:M}),Wt=r=>({typeId:l.Struct,children:Array.isArray(r)&&r.length>0&&wn(r[0])?r:Object.entries(r).map(([t,e])=>yt(t,e))}),Lr=(r,t,e,n)=>(e??=t.map((o,s)=>s),{typeId:l.Union,mode:F(r,j),typeIds:e,typeMap:e.reduce((o,s,c)=>(o[s]=c,o),{}),children:t.map((o,s)=>ot(o,`_${s}`)),typeIdForValue:n,offsets:M}),Cr=r=>({typeId:l.FixedSizeBinary,stride:r}),Zt=(r,t)=>({typeId:l.FixedSizeList,stride:t,children:[ot(r)]}),Tn=(r,t)=>({typeId:l.Map,keysSorted:r,children:[t],offsets:M});var Vr=(r=w.MILLISECOND)=>({typeId:l.Duration,unit:F(r,w),values:N}),Rr=()=>({typeId:l.LargeBinary,offsets:N}),Hr=()=>({typeId:l.LargeUtf8,offsets:N}),jr=r=>({typeId:l.LargeList,children:[ot(r)],offsets:N}),zr=(r,t)=>({typeId:l.RunEndEncoded,children:[de(ot(r,"run_ends"),e=>e.type.typeId===l.Int,()=>"Run-ends must have an integer type."),ot(t,"values")]});var Gr=r=>({typeId:l.ListView,children:[ot(r,"value")],offsets:M}),$r=r=>({typeId:l.LargeListView,children:[ot(r,"value")],offsets:N});var _n=new et(2),he=_n.buffer,hs=new N(he),Nt=new zt(he),En=new M(he),ps=new q(he);function bs(r){return r}function Y(r){return BigInt(r)}function pe(r){return _t(r)?Y:bs}function kn(r){return r/864e5|0}function Sn(r){return r===w.SECOND?t=>Y(t/1e3):r===w.MILLISECOND?Y:r===w.MICROSECOND?t=>Y(t*1e3):t=>Y(t*1e6)}function Dn([r,t,e]){return En[0]=r,En[1]=t,hs[1]=Y(e),ps}function X(r){if(r>Number.MAX_SAFE_INTEGER||r<Number.MIN_SAFE_INTEGER)throw Error(`BigInt exceeds integer number representation: ${r}`);return Number(r)}function be(r,t){return Number(r/t)+Number(r%t)/Number(t)}function On(r){return t=>typeof t=="bigint"?Number(t):Math.trunc(t*r)}function Bn(r,t,e,n,o){let s=typeof r=="bigint"?r:Y(Math.trunc(r*o));t[e]=s,n>1&&(t[e+1]=s>>64n,n>2&&(t[e+2]=s>>128n,t[e+3]=s>>192n));}var Ft=r=>BigInt.asUintN(64,r);function Un(r,t){return BigInt.asIntN(64,r[t])}function Pn(r,t){let e=t<<1,n;return BigInt.asIntN(64,r[e+1])<0?(n=Ft(~r[e])|Ft(~r[e+1])<<64n,n=-(n+1n)):n=r[e]|r[e+1]<<64n,n}function Mn(r,t){let e=t<<2,n;return BigInt.asIntN(64,r[e+3])<0?(n=Ft(~r[e])|Ft(~r[e+1])<<64n|Ft(~r[e+2])<<128n|Ft(~r[e+3])<<192n,n=-(n+1n)):n=r[e]|r[e+1]<<64n|r[e+2]<<128n|r[e+3]<<192n,n}function vn(r){if(r!==r)return 32256;_n[0]=r;let t=(Nt[1]&2147483648)>>16&65535,e=Nt[1]&2146435072,n=0;return e>=1089470464?Nt[0]>0?e=31744:(e=(e&2080374784)>>16,n=(Nt[1]&1048575)>>10):e<=1056964608?(n=1048576+(Nt[1]&1048575),n=1048576+(n<<(e>>20)-998)>>21,e=0):(e=e-1056964608>>10,n=(Nt[1]&1048575)+512>>10),t|e|n&65535}var ys=new TextDecoder("utf-8"),gs=new TextEncoder;function Lt(r){return ys.decode(r)}function ye(r){return gs.encode(r)}function Ct(r){return `${typeof r!="object"||!r?r??null:gn(r)?+r:le(r)?`[${r.map(Ct)}]`:Is(r)}`}function Is(r){let t="",e=-1;for(let n in r)++e>0&&(t+=","),t+=`"${n}":${Ct(r[n])}`;return `{${t}}`}var V=4,ge=2;function Yr(r,t){return (r[t>>3]&1<<t%8)!==0}function B(r,t){let e=t+x(r,t),n=e-x(r,e),o=S(r,n);return (s,c,a=null)=>{if(s<o){let i=S(r,n+s);if(i)return c(r,e+i)}return a}}function Q(r,t){return t}function lt(r,t){return !!Ie(r,t)}function Ie(r,t){return qt(r,t)<<24>>24}function qt(r,t){return r[t]}function S(r,t){return ws(r,t)<<16>>16}function ws(r,t){return r[t]|r[t+1]<<8}function x(r,t){return r[t]|r[t+1]<<8|r[t+2]<<16|r[t+3]<<24}function Nn(r,t){return x(r,t)>>>0}function D(r,t){return X(BigInt.asIntN(64,BigInt(Nn(r,t))+(BigInt(Nn(r,t+V))<<32n)))}function gt(r,t){let e=t+x(r,t),n=x(r,e);return e+=V,Lt(r.subarray(e,e+n))}function L(r,t,e,n){if(!t)return [];let o=t+x(r,t);return Array.from({length:x(r,o)},(s,c)=>n(r,o+V+c*e))}var we=Symbol("rowIndex");function Xt(r,t){class e{constructor(s){this[we]=s;}toJSON(){return Fn(r,t,this[we])}}let n=e.prototype;for(let o=0;o<r.length;++o){if(Object.hasOwn(n,r[o]))continue;let s=t[o];Object.defineProperty(n,r[o],{get(){return s.at(this[we])},enumerable:true});}return o=>new e(o)}function xe(r,t){return e=>Fn(r,t,e)}function Fn(r,t,e){let n={};for(let o=0;o<r.length;++o)n[r[o]]=t[o].at(e);return n}function Ln(r){return r instanceof K}var It=class{static ArrayType=null;constructor({length:t,nullCount:e,type:n,validity:o,values:s,offsets:c,sizes:a,children:i}){this.length=t,this.nullCount=e,this.type=n,this.validity=o,this.values=s,this.offsets=c,this.sizes=a,this.children=i,(!e||!this.validity)&&(this.at=u=>this.value(u));}get[Symbol.toStringTag](){return "Batch"}at(t){return this.isValid(t)?this.value(t):null}isValid(t){return Yr(this.validity,t)}value(t){return this.values[t]}slice(t,e){let n=e-t,o=Array(n);for(let s=0;s<n;++s)o[s]=this.at(t+s);return o}*[Symbol.iterator](){for(let t=0;t<this.length;++t)yield this.at(t);}},K=class extends It{constructor(t){super(t);let{length:e,values:n}=this;this.values=n.subarray(0,e);}slice(t,e){return this.nullCount?super.slice(t,e):this.values.subarray(t,e)}[Symbol.iterator](){return this.nullCount?super[Symbol.iterator]():this.values[Symbol.iterator]()}},Vt=class extends It{static ArrayType=et},T=class extends It{static ArrayType=Array},Rt=class extends T{value(t){return null}},W=class extends Vt{value(t){return X(this.values[t])}},Ae=class extends Vt{value(t){let e=this.values[t],n=(e&31744)>>10,o=(e&1023)/1024,s=(-1)**((e&32768)>>15);switch(n){case 31:return s*(o?Number.NaN:1/0);case 0:return s*(o?6103515625e-14*o:0)}return s*2**(n-15)*(1+o)}},Te=class extends T{value(t){return Yr(this.values,t)}},Ee=class extends Vt{constructor(t){super(t);let{scale:e}=this.type;this.scale=10**e;}value(t){return this.values[t]/this.scale}},_e=class extends It{constructor(t){super(t);let{bitWidth:e,scale:n}=this.type;this.decimal=e===64?Un:e===128?Pn:Mn,this.scale=10n**BigInt(n);}},ke=class extends _e{static ArrayType=et;value(t){return be(this.decimal(this.values,t),this.scale)}},Se=class extends _e{static ArrayType=Array;value(t){return this.decimal(this.values,t)}},Qt=class extends T{constructor(t){super(t),this.source=t;}value(t){return new Date(this.source.value(t))}},De=class extends Vt{value(t){return 864e5*this.values[t]}},Cn=W,Oe=class extends W{value(t){return super.value(t)*1e3}},Vn=W,Be=class extends W{value(t){return be(this.values[t],1000n)}},Ue=class extends W{value(t){return be(this.values[t],1000000n)}},Pe=class extends T{value(t){return this.values.subarray(t<<1,t+1<<1)}},Me=class extends T{value(t){let e=this.values,n=t<<4;return Float64Array.of(x(e,n),x(e,n+4),D(e,n+8))}},Rn=({values:r,offsets:t},e)=>r.subarray(t[e],t[e+1]),Hn=({values:r,offsets:t},e)=>r.subarray(X(t[e]),X(t[e+1])),ve=class extends T{value(t){return Rn(this,t)}},Ne=class extends T{value(t){return Hn(this,t)}},Fe=class extends T{value(t){return Lt(Rn(this,t))}},Le=class extends T{value(t){return Lt(Hn(this,t))}},Ce=class extends T{value(t){let e=this.offsets;return this.children[0].slice(e[t],e[t+1])}},Ve=class extends T{value(t){let e=this.offsets;return this.children[0].slice(X(e[t]),X(e[t+1]))}},Re=class extends T{value(t){let e=this.offsets[t],n=e+this.sizes[t];return this.children[0].slice(e,n)}},He=class extends T{value(t){let e=this.offsets[t],n=e+this.sizes[t];return this.children[0].slice(X(e),X(n))}},je=class extends T{constructor(t){super(t),this.stride=this.type.stride;}},ze=class extends je{value(t){let{stride:e,values:n}=this;return n.subarray(t*e,(t+1)*e)}},Ge=class extends je{value(t){let{children:e,stride:n}=this;return e[0].slice(t*n,(t+1)*n)}};function jn({children:r,offsets:t},e){let[n,o]=r[0].children,s=t[e],c=t[e+1],a=[];for(let i=s;i<c;++i)a.push([n.at(i),o.at(i)]);return a}var $e=class extends T{value(t){return jn(this,t)}},Ye=class extends T{value(t){return new Map(jn(this,t))}},Kt=class extends T{constructor({typeIds:t,...e}){super(e),this.typeIds=t,this.typeMap=this.type.typeMap;}value(t,e=t){let{typeIds:n,children:o,typeMap:s}=this;return o[s[n[t]]].at(e)}},We=class extends Kt{value(t){return super.value(t,this.offsets[t])}},Jt=class extends T{constructor(t,e=xe){super(t),this.names=this.type.children.map(n=>n.name),this.factory=e(this.names,this.children);}value(t){return this.factory(t)}},Ze=class extends Jt{constructor(t){super(t,Xt);}},qe=class extends T{value(t){let[{values:e},n]=this.children;return n.at(kt(e,t))}},Xe=class extends T{setDictionary(t){return this.dictionary=t,this.cache=t.cache(),this}value(t){return this.cache[this.key(t)]}key(t){return this.values[t]}},Qe=class extends T{constructor({data:t,...e}){super(e),this.data=t;}view(t){let{values:e,data:n}=this,o=t<<4,s=o+4,c=e,a=x(c,o);return a>12&&(s=x(c,o+12),c=n[x(c,o+8)]),c.subarray(s,s+a)}},Ke=class extends Qe{value(t){return this.view(t)}},Je=class extends Qe{value(t){return Lt(this.view(t))}};function Wr(r){let t=[];return {add(e){return t.push(e),this},clear:()=>t=[],done:()=>new J(t,r)}}var J=class{constructor(t,e=t[0]?.type){this.type=e,this.length=t.reduce((s,c)=>s+c.length,0),this.nullCount=t.reduce((s,c)=>s+c.nullCount,0),this.data=t;let n=t.length,o=new Int32Array(n+1);if(n===1){let[s]=t;o[1]=s.length,this.at=c=>s.at(c);}else for(let s=0,c=0;s<n;++s)o[s+1]=c+=t[s].length;this.offsets=o;}get[Symbol.toStringTag](){return "Column"}[Symbol.iterator](){let t=this.data;return t.length===1?t[0][Symbol.iterator]():xs(t)}at(t){let{data:e,offsets:n}=this,o=kt(n,t)-1;return e[o]?.at(t-n[o])}get(t){return this.at(t)}toArray(){let{length:t,nullCount:e,data:n}=this,o=!e&&Ln(n[0]),s=n.length;if(o&&s===1)return n[0].values;let c=!s||e>0?Array:n[0].constructor.ArrayType??n[0].values.constructor,a=new c(t);return o?As(a,n):Ts(a,n)}cache(){return this._cache??(this._cache=this.toArray())}};function*xs(r){for(let t=0;t<r.length;++t){let e=r[t][Symbol.iterator]();for(let n=e.next();!n.done;n=e.next())yield n.value;}}function As(r,t){for(let e=0,n=0;e<t.length;++e){let{values:o}=t[e];r.set(o,n),n+=o.length;}return r}function Ts(r,t){let e=-1;for(let n=0;n<t.length;++n){let o=t[n];for(let s=0;s<o.length;++s)r[++e]=o.at(s);}return r}var wt=class r{constructor(t,e,n=false){let o=t.fields.map(c=>c.name);this.schema=t,this.names=o,this.children=e,this.factory=n?Xt:xe;let s=[];this.getFactory=c=>s[c]??(s[c]=this.factory(o,e.map(a=>a.data[c])));}get[Symbol.toStringTag](){return "Table"}get numCols(){return this.names.length}get numRows(){return this.children[0]?.length??0}getChildAt(t){return this.children[t]}getChild(t){let e=this.names.findIndex(n=>n===t);return e>-1?this.children[e]:void 0}selectAt(t,e=[]){let{children:n,factory:o,schema:s}=this,{fields:c}=s;return new r({...s,fields:t.map((a,i)=>Es(c[a],e[i]))},t.map(a=>n[a]),o===Xt)}select(t,e){let n=this.names,o=t.map(s=>n.indexOf(s));return this.selectAt(o,e)}toColumns(){let{children:t,names:e}=this,n={};return e.forEach((o,s)=>n[o]=t[s]?.toArray()??[]),n}toArray(){let{children:t,getFactory:e,numRows:n}=this,o=t[0]?.data??[],s=Array(n);for(let c=0,a=-1;c<o.length;++c){let i=e(c);for(let u=0;u<o[c].length;++u)s[++a]=i(u);}return s}*[Symbol.iterator](){let{children:t,getFactory:e}=this,n=t[0]?.data??[];for(let o=0;o<n.length;++o){let s=e(o);for(let c=0;c<n[o].length;++c)yield s(c);}}at(t){let{children:e,getFactory:n,numRows:o}=this;if(t<0||t>=o)return null;let[{offsets:s}]=e,c=kt(s,t)-1;return n(c)(t-s[c])}get(t){return this.at(t)}};function Es(r,t){return t!=null&&t!==r.name?{...r,name:t}:r}function xt(r,t={}){let{typeId:e,bitWidth:n,mode:o,precision:s,unit:c}=r,{useBigInt:a,useDate:i,useDecimalInt:u,useMap:f,useProxy:m}=t;switch(e){case l.Null:return Rt;case l.Bool:return Te;case l.Int:case l.Time:case l.Duration:return a||n<64?K:W;case l.Float:return s?K:Ae;case l.Date:return zn(c===H.DAY?De:Cn,i&&Qt);case l.Timestamp:return zn(c===w.SECOND?Oe:c===w.MILLISECOND?Vn:c===w.MICROSECOND?Be:Ue,i&&Qt);case l.Decimal:return n===32?u?K:Ee:u?Se:ke;case l.Interval:return c===v.DAY_TIME?Pe:c===v.YEAR_MONTH?K:Me;case l.FixedSizeBinary:return ze;case l.Utf8:return Fe;case l.LargeUtf8:return Le;case l.Binary:return ve;case l.LargeBinary:return Ne;case l.BinaryView:return Ke;case l.Utf8View:return Je;case l.List:return Ce;case l.LargeList:return Ve;case l.Map:return f?Ye:$e;case l.ListView:return Re;case l.LargeListView:return He;case l.FixedSizeList:return Ge;case l.Struct:return m?Ze:Jt;case l.RunEndEncoded:return qe;case l.Dictionary:return Xe;case l.Union:return o?We:Kt}throw new Error(C(e))}function zn(r,t){return t?class extends t{constructor(n){super(new r(n));}}:r}function te(r,t,e){r[t]=e,r[t+1]=e>>8,r[t+2]=e>>16,r[t+3]=e>>24;}function Zr(r,t,e){let n=BigInt(e);te(r,t+4,Number(BigInt.asIntN(32,n>>BigInt(32)))),te(r,t+0,Number(BigInt.asIntN(32,n)));}var tr=1024,er=class{constructor(t){this.sink=t,this.minalign=1,this.buf=new Uint8Array(tr),this.space=tr,this.vtables=[],this.outputBytes=0;}offset(){return this.buf.length-this.space}writeInt8(t){this.buf[this.space-=1]=t;}writeInt16(t){this.buf[this.space-=2]=t,this.buf[this.space+1]=t>>8;}writeInt32(t){te(this.buf,this.space-=4,t);}writeInt64(t){Zr(this.buf,this.space-=8,t);}addInt8(t){nt(this,1,0),this.writeInt8(t);}addInt16(t){nt(this,2,0),this.writeInt16(t);}addInt32(t){nt(this,4,0),this.writeInt32(t);}addInt64(t){nt(this,8,0),this.writeInt64(t);}addOffset(t){nt(this,V,0),this.writeInt32(this.offset()-t+V);}addObject(t,e){let n=_s(this,t);return e?.(n),n.finish()}addVector(t,e,n,o){let s=t?.length;if(!s)return 0;nt(this,V,e*s),nt(this,n,e*s);for(let c=s;--c>=0;)o(this,t[c]);return this.writeInt32(s),this.offset()}addOffsetVector(t){return this.addVector(t,4,4,(e,n)=>e.addOffset(n))}addString(t){if(t==null)return 0;let e=ye(t),n=e.length;return this.addInt8(0),nt(this,V,n),this.buf.set(e,this.space-=n),this.writeInt32(n),this.offset()}finish(t){nt(this,this.minalign,V),this.addOffset(t);}flush(){let{buf:t,sink:e}=this,n=t.subarray(this.space,t.length);e.write(n),this.outputBytes+=n.byteLength,this.minalign=1,this.vtables=[],this.buf=new Uint8Array(tr),this.space=tr;}addBuffer(t){let e=t.byteLength;if(!e)return 0;this.sink.write(t),this.outputBytes+=e;let n=(e+7&-8)-e;return this.addPadding(n),e+n}addPadding(t){t>0&&(this.sink.write(new Uint8Array(t)),this.outputBytes+=t);}};function nt(r,t,e){let{buf:n,space:o,minalign:s}=r;t>s&&(r.minalign=t);let c=n.length,a=c-o+e,i=~a+1&t-1;n=fe(n,a+i+t-1,true),o+=n.length-c;for(let u=0;u<i;++u)n[--o]=0;r.buf=n,r.space=o;}function _s(r,t){let e=Array(t).fill(0),n=r.offset();function o(s){e[s]=r.offset();}return {addInt8(s,c,a){c!=a&&(r.addInt8(c),o(s));},addInt16(s,c,a){c!=a&&(r.addInt16(c),o(s));},addInt32(s,c,a){c!=a&&(r.addInt32(c),o(s));},addInt64(s,c,a){c!=a&&(r.addInt64(c),o(s));},addOffset(s,c,a){c!=a&&(r.addOffset(c),o(s));},finish(){r.addInt32(0);let s=r.offset(),c=t;for(;--c>=0&&e[c]===0;);let a=c+1;for(;c>=0;--c)r.addInt16(e[c]?s-e[c]:0);let i=2;r.addInt16(s-n);let u=(a+i)*ge;r.addInt16(u);let f=0,{buf:m,vtables:b,space:p}=r;t:for(c=0;c<b.length;++c){let g=m.length-b[c];if(u==S(m,g)){for(let A=ge;A<u;A+=ge)if(S(m,p+A)!=S(m,g+A))continue t;f=b[c];break}}if(f)r.space=m.length-s,te(m,r.space,f-s);else {let g=r.offset();b.push(g),te(m,m.length-s,g-s);}return s}}}var Gn=-1,qr=8;function rr(r){return `Missing compression codec "${St(pt,r)}" (id ${r})`}var ks=new Map;function ee(r){return r!=null&&ks.get(r)||null}function $n(r,{offset:t,length:e},n){if(e===0)return {bytes:new Uint8Array(0),offset:0,length:0};let o=D(r,t),s=r.subarray(t+qr,t+e),c=o===Gn?s:n.decode(s);return {bytes:c,offset:0,length:c.length}}function Yn(r,t){let e=t.encode(r),n=e.length<r.length,o=n?e:r,s=new Uint8Array(qr+o.length);return Zr(s,0,n?r.length:Gn),s.set(o,qr),s}function Ss(r,t){return {offset:D(r,t),metadataLength:x(r,t+8),bodyLength:D(r,t+16)}}function Xr(r,t){return L(r,t,24,Ss)}function Wn(r,t){let e=B(r,t);return {codec:e(4,Ie,pt.LZ4_FRAME),method:e(6,Ie,st.BUFFER)}}function nr(r,t,e){let n=B(r,t),o=e<U.V4?8:0;return {length:n(4,D,0),nodes:L(r,n(6,Q),16,(s,c)=>({length:D(s,c),nullCount:D(s,c+8)})),regions:L(r,n(8,Q),16+o,(s,c)=>({offset:D(s,c+o),length:D(s,c+o+8)})),compression:n(10,Wn),variadic:L(r,n(12,Q),8,D)}}function Zn(r,t,e){let n=B(r,t);return {id:n(4,D,0),data:n(6,(o,s)=>nr(o,s,e)),isDelta:n(8,lt,false)}}function Qr(r,t,e,n){F(e,l,C);let o=B(r,t);switch(e){case l.Binary:return Mr();case l.Utf8:return $();case l.LargeBinary:return Rr();case l.LargeUtf8:return Hr();case l.List:return Yt(n[0]);case l.ListView:return Gr(n[0]);case l.LargeList:return jr(n[0]);case l.LargeListView:return $r(n[0]);case l.Struct:return Wt(n);case l.RunEndEncoded:return zr(n[0],n[1]);case l.Int:return z(o(4,x,0),o(6,lt,false));case l.Float:return $t(o(4,S,tt.HALF));case l.Decimal:return vr(o(4,x,0),o(6,x,0),o(8,x,128));case l.Date:return me(o(4,S,H.MILLISECOND));case l.Time:return Nr(o(4,S,w.MILLISECOND));case l.Timestamp:return ut(o(4,S,w.SECOND),o(6,gt));case l.Interval:return Fr(o(4,S,v.YEAR_MONTH));case l.Duration:return Vr(o(4,S,w.MILLISECOND));case l.FixedSizeBinary:return Cr(o(4,x,0));case l.FixedSizeList:return Zt(n[0],o(4,x,0));case l.Map:return Tn(o(4,lt,false),n[0]);case l.Union:return Lr(o(4,S,j.Sparse),n,L(r,o(6,Q),4,x))}return {typeId:e}}function re(r,t){let e=L(r,t,4,(n,o)=>{let s=B(n,o);return [s(4,gt),s(6,gt)]});return e.length?new Map(e):null}function sr(r,t,e){let n=B(r,t);return {version:e,endianness:n(4,S,0),fields:n(6,Ds,[]),metadata:n(8,re)}}function Ds(r,t){return L(r,t,4,qn)}function qn(r,t){let e=B(r,t),n=e(8,qt,l.NONE),o=e(10,Q,0),s=e(12,Bs),c=e(14,Os,[]),a=Qr(r,o,n,c);return s&&(s.dictionary=a,a=s),{name:e(4,gt),type:a,nullable:e(6,lt,false),metadata:e(16,re)}}function Os(r,t){return L(r,t,4,qn)}function Bs(r,t){if(!t)return null;let e=B(r,t);return Gt(null,e(6,Us,G()),e(8,lt,false),e(4,D,0))}function Us(r,t){return Qr(r,t,l.Int)}var Ps=(r,t)=>`Expected to read ${r} metadata bytes, but only read ${t}.`,Ms=(r,t)=>`Expected to read ${r} bytes for message body, but only read ${t}.`,vs=r=>`Unsupported message type: ${r} (${St(P,r)})`;function or(r,t){let e=x(r,t)||0;if(t+=V,e===-1&&(e=x(r,t)||0,t+=V),e===0)return null;let n=r.subarray(t,t+=e);if(n.byteLength<e)throw new Error(Ps(e,n.byteLength));let o=B(n,0),s=o(4,S,U.V1),c=o(6,qt,P.NONE),a=o(8,Q,0),i=o(10,D,0),u;if(a){let f=c===P.Schema?sr:c===P.DictionaryBatch?Zn:c===P.RecordBatch?nr:null;if(!f)throw new Error(vs(c));if(u=f(n,a,s),i>0){let m=r.subarray(t,t+=i);if(m.byteLength<i)throw new Error(Ms(i,m.byteLength));u.body=m;}else c!==P.Schema&&(u.body=new Uint8Array(0));}return {version:s,type:c,index:t,content:u}}function Xn(r){let t=hn(r)?new Uint8Array(r):r;return t instanceof Uint8Array&&Ns(t)?Ls(t):Fs(t)}function Ns(r){if(!r||r.length<4)return false;for(let t=0;t<6;++t)if(ht[t]!==r[t])return false;return true}function Fs(r){let t=[r].flat(),e,n=[],o=[];for(let s of t){if(!(s instanceof Uint8Array))throw new Error("IPC data batch was not a Uint8Array.");let c=0;for(;;){let a=or(s,c);if(a===null)break;if(c=a.index,!!a.content)switch(a.type){case P.Schema:e||(e=a.content);break;case P.RecordBatch:n.push(a.content);break;case P.DictionaryBatch:o.push(a.content);break}}}return {schema:e,dictionaries:o,records:n,metadata:null}}function Ls(r){let t=r.byteLength-(ht.length+4),e=x(r,t),n=B(r,t-e),o=n(4,S,U.V1),s=n(8,Xr,[]),c=n(10,Xr,[]);return {schema:n(6,(a,i)=>sr(a,i,o)),dictionaries:s.map(({offset:a})=>or(r,a).content),records:c.map(({offset:a})=>or(r,a).content),metadata:n(12,re)}}function Qn(r,t){return Cs(Xn(r),t)}function Cs(r,t={}){let{schema:e={fields:[]},dictionaries:n,records:o}=r,{version:s,fields:c}=e,a=new Map,i=Rs(t,s,a),u=new Map;Vs(e,b=>{let p=b.type;p.typeId===l.Dictionary&&u.set(p.id,p.dictionary);});let f=new Map;for(let b of n){let{id:p,data:g,isDelta:A,body:k}=b,Z=u.get(p),Tt=Kr(Z,i({...g,body:k}));if(f.has(p)){let I=f.get(p);A||I.clear(),I.add(Tt);}else {if(A)throw new Error("Delta update can not be first dictionary batch.");f.set(p,Wr(Z).add(Tt));}}f.forEach((b,p)=>a.set(p,b.done()));let m=c.map(b=>Wr(b.type));for(let b of o){let p=i(b);c.forEach((g,A)=>m[A].add(Kr(g.type,p)));}return new wt(e,m.map(b=>b.done()),t.useProxy)}function Vs(r,t){r.fields.forEach(function e(n){t(n),n.type.dictionary?.children?.forEach(e),n.type.children?.forEach(e);});}function Rs(r,t,e){let n={version:t,options:r,dictionary:o=>e.get(o)};return o=>{let{length:s,nodes:c,regions:a,compression:i,variadic:u,body:f}=o,m=-1,b=-1,p=-1;return {...n,length:s,node:()=>c[++m],buffer:g=>{let{bytes:A,length:k,offset:Z}=Hs(f,a[++b],i);return g?new g(A.buffer,A.byteOffset+Z,k/g.BYTES_PER_ELEMENT):A.subarray(Z,Z+k)},variadic:()=>u[++p],visit(g){return g.map(A=>Kr(A.type,this))}}}}function Hs(r,t,e){if(e){if(e.method!==st.BUFFER)throw new Error(`Unknown compression method (${e.method})`);{let n=e.codec,o=ee(n);if(!o)throw new Error(rr(n));return $n(r,t,o)}}else return {bytes:r,...t}}function Kr(r,t){let{typeId:e}=r,{options:n,node:o,buffer:s,variadic:c,version:a}=t,i=xt(r,n),u={...o(),type:r};if(e===l.Null)return new i({...u,nullCount:u.length});switch(e){case l.Bool:case l.Int:case l.Time:case l.Duration:case l.Float:case l.Decimal:case l.Date:case l.Timestamp:case l.Interval:case l.FixedSizeBinary:return new i({...u,validity:s(),values:s(r.values)});case l.Utf8:case l.LargeUtf8:case l.Binary:case l.LargeBinary:return new i({...u,validity:s(),offsets:s(r.offsets),values:s()});case l.BinaryView:case l.Utf8View:return new i({...u,validity:s(),values:s(),data:Array.from({length:c()},()=>s())});case l.List:case l.LargeList:case l.Map:return new i({...u,validity:s(),offsets:s(r.offsets),children:t.visit(r.children)});case l.ListView:case l.LargeListView:return new i({...u,validity:s(),offsets:s(r.offsets),sizes:s(r.offsets),children:t.visit(r.children)});case l.FixedSizeList:case l.Struct:return new i({...u,validity:s(),children:t.visit(r.children)});case l.RunEndEncoded:return new i({...u,children:t.visit(r.children)});case l.Dictionary:{let{id:f,indices:m}=r;return new i({...u,validity:s(),values:s(m.values)}).setDictionary(t.dictionary(f))}case l.Union:return a<U.V5&&s(),new i({...u,typeIds:s(bt),offsets:r.mode===j.Sparse?null:s(r.offsets),children:t.visit(r.children)});default:throw new Error(C(e))}}function ir(r,t,e){let{nodes:n,regions:o,variadic:s}=t,c=r.addVector(n,16,8,(u,f)=>(u.writeInt64(f.nullCount),u.writeInt64(f.length),u.offset())),a=r.addVector(o,16,8,(u,f)=>(u.writeInt64(f.length),u.writeInt64(f.offset),u.offset())),i=r.addVector(s,8,8,(u,f)=>u.addInt64(f));return r.addObject(5,u=>{u.addInt64(0,n[0].length,0),u.addOffset(1,c,0),u.addOffset(2,a,0),u.addOffset(3,js(r,e),0),u.addOffset(4,i,0);})}function js(r,t){if(!t)return 0;let{codec:e,method:n}=t;return r.addObject(2,o=>{o.addInt8(0,e,pt.LZ4_FRAME),o.addInt8(1,n,st.BUFFER);})}function Kn(r,t,e){let n=ir(r,t.data,e);return r.addObject(3,o=>{o.addInt64(0,t.id,0),o.addOffset(1,n,0),o.addInt8(2,+t.isDelta,0);})}function ne(r,t){return t?.size>0?r.addOffsetVector(Array.from(t,([e,n])=>{let o=r.addString(`${e}`),s=r.addString(`${n}`);return r.addObject(2,c=>{c.addOffset(0,o,0),c.addOffset(1,s,0);})})):0}function se(r,t){switch(F(t.typeId,l,C)){case l.Dictionary:return to(r,t);case l.Int:return Zs(r,t);case l.Float:return Ws(r,t);case l.Decimal:return Gs(r,t);case l.Date:return zs(r,t);case l.Time:return Qs(r,t);case l.Timestamp:return Ks(r,t);case l.Interval:return qs(r,t);case l.Duration:return $s(r,t);case l.FixedSizeBinary:case l.FixedSizeList:return Ys(r,t);case l.Map:return Xs(r,t);case l.Union:return Js(r,t)}return r.addObject(0)}function zs(r,t){return r.addObject(1,e=>{e.addInt16(0,t.unit,H.MILLISECOND);})}function Gs(r,t){return r.addObject(3,e=>{e.addInt32(0,t.precision,0),e.addInt32(1,t.scale,0),e.addInt32(2,t.bitWidth,128);})}function $s(r,t){return r.addObject(1,e=>{e.addInt16(0,t.unit,w.MILLISECOND);})}function Ys(r,t){return r.addObject(1,e=>{e.addInt32(0,t.stride,0);})}function Ws(r,t){return r.addObject(1,e=>{e.addInt16(0,t.precision,tt.HALF);})}function Zs(r,t){return r.addObject(2,e=>{e.addInt32(0,t.bitWidth,0),e.addInt8(1,+t.signed,0);})}function qs(r,t){return r.addObject(1,e=>{e.addInt16(0,t.unit,v.YEAR_MONTH);})}function Xs(r,t){return r.addObject(1,e=>{e.addInt8(0,+t.keysSorted,0);})}function Qs(r,t){return r.addObject(2,e=>{e.addInt16(0,t.unit,w.MILLISECOND),e.addInt32(1,t.bitWidth,32);})}function Ks(r,t){let e=r.addString(t.timezone);return r.addObject(2,n=>{n.addInt16(0,t.unit,w.SECOND),n.addOffset(1,e,0);})}function Js(r,t){let e=r.addVector(t.typeIds,4,4,(n,o)=>n.addInt32(o));return r.addObject(2,n=>{n.addInt16(0,t.mode,j.Sparse),n.addOffset(1,e,0);})}function to(r,t){return r.addObject(4,e=>{e.addInt64(0,t.id,0),e.addOffset(1,se(r,t.indices),0),e.addInt8(2,+t.ordered,0);})}var eo=new Uint16Array(new Uint8Array([1,0]).buffer)[0]===1;function cr(r,t){let{fields:e,metadata:n}=t,o=e.map(a=>Jn(r,a)),s=r.addOffsetVector(o),c=ne(r,n);return r.addObject(4,a=>{a.addInt16(0,+!eo,0),a.addOffset(1,s,0),a.addOffset(2,c,0);})}function Jn(r,t){let{name:e,nullable:n,type:o,metadata:s}=t,{typeId:c}=o,a=0,i=0;if(c!==l.Dictionary)a=se(r,o);else {let p=o.dictionary;c=p.typeId,i=se(r,o),a=se(r,p);}let u=(o.children||[]).map(p=>Jn(r,p)),f=r.addOffsetVector(u),m=ne(r,s),b=r.addString(e);return r.addObject(7,p=>{p.addOffset(0,b,0),p.addInt8(1,+n,0),p.addInt8(2,c,l.NONE),p.addOffset(3,a,0),p.addOffset(4,i,0),p.addOffset(5,f,0),p.addOffset(6,m,0);})}function es(r,t,e,n,o){let s=ne(r,o),c=r.addVector(n,24,8,ts),a=r.addVector(e,24,8,ts),i=cr(r,t);r.finish(r.addObject(5,f=>{f.addInt16(0,U.V5,U.V1),f.addOffset(1,i,0),f.addOffset(2,a,0),f.addOffset(3,c,0),f.addOffset(4,s,0);}));let u=r.offset();r.addInt32(0),r.addInt32(-1),r.flush(),r.sink.write(new Uint8Array(Int32Array.of(u).buffer)),r.sink.write(ht);}function ts(r,{offset:t,metadataLength:e,bodyLength:n}){return r.writeInt64(n),r.writeInt32(0),r.writeInt32(e),r.writeInt64(t),r.offset()}function ar(r,t,e,n,o){r.finish(r.addObject(5,i=>{i.addInt16(0,U.V5,U.V1),i.addInt8(1,t,P.NONE),i.addOffset(2,e,0),i.addInt64(3,n,0);}));let s=8,c=r.offset(),a=c+s+7&-8;o?.push({offset:r.outputBytes,metadataLength:a,bodyLength:n}),r.addInt32(a-s),r.addInt32(-1),r.flush(),r.addPadding(a-c-s);}var Jr=class{write(t){}pad(t){this.write(new Uint8Array(t));}finish(){return null}},ur=class extends Jr{constructor(){super(),this.buffers=[];}write(t){this.buffers.push(t);}finish(){let t=this.buffers,e=t.reduce((o,s)=>o+s.byteLength,0),n=new Uint8Array(e);for(let o=0,s=0;o<t.length;++o)n.set(t[o],s),s+=t[o].byteLength;return n}};var rs="stream",ns="file";function os(r,{sink:t,format:e=rs,codec:n}={}){if(e!==rs&&e!==ns)throw new Error(`Unrecognized Arrow IPC format: ${e}`);let{schema:o,dictionaries:s=[],records:c=[],metadata:a}=r,i=new er(t||new ur),u=e===ns,f=[],m=[],b=n!=null?{codec:n,method:st.BUFFER}:null;u&&i.addBuffer(ht),o&&ar(i,P.Schema,cr(i,o),0);for(let p of s){let{data:g}=p;ar(i,P.DictionaryBatch,Kn(i,p,b),g.byteLength,f),ss(i,g.buffers);}for(let p of c)ar(i,P.RecordBatch,ir(i,p,b),p.byteLength,m),ss(i,p.buffers);return i.addBuffer(mn),u&&es(i,o,f,m,a),i.sink}function ss(r,t){for(let e=0;e<t.length;++e)r.addBuffer(t[e]);}function is(r,t){typeof t=="string"&&(t={format:t});let e=t?.codec,n=ee(e);if(e!=null&&!n)throw new Error(rr(e));let o=r.children;ro(o);let{dictionaries:s,idMap:c}=so(o,n),a=io(o,n),u={schema:oo(r.schema,c),dictionaries:s,records:a};return os(u,{...t,codec:e}).finish()}function ro(r){let t=r[0]?.data.map(e=>e.length);r.forEach(({data:e})=>{if(e.length!==t.length||e.some((n,o)=>n.length!==t[o]))throw new Error("Columns have inconsistent batch sizes.")});}function no(r){let t=0,e=[],n=[],o=[],s=[];return {node(c,a){e.push({length:c,nullCount:a});},buffer(c){let a=new Uint8Array(c.buffer,c.byteOffset,c.byteLength),i=r?Yn(a,r):a,u=i.byteLength;n.push({offset:t,length:u}),o.push(i),t+=u+7&-8;},variadic(c){s.push(c);},children(c,a){c.children.forEach((i,u)=>{as(i.type,a.children[u],this);});},done(){return {byteLength:t,nodes:e,regions:n,variadic:s,buffers:o}}}}function so(r,t){let e=[],n=new Map,o=new Map,s=-1,c=a=>{if(n.has(a))o.set(a.type,n.get(a));else {n.set(a,++s);for(let i=0;i<a.data.length;++i)e.push({id:s,isDelta:i>0,data:cs([a],i,t)});o.set(a.type,s);}};return r.forEach(a=>tn(a.data[0],c)),{dictionaries:e,idMap:o}}function tn(r,t){if(r?.type.typeId===l.Dictionary){let e=r.dictionary;t(e),tn(e.data[0],t);}r?.children?.forEach(e=>tn(e,t));}function oo(r,t){if(!t.size)return r;let e=s=>{s.typeId===l.Dictionary&&(s.id=t.get(s.dictionary),o(s)),s.children&&(s.children=s.children.slice()).forEach(n);},n=(s,c,a)=>{let i={...s.type};a[c]={...s,type:i},e(i);},o=s=>{let c={...s.dictionary};s.dictionary=c,e(c);};return r={...r,fields:r.fields.slice()},r.fields.forEach(n),r}function io(r,t){return (r[0]?.data||[]).map((e,n)=>cs(r,n,t))}function cs(r,t=0,e){let n=no(e);return r.forEach(o=>{as(o.type,o.data[t],n);}),n.done()}function as(r,t,e){let{typeId:n}=r;if(e.node(t.length,t.nullCount),n!==l.Null)switch(n){case l.Bool:case l.Int:case l.Time:case l.Duration:case l.Float:case l.Date:case l.Timestamp:case l.Decimal:case l.Interval:case l.FixedSizeBinary:case l.Dictionary:e.buffer(t.validity),e.buffer(t.values);return;case l.Utf8:case l.LargeUtf8:case l.Binary:case l.LargeBinary:e.buffer(t.validity),e.buffer(t.offsets),e.buffer(t.values);return;case l.BinaryView:case l.Utf8View:e.buffer(t.validity),e.buffer(t.values),e.variadic(t.data.length),t.data.forEach(o=>e.buffer(o));return;case l.List:case l.LargeList:case l.Map:e.buffer(t.validity),e.buffer(t.offsets),e.children(r,t);return;case l.ListView:case l.LargeListView:e.buffer(t.validity),e.buffer(t.offsets),e.buffer(t.sizes),e.children(r,t);return;case l.FixedSizeList:case l.Struct:e.buffer(t.validity),e.children(r,t);return;case l.RunEndEncoded:e.children(r,t);return;case l.Union:{e.buffer(t.typeIds),r.mode===j.Dense&&e.buffer(t.offsets),e.children(r,t);return}default:throw new Error(C(n))}}function O(r){return new lr(r)}var lr=class{constructor(t=q){this.buf=new t(512);}array(t){return bn(this.buf,t)}prep(t){t>=this.buf.length&&(this.buf=fe(this.buf,t));}get(t){return this.buf[t]}set(t,e){this.prep(e),this.buf[e]=t;}write(t,e){this.prep(e+t.length),this.buf.set(t,e);}};function fr(){return new en}var en=class extends lr{set(t){let e=t>>3;this.prep(e),this.buf[e]|=1<<t%8;}};var ft=class{constructor(t,e){this.type=t,this.ctx=e,this.batchClass=e.batchType(t);}init(){return this.index=-1,this}set(t,e){return this.index=e,false}done(){return null}batch(){let t=new this.batchClass(this.done());return this.init(),t}};var E=class extends ft{constructor(t,e){super(t,e);}init(){return this.nullCount=0,this.validity=fr(),super.init()}set(t,e){this.index=e;let n=t!=null;return n?this.validity.set(e):this.nullCount++,n}done(){let{index:t,nullCount:e,type:n,validity:o}=this;return {length:t+1,nullCount:e,type:n,validity:e?o.array((t>>3)+1):new q(0)}}};function oe(){let r=new Map,t=new Set;return {get(e,n){let o=e.id;if(o>=0&&r.has(o))return r.get(o);{let s=co(e,n);return o>=0&&r.set(o,s),t.add(s),s}},finish(e){t.forEach(n=>n.finish(e));}}}function co(r,t){let e=Object.create(null),n=t.builder(r.dictionary),o=[];n.init();let s=-1;return {type:r,values:n,add(c){return o.push(c),c},key(c){let a=Ct(c),i=e[a];return i===void 0&&(e[a]=i=++s,n.set(c,i)),i},finish(c){let a=r.dictionary,i=new(xt(a,c))(n.done()),u=new J([i]);o.forEach(f=>f.setDictionary(u));}}}var dr=class extends E{constructor(t,e){super(t,e),this.dict=e.dictionary(t);}init(){return this.values=O(this.type.indices.values),super.init()}set(t,e){super.set(t,e)&&this.values.set(this.dict.key(t),e);}done(){return {...super.done(),values:this.values.array(this.index+1)}}batch(){return this.dict.add(super.batch())}};function us(r){let t=rn();return r(e=>t.add(e)),t.type()}function rn(){let r=0,t=0,e=0,n=0,o=0,s=0,c=0,a=0,i=0,u=0,f=0,m=1/0,b=-1/0,p=1/0,g=-1/0,A,k,Z,Tt={};return {add(I){if(r++,I==null){t++;return}switch(typeof I){case "string":i++;break;case "number":n++,I<m&&(m=I),I>b&&(b=I),Number.isInteger(I)&&o++;break;case "bigint":s++,A===void 0?A=k=I:(I<A&&(A=I),I>k&&(k=I));break;case "boolean":e++;break;case "object":if(I instanceof Date)c++,+I%864e5===0&&a++;else if(le(I)){u++;let R=I.length;R<p&&(p=R),R>g&&(g=R),Z??=rn(),I.forEach(Z.add);}else {f++;for(let R in I)(Tt[R]??(Tt[R]=rn())).add(I[R]);}}},type(){let I=r-t;return I===0?Pr():o===I?uo(m,b):n===I?rt():s===I?lo(A,k):e===I?Mt():a===I?vt():c===I?ut():i===I?Gt($()):u===I?ao(Z.type(),p,g):f===I?Wt(Object.entries(Tt).map(R=>yt(R[0],R[1].type()))):fo()}}}function ao(r,t,e){return e===t?Zt(r,t):Yt(r)}function uo(r,t){let e=Math.max(Math.abs(r)-1,t);return e<128?it():e<32768?ct():e<2**31?G():rt()}function lo(r,t){let e=-r>t?-r-1n:t;if(e>=2**63)throw new Error(`BigInt exceeds 64 bits: ${e}`);return at()}function fo(){throw new Error("Mixed types detected, please define a union type.")}var Ht=class extends E{constructor(t,e){super(t,e),this.toOffset=pe(t.offsets);}init(){return this.offsets=O(this.type.offsets),this.values=O(),this.pos=0,super.init()}set(t,e){let{offsets:n,values:o,toOffset:s}=this;super.set(t,e)&&(o.write(t,this.pos),this.pos+=t.length),n.set(s(this.pos),e+1);}done(){return {...super.done(),offsets:this.offsets.array(this.index+2),values:this.values.array(this.pos+1)}}};var mr=class extends E{constructor(t,e){super(t,e);}init(){return this.values=fr(),super.init()}set(t,e){super.set(t,e),t&&this.values.set(e);}done(){return {...super.done(),values:this.values.array((this.index>>3)+1)}}};var hr=class extends E{constructor(t,e){super(t,e),this.scale=10**t.scale,this.stride=t.bitWidth>>6;}init(){return this.values=O(this.type.values),super.init()}set(t,e){let{scale:n,stride:o,values:s}=this;super.set(t,e)&&(s.prep((e+1)*o),Bn(t,s.buf,e*o,o,n));}done(){let{index:t,stride:e,values:n}=this;return {...super.done(),values:n.array((t+1)*e)}}};var pr=class extends E{constructor(t,e){super(t,e),this.stride=t.stride;}init(){return this.values=O(),super.init()}set(t,e){super.set(t,e)&&this.values.write(t,e*this.stride);}done(){let{stride:t,values:e}=this;return {...super.done(),values:e.array(t*(this.index+1))}}};var br=class extends E{constructor(t,e){super(t,e),this.child=e.builder(this.type.children[0].type),this.stride=t.stride;}init(){return this.child.init(),super.init()}set(t,e){let{child:n,stride:o}=this,s=e*o;if(super.set(t,e))for(let c=0;c<o;++c)n.set(t[c],s+c);else n.index=s+o;}done(){let{child:t}=this;return {...super.done(),children:[t.batch()]}}};var yr=class extends E{init(){return this.values=O(this.type.values),super.init()}set(t,e){if(super.set(t,e)){let n=e<<1;this.values.set(t[0],n),this.values.set(t[1],n+1);}}done(){return {...super.done(),values:this.values.array(this.index+1<<1)}}},gr=class extends E{init(){return this.values=O(),super.init()}set(t,e){super.set(t,e)&&this.values.write(Dn(t),e<<4);}done(){return {...super.done(),values:this.values.array(this.index+1<<4)}}};var ie=class extends E{constructor(t,e,n){super(t,e),this.child=n;}init(){this.child.init();let t=this.type.offsets;return this.offsets=O(t),this.toOffset=pe(t),this.pos=0,super.init()}done(){return {...super.done(),offsets:this.offsets.array(this.index+2),children:[this.child.batch()]}}},Ir=class extends ie{constructor(t,e){super(t,e,e.builder(t.children[0].type));}set(t,e){let{child:n,offsets:o,toOffset:s}=this;super.set(t,e)&&t.forEach(c=>n.set(c,this.pos++)),o.set(s(this.pos),e+1);}};var ce=class extends E{constructor(t,e){super(t,e),this.children=t.children.map(n=>e.builder(n.type));}init(){return this.children.forEach(t=>t.init()),super.init()}done(){let{children:t}=this;return t.forEach(e=>e.index=this.index),{...super.done(),children:t.map(e=>e.batch())}}},wr=class extends ce{constructor(t,e){super(t,e),this.setters=this.children.map((n,o)=>{let s=t.children[o].name;return (c,a)=>n.set(c?.[s],a)});}set(t,e){super.set(t,e);let n=this.setters;for(let o=0;o<n.length;++o)n[o](t,e);}};var xr=class extends ie{constructor(t,e){super(t,e,new nn(t.children[0].type,e));}set(t,e){let{child:n,offsets:o,toOffset:s}=this;if(super.set(t,e))for(let c of t)n.set(c,this.pos++);o.set(s(this.pos),e+1);}},nn=class extends ce{set(t,e){super.set(t,e);let[n,o]=this.children;n.set(t[0],e),o.set(t[1],e);}};var mo={},Ar=class extends ft{constructor(t,e){super(t,e),this.children=t.children.map(n=>e.builder(n.type));}init(){return this.pos=0,this.key=null,this.value=mo,this.children.forEach(t=>t.init()),super.init()}next(){let[t,e]=this.children;t.set(this.index+1,this.pos),e.set(this.value,this.pos++);}set(t,e){if(t!==this.value){let n=Ct(t);n!==this.key&&(this.key&&this.next(),this.key=n,this.value=t);}this.index=e;}done(){this.next();let{children:t,index:e,type:n}=this;return {length:e+1,nullCount:0,type:n,children:t.map(o=>o.batch())}}};var Tr=class extends ft{constructor(t,e){super(t,e),this.children=t.children.map(n=>e.builder(n.type)),this.typeMap=t.typeMap,this.lookup=t.typeIdForValue;}init(){return this.nullCount=0,this.typeIds=O(bt),this.children.forEach(t=>t.init()),super.init()}set(t,e){let{children:n,lookup:o,typeMap:s,typeIds:c}=this;this.index=e;let a=o(t,e),i=n[s[a]];c.set(a,e),t==null&&++this.nullCount,this.update(t,e,i);}done(){let{children:t,nullCount:e,type:n,typeIds:o}=this,s=this.index+1;return {length:s,nullCount:e,type:n,typeIds:o.array(s),children:t.map(c=>c.batch())}}},Er=class extends Tr{update(t,e,n){n.set(t,e),this.children.forEach(o=>{o!==n&&o.set(null,e);});}},_r=class extends Tr{init(){return this.offsets=O(this.type.offsets),super.init()}update(t,e,n){let o=n.index+1;n.set(t,o),this.offsets.set(o,e);}done(){return {...super.done(),offsets:this.offsets.array(this.index+1)}}};var kr=class extends Ht{set(t,e){super.set(t&&ye(t),e);}};var dt=class extends E{constructor(t,e){super(t,e),this.values=O(t.values);}init(){return this.values=O(this.type.values),super.init()}set(t,e){super.set(t,e)&&this.values.set(t,e);}done(){return {...super.done(),values:this.values.array(this.index+1)}}},Sr=class extends dt{set(t,e){super.set(t==null?t:Y(t),e);}},At=class extends dt{constructor(t,e,n){super(t,e),this.transform=n;}set(t,e){super.set(t==null?t:this.transform(t),e);}};function sn(r={},t=oe()){return {batchType:e=>xt(e,r),builder(e){return on(e,this)},dictionary(e){return t.get(e,this)},finish:()=>t.finish(r)}}function on(r,t=sn()){let{typeId:e}=r;switch(e){case l.Int:case l.Time:case l.Duration:return _t(r.values)?new Sr(r,t):new dt(r,t);case l.Float:return r.precision?new dt(r,t):new At(r,t,vn);case l.Binary:case l.LargeBinary:return new Ht(r,t);case l.Utf8:case l.LargeUtf8:return new kr(r,t);case l.Bool:return new mr(r,t);case l.Decimal:return r.bitWidth===32?new At(r,t,On(r.scale)):new hr(r,t);case l.Date:return new At(r,t,r.unit?Y:kn);case l.Timestamp:return new At(r,t,Sn(r.unit));case l.Interval:switch(r.unit){case v.DAY_TIME:return new yr(r,t);case v.MONTH_DAY_NANO:return new gr(r,t)}return new dt(r,t);case l.List:case l.LargeList:return new Ir(r,t);case l.Struct:return new wr(r,t);case l.Union:return r.mode?new _r(r,t):new Er(r,t);case l.FixedSizeBinary:return new pr(r,t);case l.FixedSizeList:return new br(r,t);case l.Map:return new xr(r,t);case l.RunEndEncoded:return new Ar(r,t);case l.Dictionary:return new dr(r,t)}throw new Error(C(e))}function cn(r,t,e={},n){let o=In(r)?i=>{for(let u of r)i(u);}:r;t??=us(o);let{maxBatchRows:s=1/0,...c}=e,a;if(t.typeId===l.Null){let i=0;o(()=>++i),a=ho(t,i,s);}else {let i=sn(c,n),u=on(t,i).init(),f=b=>a.push(b.batch());a=[];let m=0;o(b=>{u.set(b,m++),m>=s&&(f(u),m=0);}),m&&f(u),i.finish();}return new J(a,t)}function ho(r,t,e){let n=[],o=a=>new Rt({length:a,nullCount:a,type:r}),s=Math.floor(t/e);for(let a=0;a<s;++a)n.push(o(e));let c=t%e;return c&&n.push(o(c)),n}function an(r,t,e={},n){return !t&&Ur(r)?po(r,e):cn(o=>r.forEach(o),t,e,n)}function po(r,{maxBatchRows:t,useBigInt:e}){let n=r.constructor,o=bo(n),s=r.length,c=Math.min(t||1/0,s),a=Math.floor(s/c),i=[],u=_t(n)&&!e?W:K,f=(b,p)=>i.push(new u({length:p-b,nullCount:0,type:o,validity:new q(0),values:r.subarray(b,p)})),m=0;for(let b=0;b<a;++b)f(m,m+=c);return m<s&&f(m,s),new J(i)}function bo(r){switch(r){case ue:return Pt();case et:return rt();case bt:return it();case Br:return ct();case M:return G();case N:return at();case q:return Dt();case jt:return Ot();case zt:return Bt();case Et:return Ut()}}function un(r,t){let e=[],n=Array.isArray(r)?r:Object.entries(r),o=n[0]?.[1].length,s=n.map(([a,i])=>{if(i.length!==o)throw new Error("All columns must have the same length.");return e.push(yt(a,i.type)),i}),c={version:U.V5,endianness:Or.Little,fields:e,metadata:null};return new wt(c,s,t)}function ae(r,t={}){let{types:e={},...n}=t,o=oe(),c=(Array.isArray(r)?r:Object.entries(r)).map(([a,i])=>[a,an(i,e[a],n,o)]);return un(c,t.useProxy)}var h={INVALID:0,BOOLEAN:1,TINYINT:2,SMALLINT:3,INTEGER:4,BIGINT:5,UTINYINT:6,USMALLINT:7,UINTEGER:8,UBIGINT:9,FLOAT:10,DOUBLE:11,TIMESTAMP:12,DATE:13,TIME:14,INTERVAL:15,HUGEINT:16,UHUGEINT:32,VARCHAR:17,BLOB:18,DECIMAL:19,TIMESTAMP_S:20,TIMESTAMP_MS:21,TIMESTAMP_NS:22,ENUM:23,LIST:24,STRUCT:25,MAP:26,ARRAY:33,UUID:27,UNION:28,BIT:29,TIME_TZ:30,TIMESTAMP_TZ:31},yo=(n=>(n.AUTOMATIC="automatic",n.READ_ONLY="read_only",n.READ_WRITE="read_write",n))(yo||{}),y=class r extends Error{code;query;constructor(t,e,n){super(t),this.name="DuckDBError",this.code=e,this.query=n,Error.captureStackTrace&&Error.captureStackTrace(this,r);}};function go(r){let t=r.replace(/\/\*[\s\S]*?\*\//g," ");return t=t.replace(/--[^\n\r]*/g," "),t=t.replace(/#[^\n\r]*/g," "),t}var mt={secretsFunction:{pattern:/\bduckdb_secrets\s*\(/i,message:"Access to duckdb_secrets() is not allowed"},pragma:{pattern:/^\s*PRAGMA\b/im,message:"PRAGMA statements are not allowed"},copyTo:{pattern:/\bCOPY\b[\s\S]+?\bTO\b\s+['"`]/i,message:"COPY ... TO statements are not allowed"},exportDatabase:{pattern:/\bEXPORT\s+DATABASE\b/i,message:"EXPORT DATABASE statements are not allowed"}};function Io(r,t={}){let e=go(r);return !t.allowSecretsFunction&&mt.secretsFunction.pattern.test(e)?{safe:false,sql:r,reason:mt.secretsFunction.message,matchedPattern:"duckdb_secrets()"}:!t.allowPragma&&mt.pragma.pattern.test(e)?{safe:false,sql:r,reason:mt.pragma.message,matchedPattern:"PRAGMA"}:!t.allowCopyTo&&mt.copyTo.pattern.test(e)?{safe:false,sql:r,reason:mt.copyTo.message,matchedPattern:"COPY ... TO"}:!t.allowExportDatabase&&mt.exportDatabase.pattern.test(e)?{safe:false,sql:r,reason:mt.exportDatabase.message,matchedPattern:"EXPORT DATABASE"}:{safe:true,sql:r}}function ul(r,t={}){let e=Io(r,t);if(!e.safe)throw new y(e.reason,"SANITIZE_ERROR",r);return r}var d=null,Dr=null;function _(){if(!d)throw new y("DuckDB WASM not initialized. Call init() first.");return d}async function ll(r){if(!d){if(Dr){await Dr;return}if(!r?.wasmModule)throw new Error("Workers build requires a pre-compiled WASM module. Import the WASM file and pass it as { wasmModule }.");Dr=(async()=>{let t=(await import('./wasm/duckdb-workers.js')).default;return await t({instantiateWasm:(o,s)=>(WebAssembly.instantiate(r.wasmModule,o).then(c=>{s(c);}),{})})})(),d=await Dr;}}function fl(){if(!d)throw new y("DuckDB WASM not initialized. Call init() first.");let r=d.ccall("duckdb_library_version","number",[],[]);return d.UTF8ToString(r)}function wo(r,t,e,n,o){let s=new Array(n),c=e!==0;switch(o){case h.BOOLEAN:{for(let a=0;a<n;a++)c&&r.HEAPU8[e+a]?s[a]=null:s[a]=r.HEAPU8[t+a]!==0;break}case h.TINYINT:{for(let a=0;a<n;a++)c&&r.HEAPU8[e+a]?s[a]=null:s[a]=r.HEAP8[t+a];break}case h.SMALLINT:{let a=t>>1;for(let i=0;i<n;i++)c&&r.HEAPU8[e+i]?s[i]=null:s[i]=r.HEAP16[a+i];break}case h.INTEGER:{let a=t>>2;for(let i=0;i<n;i++)c&&r.HEAPU8[e+i]?s[i]=null:s[i]=r.HEAP32[a+i];break}case h.BIGINT:{let a=t>>2;for(let i=0;i<n;i++)if(c&&r.HEAPU8[e+i])s[i]=null;else {let u=r.HEAPU32[a+i*2],f=r.HEAP32[a+i*2+1];if(f===0&&u<=9007199254740991)s[i]=u;else if(f===-1&&u>=2147483648)s[i]=f*4294967296+u;else {let m=BigInt(f)*BigInt(4294967296)+BigInt(u>>>0);s[i]=m.toString();}}break}case h.UTINYINT:{for(let a=0;a<n;a++)c&&r.HEAPU8[e+a]?s[a]=null:s[a]=r.HEAPU8[t+a];break}case h.USMALLINT:{let a=t>>1;for(let i=0;i<n;i++)c&&r.HEAPU8[e+i]?s[i]=null:s[i]=r.HEAPU16[a+i];break}case h.UINTEGER:{let a=t>>2;for(let i=0;i<n;i++)c&&r.HEAPU8[e+i]?s[i]=null:s[i]=r.HEAPU32[a+i];break}case h.UBIGINT:{let a=t>>2;for(let i=0;i<n;i++)if(c&&r.HEAPU8[e+i])s[i]=null;else {let u=r.HEAPU32[a+i*2],f=r.HEAPU32[a+i*2+1];if(f===0&&u<=9007199254740991)s[i]=u;else {let m=BigInt(f)*BigInt(4294967296)+BigInt(u);s[i]=m.toString();}}break}case h.FLOAT:{let a=t>>2;for(let i=0;i<n;i++)c&&r.HEAPU8[e+i]?s[i]=null:s[i]=r.HEAPF32[a+i];break}case h.DOUBLE:{let a=t>>3;for(let i=0;i<n;i++)c&&r.HEAPU8[e+i]?s[i]=null:s[i]=r.HEAPF64[a+i];break}case h.DATE:{let a=t>>2;for(let i=0;i<n;i++)if(c&&r.HEAPU8[e+i])s[i]=null;else {let u=r.HEAP32[a+i],f=new Date(u*24*60*60*1e3);s[i]=f.toISOString().split("T")[0];}break}case h.TIMESTAMP:case h.TIMESTAMP_TZ:{let a=t>>2;for(let i=0;i<n;i++)if(c&&r.HEAPU8[e+i])s[i]=null;else {let u=r.HEAPU32[a+i*2],f=r.HEAP32[a+i*2+1],m=BigInt(f)*BigInt(4294967296)+BigInt(u>>>0),b=Number(m/BigInt(1e3)),p=new Date(b);s[i]=p.toISOString();}break}default:for(let a=0;a<n;a++)s[a]=null;break}return s}var ln=class{stmtPtr;closed=false;sql;constructor(t,e,n){this.stmtPtr=t,this.sql=n;}parameterCount(){if(this.closed)throw new y("Statement is closed");return _().ccall("duckdb_nparams","number",["number"],[this.stmtPtr])}bindBoolean(t,e){if(this.closed)throw new y("Statement is closed");if(_().ccall("duckdb_bind_boolean","number",["number","number","number","number"],[this.stmtPtr,t,0,e?1:0])!==0)throw new y(`Failed to bind boolean at index ${t}`);return this}bindInt32(t,e){if(this.closed)throw new y("Statement is closed");if(_().ccall("duckdb_bind_int32","number",["number","number","number","number"],[this.stmtPtr,t,0,e])!==0)throw new y(`Failed to bind int32 at index ${t}`);return this}bindInt64(t,e){if(this.closed)throw new y("Statement is closed");let n=_(),o=BigInt(e),s=Number(o&BigInt(4294967295)),c=Number(o>>BigInt(32)&BigInt(4294967295));if(n.ccall("duckdb_bind_int64","number",["number","number","number","number","number"],[this.stmtPtr,t,0,s,c])!==0)throw new y(`Failed to bind int64 at index ${t}`);return this}bindFloat(t,e){if(this.closed)throw new y("Statement is closed");if(_().ccall("duckdb_bind_float","number",["number","number","number","number"],[this.stmtPtr,t,0,e])!==0)throw new y(`Failed to bind float at index ${t}`);return this}bindDouble(t,e){if(this.closed)throw new y("Statement is closed");if(_().ccall("duckdb_bind_double","number",["number","number","number","number"],[this.stmtPtr,t,0,e])!==0)throw new y(`Failed to bind double at index ${t}`);return this}bindString(t,e){if(this.closed)throw new y("Statement is closed");if(_().ccall("duckdb_bind_varchar","number",["number","number","number","string"],[this.stmtPtr,t,0,e])!==0)throw new y(`Failed to bind string at index ${t}`);return this}bindBlob(t,e){if(this.closed)throw new y("Statement is closed");let n=_(),o=n._malloc(e.length);try{if(n.HEAPU8.set(e,o),n.ccall("duckdb_bind_blob","number",["number","number","number","number","number","number"],[this.stmtPtr,t,0,o,e.length,0])!==0)throw new y(`Failed to bind blob at index ${t}`)}finally{n._free(o);}return this}bindNull(t){if(this.closed)throw new y("Statement is closed");if(_().ccall("duckdb_bind_null","number",["number","number","number"],[this.stmtPtr,t,0])!==0)throw new y(`Failed to bind null at index ${t}`);return this}bindTimestamp(t,e){if(this.closed)throw new y("Statement is closed");let n=_(),o=BigInt(e.getTime())*BigInt(1e3),s=Number(o&BigInt(4294967295)),c=Number(o>>BigInt(32)&BigInt(4294967295));if(n.ccall("duckdb_bind_timestamp","number",["number","number","number","number","number"],[this.stmtPtr,t,0,s,c])!==0)throw new y(`Failed to bind timestamp at index ${t}`);return this}bindDate(t,e){if(this.closed)throw new y("Statement is closed");let n=_(),o=Math.floor(e.getTime()/(1440*60*1e3));if(n.ccall("duckdb_bind_date","number",["number","number","number","number"],[this.stmtPtr,t,0,o])!==0)throw new y(`Failed to bind date at index ${t}`);return this}bind(t,e){return e==null?this.bindNull(t):typeof e=="boolean"?this.bindBoolean(t,e):typeof e=="number"?Number.isInteger(e)&&e>=-2147483648&&e<=2147483647?this.bindInt32(t,e):this.bindDouble(t,e):typeof e=="bigint"?this.bindInt64(t,e):typeof e=="string"?this.bindString(t,e):e instanceof Date?this.bindTimestamp(t,e):e instanceof Uint8Array?this.bindBlob(t,e):this.bindString(t,String(e))}async run(){if(this.closed)throw new y("Statement is closed");let t=_(),e=t._malloc(64);try{if(await t.ccall("duckdb_execute_prepared","number",["number","number"],[this.stmtPtr,e],{async:!0})!==0){let u=t.ccall("duckdb_result_error","number",["number"],[e]),f=u?t.UTF8ToString(u):"Prepared statement execution failed";throw t.ccall("duckdb_destroy_result",null,["number"],[e]),new y(f,void 0,this.sql)}let o=t.ccall("duckdb_column_count","number",["number"],[e]),s=t.ccall("duckdb_row_count","number",["number"],[e]),c=[];for(let u=0;u<o;u++){let f=t.ccall("duckdb_column_name","number",["number","number"],[e,u]),m=t.ccall("duckdb_column_type","number",["number","number"],[e,u]);c.push({name:t.UTF8ToString(f),type:m});}let a=[];for(let u=0;u<o;u++){let f=c[u],m=t.ccall("duckdb_column_data","number",["number","number"],[e,u]),b=t.ccall("duckdb_nullmask_data","number",["number","number"],[e,u]);if(f.type===h.VARCHAR||m===0){let p=new Array(s);for(let g=0;g<s;g++)if(t.ccall("duckdb_value_is_null","number",["number","number","number","number","number"],[e,u,0,g,0]))p[g]=null;else {let k=t.ccall("duckdb_value_varchar","number",["number","number","number","number","number"],[e,u,0,g,0]);k?(p[g]=t.UTF8ToString(k),t._free(k)):p[g]=null;}a.push(p);}else a.push(wo(t,m,b,s,f.type));}let i=new Array(s);for(let u=0;u<s;u++){let f={};for(let m=0;m<o;m++)f[c[m].name]=a[m][u];i[u]=f;}return t.ccall("duckdb_destroy_result",null,["number"],[e]),i}finally{t._free(e);}}async execute(){if(this.closed)throw new y("Statement is closed");let t=_(),e=t._malloc(64);try{if(await t.ccall("duckdb_execute_prepared","number",["number","number"],[this.stmtPtr,e],{async:!0})!==0){let s=t.ccall("duckdb_result_error","number",["number"],[e]),c=s?t.UTF8ToString(s):"Prepared statement execution failed";throw t.ccall("duckdb_destroy_result",null,["number"],[e]),new y(c,void 0,this.sql)}let o=t.ccall("duckdb_rows_changed","number",["number"],[e]);return t.ccall("duckdb_destroy_result",null,["number"],[e]),o}finally{t._free(e);}}close(){if(this.closed)return;let t=_(),e=t._malloc(4);try{t.setValue(e,this.stmtPtr,"i32"),t.ccall("duckdb_destroy_prepare",null,["number"],[e]);}finally{t._free(e);}this.closed=true,this.stmtPtr=0;}},fn=class{chunkPtr;destroyed=false;columns;constructor(t,e){this.chunkPtr=t,this.columns=e;}get rowCount(){return this.destroyed?0:_().ccall("duckdb_data_chunk_get_size","number",["number"],[this.chunkPtr])}get columnCount(){return this.destroyed?0:_().ccall("duckdb_data_chunk_get_column_count","number",["number"],[this.chunkPtr])}getColumnInfo(){return this.columns}getColumn(t){if(this.destroyed)throw new y("DataChunk has been destroyed");let e=_(),n=this.rowCount,o=e.ccall("duckdb_data_chunk_get_vector","number",["number","number"],[this.chunkPtr,t]);if(!o)throw new y(`Failed to get vector for column ${t}`);let s=e.ccall("duckdb_vector_get_data","number",["number"],[o]),c=e.ccall("duckdb_vector_get_validity","number",["number"],[o]),a=this.columns[t]?.type??h.VARCHAR;return this.readVectorData(s,c,n,a)}toArray(){if(this.destroyed)return [];let t=this.rowCount,e=this.columnCount,n=[];for(let s=0;s<e;s++)n.push(this.getColumn(s));let o=new Array(t);for(let s=0;s<t;s++){let c={};for(let a=0;a<e;a++)c[this.columns[a].name]=n[a][s];o[s]=c;}return o}readVectorData(t,e,n,o){let s=_(),c=new Array(n),a=i=>e===0?false:s.ccall("duckdb_validity_row_is_valid","number",["number","number"],[e,i])===0;switch(o){case h.BOOLEAN:for(let i=0;i<n;i++)c[i]=a(i)?null:s.HEAPU8[t+i]!==0;break;case h.TINYINT:for(let i=0;i<n;i++)c[i]=a(i)?null:s.HEAP8[t+i];break;case h.SMALLINT:{let i=t>>1;for(let u=0;u<n;u++)c[u]=a(u)?null:s.HEAP16[i+u];break}case h.INTEGER:{let i=t>>2;for(let u=0;u<n;u++)c[u]=a(u)?null:s.HEAP32[i+u];break}case h.BIGINT:{let i=t>>2;for(let u=0;u<n;u++)if(a(u))c[u]=null;else {let f=s.HEAPU32[i+u*2],m=s.HEAP32[i+u*2+1];c[u]=m*4294967296+f;}break}case h.FLOAT:{let i=t>>2;for(let u=0;u<n;u++)c[u]=a(u)?null:s.HEAPF32[i+u];break}case h.DOUBLE:{let i=t>>3;for(let u=0;u<n;u++)c[u]=a(u)?null:s.HEAPF64[i+u];break}case h.DATE:{let i=t>>2;for(let u=0;u<n;u++)if(a(u))c[u]=null;else {let f=s.HEAP32[i+u],m=new Date(f*24*60*60*1e3);c[u]=m.toISOString().split("T")[0];}break}case h.TIMESTAMP:case h.TIMESTAMP_TZ:{let i=t>>2;for(let u=0;u<n;u++)if(a(u))c[u]=null;else {let f=s.HEAPU32[i+u*2],b=s.HEAP32[i+u*2+1]*4294967296+f,p=new Date(b/1e3);c[u]=p.toISOString();}break}default:for(let i=0;i<n;i++)if(a(i))c[i]=null;else {let u=t+i*16,f=s.HEAP32[u>>2];if(f<=12){let m="";for(let b=0;b<f;b++)m+=String.fromCharCode(s.HEAPU8[u+4+b]);c[i]=m;}else {let m=s.HEAP32[u+8>>2];c[i]=s.UTF8ToString(m);}}break}return c}destroy(){if(this.destroyed)return;let t=_(),e=t._malloc(4);try{t.setValue(e,this.chunkPtr,"i32"),t.ccall("duckdb_destroy_data_chunk",null,["number"],[e]);}finally{t._free(e);}this.destroyed=true,this.chunkPtr=0;}},ls=class{resultPtr;closed=false;columns;currentChunkIndex=0;totalChunks;constructor(t,e){this.resultPtr=t,this.columns=e;let n=_();this.totalChunks=n.ccall("duckdb_result_chunk_count","number",["number"],[t]);}getColumns(){return this.columns}get columnCount(){return this.columns.length}get chunkCount(){return this.totalChunks}nextChunk(){if(this.closed||this.currentChunkIndex>=this.totalChunks)return null;let e=_().ccall("duckdb_result_get_chunk","number",["number","number"],[this.resultPtr,this.currentChunkIndex]);return e?(this.currentChunkIndex++,new fn(e,this.columns)):null}reset(){this.currentChunkIndex=0;}*[Symbol.iterator](){this.reset();let t;for(;(t=this.nextChunk())!==null;)try{yield t;}finally{t.destroy();}}toArray(){let t=[];for(let e of this)t.push(...e.toArray());return t}toArrowTable(){let t={},e={};for(let n of this.columns)t[n.name]=[],e[n.name]=this.getFlechetteType(n.type);for(let n of this)for(let o=0;o<this.columns.length;o++){let s=this.columns[o].name,c=n.getColumn(o);t[s].push(...c);}return ae(t,{types:e})}getFlechetteType(t){switch(t){case h.BOOLEAN:return Mt();case h.TINYINT:return it();case h.SMALLINT:return ct();case h.INTEGER:return G();case h.BIGINT:return at();case h.UTINYINT:return Dt();case h.USMALLINT:return Ot();case h.UINTEGER:return Bt();case h.UBIGINT:return Ut();case h.FLOAT:return Pt();case h.DOUBLE:return rt();case h.VARCHAR:return $();case h.DATE:return vt();case h.TIMESTAMP:case h.TIMESTAMP_TZ:return ut(w.MICROSECOND);default:return $()}}close(){if(this.closed)return;_().ccall("duckdb_destroy_result",null,["number"],[this.resultPtr]),this.closed=true,this.resultPtr=0;}},fs=class r{dbPtr=0;closed=false;constructor(t={}){if(!d)throw new y("DuckDB WASM not initialized. Call init() first.");let e=d,n={accessMode:t.accessMode??"automatic",enableExternalAccess:t.enableExternalAccess??true,lockConfiguration:t.lockConfiguration??true,customConfig:t.customConfig??{}},o=e._malloc(4);if(e.ccall("duckdb_create_config","number",["number"],[o])!==0)throw e._free(o),new y("Failed to create DuckDB configuration");let c=e.getValue(o,"i32");e._free(o);try{let a=(f,m)=>{if(e.ccall("duckdb_set_config","number",["number","string","string"],[c,f,m])!==0)throw new y(`Failed to set config option: ${f}`)};n.accessMode!=="automatic"&&a("access_mode",n.accessMode),n.enableExternalAccess===!1&&a("enable_external_access","false");for(let[f,m]of Object.entries(n.customConfig))a(f,m);let i=e._malloc(4),u=e._malloc(4);try{if(e.ccall("duckdb_open_ext","number",["string","number","number","number"],[":memory:",i,c,u])!==0){let m=e.getValue(u,"i32"),b=m?e.UTF8ToString(m):"Unknown error";throw new y(`Failed to open database: ${b}`)}this.dbPtr=e.getValue(i,"i32");}finally{e._free(i),e._free(u);}if(n.enableExternalAccess!==!1&&e.ccall("duckdb_wasm_httpfs_init",null,["number"],[this.dbPtr]),n.lockConfiguration!==!1){let f=e._malloc(4);try{if(e.ccall("duckdb_connect","number",["number","number"],[this.dbPtr,f])===0){let b=e.getValue(f,"i32"),p=e._malloc(64);try{e.ccall("duckdb_query","number",["number","string","number"],[b,"SET lock_configuration = true",p]),e.ccall("duckdb_destroy_result",null,["number"],[p]);}finally{e._free(p);}e.ccall("duckdb_disconnect",null,["number"],[f]);}}finally{e._free(f);}}}finally{let a=e._malloc(4);e.setValue(a,c,"i32"),e.ccall("duckdb_destroy_config",null,["number"],[a]),e._free(a);}}static async create(t){return new r(t)}connect(){if(this.closed||!d)throw new y("Database is closed");let t=d._malloc(4);try{if(d.ccall("duckdb_connect","number",["number","number"],[this.dbPtr,t])!==0)throw new y("Failed to create connection");let n=d.getValue(t,"*");return new dn(n)}finally{d._free(t);}}close(){if(this.closed||!d)return;let t=d._malloc(4);try{d.setValue(t,this.dbPtr,"*"),d.ccall("duckdb_close",null,["number"],[t]);}finally{d._free(t);}this.closed=true,this.dbPtr=0;}},dn=class{connPtr;closed=false;constructor(t){this.connPtr=t;}async query(t){if(this.closed||!d)throw new y("Connection is closed");let e=d._malloc(64);try{if(await d.ccall("duckdb_query","number",["number","string","number"],[this.connPtr,t,e],{async:!0})!==0){let u=d.ccall("duckdb_result_error","number",["number"],[e]),f=u?d.UTF8ToString(u):"Query failed";throw d.ccall("duckdb_destroy_result",null,["number"],[e]),new y(f,void 0,t)}let o=d.ccall("duckdb_column_count","number",["number"],[e]),s=[];for(let u=0;u<o;u++){let f=d.ccall("duckdb_column_name","number",["number","number"],[e,u]),m=d.ccall("duckdb_column_type","number",["number","number"],[e,u]);s.push({name:d.UTF8ToString(f),type:m});}let c=d.ccall("duckdb_row_count","number",["number"],[e]),a=[];for(let u=0;u<o;u++){let f=s[u],m=d.ccall("duckdb_column_data","number",["number","number"],[e,u]),b=d.ccall("duckdb_nullmask_data","number",["number","number"],[e,u]);if(f.type===h.VARCHAR||m===0){let p=new Array(c);for(let g=0;g<c;g++)if(d.ccall("duckdb_value_is_null","number",["number","number","number","number","number"],[e,u,0,g,0]))p[g]=null;else {let k=d.ccall("duckdb_value_varchar","number",["number","number","number","number","number"],[e,u,0,g,0]);k?(p[g]=d.UTF8ToString(k),d._free(k)):p[g]=null;}a.push(p);}else a.push(this.readColumnData(m,b,c,f.type));}let i=new Array(c);for(let u=0;u<c;u++){let f={};for(let m=0;m<o;m++)f[s[m].name]=a[m][u];i[u]=f;}return d.ccall("duckdb_destroy_result",null,["number"],[e]),i}finally{d._free(e);}}readColumnData(t,e,n,o){if(!d)return [];let s=new Array(n),c=e!==0;switch(o){case h.BOOLEAN:{let a=t;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAPU8[a+i]!==0;break}case h.TINYINT:{for(let a=0;a<n;a++)c&&d.HEAPU8[e+a]?s[a]=null:s[a]=d.HEAP8[t+a];break}case h.SMALLINT:{let a=t>>1;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAP16[a+i];break}case h.INTEGER:{let a=t>>2;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAP32[a+i];break}case h.BIGINT:{let a=t>>2;for(let i=0;i<n;i++)if(c&&d.HEAPU8[e+i])s[i]=null;else {let u=d.HEAPU32[a+i*2],f=d.HEAP32[a+i*2+1];if(f===0&&u<=9007199254740991)s[i]=u;else if(f===-1&&u>=2147483648)s[i]=f*4294967296+u;else {let m=BigInt(f)*BigInt(4294967296)+BigInt(u>>>0);s[i]=m.toString();}}break}case h.UTINYINT:{for(let a=0;a<n;a++)c&&d.HEAPU8[e+a]?s[a]=null:s[a]=d.HEAPU8[t+a];break}case h.USMALLINT:{let a=t>>1;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAPU16[a+i];break}case h.UINTEGER:{let a=t>>2;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAPU32[a+i];break}case h.UBIGINT:{let a=t>>2;for(let i=0;i<n;i++)if(c&&d.HEAPU8[e+i])s[i]=null;else {let u=d.HEAPU32[a+i*2],f=d.HEAPU32[a+i*2+1];if(f===0&&u<=9007199254740991)s[i]=u;else {let m=BigInt(f)*BigInt(4294967296)+BigInt(u);s[i]=m.toString();}}break}case h.FLOAT:{let a=t>>2;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAPF32[a+i];break}case h.DOUBLE:{let a=t>>3;for(let i=0;i<n;i++)c&&d.HEAPU8[e+i]?s[i]=null:s[i]=d.HEAPF64[a+i];break}case h.DATE:{let a=t>>2;for(let i=0;i<n;i++)if(c&&d.HEAPU8[e+i])s[i]=null;else {let u=d.HEAP32[a+i],f=new Date(u*24*60*60*1e3);s[i]=f.toISOString().split("T")[0];}break}case h.TIMESTAMP:case h.TIMESTAMP_TZ:{let a=t>>2;for(let i=0;i<n;i++)if(c&&d.HEAPU8[e+i])s[i]=null;else {let u=d.HEAPU32[a+i*2],f=d.HEAP32[a+i*2+1],m=BigInt(f)*BigInt(4294967296)+BigInt(u>>>0),b=Number(m/BigInt(1e3)),p=new Date(b);s[i]=p.toISOString();}break}default:for(let a=0;a<n;a++)s[a]=null;break}return s}async queryArrow(t){if(this.closed||!d)throw new y("Connection is closed");let e=d._malloc(64);try{if(await d.ccall("duckdb_query","number",["number","string","number"],[this.connPtr,t,e],{async:!0})!==0){let u=d.ccall("duckdb_result_error","number",["number"],[e]),f=u?d.UTF8ToString(u):"Query failed";throw d.ccall("duckdb_destroy_result",null,["number"],[e]),new y(f,void 0,t)}let o=d.ccall("duckdb_column_count","number",["number"],[e]),s=d.ccall("duckdb_row_count","number",["number"],[e]),c=[];for(let u=0;u<o;u++){let f=d.ccall("duckdb_column_name","number",["number","number"],[e,u]),m=d.ccall("duckdb_column_type","number",["number","number"],[e,u]);c.push({name:d.UTF8ToString(f),type:m,flechetteType:this.getFlechetteType(m)||$()});}let a={},i={};for(let u=0;u<o;u++){let f=c[u],m=d.ccall("duckdb_column_data","number",["number","number"],[e,u]),b=d.ccall("duckdb_nullmask_data","number",["number","number"],[e,u]);if(f.type===h.VARCHAR||m===0){let p=new Array(s);for(let g=0;g<s;g++)if(d.ccall("duckdb_value_is_null","number",["number","number","number","number","number"],[e,u,0,g,0]))p[g]=null;else {let k=d.ccall("duckdb_value_varchar","number",["number","number","number","number","number"],[e,u,0,g,0]);k?(p[g]=d.UTF8ToString(k),d._free(k)):p[g]=null;}a[f.name]=p;}else a[f.name]=this.readColumnData(m,b,s,f.type);i[f.name]=f.flechetteType;}return d.ccall("duckdb_destroy_result",null,["number"],[e]),ae(a,{types:i})}finally{d._free(e);}}getFlechetteType(t){switch(t){case h.BOOLEAN:return Mt();case h.TINYINT:return it();case h.SMALLINT:return ct();case h.INTEGER:return G();case h.BIGINT:return at();case h.UTINYINT:return Dt();case h.USMALLINT:return Ot();case h.UINTEGER:return Bt();case h.UBIGINT:return Ut();case h.FLOAT:return Pt();case h.DOUBLE:return rt();case h.VARCHAR:return $();case h.DATE:return vt();case h.TIMESTAMP:case h.TIMESTAMP_TZ:return ut(w.MICROSECOND);default:return $()}}async execute(t){if(this.closed||!d)throw new y("Connection is closed");let e=d._malloc(64);try{if(await d.ccall("duckdb_query","number",["number","string","number"],[this.connPtr,t,e],{async:!0})!==0){let s=d.ccall("duckdb_result_error","number",["number"],[e]),c=s?d.UTF8ToString(s):"Query failed";throw d.ccall("duckdb_destroy_result",null,["number"],[e]),new y(c,void 0,t)}let o=d.ccall("duckdb_rows_changed","number",["number"],[e]);return d.ccall("duckdb_destroy_result",null,["number"],[e]),o}finally{d._free(e);}}prepare(t){if(this.closed||!d)throw new y("Connection is closed");let e=d._malloc(4);try{if(d.ccall("duckdb_prepare","number",["number","string","number"],[this.connPtr,t,e])!==0){let s=d.getValue(e,"*");if(s){let c=d.ccall("duckdb_prepare_error","number",["number"],[s]),a=c?d.UTF8ToString(c):"Failed to prepare statement";throw d.ccall("duckdb_destroy_prepare",null,["number"],[s]),new y(a,void 0,t)}throw new y("Failed to prepare statement",void 0,t)}let o=d.getValue(e,"*");return new ln(o,this.connPtr,t)}finally{d._free(e);}}async beginTransaction(){await this.execute("BEGIN TRANSACTION");}async commit(){await this.execute("COMMIT");}async rollback(){await this.execute("ROLLBACK");}async transaction(t){await this.beginTransaction();try{let e=await t();return await this.commit(),e}catch(e){throw await this.rollback(),e}}async insertArrowFromIPCStream(t,e){if(this.closed||!d)throw new y("Connection is closed");let n=d._malloc(e.length);d.HEAPU8.set(e,n);try{if(await d.ccall("duckdb_wasm_insert_arrow_ipc","number",["number","string","number","number"],[this.connPtr,t,n,e.length],{async:!0})!==0)throw new y(`Failed to insert Arrow IPC data into "${t}"`)}finally{d._free(n);}}close(){if(this.closed||!d)return;let t=d._malloc(4);try{d.setValue(t,this.connPtr,"*"),d.ccall("duckdb_disconnect",null,["number"],[t]);}finally{d._free(t);}this.closed=true,this.connPtr=0;}};
|
|
3
3
|
export{yo as AccessMode,dn as Connection,fn as DataChunk,fs as DuckDB,y as DuckDBError,h as DuckDBType,ln as PreparedStatement,ls as StreamingResult,Io as checkSql,ll as init,ul as sanitizeSql,ae as tableFromArrays,Qn as tableFromIPC,is as tableToIPC,fl as version};//# sourceMappingURL=index.js.map
|
|
4
4
|
//# sourceMappingURL=index.js.map
|