databonk 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/parquet.cjs CHANGED
@@ -1,3 +1,3 @@
1
- 'use strict';var index_js=require('hyparquet/src/index.js'),index_js$1=require('hyparquet-writer/src/index.js');var b={f64:{name:"f64",size:8,view:"f64",ctor:Float64Array,wasm:"f64",float:true},f32:{name:"f32",size:4,view:"f32",ctor:Float32Array,wasm:"f32",float:true},i32:{name:"i32",size:4,view:"i32",ctor:Int32Array,wasm:"i32",float:false},u32:{name:"u32",size:4,view:"u32",ctor:Uint32Array,wasm:"u32",float:false},bool:{name:"bool",size:1,view:"bool",ctor:Uint8Array,wasm:"bool",float:false},utf8:{name:"utf8",size:4,view:"i32",ctor:Int32Array,wasm:"utf8",float:false},i64:{name:"i64",size:8,view:"i64",ctor:BigInt64Array,wasm:"i64",float:false},date32:{name:"date32",size:4,view:"i32",ctor:Int32Array,wasm:"i32",float:false},timestamp:{name:"timestamp",size:8,view:"i64",ctor:BigInt64Array,wasm:"i64",float:false}};function A(t){return t+7>>3}function D(t,e){return (t[e>>3]&1<<(e&7))!==0}function O(t,e){let n=e>>3;t[n]=(t[n]??0)|1<<(e&7);}var Ke=new TextEncoder,Ye=new TextDecoder,ne=new WeakMap;function We(t){let e=ne.get(t);return e===void 0&&(e={slots:new Array(t.count),stats:{hits:0,misses:0}},ne.set(t,e)),e}function Pt(t,e){let n=e.length,r=e.map(u=>Ke.encode(u)),o=0;for(let u of r)o+=u.length;let i=t.mod.alloc((n+1)*4),a=t.mod.alloc(o),s=t.viewOf({ptr:i,length:n+1,dtype:"i32"}),l=t.viewOf({ptr:a,length:o,dtype:"u8"});s[0]=0;let c=0;for(let u=0;u<n;u++){let p=r[u];l.set(p,c),c+=p.length,s[u+1]=c;}return {count:n,offsetsPtr:i,bytesPtr:a,bytesLen:o}}function j(t,e,n){let r=We(e),o=r.slots[n];if(o!==void 0)return r.stats.hits++,o;r.stats.misses++;let i=t.viewOf({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"}),a=i[n],s=i[n+1],l;if(s<=a)l="";else {let c=t.viewOf({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"});l=Ye.decode(c.subarray(a,s));}return r.slots[n]=l,l}function X(t,e){let n=new Array(e.count);for(let r=0;r<e.count;r++)n[r]=j(t,e,r);return n}function re(t,e,n){let r=X(t,e),o=X(t,n),i=[],a=new Map,s=u=>{let p=a.get(u);return p===void 0&&(p=i.length,i.push(u),a.set(u,p)),p},l=new Int32Array(r.length),c=new Int32Array(o.length);for(let u=0;u<r.length;u++)l[u]=s(r[u]);for(let u=0;u<o.length;u++)c[u]=s(o[u]);return {merged:i,remapA:l,remapB:c}}function oe(t,e){t.viewOf.forget({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"}),t.viewOf.forget({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"}),t.mod.free(e.offsetsPtr),t.mod.free(e.bytesPtr);}function W(t){return t==null}function H(t,e,n,r,o,i){return {dtype:t,length:e,dataPtr:n,validityPtr:r,validityBitOffset:0,dict:o,owned:true,...i!==void 0?{tz:i}:{}}}function v(t,e,n,r){return e==="utf8"?Qe(t,n):e==="bool"?Ge(t,n):e==="i64"?Ze(t,n):e==="timestamp"||e==="date32"?Xe(t,e,n,r):Je(t,e,n)}function He(t,e){switch(t){case "f64":return e instanceof Float64Array;case "f32":return e instanceof Float32Array;case "i32":return e instanceof Int32Array;case "u32":return e instanceof Uint32Array;default:return false}}function Je(t,e,n){let r=b[e],o=n.length;if(He(e,n)){let u=t.mod.alloc(o*r.size);return t.viewOf({ptr:u,length:o,dtype:r.view}).set(n),H(e,o,u,0,null)}let i=0;for(let u=0;u<o;u++)W(n[u])&&i++;let a=t.mod.alloc(o*r.size),s=i>0?t.mod.alloc(A(o)):0,l=t.viewOf({ptr:a,length:o,dtype:r.view}),c=null;s!==0&&(c=t.viewOf({ptr:s,length:A(o),dtype:"u8"}),c.fill(0));for(let u=0;u<o;u++){let p=n[u];W(p)?l[u]=0:(l[u]=p,c&&O(c,u));}return H(e,o,a,s,null)}function Ge(t,e){let n=e.length;if(e instanceof Uint8Array){let l=t.mod.alloc(n);return t.viewOf({ptr:l,length:n,dtype:"bool"}).set(e),H("bool",n,l,0,null)}let r=0;for(let l=0;l<n;l++)W(e[l])&&r++;let o=t.mod.alloc(n),i=r>0?t.mod.alloc(A(n)):0,a=t.viewOf({ptr:o,length:n,dtype:"bool"}),s=null;i!==0&&(s=t.viewOf({ptr:i,length:A(n),dtype:"u8"}),s.fill(0));for(let l=0;l<n;l++){let c=e[l];W(c)?a[l]=0:(a[l]=c?1:0,s&&O(s,l));}return H("bool",n,o,i,null)}function Ze(t,e){let n=e.length;if(e instanceof BigInt64Array){let l=t.mod.alloc(n*8);return t.viewOf({ptr:l,length:n,dtype:"i64"}).set(e),H("i64",n,l,0,null)}let r=0;for(let l=0;l<n;l++){let c=e[l];if(W(c)){r++;continue}if(typeof c=="number"){if(!Number.isInteger(c))throw new RangeError(`i64 column: non-integer number ${c} at index ${l} \u2014 use BigInt or null`);if(!Number.isSafeInteger(c))throw new RangeError(`i64 column: unsafe integer ${c} at index ${l} \u2014 use BigInt (e.g. ${c}n)`)}}let o=t.mod.alloc(n*8),i=r>0?t.mod.alloc(A(n)):0,a=t.viewOf({ptr:o,length:n,dtype:"i64"}),s=null;i!==0&&(s=t.viewOf({ptr:i,length:A(n),dtype:"u8"}),s.fill(0));for(let l=0;l<n;l++){let c=e[l];W(c)?a[l]=0n:(a[l]=typeof c=="bigint"?c:BigInt(c),s&&O(s,l));}return H("i64",n,o,i,null)}function Xe(t,e,n,r){let o=e==="timestamp",i=o?8:4,a=n.length,s=o?"i64":"i32";if(o?n instanceof BigInt64Array:n instanceof Int32Array){let m=t.mod.alloc(Math.max(a*i,1));return t.viewOf({ptr:m,length:a,dtype:s}).set(n),H(e,a,m,0,null,r)}let l=0;for(let m=0;m<a;m++)W(n[m])&&l++;let c=t.mod.alloc(Math.max(a*i,1)),u=l>0?t.mod.alloc(A(a)):0,p=t.viewOf({ptr:c,length:a,dtype:s}),f=null;u&&(f=t.viewOf({ptr:u,length:A(a),dtype:"u8"}),f.fill(0));for(let m=0;m<a;m++){let d=n[m];W(d)?p[m]=o?0n:0:(p[m]=o?typeof d=="bigint"?d:BigInt(d):d,f&&O(f,m));}return H(e,a,c,u,null,r)}function Qe(t,e){let n=e.length,r=new Map,o=[],i=new Int32Array(n),a=new Uint8Array(n),s=0;for(let f=0;f<n;f++){let m=e[f];if(W(m))s++,a[f]=1,i[f]=0;else {let d=m,y=r.get(d);y===void 0&&(y=o.length,o.push(d),r.set(d,y)),i[f]=y;}}let l=t.mod.alloc(n*4),c=s>0?t.mod.alloc(A(n)):0,u=Pt(t,o);if(t.viewOf({ptr:l,length:n,dtype:"i32"}).set(i),c!==0){let f=t.viewOf({ptr:c,length:A(n),dtype:"u8"});f.fill(0);for(let m=0;m<n;m++)a[m]===0&&O(f,m);}return H("utf8",n,l,c,u)}function z(t,e){let{length:n,validityPtr:r,validityBitOffset:o}=e,i=r===0?null:t.viewOf({ptr:r,length:A(o+n),dtype:"u8"}),a=new Array(n);if(e.dtype==="utf8"){let u=t.viewOf({ptr:e.dataPtr,length:n,dtype:"i32"}),p=e.dict;for(let f=0;f<n;f++)a[f]=i&&!D(i,o+f)?null:j(t,p,u[f]);return a}if(e.dtype==="i64"||e.dtype==="timestamp"){let u=t.viewOf({ptr:e.dataPtr,length:n,dtype:"i64"});for(let p=0;p<n;p++)a[p]=i&&!D(i,o+p)?null:u[p];return a}if(e.dtype==="date32"){let u=t.viewOf({ptr:e.dataPtr,length:n,dtype:"i32"});for(let p=0;p<n;p++)a[p]=i&&!D(i,o+p)?null:u[p];return a}let s=b[e.dtype],l=t.viewOf({ptr:e.dataPtr,length:n,dtype:s.view}),c=e.dtype==="bool";for(let u=0;u<n;u++)i&&!D(i,o+u)?a[u]=null:a[u]=c?l[u]!==0:l[u];return a}function Vt(t,e,n){let r=Math.max(0,Math.min(e,t.length)),i=Math.max(r,Math.min(n,t.length))-r,a=b[t.dtype].size;return {dtype:t.dtype,length:i,dataPtr:t.dataPtr+r*a,validityPtr:t.validityPtr,validityBitOffset:t.validityBitOffset+r,dict:t.dict,owned:false,...t.tz!==void 0?{tz:t.tz}:{}}}function Q(t,e){if(!e.owned)return;let n=b[e.dtype];t.viewOf.forget({ptr:e.dataPtr,length:e.length,dtype:n.view}),t.mod.free(e.dataPtr),e.validityPtr!==0&&(t.viewOf.forget({ptr:e.validityPtr,length:A(e.validityBitOffset+e.length),dtype:"u8"}),t.mod.free(e.validityPtr)),e.dict!==null&&oe(t,e.dict);}function pt(t){return t instanceof V?t:ie(t)}var V=class t{node;constructor(e){this.node=Object.freeze(e),Object.freeze(this);}add(e){return ct("add",this,e)}sub(e){return ct("sub",this,e)}mul(e){return ct("mul",this,e)}div(e){return ct("div",this,e)}mod(e){return ct("mod",this,e)}neg(){return new t({kind:"neg",operand:this})}gt(e){return rt("gt",this,e)}ge(e){return rt("ge",this,e)}lt(e){return rt("lt",this,e)}le(e){return rt("le",this,e)}eq(e){return rt("eq",this,e)}ne(e){return rt("ne",this,e)}and(e){return new t({kind:"bool",op:"and",left:this,right:pt(e)})}or(e){return new t({kind:"bool",op:"or",left:this,right:pt(e)})}not(){return new t({kind:"not",operand:this})}isNull(){return new t({kind:"isNull",operand:this})}notNull(){return this.isNull().not()}fillNull(e){return new t({kind:"fillNull",operand:this,value:e})}cast(e){return new t({kind:"cast",operand:this,to:e})}sum(){return q("sum",this)}mean(){return q("mean",this)}min(){return q("min",this)}max(){return q("max",this)}count(){return q("count",this)}nunique(){return q("nunique",this)}std(){return q("std",this)}var(){return q("var",this)}first(){return q("first",this)}last(){return q("last",this)}get dt(){return new Nt(this)}toString(){return R(this.node)}},Nt=class{constructor(e){this.operand=e;}operand;year(){return K("year",this.operand)}month(){return K("month",this.operand)}day(){return K("day",this.operand)}hour(){return K("hour",this.operand)}minute(){return K("minute",this.operand)}second(){return K("second",this.operand)}millisecond(){return K("millisecond",this.operand)}weekday(){return K("weekday",this.operand)}dayOfYear(){return K("dayOfYear",this.operand)}quarter(){return K("quarter",this.operand)}};function ie(t,e){return new V({kind:"lit",value:t,dtype:null})}function ct(t,e,n){return new V({kind:"arith",op:t,left:e,right:pt(n)})}function rt(t,e,n){return new V({kind:"compare",op:t,left:e,right:pt(n)})}function q(t,e){return new V({kind:"agg",op:t,operand:e})}function K(t,e){return new V({kind:"dt",component:t,operand:e})}var tn={add:"+",sub:"-",mul:"*",div:"/",mod:"%"};function Ft(t){return typeof t=="string"?JSON.stringify(t):String(t)}function R(t){switch(t.kind){case "col":return `col(${JSON.stringify(t.name)})`;case "lit":return t.dtype?`lit(${Ft(t.value)}, ${t.dtype})`:`lit(${Ft(t.value)})`;case "arith":return `(${R(t.left.node)} ${tn[t.op]} ${R(t.right.node)})`;case "neg":return `(-${R(t.operand.node)})`;case "compare":return `${R(t.left.node)}.${t.op}(${R(t.right.node)})`;case "bool":return `${R(t.left.node)}.${t.op}(${R(t.right.node)})`;case "not":return `${R(t.operand.node)}.not()`;case "isNull":return `${R(t.operand.node)}.isNull()`;case "fillNull":return `${R(t.operand.node)}.fillNull(${Ft(t.value)})`;case "cast":return `${R(t.operand.node)}.cast(${t.to})`;case "agg":return `${R(t.operand.node)}.${t.op}()`;case "dt":return `${R(t.operand.node)}.dt.${t.component}()`}}var S=class extends Error{constructor(e){super(e),this.name="ExprError";}};function tt(t,e,n,r){let o=r?` ${r}`:"";return new S(`dtype mismatch in '${t}': cannot combine ${e} and ${n}.${o}`)}function P(t,e,n){let r=n?` ${n}`:"";return new S(`operation '${t}' is not supported for dtype ${e}.${r}`)}function $t(t,e){return new S(`cast from ${t} to ${e} is not supported in v1`+(t==="utf8"||e==="utf8"?" (numeric\u2194utf8 conversion is not a kernel cast; use CSV inference / toString).":"."))}function Lt(t,e){let n=Et(t,e),r=n?` Did you mean '${n}'?`:"",o=e.length?` Known columns: ${e.map(i=>`'${i}'`).join(", ")}.`:"";return new S(`unknown column '${t}'.${r}${o}`)}function Z(t,e,n){return new S(`literal ${JSON.stringify(t)} is not a valid ${e} value for '${n}'.`)}function Et(t,e){let n=null,r=1/0;for(let i of e){let a=en(t,i);a<r&&(r=a,n=i);}let o=Math.max(2,Math.ceil(t.length/3));return n!==null&&r<=o?n:null}function en(t,e){let n=t.length,r=e.length;if(n===0)return r;if(r===0)return n;let o=new Array(r+1),i=new Array(r+1);for(let a=0;a<=r;a++)o[a]=a;for(let a=1;a<=n;a++){i[0]=a;let s=t.charCodeAt(a-1);for(let l=1;l<=r;l++){let c=s===e.charCodeAt(l-1)?0:1;i[l]=Math.min(o[l]+1,i[l-1]+1,o[l-1]+c);}[o,i]=[i,o];}return o[r]}var nn=new Set(["f64","f32","i32","u32","i64"]),ae=new Set(["i32","u32","i64"]),dt=new Set(["f64","f32"]),ot=new Set(["date32","timestamp"]);function J(t){return nn.has(t)}function Ut(t,e){return Number.isInteger(t)?e==="i32"?t>=-2147483648&&t<=2147483647:t>=0&&t<=4294967295:false}function It(t){let e=t.node;return e.kind==="lit"&&e.dtype===null&&typeof e.value=="number"?e.value:null}function M(t,e){return {kind:"lit",value:t,dtype:e}}function Tt(t,e){return t.dtype===e?t:{kind:"cast",dtype:e,from:t.dtype,operand:t}}function yt(t,e){return E(t,e)}function jt(t,e){return yt(t,e).dtype}function E(t,e){let n=t.node;switch(n.kind){case "col":{let r=e.dtypeOf(n.name);if(r===void 0)throw Lt(n.name,e.columnNames());return {kind:"col",name:n.name,dtype:r}}case "lit":return n.dtype!==null?M(n.value,n.dtype):typeof n.value=="bigint"?M(n.value,"i64"):typeof n.value=="number"?M(n.value,Number.isInteger(n.value)?"i32":"f64"):M(n.value,typeof n.value=="string"?"utf8":"bool");case "arith":return rn(n.op,n.left,n.right,e);case "neg":return ln(n.operand,e);case "compare":return cn(n.op,n.left,n.right,e);case "bool":return mn(n.op,n.left,n.right,e);case "not":return fn(n.operand,e);case "isNull":return {kind:"isNull",dtype:"bool",operand:E(n.operand,e)};case "fillNull":return dn(n.operand,n.value,e);case "cast":return gn(n.operand,n.to,e);case "agg":return wn(n.op,n.operand,e);case "dt":return un(n.component,n.operand,e)}}function rn(t,e,n,r){let o=It(e),i=It(n);if(o!==null&&i!==null){let c=Number.isInteger(o)?"i32":"f64",u=Number.isInteger(i)?"i32":"f64",p=c===u?c:"f64";return {kind:"arith",op:t,dtype:p,left:M(o,p),right:M(i,p)}}if(o!==null||i!==null){let c=o??i,u=E(o!==null?n:e,r),{opDtype:p,colTarget:f}=on(t,c,u.dtype),m=Tt(u,f),d=M(c,p);return {kind:"arith",op:t,dtype:p,left:o!==null?d:m,right:o!==null?m:d}}let a=E(e,r),s=E(n,r),l=an(t,a.dtype,s.dtype);return {kind:"arith",op:t,dtype:l,left:Tt(a,l),right:Tt(s,l)}}function on(t,e,n){if(n==="timestamp")return {opDtype:"timestamp",colTarget:"timestamp"};if(n==="date32")return {opDtype:"date32",colTarget:"date32"};if(!J(n))throw P(t,n,"arithmetic");return dt.has(n)?{opDtype:n,colTarget:n}:n==="i64"?Number.isSafeInteger(e)?{opDtype:"i64",colTarget:"i64"}:{opDtype:"f64",colTarget:"f64"}:Ut(e,n)?{opDtype:n,colTarget:n}:{opDtype:"f64",colTarget:"f64"}}function an(t,e,n){if(ot.has(e)||ot.has(n))return sn(t,e,n);if(!J(e))throw P(t,e,"arithmetic");if(!J(n))throw P(t,n,"arithmetic");if(e===n)return e;if(e==="i64"&&(n==="i32"||n==="u32")||n==="i64"&&(e==="i32"||e==="u32"))return "i64";if(e==="i64"&&dt.has(n)||n==="i64"&&dt.has(e)||(e==="i32"||e==="u32")&&n==="f64"||(n==="i32"||n==="u32")&&e==="f64")return "f64";if((e==="i32"||e==="u32")&&n==="f32"||(n==="i32"||n==="u32")&&e==="f32")return "f32";throw tt(t,e,n,"insert an explicit .cast() (only int\u2192float widening is implicit).")}function sn(t,e,n){if(t==="sub"&&e==="timestamp"&&n==="timestamp")return "i64";if((t==="add"||t==="sub")&&e==="timestamp"&&ae.has(n)||t==="add"&&ae.has(e)&&n==="timestamp")return "timestamp";if(t==="sub"&&e==="date32"&&n==="date32")return "i32";if((t==="add"||t==="sub")&&e==="date32"&&(n==="i32"||n==="u32")||t==="add"&&(e==="i32"||e==="u32")&&n==="date32")return "date32";throw new S(`${t}(${e},${n})`)}function ln(t,e){let n=E(t,e);if(!J(n.dtype))throw P("neg",n.dtype);return {kind:"neg",dtype:n.dtype,operand:n}}function un(t,e,n){let r=E(e,n);if(!ot.has(r.dtype))throw P("dt",r.dtype);return {kind:"dt",component:t,dtype:"i32",operand:r}}function cn(t,e,n,r){let o=t==="eq"||t==="ne",i=mt(e)??mt(n),a=ft(e)??ft(n),s=It(e),l=It(n);if(s!==null&&l!==null){let f=Number.isInteger(s)&&Number.isInteger(l)?"i32":"f64";return et(t,f,M(s,f),M(l,f))}if(s!==null||l!==null){let f=s??l,m=E(s!==null?n:e,r);if(m.dtype==="date32"){let h=M(f,"date32");return et(t,"date32",s!==null?h:m,s!==null?m:h)}if(m.dtype==="timestamp"){let h=M(f,"timestamp");return et(t,"timestamp",s!==null?h:m,s!==null?m:h)}if(!J(m.dtype))throw tt(t,s!==null?"f64":m.dtype,s!==null?m.dtype:"f64","cannot compare a number to a non-numeric column.");let d=pn(f,m.dtype),y=Tt(m,d),w=M(f,d);return et(t,d,s!==null?w:y,s!==null?y:w)}if(i!==null){let f=E(mt(e)!==null?n:e,r);if(f.dtype!=="utf8")throw tt(t,"utf8",f.dtype,"cannot compare a string to a non-utf8 column.");if(!o)throw P(t,"utf8","string ordering");let m=M(i,"utf8");return et(t,"utf8",mt(e)!==null?m:f,mt(e)!==null?f:m)}if(a!==null){let f=E(ft(e)!==null?n:e,r);if(f.dtype!=="bool")throw tt(t,"bool",f.dtype,"cannot compare a boolean to a non-bool column.");if(!o)throw P(t,"bool");let m=M(a,"bool");return et(t,"bool",ft(e)!==null?m:f,ft(e)!==null?f:m)}let c=E(e,r),u=E(n,r);if(c.dtype!==u.dtype)throw tt(t,c.dtype,u.dtype,"cast");let p=c.dtype;if(p==="utf8"||p==="bool")throw P(t,p,"column-vs-column");return et(t,p,c,u)}function et(t,e,n,r){return {kind:"compare",op:t,dtype:"bool",operandDtype:e,left:n,right:r}}function pn(t,e){return dt.has(e)?e:e==="i64"?Number.isSafeInteger(t)?"i64":"f64":Ut(t,e)?e:"f64"}function mt(t){let e=t.node;return e.kind==="lit"&&typeof e.value=="string"?e.value:null}function ft(t){let e=t.node;return e.kind==="lit"&&typeof e.value=="boolean"?e.value:null}function mn(t,e,n,r){let o=E(e,r),i=E(n,r);if(o.dtype!=="bool")throw P(t,o.dtype,"boolean");if(i.dtype!=="bool")throw P(t,i.dtype,"boolean");return {kind:"bool",op:t,dtype:"bool",left:o,right:i}}function fn(t,e){let n=E(t,e);if(n.dtype!=="bool")throw P("not",n.dtype,"boolean");return {kind:"not",dtype:"bool",operand:n}}function dn(t,e,n){let r=E(t,n);return yn(e,r.dtype),{kind:"fillNull",dtype:r.dtype,operand:r,value:e}}function yn(t,e){if(e==="utf8"){if(typeof t!="string")throw Z(t,e,"fillNull");return}if(e==="bool"){if(typeof t!="boolean")throw Z(t,e,"fillNull");return}if(e==="i64"||e==="timestamp"){if(typeof t!="bigint"&&typeof t!="number")throw Z(t,e,"fillNull");if(typeof t=="number"&&!Number.isSafeInteger(t))throw Z(t,e,"fillNull");return}if(typeof t!="number")throw Z(t,e,"fillNull");if((e==="i32"||e==="u32")&&!Ut(t,e))throw Z(t,e,"fillNull")}var hn={f64:new Set(["f64","f32","i32","u32","bool","i64"]),f32:new Set(["f64","f32","i32","u32","bool","i64"]),i32:new Set(["f64","f32","i32","u32","bool","i64","date32"]),u32:new Set(["f64","f32","i32","u32","bool","i64"]),bool:new Set(["f64","f32","i32","u32","bool","i64"]),utf8:new Set(["utf8"]),i64:new Set(["f64","f32","i32","u32","bool","i64","timestamp"]),date32:new Set(["i32","timestamp","date32"]),timestamp:new Set(["i64","date32","timestamp"])};function gn(t,e,n){let r=E(t,n),o=r.dtype;if(!hn[o]?.has(e))throw $t(o,e);return {kind:"cast",dtype:e,from:o,operand:r}}function wn(t,e,n){let r=E(e,n),o=ht(t,r.dtype);return {kind:"agg",op:t,dtype:o,operandDtype:r.dtype,operand:r}}function ht(t,e){switch(t){case "count":return "i32";case "nunique":if(J(e)||e==="utf8"||ot.has(e))return "i32";throw P("nunique",e);case "sum":if(dt.has(e))return e;if(e==="i64")return "i64";if(e==="i32"||e==="u32")return "f64";throw P("sum",e);case "mean":case "std":case "var":if(J(e))return "f64";throw P(t,e);case "min":case "max":if(J(e)||ot.has(e))return e;throw P(t,e);case "first":case "last":if(J(e)||e==="utf8"||ot.has(e))return e;throw P(t,e)}}function se(t,e,n){let r=t[e];if(typeof r!="function")throw new Error(`kernel export not found: ${e}`);return r(...n)}function le(t,e,n){let r=t[e];if(typeof r!="function")throw new Error(`kernel export not found: ${e}`);return r(...n)}function Ot(t){let e=0,n=0;for(let r of t.allocs)r.purpose==="data"?e++:r.purpose==="mask"&&n++;return {kernelCalls:t.kernels.length,kernels:t.kernels.slice(),allocations:t.allocs.length,dataAllocations:e,maskAllocations:n,frees:t.frees}}var gt=class{ctx;wasm;len;owned=new Set;trace={kernels:[],allocs:[],frees:0};viewed=[];constructor(e){this.ctx=e.ctx,this.wasm=e.wasm,this.len=e.length;}get validityBytes(){return A(this.len)}alloc(e,n){let r=this.ctx.mod.alloc(e);if(r===0)throw new Error(`out of wasm memory allocating ${e} bytes`);return this.owned.add(r),this.trace.allocs.push({bytes:e,purpose:n}),r}track(e,n,r){e!==0&&(this.owned.add(e),this.trace.allocs.push({bytes:n,purpose:r}));}free(e){e!==0&&this.owned.delete(e)&&(this.forgetViews(e),this.ctx.mod.free(e),this.trace.frees++);}transfer(e){this.owned.delete(e),this.forgetViews(e);}freeAll(){for(let e of this.viewed)this.ctx.viewOf.forget(e);this.viewed.length=0;for(let e of this.owned)this.ctx.mod.free(e),this.trace.frees++;this.owned.clear();}call(e,...n){return this.trace.kernels.push(e),se(this.wasm,e,n)}callBigInt(e,...n){return this.trace.kernels.push(e),le(this.wasm,e,n)}view(e,n,r){let o={ptr:e,length:n,dtype:r};return this.viewed.push(o),this.ctx.viewOf(o)}forgetViews(e){for(let n=this.viewed.length-1;n>=0;n--){let r=this.viewed[n];r.ptr===e&&(this.ctx.viewOf.forget(r),this.viewed.splice(n,1));}}},_={ptr:0,owns:false};function T(t,e){e.owns&&t.free(e.ptr);}function ue(t,e,n){if(e===0)return _;if((n&7)===0)return {ptr:e+(n>>3),owns:false};let r=t.validityBytes,o=t.alloc(r,"validity"),i=t.view(e,A(n+t.len),"u8"),a=t.view(o,r,"u8");a.fill(0);for(let s=0;s<t.len;s++)D(i,n+s)&&O(a,s);return {ptr:o,owns:true}}function ce(t,e,n){if(n===0)return 0;let r=t.view(e,A(n),"u8"),o=0;for(let i=0;i<n;i++)r[i>>3]>>(i&7)&1&&o++;return o}var zt=86400000n,bn=[0,31,59,90,120,151,181,212,243,273,304,334];function pe(t){let e=t+719468,n=Math.floor(e/146097),r=e-n*146097,o=Math.floor((r-Math.floor(r/1460)+Math.floor(r/36524)-Math.floor(r/146096))/365),i=o+n*400,a=r-(365*o+Math.floor(o/4)-Math.floor(o/100)),s=Math.floor((5*a+2)/153),l=a-Math.floor((153*s+2)/5)+1,c=s<10?s+3:s-9;return {year:i+(c<=2?1:0),month:c,day:l}}function me(t,e,n){let r=t-(e<=2?1:0),o=Math.floor(r/400),i=r-o*400,a=Math.floor((153*(e>2?e-3:e+9)+2)/5)+n-1,s=i*365+Math.floor(i/4)-Math.floor(i/100)+a;return o*146097+s-719468}function xn(t){return t%4===0&&t%100!==0||t%400===0}function Rt(t,e,n){let r=(bn[e-1]??0)+n;return e>2&&xn(t)?r+1:r}function St(t){return ((t+3)%7+7)%7+1}function An(t){let e=t/zt;return t%zt!==0n&&t<0n&&(e-=1n),Number(e)}function fe(t){let{year:e,month:n,day:r}=pe(t);return {year:e,month:n,day:r,hour:0,minute:0,second:0,millisecond:0,weekday:St(t),dayOfYear:Rt(e,n,r),quarter:Math.ceil(n/3)}}function de(t){let e=typeof t=="bigint"?t:BigInt(Math.trunc(t)),n=An(e),r=Number(e-BigInt(n)*zt),{year:o,month:i,day:a}=pe(n),s=Math.floor(r/36e5),l=r%36e5,c=Math.floor(l/6e4),u=l%6e4,p=Math.floor(u/1e3),f=u%1e3;return {year:o,month:i,day:a,hour:s,minute:c,second:p,millisecond:f,weekday:St(n),dayOfYear:Rt(o,i,a),quarter:Math.ceil(i/3)}}var ye=new Map;function vn(t){let e=ye.get(t);if(e!==void 0)return e;let n;try{n=new Intl.DateTimeFormat("en",{timeZone:t,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric",hourCycle:"h23"});}catch(r){throw new RangeError(`Invalid IANA timezone: "${t}". ${r instanceof Error?r.message:String(r)}`)}return ye.set(t,n),n}function he(t,e){let r=vn(e).formatToParts(t),o=0,i=0,a=0,s=0,l=0,c=0;for(let f of r)switch(f.type){case "year":o=parseInt(f.value,10);break;case "month":i=parseInt(f.value,10);break;case "day":a=parseInt(f.value,10);break;case "hour":s=parseInt(f.value,10);break;case "minute":l=parseInt(f.value,10);break;case "second":c=parseInt(f.value,10);break}let u=(t%1e3+1e3)%1e3,p=me(o,i,a);return {year:o,month:i,day:a,hour:s,minute:l,second:c,millisecond:u,weekday:St(p),dayOfYear:Rt(o,i,a),quarter:Math.ceil(i/3)}}function Dn(t,e){return ((t[e>>3]??0)&1<<(e&7))!==0}function it(t,e,n,r){let o=t.length,i=new Int32Array(o),a=t instanceof Int32Array,s=typeof r=="string"&&r.length>0;for(let l=0;l<o;l++){if(e!==null&&!Dn(e,l))continue;let c;if(a)c=fe(t[l]??0);else if(s){let u=Number(t[l]??0n);c=he(u,r);}else {let u=t[l]??0n;c=de(u);}i[l]=c[n];}return {data:i,validity:e}}function xt(t,e){let n=yt(t,e),r=n.kind==="agg"?"scalar":"column";return {dtype:n.dtype,resultKind:r,execute(){let o=new gt(e);try{if(n.kind==="agg")return {kind:"scalar",scalar:Ht(o,e,n),stats:Ot(o.trace)};let i=U(o,e,n);return {kind:"column",column:ve(o,i),stats:Ot(o.trace)}}finally{o.freeAll();}}}}function Wt(t,e){let n=yt(t,e);if(n.dtype!=="bool")throw new S(`filter predicate must be boolean, got ${n.dtype}. Use a comparison / boolean expression.`);return {execute(){let r=new gt(e),o=qn(r,U(r,e,n)),i;if(o.validity.ptr===0)i=o.maskPtr;else if(o.ownsMask)r.call("validity_and",o.maskPtr,o.validity.ptr,o.maskPtr,r.len),T(r,o.validity),i=o.maskPtr;else {let s=r.alloc(r.validityBytes,"mask");r.call("validity_and",o.maskPtr,o.validity.ptr,s,r.len),T(r,o.validity),i=s;}let a=ce(r,i,r.len);return Cn(r,e,i,a)}}}function Cn(t,e,n,r){let o=null,i=()=>{if(o)return o;let a=new Int32Array(r),s=t.view(n,A(t.len),"u8"),l=0;for(let c=0;c<t.len;c++)s[c>>3]>>(c&7)&1&&(a[l++]=c);return o=a,a};return {count:r,compact(a){let s=b[a.dtype],l=e.ctx.mod.alloc(Math.max(r*s.size,1)),c=t.call(`filter_${Kn(a.dtype)}`,a.dataPtr,n,l,t.len);if(c!==r)throw new Error(`filter count mismatch: kernel ${c}, expected ${r}`);let u=0;if(a.validityPtr!==0){let f=i(),m=t.view(a.validityPtr,A(a.validityBitOffset+t.len),"u8"),d=0,y=A(r),w=e.ctx.mod.alloc(Math.max(y,1)),h=t.view(w,y,"u8");h.fill(0);for(let g=0;g<r;g++)D(m,a.validityBitOffset+f[g])?O(h,g):d++;d===0?(e.ctx.viewOf.forget({ptr:w,length:y,dtype:"u8"}),e.ctx.mod.free(w)):u=w;}let p=a.dict?De(e.ctx,a.dict):null;return {dtype:a.dtype,length:r,dataPtr:l,validityPtr:u,validityBitOffset:0,dict:p,owned:true}},free(){t.freeAll();},get stats(){return Ot(t.trace)}}}var kn={gt:"lt",lt:"gt",ge:"le",le:"ge",eq:"eq",ne:"ne"},Pn=new Set(["add","mul"]),En=new Set(["f64_i32","f64_u32","f64_bool","f32_i32","f32_u32","f32_bool","i32_u32","u32_i32","f64_i64","f32_i64"]),qt=86400000n;function U(t,e,n){switch(n.kind){case "col":return Tn(t,e,n.name,n.dtype);case "lit":return Mt(t,n.value,n.dtype);case "arith":return In(t,e,n);case "neg":return Rn(t,e,n.dtype,n.operand);case "compare":return Sn(t,e,n);case "bool":return Bn(t,e,n.op,n.left,n.right);case "not":return Vn(t,e,n.operand);case "isNull":return Fn(t,e,n.operand);case "fillNull":return Nn(t,e,n.dtype,n.operand,n.value);case "cast":return Un(t,e,n.from,n.dtype,n.operand);case "agg":{let r=Ht(t,e,n);return Mt(t,r.value,n.dtype)}case "dt":return jn(t,e,n.component,n.operand)}}function Tn(t,e,n,r){let o=e.getColumn(n);if(!o)throw new S(`column '${n}' vanished during execution`);let i=ue(t,o.validityPtr,o.validityBitOffset);return r==="bool"?{rep:"boolcol",dataPtr:o.dataPtr,ownsData:false,validity:i}:{rep:"column",dtype:r,dataPtr:o.dataPtr,ownsData:false,validity:i,dict:o.dict,ownsDict:false}}function N(t,e,n){let r=U(t,e,n);if(r.rep!=="column")throw new Error(`internal: expected numeric column, got ${r.rep}`);return r}function wt(t){return t.kind==="lit"||t.kind==="agg"||t.kind==="cast"&&wt(t.operand)}function In(t,e,n){let r=n.dtype,o=wt(n.left),i=wt(n.right);if(o&&i){let l=Mt(t,Y(t,e,n.left),r);return Kt(t,e,n.op,l,Y(t,e,n.right),r)}if(i){let l=N(t,e,n.left);return Kt(t,e,n.op,l,Y(t,e,n.right),r)}if(o){let l=N(t,e,n.right);return On(t,e,n.op,Y(t,e,n.left),l,r)}let a=N(t,e,n.left),s=N(t,e,n.right);return be(t,e,n.op,a,s,r)}function Kt(t,e,n,r,o,i){let a=b[i].size,s=r.ownsData?r.dataPtr:t.alloc(t.len*a,"data");if(o===null||(n==="div"||n==="mod")&&((i==="i32"||i==="u32")&&o===0||i==="i64"&&o===0n))return T(t,r.validity),Jn(t,s,t.len*a),$(i,s,nt(t),null,false);let c=b[i].wasm;if(c==="i64"){let u=typeof o=="bigint"?o:BigInt(o);t.callBigInt(`${n}_i64_scalar`,r.dataPtr,u,s,t.len);}else t.call(`${n}_${c}_scalar`,r.dataPtr,o,s,t.len);return $(i,s,r.validity,null,false)}function On(t,e,n,r,o,i){if(Pn.has(n))return Kt(t,e,n,o,r,i);let a=Mt(t,r,i);return be(t,e,n,a,o,i)}function be(t,e,n,r,o,i){let a=b[i].size,s=(n==="div"||n==="mod")&&(i==="i32"||i==="u32"||i==="i64"),l=_;if(s){let f=t.alloc(t.validityBytes,"mask");i==="i64"?t.callBigInt("ne_i64_scalar_mask",o.dataPtr,0n,f,t.len):t.call(`ne_${i}_scalar_mask`,o.dataPtr,0,f,t.len),l={ptr:f,owns:true};}let c=r.ownsData?r.dataPtr:o.ownsData?o.dataPtr:t.alloc(t.len*a,"data"),u=b[i].wasm;t.call(`${n}_${u}`,r.dataPtr,o.dataPtr,c,t.len),c!==r.dataPtr&&r.ownsData&&t.free(r.dataPtr),c!==o.dataPtr&&o.ownsData&&t.free(o.dataPtr);let p=Yt(t,r.validity,o.validity);return s&&(p=Yt(t,p,l)),$(i,c,p,null,false)}function Rn(t,e,n,r){let o=N(t,e,r),i=b[n].size,a=o.ownsData?o.dataPtr:t.alloc(t.len*i,"data");return t.call(`neg_${n}`,o.dataPtr,a,t.len),$(n,a,o.validity,null,false)}function Sn(t,e,n){let r=n.operandDtype,o=wt(n.left),i=wt(n.right);if(o&&i){let u=Y(t,e,n.left),p=Y(t,e,n.right);return Hn(t,Wn(n.op,u,p))}if(r==="bool")return _n(t,e,n,o);if(r==="utf8")return Mn(t,e,n,o);let a=b[r].wasm,s=t.alloc(t.validityBytes,"mask");if(o||i){let u=N(t,e,o?n.right:n.left),p=Y(t,e,o?n.left:n.right);if(p===null)return T(t,u.validity),t.free(u.ownsData?u.dataPtr:0),t.view(s,t.validityBytes,"u8").fill(0),{rep:"mask",maskPtr:s,ownsMask:true,validity:nt(t)};let f=o?kn[n.op]:n.op;if(a==="i64"){let m=typeof p=="bigint"?p:BigInt(p);t.callBigInt(`${f}_i64_scalar_mask`,u.dataPtr,m,s,t.len);}else t.call(`${f}_${a}_scalar_mask`,u.dataPtr,p,s,t.len);return u.ownsData&&t.free(u.dataPtr),{rep:"mask",maskPtr:s,ownsMask:true,validity:u.validity}}let l=N(t,e,n.left),c=N(t,e,n.right);return t.call(`${n.op}_${a}_mask`,l.dataPtr,c.dataPtr,s,t.len),l.ownsData&&t.free(l.dataPtr),c.ownsData&&t.free(c.dataPtr),{rep:"mask",maskPtr:s,ownsMask:true,validity:Yt(t,l.validity,c.validity)}}function Mn(t,e,n,r){let o=N(t,e,r?n.right:n.left),i=Y(t,e,r?n.left:n.right),a=t.alloc(t.validityBytes,"mask"),s=t.view(a,t.validityBytes,"u8");if(i===null)return s.fill(0),o.ownsData&&t.free(o.dataPtr),T(t,o.validity),o.ownsDict&&bt(e,o.dict),{rep:"mask",maskPtr:a,ownsMask:true,validity:nt(t)};let c=(o.dict?X(e.ctx,o.dict):[]).indexOf(i);return c>=0?t.call(`${n.op}_i32_scalar_mask`,o.dataPtr,c,a,t.len):s.fill(n.op==="ne"?255:0),o.ownsData&&t.free(o.dataPtr),o.ownsDict&&bt(e,o.dict),{rep:"mask",maskPtr:a,ownsMask:true,validity:o.validity}}function _n(t,e,n,r){let o=Y(t,e,r?n.left:n.right),i=at(t,U(t,e,r?n.right:n.left));return (n.op==="eq"?o===false:o===true)?xe(t,i):i}function Bn(t,e,n,r,o){let i=at(t,U(t,e,r)),a=at(t,U(t,e,o)),s=n==="and"?"and_kleene":"or_kleene",l=i.ownsData?i.dataPtr:a.ownsData?a.dataPtr:t.alloc(t.len,"data"),c=i.validity.ptr===0&&a.validity.ptr===0,u;if(c){let p=t.alloc(t.validityBytes,"scratch");t.call(s,i.dataPtr,0,a.dataPtr,0,l,p,t.len),t.free(p),u=_;}else {let p=t.alloc(t.validityBytes,"validity");t.call(s,i.dataPtr,i.validity.ptr,a.dataPtr,a.validity.ptr,l,p,t.len),T(t,i.validity),T(t,a.validity),u={ptr:p,owns:true};}return l!==i.dataPtr&&i.ownsData&&t.free(i.dataPtr),l!==a.dataPtr&&a.ownsData&&t.free(a.dataPtr),{rep:"boolcol",dataPtr:l,ownsData:true,validity:u}}function Vn(t,e,n){return xe(t,at(t,U(t,e,n)))}function xe(t,e){let n=e.ownsData?e.dataPtr:t.alloc(t.len,"data"),r;if(e.validity.ptr===0){let o=t.alloc(t.validityBytes,"scratch");t.call("not_bool",e.dataPtr,0,n,o,t.len),t.free(o),r=_;}else {let o=t.alloc(t.validityBytes,"validity");t.call("not_bool",e.dataPtr,e.validity.ptr,n,o,t.len),T(t,e.validity),r={ptr:o,owns:true};}return n!==e.dataPtr&&e.ownsData&&t.free(e.dataPtr),{rep:"boolcol",dataPtr:n,ownsData:true,validity:r}}function Fn(t,e,n){let r=U(t,e,n),o=Jt(r),i=t.alloc(t.len,"data");return t.call("is_null",o.ptr,i,t.len),Ae(t,e,r),{rep:"boolcol",dataPtr:i,ownsData:true,validity:_}}function Nn(t,e,n,r,o){if(n==="bool")return $n(t,e,r,o);if(n==="utf8")return Ln(t,e,r,o);let i=N(t,e,r);if(i.validity.ptr===0)return i;let a=b[n].size,s=i.ownsData?i.dataPtr:t.alloc(t.len*a,"data"),l=b[n].wasm;if(l==="i64"){let c=typeof o=="bigint"?o:BigInt(o);t.callBigInt("fill_null_i64",i.dataPtr,i.validity.ptr,c,s,t.len);}else t.call(`fill_null_${l}`,i.dataPtr,i.validity.ptr,o,s,t.len);return T(t,i.validity),$(n,s,_,null,false)}function $n(t,e,n,r){let o=at(t,U(t,e,n));if(o.validity.ptr===0)return o;let i=o.ownsData?o.dataPtr:t.alloc(t.len,"data"),a=t.view(o.dataPtr,t.len,"u8"),s=t.view(o.validity.ptr,t.validityBytes,"u8"),l=t.view(i,t.len,"u8"),c=r?1:0;for(let u=0;u<t.len;u++)l[u]=D(s,u)?a[u]:c;return T(t,o.validity),{rep:"boolcol",dataPtr:i,ownsData:true,validity:_}}function Ln(t,e,n,r){let o=N(t,e,n);if(o.validity.ptr===0)return o;let i=o.dict?X(e.ctx,o.dict):[],a=i.indexOf(r),s=o.dict,l=o.ownsDict;a<0&&(a=i.length,s=Pt(e.ctx,[...i,r]),t.track(s.offsetsPtr,(s.count+1)*4,"dict"),t.track(s.bytesPtr,Math.max(s.bytesLen,1),"dict"),o.ownsDict&&bt(e,o.dict),l=true);let c=o.ownsData?o.dataPtr:t.alloc(t.len*4,"data"),u=t.view(o.dataPtr,t.len,"i32"),p=t.view(o.validity.ptr,t.validityBytes,"u8"),f=t.view(c,t.len,"i32");for(let m=0;m<t.len;m++)f[m]=D(p,m)?u[m]:a;return T(t,o.validity),$("utf8",c,_,s,l)}function Un(t,e,n,r,o){let i=U(t,e,o);if(n===r)return i;let a=(i.rep==="boolcol",i.dataPtr),s=(i.rep==="boolcol",i.ownsData),l=Jt(i);if(n==="date32"&&r==="i32"||n==="i32"&&r==="date32"||n==="timestamp"&&r==="i64"||n==="i64"&&r==="timestamp")return {...i,dtype:r};if(n==="date32"&&r==="timestamp"){let d=t.alloc(t.len*8,"data"),y=t.alloc(t.validityBytes,"scratch");return t.call("cast_i32_i64",a,l.ptr,d,y,t.len),t.free(y),t.callBigInt("mul_i64_scalar",d,qt,d,t.len),s&&t.free(a),$("timestamp",d,l,null,false)}if(n==="timestamp"&&r==="date32"){let d=t.view(a,t.len,"i64"),y=t.alloc(t.len*4,"data"),w=t.view(y,t.len,"i32");for(let h=0;h<t.len;h++){let g=d[h],x=g/qt;g%qt!==0n&&g<0n&&(x-=1n),w[h]=Number(x);}return s&&t.free(a),$("date32",y,l,null,false)}let c=b[r].size,p=s&&b[n].size===c?a:t.alloc(t.len*c,"data"),f=En.has(`${n}_${r}`),m;if(f){let d=t.alloc(t.validityBytes,"validity");t.call(`cast_${n}_${r}`,a,l.ptr,p,d,t.len),T(t,l),m={ptr:d,owns:true};}else {let d=t.alloc(t.validityBytes,"scratch");t.call(`cast_${n}_${r}`,a,l.ptr,p,d,t.len),t.free(d),m=l;}return p!==a&&s&&t.free(a),r==="bool"?{rep:"boolcol",dataPtr:p,ownsData:true,validity:m}:$(r,p,m,null,false)}function jn(t,e,n,r){let o=N(t,e,r),i=t.len,a;if(r.kind==="col"){let p=e.getColumn(r.name);p?.tz&&(a=p.tz);}let s=null;o.validity.ptr!==0&&(s=t.view(o.validity.ptr,t.validityBytes,"u8"));let l;if(o.dtype==="date32"){let p=t.view(o.dataPtr,i,"i32");l=it(p,s,n);}else {let p=t.view(o.dataPtr,i,"i64");l=it(p,s,n,a);}let c=t.alloc(i*4,"data");return t.view(c,i,"i32").set(l.data),o.ownsData&&t.free(o.dataPtr),o.ownsDict&&o.dict&&bt(e,o.dict),$("i32",c,o.validity,null,false)}function Ht(t,e,n){let r=U(t,e,n.operand),o=Jt(r),i=r.rep==="mask"?0:(r.rep==="boolcol",r.dataPtr),a=r.rep==="column"?r.dict:null,s=n.operandDtype,l=s==="utf8"?"i32":s,c=t.len,u=o.ptr===0?c:t.call("count_null",o.ptr,c),p=zn(t,e,n.op,s,l,i,o.ptr,c,u,a);return Ae(t,e,r),{value:p,dtype:n.dtype}}function zn(t,e,n,r,o,i,a,s,l,c){let u=r==="utf8"?"i32":b[r].wasm,p=f=>o==="u32"?f>>>0:f;switch(n){case "count":return l;case "nunique":return t.call(`nunique_${u}_null`,i,a,s);case "sum":return u==="i64"?t.callBigInt("sum_i64_null",i,a,s):t.call(`sum_${r}_null`,i,a,s);case "mean":return l>=1?t.call(`mean_${r}_null`,i,a,s):null;case "std":return l>=2?t.call(`std_${r}_null`,i,a,s):null;case "var":return l>=2?t.call(`var_${r}_null`,i,a,s):null;case "min":case "max":return l?u==="i64"?t.callBigInt(`${n}_i64_null`,i,a,s):p(t.call(`${n}_${u}_null`,i,a,s)):null;case "first":case "last":{let f=t.alloc(4,"scratch");t.view(f,1,"i32")[0]=0;let m;u==="i64"?m=t.callBigInt(`${n}_i64_null`,i,a,s,f):m=t.call(`${n}_${u}_null`,i,a,s,f);let d=t.view(f,1,"i32")[0]!==0;return t.free(f),d?u==="i64"?m:r==="utf8"?j(e.ctx,c,m):p(m):null}}}function at(t,e){if(e.rep==="boolcol")return e;if(e.rep==="mask"){let n=t.alloc(t.len,"data");return t.call("expand_mask_bool",e.maskPtr,n,t.len),e.ownsMask&&t.free(e.maskPtr),{rep:"boolcol",dataPtr:n,ownsData:true,validity:e.validity}}throw new Error("internal: expected a boolean value")}function qn(t,e){if(e.rep==="mask")return e;if(e.rep==="boolcol"){let n=t.alloc(t.validityBytes,"mask"),r=t.view(e.dataPtr,t.len,"u8"),o=t.view(n,t.validityBytes,"u8");o.fill(0);for(let i=0;i<t.len;i++)r[i]&&O(o,i);return e.ownsData&&t.free(e.dataPtr),{rep:"mask",maskPtr:n,ownsMask:true,validity:e.validity}}throw new Error("internal: expected a boolean value")}function Jt(t){return t.validity}function Kn(t){return t==="bool"?"u8":t==="utf8"?"i32":b[t].wasm}function Y(t,e,n){if(n.kind==="lit")return n.dtype==="i64"&&typeof n.value=="number"?BigInt(n.value):n.value;if(n.kind==="agg")return Ht(t,e,n).value;if(n.kind==="cast")return Yn(Y(t,e,n.operand),n.from,n.dtype);throw new Error(`internal: ${n.kind} is not a scalar operand`)}var ge=9223372036854776e3;function Yn(t,e,n){if(t===null)return null;if(e===n)return t;if(e==="i64"){let i=t;return n==="f64"?Number(i):n==="f32"?Math.fround(Number(i)):n==="i32"?Number(BigInt.asIntN(32,i)):n==="u32"?Number(BigInt.asUintN(32,i)):n==="bool"?i!==0n:null}if(n==="i64"){if(typeof t=="boolean")return t?1n:0n;let i=t;if(!Number.isFinite(i))return null;let a=Math.trunc(i);return a>=ge||a<-ge?null:BigInt(a)}if(n==="bool")return typeof t=="number"&&Number.isNaN(t)?null:t!==0&&t!==false;let r=typeof t=="boolean"?t?1:0:t;if(n==="f64")return r;if(n==="f32")return Math.fround(r);if(!Number.isFinite(r))return null;let o=Math.trunc(r);return n==="i32"?o>=-2147483648&&o<=2147483647?o:null:r<0?null:o<=4294967295?o:null}function Wn(t,e,n){if(e===null||n===null)return null;let r=e,o=n;switch(t){case "gt":return r>o;case "ge":return r>=o;case "lt":return r<o;case "le":return r<=o;case "eq":return r===o;case "ne":return r!==o}}function Mt(t,e,n){if(n==="utf8")throw new S("broadcasting a utf8 scalar to a column is not supported in P3.1.");if(n==="bool"){let s=t.alloc(t.len,"data");return t.view(s,t.len,"u8").fill(e===null?0:e?1:0),{rep:"boolcol",dataPtr:s,ownsData:true,validity:e===null?nt(t):_}}let r=b[n].size,o=t.alloc(t.len*r,"data");if(n==="i64"||n==="timestamp"){let s=t.view(o,t.len,"i64"),l=e===null?0n:typeof e=="bigint"?e:BigInt(e);for(let c=0;c<t.len;c++)s[c]=l;return $(n,o,e===null?nt(t):_,null,false)}let i=t.view(o,t.len,b[n].view),a=e===null?0:e;for(let s=0;s<t.len;s++)i[s]=a;return $(n,o,e===null?nt(t):_,null,false)}function Hn(t,e){let n=t.alloc(t.validityBytes,"mask");return t.view(n,t.validityBytes,"u8").fill(e===true?255:0),{rep:"mask",maskPtr:n,ownsMask:true,validity:e===null?nt(t):_}}function Yt(t,e,n){if(e.ptr===0&&n.ptr===0)return _;if(n.ptr===0)return e;if(e.ptr===0)return n;let r=t.alloc(t.validityBytes,"validity");return t.call("validity_and",e.ptr,n.ptr,r,t.len),T(t,e),T(t,n),{ptr:r,owns:true}}function nt(t){let e=t.alloc(t.validityBytes,"validity");return t.view(e,t.validityBytes,"u8").fill(0),{ptr:e,owns:true}}function Jn(t,e,n){t.view(e,n,"u8").fill(0);}function $(t,e,n,r,o){return {rep:"column",dtype:t,dataPtr:e,ownsData:true,validity:n,dict:r,ownsDict:o}}function Ae(t,e,n){if(n.rep==="mask"){n.ownsMask&&t.free(n.maskPtr),T(t,n.validity);return}if(n.rep==="boolcol"){n.ownsData&&t.free(n.dataPtr),T(t,n.validity);return}n.ownsData&&t.free(n.dataPtr),T(t,n.validity),n.ownsDict&&bt(e,n.dict);}function bt(t,e){e&&(t.ctx.viewOf.forget({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"}),t.ctx.viewOf.forget({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"}),t.ctx.mod.free(e.offsetsPtr),t.ctx.mod.free(e.bytesPtr));}function ve(t,e){if(e.rep==="mask")return ve(t,at(t,e));let n=t.ctx,r=e.rep==="boolcol"?"bool":e.dtype,o=b[r],i;e.ownsData?(i=e.dataPtr,t.transfer(e.dataPtr)):(i=n.mod.alloc(Math.max(t.len*o.size,1)),we(t,e.dataPtr,i,t.len*o.size));let a=e.validity,s=0;a.ptr!==0&&(a.owns?(s=a.ptr,t.transfer(a.ptr)):(s=n.mod.alloc(Math.max(t.validityBytes,1)),we(t,a.ptr,s,t.validityBytes)));let l=null;return e.rep==="column"&&e.dict&&(e.ownsDict?(l=e.dict,t.transfer(e.dict.offsetsPtr),t.transfer(e.dict.bytesPtr)):l=De(n,e.dict)),{dtype:r,length:t.len,dataPtr:i,validityPtr:s,validityBitOffset:0,dict:l,owned:true}}function we(t,e,n,r){if(r<=0)return;let o=t.view(e,r,"u8");t.view(n,r,"u8").set(o);}function De(t,e){let n=(e.count+1)*4,r=t.mod.alloc(Math.max(n,1)),o=t.mod.alloc(Math.max(e.bytesLen,1));return t.viewOf({ptr:r,length:e.count+1,dtype:"i32"}).set(t.viewOf({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"})),e.bytesLen>0&&t.viewOf({ptr:o,length:e.bytesLen,dtype:"u8"}).set(t.viewOf({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"})),{count:e.count,offsetsPtr:r,bytesPtr:o,bytesLen:e.bytesLen}}var I=class extends Error{constructor(e){super(e),this.name="FrameError";}};function G(t,e){let n=Et(t,e),r=n?` Did you mean '${n}'?`:"",o=e.length?` Available columns: ${e.map(i=>`'${i}'`).join(", ")}.`:"";return new I(`unknown column '${t}'.${r}${o}`)}function Ce(t,e,n,r){let o=r?` ${r}`:"";return new I(`dtype mismatch in '${t}': ${e} vs ${n}.${o}`)}function B(t,e){let n=t[e];if(typeof n!="function")throw new I(`kernel export not found: ${e}`);return n}typeof process<"u"&&process.versions!=null&&process.versions.node!=null;function Gt(){throw new I("no DataFrame runtime is loaded \u2014 call `await init()` first (or pass a runtime).");}var At=class{col;ctx;refs;constructor(e,n){this.ctx=e,this.col=n,this.refs=1;}retain(){return this.refs++,this}release(){this.refs<=0||--this.refs===0&&Q(this.ctx,this.col);}};function Zt(t){if(t instanceof BigInt64Array)return "i64";if(t instanceof Float64Array)return "f64";if(t instanceof Float32Array)return "f32";if(t instanceof Int32Array)return "i32";if(t instanceof Uint32Array)return "u32";if(t instanceof Uint8Array)return "bool";let e=t;for(let n=0;n<e.length;n++){let r=e[n];if(r!=null){if(typeof r=="bigint")return "i64";if(typeof r=="number")return "f64";if(typeof r=="boolean")return "bool";if(typeof r=="string")return "utf8"}}return "f64"}var Gn=new Set(["year","month","day","weekday","dayOfYear","quarter"]),Xt=class{constructor(e){this.series=e;}series;extract(e){let{dtype:n}=this.series;if(n==="date32"){if(!Gn.has(e))throw new TypeError(`dt.${e} is not supported for date32 columns (time-of-day fields are always 0 for date32; use year/month/day/weekday/dayOfYear/quarter, or cast to timestamp first).`)}else if(n!=="timestamp")throw new TypeError(`dt accessor requires a date32 or timestamp column, got ${n}.`);let r=this.series.col,o=this.series.ctx,i=r.length,a=n==="timestamp"&&r.tz?r.tz:void 0,s=null;if(r.validityPtr!==0){let p=r.validityBitOffset,f=Math.ceil((p+i)/8),m=o.viewOf({ptr:r.validityPtr,length:f,dtype:"u8"});if(p===0)s=m;else {let d=new Uint8Array(Math.ceil(i/8));for(let y=0;y<i;y++){let w=p+y;m[w>>3]>>(w&7)&1&&(d[y>>3]|=1<<(y&7));}s=d;}}let l;if(n==="date32"){let p=o.viewOf({ptr:r.dataPtr,length:i,dtype:"i32"});({data:l}=it(p,s,e));}else {let p=o.viewOf({ptr:r.dataPtr,length:i,dtype:"i64"});({data:l}=it(p,s,e,a));}let c=new Array(i);if(s!==null)for(let p=0;p<i;p++){let f=(s[p>>3]??0)>>(p&7)&1;c[p]=f?l[p]??0:null;}else for(let p=0;p<i;p++)c[p]=l[p]??0;let u=v(o,"i32",c);return new vt(o,`${this.series.name}.dt.${e}`,u)}year(){return this.extract("year")}month(){return this.extract("month")}day(){return this.extract("day")}hour(){return this.extract("hour")}minute(){return this.extract("minute")}second(){return this.extract("second")}millisecond(){return this.extract("millisecond")}weekday(){return this.extract("weekday")}dayOfYear(){return this.extract("dayOfYear")}quarter(){return this.extract("quarter")}},vt=class{name;dtype;length;ctx;column;constructor(e,n,r){this.ctx=e,this.name=n,this.dtype=r.dtype,this.length=r.length,this.column=r;}get col(){return this.column}toArray(){return z(this.ctx,this.column)}get(e){return e<0||e>=this.length?null:this.toArray()[e]??null}values(){return this.ctx.viewOf({ptr:this.column.dataPtr,length:this.length,dtype:b[this.dtype].view})}get dt(){if(this.dtype!=="date32"&&this.dtype!=="timestamp")throw new TypeError(`dt accessor requires a date32 or timestamp column, got ${this.dtype}.`);return new Xt(this)}[Symbol.iterator](){return this.toArray()[Symbol.iterator]()}toString(){return `Series '${this.name}' (${this.dtype}, ${this.length} rows)`}};function Pe(t){return t==="bool"?"u8":t==="utf8"?"i32":b[t].wasm}function st(t,e){if(e.validityPtr===0)return {ptr:0,owns:false};let n=e.validityBitOffset;if((n&7)===0)return {ptr:e.validityPtr+(n>>3),owns:false};let r=e.length,o=A(r),i=t.mod.alloc(Math.max(o,1)),a=t.viewOf({ptr:e.validityPtr,length:A(n+r),dtype:"u8"}),s=t.viewOf({ptr:i,length:o,dtype:"u8"});s.fill(0);for(let l=0;l<r;l++)D(a,n+l)&&O(s,l);return {ptr:i,owns:true}}function lt(t,e){e.owns&&e.ptr!==0&&(t.viewOf.forget({ptr:e.ptr,length:1,dtype:"u8"}),t.mod.free(e.ptr));}function Ee(t,e){let n=(e.count+1)*4,r=t.mod.alloc(Math.max(n,1)),o=t.mod.alloc(Math.max(e.bytesLen,1));return t.viewOf({ptr:r,length:e.count+1,dtype:"i32"}).set(t.viewOf({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"})),e.bytesLen>0&&t.viewOf({ptr:o,length:e.bytesLen,dtype:"u8"}).set(t.viewOf({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"})),{count:e.count,offsetsPtr:r,bytesPtr:o,bytesLen:e.bytesLen}}function Te(t,e){return Math.max(e*b[t].size,1)}function Qt(t,e,n,r){let{ctx:o,wasm:i}=t,a=Pe(e.dtype),s=o.mod.alloc(Te(e.dtype,r));B(i,`gather_${a}`)(e.dataPtr,n,r,s);let l=0;if(e.validityPtr!==0){let u=st(o,e),p=A(r),f=o.mod.alloc(Math.max(p,1));o.viewOf({ptr:f,length:Math.max(p,1),dtype:"u8"}).fill(0),B(i,"gather_validity")(u.ptr,n,r,f),lt(o,u),l=f;}let c=e.dict?Ee(o,e.dict):null;return {dtype:e.dtype,length:r,dataPtr:s,validityPtr:l,validityBitOffset:0,dict:c,owned:true}}function Ie(t,e,n){let{ctx:r,wasm:o}=t,i=e.length>0?e[0].length:0,a=r.mod.alloc(Math.max(i*4,1)),s=r.viewOf({ptr:a,length:i,dtype:"i32"});for(let u=0;u<i;u++)s[u]=u;let l=B(o,"argsort_i32").length>=6,c=l?r.mod.alloc(Math.max(i*4,1)):0;try{for(let u=e.length-1;u>=0;u--)Zn(t,e[u],n[u]===!0,a,i,c,l);}finally{c!==0&&(r.viewOf.forget({ptr:c,length:i,dtype:"i32"}),r.mod.free(c));}return {ptr:a,len:i}}function te(t,e,n,r,o,i,a,s,l){let c=B(t,e);l?c(n,r,o,i,a,s):c(n,r,o,i,a);}function Zn(t,e,n,r,o,i,a){let{ctx:s,wasm:l}=t,c=n?1:0,u=st(s,e);try{if(e.dtype==="utf8"){let p=Xn(t,e,o);try{te(l,"argsort_i32",p,u.ptr,r,o,c,i,a);}finally{s.viewOf.forget({ptr:p,length:o,dtype:"i32"}),s.mod.free(p);}}else if(e.dtype==="bool"){let p=Qn(t,e,o);try{te(l,"argsort_i32",p,u.ptr,r,o,c,i,a);}finally{s.viewOf.forget({ptr:p,length:o,dtype:"i32"}),s.mod.free(p);}}else {let p=b[e.dtype].wasm;te(l,`argsort_${p}`,e.dataPtr,u.ptr,r,o,c,i,a);}}finally{lt(s,u);}}function Xn(t,e,n){let{ctx:r}=t,o=e.dict?X(r,e.dict):[],i=o.map((u,p)=>p).sort((u,p)=>{let f=o[u],m=o[p];return f<m?-1:f>m?1:0}),a=new Int32Array(o.length);for(let u=0;u<i.length;u++)a[i[u]]=u;let s=r.mod.alloc(Math.max(n*4,1)),l=r.viewOf({ptr:e.dataPtr,length:n,dtype:"i32"}),c=r.viewOf({ptr:s,length:n,dtype:"i32"});for(let u=0;u<n;u++){let p=l[u];c[u]=p>=0&&p<a.length?a[p]:0;}return s}function Qn(t,e,n){let{ctx:r}=t,o=r.mod.alloc(Math.max(n*4,1)),i=r.viewOf({ptr:e.dataPtr,length:n,dtype:"bool"}),a=r.viewOf({ptr:o,length:n,dtype:"i32"});for(let s=0;s<n;s++)a[s]=i[s];return o}function ee(t,e){let n=0,r={};for(let{name:o,col:i}of e){let a=i.length,s=i.validityBitOffset,l=i.validityPtr===0?null:t.viewOf({ptr:i.validityPtr,length:A(s+a),dtype:"u8"});if(i.dtype==="utf8"){let c=t.viewOf({ptr:i.dataPtr,length:a,dtype:"i32"}),u=i.dict;Object.defineProperty(r,o,{enumerable:true,get(){return l&&!D(l,s+n)?null:j(t,u,c[n])}});}else if(i.dtype==="bool"){let c=t.viewOf({ptr:i.dataPtr,length:a,dtype:"bool"});Object.defineProperty(r,o,{enumerable:true,get(){return l&&!D(l,s+n)?null:c[n]!==0}});}else if(i.dtype==="i64"){let c=t.viewOf({ptr:i.dataPtr,length:a,dtype:"i64"});Object.defineProperty(r,o,{enumerable:true,get(){return l&&!D(l,s+n)?null:c[n]}});}else {let c=t.viewOf({ptr:i.dataPtr,length:a,dtype:b[i.dtype].view});Object.defineProperty(r,o,{enumerable:true,get(){return l&&!D(l,s+n)?null:c[n]}});}}return {at(o){return n=o,r}}}function Oe(t){if(t<=1)return 1;let e=1;for(;e<t;)e<<=1;return e}var Re=4,Dt=16;function Ct(t,e){if(e===0)return t.alloc(0);let n=t.alloc(e);if(n===0)throw new Error(`[hash stubs] OOM: alloc(${e}) returned 0`);return new Uint8Array(t.memory.buffer,n,e).fill(0),n}function Se(t,e,n,r,o){let i=Math.max(Re,Oe(2*Math.max(n,1))),a=0;try{for(a=Ct(t,i*Dt);;){let s=t.group_build(e,n,a,i,r);if(s!==-1)return s;t.free(a),a=0,i*=2,a=Ct(t,i*Dt);}}finally{a!==0&&t.free(a);}}function Me(t,e,n,r,o,i,a,s,l){let c=Math.max(Re,Oe(2*Math.max(s,1))),u=Math.max(1,o+s),p=0,f=0,m=0;try{if(p=Ct(t,c*Dt),f=t.alloc(u*4),f===0)throw new Error("[hash stubs] OOM: out_l_idx alloc failed");if(m=t.alloc(u*4),m===0)throw new Error("[hash stubs] OOM: out_r_idx alloc failed");for(;;){let d=e(n,r,o,i,a,s,p,c,f,m,u);if(d===-1){t.free(p),p=0,c*=2,p=Ct(t,c*Dt);continue}if(d>u){if(t.free(f),f=0,t.free(m),m=0,u=d,f=t.alloc(u*4),f===0)throw new Error("[hash stubs] OOM: out_l_idx resize failed");if(m=t.alloc(u*4),m===0)throw new Error("[hash stubs] OOM: out_r_idx resize failed");t.free(p),p=0,p=Ct(t,c*Dt);continue}let y=new Int32Array(t.memory.buffer,f,d).slice(),w=new Int32Array(t.memory.buffer,m,d).slice();return {count:d,lIdx:y,rIdx:w}}}finally{p!==0&&t.free(p),f!==0&&t.free(f),m!==0&&t.free(m);}}function _e(t,e,n,r,o,i,a,s){return Me(t,(l,c,u,p,f,m,d,y,w,h,g)=>t.join_hash_inner(l,c,u,p,f,m,d,y,w,h,g),e,n,r,o,i,a)}function Be(t,e,n,r,o,i,a,s){return Me(t,(l,c,u,p,f,m,d,y,w,h,g)=>t.join_hash_left(l,c,u,p,f,m,d,y,w,h,g),e,n,r,o,i,a)}var tr=new Set(["sum","mean","min","max","count","size","nunique","std","var","first","last"]),_t=class{src;keys;constructor(e,n){this.src=e,this.keys=n;for(let r of n)if(e.dtypeOf(r)===void 0)throw G(r,e.columnNames())}agg(e){let{rt:n}=this.src,{ctx:r}=n,o=this.src.length,i=this.keys.map(c=>this.src.getColumn(c)),a=this.buildPlan(e),s=nr(n,i),l=[];try{let c=s.firstRow,u=[];for(let m=0;m<this.keys.length;m++){let d=or(n,i[m],c);l.push(d),u.push({name:this.keys[m],col:d});}let p=r.viewOf({ptr:s.groupIdsPtr,length:o,dtype:"i32"});for(let m of a){let d=ir(n,m,p,s.groupCount,o);l.push(d),u.push({name:m.outName,col:d});}let f=this.src.buildResult(u);return l.length=0,f}finally{for(let c of l)Q(r,c);for(let c of a)c.ownsOperand&&c.operand&&Q(r,c.operand);r.viewOf.forget({ptr:s.groupIdsPtr,length:o,dtype:"i32"}),r.mod.free(s.groupIdsPtr);}}buildPlan(e){let n=[];for(let[r,o]of Object.entries(e))if(o instanceof V)n.push(this.exprPlan(r,o));else if(Array.isArray(o))for(let i of o)n.push(this.columnPlan(`${r}_${i}`,r,i));else n.push(this.columnPlan(r,r,o));return n}columnPlan(e,n,r){if(!tr.has(r))throw new I(`unknown aggregation '${r}' for '${n}'.`);if(r==="size")return {outName:e,op:r,operand:null,operandDtype:"i32",ownsOperand:false,resultDtype:"i32"};let o=this.src.getColumn(n);if(!o)throw G(n,this.src.columnNames());let i=er(r,o.dtype);return {outName:e,op:r,operand:o,operandDtype:o.dtype,ownsOperand:false,resultDtype:i}}exprPlan(e,n){let r=n.node;if(r.kind!=="agg")throw new I(`agg value for '${e}' must be a top-level aggregation (e.g. col('a').sum()).`);let o=r.op,i=jt(r.operand,this.src),a=ht(o,i),s=xt(r.operand,this.src).execute();if(s.kind!=="column"||!s.column)throw new I(`aggregation operand for '${e}' did not produce a column.`);return {outName:e,op:o,operand:s.column,operandDtype:i,ownsOperand:true,resultDtype:a}}};function er(t,e){return t==="size"?"i32":ht(t,e)}function nr(t,e){let{ctx:n,wasm:r}=t,o=e.length>0?e[0].length:0,i=n.mod.alloc(Math.max(o*8,1));try{for(let p=0;p<e.length;p++){let f=e[p],m=rr(t,f,o),d=st(n,f);try{if(p===0)B(r,`hash_${m.tok}`)(m.ptr,d.ptr,i,o);else {let y=n.mod.alloc(Math.max(o*8,1));try{B(r,`hash_${m.tok}`)(m.ptr,d.ptr,y,o),B(r,"hash_combine")(i,y,o);}finally{n.viewOf.forget({ptr:y,length:o,dtype:"u8"}),n.mod.free(y);}}}finally{lt(n,d),m.free();}}let a=n.mod.alloc(Math.max(o*4,1)),s=Se(r,i,o,a),l=n.viewOf({ptr:a,length:o,dtype:"i32"}),c=new Int32Array(s),u=0;for(let p=0;p<o&&u<s;p++)l[p]===u&&(c[u++]=p);return {groupIdsPtr:a,groupCount:s,firstRow:c}}finally{n.viewOf.forget({ptr:i,length:o,dtype:"u8"}),n.mod.free(i);}}function rr(t,e,n){let{ctx:r}=t;if(e.dtype==="utf8")return {ptr:e.dataPtr,tok:"i32",free(){}};if(e.dtype==="bool"){let o=r.mod.alloc(Math.max(n*4,1)),i=r.viewOf({ptr:e.dataPtr,length:n,dtype:"bool"}),a=r.viewOf({ptr:o,length:n,dtype:"i32"});for(let s=0;s<n;s++)a[s]=i[s];return {ptr:o,tok:"i32",free(){r.viewOf.forget({ptr:o,length:n,dtype:"i32"}),r.mod.free(o);}}}return {ptr:e.dataPtr,tok:b[e.dtype].wasm,free(){}}}function or(t,e,n){let{ctx:r}=t,o=n.length,i=e.validityPtr===0?null:r.viewOf({ptr:e.validityPtr,length:A(e.validityBitOffset+e.length),dtype:"u8"}),a=e.validityBitOffset;if(e.dtype==="utf8"){let u=r.viewOf({ptr:e.dataPtr,length:e.length,dtype:"i32"}),p=e.dict,f=new Array(o);for(let m=0;m<o;m++){let d=n[m];f[m]=i&&!D(i,a+d)?null:j(r,p,u[d]);}return v(r,"utf8",f)}if(e.dtype==="i64"){let u=r.viewOf({ptr:e.dataPtr,length:e.length,dtype:"i64"}),p=new Array(o);for(let f=0;f<o;f++){let m=n[f];p[f]=i&&!D(i,a+m)?null:u[m];}return v(r,"i64",p)}let s=e.dtype==="bool",l=r.viewOf({ptr:e.dataPtr,length:e.length,dtype:b[e.dtype].view}),c=new Array(o);for(let u=0;u<o;u++){let p=n[u];i&&!D(i,a+p)?c[u]=null:c[u]=s?l[p]!==0:l[p];}return v(r,e.dtype,c)}function ir(t,e,n,r,o){let{ctx:i}=t,{op:a}=e;if(a==="size"){let h=new Array(r).fill(0);for(let g=0;g<o;g++){let x=n[g];h[x]=h[x]+1;}return v(i,"i32",h)}let s=e.operand,l=s.validityPtr===0?null:i.viewOf({ptr:s.validityPtr,length:A(s.validityBitOffset+o),dtype:"u8"}),c=s.validityBitOffset,u=h=>l===null||D(l,c+h);if(a==="count"){let h=new Array(r).fill(0);for(let g=0;g<o;g++)if(u(g)){let x=n[g];h[x]=h[x]+1;}return v(i,"i32",h)}if(a==="first"||a==="last")return lr(t,e,n,r,o,u);if(a==="nunique")return sr(t,e,n,r,o,u);if(a==="std"||a==="var")return ar(t,e,n,r,o,u);if(s.dtype==="i64"){let h=i.viewOf({ptr:s.dataPtr,length:o,dtype:"i64"});if(a==="sum"){let C=new Array(r).fill(0n);for(let k=0;k<o;k++)if(u(k)){let L=n[k];C[L]=C[L]+h[k];}return v(i,"i64",C)}if(a==="mean"){let C=new Float64Array(r),k=new Int32Array(r);for(let F=0;F<o;F++){if(!u(F))continue;let kt=n[F];C[kt]=C[kt]+Number(h[F]),k[kt]=k[kt]+1;}let L=new Array(r);for(let F=0;F<r;F++)L[F]=k[F]>0?C[F]/k[F]:null;return v(i,"f64",L)}let g=new Array(r).fill(null),x=a==="min";for(let C=0;C<o;C++){if(!u(C))continue;let k=n[C],L=h[C];g[k]===null?g[k]=L:g[k]=x?L<g[k]?L:g[k]:L>g[k]?L:g[k];}return v(i,"i64",g)}let p=i.viewOf({ptr:s.dataPtr,length:o,dtype:b[s.dtype].view}),f=new Array(r).fill(null);if(a==="sum"){let h=new Float64Array(r);for(let g=0;g<o;g++)if(u(g)){let x=n[g];h[x]=h[x]+p[g];}for(let g=0;g<r;g++)f[g]=h[g];return v(i,e.resultDtype,f)}if(a==="mean"){let h=new Float64Array(r),g=new Int32Array(r);for(let x=0;x<o;x++){if(!u(x))continue;let C=n[x];h[C]=h[C]+p[x],g[C]=g[C]+1;}for(let x=0;x<r;x++)f[x]=g[x]>0?h[x]/g[x]:null;return v(i,"f64",f)}let m=new Int32Array(r),d=new Int32Array(r),y=new Float64Array(r),w=a==="min";for(let h=0;h<o;h++){if(!u(h))continue;let g=n[h];m[g]=m[g]+1;let x=p[h];Number.isNaN(x)||(d[g]===0?y[g]=x:y[g]=w?Math.min(y[g],x):Math.max(y[g],x),d[g]=d[g]+1);}for(let h=0;h<r;h++)f[h]=m[h]===0?null:d[h]===0?NaN:y[h];return v(i,e.resultDtype,f)}function ar(t,e,n,r,o,i){let{ctx:a}=t,s=e.operand,l=s.dtype==="i64",c=l?a.viewOf({ptr:s.dataPtr,length:o,dtype:"i64"}):a.viewOf({ptr:s.dataPtr,length:o,dtype:b[s.dtype].view}),u=new Float64Array(r),p=new Int32Array(r);for(let w=0;w<o;w++){if(!i(w))continue;let h=n[w];u[h]=u[h]+(l?Number(c[w]):c[w]),p[h]=p[h]+1;}let f=new Float64Array(r);for(let w=0;w<r;w++)f[w]=p[w]>0?u[w]/p[w]:0;let m=new Float64Array(r);for(let w=0;w<o;w++){if(!i(w))continue;let h=n[w],x=(l?Number(c[w]):c[w])-f[h];m[h]=m[h]+x*x;}let d=new Array(r),y=e.op==="std";for(let w=0;w<r;w++)if(p[w]<2)d[w]=null;else {let h=m[w]/(p[w]-1);d[w]=y?Math.sqrt(h):h;}return v(a,"f64",d)}function sr(t,e,n,r,o,i){let{ctx:a}=t,s=e.operand,l=new Array(r),c=s.dtype==="utf8"||s.dtype==="bool"?a.viewOf({ptr:s.dataPtr,length:o,dtype:s.dtype==="utf8"?"i32":"bool"}):s.dtype==="i64"?a.viewOf({ptr:s.dataPtr,length:o,dtype:"i64"}):a.viewOf({ptr:s.dataPtr,length:o,dtype:b[s.dtype].view});for(let p=0;p<o;p++){if(!i(p))continue;let f=n[p];(l[f]??=new Set).add(c[p]);}let u=new Array(r);for(let p=0;p<r;p++)u[p]=l[p]?l[p].size:0;return v(a,"i32",u)}function lr(t,e,n,r,o,i){let{ctx:a}=t,s=e.operand,l=e.op==="first",c=new Int32Array(r).fill(-1);for(let m=0;m<o;m++){if(!i(m))continue;let d=n[m];l?c[d]===-1&&(c[d]=m):c[d]=m;}let u=new Array(r);if(s.dtype==="utf8"){let m=a.viewOf({ptr:s.dataPtr,length:o,dtype:"i32"}),d=s.dict;for(let y=0;y<r;y++)u[y]=c[y]===-1?null:j(a,d,m[c[y]]);return v(a,"utf8",u)}if(s.dtype==="i64"){let m=a.viewOf({ptr:s.dataPtr,length:o,dtype:"i64"});for(let d=0;d<r;d++){let y=c[d];u[d]=y===-1?null:m[y];}return v(a,"i64",u)}let p=s.dtype==="bool",f=a.viewOf({ptr:s.dataPtr,length:o,dtype:b[s.dtype].view});for(let m=0;m<r;m++){let d=c[m];u[m]=d===-1?null:p?f[d]!==0:f[d];}return v(a,s.dtype,u)}function Ue(t,e,n){let r=n.how??"inner",o=Array.isArray(n.on)?n.on:[n.on];if(o.length===0)throw new I("join requires at least one key in `on`.");let i=t.rt,{ctx:a}=i,s=t.length,l=e.length;for(let u of o){let p=t.dtypeOf(u);if(p===void 0)throw G(u,t.columnNames());let f=e.dtypeOf(u);if(f===void 0)throw G(u,e.columnNames());if(p!==f)throw Ce("join",p,f,`join key '${u}' must have one dtype.`)}let c=o.map(u=>cr(i,t.getColumn(u),e.getColumn(u),s,l));try{let u=$e(i,c.map(y=>({ptr:y.leftPtr,tok:y.tok})),s),p=$e(i,c.map(y=>({ptr:y.rightPtr,tok:y.tok})),l),f=Le(i,o.map(y=>t.getColumn(y)),s),m=Le(i,o.map(y=>e.getColumn(y)),l),d;try{let y=i.wasm;d=r==="left"?Be(y,u,f.ptr,s,p,m.ptr,l):_e(y,u,f.ptr,s,p,m.ptr,l);}finally{a.viewOf.forget({ptr:u,length:s,dtype:"u8"}),a.mod.free(u),a.viewOf.forget({ptr:p,length:l,dtype:"u8"}),a.mod.free(p),ut(i,f.ptr,f.owns),ut(i,m.ptr,m.owns);}return ur(t,e,o,d,r)}finally{for(let u of c)u.free();}}function ur(t,e,n,r,o){let i=t.rt,{ctx:a}=i,s=new Set(n),l=t.columnNames(),c=e.columnNames().filter(f=>!s.has(f)),u=new Set(l),p=[];try{let f=[];for(let d of l){let y=Ve(i,t.getColumn(d),r.lIdx);p.push(y),f.push({name:d,col:y});}for(let d of c){let y=Ve(i,e.getColumn(d),r.rIdx);p.push(y);let w=u.has(d)?`${d}_right`:d;f.push({name:w,col:y});}let m=t.buildResult(f);return p.length=0,m}finally{for(let f of p)Q(a,f);}}function Ve(t,e,n){let{ctx:r}=t,o=z(r,e),i=new Array(n.length);for(let a=0;a<n.length;a++){let s=n[a];i[a]=s<0?null:o[s];}return v(r,e.dtype,i)}function cr(t,e,n,r,o){let{ctx:i}=t;if(e.dtype==="utf8"){let a=re(i,e.dict,n.dict),s=Fe(t,e,a.remapA,r),l=Fe(t,n,a.remapB,o);return {leftPtr:s,rightPtr:l,tok:"i32",free(){ut(t,s,true),ut(t,l,true);}}}if(e.dtype==="bool"){let a=Ne(t,e,r),s=Ne(t,n,o);return {leftPtr:a,rightPtr:s,tok:"i32",free(){ut(t,a,true),ut(t,s,true);}}}return {leftPtr:e.dataPtr,rightPtr:n.dataPtr,tok:b[e.dtype].wasm,free(){}}}function Fe(t,e,n,r){let{ctx:o}=t,i=o.mod.alloc(Math.max(r*4,1)),a=o.viewOf({ptr:e.dataPtr,length:r,dtype:"i32"}),s=o.viewOf({ptr:i,length:r,dtype:"i32"});for(let l=0;l<r;l++){let c=a[l];s[l]=c>=0&&c<n.length?n[c]:0;}return i}function Ne(t,e,n){let{ctx:r}=t,o=r.mod.alloc(Math.max(n*4,1)),i=r.viewOf({ptr:e.dataPtr,length:n,dtype:"bool"}),a=r.viewOf({ptr:o,length:n,dtype:"i32"});for(let s=0;s<n;s++)a[s]=i[s];return o}function $e(t,e,n){let{ctx:r,wasm:o}=t,i=r.mod.alloc(Math.max(n*8,1));for(let a=0;a<e.length;a++){let s=e[a];if(a===0)B(o,`hash_${s.tok}`)(s.ptr,0,i,n);else {let l=r.mod.alloc(Math.max(n*8,1));try{B(o,`hash_${s.tok}`)(s.ptr,0,l,n),B(o,"hash_combine")(i,l,n);}finally{r.viewOf.forget({ptr:l,length:n,dtype:"u8"}),r.mod.free(l);}}}return i}function Le(t,e,n){let{ctx:r}=t,o=e.filter(c=>c.validityPtr!==0);if(o.length===0)return {ptr:0,owns:false};let i=A(n),a=r.mod.alloc(Math.max(i,1)),s=r.viewOf({ptr:a,length:Math.max(i,1),dtype:"u8"});s.fill(0);let l=o.map(c=>({view:r.viewOf({ptr:c.validityPtr,length:A(c.validityBitOffset+n),dtype:"u8"}),bitOff:c.validityBitOffset}));for(let c=0;c<n;c++){let u=true;for(let p of l)if(!D(p.view,p.bitOff+c)){u=false;break}u&&O(s,c);}return {ptr:a,owns:true}}function ut(t,e,n){n&&e!==0&&(t.ctx.viewOf.forget({ptr:e,length:1,dtype:"u8"}),t.ctx.mod.free(e));}function je(t,e){if(t===null)return "null";if(typeof t=="boolean")return t?"true":"false";if(typeof t=="string")return t.length>24?`${t.slice(0,23)}\u2026`:t;if(e==="date32"&&typeof t=="number")return new Date(t*864e5).toISOString().slice(0,10);if(typeof t=="bigint")return e==="timestamp"?new Date(Number(t)).toISOString():String(t);if(Number.isNaN(t))return "NaN";if(t===1/0)return "inf";if(t===-1/0)return "-inf";if(Number.isInteger(t))return String(t);if(e==="f32"||e==="f64"){let n=t.toPrecision(6);return n.includes(".")&&!n.includes("e")?n.replace(/0+$/,"").replace(/\.$/,""):n}return String(t)}function ze(t){return t==="utf8"||t==="bool"?"left":"right"}function pr(t,e,n){let r=e-t.length;return r<=0?t:n==="right"?" ".repeat(r)+t:t+" ".repeat(r)}function qe(t,e,n,r,o){let i=t.length,a=new Array(i);for(let u=0;u<i;u++){let p=Math.max(t[u].length,e[u].length);for(let f of n)p=Math.max(p,f[u].length);a[u]=p;}let s=u=>u.map((p,f)=>pr(p,a[f],r[f])).join(" "),l=a.map(u=>"\u2500".repeat(u)).join(" "),c=[];c.push(s(t)),c.push(s(e)),c.push(l);for(let u of n)c.push(s(u));return o&&c.push(o),c.join(`
2
- `)}var fr=new Set(["f64","f32","i32","u32","i64"]),Bt=class t{length;_rt;entries;byName;disposed=false;constructor(e,n,r){this._rt=e,this.entries=n,this.length=r,this.byName=new Map(n.map(o=>[o.name,o]));}get ctx(){return this._rt.ctx}get wasm(){return this._rt.wasm}get rt(){return this._rt}getColumn(e){return this.byName.get(e)?.col}dtypeOf(e){return this.byName.get(e)?.col.dtype}columnNames(){return this.entries.map(e=>e.name)}buildResult(e){let n=e.length>0?e[0].col.length:this.length;return t.fromRoots(this._rt,e,n)}static fromColumns(e,n={}){let r=n.runtime??Gt(),o=[],i=-1;for(let[a,s]of Object.entries(e)){let l=n.dtypes?.[a]??Zt(s),c=l==="timestamp"?n.tzs?.[a]:void 0,u=v(r.ctx,l,s,c);if(i===-1)i=u.length;else if(u.length!==i)throw new I(`column '${a}' has length ${u.length}, expected ${i}.`);o.push({name:a,col:u});}return t.fromRoots(r,o,i===-1?0:i)}static fromRecords(e,n={}){n.runtime??Gt();let o=[],i=new Set;for(let s of e)for(let l of Object.keys(s))i.has(l)||(i.add(l),o.push(l));let a={};for(let s of o){let l=new Array(e.length);for(let c=0;c<e.length;c++){let u=e[c][s];l[c]=u===void 0?null:u;}a[s]=l;}return t.fromColumns(a,n)}static fromRoots(e,n,r){let o=n.map(i=>({name:i.name,col:i.col,owner:new At(e.ctx,i.col)}));return new t(e,o,r)}get shape(){return [this.length,this.entries.length]}get columns(){return this.columnNames()}get dtypes(){let e={};for(let n of this.entries)e[n.name]=n.col.dtype;return e}col(e){let n=this.entryOf(e);return new vt(this._rt.ctx,e,n.col)}select(e){return new t(this._rt,this.shareEntries(e),this.length)}drop(e){let n=new Set(e);for(let o of e)if(!this.byName.has(o))throw G(o,this.columnNames());let r=this.entries.filter(o=>!n.has(o.name)).map(o=>o.name);return new t(this._rt,this.shareEntries(r),this.length)}withColumn(e,n,r={}){let o=this.materializeColumn(n,r),i=new At(this._rt.ctx,o),a={name:e,col:o,owner:i},s=[],l=false;for(let c of this.entries)c.name===e?(s.push(a),l=true):s.push(this.retainEntry(c));return l||s.push(a),new t(this._rt,s,this.length)}assign(e,n,r){return this.withColumn(e,n,r)}filter(e){let n=Wt(e,this).execute();try{let r=this.entries.map(o=>({name:o.name,col:n.compact(o.col)}));return t.fromRoots(this._rt,r,n.count)}finally{n.free();}}filterFn(e){let n=ee(this._rt.ctx,this.entries),r=[];for(let o=0;o<this.length;o++)e(n.at(o))&&r.push(o);return this.gatherRows(r)}mapFn(e){let n=ee(this._rt.ctx,this.entries),r=new Array(this.length);for(let o=0;o<this.length;o++)r[o]=e(n.at(o));return r}sortValues(e,n={}){let r=typeof e=="string"?[e]:[...e];if(r.length===0)throw new I("sortValues requires at least one key.");let o=r.map(s=>this.entryOf(s).col),i=Array.isArray(n.descending)?r.map((s,l)=>n.descending[l]===true):r.map(()=>n.descending===true),a=Ie(this._rt,o,i);try{let s=this.entries.map(l=>({name:l.name,col:Qt(this._rt,l.col,a.ptr,a.len)}));return t.fromRoots(this._rt,s,a.len)}finally{this._rt.ctx.viewOf.forget({ptr:a.ptr,length:a.len,dtype:"i32"}),this._rt.ctx.mod.free(a.ptr);}}groupby(e){let n=typeof e=="string"?[e]:[...e];if(n.length===0)throw new I("groupby requires at least one key.");return new _t(this,n)}join(e,n){return Ue(this,e,n)}head(e=5){return this.sliceRange(0,e)}tail(e=5){return this.sliceRange(Math.max(0,this.length-e),this.length)}slice(e,n=this.length){return this.sliceRange(e,n)}sliceRange(e,n){let r=Math.max(0,Math.min(e,this.length)),o=Math.max(r,Math.min(n,this.length)),i=this.entries.map(a=>({name:a.name,col:Vt(a.col,r,o),owner:a.owner.retain()}));return new t(this._rt,i,o-r)}toColumns(){let e={};for(let n of this.entries)e[n.name]=z(this._rt.ctx,n.col);return e}toRecords(){let e=this.toColumns(),n=this.columnNames(),r=new Array(this.length);for(let o=0;o<this.length;o++){let i={};for(let a of n)i[a]=e[a][o];r[o]=i;}return r}describe(){let e=["count","mean","std","min","25%","50%","75%","max"],n=this.entries.filter(i=>fr.has(i.col.dtype)),r={statistic:e},o={statistic:"utf8"};for(let i of n)r[i.name]=dr(z(this._rt.ctx,i.col)),o[i.name]="f64";return t.fromColumns(r,{runtime:this._rt,dtypes:o})}dispose(){if(!this.disposed){this.disposed=true;for(let e of this.entries)e.owner.release();}}toString(){return this.render()}[Symbol.for("nodejs.util.inspect.custom")](){return this.render()}entryOf(e){let n=this.byName.get(e);if(!n)throw G(e,this.columnNames());return n}retainEntry(e){return e.owner.retain(),{name:e.name,col:e.col,owner:e.owner}}shareEntries(e){return e.map(n=>this.retainEntry(this.entryOf(n)))}materializeColumn(e,n){if(e instanceof V){let o=xt(e,this).execute();if(o.kind==="column"&&o.column)return o.column;let i=o.scalar,a=new Array(this.length).fill(i.value);return v(this._rt.ctx,i.dtype,a)}let r=n.dtype??Zt(e);if(e.length!==this.length)throw new I(`withColumn value has length ${e.length}, expected ${this.length}.`);return v(this._rt.ctx,r,e)}gatherRows(e){let{ctx:n}=this._rt,r=e.length,o=n.mod.alloc(Math.max(r*4,1));try{let i=n.viewOf({ptr:o,length:r,dtype:"i32"});for(let s=0;s<r;s++)i[s]=e[s];let a=this.entries.map(s=>({name:s.name,col:Qt(this._rt,s.col,o,r)}));return t.fromRoots(this._rt,a,r)}finally{n.viewOf.forget({ptr:o,length:r,dtype:"i32"}),n.mod.free(o);}}render(){let e=this.columnNames();if(e.length===0)return `Empty DataFrame [${this.length} rows \xD7 0 columns]`;let n=this.entries.map(u=>u.col.dtype),r=n.map(ze),o=5,i=5,a=[],s=this.length>o+i,l=(u,p)=>{let f=this.entries.map(m=>z(this._rt.ctx,Vt(m.col,u,p)));for(let m=0;m<p-u;m++)a.push(f.map((d,y)=>je(d[m],n[y])));};s?(l(0,o),a.push(e.map(()=>"\u2026")),l(this.length-i,this.length)):l(0,this.length);let c=`[${this.length} rows \xD7 ${e.length} columns]`;return qe(e,n,a,r,c)}};function dr(t){let e=[];for(let l of t){if(l===null)continue;let c=typeof l=="bigint"?Number(l):l;Number.isFinite(c)&&e.push(c);}let n=e.length;if(n===0)return [0,null,null,null,null,null,null,null];let r=e.slice().sort((l,c)=>l-c),i=e.reduce((l,c)=>l+c,0)/n,a=n>=2?Math.sqrt(e.reduce((l,c)=>l+(c-i)*(c-i),0)/(n-1)):null,s=l=>{if(r.length===1)return r[0];let c=l*(r.length-1),u=Math.floor(c),p=Math.ceil(c),f=c-u;return r[u]+(r[p]-r[u])*f};return [n,i,a,r[0],s(.25),s(.5),s(.75),r[r.length-1]]}var br=new Set(["UNCOMPRESSED","SNAPPY",void 0]),xr={timestampFromMilliseconds:t=>t,timestampFromMicroseconds:t=>t/1000n,timestampFromNanoseconds:t=>t/1000000n,dateFromDays:t=>t,stringFromBytes:t=>t!=null?new TextDecoder().decode(t):null,jsonFromBytes:t=>t!=null?JSON.parse(new TextDecoder().decode(t)):null,bsonFromBytes:t=>t,geometryFromBytes:t=>t,geographyFromBytes:t=>t,uuidFromBytes:t=>t};function Ar(t){let e=t instanceof ArrayBuffer?t:t.buffer.slice(t.byteOffset,t.byteOffset+t.byteLength);return {byteLength:e.byteLength,slice:(n,r)=>Promise.resolve(e.slice(n,r))}}function vr(t,e){let n=t.name,r=t.type,o=t.logical_type,i=t.converted_type,a=t.num_children,s=t.repetition_type;if(a!=null&&a>0){let l=o?.type;throw l==="LIST"?new Error(`unsupported Parquet feature: LIST column '${n}' \u2014 nested/repeated types are not supported by databonk/parquet (ADR-011)`):l==="MAP"?new Error(`unsupported Parquet feature: MAP column '${n}' \u2014 nested types are not supported (ADR-011)`):new Error(`unsupported Parquet feature: nested/group column '${n}' \u2014 databonk/parquet only supports flat columns (ADR-011)`)}if(s==="REPEATED")throw new Error(`unsupported Parquet feature: REPEATED field '${n}' \u2014 only OPTIONAL/REQUIRED are supported (ADR-011)`);switch(r){case "DOUBLE":return {name:n,dtype:"f64"};case "FLOAT":return {name:n,dtype:"f32"};case "BOOLEAN":return {name:n,dtype:"bool"};case "INT32":{let l=o?.type;if(l==="DATE"||i==="DATE")return {name:n,dtype:"date32"};if(l==="DECIMAL"||i==="DECIMAL")throw new Error(`unsupported Parquet feature: DECIMAL column '${n}' (ADR-011 \u2014 only the databonk numeric dtypes are supported)`);if(l==="INTEGER"){let c=o?.isSigned;return {name:n,dtype:c===false?"u32":"i32"}}return i==="UINT_32"?{name:n,dtype:"u32"}:{name:n,dtype:"i32"}}case "INT64":{let l=o?.type;if(l==="DECIMAL"||i==="DECIMAL")throw new Error(`unsupported Parquet feature: DECIMAL column '${n}' (ADR-011)`);if(l==="TIMESTAMP"||i==="TIMESTAMP_MILLIS"){let c=o?.unit;if(c!=null&&c!=="MILLIS")throw new Error(`unsupported Parquet feature: TIMESTAMP with unit '${c}' in column '${n}' \u2014 only MILLIS is supported (ADR-011)`);if(i==="TIMESTAMP_MICROS")throw new Error(`unsupported Parquet feature: TIMESTAMP_MICROS column '${n}' \u2014 only MILLIS is supported (ADR-011)`);let u=e.get(n);return u!==void 0?{name:n,dtype:"timestamp",tz:u}:{name:n,dtype:"timestamp"}}return {name:n,dtype:"i64"}}case "INT96":throw new Error(`unsupported Parquet feature: INT96 timestamp column '${n}' \u2014 only TIMESTAMP(INT64, MILLIS) is supported (ADR-011)`);case "FIXED_LEN_BYTE_ARRAY":throw new Error(`unsupported Parquet feature: FIXED_LEN_BYTE_ARRAY column '${n}' (ADR-011)`);case "BYTE_ARRAY":{let l=o?.type;if(l==="STRING"||i==="UTF8")return {name:n,dtype:"utf8"};throw l==="DECIMAL"||i==="DECIMAL"?new Error(`unsupported Parquet feature: DECIMAL column '${n}' (ADR-011)`):new Error(`unsupported Parquet feature: BYTE_ARRAY column '${n}' without STRING/UTF8 annotation is not supported (ADR-011)`)}default:throw new Error(`unsupported Parquet feature: unknown physical type '${String(r??"GROUP")}' in column '${n}' (ADR-011)`)}}function Dr(t){if(t.length===0)return [];if(t.length===1)return t[0];let e=t.reduce((o,i)=>o+i.length,0),n=new Array(e),r=0;for(let o of t){let i=o.length;for(let a=0;a<i;a++)n[r++]=o[a];}return n}async function xi(t,e){let n=Ar(t),r=await index_js.parquetMetadataAsync(n);for(let m of r.row_groups??[])for(let d of m.columns??[]){let y=d.meta_data?.codec;if(!br.has(y))throw new Error(`unsupported Parquet compression codec: '${y}'. databonk/parquet supports only SNAPPY and UNCOMPRESSED (ADR-011). For GZIP/ZSTD/BROTLI/LZ4, re-compress the file first.`)}let o=new Map,i=r.key_value_metadata??[],a="databonk:tz:";for(let{key:m,value:d}of i)m.startsWith(a)&&d&&o.set(m.slice(a.length),d);let l=index_js.parquetSchema(r).children.map(m=>vr(m.element,o)),c=new Map;for(let{name:m}of l)c.set(m,[]);await index_js.parquetRead({file:n,metadata:r,parsers:xr,onChunk(m){let d=c.get(m.columnName);d&&d.push(m.columnData);}});let u=[];for(let{name:m,dtype:d,tz:y}of l){let w=c.get(m)??[],h=Dr(w),g=v(e.ctx,d,h,y);u.push({name:m,col:g});}let p=Bt.fromColumns({},{runtime:e}),f=p.buildResult(u);return p.dispose(),f}function Cr(t,e,n,r){let o=z(t,n),i,a;switch(n.dtype){case "f64":i={name:e,type:"DOUBLE",repetition_type:"OPTIONAL"};break;case "f32":i={name:e,type:"FLOAT",repetition_type:"OPTIONAL"};break;case "i32":i={name:e,type:"INT32",repetition_type:"OPTIONAL"};break;case "u32":i={name:e,type:"INT32",repetition_type:"OPTIONAL",converted_type:"UINT_32"};break;case "i64":i={name:e,type:"INT64",repetition_type:"OPTIONAL"};break;case "bool":i={name:e,type:"BOOLEAN",repetition_type:"OPTIONAL"};break;case "utf8":i={name:e,type:"BYTE_ARRAY",repetition_type:"OPTIONAL",logical_type:{type:"STRING"},converted_type:"UTF8"};break;case "date32":i={name:e,type:"INT32",repetition_type:"OPTIONAL",logical_type:{type:"DATE"}};break;case "timestamp":{i={name:e,type:"INT64",repetition_type:"OPTIONAL",logical_type:{type:"TIMESTAMP",isAdjustedToUTC:true,unit:{MILLIS:{}}}},n.tz&&(a={key:`databonk:tz:${e}`,value:n.tz});break}default:{let s=n.dtype;throw new Error(`unsupported databonk dtype for Parquet write: '${String(s)}'`)}}return a!==void 0?{el:i,data:o,tzKey:a}:{el:i,data:o}}function Ai(t,e={}){let{compression:n="uncompressed"}=e;if(n!=="snappy"&&n!=="uncompressed")throw new Error(`unsupported Parquet compression codec: '${n}'. databonk/parquet writeParquet only accepts 'snappy' or 'uncompressed' (ADR-011).`);let r=n==="snappy"?"SNAPPY":"UNCOMPRESSED",o=t.columns;t.length;let a=t.ctx,s=[{name:"root",num_children:o.length}],l=[],c=[];for(let f of o){let m=t.getColumn(f),{el:d,data:y,tzKey:w}=Cr(a,f,m);s.push(d),l.push({name:f,data:y}),w&&c.push(w);}let u={schema:s,columnData:l,codec:r};c.length>0&&(u.kvMetadata=c);let p=index_js$1.parquetWriteBuffer(u);return new Uint8Array(p)}exports.readParquet=xi;exports.writeParquet=Ai;//# sourceMappingURL=parquet.cjs.map
1
+ 'use strict';var index_js=require('hyparquet/src/index.js'),index_js$1=require('hyparquet-writer/src/index.js');var x={f64:{name:"f64",size:8,view:"f64",ctor:Float64Array,wasm:"f64",float:true},f32:{name:"f32",size:4,view:"f32",ctor:Float32Array,wasm:"f32",float:true},i32:{name:"i32",size:4,view:"i32",ctor:Int32Array,wasm:"i32",float:false},u32:{name:"u32",size:4,view:"u32",ctor:Uint32Array,wasm:"u32",float:false},bool:{name:"bool",size:1,view:"bool",ctor:Uint8Array,wasm:"bool",float:false},utf8:{name:"utf8",size:4,view:"i32",ctor:Int32Array,wasm:"utf8",float:false},i64:{name:"i64",size:8,view:"i64",ctor:BigInt64Array,wasm:"i64",float:false},date32:{name:"date32",size:4,view:"i32",ctor:Int32Array,wasm:"i32",float:false},timestamp:{name:"timestamp",size:8,view:"i64",ctor:BigInt64Array,wasm:"i64",float:false}};function A(t){return t+7>>3}function v(t,e){return (t[e>>3]&1<<(e&7))!==0}function S(t,e){let n=e>>3;t[n]=(t[n]??0)|1<<(e&7);}var Je=new TextEncoder,Ze=new TextDecoder,oe=new WeakMap;function Xe(t){let e=oe.get(t);return e===void 0&&(e={slots:new Array(t.count),stats:{hits:0,misses:0}},oe.set(t,e)),e}function pt(t,e){let n=e.length,r=e.map(c=>Je.encode(c)),o=0;for(let c of r)o+=c.length;let i=t.mod.alloc((n+1)*4),a=t.mod.alloc(o),s=t.viewOf({ptr:i,length:n+1,dtype:"i32"}),l=t.viewOf({ptr:a,length:o,dtype:"u8"});s[0]=0;let u=0;for(let c=0;c<n;c++){let m=r[c];l.set(m,u),u+=m.length,s[c+1]=u;}return {count:n,offsetsPtr:i,bytesPtr:a,bytesLen:o}}function j(t,e,n){let r=Xe(e),o=r.slots[n];if(o!==void 0)return r.stats.hits++,o;r.stats.misses++;let i=t.viewOf({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"}),a=i[n],s=i[n+1],l;if(s<=a)l="";else {let u=t.viewOf({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"});l=Ze.decode(u.subarray(a,s));}return r.slots[n]=l,l}function Z(t,e){let n=new Array(e.count);for(let r=0;r<e.count;r++)n[r]=j(t,e,r);return n}function ie(t,e){t.viewOf.forget({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"}),t.viewOf.forget({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"}),t.mod.free(e.offsetsPtr),t.mod.free(e.bytesPtr);}function Y(t){return t==null}function W(t,e,n,r,o,i){return {dtype:t,length:e,dataPtr:n,validityPtr:r,validityBitOffset:0,dict:o,owned:true,...i!==void 0?{tz:i}:{}}}function D(t,e,n,r){return e==="utf8"?on(t,n):e==="bool"?en(t,n):e==="i64"?nn(t,n):e==="timestamp"||e==="date32"?rn(t,e,n,r):tn(t,e,n)}function Qe(t,e){switch(t){case "f64":return e instanceof Float64Array;case "f32":return e instanceof Float32Array;case "i32":return e instanceof Int32Array;case "u32":return e instanceof Uint32Array;default:return false}}function tn(t,e,n){let r=x[e],o=n.length;if(Qe(e,n)){let c=t.mod.alloc(o*r.size);return t.viewOf({ptr:c,length:o,dtype:r.view}).set(n),W(e,o,c,0,null)}let i=0;for(let c=0;c<o;c++)Y(n[c])&&i++;let a=t.mod.alloc(o*r.size),s=i>0?t.mod.alloc(A(o)):0,l=t.viewOf({ptr:a,length:o,dtype:r.view}),u=null;s!==0&&(u=t.viewOf({ptr:s,length:A(o),dtype:"u8"}),u.fill(0));for(let c=0;c<o;c++){let m=n[c];Y(m)?l[c]=0:(l[c]=m,u&&S(u,c));}return W(e,o,a,s,null)}function en(t,e){let n=e.length;if(e instanceof Uint8Array){let l=t.mod.alloc(n);return t.viewOf({ptr:l,length:n,dtype:"bool"}).set(e),W("bool",n,l,0,null)}let r=0;for(let l=0;l<n;l++)Y(e[l])&&r++;let o=t.mod.alloc(n),i=r>0?t.mod.alloc(A(n)):0,a=t.viewOf({ptr:o,length:n,dtype:"bool"}),s=null;i!==0&&(s=t.viewOf({ptr:i,length:A(n),dtype:"u8"}),s.fill(0));for(let l=0;l<n;l++){let u=e[l];Y(u)?a[l]=0:(a[l]=u?1:0,s&&S(s,l));}return W("bool",n,o,i,null)}function nn(t,e){let n=e.length;if(e instanceof BigInt64Array){let l=t.mod.alloc(n*8);return t.viewOf({ptr:l,length:n,dtype:"i64"}).set(e),W("i64",n,l,0,null)}let r=0;for(let l=0;l<n;l++){let u=e[l];if(Y(u)){r++;continue}if(typeof u=="number"){if(!Number.isInteger(u))throw new RangeError(`i64 column: non-integer number ${u} at index ${l} \u2014 use BigInt or null`);if(!Number.isSafeInteger(u))throw new RangeError(`i64 column: unsafe integer ${u} at index ${l} \u2014 use BigInt (e.g. ${u}n)`)}}let o=t.mod.alloc(n*8),i=r>0?t.mod.alloc(A(n)):0,a=t.viewOf({ptr:o,length:n,dtype:"i64"}),s=null;i!==0&&(s=t.viewOf({ptr:i,length:A(n),dtype:"u8"}),s.fill(0));for(let l=0;l<n;l++){let u=e[l];Y(u)?a[l]=0n:(a[l]=typeof u=="bigint"?u:BigInt(u),s&&S(s,l));}return W("i64",n,o,i,null)}function rn(t,e,n,r){let o=e==="timestamp",i=o?8:4,a=n.length,s=o?"i64":"i32";if(o?n instanceof BigInt64Array:n instanceof Int32Array){let p=t.mod.alloc(Math.max(a*i,1));return t.viewOf({ptr:p,length:a,dtype:s}).set(n),W(e,a,p,0,null,r)}let l=0;for(let p=0;p<a;p++)Y(n[p])&&l++;let u=t.mod.alloc(Math.max(a*i,1)),c=l>0?t.mod.alloc(A(a)):0,m=t.viewOf({ptr:u,length:a,dtype:s}),f=null;c&&(f=t.viewOf({ptr:c,length:A(a),dtype:"u8"}),f.fill(0));for(let p=0;p<a;p++){let d=n[p];Y(d)?m[p]=o?0n:0:(m[p]=o?typeof d=="bigint"?d:BigInt(d):d,f&&S(f,p));}return W(e,a,u,c,null,r)}function on(t,e){let n=e.length,r=new Map,o=[],i=new Int32Array(n),a=new Uint8Array(n),s=0;for(let f=0;f<n;f++){let p=e[f];if(Y(p))s++,a[f]=1,i[f]=0;else {let d=p,h=r.get(d);h===void 0&&(h=o.length,o.push(d),r.set(d,h)),i[f]=h;}}let l=t.mod.alloc(n*4),u=s>0?t.mod.alloc(A(n)):0,c=pt(t,o);if(t.viewOf({ptr:l,length:n,dtype:"i32"}).set(i),u!==0){let f=t.viewOf({ptr:u,length:A(n),dtype:"u8"});f.fill(0);for(let p=0;p<n;p++)a[p]===0&&S(f,p);}return W("utf8",n,l,u,c)}function H(t,e){let{length:n,validityPtr:r,validityBitOffset:o}=e,i=r===0?null:t.viewOf({ptr:r,length:A(o+n),dtype:"u8"}),a=new Array(n);if(e.dtype==="utf8"){let c=t.viewOf({ptr:e.dataPtr,length:n,dtype:"i32"}),m=e.dict;for(let f=0;f<n;f++)a[f]=i&&!v(i,o+f)?null:j(t,m,c[f]);return a}if(e.dtype==="i64"||e.dtype==="timestamp"){let c=t.viewOf({ptr:e.dataPtr,length:n,dtype:"i64"});for(let m=0;m<n;m++)a[m]=i&&!v(i,o+m)?null:c[m];return a}if(e.dtype==="date32"){let c=t.viewOf({ptr:e.dataPtr,length:n,dtype:"i32"});for(let m=0;m<n;m++)a[m]=i&&!v(i,o+m)?null:c[m];return a}let s=x[e.dtype],l=t.viewOf({ptr:e.dataPtr,length:n,dtype:s.view}),u=e.dtype==="bool";for(let c=0;c<n;c++)i&&!v(i,o+c)?a[c]=null:a[c]=u?l[c]!==0:l[c];return a}function Ft(t,e,n){let r=Math.max(0,Math.min(e,t.length)),i=Math.max(r,Math.min(n,t.length))-r,a=x[t.dtype].size;return {dtype:t.dtype,length:i,dataPtr:t.dataPtr+r*a,validityPtr:t.validityPtr,validityBitOffset:t.validityBitOffset+r,dict:t.dict,owned:false,...t.tz!==void 0?{tz:t.tz}:{}}}function Q(t,e){if(!e.owned)return;let n=x[e.dtype];t.viewOf.forget({ptr:e.dataPtr,length:e.length,dtype:n.view}),t.mod.free(e.dataPtr),e.validityPtr!==0&&(t.viewOf.forget({ptr:e.validityPtr,length:A(e.validityBitOffset+e.length),dtype:"u8"}),t.mod.free(e.validityPtr)),e.dict!==null&&ie(t,e.dict);}function dt(t){return t instanceof M?t:ae(t)}var M=class t{node;constructor(e){this.node=Object.freeze(e),Object.freeze(this);}add(e){return ft("add",this,e)}sub(e){return ft("sub",this,e)}mul(e){return ft("mul",this,e)}div(e){return ft("div",this,e)}mod(e){return ft("mod",this,e)}neg(){return new t({kind:"neg",operand:this})}gt(e){return rt("gt",this,e)}ge(e){return rt("ge",this,e)}lt(e){return rt("lt",this,e)}le(e){return rt("le",this,e)}eq(e){return rt("eq",this,e)}ne(e){return rt("ne",this,e)}and(e){return new t({kind:"bool",op:"and",left:this,right:dt(e)})}or(e){return new t({kind:"bool",op:"or",left:this,right:dt(e)})}not(){return new t({kind:"not",operand:this})}isNull(){return new t({kind:"isNull",operand:this})}notNull(){return this.isNull().not()}fillNull(e){return new t({kind:"fillNull",operand:this,value:e})}cast(e){return new t({kind:"cast",operand:this,to:e})}sum(){return q("sum",this)}mean(){return q("mean",this)}min(){return q("min",this)}max(){return q("max",this)}count(){return q("count",this)}nunique(){return q("nunique",this)}std(){return q("std",this)}var(){return q("var",this)}first(){return q("first",this)}last(){return q("last",this)}get dt(){return new Et(this)}get str(){return new Tt(this)}toString(){return R(this.node)}},Et=class{constructor(e){this.operand=e;}operand;year(){return z("year",this.operand)}month(){return z("month",this.operand)}day(){return z("day",this.operand)}hour(){return z("hour",this.operand)}minute(){return z("minute",this.operand)}second(){return z("second",this.operand)}millisecond(){return z("millisecond",this.operand)}weekday(){return z("weekday",this.operand)}dayOfYear(){return z("dayOfYear",this.operand)}quarter(){return z("quarter",this.operand)}},Tt=class{constructor(e){this.operand=e;}operand;slice(e,n){return new M({kind:"strSlice",operand:this.operand,start:e,end:n})}};function ae(t,e){return new M({kind:"lit",value:t,dtype:null})}function ft(t,e,n){return new M({kind:"arith",op:t,left:e,right:dt(n)})}function rt(t,e,n){return new M({kind:"compare",op:t,left:e,right:dt(n)})}function q(t,e){return new M({kind:"agg",op:t,operand:e})}function z(t,e){return new M({kind:"dt",component:t,operand:e})}var an={add:"+",sub:"-",mul:"*",div:"/",mod:"%"};function $t(t){return typeof t=="string"?JSON.stringify(t):String(t)}function R(t){switch(t.kind){case "col":return `col(${JSON.stringify(t.name)})`;case "lit":return t.dtype?`lit(${$t(t.value)}, ${t.dtype})`:`lit(${$t(t.value)})`;case "arith":return `(${R(t.left.node)} ${an[t.op]} ${R(t.right.node)})`;case "neg":return `(-${R(t.operand.node)})`;case "compare":return `${R(t.left.node)}.${t.op}(${R(t.right.node)})`;case "bool":return `${R(t.left.node)}.${t.op}(${R(t.right.node)})`;case "not":return `${R(t.operand.node)}.not()`;case "isNull":return `${R(t.operand.node)}.isNull()`;case "fillNull":return `${R(t.operand.node)}.fillNull(${$t(t.value)})`;case "cast":return `${R(t.operand.node)}.cast(${t.to})`;case "agg":return `${R(t.operand.node)}.${t.op}()`;case "dt":return `${R(t.operand.node)}.dt.${t.component}()`;case "strSlice":return t.end===void 0?`${R(t.operand.node)}.str.slice(${t.start})`:`${R(t.operand.node)}.str.slice(${t.start}, ${t.end})`}}var _=class extends Error{constructor(e){super(e),this.name="ExprError";}};function tt(t,e,n,r){let o=r?` ${r}`:"";return new _(`dtype mismatch in '${t}': cannot combine ${e} and ${n}.${o}`)}function P(t,e,n){let r=n?` ${n}`:"";return new _(`operation '${t}' is not supported for dtype ${e}.${r}`)}function Lt(t,e){return new _(`cast from ${t} to ${e} is not supported in v1`+(t==="utf8"||e==="utf8"?" (numeric\u2194utf8 conversion is not a kernel cast; use CSV inference / toString).":"."))}function Ut(t,e){let n=It(t,e),r=n?` Did you mean '${n}'?`:"",o=e.length?` Known columns: ${e.map(i=>`'${i}'`).join(", ")}.`:"";return new _(`unknown column '${t}'.${r}${o}`)}function X(t,e,n){return new _(`literal ${JSON.stringify(t)} is not a valid ${e} value for '${n}'.`)}function It(t,e){let n=null,r=1/0;for(let i of e){let a=sn(t,i);a<r&&(r=a,n=i);}let o=Math.max(2,Math.ceil(t.length/3));return n!==null&&r<=o?n:null}function sn(t,e){let n=t.length,r=e.length;if(n===0)return r;if(r===0)return n;let o=new Array(r+1),i=new Array(r+1);for(let a=0;a<=r;a++)o[a]=a;for(let a=1;a<=n;a++){i[0]=a;let s=t.charCodeAt(a-1);for(let l=1;l<=r;l++){let u=s===e.charCodeAt(l-1)?0:1;i[l]=Math.min(o[l]+1,i[l-1]+1,o[l-1]+u);}[o,i]=[i,o];}return o[r]}var ln=new Set(["f64","f32","i32","u32","i64"]),se=new Set(["i32","u32","i64"]),gt=new Set(["f64","f32"]),ot=new Set(["date32","timestamp"]);function G(t){return ln.has(t)}function jt(t,e){return Number.isInteger(t)?e==="i32"?t>=-2147483648&&t<=2147483647:t>=0&&t<=4294967295:false}function Rt(t){let e=t.node;return e.kind==="lit"&&e.dtype===null&&typeof e.value=="number"?e.value:null}function B(t,e){return {kind:"lit",value:t,dtype:e}}function Ot(t,e){return t.dtype===e?t:{kind:"cast",dtype:e,from:t.dtype,operand:t}}function wt(t,e){return E(t,e)}function qt(t,e){return wt(t,e).dtype}function E(t,e){let n=t.node;switch(n.kind){case "col":{let r=e.dtypeOf(n.name);if(r===void 0)throw Ut(n.name,e.columnNames());return {kind:"col",name:n.name,dtype:r}}case "lit":return n.dtype!==null?B(n.value,n.dtype):typeof n.value=="bigint"?B(n.value,"i64"):typeof n.value=="number"?B(n.value,Number.isInteger(n.value)?"i32":"f64"):B(n.value,typeof n.value=="string"?"utf8":"bool");case "arith":return un(n.op,n.left,n.right,e);case "neg":return fn(n.operand,e);case "compare":return hn(n.op,n.left,n.right,e);case "bool":return wn(n.op,n.left,n.right,e);case "not":return bn(n.operand,e);case "isNull":return {kind:"isNull",dtype:"bool",operand:E(n.operand,e)};case "fillNull":return xn(n.operand,n.value,e);case "cast":return Dn(n.operand,n.to,e);case "agg":return Cn(n.op,n.operand,e);case "dt":return dn(n.component,n.operand,e);case "strSlice":return yn(n.operand,n.start,n.end,e)}}function un(t,e,n,r){let o=Rt(e),i=Rt(n);if(o!==null&&i!==null){let u=Number.isInteger(o)?"i32":"f64",c=Number.isInteger(i)?"i32":"f64",m=u===c?u:"f64";return {kind:"arith",op:t,dtype:m,left:B(o,m),right:B(i,m)}}if(o!==null||i!==null){let u=o??i,c=E(o!==null?n:e,r),{opDtype:m,colTarget:f}=cn(t,u,c.dtype),p=Ot(c,f),d=B(u,m);return {kind:"arith",op:t,dtype:m,left:o!==null?d:p,right:o!==null?p:d}}let a=E(e,r),s=E(n,r),l=mn(t,a.dtype,s.dtype);return {kind:"arith",op:t,dtype:l,left:Ot(a,l),right:Ot(s,l)}}function cn(t,e,n){if(n==="timestamp")return {opDtype:"timestamp",colTarget:"timestamp"};if(n==="date32")return {opDtype:"date32",colTarget:"date32"};if(!G(n))throw P(t,n,"arithmetic");return gt.has(n)?{opDtype:n,colTarget:n}:n==="i64"?Number.isSafeInteger(e)?{opDtype:"i64",colTarget:"i64"}:{opDtype:"f64",colTarget:"f64"}:jt(e,n)?{opDtype:n,colTarget:n}:{opDtype:"f64",colTarget:"f64"}}function mn(t,e,n){if(ot.has(e)||ot.has(n))return pn(t,e,n);if(!G(e))throw P(t,e,"arithmetic");if(!G(n))throw P(t,n,"arithmetic");if(e===n)return e;if(e==="i64"&&(n==="i32"||n==="u32")||n==="i64"&&(e==="i32"||e==="u32"))return "i64";if(e==="i64"&&gt.has(n)||n==="i64"&&gt.has(e)||(e==="i32"||e==="u32")&&n==="f64"||(n==="i32"||n==="u32")&&e==="f64")return "f64";if((e==="i32"||e==="u32")&&n==="f32"||(n==="i32"||n==="u32")&&e==="f32")return "f32";throw tt(t,e,n,"insert an explicit .cast() (only int\u2192float widening is implicit).")}function pn(t,e,n){if(t==="sub"&&e==="timestamp"&&n==="timestamp")return "i64";if((t==="add"||t==="sub")&&e==="timestamp"&&se.has(n)||t==="add"&&se.has(e)&&n==="timestamp")return "timestamp";if(t==="sub"&&e==="date32"&&n==="date32")return "i32";if((t==="add"||t==="sub")&&e==="date32"&&(n==="i32"||n==="u32")||t==="add"&&(e==="i32"||e==="u32")&&n==="date32")return "date32";throw new _(`${t}(${e},${n})`)}function fn(t,e){let n=E(t,e);if(!G(n.dtype))throw P("neg",n.dtype);return {kind:"neg",dtype:n.dtype,operand:n}}function dn(t,e,n){let r=E(e,n);if(!ot.has(r.dtype))throw P("dt",r.dtype);return {kind:"dt",component:t,dtype:"i32",operand:r}}function yn(t,e,n,r){let o=E(t,r);if(o.dtype!=="utf8")throw P("str.slice",o.dtype,`str namespace requires a utf8 column; got ${o.dtype}`);return {kind:"strSlice",dtype:"utf8",operand:o,start:e,end:n}}function hn(t,e,n,r){let o=t==="eq"||t==="ne",i=yt(e)??yt(n),a=ht(e)??ht(n),s=Rt(e),l=Rt(n);if(s!==null&&l!==null){let f=Number.isInteger(s)&&Number.isInteger(l)?"i32":"f64";return et(t,f,B(s,f),B(l,f))}if(s!==null||l!==null){let f=s??l,p=E(s!==null?n:e,r);if(p.dtype==="date32"){let w=B(f,"date32");return et(t,"date32",s!==null?w:p,s!==null?p:w)}if(p.dtype==="timestamp"){let w=B(f,"timestamp");return et(t,"timestamp",s!==null?w:p,s!==null?p:w)}if(!G(p.dtype))throw tt(t,s!==null?"f64":p.dtype,s!==null?p.dtype:"f64","cannot compare a number to a non-numeric column.");let d=gn(f,p.dtype),h=Ot(p,d),y=B(f,d);return et(t,d,s!==null?y:h,s!==null?h:y)}if(i!==null){let f=E(yt(e)!==null?n:e,r);if(f.dtype!=="utf8")throw tt(t,"utf8",f.dtype,"cannot compare a string to a non-utf8 column.");if(!o)throw P(t,"utf8","string ordering");let p=B(i,"utf8");return et(t,"utf8",yt(e)!==null?p:f,yt(e)!==null?f:p)}if(a!==null){let f=E(ht(e)!==null?n:e,r);if(f.dtype!=="bool")throw tt(t,"bool",f.dtype,"cannot compare a boolean to a non-bool column.");if(!o)throw P(t,"bool");let p=B(a,"bool");return et(t,"bool",ht(e)!==null?p:f,ht(e)!==null?f:p)}let u=E(e,r),c=E(n,r);if(u.dtype!==c.dtype)throw tt(t,u.dtype,c.dtype,"cast");let m=u.dtype;if(m==="utf8"||m==="bool")throw P(t,m,"column-vs-column");return et(t,m,u,c)}function et(t,e,n,r){return {kind:"compare",op:t,dtype:"bool",operandDtype:e,left:n,right:r}}function gn(t,e){return gt.has(e)?e:e==="i64"?Number.isSafeInteger(t)?"i64":"f64":jt(t,e)?e:"f64"}function yt(t){let e=t.node;return e.kind==="lit"&&typeof e.value=="string"?e.value:null}function ht(t){let e=t.node;return e.kind==="lit"&&typeof e.value=="boolean"?e.value:null}function wn(t,e,n,r){let o=E(e,r),i=E(n,r);if(o.dtype!=="bool")throw P(t,o.dtype,"boolean");if(i.dtype!=="bool")throw P(t,i.dtype,"boolean");return {kind:"bool",op:t,dtype:"bool",left:o,right:i}}function bn(t,e){let n=E(t,e);if(n.dtype!=="bool")throw P("not",n.dtype,"boolean");return {kind:"not",dtype:"bool",operand:n}}function xn(t,e,n){let r=E(t,n);return An(e,r.dtype),{kind:"fillNull",dtype:r.dtype,operand:r,value:e}}function An(t,e){if(e==="utf8"){if(typeof t!="string")throw X(t,e,"fillNull");return}if(e==="bool"){if(typeof t!="boolean")throw X(t,e,"fillNull");return}if(e==="i64"||e==="timestamp"){if(typeof t!="bigint"&&typeof t!="number")throw X(t,e,"fillNull");if(typeof t=="number"&&!Number.isSafeInteger(t))throw X(t,e,"fillNull");return}if(typeof t!="number")throw X(t,e,"fillNull");if((e==="i32"||e==="u32")&&!jt(t,e))throw X(t,e,"fillNull")}var vn={f64:new Set(["f64","f32","i32","u32","bool","i64"]),f32:new Set(["f64","f32","i32","u32","bool","i64"]),i32:new Set(["f64","f32","i32","u32","bool","i64","date32"]),u32:new Set(["f64","f32","i32","u32","bool","i64"]),bool:new Set(["f64","f32","i32","u32","bool","i64"]),utf8:new Set(["utf8"]),i64:new Set(["f64","f32","i32","u32","bool","i64","timestamp"]),date32:new Set(["i32","timestamp","date32"]),timestamp:new Set(["i64","date32","timestamp"])};function Dn(t,e,n){let r=E(t,n),o=r.dtype;if(!vn[o]?.has(e))throw Lt(o,e);return {kind:"cast",dtype:e,from:o,operand:r}}function Cn(t,e,n){let r=E(e,n),o=bt(t,r.dtype);return {kind:"agg",op:t,dtype:o,operandDtype:r.dtype,operand:r}}function bt(t,e){switch(t){case "count":return "i32";case "nunique":if(G(e)||e==="utf8"||ot.has(e))return "i32";throw P("nunique",e);case "sum":if(gt.has(e))return e;if(e==="i64")return "i64";if(e==="i32"||e==="u32")return "f64";throw P("sum",e);case "mean":case "std":case "var":if(G(e))return "f64";throw P(t,e);case "min":case "max":if(G(e)||ot.has(e))return e;throw P(t,e);case "first":case "last":if(G(e)||e==="utf8"||ot.has(e))return e;throw P(t,e)}}function le(t,e,n){let r=t[e];if(typeof r!="function")throw new Error(`kernel export not found: ${e}`);return r(...n)}function ue(t,e,n){let r=t[e];if(typeof r!="function")throw new Error(`kernel export not found: ${e}`);return r(...n)}function St(t){let e=0,n=0;for(let r of t.allocs)r.purpose==="data"?e++:r.purpose==="mask"&&n++;return {kernelCalls:t.kernels.length,kernels:t.kernels.slice(),allocations:t.allocs.length,dataAllocations:e,maskAllocations:n,frees:t.frees}}var xt=class{ctx;wasm;len;owned=new Set;trace={kernels:[],allocs:[],frees:0};viewed=[];constructor(e){this.ctx=e.ctx,this.wasm=e.wasm,this.len=e.length;}get validityBytes(){return A(this.len)}alloc(e,n){let r=this.ctx.mod.alloc(e);if(r===0)throw new Error(`out of wasm memory allocating ${e} bytes`);return this.owned.add(r),this.trace.allocs.push({bytes:e,purpose:n}),r}track(e,n,r){e!==0&&(this.owned.add(e),this.trace.allocs.push({bytes:n,purpose:r}));}free(e){e!==0&&this.owned.delete(e)&&(this.forgetViews(e),this.ctx.mod.free(e),this.trace.frees++);}transfer(e){this.owned.delete(e),this.forgetViews(e);}freeAll(){for(let e of this.viewed)this.ctx.viewOf.forget(e);this.viewed.length=0;for(let e of this.owned)this.ctx.mod.free(e),this.trace.frees++;this.owned.clear();}call(e,...n){return this.trace.kernels.push(e),le(this.wasm,e,n)}callBigInt(e,...n){return this.trace.kernels.push(e),ue(this.wasm,e,n)}view(e,n,r){let o={ptr:e,length:n,dtype:r};return this.viewed.push(o),this.ctx.viewOf(o)}forgetViews(e){for(let n=this.viewed.length-1;n>=0;n--){let r=this.viewed[n];r.ptr===e&&(this.ctx.viewOf.forget(r),this.viewed.splice(n,1));}}},N={ptr:0,owns:false};function I(t,e){e.owns&&t.free(e.ptr);}function ce(t,e,n){if(e===0)return N;if((n&7)===0)return {ptr:e+(n>>3),owns:false};let r=t.validityBytes,o=t.alloc(r,"validity"),i=t.view(e,A(n+t.len),"u8"),a=t.view(o,r,"u8");a.fill(0);for(let s=0;s<t.len;s++)v(i,n+s)&&S(a,s);return {ptr:o,owns:true}}function me(t,e,n){if(n===0)return 0;let r=t.view(e,A(n),"u8"),o=0;for(let i=0;i<n;i++)r[i>>3]>>(i&7)&1&&o++;return o}var zt=86400000n,kn=[0,31,59,90,120,151,181,212,243,273,304,334];function pe(t){let e=t+719468,n=Math.floor(e/146097),r=e-n*146097,o=Math.floor((r-Math.floor(r/1460)+Math.floor(r/36524)-Math.floor(r/146096))/365),i=o+n*400,a=r-(365*o+Math.floor(o/4)-Math.floor(o/100)),s=Math.floor((5*a+2)/153),l=a-Math.floor((153*s+2)/5)+1,u=s<10?s+3:s-9;return {year:i+(u<=2?1:0),month:u,day:l}}function fe(t,e,n){let r=t-(e<=2?1:0),o=Math.floor(r/400),i=r-o*400,a=Math.floor((153*(e>2?e-3:e+9)+2)/5)+n-1,s=i*365+Math.floor(i/4)-Math.floor(i/100)+a;return o*146097+s-719468}function Pn(t){return t%4===0&&t%100!==0||t%400===0}function Mt(t,e,n){let r=(kn[e-1]??0)+n;return e>2&&Pn(t)?r+1:r}function _t(t){return ((t+3)%7+7)%7+1}function En(t){let e=t/zt;return t%zt!==0n&&t<0n&&(e-=1n),Number(e)}function de(t){let{year:e,month:n,day:r}=pe(t);return {year:e,month:n,day:r,hour:0,minute:0,second:0,millisecond:0,weekday:_t(t),dayOfYear:Mt(e,n,r),quarter:Math.ceil(n/3)}}function ye(t){let e=typeof t=="bigint"?t:BigInt(Math.trunc(t)),n=En(e),r=Number(e-BigInt(n)*zt),{year:o,month:i,day:a}=pe(n),s=Math.floor(r/36e5),l=r%36e5,u=Math.floor(l/6e4),c=l%6e4,m=Math.floor(c/1e3),f=c%1e3;return {year:o,month:i,day:a,hour:s,minute:u,second:m,millisecond:f,weekday:_t(n),dayOfYear:Mt(o,i,a),quarter:Math.ceil(i/3)}}var he=new Map;function Tn(t){let e=he.get(t);if(e!==void 0)return e;let n;try{n=new Intl.DateTimeFormat("en",{timeZone:t,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric",hourCycle:"h23"});}catch(r){throw new RangeError(`Invalid IANA timezone: "${t}". ${r instanceof Error?r.message:String(r)}`)}return he.set(t,n),n}function ge(t,e){let r=Tn(e).formatToParts(t),o=0,i=0,a=0,s=0,l=0,u=0;for(let f of r)switch(f.type){case "year":o=parseInt(f.value,10);break;case "month":i=parseInt(f.value,10);break;case "day":a=parseInt(f.value,10);break;case "hour":s=parseInt(f.value,10);break;case "minute":l=parseInt(f.value,10);break;case "second":u=parseInt(f.value,10);break}let c=(t%1e3+1e3)%1e3,m=fe(o,i,a);return {year:o,month:i,day:a,hour:s,minute:l,second:u,millisecond:c,weekday:_t(m),dayOfYear:Mt(o,i,a),quarter:Math.ceil(i/3)}}function In(t,e){return ((t[e>>3]??0)&1<<(e&7))!==0}function it(t,e,n,r){let o=t.length,i=new Int32Array(o),a=t instanceof Int32Array,s=typeof r=="string"&&r.length>0;for(let l=0;l<o;l++){if(e!==null&&!In(e,l))continue;let u;if(a)u=de(t[l]??0);else if(s){let c=Number(t[l]??0n);u=ge(c,r);}else {let c=t[l]??0n;u=ye(c);}i[l]=u[n];}return {data:i,validity:e}}function vt(t,e){let n=wt(t,e),r=n.kind==="agg"?"scalar":"column";return {dtype:n.dtype,resultKind:r,execute(){let o=new xt(e);try{if(n.kind==="agg")return {kind:"scalar",scalar:Gt(o,e,n),stats:St(o.trace)};let i=U(o,e,n);return {kind:"column",column:De(o,i),stats:St(o.trace)}}finally{o.freeAll();}}}}function Ht(t,e){let n=wt(t,e);if(n.dtype!=="bool")throw new _(`filter predicate must be boolean, got ${n.dtype}. Use a comparison / boolean expression.`);return {execute(){let r=new xt(e),o=Zn(r,U(r,e,n)),i;if(o.validity.ptr===0)i=o.maskPtr;else if(o.ownsMask)r.call("validity_and",o.maskPtr,o.validity.ptr,o.maskPtr,r.len),I(r,o.validity),i=o.maskPtr;else {let s=r.alloc(r.validityBytes,"mask");r.call("validity_and",o.maskPtr,o.validity.ptr,s,r.len),I(r,o.validity),i=s;}let a=me(r,i,r.len);return On(r,e,i,a)}}}function On(t,e,n,r){let o=null,i=()=>{if(o)return o;let a=new Int32Array(r),s=t.view(n,A(t.len),"u8"),l=0;for(let u=0;u<t.len;u++)s[u>>3]>>(u&7)&1&&(a[l++]=u);return o=a,a};return {count:r,compact(a){let s=x[a.dtype],l=e.ctx.mod.alloc(Math.max(r*s.size,1)),u=t.call(`filter_${Xn(a.dtype)}`,a.dataPtr,n,l,t.len);if(u!==r)throw new Error(`filter count mismatch: kernel ${u}, expected ${r}`);let c=0;if(a.validityPtr!==0){let f=i(),p=t.view(a.validityPtr,A(a.validityBitOffset+t.len),"u8"),d=0,h=A(r),y=e.ctx.mod.alloc(Math.max(h,1)),w=t.view(y,h,"u8");w.fill(0);for(let g=0;g<r;g++)v(p,a.validityBitOffset+f[g])?S(w,g):d++;d===0?(e.ctx.viewOf.forget({ptr:y,length:h,dtype:"u8"}),e.ctx.mod.free(y)):c=y;}let m=a.dict?Ce(e.ctx,a.dict):null;return {dtype:a.dtype,length:r,dataPtr:l,validityPtr:c,validityBitOffset:0,dict:m,owned:true}},free(){t.freeAll();},get stats(){return St(t.trace)}}}var Rn={gt:"lt",lt:"gt",ge:"le",le:"ge",eq:"eq",ne:"ne"},Sn=new Set(["add","mul"]),Mn=new Set(["f64_i32","f64_u32","f64_bool","f32_i32","f32_u32","f32_bool","i32_u32","u32_i32","f64_i64","f32_i64"]),Kt=86400000n;function U(t,e,n){switch(n.kind){case "col":return _n(t,e,n.name,n.dtype);case "lit":return Bt(t,n.value,n.dtype);case "arith":return Bn(t,e,n);case "neg":return Vn(t,e,n.dtype,n.operand);case "compare":return Fn(t,e,n);case "bool":return Un(t,e,n.op,n.left,n.right);case "not":return jn(t,e,n.operand);case "isNull":return qn(t,e,n.operand);case "fillNull":return zn(t,e,n.dtype,n.operand,n.value);case "cast":return Wn(t,e,n.from,n.dtype,n.operand);case "agg":{let r=Gt(t,e,n);return Bt(t,r.value,n.dtype)}case "dt":return Hn(t,e,n.component,n.operand);case "strSlice":return Gn(t,e,n.operand,n.start,n.end)}}function _n(t,e,n,r){let o=e.getColumn(n);if(!o)throw new _(`column '${n}' vanished during execution`);let i=ce(t,o.validityPtr,o.validityBitOffset);return r==="bool"?{rep:"boolcol",dataPtr:o.dataPtr,ownsData:false,validity:i}:{rep:"column",dtype:r,dataPtr:o.dataPtr,ownsData:false,validity:i,dict:o.dict,ownsDict:false}}function V(t,e,n){let r=U(t,e,n);if(r.rep!=="column")throw new Error(`internal: expected numeric column, got ${r.rep}`);return r}function At(t){return t.kind==="lit"||t.kind==="agg"||t.kind==="cast"&&At(t.operand)}function Bn(t,e,n){let r=n.dtype,o=At(n.left),i=At(n.right);if(o&&i){let l=Bt(t,K(t,e,n.left),r);return Yt(t,e,n.op,l,K(t,e,n.right),r)}if(i){let l=V(t,e,n.left);return Yt(t,e,n.op,l,K(t,e,n.right),r)}if(o){let l=V(t,e,n.right);return Nn(t,e,n.op,K(t,e,n.left),l,r)}let a=V(t,e,n.left),s=V(t,e,n.right);return xe(t,e,n.op,a,s,r)}function Yt(t,e,n,r,o,i){let a=x[i].size,s=r.ownsData?r.dataPtr:t.alloc(t.len*a,"data");if(o===null||(n==="div"||n==="mod")&&((i==="i32"||i==="u32")&&o===0||i==="i64"&&o===0n))return I(t,r.validity),nr(t,s,t.len*a),F(i,s,nt(t),null,false);let u=x[i].wasm;if(u==="i64"){let c=typeof o=="bigint"?o:BigInt(o);t.callBigInt(`${n}_i64_scalar`,r.dataPtr,c,s,t.len);}else t.call(`${n}_${u}_scalar`,r.dataPtr,o,s,t.len);return F(i,s,r.validity,null,false)}function Nn(t,e,n,r,o,i){if(Sn.has(n))return Yt(t,e,n,o,r,i);let a=Bt(t,r,i);return xe(t,e,n,a,o,i)}function xe(t,e,n,r,o,i){let a=x[i].size,s=(n==="div"||n==="mod")&&(i==="i32"||i==="u32"||i==="i64"),l=N;if(s){let f=t.alloc(t.validityBytes,"mask");i==="i64"?t.callBigInt("ne_i64_scalar_mask",o.dataPtr,0n,f,t.len):t.call(`ne_${i}_scalar_mask`,o.dataPtr,0,f,t.len),l={ptr:f,owns:true};}let u=r.ownsData?r.dataPtr:o.ownsData?o.dataPtr:t.alloc(t.len*a,"data"),c=x[i].wasm;t.call(`${n}_${c}`,r.dataPtr,o.dataPtr,u,t.len),u!==r.dataPtr&&r.ownsData&&t.free(r.dataPtr),u!==o.dataPtr&&o.ownsData&&t.free(o.dataPtr);let m=Wt(t,r.validity,o.validity);return s&&(m=Wt(t,m,l)),F(i,u,m,null,false)}function Vn(t,e,n,r){let o=V(t,e,r),i=x[n].size,a=o.ownsData?o.dataPtr:t.alloc(t.len*i,"data");return t.call(`neg_${n}`,o.dataPtr,a,t.len),F(n,a,o.validity,null,false)}function Fn(t,e,n){let r=n.operandDtype,o=At(n.left),i=At(n.right);if(o&&i){let c=K(t,e,n.left),m=K(t,e,n.right);return er(t,tr(n.op,c,m))}if(r==="bool")return Ln(t,e,n,o);if(r==="utf8")return $n(t,e,n,o);let a=x[r].wasm,s=t.alloc(t.validityBytes,"mask");if(o||i){let c=V(t,e,o?n.right:n.left),m=K(t,e,o?n.left:n.right);if(m===null)return I(t,c.validity),t.free(c.ownsData?c.dataPtr:0),t.view(s,t.validityBytes,"u8").fill(0),{rep:"mask",maskPtr:s,ownsMask:true,validity:nt(t)};let f=o?Rn[n.op]:n.op;if(a==="i64"){let p=typeof m=="bigint"?m:BigInt(m);t.callBigInt(`${f}_i64_scalar_mask`,c.dataPtr,p,s,t.len);}else t.call(`${f}_${a}_scalar_mask`,c.dataPtr,m,s,t.len);return c.ownsData&&t.free(c.dataPtr),{rep:"mask",maskPtr:s,ownsMask:true,validity:c.validity}}let l=V(t,e,n.left),u=V(t,e,n.right);return t.call(`${n.op}_${a}_mask`,l.dataPtr,u.dataPtr,s,t.len),l.ownsData&&t.free(l.dataPtr),u.ownsData&&t.free(u.dataPtr),{rep:"mask",maskPtr:s,ownsMask:true,validity:Wt(t,l.validity,u.validity)}}function $n(t,e,n,r){let o=V(t,e,r?n.right:n.left),i=K(t,e,r?n.left:n.right),a=t.alloc(t.validityBytes,"mask"),s=t.view(a,t.validityBytes,"u8");if(i===null)return s.fill(0),o.ownsData&&t.free(o.dataPtr),I(t,o.validity),o.ownsDict&&st(e,o.dict),{rep:"mask",maskPtr:a,ownsMask:true,validity:nt(t)};let u=(o.dict?Z(e.ctx,o.dict):[]).indexOf(i);return u>=0?t.call(`${n.op}_i32_scalar_mask`,o.dataPtr,u,a,t.len):s.fill(n.op==="ne"?255:0),o.ownsData&&t.free(o.dataPtr),o.ownsDict&&st(e,o.dict),{rep:"mask",maskPtr:a,ownsMask:true,validity:o.validity}}function Ln(t,e,n,r){let o=K(t,e,r?n.left:n.right),i=at(t,U(t,e,r?n.right:n.left));return (n.op==="eq"?o===false:o===true)?Ae(t,i):i}function Un(t,e,n,r,o){let i=at(t,U(t,e,r)),a=at(t,U(t,e,o)),s=n==="and"?"and_kleene":"or_kleene",l=i.ownsData?i.dataPtr:a.ownsData?a.dataPtr:t.alloc(t.len,"data"),u=i.validity.ptr===0&&a.validity.ptr===0,c;if(u){let m=t.alloc(t.validityBytes,"scratch");t.call(s,i.dataPtr,0,a.dataPtr,0,l,m,t.len),t.free(m),c=N;}else {let m=t.alloc(t.validityBytes,"validity");t.call(s,i.dataPtr,i.validity.ptr,a.dataPtr,a.validity.ptr,l,m,t.len),I(t,i.validity),I(t,a.validity),c={ptr:m,owns:true};}return l!==i.dataPtr&&i.ownsData&&t.free(i.dataPtr),l!==a.dataPtr&&a.ownsData&&t.free(a.dataPtr),{rep:"boolcol",dataPtr:l,ownsData:true,validity:c}}function jn(t,e,n){return Ae(t,at(t,U(t,e,n)))}function Ae(t,e){let n=e.ownsData?e.dataPtr:t.alloc(t.len,"data"),r;if(e.validity.ptr===0){let o=t.alloc(t.validityBytes,"scratch");t.call("not_bool",e.dataPtr,0,n,o,t.len),t.free(o),r=N;}else {let o=t.alloc(t.validityBytes,"validity");t.call("not_bool",e.dataPtr,e.validity.ptr,n,o,t.len),I(t,e.validity),r={ptr:o,owns:true};}return n!==e.dataPtr&&e.ownsData&&t.free(e.dataPtr),{rep:"boolcol",dataPtr:n,ownsData:true,validity:r}}function qn(t,e,n){let r=U(t,e,n),o=Jt(r),i=t.alloc(t.len,"data");return t.call("is_null",o.ptr,i,t.len),ve(t,e,r),{rep:"boolcol",dataPtr:i,ownsData:true,validity:N}}function zn(t,e,n,r,o){if(n==="bool")return Kn(t,e,r,o);if(n==="utf8")return Yn(t,e,r,o);let i=V(t,e,r);if(i.validity.ptr===0)return i;let a=x[n].size,s=i.ownsData?i.dataPtr:t.alloc(t.len*a,"data"),l=x[n].wasm;if(l==="i64"){let u=typeof o=="bigint"?o:BigInt(o);t.callBigInt("fill_null_i64",i.dataPtr,i.validity.ptr,u,s,t.len);}else t.call(`fill_null_${l}`,i.dataPtr,i.validity.ptr,o,s,t.len);return I(t,i.validity),F(n,s,N,null,false)}function Kn(t,e,n,r){let o=at(t,U(t,e,n));if(o.validity.ptr===0)return o;let i=o.ownsData?o.dataPtr:t.alloc(t.len,"data"),a=t.view(o.dataPtr,t.len,"u8"),s=t.view(o.validity.ptr,t.validityBytes,"u8"),l=t.view(i,t.len,"u8"),u=r?1:0;for(let c=0;c<t.len;c++)l[c]=v(s,c)?a[c]:u;return I(t,o.validity),{rep:"boolcol",dataPtr:i,ownsData:true,validity:N}}function Yn(t,e,n,r){let o=V(t,e,n);if(o.validity.ptr===0)return o;let i=o.dict?Z(e.ctx,o.dict):[],a=i.indexOf(r),s=o.dict,l=o.ownsDict;a<0&&(a=i.length,s=pt(e.ctx,[...i,r]),t.track(s.offsetsPtr,(s.count+1)*4,"dict"),t.track(s.bytesPtr,Math.max(s.bytesLen,1),"dict"),o.ownsDict&&st(e,o.dict),l=true);let u=o.ownsData?o.dataPtr:t.alloc(t.len*4,"data"),c=t.view(o.dataPtr,t.len,"i32"),m=t.view(o.validity.ptr,t.validityBytes,"u8"),f=t.view(u,t.len,"i32");for(let p=0;p<t.len;p++)f[p]=v(m,p)?c[p]:a;return I(t,o.validity),F("utf8",u,N,s,l)}function Wn(t,e,n,r,o){let i=U(t,e,o);if(n===r)return i;let a=(i.rep==="boolcol",i.dataPtr),s=(i.rep==="boolcol",i.ownsData),l=Jt(i);if(n==="date32"&&r==="i32"||n==="i32"&&r==="date32"||n==="timestamp"&&r==="i64"||n==="i64"&&r==="timestamp")return {...i,dtype:r};if(n==="date32"&&r==="timestamp"){let d=t.alloc(t.len*8,"data"),h=t.alloc(t.validityBytes,"scratch");return t.call("cast_i32_i64",a,l.ptr,d,h,t.len),t.free(h),t.callBigInt("mul_i64_scalar",d,Kt,d,t.len),s&&t.free(a),F("timestamp",d,l,null,false)}if(n==="timestamp"&&r==="date32"){let d=t.view(a,t.len,"i64"),h=t.alloc(t.len*4,"data"),y=t.view(h,t.len,"i32");for(let w=0;w<t.len;w++){let g=d[w],b=g/Kt;g%Kt!==0n&&g<0n&&(b-=1n),y[w]=Number(b);}return s&&t.free(a),F("date32",h,l,null,false)}let u=x[r].size,m=s&&x[n].size===u?a:t.alloc(t.len*u,"data"),f=Mn.has(`${n}_${r}`),p;if(f){let d=t.alloc(t.validityBytes,"validity");t.call(`cast_${n}_${r}`,a,l.ptr,m,d,t.len),I(t,l),p={ptr:d,owns:true};}else {let d=t.alloc(t.validityBytes,"scratch");t.call(`cast_${n}_${r}`,a,l.ptr,m,d,t.len),t.free(d),p=l;}return m!==a&&s&&t.free(a),r==="bool"?{rep:"boolcol",dataPtr:m,ownsData:true,validity:p}:F(r,m,p,null,false)}function Hn(t,e,n,r){let o=V(t,e,r),i=t.len,a;if(r.kind==="col"){let m=e.getColumn(r.name);m?.tz&&(a=m.tz);}let s=null;o.validity.ptr!==0&&(s=t.view(o.validity.ptr,t.validityBytes,"u8"));let l;if(o.dtype==="date32"){let m=t.view(o.dataPtr,i,"i32");l=it(m,s,n);}else {let m=t.view(o.dataPtr,i,"i64");l=it(m,s,n,a);}let u=t.alloc(i*4,"data");return t.view(u,i,"i32").set(l.data),o.ownsData&&t.free(o.dataPtr),o.ownsDict&&o.dict&&st(e,o.dict),F("i32",u,o.validity,null,false)}function Gn(t,e,n,r,o){let i=V(t,e,n),a=t.len,s=i.dict?Z(e.ctx,i.dict):[],l=s.length,u=new Int32Array(l),c=[],m=new Map;for(let y=0;y<l;y++){let w=o===void 0?s[y].slice(r):s[y].slice(r,o),g=m.get(w);g===void 0&&(g=c.length,c.push(w),m.set(w,g)),u[y]=g;}let f=pt(e.ctx,c);t.track(f.offsetsPtr,(f.count+1)*4,"dict"),t.track(f.bytesPtr,Math.max(f.bytesLen,1),"dict");let p=i.ownsData?i.dataPtr:t.alloc(a*4,"data"),d=t.view(i.dataPtr,a,"i32"),h=t.view(p,a,"i32");for(let y=0;y<a;y++)h[y]=u[d[y]];return p!==i.dataPtr&&i.ownsData&&t.free(i.dataPtr),i.ownsDict&&st(e,i.dict),F("utf8",p,i.validity,f,true)}function Gt(t,e,n){let r=U(t,e,n.operand),o=Jt(r),i=r.rep==="mask"?0:(r.rep==="boolcol",r.dataPtr),a=r.rep==="column"?r.dict:null,s=n.operandDtype,l=s==="utf8"?"i32":s,u=t.len,c=o.ptr===0?u:t.call("count_null",o.ptr,u),m=Jn(t,e,n.op,s,l,i,o.ptr,u,c,a);return ve(t,e,r),{value:m,dtype:n.dtype}}function Jn(t,e,n,r,o,i,a,s,l,u){let c=r==="utf8"?"i32":x[r].wasm,m=f=>o==="u32"?f>>>0:f;switch(n){case "count":return l;case "nunique":return t.call(`nunique_${c}_null`,i,a,s);case "sum":return c==="i64"?t.callBigInt("sum_i64_null",i,a,s):t.call(`sum_${r}_null`,i,a,s);case "mean":return l>=1?t.call(`mean_${r}_null`,i,a,s):null;case "std":return l>=2?t.call(`std_${r}_null`,i,a,s):null;case "var":return l>=2?t.call(`var_${r}_null`,i,a,s):null;case "min":case "max":return l?c==="i64"?t.callBigInt(`${n}_i64_null`,i,a,s):m(t.call(`${n}_${c}_null`,i,a,s)):null;case "first":case "last":{let f=t.alloc(4,"scratch");t.view(f,1,"i32")[0]=0;let p;c==="i64"?p=t.callBigInt(`${n}_i64_null`,i,a,s,f):p=t.call(`${n}_${c}_null`,i,a,s,f);let d=t.view(f,1,"i32")[0]!==0;return t.free(f),d?c==="i64"?p:r==="utf8"?j(e.ctx,u,p):m(p):null}}}function at(t,e){if(e.rep==="boolcol")return e;if(e.rep==="mask"){let n=t.alloc(t.len,"data");return t.call("expand_mask_bool",e.maskPtr,n,t.len),e.ownsMask&&t.free(e.maskPtr),{rep:"boolcol",dataPtr:n,ownsData:true,validity:e.validity}}throw new Error("internal: expected a boolean value")}function Zn(t,e){if(e.rep==="mask")return e;if(e.rep==="boolcol"){let n=t.alloc(t.validityBytes,"mask"),r=t.view(e.dataPtr,t.len,"u8"),o=t.view(n,t.validityBytes,"u8");o.fill(0);for(let i=0;i<t.len;i++)r[i]&&S(o,i);return e.ownsData&&t.free(e.dataPtr),{rep:"mask",maskPtr:n,ownsMask:true,validity:e.validity}}throw new Error("internal: expected a boolean value")}function Jt(t){return t.validity}function Xn(t){return t==="bool"?"u8":t==="utf8"?"i32":x[t].wasm}function K(t,e,n){if(n.kind==="lit")return n.dtype==="i64"&&typeof n.value=="number"?BigInt(n.value):n.value;if(n.kind==="agg")return Gt(t,e,n).value;if(n.kind==="cast")return Qn(K(t,e,n.operand),n.from,n.dtype);throw new Error(`internal: ${n.kind} is not a scalar operand`)}var we=9223372036854776e3;function Qn(t,e,n){if(t===null)return null;if(e===n)return t;if(e==="i64"){let i=t;return n==="f64"?Number(i):n==="f32"?Math.fround(Number(i)):n==="i32"?Number(BigInt.asIntN(32,i)):n==="u32"?Number(BigInt.asUintN(32,i)):n==="bool"?i!==0n:null}if(n==="i64"){if(typeof t=="boolean")return t?1n:0n;let i=t;if(!Number.isFinite(i))return null;let a=Math.trunc(i);return a>=we||a<-we?null:BigInt(a)}if(n==="bool")return typeof t=="number"&&Number.isNaN(t)?null:t!==0&&t!==false;let r=typeof t=="boolean"?t?1:0:t;if(n==="f64")return r;if(n==="f32")return Math.fround(r);if(!Number.isFinite(r))return null;let o=Math.trunc(r);return n==="i32"?o>=-2147483648&&o<=2147483647?o:null:r<0?null:o<=4294967295?o:null}function tr(t,e,n){if(e===null||n===null)return null;let r=e,o=n;switch(t){case "gt":return r>o;case "ge":return r>=o;case "lt":return r<o;case "le":return r<=o;case "eq":return r===o;case "ne":return r!==o}}function Bt(t,e,n){if(n==="utf8")throw new _("broadcasting a utf8 scalar to a column is not supported in P3.1.");if(n==="bool"){let s=t.alloc(t.len,"data");return t.view(s,t.len,"u8").fill(e===null?0:e?1:0),{rep:"boolcol",dataPtr:s,ownsData:true,validity:e===null?nt(t):N}}let r=x[n].size,o=t.alloc(t.len*r,"data");if(n==="i64"||n==="timestamp"){let s=t.view(o,t.len,"i64"),l=e===null?0n:typeof e=="bigint"?e:BigInt(e);for(let u=0;u<t.len;u++)s[u]=l;return F(n,o,e===null?nt(t):N,null,false)}let i=t.view(o,t.len,x[n].view),a=e===null?0:e;for(let s=0;s<t.len;s++)i[s]=a;return F(n,o,e===null?nt(t):N,null,false)}function er(t,e){let n=t.alloc(t.validityBytes,"mask");return t.view(n,t.validityBytes,"u8").fill(e===true?255:0),{rep:"mask",maskPtr:n,ownsMask:true,validity:e===null?nt(t):N}}function Wt(t,e,n){if(e.ptr===0&&n.ptr===0)return N;if(n.ptr===0)return e;if(e.ptr===0)return n;let r=t.alloc(t.validityBytes,"validity");return t.call("validity_and",e.ptr,n.ptr,r,t.len),I(t,e),I(t,n),{ptr:r,owns:true}}function nt(t){let e=t.alloc(t.validityBytes,"validity");return t.view(e,t.validityBytes,"u8").fill(0),{ptr:e,owns:true}}function nr(t,e,n){t.view(e,n,"u8").fill(0);}function F(t,e,n,r,o){return {rep:"column",dtype:t,dataPtr:e,ownsData:true,validity:n,dict:r,ownsDict:o}}function ve(t,e,n){if(n.rep==="mask"){n.ownsMask&&t.free(n.maskPtr),I(t,n.validity);return}if(n.rep==="boolcol"){n.ownsData&&t.free(n.dataPtr),I(t,n.validity);return}n.ownsData&&t.free(n.dataPtr),I(t,n.validity),n.ownsDict&&st(e,n.dict);}function st(t,e){e&&(t.ctx.viewOf.forget({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"}),t.ctx.viewOf.forget({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"}),t.ctx.mod.free(e.offsetsPtr),t.ctx.mod.free(e.bytesPtr));}function De(t,e){if(e.rep==="mask")return De(t,at(t,e));let n=t.ctx,r=e.rep==="boolcol"?"bool":e.dtype,o=x[r],i;e.ownsData?(i=e.dataPtr,t.transfer(e.dataPtr)):(i=n.mod.alloc(Math.max(t.len*o.size,1)),be(t,e.dataPtr,i,t.len*o.size));let a=e.validity,s=0;a.ptr!==0&&(a.owns?(s=a.ptr,t.transfer(a.ptr)):(s=n.mod.alloc(Math.max(t.validityBytes,1)),be(t,a.ptr,s,t.validityBytes)));let l=null;return e.rep==="column"&&e.dict&&(e.ownsDict?(l=e.dict,t.transfer(e.dict.offsetsPtr),t.transfer(e.dict.bytesPtr)):l=Ce(n,e.dict)),{dtype:r,length:t.len,dataPtr:i,validityPtr:s,validityBitOffset:0,dict:l,owned:true}}function be(t,e,n,r){if(r<=0)return;let o=t.view(e,r,"u8");t.view(n,r,"u8").set(o);}function Ce(t,e){let n=(e.count+1)*4,r=t.mod.alloc(Math.max(n,1)),o=t.mod.alloc(Math.max(e.bytesLen,1));return t.viewOf({ptr:r,length:e.count+1,dtype:"i32"}).set(t.viewOf({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"})),e.bytesLen>0&&t.viewOf({ptr:o,length:e.bytesLen,dtype:"u8"}).set(t.viewOf({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"})),{count:e.count,offsetsPtr:r,bytesPtr:o,bytesLen:e.bytesLen}}var O=class extends Error{constructor(e){super(e),this.name="FrameError";}};function J(t,e){let n=It(t,e),r=n?` Did you mean '${n}'?`:"",o=e.length?` Available columns: ${e.map(i=>`'${i}'`).join(", ")}.`:"";return new O(`unknown column '${t}'.${r}${o}`)}function ke(t,e,n,r){let o=r?` ${r}`:"";return new O(`dtype mismatch in '${t}': ${e} vs ${n}.${o}`)}function C(t,e){let n=t[e];if(typeof n!="function")throw new O(`kernel export not found: ${e}`);return n}typeof process<"u"&&process.versions!=null&&process.versions.node!=null;function Zt(){throw new O("no DataFrame runtime is loaded \u2014 call `await init()` first (or pass a runtime).");}var Dt=class{col;ctx;refs;constructor(e,n){this.ctx=e,this.col=n,this.refs=1;}retain(){return this.refs++,this}release(){this.refs<=0||--this.refs===0&&Q(this.ctx,this.col);}};function Xt(t){if(t instanceof BigInt64Array)return "i64";if(t instanceof Float64Array)return "f64";if(t instanceof Float32Array)return "f32";if(t instanceof Int32Array)return "i32";if(t instanceof Uint32Array)return "u32";if(t instanceof Uint8Array)return "bool";let e=t;for(let n=0;n<e.length;n++){let r=e[n];if(r!=null){if(typeof r=="bigint")return "i64";if(typeof r=="number")return "f64";if(typeof r=="boolean")return "bool";if(typeof r=="string")return "utf8"}}return "f64"}var rr=new Set(["year","month","day","weekday","dayOfYear","quarter"]),Qt=class{constructor(e){this.series=e;}series;extract(e){let{dtype:n}=this.series;if(n==="date32"){if(!rr.has(e))throw new TypeError(`dt.${e} is not supported for date32 columns (time-of-day fields are always 0 for date32; use year/month/day/weekday/dayOfYear/quarter, or cast to timestamp first).`)}else if(n!=="timestamp")throw new TypeError(`dt accessor requires a date32 or timestamp column, got ${n}.`);let r=this.series.col,o=this.series.ctx,i=r.length,a=n==="timestamp"&&r.tz?r.tz:void 0,s=null;if(r.validityPtr!==0){let m=r.validityBitOffset,f=Math.ceil((m+i)/8),p=o.viewOf({ptr:r.validityPtr,length:f,dtype:"u8"});if(m===0)s=p;else {let d=new Uint8Array(Math.ceil(i/8));for(let h=0;h<i;h++){let y=m+h;p[y>>3]>>(y&7)&1&&(d[h>>3]|=1<<(h&7));}s=d;}}let l;if(n==="date32"){let m=o.viewOf({ptr:r.dataPtr,length:i,dtype:"i32"});({data:l}=it(m,s,e));}else {let m=o.viewOf({ptr:r.dataPtr,length:i,dtype:"i64"});({data:l}=it(m,s,e,a));}let u=new Array(i);if(s!==null)for(let m=0;m<i;m++){let f=(s[m>>3]??0)>>(m&7)&1;u[m]=f?l[m]??0:null;}else for(let m=0;m<i;m++)u[m]=l[m]??0;let c=D(o,"i32",u);return new lt(o,`${this.series.name}.dt.${e}`,c)}year(){return this.extract("year")}month(){return this.extract("month")}day(){return this.extract("day")}hour(){return this.extract("hour")}minute(){return this.extract("minute")}second(){return this.extract("second")}millisecond(){return this.extract("millisecond")}weekday(){return this.extract("weekday")}dayOfYear(){return this.extract("dayOfYear")}quarter(){return this.extract("quarter")}},te=class{constructor(e){this.series=e;}series;slice(e,n){let{dtype:r,col:o,name:i}=this.series,a=this.series.ctx;if(r!=="utf8")throw new TypeError(`str.slice requires a utf8 column, got ${r}.`);let s=o.length,l=o.dict?Z(a,o.dict):[],u=l.length,c=new Int32Array(u),m=[],f=new Map;for(let y=0;y<u;y++){let w=n===void 0?l[y].slice(e):l[y].slice(e,n),g=f.get(w);g===void 0&&(g=m.length,m.push(w),f.set(w,g)),c[y]=g;}let p=a.viewOf({ptr:o.dataPtr,length:s,dtype:"i32"}),d=new Array(s);if(o.validityPtr===0)for(let y=0;y<s;y++)d[y]=m[c[p[y]]];else {let y=o.validityBitOffset,w=A(y+s),g=a.viewOf({ptr:o.validityPtr,length:w,dtype:"u8"});for(let b=0;b<s;b++)v(g,y+b)?d[b]=m[c[p[b]]]:d[b]=null;}let h=D(a,"utf8",d);return new lt(a,`${i}.str.slice(${e}${n===void 0?"":`, ${n}`})`,h)}},lt=class{name;dtype;length;ctx;column;constructor(e,n,r){this.ctx=e,this.name=n,this.dtype=r.dtype,this.length=r.length,this.column=r;}get col(){return this.column}toArray(){return H(this.ctx,this.column)}get(e){return e<0||e>=this.length?null:this.toArray()[e]??null}values(){return this.ctx.viewOf({ptr:this.column.dataPtr,length:this.length,dtype:x[this.dtype].view})}get dt(){if(this.dtype!=="date32"&&this.dtype!=="timestamp")throw new TypeError(`dt accessor requires a date32 or timestamp column, got ${this.dtype}.`);return new Qt(this)}get str(){if(this.dtype!=="utf8")throw new TypeError(`str accessor requires a utf8 column, got ${this.dtype}.`);return new te(this)}[Symbol.iterator](){return this.toArray()[Symbol.iterator]()}toString(){return `Series '${this.name}' (${this.dtype}, ${this.length} rows)`}};function Ee(t){return t==="bool"?"u8":t==="utf8"?"i32":x[t].wasm}function ut(t,e){if(e.validityPtr===0)return {ptr:0,owns:false};let n=e.validityBitOffset;if((n&7)===0)return {ptr:e.validityPtr+(n>>3),owns:false};let r=e.length,o=A(r),i=t.mod.alloc(Math.max(o,1)),a=t.viewOf({ptr:e.validityPtr,length:A(n+r),dtype:"u8"}),s=t.viewOf({ptr:i,length:o,dtype:"u8"});s.fill(0);for(let l=0;l<r;l++)v(a,n+l)&&S(s,l);return {ptr:i,owns:true}}function ct(t,e){e.owns&&e.ptr!==0&&(t.viewOf.forget({ptr:e.ptr,length:1,dtype:"u8"}),t.mod.free(e.ptr));}function Te(t,e){let n=(e.count+1)*4,r=t.mod.alloc(Math.max(n,1)),o=t.mod.alloc(Math.max(e.bytesLen,1));return t.viewOf({ptr:r,length:e.count+1,dtype:"i32"}).set(t.viewOf({ptr:e.offsetsPtr,length:e.count+1,dtype:"i32"})),e.bytesLen>0&&t.viewOf({ptr:o,length:e.bytesLen,dtype:"u8"}).set(t.viewOf({ptr:e.bytesPtr,length:e.bytesLen,dtype:"u8"})),{count:e.count,offsetsPtr:r,bytesPtr:o,bytesLen:e.bytesLen}}function Ie(t,e){return Math.max(e*x[t].size,1)}function ee(t,e,n,r){let{ctx:o,wasm:i}=t,a=Ee(e.dtype),s=o.mod.alloc(Ie(e.dtype,r));C(i,`gather_${a}`)(e.dataPtr,n,r,s);let l=0;if(e.validityPtr!==0){let c=ut(o,e),m=A(r),f=o.mod.alloc(Math.max(m,1));o.viewOf({ptr:f,length:Math.max(m,1),dtype:"u8"}).fill(0),C(i,"gather_validity")(c.ptr,n,r,f),ct(o,c),l=f;}let u=e.dict?Te(o,e.dict):null;return {dtype:e.dtype,length:r,dataPtr:s,validityPtr:l,validityBitOffset:0,dict:u,owned:true}}function Oe(t,e,n){let{ctx:r,wasm:o}=t,i=e.length>0?e[0].length:0,a=r.mod.alloc(Math.max(i*4,1)),s=r.viewOf({ptr:a,length:i,dtype:"i32"});for(let c=0;c<i;c++)s[c]=c;let l=C(o,"argsort_i32").length>=6,u=l?r.mod.alloc(Math.max(i*4,1)):0;try{for(let c=e.length-1;c>=0;c--)or(t,e[c],n[c]===!0,a,i,u,l);}finally{u!==0&&(r.viewOf.forget({ptr:u,length:i,dtype:"i32"}),r.mod.free(u));}return {ptr:a,len:i}}function ne(t,e,n,r,o,i,a,s,l){let u=C(t,e);l?u(n,r,o,i,a,s):u(n,r,o,i,a);}function or(t,e,n,r,o,i,a){let{ctx:s,wasm:l}=t,u=n?1:0,c=ut(s,e);try{if(e.dtype==="utf8"){let m=ir(t,e,o);try{ne(l,"argsort_i32",m,c.ptr,r,o,u,i,a);}finally{s.viewOf.forget({ptr:m,length:o,dtype:"i32"}),s.mod.free(m);}}else if(e.dtype==="bool"){let m=ar(t,e,o);try{ne(l,"argsort_i32",m,c.ptr,r,o,u,i,a);}finally{s.viewOf.forget({ptr:m,length:o,dtype:"i32"}),s.mod.free(m);}}else {let m=x[e.dtype].wasm;ne(l,`argsort_${m}`,e.dataPtr,c.ptr,r,o,u,i,a);}}finally{ct(s,c);}}function ir(t,e,n){let{ctx:r}=t,o=e.dict?Z(r,e.dict):[],i=o.map((c,m)=>m).sort((c,m)=>{let f=o[c],p=o[m];return f<p?-1:f>p?1:0}),a=new Int32Array(o.length);for(let c=0;c<i.length;c++)a[i[c]]=c;let s=r.mod.alloc(Math.max(n*4,1)),l=r.viewOf({ptr:e.dataPtr,length:n,dtype:"i32"}),u=r.viewOf({ptr:s,length:n,dtype:"i32"});for(let c=0;c<n;c++){let m=l[c];u[c]=m>=0&&m<a.length?a[m]:0;}return s}function ar(t,e,n){let{ctx:r}=t,o=r.mod.alloc(Math.max(n*4,1)),i=r.viewOf({ptr:e.dataPtr,length:n,dtype:"bool"}),a=r.viewOf({ptr:o,length:n,dtype:"i32"});for(let s=0;s<n;s++)a[s]=i[s];return o}function re(t,e){let n=0,r={};for(let{name:o,col:i}of e){let a=i.length,s=i.validityBitOffset,l=i.validityPtr===0?null:t.viewOf({ptr:i.validityPtr,length:A(s+a),dtype:"u8"});if(i.dtype==="utf8"){let u=t.viewOf({ptr:i.dataPtr,length:a,dtype:"i32"}),c=i.dict;Object.defineProperty(r,o,{enumerable:true,get(){return l&&!v(l,s+n)?null:j(t,c,u[n])}});}else if(i.dtype==="bool"){let u=t.viewOf({ptr:i.dataPtr,length:a,dtype:"bool"});Object.defineProperty(r,o,{enumerable:true,get(){return l&&!v(l,s+n)?null:u[n]!==0}});}else if(i.dtype==="i64"){let u=t.viewOf({ptr:i.dataPtr,length:a,dtype:"i64"});Object.defineProperty(r,o,{enumerable:true,get(){return l&&!v(l,s+n)?null:u[n]}});}else {let u=t.viewOf({ptr:i.dataPtr,length:a,dtype:x[i.dtype].view});Object.defineProperty(r,o,{enumerable:true,get(){return l&&!v(l,s+n)?null:u[n]}});}}return {at(o){return n=o,r}}}function Re(t){if(t<=1)return 1;let e=1;for(;e<t;)e<<=1;return e}var Se=4,Ct=16;function kt(t,e){if(e===0)return t.alloc(0);let n=t.alloc(e);if(n===0)throw new Error(`[hash stubs] OOM: alloc(${e}) returned 0`);return new Uint8Array(t.memory.buffer,n,e).fill(0),n}function Me(t,e,n,r,o){let i=Math.max(Se,Re(2*Math.max(n,1))),a=0;try{for(a=kt(t,i*Ct);;){let s=t.group_build(e,n,a,i,r);if(s!==-1)return s;t.free(a),a=0,i*=2,a=kt(t,i*Ct);}}finally{a!==0&&t.free(a);}}function _e(t,e,n,r,o,i,a,s,l){let u=Math.max(Se,Re(2*Math.max(s,1))),c=Math.max(1,o+s),m=0,f=0,p=0;try{if(m=kt(t,u*Ct),f=t.alloc(c*4),f===0)throw new Error("[hash stubs] OOM: out_l_idx alloc failed");if(p=t.alloc(c*4),p===0)throw new Error("[hash stubs] OOM: out_r_idx alloc failed");for(;;){let d=e(n,r,o,i,a,s,m,u,f,p,c);if(d===-1){t.free(m),m=0,u*=2,m=kt(t,u*Ct);continue}if(d>c){if(t.free(f),f=0,t.free(p),p=0,c=d,f=t.alloc(c*4),f===0)throw new Error("[hash stubs] OOM: out_l_idx resize failed");if(p=t.alloc(c*4),p===0)throw new Error("[hash stubs] OOM: out_r_idx resize failed");t.free(m),m=0,m=kt(t,u*Ct);continue}let h=new Int32Array(t.memory.buffer,f,d).slice(),y=new Int32Array(t.memory.buffer,p,d).slice();return {count:d,lIdx:h,rIdx:y}}}finally{m!==0&&t.free(m),f!==0&&t.free(f),p!==0&&t.free(p);}}function Be(t,e,n,r,o,i,a,s){return _e(t,(l,u,c,m,f,p,d,h,y,w,g)=>t.join_hash_inner(l,u,c,m,f,p,d,h,y,w,g),e,n,r,o,i,a)}function Ne(t,e,n,r,o,i,a,s){return _e(t,(l,u,c,m,f,p,d,h,y,w,g)=>t.join_hash_left(l,u,c,m,f,p,d,h,y,w,g),e,n,r,o,i,a)}var sr=new Set(["sum","mean","min","max","count","size","nunique","std","var","first","last"]),Nt=class{src;keys;constructor(e,n){this.src=e,this.keys=n;for(let r of n)if(e.dtypeOf(r)===void 0)throw J(r,e.columnNames())}agg(e){let{rt:n}=this.src,{ctx:r}=n,o=this.src.length,i=this.keys.map(u=>this.src.getColumn(u)),a=this.buildPlan(e),s=ur(n,i),l=[];try{let u=s.firstRow,c=[];for(let p=0;p<this.keys.length;p++){let d=mr(n,i[p],u);l.push(d),c.push({name:this.keys[p],col:d});}let m=r.viewOf({ptr:s.groupIdsPtr,length:o,dtype:"i32"});for(let p of a){let d=pr(n,p,m,s.groupCount,o);l.push(d),c.push({name:p.outName,col:d});}let f=this.src.buildResult(c);return l.length=0,f}finally{for(let u of l)Q(r,u);for(let u of a)u.ownsOperand&&u.operand&&Q(r,u.operand);r.viewOf.forget({ptr:s.groupIdsPtr,length:o,dtype:"i32"}),r.mod.free(s.groupIdsPtr);}}buildPlan(e){let n=[];for(let[r,o]of Object.entries(e))if(o instanceof M)n.push(this.exprPlan(r,o));else if(Array.isArray(o))for(let i of o)n.push(this.columnPlan(`${r}_${i}`,r,i));else n.push(this.columnPlan(r,r,o));return n}columnPlan(e,n,r){if(!sr.has(r))throw new O(`unknown aggregation '${r}' for '${n}'.`);if(r==="size")return {outName:e,op:r,operand:null,operandDtype:"i32",ownsOperand:false,resultDtype:"i32"};let o=this.src.getColumn(n);if(!o)throw J(n,this.src.columnNames());let i=lr(r,o.dtype);return {outName:e,op:r,operand:o,operandDtype:o.dtype,ownsOperand:false,resultDtype:i}}exprPlan(e,n){let r=n.node;if(r.kind!=="agg")throw new O(`agg value for '${e}' must be a top-level aggregation (e.g. col('a').sum()).`);let o=r.op,i=qt(r.operand,this.src),a=bt(o,i),s=vt(r.operand,this.src).execute();if(s.kind!=="column"||!s.column)throw new O(`aggregation operand for '${e}' did not produce a column.`);return {outName:e,op:o,operand:s.column,operandDtype:i,ownsOperand:true,resultDtype:a}}};function lr(t,e){return t==="size"?"i32":bt(t,e)}function ur(t,e){let{ctx:n,wasm:r}=t,o=e.length>0?e[0].length:0,i=n.mod.alloc(Math.max(o*8,1));try{for(let m=0;m<e.length;m++){let f=e[m],p=cr(t,f,o),d=ut(n,f);try{if(m===0)C(r,`hash_${p.tok}`)(p.ptr,d.ptr,i,o);else {let h=n.mod.alloc(Math.max(o*8,1));try{C(r,`hash_${p.tok}`)(p.ptr,d.ptr,h,o),C(r,"hash_combine")(i,h,o);}finally{n.viewOf.forget({ptr:h,length:o,dtype:"u8"}),n.mod.free(h);}}}finally{ct(n,d),p.free();}}let a=n.mod.alloc(Math.max(o*4,1)),s=Me(r,i,o,a),l=n.viewOf({ptr:a,length:o,dtype:"i32"}),u=new Int32Array(s),c=0;for(let m=0;m<o&&c<s;m++)l[m]===c&&(u[c++]=m);return {groupIdsPtr:a,groupCount:s,firstRow:u}}finally{n.viewOf.forget({ptr:i,length:o,dtype:"u8"}),n.mod.free(i);}}function cr(t,e,n){let{ctx:r}=t;if(e.dtype==="utf8")return {ptr:e.dataPtr,tok:"i32",free(){}};if(e.dtype==="bool"){let o=r.mod.alloc(Math.max(n*4,1)),i=r.viewOf({ptr:e.dataPtr,length:n,dtype:"bool"}),a=r.viewOf({ptr:o,length:n,dtype:"i32"});for(let s=0;s<n;s++)a[s]=i[s];return {ptr:o,tok:"i32",free(){r.viewOf.forget({ptr:o,length:n,dtype:"i32"}),r.mod.free(o);}}}return {ptr:e.dataPtr,tok:x[e.dtype].wasm,free(){}}}function mr(t,e,n){let{ctx:r}=t,o=n.length,i=e.validityPtr===0?null:r.viewOf({ptr:e.validityPtr,length:A(e.validityBitOffset+e.length),dtype:"u8"}),a=e.validityBitOffset;if(e.dtype==="utf8"){let c=r.viewOf({ptr:e.dataPtr,length:e.length,dtype:"i32"}),m=e.dict,f=new Array(o);for(let p=0;p<o;p++){let d=n[p];f[p]=i&&!v(i,a+d)?null:j(r,m,c[d]);}return D(r,"utf8",f)}if(e.dtype==="i64"){let c=r.viewOf({ptr:e.dataPtr,length:e.length,dtype:"i64"}),m=new Array(o);for(let f=0;f<o;f++){let p=n[f];m[f]=i&&!v(i,a+p)?null:c[p];}return D(r,"i64",m)}let s=e.dtype==="bool",l=r.viewOf({ptr:e.dataPtr,length:e.length,dtype:x[e.dtype].view}),u=new Array(o);for(let c=0;c<o;c++){let m=n[c];i&&!v(i,a+m)?u[c]=null:u[c]=s?l[m]!==0:l[m];}return D(r,e.dtype,u)}function pr(t,e,n,r,o){let{ctx:i}=t,{op:a}=e;if(a==="size"){let w=new Array(r).fill(0);for(let g=0;g<o;g++){let b=n[g];w[b]=w[b]+1;}return D(i,"i32",w)}let s=e.operand,l=s.validityPtr===0?null:i.viewOf({ptr:s.validityPtr,length:A(s.validityBitOffset+o),dtype:"u8"}),u=s.validityBitOffset,c=w=>l===null||v(l,u+w);if(a==="count"){let w=new Array(r).fill(0);for(let g=0;g<o;g++)if(c(g)){let b=n[g];w[b]=w[b]+1;}return D(i,"i32",w)}if(a==="first"||a==="last")return yr(t,e,n,r,o,c);if(a==="nunique")return dr(t,e,n,r,o,c);if(a==="std"||a==="var")return fr(t,e,n,r,o,c);if(s.dtype==="i64"){let w=i.viewOf({ptr:s.dataPtr,length:o,dtype:"i64"});if(a==="sum"){let k=new Array(r).fill(0n);for(let T=0;T<o;T++)if(c(T)){let L=n[T];k[L]=k[L]+w[T];}return D(i,"i64",k)}if(a==="mean"){let k=new Float64Array(r),T=new Int32Array(r);for(let $=0;$<o;$++){if(!c($))continue;let Pt=n[$];k[Pt]=k[Pt]+Number(w[$]),T[Pt]=T[Pt]+1;}let L=new Array(r);for(let $=0;$<r;$++)L[$]=T[$]>0?k[$]/T[$]:null;return D(i,"f64",L)}let g=new Array(r).fill(null),b=a==="min";for(let k=0;k<o;k++){if(!c(k))continue;let T=n[k],L=w[k];g[T]===null?g[T]=L:g[T]=b?L<g[T]?L:g[T]:L>g[T]?L:g[T];}return D(i,"i64",g)}let m=i.viewOf({ptr:s.dataPtr,length:o,dtype:x[s.dtype].view}),f=new Array(r).fill(null);if(a==="sum"){let w=new Float64Array(r);for(let g=0;g<o;g++)if(c(g)){let b=n[g];w[b]=w[b]+m[g];}for(let g=0;g<r;g++)f[g]=w[g];return D(i,e.resultDtype,f)}if(a==="mean"){let w=new Float64Array(r),g=new Int32Array(r);for(let b=0;b<o;b++){if(!c(b))continue;let k=n[b];w[k]=w[k]+m[b],g[k]=g[k]+1;}for(let b=0;b<r;b++)f[b]=g[b]>0?w[b]/g[b]:null;return D(i,"f64",f)}let p=new Int32Array(r),d=new Int32Array(r),h=new Float64Array(r),y=a==="min";for(let w=0;w<o;w++){if(!c(w))continue;let g=n[w];p[g]=p[g]+1;let b=m[w];Number.isNaN(b)||(d[g]===0?h[g]=b:h[g]=y?Math.min(h[g],b):Math.max(h[g],b),d[g]=d[g]+1);}for(let w=0;w<r;w++)f[w]=p[w]===0?null:d[w]===0?NaN:h[w];return D(i,e.resultDtype,f)}function fr(t,e,n,r,o,i){let{ctx:a}=t,s=e.operand,l=s.dtype==="i64",u=l?a.viewOf({ptr:s.dataPtr,length:o,dtype:"i64"}):a.viewOf({ptr:s.dataPtr,length:o,dtype:x[s.dtype].view}),c=new Float64Array(r),m=new Int32Array(r);for(let y=0;y<o;y++){if(!i(y))continue;let w=n[y];c[w]=c[w]+(l?Number(u[y]):u[y]),m[w]=m[w]+1;}let f=new Float64Array(r);for(let y=0;y<r;y++)f[y]=m[y]>0?c[y]/m[y]:0;let p=new Float64Array(r);for(let y=0;y<o;y++){if(!i(y))continue;let w=n[y],b=(l?Number(u[y]):u[y])-f[w];p[w]=p[w]+b*b;}let d=new Array(r),h=e.op==="std";for(let y=0;y<r;y++)if(m[y]<2)d[y]=null;else {let w=p[y]/(m[y]-1);d[y]=h?Math.sqrt(w):w;}return D(a,"f64",d)}function dr(t,e,n,r,o,i){let{ctx:a}=t,s=e.operand,l=new Array(r),u=s.dtype==="utf8"||s.dtype==="bool"?a.viewOf({ptr:s.dataPtr,length:o,dtype:s.dtype==="utf8"?"i32":"bool"}):s.dtype==="i64"?a.viewOf({ptr:s.dataPtr,length:o,dtype:"i64"}):a.viewOf({ptr:s.dataPtr,length:o,dtype:x[s.dtype].view});for(let m=0;m<o;m++){if(!i(m))continue;let f=n[m];(l[f]??=new Set).add(u[m]);}let c=new Array(r);for(let m=0;m<r;m++)c[m]=l[m]?l[m].size:0;return D(a,"i32",c)}function yr(t,e,n,r,o,i){let{ctx:a}=t,s=e.operand,l=e.op==="first",u=new Int32Array(r).fill(-1);for(let p=0;p<o;p++){if(!i(p))continue;let d=n[p];l?u[d]===-1&&(u[d]=p):u[d]=p;}let c=new Array(r);if(s.dtype==="utf8"){let p=a.viewOf({ptr:s.dataPtr,length:o,dtype:"i32"}),d=s.dict;for(let h=0;h<r;h++)c[h]=u[h]===-1?null:j(a,d,p[u[h]]);return D(a,"utf8",c)}if(s.dtype==="i64"){let p=a.viewOf({ptr:s.dataPtr,length:o,dtype:"i64"});for(let d=0;d<r;d++){let h=u[d];c[d]=h===-1?null:p[h];}return D(a,"i64",c)}let m=s.dtype==="bool",f=a.viewOf({ptr:s.dataPtr,length:o,dtype:x[s.dtype].view});for(let p=0;p<r;p++){let d=u[p];c[p]=d===-1?null:m?f[d]!==0:f[d];}return D(a,s.dtype,c)}function Ke(t,e,n){let r=n.how??"inner",o=Array.isArray(n.on)?n.on:[n.on];if(o.length===0)throw new O("join requires at least one key in `on`.");let i=t.rt,{ctx:a}=i,s=t.length,l=e.length;for(let c of o){let m=t.dtypeOf(c);if(m===void 0)throw J(c,t.columnNames());let f=e.dtypeOf(c);if(f===void 0)throw J(c,e.columnNames());if(m!==f)throw ke("join",m,f,`join key '${c}' must have one dtype.`)}let u=o.map(c=>Ar(i,t.getColumn(c),e.getColumn(c),s,l));try{let c=qe(i,u.map(h=>({ptr:h.leftPtr,tok:h.tok})),s),m=qe(i,u.map(h=>({ptr:h.rightPtr,tok:h.tok})),l),f=ze(i,o.map(h=>t.getColumn(h)),s),p=ze(i,o.map(h=>e.getColumn(h)),l),d;try{let h=i.wasm;d=r==="left"?Ne(h,c,f.ptr,s,m,p.ptr,l):Be(h,c,f.ptr,s,m,p.ptr,l);}finally{a.viewOf.forget({ptr:c,length:s,dtype:"u8"}),a.mod.free(c),a.viewOf.forget({ptr:m,length:l,dtype:"u8"}),a.mod.free(m),mt(i,f.ptr,f.owns),mt(i,p.ptr,p.owns);}return hr(t,e,o,d,r)}finally{for(let c of u)c.free();}}function hr(t,e,n,r,o){let i=t.rt,{ctx:a}=i,s=r.count,l=new Set(n),u=t.columnNames(),c=e.columnNames().filter(y=>!l.has(y)),m=new Set(u),f=Ve(i,r.lIdx,s),p,d=null;o==="left"?(d=wr(r.rIdx,s),p=gr(i,r.rIdx,s)):p=Ve(i,r.rIdx,s);let h=[];try{let y=[];for(let g of u){let b=Le(i,t.getColumn(g),f,s,null);h.push(b),y.push({name:g,col:b});}for(let g of c){let b=Le(i,e.getColumn(g),p,s,d);h.push(b);let k=m.has(g)?`${g}_right`:g;y.push({name:k,col:b});}let w=t.buildResult(y);return h.length=0,w}finally{for(let y of h)Q(a,y);a.mod.free(f),a.mod.free(p);}}function Ve(t,e,n){let r=t.ctx.mod.alloc(Math.max(n*4,1));return n>0&&new Int32Array(t.wasm.memory.buffer,r,n).set(e),r}function gr(t,e,n){let r=t.ctx.mod.alloc(Math.max(n*4,1));if(n>0){let o=new Int32Array(t.wasm.memory.buffer,r,n);for(let i=0;i<n;i++)o[i]=e[i]<0?0:e[i];}return r}function wr(t,e){let n=false;for(let i=0;i<e;i++)if(t[i]<0){n=true;break}if(!n)return null;let r=A(e),o=new Uint8Array(r).fill(255);for(let i=0;i<e;i++)if(t[i]<0){let a=i>>3;o[a]=o[a]&~(1<<(i&7));}return e&7&&(o[r-1]=o[r-1]&(1<<(e&7))-1),o}function Fe(t,e,n,r,o){return {dtype:t,length:e,dataPtr:n,validityPtr:r,validityBitOffset:0,dict:o,owned:true}}function br(t,e){let{count:n,offsetsPtr:r,bytesPtr:o,bytesLen:i}=e,a=(n+1)*4,s=t.ctx.mod.alloc(a),l=t.ctx.mod.alloc(Math.max(i,1)),u=t.wasm.memory.buffer;return new Uint8Array(u,s,a).set(new Uint8Array(u,r,a)),i>0&&new Uint8Array(u,l,i).set(new Uint8Array(u,o,i)),{count:n,offsetsPtr:s,bytesPtr:l,bytesLen:i}}function $e(t,e,n,r,o){let i=e.validityPtr!==0,a=o!==null;if(!i&&!a)return 0;let s=A(r);if(!a){let u=t.ctx.mod.alloc(Math.max(s,1));return C(t.wasm,"gather_validity")(e.validityPtr,n,r,u),u}let l=t.ctx.mod.alloc(Math.max(s,1));if(new Uint8Array(t.wasm.memory.buffer,l,s).set(o),i){let u=t.ctx.mod.alloc(Math.max(s,1));try{C(t.wasm,"gather_validity")(e.validityPtr,n,r,u);let c=new Uint8Array(t.wasm.memory.buffer,l,s),m=new Uint8Array(t.wasm.memory.buffer,u,s);for(let f=0;f<s;f++)c[f]=c[f]&m[f];}finally{t.ctx.mod.free(u);}}return l}var xr={bool:"u8"};function Le(t,e,n,r,o){let{ctx:i}=t;if(e.dtype==="utf8"){let u=i.mod.alloc(Math.max(r*4,1));r>0&&C(t.wasm,"gather_i32")(e.dataPtr,n,r,u);let c=br(t,e.dict);return Fe("utf8",r,u,$e(t,e,n,r,o),c)}let a=x[e.dtype],s=xr[a.wasm]??a.wasm,l=i.mod.alloc(Math.max(r*a.size,1));return r>0&&C(t.wasm,`gather_${s}`)(e.dataPtr,n,r,l),Fe(e.dtype,r,l,$e(t,e,n,r,o),null)}var Ye="i64_direct";function Ar(t,e,n,r,o){if(e.dtype==="utf8"){let i=Ue(t,e,r),a=Ue(t,n,o);return {leftPtr:i,rightPtr:a,tok:Ye,free(){mt(t,i,true),mt(t,a,true);}}}if(e.dtype==="bool"){let i=je(t,e,r),a=je(t,n,o);return {leftPtr:i,rightPtr:a,tok:"i32",free(){mt(t,i,true),mt(t,a,true);}}}return {leftPtr:e.dataPtr,rightPtr:n.dataPtr,tok:x[e.dtype].wasm,free(){}}}function Ue(t,e,n){let{ctx:r,wasm:o}=t,i=e.dict,a=i.count,s=r.mod.alloc(Math.max(a*8,1));try{C(o,"hash_utf8_dict")(i.offsetsPtr,i.bytesPtr,a,s);let l=r.mod.alloc(Math.max(n*8,1));return a===0||n===0||C(o,"gather_i64")(s,e.dataPtr,n,l),l}finally{r.mod.free(s);}}function je(t,e,n){let{ctx:r}=t,o=r.mod.alloc(Math.max(n*4,1)),i=r.viewOf({ptr:e.dataPtr,length:n,dtype:"bool"}),a=r.viewOf({ptr:o,length:n,dtype:"i32"});for(let s=0;s<n;s++)a[s]=i[s];return o}function qe(t,e,n){let{ctx:r,wasm:o}=t,i=r.mod.alloc(Math.max(n*8,1)),a=n*8;for(let s=0;s<e.length;s++){let l=e[s];if(l.tok===Ye)if(s===0){if(a>0){let u=o.memory.buffer;new Uint8Array(u,i,a).set(new Uint8Array(u,l.ptr,a));}}else C(o,"hash_combine")(i,l.ptr,n);else if(s===0)C(o,`hash_${l.tok}`)(l.ptr,0,i,n);else {let u=r.mod.alloc(Math.max(n*8,1));try{C(o,`hash_${l.tok}`)(l.ptr,0,u,n),C(o,"hash_combine")(i,u,n);}finally{r.viewOf.forget({ptr:u,length:n,dtype:"u8"}),r.mod.free(u);}}}return i}function ze(t,e,n){let{ctx:r}=t,o=e.filter(u=>u.validityPtr!==0);if(o.length===0)return {ptr:0,owns:false};let i=A(n),a=r.mod.alloc(Math.max(i,1)),s=r.viewOf({ptr:a,length:Math.max(i,1),dtype:"u8"});s.fill(0);let l=o.map(u=>({view:r.viewOf({ptr:u.validityPtr,length:A(u.validityBitOffset+n),dtype:"u8"}),bitOff:u.validityBitOffset}));for(let u=0;u<n;u++){let c=true;for(let m of l)if(!v(m.view,m.bitOff+u)){c=false;break}c&&S(s,u);}return {ptr:a,owns:true}}function mt(t,e,n){n&&e!==0&&(t.ctx.viewOf.forget({ptr:e,length:1,dtype:"u8"}),t.ctx.mod.free(e));}function We(t,e){if(t===null)return "null";if(typeof t=="boolean")return t?"true":"false";if(typeof t=="string")return t.length>24?`${t.slice(0,23)}\u2026`:t;if(e==="date32"&&typeof t=="number")return new Date(t*864e5).toISOString().slice(0,10);if(typeof t=="bigint")return e==="timestamp"?new Date(Number(t)).toISOString():String(t);if(Number.isNaN(t))return "NaN";if(t===1/0)return "inf";if(t===-1/0)return "-inf";if(Number.isInteger(t))return String(t);if(e==="f32"||e==="f64"){let n=t.toPrecision(6);return n.includes(".")&&!n.includes("e")?n.replace(/0+$/,"").replace(/\.$/,""):n}return String(t)}function He(t){return t==="utf8"||t==="bool"?"left":"right"}function vr(t,e,n){let r=e-t.length;return r<=0?t:n==="right"?" ".repeat(r)+t:t+" ".repeat(r)}function Ge(t,e,n,r,o){let i=t.length,a=new Array(i);for(let c=0;c<i;c++){let m=Math.max(t[c].length,e[c].length);for(let f of n)m=Math.max(m,f[c].length);a[c]=m;}let s=c=>c.map((m,f)=>vr(m,a[f],r[f])).join(" "),l=a.map(c=>"\u2500".repeat(c)).join(" "),u=[];u.push(s(t)),u.push(s(e)),u.push(l);for(let c of n)u.push(s(c));return o&&u.push(o),u.join(`
2
+ `)}var Cr=new Set(["f64","f32","i32","u32","i64"]),Vt=class t{length;_rt;entries;byName;disposed=false;constructor(e,n,r){this._rt=e,this.entries=n,this.length=r,this.byName=new Map(n.map(o=>[o.name,o]));}get ctx(){return this._rt.ctx}get wasm(){return this._rt.wasm}get rt(){return this._rt}getColumn(e){return this.byName.get(e)?.col}dtypeOf(e){return this.byName.get(e)?.col.dtype}columnNames(){return this.entries.map(e=>e.name)}buildResult(e){let n=e.length>0?e[0].col.length:this.length;return t.fromRoots(this._rt,e,n)}static fromColumns(e,n={}){let r=n.runtime??Zt(),o=[],i=-1;for(let[a,s]of Object.entries(e)){let l=n.dtypes?.[a]??Xt(s),u=l==="timestamp"?n.tzs?.[a]:void 0,c=D(r.ctx,l,s,u);if(i===-1)i=c.length;else if(c.length!==i)throw new O(`column '${a}' has length ${c.length}, expected ${i}.`);o.push({name:a,col:c});}return t.fromRoots(r,o,i===-1?0:i)}static fromRecords(e,n={}){n.runtime??Zt();let o=[],i=new Set;for(let s of e)for(let l of Object.keys(s))i.has(l)||(i.add(l),o.push(l));let a={};for(let s of o){let l=new Array(e.length);for(let u=0;u<e.length;u++){let c=e[u][s];l[u]=c===void 0?null:c;}a[s]=l;}return t.fromColumns(a,n)}static fromRoots(e,n,r){let o=n.map(i=>({name:i.name,col:i.col,owner:new Dt(e.ctx,i.col)}));return new t(e,o,r)}static _adoptColumns(e,n,r){return t.fromRoots(e,n,r)}get shape(){return [this.length,this.entries.length]}get columns(){return this.columnNames()}get dtypes(){let e={};for(let n of this.entries)e[n.name]=n.col.dtype;return e}col(e){let n=this.entryOf(e);return new lt(this._rt.ctx,e,n.col)}select(e){return new t(this._rt,this.shareEntries(e),this.length)}drop(e){let n=new Set(e);for(let o of e)if(!this.byName.has(o))throw J(o,this.columnNames());let r=this.entries.filter(o=>!n.has(o.name)).map(o=>o.name);return new t(this._rt,this.shareEntries(r),this.length)}withColumn(e,n,r={}){let o=this.materializeColumn(n,r),i=new Dt(this._rt.ctx,o),a={name:e,col:o,owner:i},s=[],l=false;for(let u of this.entries)u.name===e?(s.push(a),l=true):s.push(this.retainEntry(u));return l||s.push(a),new t(this._rt,s,this.length)}assign(e,n,r){return this.withColumn(e,n,r)}filter(e){let n=Ht(e,this).execute();try{let r=this.entries.map(o=>({name:o.name,col:n.compact(o.col)}));return t.fromRoots(this._rt,r,n.count)}finally{n.free();}}filterFn(e){let n=re(this._rt.ctx,this.entries),r=[];for(let o=0;o<this.length;o++)e(n.at(o))&&r.push(o);return this.gatherRows(r)}mapFn(e){let n=re(this._rt.ctx,this.entries),r=new Array(this.length);for(let o=0;o<this.length;o++)r[o]=e(n.at(o));return r}sortValues(e,n={}){let r=typeof e=="string"?[e]:[...e];if(r.length===0)throw new O("sortValues requires at least one key.");let o=r.map(s=>this.entryOf(s).col),i=Array.isArray(n.descending)?r.map((s,l)=>n.descending[l]===true):r.map(()=>n.descending===true),a=Oe(this._rt,o,i);try{let s=this.entries.map(l=>({name:l.name,col:ee(this._rt,l.col,a.ptr,a.len)}));return t.fromRoots(this._rt,s,a.len)}finally{this._rt.ctx.viewOf.forget({ptr:a.ptr,length:a.len,dtype:"i32"}),this._rt.ctx.mod.free(a.ptr);}}groupby(e){let n=typeof e=="string"?[e]:[...e];if(n.length===0)throw new O("groupby requires at least one key.");return new Nt(this,n)}join(e,n){return Ke(this,e,n)}head(e=5){return this.sliceRange(0,e)}tail(e=5){return this.sliceRange(Math.max(0,this.length-e),this.length)}slice(e,n=this.length){return this.sliceRange(e,n)}sliceRange(e,n){let r=Math.max(0,Math.min(e,this.length)),o=Math.max(r,Math.min(n,this.length)),i=this.entries.map(a=>({name:a.name,col:Ft(a.col,r,o),owner:a.owner.retain()}));return new t(this._rt,i,o-r)}toColumns(){let e={};for(let n of this.entries)e[n.name]=H(this._rt.ctx,n.col);return e}toRecords(){let e=this.toColumns(),n=this.columnNames(),r=new Array(this.length);for(let o=0;o<this.length;o++){let i={};for(let a of n)i[a]=e[a][o];r[o]=i;}return r}describe(){let e=["count","mean","std","min","25%","50%","75%","max"],n=this.entries.filter(i=>Cr.has(i.col.dtype)),r={statistic:e},o={statistic:"utf8"};for(let i of n)r[i.name]=kr(H(this._rt.ctx,i.col)),o[i.name]="f64";return t.fromColumns(r,{runtime:this._rt,dtypes:o})}dispose(){if(!this.disposed){this.disposed=true;for(let e of this.entries)e.owner.release();}}toString(){return this.render()}[Symbol.for("nodejs.util.inspect.custom")](){return this.render()}entryOf(e){let n=this.byName.get(e);if(!n)throw J(e,this.columnNames());return n}retainEntry(e){return e.owner.retain(),{name:e.name,col:e.col,owner:e.owner}}shareEntries(e){return e.map(n=>this.retainEntry(this.entryOf(n)))}materializeColumn(e,n){if(e instanceof M){let o=vt(e,this).execute();if(o.kind==="column"&&o.column)return o.column;let i=o.scalar,a=new Array(this.length).fill(i.value);return D(this._rt.ctx,i.dtype,a)}let r=n.dtype??Xt(e);if(e.length!==this.length)throw new O(`withColumn value has length ${e.length}, expected ${this.length}.`);return D(this._rt.ctx,r,e)}gatherRows(e){let{ctx:n}=this._rt,r=e.length,o=n.mod.alloc(Math.max(r*4,1));try{let i=n.viewOf({ptr:o,length:r,dtype:"i32"});for(let s=0;s<r;s++)i[s]=e[s];let a=this.entries.map(s=>({name:s.name,col:ee(this._rt,s.col,o,r)}));return t.fromRoots(this._rt,a,r)}finally{n.viewOf.forget({ptr:o,length:r,dtype:"i32"}),n.mod.free(o);}}render(){let e=this.columnNames();if(e.length===0)return `Empty DataFrame [${this.length} rows \xD7 0 columns]`;let n=this.entries.map(c=>c.col.dtype),r=n.map(He),o=5,i=5,a=[],s=this.length>o+i,l=(c,m)=>{let f=this.entries.map(p=>H(this._rt.ctx,Ft(p.col,c,m)));for(let p=0;p<m-c;p++)a.push(f.map((d,h)=>We(d[p],n[h])));};s?(l(0,o),a.push(e.map(()=>"\u2026")),l(this.length-i,this.length)):l(0,this.length);let u=`[${this.length} rows \xD7 ${e.length} columns]`;return Ge(e,n,a,r,u)}};function kr(t){let e=[];for(let l of t){if(l===null)continue;let u=typeof l=="bigint"?Number(l):l;Number.isFinite(u)&&e.push(u);}let n=e.length;if(n===0)return [0,null,null,null,null,null,null,null];let r=e.slice().sort((l,u)=>l-u),i=e.reduce((l,u)=>l+u,0)/n,a=n>=2?Math.sqrt(e.reduce((l,u)=>l+(u-i)*(u-i),0)/(n-1)):null,s=l=>{if(r.length===1)return r[0];let u=l*(r.length-1),c=Math.floor(u),m=Math.ceil(u),f=u-c;return r[c]+(r[m]-r[c])*f};return [n,i,a,r[0],s(.25),s(.5),s(.75),r[r.length-1]]}var Or=new Set(["UNCOMPRESSED","SNAPPY",void 0]),Rr={timestampFromMilliseconds:t=>t,timestampFromMicroseconds:t=>t/1000n,timestampFromNanoseconds:t=>t/1000000n,dateFromDays:t=>t,stringFromBytes:t=>t!=null?new TextDecoder().decode(t):null,jsonFromBytes:t=>t!=null?JSON.parse(new TextDecoder().decode(t)):null,bsonFromBytes:t=>t,geometryFromBytes:t=>t,geographyFromBytes:t=>t,uuidFromBytes:t=>t};function Sr(t){let e=t instanceof ArrayBuffer?t:t.buffer.slice(t.byteOffset,t.byteOffset+t.byteLength);return {byteLength:e.byteLength,slice:(n,r)=>Promise.resolve(e.slice(n,r))}}function Mr(t,e){let n=t.name,r=t.type,o=t.logical_type,i=t.converted_type,a=t.num_children,s=t.repetition_type;if(a!=null&&a>0){let l=o?.type;throw l==="LIST"?new Error(`unsupported Parquet feature: LIST column '${n}' \u2014 nested/repeated types are not supported by databonk/parquet (ADR-011)`):l==="MAP"?new Error(`unsupported Parquet feature: MAP column '${n}' \u2014 nested types are not supported (ADR-011)`):new Error(`unsupported Parquet feature: nested/group column '${n}' \u2014 databonk/parquet only supports flat columns (ADR-011)`)}if(s==="REPEATED")throw new Error(`unsupported Parquet feature: REPEATED field '${n}' \u2014 only OPTIONAL/REQUIRED are supported (ADR-011)`);switch(r){case "DOUBLE":return {name:n,dtype:"f64"};case "FLOAT":return {name:n,dtype:"f32"};case "BOOLEAN":return {name:n,dtype:"bool"};case "INT32":{let l=o?.type;if(l==="DATE"||i==="DATE")return {name:n,dtype:"date32"};if(l==="DECIMAL"||i==="DECIMAL")throw new Error(`unsupported Parquet feature: DECIMAL column '${n}' (ADR-011 \u2014 only the databonk numeric dtypes are supported)`);if(l==="INTEGER"){let u=o?.isSigned;return {name:n,dtype:u===false?"u32":"i32"}}return i==="UINT_32"?{name:n,dtype:"u32"}:{name:n,dtype:"i32"}}case "INT64":{let l=o?.type;if(l==="DECIMAL"||i==="DECIMAL")throw new Error(`unsupported Parquet feature: DECIMAL column '${n}' (ADR-011)`);if(l==="TIMESTAMP"||i==="TIMESTAMP_MILLIS"){let u=o?.unit;if(u!=null&&u!=="MILLIS")throw new Error(`unsupported Parquet feature: TIMESTAMP with unit '${u}' in column '${n}' \u2014 only MILLIS is supported (ADR-011)`);if(i==="TIMESTAMP_MICROS")throw new Error(`unsupported Parquet feature: TIMESTAMP_MICROS column '${n}' \u2014 only MILLIS is supported (ADR-011)`);let c=e.get(n);return c!==void 0?{name:n,dtype:"timestamp",tz:c}:{name:n,dtype:"timestamp"}}return {name:n,dtype:"i64"}}case "INT96":throw new Error(`unsupported Parquet feature: INT96 timestamp column '${n}' \u2014 only TIMESTAMP(INT64, MILLIS) is supported (ADR-011)`);case "FIXED_LEN_BYTE_ARRAY":throw new Error(`unsupported Parquet feature: FIXED_LEN_BYTE_ARRAY column '${n}' (ADR-011)`);case "BYTE_ARRAY":{let l=o?.type;if(l==="STRING"||i==="UTF8")return {name:n,dtype:"utf8"};throw l==="DECIMAL"||i==="DECIMAL"?new Error(`unsupported Parquet feature: DECIMAL column '${n}' (ADR-011)`):new Error(`unsupported Parquet feature: BYTE_ARRAY column '${n}' without STRING/UTF8 annotation is not supported (ADR-011)`)}default:throw new Error(`unsupported Parquet feature: unknown physical type '${String(r??"GROUP")}' in column '${n}' (ADR-011)`)}}function _r(t){if(t.length===0)return [];if(t.length===1)return t[0];let e=t.reduce((o,i)=>o+i.length,0),n=new Array(e),r=0;for(let o of t){let i=o.length;for(let a=0;a<i;a++)n[r++]=o[a];}return n}async function _i(t,e){let n=Sr(t),r=await index_js.parquetMetadataAsync(n);for(let p of r.row_groups??[])for(let d of p.columns??[]){let h=d.meta_data?.codec;if(!Or.has(h))throw new Error(`unsupported Parquet compression codec: '${h}'. databonk/parquet supports only SNAPPY and UNCOMPRESSED (ADR-011). For GZIP/ZSTD/BROTLI/LZ4, re-compress the file first.`)}let o=new Map,i=r.key_value_metadata??[],a="databonk:tz:";for(let{key:p,value:d}of i)p.startsWith(a)&&d&&o.set(p.slice(a.length),d);let l=index_js.parquetSchema(r).children.map(p=>Mr(p.element,o)),u=new Map;for(let{name:p}of l)u.set(p,[]);await index_js.parquetRead({file:n,metadata:r,parsers:Rr,onChunk(p){let d=u.get(p.columnName);d&&d.push(p.columnData);}});let c=[];for(let{name:p,dtype:d,tz:h}of l){let y=u.get(p)??[],w=_r(y),g=D(e.ctx,d,w,h);c.push({name:p,col:g});}let m=Vt.fromColumns({},{runtime:e}),f=m.buildResult(c);return m.dispose(),f}function Br(t,e,n,r){let o=H(t,n),i,a;switch(n.dtype){case "f64":i={name:e,type:"DOUBLE",repetition_type:"OPTIONAL"};break;case "f32":i={name:e,type:"FLOAT",repetition_type:"OPTIONAL"};break;case "i32":i={name:e,type:"INT32",repetition_type:"OPTIONAL"};break;case "u32":i={name:e,type:"INT32",repetition_type:"OPTIONAL",converted_type:"UINT_32"};break;case "i64":i={name:e,type:"INT64",repetition_type:"OPTIONAL"};break;case "bool":i={name:e,type:"BOOLEAN",repetition_type:"OPTIONAL"};break;case "utf8":i={name:e,type:"BYTE_ARRAY",repetition_type:"OPTIONAL",logical_type:{type:"STRING"},converted_type:"UTF8"};break;case "date32":i={name:e,type:"INT32",repetition_type:"OPTIONAL",logical_type:{type:"DATE"}};break;case "timestamp":{i={name:e,type:"INT64",repetition_type:"OPTIONAL",logical_type:{type:"TIMESTAMP",isAdjustedToUTC:true,unit:{MILLIS:{}}}},n.tz&&(a={key:`databonk:tz:${e}`,value:n.tz});break}default:{let s=n.dtype;throw new Error(`unsupported databonk dtype for Parquet write: '${String(s)}'`)}}return a!==void 0?{el:i,data:o,tzKey:a}:{el:i,data:o}}function Bi(t,e={}){let{compression:n="uncompressed"}=e;if(n!=="snappy"&&n!=="uncompressed")throw new Error(`unsupported Parquet compression codec: '${n}'. databonk/parquet writeParquet only accepts 'snappy' or 'uncompressed' (ADR-011).`);let r=n==="snappy"?"SNAPPY":"UNCOMPRESSED",o=t.columns;t.length;let a=t.ctx,s=[{name:"root",num_children:o.length}],l=[],u=[];for(let f of o){let p=t.getColumn(f),{el:d,data:h,tzKey:y}=Br(a,f,p);s.push(d),l.push({name:f,data:h}),y&&u.push(y);}let c={schema:s,columnData:l,codec:r};u.length>0&&(c.kvMetadata=u);let m=index_js$1.parquetWriteBuffer(c);return new Uint8Array(m)}exports.readParquet=_i;exports.writeParquet=Bi;//# sourceMappingURL=parquet.cjs.map
3
3
  //# sourceMappingURL=parquet.cjs.map