UNPKG

@uwdata/flechette

Version:

Fast, lightweight access to Apache Arrow data.

3 lines (2 loc) 45.1 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).fl={})}(this,function(t){"use strict";const e=Uint8Array.of(65,82,82,79,87,49),n=Uint8Array.of(255,255,255,255,0,0,0,0),s={V1:0,V2:1,V3:2,V4:3,V5:4},r={Little:0,Big:1},i={NONE:0,Schema:1,DictionaryBatch:2,RecordBatch:3,Tensor:4,SparseTensor:5},a={Dictionary:-1,NONE:0,Null:1,Int:2,Float:3,Binary:4,Utf8:5,Bool:6,Decimal:7,Date:8,Time:9,Timestamp:10,Interval:11,List:12,Struct:13,Union:14,FixedSizeBinary:15,FixedSizeList:16,Map:17,Duration:18,LargeBinary:19,LargeUtf8:20,LargeList:21,RunEndEncoded:22,BinaryView:23,Utf8View:24,ListView:25,LargeListView:26},c={HALF:0,SINGLE:1,DOUBLE:2},u={DAY:0,MILLISECOND:1},o={SECOND:0,MILLISECOND:1,MICROSECOND:2,NANOSECOND:3},d={YEAR_MONTH:0,DAY_TIME:1,MONTH_DAY_NANO:2},l={Sparse:0,Dense:1},f={LZ4_FRAME:0,ZSTD:1},h=0,y=Uint8Array,p=Uint16Array,b=Uint32Array,g=BigUint64Array,I=Int8Array,m=Int16Array,w=Int32Array,v=BigInt64Array,x=Float32Array,L=Float64Array;function O(t,e){const n=Math.log2(t)-3;return(e?[I,m,w,v]:[y,p,b,g])[n]}const E=Object.getPrototypeOf(Int8Array);function A(t){return t instanceof E}function D(t){return Array.isArray(t)||A(t)}function S(t){return t===v||t===g}function N(t,e){let n=0,s=t.length;if(s<=2147483648)do{const r=n+s>>>1;t[r]<=e?n=r+1:s=r}while(n<s);else do{const r=Math.trunc((n+s)/2);t[r]<=e?n=r+1:s=r}while(n<s);return n}function B(t,e,n=0){const s=new t.constructor(e);return s.set(t,n),s}function M(t,e,n){for(;t.length<=e;)t=B(t,t.length<<1,n?t.length:0);return t}function C(t,e,n){if(e(t))return t;throw new Error(n(t))}function U(t,e,n){return e=Array.isArray(e)?e:Object.values(e),C(t,t=>e.includes(t),n??(()=>`${t} must be one of ${e}`))}function T(t,e){for(const[n,s]of Object.entries(t))if(s===e)return n;return"<Unknown>"}const V=t=>`Unsupported data type: "${T(a,t)}" (id ${t})`,F=(t,e,n=!0,s=null)=>({name:t,type:e,nullable:n,metadata:s});function j(t){return Object.hasOwn(t,"name")&&R(t.type)}function R(t){return"number"==typeof t?.typeId}function k(t,e="",n=!0){return j(t)?t:F(e,C(t,R,()=>"Data type expected."),n)}const z=t=>({typeId:t}),_=(t,e,n=!1,s=-1)=>({typeId:a.Dictionary,id:s,dictionary:t,indices:e||W(),ordered:n}),$=()=>z(a.Null),Y=(t=32,e=!0)=>({typeId:a.Int,bitWidth:U(t,[8,16,32,64]),signed:e,values:O(t,e)}),P=()=>Y(8),H=()=>Y(16),W=()=>Y(32),G=()=>Y(64),Z=()=>Y(8,!1),J=()=>Y(16,!1),X=()=>Y(32,!1),q=()=>Y(64,!1),K=(t=2)=>({typeId:a.Float,precision:U(t,c),values:[p,x,L][t]}),Q=()=>K(c.SINGLE),tt=()=>K(c.DOUBLE),et=()=>({typeId:a.Binary,offsets:w}),nt=()=>({typeId:a.Utf8,offsets:w}),st=()=>z(a.Bool),rt=(t,e,n=128)=>({typeId:a.Decimal,precision:t,scale:e,bitWidth:U(n,[32,64,128,256]),values:32===n?w:g}),it=t=>({typeId:a.Date,unit:U(t,u),values:t===u.DAY?w:v}),at=()=>it(u.DAY),ct=(t=o.MILLISECOND)=>{const e=(t=U(t,o))===o.SECOND||t===o.MILLISECOND?32:64;return{typeId:a.Time,unit:t,bitWidth:e,values:32===e?w:v}},ut=(t=o.MILLISECOND,e=null)=>({typeId:a.Timestamp,unit:U(t,o),timezone:e,values:v}),ot=(t=d.MONTH_DAY_NANO)=>({typeId:a.Interval,unit:U(t,d),values:t===d.MONTH_DAY_NANO?void 0:w}),dt=t=>({typeId:a.List,children:[k(t)],offsets:w}),lt=t=>({typeId:a.Struct,children:Array.isArray(t)&&t.length>0&&j(t[0])?t:Object.entries(t).map(([t,e])=>F(t,e))}),ft=(t,e,n,s)=>(n??=e.map((t,e)=>e),{typeId:a.Union,mode:U(t,l),typeIds:n,typeMap:n.reduce((t,e,n)=>(t[e]=n,t),{}),children:e.map((t,e)=>k(t,`_${e}`)),typeIdForValue:s,offsets:w}),ht=t=>({typeId:a.FixedSizeBinary,stride:t}),yt=(t,e)=>({typeId:a.FixedSizeList,stride:e,children:[k(t)]}),pt=(t,e)=>({typeId:a.Map,keysSorted:t,children:[e],offsets:w}),bt=(t=o.MILLISECOND)=>({typeId:a.Duration,unit:U(t,o),values:v}),gt=()=>({typeId:a.LargeBinary,offsets:v}),It=()=>({typeId:a.LargeUtf8,offsets:v}),mt=t=>({typeId:a.LargeList,children:[k(t)],offsets:v}),wt=(t,e)=>({typeId:a.RunEndEncoded,children:[C(k(t,"run_ends"),t=>t.type.typeId===a.Int,()=>"Run-ends must have an integer type."),k(e,"values")]}),vt=t=>({typeId:a.ListView,children:[k(t,"value")],offsets:w}),xt=t=>({typeId:a.LargeListView,children:[k(t,"value")],offsets:v}),Lt=new L(2),Ot=Lt.buffer,Et=new v(Ot),At=new b(Ot),Dt=new w(Ot),St=new y(Ot);function Nt(t){return t}function Bt(t){return BigInt(t)}function Mt(t){return S(t)?Bt:Nt}function Ct(t){return t/864e5|0}function Ut(t){if(t>Number.MAX_SAFE_INTEGER||t<Number.MIN_SAFE_INTEGER)throw Error(`BigInt exceeds integer number representation: ${t}`);return Number(t)}function Tt(t,e){return Number(t/e)+Number(t%e)/Number(e)}const Vt=t=>BigInt.asUintN(64,t);function Ft(t,e){return BigInt.asIntN(64,t[e])}function jt(t,e){const n=e<<1;let s;return BigInt.asIntN(64,t[n+1])<0?(s=Vt(~t[n])|Vt(~t[n+1])<<64n,s=-(s+1n)):s=t[n]|t[n+1]<<64n,s}function Rt(t,e){const n=e<<2;let s;return BigInt.asIntN(64,t[n+3])<0?(s=Vt(~t[n])|Vt(~t[n+1])<<64n|Vt(~t[n+2])<<128n|Vt(~t[n+3])<<192n,s=-(s+1n)):s=t[n]|t[n+1]<<64n|t[n+2]<<128n|t[n+3]<<192n,s}function kt(t){if(t!=t)return 32256;Lt[0]=t;const e=(2147483648&At[1])>>16&65535;let n=2146435072&At[1],s=0;return n>=1089470464?At[0]>0?n=31744:(n=(2080374784&n)>>16,s=(1048575&At[1])>>10):n<=1056964608?(s=1048576+(1048575&At[1]),s=1048576+(s<<(n>>20)-998)>>21,n=0):(n=n-1056964608>>10,s=512+(1048575&At[1])>>10),e|n|65535&s}const zt=new TextDecoder("utf-8"),_t=new TextEncoder;function $t(t){return zt.decode(t)}function Yt(t){return _t.encode(t)}function Pt(t){return`${"object"==typeof t&&t?function(t){return t instanceof Date}(t)?+t:D(t)?`[${t.map(Pt)}]`:function(t){let e="",n=-1;for(const s in t)++n>0&&(e+=","),e+=`"${s}":${Pt(t[s])}`;return`{${e}}`}(t):t??null}`}function Ht(t,e){return!!(t[e>>3]&1<<e%8)}function Wt(t,e){const n=e+Kt(t,e),s=n-Kt(t,n),r=qt(t,s);return(e,i,a=null)=>{if(e<r){const r=qt(t,s+e);if(r)return i(t,n+r)}return a}}function Gt(t,e){return e}function Zt(t,e){return!!Jt(t,e)}function Jt(t,e){return Xt(t,e)<<24>>24}function Xt(t,e){return t[e]}function qt(t,e){return function(t,e){return t[e]|t[e+1]<<8}(t,e)<<16>>16}function Kt(t,e){return t[e]|t[e+1]<<8|t[e+2]<<16|t[e+3]<<24}function Qt(t,e){return Kt(t,e)>>>0}function te(t,e){return Ut(BigInt.asIntN(64,BigInt(Qt(t,e))+(BigInt(Qt(t,e+4))<<32n)))}function ee(t,e){let n=e+Kt(t,e);const s=Kt(t,n);return n+=4,$t(t.subarray(n,n+s))}function ne(t,e,n,s){if(!e)return[];const r=e+Kt(t,e);return Array.from({length:Kt(t,r)},(e,i)=>s(t,r+4+i*n))}const se=Symbol("rowIndex");function re(t,e){class n{constructor(t){this[se]=t}toJSON(){return ae(t,e,this[se])}}const s=n.prototype;for(let n=0;n<t.length;++n){if(Object.hasOwn(s,t[n]))continue;const r=e[n];Object.defineProperty(s,t[n],{get(){return r.at(this[se])},enumerable:!0})}return t=>new n(t)}function ie(t,e){return n=>ae(t,e,n)}function ae(t,e,n){const s={};for(let r=0;r<t.length;++r)s[t[r]]=e[r].at(n);return s}class ce{static ArrayType=null;constructor({length:t,nullCount:e,type:n,validity:s,values:r,offsets:i,sizes:a,children:c}){this.length=t,this.nullCount=e,this.type=n,this.validity=s,this.values=r,this.offsets=i,this.sizes=a,this.children=c,e&&this.validity||(this.at=t=>this.value(t))}get[Symbol.toStringTag](){return"Batch"}at(t){return this.isValid(t)?this.value(t):null}isValid(t){return Ht(this.validity,t)}value(t){return this.values[t]}slice(t,e){const n=e-t,s=Array(n);for(let e=0;e<n;++e)s[e]=this.at(t+e);return s}*[Symbol.iterator](){for(let t=0;t<this.length;++t)yield this.at(t)}}class ue extends ce{constructor(t){super(t);const{length:e,values:n}=this;this.values=n.subarray(0,e)}slice(t,e){return this.nullCount?super.slice(t,e):this.values.subarray(t,e)}[Symbol.iterator](){return this.nullCount?super[Symbol.iterator]():this.values[Symbol.iterator]()}}class oe extends ce{static ArrayType=L}class de extends ce{static ArrayType=Array}class le extends de{value(t){return null}}class fe extends oe{value(t){return Ut(this.values[t])}}class he extends oe{value(t){const e=this.values[t],n=(31744&e)>>10,s=(1023&e)/1024,r=(-1)**((32768&e)>>15);switch(n){case 31:return r*(s?Number.NaN:1/0);case 0:return r*(s?6103515625e-14*s:0)}return r*2**(n-15)*(1+s)}}class ye extends de{value(t){return Ht(this.values,t)}}class pe extends oe{constructor(t){super(t);const{scale:e}=this.type;this.scale=10**e}value(t){return this.values[t]/this.scale}}class be extends ce{constructor(t){super(t);const{bitWidth:e,scale:n}=this.type;this.decimal=64===e?Ft:128===e?jt:Rt,this.scale=10n**BigInt(n)}}class ge extends be{static ArrayType=L;value(t){return Tt(this.decimal(this.values,t),this.scale)}}class Ie extends be{static ArrayType=Array;value(t){return this.decimal(this.values,t)}}class me extends de{constructor(t){super(t),this.source=t}value(t){return new Date(this.source.value(t))}}class we extends oe{value(t){return 864e5*this.values[t]}}const ve=fe;class xe extends fe{value(t){return 1e3*super.value(t)}}const Le=fe;class Oe extends fe{value(t){return Tt(this.values[t],1000n)}}class Ee extends fe{value(t){return Tt(this.values[t],1000000n)}}class Ae extends de{value(t){return this.values.subarray(t<<1,t+1<<1)}}class De extends de{value(t){const e=this.values,n=t<<4;return Float64Array.of(Kt(e,n),Kt(e,n+4),te(e,n+8))}}const Se=({values:t,offsets:e},n)=>t.subarray(e[n],e[n+1]),Ne=({values:t,offsets:e},n)=>t.subarray(Ut(e[n]),Ut(e[n+1]));class Be extends de{value(t){return Se(this,t)}}class Me extends de{value(t){return Ne(this,t)}}class Ce extends de{value(t){return $t(Se(this,t))}}class Ue extends de{value(t){return $t(Ne(this,t))}}class Te extends de{value(t){const e=this.offsets;return this.children[0].slice(e[t],e[t+1])}}class Ve extends de{value(t){const e=this.offsets;return this.children[0].slice(Ut(e[t]),Ut(e[t+1]))}}class Fe extends de{value(t){const e=this.offsets[t],n=e+this.sizes[t];return this.children[0].slice(e,n)}}class je extends de{value(t){const e=this.offsets[t],n=e+this.sizes[t];return this.children[0].slice(Ut(e),Ut(n))}}class Re extends de{constructor(t){super(t),this.stride=this.type.stride}}class ke extends Re{value(t){const{stride:e,values:n}=this;return n.subarray(t*e,(t+1)*e)}}class ze extends Re{value(t){const{children:e,stride:n}=this;return e[0].slice(t*n,(t+1)*n)}}function _e({children:t,offsets:e},n){const[s,r]=t[0].children,i=e[n],a=e[n+1],c=[];for(let t=i;t<a;++t)c.push([s.at(t),r.at(t)]);return c}class $e extends de{value(t){return _e(this,t)}}class Ye extends de{value(t){return new Map(_e(this,t))}}class Pe extends de{constructor({typeIds:t,...e}){super(e),this.typeIds=t,this.typeMap=this.type.typeMap}value(t,e=t){const{typeIds:n,children:s,typeMap:r}=this;return s[r[n[t]]].at(e)}}class He extends Pe{value(t){return super.value(t,this.offsets[t])}}class We extends de{constructor(t,e=ie){super(t),this.names=this.type.children.map(t=>t.name),this.factory=e(this.names,this.children)}value(t){return this.factory(t)}}class Ge extends We{constructor(t){super(t,re)}}class Ze extends de{value(t){const[{values:e},n]=this.children;return n.at(N(e,t))}}class Je extends de{setDictionary(t){return this.dictionary=t,this.cache=t.cache(),this}value(t){return this.cache[this.key(t)]}key(t){return this.values[t]}}class Xe extends de{constructor({data:t,...e}){super(e),this.data=t}view(t){const{values:e,data:n}=this,s=t<<4;let r=s+4,i=e;const a=Kt(i,s);return a>12&&(r=Kt(i,s+12),i=n[Kt(i,s+8)]),i.subarray(r,r+a)}}class qe extends Xe{value(t){return this.view(t)}}class Ke extends Xe{value(t){return $t(this.view(t))}}function Qe(t){let e=[];return{add(t){return e.push(t),this},clear:()=>e=[],done:()=>new tn(e,t)}}class tn{constructor(t,e=t[0]?.type){this.type=e,this.length=t.reduce((t,e)=>t+e.length,0),this.nullCount=t.reduce((t,e)=>t+e.nullCount,0),this.data=t;const n=t.length,s=new Int32Array(n+1);if(1===n){const[e]=t;s[1]=e.length,this.at=t=>e.at(t)}else for(let e=0,r=0;e<n;++e)s[e+1]=r+=t[e].length;this.offsets=s}get[Symbol.toStringTag](){return"Column"}[Symbol.iterator](){const t=this.data;return 1===t.length?t[0][Symbol.iterator]():function*(t){for(let e=0;e<t.length;++e){const n=t[e][Symbol.iterator]();for(let t=n.next();!t.done;t=n.next())yield t.value}}(t)}at(t){const{data:e,offsets:n}=this,s=N(n,t)-1;return e[s]?.at(t-n[s])}get(t){return this.at(t)}toArray(){const{length:t,nullCount:e,data:n}=this,s=!e&&n[0]instanceof ue;const r=n.length;if(s&&1===r)return n[0].values;const i=new(!r||e>0?Array:n[0].constructor.ArrayType??n[0].values.constructor)(t);return s?function(t,e){for(let n=0,s=0;n<e.length;++n){const{values:r}=e[n];t.set(r,s),s+=r.length}return t}(i,n):function(t,e){let n=-1;for(let s=0;s<e.length;++s){const r=e[s];for(let e=0;e<r.length;++e)t[++n]=r.at(e)}return t}(i,n)}cache(){return this._cache??(this._cache=this.toArray())}}class en{constructor(t,e,n=!1){const s=t.fields.map(t=>t.name);this.schema=t,this.names=s,this.children=e,this.factory=n?re:ie;const r=[];this.getFactory=t=>r[t]??(r[t]=this.factory(s,e.map(e=>e.data[t])))}get[Symbol.toStringTag](){return"Table"}get numCols(){return this.names.length}get numRows(){return this.children[0]?.length??0}getChildAt(t){return this.children[t]}getChild(t){const e=this.names.findIndex(e=>e===t);return e>-1?this.children[e]:void 0}selectAt(t,e=[]){const{children:n,factory:s,schema:r}=this,{fields:i}=r;return new en({...r,fields:t.map((t,n)=>function(t,e){return null!=e&&e!==t.name?{...t,name:e}:t}(i[t],e[n]))},t.map(t=>n[t]),s===re)}select(t,e){const n=this.names,s=t.map(t=>n.indexOf(t));return this.selectAt(s,e)}toColumns(){const{children:t,names:e}=this,n={};return e.forEach((e,s)=>n[e]=t[s]?.toArray()??[]),n}toArray(){const{children:t,getFactory:e,numRows:n}=this,s=t[0]?.data??[],r=Array(n);for(let t=0,n=-1;t<s.length;++t){const i=e(t);for(let e=0;e<s[t].length;++e)r[++n]=i(e)}return r}*[Symbol.iterator](){const{children:t,getFactory:e}=this,n=t[0]?.data??[];for(let t=0;t<n.length;++t){const s=e(t);for(let e=0;e<n[t].length;++e)yield s(e)}}at(t){const{children:e,getFactory:n,numRows:s}=this;if(t<0||t>=s)return null;const[{offsets:r}]=e,i=N(r,t)-1;return n(i)(t-r[i])}get(t){return this.at(t)}}function nn(t,e={}){const{typeId:n,bitWidth:s,mode:r,precision:i,unit:c}=t,{useBigInt:l,useDate:f,useDecimalInt:h,useMap:y,useProxy:p}=e;switch(n){case a.Null:return le;case a.Bool:return ye;case a.Int:case a.Time:case a.Duration:return l||s<64?ue:fe;case a.Float:return i?ue:he;case a.Date:return sn(c===u.DAY?we:ve,f&&me);case a.Timestamp:return sn(c===o.SECOND?xe:c===o.MILLISECOND?Le:c===o.MICROSECOND?Oe:Ee,f&&me);case a.Decimal:return 32===s?h?ue:pe:h?Ie:ge;case a.Interval:return c===d.DAY_TIME?Ae:c===d.YEAR_MONTH?ue:De;case a.FixedSizeBinary:return ke;case a.Utf8:return Ce;case a.LargeUtf8:return Ue;case a.Binary:return Be;case a.LargeBinary:return Me;case a.BinaryView:return qe;case a.Utf8View:return Ke;case a.List:return Te;case a.LargeList:return Ve;case a.Map:return y?Ye:$e;case a.ListView:return Fe;case a.LargeListView:return je;case a.FixedSizeList:return ze;case a.Struct:return p?Ge:We;case a.RunEndEncoded:return Ze;case a.Dictionary:return Je;case a.Union:return r?He:Pe}throw new Error(V(n))}function sn(t,e){return e?class extends e{constructor(e){super(new t(e))}}:t}function rn(t,e,n){t[e]=n,t[e+1]=n>>8,t[e+2]=n>>16,t[e+3]=n>>24}function an(t,e,n){const s=BigInt(n);rn(t,e+4,Number(BigInt.asIntN(32,s>>BigInt(32)))),rn(t,e+0,Number(BigInt.asIntN(32,s)))}const cn=1024;class un{constructor(t){this.sink=t,this.minalign=1,this.buf=new Uint8Array(cn),this.space=cn,this.vtables=[],this.outputBytes=0}offset(){return this.buf.length-this.space}writeInt8(t){this.buf[this.space-=1]=t}writeInt16(t){this.buf[this.space-=2]=t,this.buf[this.space+1]=t>>8}writeInt32(t){rn(this.buf,this.space-=4,t)}writeInt64(t){an(this.buf,this.space-=8,t)}addInt8(t){on(this,1,0),this.writeInt8(t)}addInt16(t){on(this,2,0),this.writeInt16(t)}addInt32(t){on(this,4,0),this.writeInt32(t)}addInt64(t){on(this,8,0),this.writeInt64(t)}addOffset(t){on(this,4,0),this.writeInt32(this.offset()-t+4)}addObject(t,e){const n=function(t,e){const n=Array(e).fill(0),s=t.offset();function r(e){n[e]=t.offset()}return{addInt8(e,n,s){n!=s&&(t.addInt8(n),r(e))},addInt16(e,n,s){n!=s&&(t.addInt16(n),r(e))},addInt32(e,n,s){n!=s&&(t.addInt32(n),r(e))},addInt64(e,n,s){n!=s&&(t.addInt64(n),r(e))},addOffset(e,n,s){n!=s&&(t.addOffset(n),r(e))},finish(){t.addInt32(0);const r=t.offset();let i=e;for(;--i>=0&&0===n[i];);const a=i+1;for(;i>=0;--i)t.addInt16(n[i]?r-n[i]:0);const c=2;t.addInt16(r-s);const u=2*(a+c);t.addInt16(u);let o=0;const{buf:d,vtables:l,space:f}=t;t:for(i=0;i<l.length;++i){const t=d.length-l[i];if(u==qt(d,t)){for(let e=2;e<u;e+=2)if(qt(d,f+e)!=qt(d,t+e))continue t;o=l[i];break}}if(o)t.space=d.length-r,rn(d,t.space,o-r);else{const e=t.offset();l.push(e),rn(d,d.length-r,e-r)}return r}}}(this,t);return e?.(n),n.finish()}addVector(t,e,n,s){const r=t?.length;if(!r)return 0;on(this,4,e*r),on(this,n,e*r);for(let e=r;--e>=0;)s(this,t[e]);return this.writeInt32(r),this.offset()}addOffsetVector(t){return this.addVector(t,4,4,(t,e)=>t.addOffset(e))}addString(t){if(null==t)return 0;const e=Yt(t),n=e.length;return this.addInt8(0),on(this,4,n),this.buf.set(e,this.space-=n),this.writeInt32(n),this.offset()}finish(t){on(this,this.minalign,4),this.addOffset(t)}flush(){const{buf:t,sink:e}=this,n=t.subarray(this.space,t.length);e.write(n),this.outputBytes+=n.byteLength,this.minalign=1,this.vtables=[],this.buf=new Uint8Array(cn),this.space=cn}addBuffer(t){const e=t.byteLength;if(!e)return 0;this.sink.write(t),this.outputBytes+=e;const n=(e+7&-8)-e;return this.addPadding(n),e+n}addPadding(t){t>0&&(this.sink.write(new Uint8Array(t)),this.outputBytes+=t)}}function on(t,e,n){let{buf:s,space:r,minalign:i}=t;e>i&&(t.minalign=e);const a=s.length,c=a-r+n,u=1+~c&e-1;s=M(s,c+u+e-1,!0),r+=s.length-a;for(let t=0;t<u;++t)s[--r]=0;t.buf=s,t.space=r}function dn(t){return`Missing compression codec "${T(f,t)}" (id ${t})`}const ln=new Map;function fn(t){return null!=t&&ln.get(t)||null}function hn(t,e){return{offset:te(t,e),metadataLength:Kt(t,e+8),bodyLength:te(t,e+16)}}function yn(t,e){return ne(t,e,24,hn)}function pn(t,e){const n=Wt(t,e);return{codec:n(4,Jt,f.LZ4_FRAME),method:n(6,Jt,h)}}function bn(t,e,n){const r=Wt(t,e),i=n<s.V4?8:0;return{length:r(4,te,0),nodes:ne(t,r(6,Gt),16,(t,e)=>({length:te(t,e),nullCount:te(t,e+8)})),regions:ne(t,r(8,Gt),16+i,(t,e)=>({offset:te(t,e+i),length:te(t,e+i+8)})),compression:r(10,pn),variadic:ne(t,r(12,Gt),8,te)}}function gn(t,e,n){const s=Wt(t,e);return{id:s(4,te,0),data:s(6,(t,e)=>bn(t,e,n)),isDelta:s(8,Zt,!1)}}function In(t,e,n,s){U(n,a,V);const r=Wt(t,e);switch(n){case a.Binary:return et();case a.Utf8:return nt();case a.LargeBinary:return gt();case a.LargeUtf8:return It();case a.List:return dt(s[0]);case a.ListView:return vt(s[0]);case a.LargeList:return mt(s[0]);case a.LargeListView:return xt(s[0]);case a.Struct:return lt(s);case a.RunEndEncoded:return wt(s[0],s[1]);case a.Int:return Y(r(4,Kt,0),r(6,Zt,!1));case a.Float:return K(r(4,qt,c.HALF));case a.Decimal:return rt(r(4,Kt,0),r(6,Kt,0),r(8,Kt,128));case a.Date:return it(r(4,qt,u.MILLISECOND));case a.Time:return ct(r(4,qt,o.MILLISECOND));case a.Timestamp:return ut(r(4,qt,o.SECOND),r(6,ee));case a.Interval:return ot(r(4,qt,d.YEAR_MONTH));case a.Duration:return bt(r(4,qt,o.MILLISECOND));case a.FixedSizeBinary:return ht(r(4,Kt,0));case a.FixedSizeList:return yt(s[0],r(4,Kt,0));case a.Map:return pt(r(4,Zt,!1),s[0]);case a.Union:return ft(r(4,qt,l.Sparse),s,ne(t,r(6,Gt),4,Kt))}return{typeId:n}}function mn(t,e){const n=ne(t,e,4,(t,e)=>{const n=Wt(t,e);return[n(4,ee),n(6,ee)]});return n.length?new Map(n):null}function wn(t,e,n){const s=Wt(t,e);return{version:n,endianness:s(4,qt,0),fields:s(6,vn,[]),metadata:s(8,mn)}}function vn(t,e){return ne(t,e,4,xn)}function xn(t,e){const n=Wt(t,e),s=n(8,Xt,a.NONE),r=n(10,Gt,0),i=n(12,On);let c=In(t,r,s,n(14,Ln,[]));return i&&(i.dictionary=c,c=i),{name:n(4,ee),type:c,nullable:n(6,Zt,!1),metadata:n(16,mn)}}function Ln(t,e){return ne(t,e,4,xn)}function On(t,e){if(!e)return null;const n=Wt(t,e);return _(null,n(6,En,W()),n(8,Zt,!1),n(4,te,0))}function En(t,e){return In(t,e,a.Int)}function An(t,e){let n=Kt(t,e)||0;if(e+=4,-1===n&&(n=Kt(t,e)||0,e+=4),0===n)return null;const r=t.subarray(e,e+=n);if(r.byteLength<n)throw new Error((a=n,c=r.byteLength,`Expected to read ${a} metadata bytes, but only read ${c}.`));var a,c;const u=Wt(r,0),o=u(4,qt,s.V1),d=u(6,Xt,i.NONE),l=u(8,Gt,0),f=u(10,te,0);let h;if(l){const n=d===i.Schema?wn:d===i.DictionaryBatch?gn:d===i.RecordBatch?bn:null;if(!n)throw new Error((t=>`Unsupported message type: ${t} (${T(i,t)})`)(d));if(h=n(r,l,o),f>0){const n=t.subarray(e,e+=f);if(n.byteLength<f)throw new Error(((t,e)=>`Expected to read ${t} bytes for message body, but only read ${e}.`)(f,n.byteLength));h.body=n}else d!==i.Schema&&(h.body=new Uint8Array(0))}return{version:o,type:d,index:e,content:h}}function Dn(t){const n=function(t){return t instanceof ArrayBuffer||"undefined"!=typeof SharedArrayBuffer&&t instanceof SharedArrayBuffer}(t)?new Uint8Array(t):t;return n instanceof Uint8Array&&function(t){if(!t||t.length<4)return!1;for(let n=0;n<6;++n)if(e[n]!==t[n])return!1;return!0}(n)?function(t){const n=t.byteLength-(e.length+4),r=Kt(t,n),i=Wt(t,n-r),a=i(4,qt,s.V1),c=i(8,yn,[]),u=i(10,yn,[]);return{schema:i(6,(t,e)=>wn(t,e,a)),dictionaries:c.map(({offset:e})=>An(t,e).content),records:u.map(({offset:e})=>An(t,e).content),metadata:i(12,mn)}}(n):function(t){const e=[t].flat();let n;const s=[],r=[];for(const t of e){if(!(t instanceof Uint8Array))throw new Error("IPC data batch was not a Uint8Array.");let e=0;for(;;){const a=An(t,e);if(null===a)break;if(e=a.index,a.content)switch(a.type){case i.Schema:n||(n=a.content);break;case i.RecordBatch:s.push(a.content);break;case i.DictionaryBatch:r.push(a.content)}}}return{schema:n,dictionaries:r,records:s,metadata:null}}(n)}function Sn(t,e){const{typeId:n}=t,{options:r,node:i,buffer:c,variadic:u,version:o}=e,d=nn(t,r),f={...i(),type:t};if(n===a.Null)return new d({...f,nullCount:f.length});switch(n){case a.Bool:case a.Int:case a.Time:case a.Duration:case a.Float:case a.Decimal:case a.Date:case a.Timestamp:case a.Interval:case a.FixedSizeBinary:return new d({...f,validity:c(),values:c(t.values)});case a.Utf8:case a.LargeUtf8:case a.Binary:case a.LargeBinary:return new d({...f,validity:c(),offsets:c(t.offsets),values:c()});case a.BinaryView:case a.Utf8View:return new d({...f,validity:c(),values:c(),data:Array.from({length:u()},()=>c())});case a.List:case a.LargeList:case a.Map:return new d({...f,validity:c(),offsets:c(t.offsets),children:e.visit(t.children)});case a.ListView:case a.LargeListView:return new d({...f,validity:c(),offsets:c(t.offsets),sizes:c(t.offsets),children:e.visit(t.children)});case a.FixedSizeList:case a.Struct:return new d({...f,validity:c(),children:e.visit(t.children)});case a.RunEndEncoded:return new d({...f,children:e.visit(t.children)});case a.Dictionary:{const{id:n,indices:s}=t;return new d({...f,validity:c(),values:c(s.values)}).setDictionary(e.dictionary(n))}case a.Union:return o<s.V5&&c(),new d({...f,typeIds:c(I),offsets:t.mode===l.Sparse?null:c(t.offsets),children:e.visit(t.children)});default:throw new Error(V(n))}}function Nn(t,e,n){const{nodes:s,regions:r,variadic:i}=e,a=t.addVector(s,16,8,(t,e)=>(t.writeInt64(e.nullCount),t.writeInt64(e.length),t.offset())),c=t.addVector(r,16,8,(t,e)=>(t.writeInt64(e.length),t.writeInt64(e.offset),t.offset())),u=t.addVector(i,8,8,(t,e)=>t.addInt64(e));return t.addObject(5,e=>{e.addInt64(0,s[0].length,0),e.addOffset(1,a,0),e.addOffset(2,c,0),e.addOffset(3,function(t,e){if(!e)return 0;const{codec:n,method:s}=e;return t.addObject(2,t=>{t.addInt8(0,n,f.LZ4_FRAME),t.addInt8(1,s,h)})}(t,n),0),e.addOffset(4,u,0)})}function Bn(t,e,n){const s=Nn(t,e.data,n);return t.addObject(3,t=>{t.addInt64(0,e.id,0),t.addOffset(1,s,0),t.addInt8(2,+e.isDelta,0)})}function Mn(t,e){return e?.size>0?t.addOffsetVector(Array.from(e,([e,n])=>{const s=t.addString(`${e}`),r=t.addString(`${n}`);return t.addObject(2,t=>{t.addOffset(0,s,0),t.addOffset(1,r,0)})})):0}function Cn(t,e){switch(U(e.typeId,a,V)){case a.Dictionary:return function(t,e){return t.addObject(4,n=>{n.addInt64(0,e.id,0),n.addOffset(1,Cn(t,e.indices),0),n.addInt8(2,+e.ordered,0)})}(t,e);case a.Int:return function(t,e){return t.addObject(2,t=>{t.addInt32(0,e.bitWidth,0),t.addInt8(1,+e.signed,0)})}(t,e);case a.Float:return function(t,e){return t.addObject(1,t=>{t.addInt16(0,e.precision,c.HALF)})}(t,e);case a.Decimal:return function(t,e){return t.addObject(3,t=>{t.addInt32(0,e.precision,0),t.addInt32(1,e.scale,0),t.addInt32(2,e.bitWidth,128)})}(t,e);case a.Date:return function(t,e){return t.addObject(1,t=>{t.addInt16(0,e.unit,u.MILLISECOND)})}(t,e);case a.Time:return function(t,e){return t.addObject(2,t=>{t.addInt16(0,e.unit,o.MILLISECOND),t.addInt32(1,e.bitWidth,32)})}(t,e);case a.Timestamp:return function(t,e){const n=t.addString(e.timezone);return t.addObject(2,t=>{t.addInt16(0,e.unit,o.SECOND),t.addOffset(1,n,0)})}(t,e);case a.Interval:return function(t,e){return t.addObject(1,t=>{t.addInt16(0,e.unit,d.YEAR_MONTH)})}(t,e);case a.Duration:return function(t,e){return t.addObject(1,t=>{t.addInt16(0,e.unit,o.MILLISECOND)})}(t,e);case a.FixedSizeBinary:case a.FixedSizeList:return function(t,e){return t.addObject(1,t=>{t.addInt32(0,e.stride,0)})}(t,e);case a.Map:return function(t,e){return t.addObject(1,t=>{t.addInt8(0,+e.keysSorted,0)})}(t,e);case a.Union:return function(t,e){const n=t.addVector(e.typeIds,4,4,(t,e)=>t.addInt32(e));return t.addObject(2,t=>{t.addInt16(0,e.mode,l.Sparse),t.addOffset(1,n,0)})}(t,e)}return t.addObject(0)}const Un=1===new Uint16Array(new Uint8Array([1,0]).buffer)[0];function Tn(t,e){const{fields:n,metadata:s}=e,r=n.map(e=>Vn(t,e)),i=t.addOffsetVector(r),a=Mn(t,s);return t.addObject(4,t=>{t.addInt16(0,+!Un,0),t.addOffset(1,i,0),t.addOffset(2,a,0)})}function Vn(t,e){const{name:n,nullable:s,type:r,metadata:i}=e;let{typeId:c}=r,u=0,o=0;if(c!==a.Dictionary)u=Cn(t,r);else{const e=r.dictionary;c=e.typeId,o=Cn(t,r),u=Cn(t,e)}const d=(r.children||[]).map(e=>Vn(t,e)),l=t.addOffsetVector(d),f=Mn(t,i),h=t.addString(n);return t.addObject(7,t=>{t.addOffset(0,h,0),t.addInt8(1,+s,0),t.addInt8(2,c,a.NONE),t.addOffset(3,u,0),t.addOffset(4,o,0),t.addOffset(5,l,0),t.addOffset(6,f,0)})}function Fn(t,{offset:e,metadataLength:n,bodyLength:s}){return t.writeInt64(s),t.writeInt32(0),t.writeInt32(n),t.writeInt64(e),t.offset()}function jn(t,e,n,r,a){t.finish(t.addObject(5,t=>{t.addInt16(0,s.V5,s.V1),t.addInt8(1,e,i.NONE),t.addOffset(2,n,0),t.addInt64(3,r,0)}));const c=t.offset(),u=c+8+7&-8;a?.push({offset:t.outputBytes,metadataLength:u,bodyLength:r}),t.addInt32(u-8),t.addInt32(-1),t.flush(),t.addPadding(u-c-8)}class Rn{write(t){}pad(t){this.write(new Uint8Array(t))}finish(){return null}}class kn extends Rn{constructor(){super(),this.buffers=[]}write(t){this.buffers.push(t)}finish(){const t=this.buffers,e=t.reduce((t,e)=>t+e.byteLength,0),n=new Uint8Array(e);for(let e=0,s=0;e<t.length;++e)n.set(t[e],s),s+=t[e].byteLength;return n}}const zn="stream",_n="file";function $n(t,{sink:r,format:a=zn,codec:c}={}){if(a!==zn&&a!==_n)throw new Error(`Unrecognized Arrow IPC format: ${a}`);const{schema:u,dictionaries:o=[],records:d=[],metadata:l}=t,f=new un(r||new kn),y=a===_n,p=[],b=[],g=null!=c?{codec:c,method:h}:null;y&&f.addBuffer(e),u&&jn(f,i.Schema,Tn(f,u),0);for(const t of o){const{data:e}=t;jn(f,i.DictionaryBatch,Bn(f,t,g),e.byteLength,p),Yn(f,e.buffers)}for(const t of d)jn(f,i.RecordBatch,Nn(f,t,g),t.byteLength,b),Yn(f,t.buffers);return f.addBuffer(n),y&&function(t,n,r,i,a){const c=Mn(t,a),u=t.addVector(i,24,8,Fn),o=t.addVector(r,24,8,Fn),d=Tn(t,n);t.finish(t.addObject(5,t=>{t.addInt16(0,s.V5,s.V1),t.addOffset(1,d,0),t.addOffset(2,o,0),t.addOffset(3,u,0),t.addOffset(4,c,0)}));const l=t.offset();t.addInt32(0),t.addInt32(-1),t.flush(),t.sink.write(new Uint8Array(Int32Array.of(l).buffer)),t.sink.write(e)}(f,u,p,b,l),f.sink}function Yn(t,e){for(let n=0;n<e.length;++n)t.addBuffer(e[n])}function Pn(t){let e=0;const n=[],s=[],r=[],i=[];return{node(t,e){n.push({length:t,nullCount:e})},buffer(n){const i=new Uint8Array(n.buffer,n.byteOffset,n.byteLength),a=t?function(t,e){const n=e.encode(t),s=n.length<t.length,r=s?n:t,i=new Uint8Array(8+r.length);return an(i,0,s?t.length:-1),i.set(r,8),i}(i,t):i,c=a.byteLength;s.push({offset:e,length:c}),r.push(a),e+=c+7&-8},variadic(t){i.push(t)},children(t,e){t.children.forEach((t,n)=>{Gn(t.type,e.children[n],this)})},done:()=>({byteLength:e,nodes:n,regions:s,variadic:i,buffers:r})}}function Hn(t,e){if(t?.type.typeId===a.Dictionary){const n=t.dictionary;e(n),Hn(n.data[0],e)}t?.children?.forEach(t=>Hn(t,e))}function Wn(t,e=0,n){const s=Pn(n);return t.forEach(t=>{Gn(t.type,t.data[e],s)}),s.done()}function Gn(t,e,n){const{typeId:s}=t;if(n.node(e.length,e.nullCount),s!==a.Null)switch(s){case a.Bool:case a.Int:case a.Time:case a.Duration:case a.Float:case a.Date:case a.Timestamp:case a.Decimal:case a.Interval:case a.FixedSizeBinary:case a.Dictionary:return n.buffer(e.validity),void n.buffer(e.values);case a.Utf8:case a.LargeUtf8:case a.Binary:case a.LargeBinary:return n.buffer(e.validity),n.buffer(e.offsets),void n.buffer(e.values);case a.BinaryView:case a.Utf8View:return n.buffer(e.validity),n.buffer(e.values),n.variadic(e.data.length),void e.data.forEach(t=>n.buffer(t));case a.List:case a.LargeList:case a.Map:return n.buffer(e.validity),n.buffer(e.offsets),void n.children(t,e);case a.ListView:case a.LargeListView:return n.buffer(e.validity),n.buffer(e.offsets),n.buffer(e.sizes),void n.children(t,e);case a.FixedSizeList:case a.Struct:return n.buffer(e.validity),void n.children(t,e);case a.RunEndEncoded:return void n.children(t,e);case a.Union:return n.buffer(e.typeIds),t.mode===l.Dense&&n.buffer(e.offsets),void n.children(t,e);default:throw new Error(V(s))}}function Zn(t){return new Jn(t)}class Jn{constructor(t=y){this.buf=new t(512)}array(t){return function(t,e=t.length){const n=function(t,e=1){return(t*e+7&-8)/e}(e,t.BYTES_PER_ELEMENT);return t.length>n?t.subarray(0,n):t.length<n?B(t,n):t}(this.buf,t)}prep(t){t>=this.buf.length&&(this.buf=M(this.buf,t))}get(t){return this.buf[t]}set(t,e){this.prep(e),this.buf[e]=t}write(t,e){this.prep(e+t.length),this.buf.set(t,e)}}function Xn(){return new qn}class qn extends Jn{set(t){const e=t>>3;this.prep(e),this.buf[e]|=1<<t%8}}class Kn{constructor(t,e){this.type=t,this.ctx=e,this.batchClass=e.batchType(t)}init(){return this.index=-1,this}set(t,e){return this.index=e,!1}done(){return null}batch(){const t=new this.batchClass(this.done());return this.init(),t}}class Qn extends Kn{constructor(t,e){super(t,e)}init(){return this.nullCount=0,this.validity=Xn(),super.init()}set(t,e){this.index=e;const n=null!=t;return n?this.validity.set(e):this.nullCount++,n}done(){const{index:t,nullCount:e,type:n,validity:s}=this;return{length:t+1,nullCount:e,type:n,validity:e?s.array(1+(t>>3)):new y(0)}}}function ts(){const t=new Map,e=new Set;return{get(n,s){const r=n.id;if(r>=0&&t.has(r))return t.get(r);{const i=function(t,e){const n=Object.create(null),s=e.builder(t.dictionary),r=[];s.init();let i=-1;return{type:t,values:s,add:t=>(r.push(t),t),key(t){const e=Pt(t);let r=n[e];return void 0===r&&(n[e]=r=++i,s.set(t,r)),r},finish(e){const n=new(nn(t.dictionary,e))(s.done()),i=new tn([n]);r.forEach(t=>t.setDictionary(i))}}}(n,s);return r>=0&&t.set(r,i),e.add(i),i}},finish(t){e.forEach(e=>e.finish(t))}}}class es extends Qn{constructor(t,e){super(t,e),this.dict=e.dictionary(t)}init(){return this.values=Zn(this.type.indices.values),super.init()}set(t,e){super.set(t,e)&&this.values.set(this.dict.key(t),e)}done(){return{...super.done(),values:this.values.array(this.index+1)}}batch(){return this.dict.add(super.batch())}}function ns(t){const e=ss();return t(t=>e.add(t)),e.type()}function ss(){let t,e,n,s=0,r=0,i=0,a=0,c=0,u=0,o=0,d=0,l=0,f=0,h=0,y=1/0,p=-1/0,b=1/0,g=-1/0,I={};return{add(m){if(s++,null!=m)switch(typeof m){case"string":l++;break;case"number":a++,m<y&&(y=m),m>p&&(p=m),Number.isInteger(m)&&c++;break;case"bigint":u++,void 0===t?t=e=m:(m<t&&(t=m),m>e&&(e=m));break;case"boolean":i++;break;case"object":if(m instanceof Date)o++,+m%864e5==0&&d++;else if(D(m)){f++;const t=m.length;t<b&&(b=t),t>g&&(g=t),n??=ss(),m.forEach(n.add)}else{h++;for(const t in m){(I[t]??(I[t]=ss())).add(m[t])}}}else r++},type(){const m=s-r;return 0===m?$():c===m?function(t,e){const n=Math.max(Math.abs(t)-1,e);return n<128?P():n<32768?H():n<2**31?W():tt()}(y,p):a===m?tt():u===m?function(t,e){const n=-t>e?-t-1n:e;if(n>=2**63)throw new Error(`BigInt exceeds 64 bits: ${n}`);return G()}(t,e):i===m?st():d===m?at():o===m?ut():l===m?_(nt()):f===m?function(t,e,n){return n===e?yt(t,e):dt(t)}(n.type(),b,g):h===m?lt(Object.entries(I).map(t=>F(t[0],t[1].type()))):function(){throw new Error("Mixed types detected, please define a union type.")}()}}}class rs extends Qn{constructor(t,e){super(t,e),this.toOffset=Mt(t.offsets)}init(){return this.offsets=Zn(this.type.offsets),this.values=Zn(),this.pos=0,super.init()}set(t,e){const{offsets:n,values:s,toOffset:r}=this;super.set(t,e)&&(s.write(t,this.pos),this.pos+=t.length),n.set(r(this.pos),e+1)}done(){return{...super.done(),offsets:this.offsets.array(this.index+2),values:this.values.array(this.pos+1)}}}class is extends Qn{constructor(t,e){super(t,e)}init(){return this.values=Xn(),super.init()}set(t,e){super.set(t,e),t&&this.values.set(e)}done(){return{...super.done(),values:this.values.array(1+(this.index>>3))}}}class as extends Qn{constructor(t,e){super(t,e),this.scale=10**t.scale,this.stride=t.bitWidth>>6}init(){return this.values=Zn(this.type.values),super.init()}set(t,e){const{scale:n,stride:s,values:r}=this;super.set(t,e)&&(r.prep((e+1)*s),function(t,e,n,s,r){const i="bigint"==typeof t?t:Bt(Math.trunc(t*r));e[n]=i,s>1&&(e[n+1]=i>>64n,s>2&&(e[n+2]=i>>128n,e[n+3]=i>>192n))}(t,r.buf,e*s,s,n))}done(){const{index:t,stride:e,values:n}=this;return{...super.done(),values:n.array((t+1)*e)}}}class cs extends Qn{constructor(t,e){super(t,e),this.stride=t.stride}init(){return this.values=Zn(),super.init()}set(t,e){super.set(t,e)&&this.values.write(t,e*this.stride)}done(){const{stride:t,values:e}=this;return{...super.done(),values:e.array(t*(this.index+1))}}}class us extends Qn{constructor(t,e){super(t,e),this.child=e.builder(this.type.children[0].type),this.stride=t.stride}init(){return this.child.init(),super.init()}set(t,e){const{child:n,stride:s}=this,r=e*s;if(super.set(t,e))for(let e=0;e<s;++e)n.set(t[e],r+e);else n.index=r+s}done(){const{child:t}=this;return{...super.done(),children:[t.batch()]}}}class os extends Qn{init(){return this.values=Zn(this.type.values),super.init()}set(t,e){if(super.set(t,e)){const n=e<<1;this.values.set(t[0],n),this.values.set(t[1],n+1)}}done(){return{...super.done(),values:this.values.array(this.index+1<<1)}}}class ds extends Qn{init(){return this.values=Zn(),super.init()}set(t,e){super.set(t,e)&&this.values.write(function([t,e,n]){return Dt[0]=t,Dt[1]=e,Et[1]=Bt(n),St}(t),e<<4)}done(){return{...super.done(),values:this.values.array(this.index+1<<4)}}}class ls extends Qn{constructor(t,e,n){super(t,e),this.child=n}init(){this.child.init();const t=this.type.offsets;return this.offsets=Zn(t),this.toOffset=Mt(t),this.pos=0,super.init()}done(){return{...super.done(),offsets:this.offsets.array(this.index+2),children:[this.child.batch()]}}}class fs extends ls{constructor(t,e){super(t,e,e.builder(t.children[0].type))}set(t,e){const{child:n,offsets:s,toOffset:r}=this;super.set(t,e)&&t.forEach(t=>n.set(t,this.pos++)),s.set(r(this.pos),e+1)}}class hs extends Qn{constructor(t,e){super(t,e),this.children=t.children.map(t=>e.builder(t.type))}init(){return this.children.forEach(t=>t.init()),super.init()}done(){const{children:t}=this;return t.forEach(t=>t.index=this.index),{...super.done(),children:t.map(t=>t.batch())}}}class ys extends hs{constructor(t,e){super(t,e),this.setters=this.children.map((e,n)=>{const s=t.children[n].name;return(t,n)=>e.set(t?.[s],n)})}set(t,e){super.set(t,e);const n=this.setters;for(let s=0;s<n.length;++s)n[s](t,e)}}class ps extends ls{constructor(t,e){super(t,e,new bs(t.children[0].type,e))}set(t,e){const{child:n,offsets:s,toOffset:r}=this;if(super.set(t,e))for(const e of t)n.set(e,this.pos++);s.set(r(this.pos),e+1)}}class bs extends hs{set(t,e){super.set(t,e);const[n,s]=this.children;n.set(t[0],e),s.set(t[1],e)}}const gs={};class Is extends Kn{constructor(t,e){super(t,e),this.children=t.children.map(t=>e.builder(t.type))}init(){return this.pos=0,this.key=null,this.value=gs,this.children.forEach(t=>t.init()),super.init()}next(){const[t,e]=this.children;t.set(this.index+1,this.pos),e.set(this.value,this.pos++)}set(t,e){if(t!==this.value){const e=Pt(t);e!==this.key&&(this.key&&this.next(),this.key=e,this.value=t)}this.index=e}done(){this.next();const{children:t,index:e,type:n}=this;return{length:e+1,nullCount:0,type:n,children:t.map(t=>t.batch())}}}class ms extends Kn{constructor(t,e){super(t,e),this.children=t.children.map(t=>e.builder(t.type)),this.typeMap=t.typeMap,this.lookup=t.typeIdForValue}init(){return this.nullCount=0,this.typeIds=Zn(I),this.children.forEach(t=>t.init()),super.init()}set(t,e){const{children:n,lookup:s,typeMap:r,typeIds:i}=this;this.index=e;const a=s(t,e),c=n[r[a]];i.set(a,e),null==t&&++this.nullCount,this.update(t,e,c)}done(){const{children:t,nullCount:e,type:n,typeIds:s}=this,r=this.index+1;return{length:r,nullCount:e,type:n,typeIds:s.array(r),children:t.map(t=>t.batch())}}}class ws extends ms{update(t,e,n){n.set(t,e),this.children.forEach(t=>{t!==n&&t.set(null,e)})}}class vs extends ms{init(){return this.offsets=Zn(this.type.offsets),super.init()}update(t,e,n){const s=n.index+1;n.set(t,s),this.offsets.set(s,e)}done(){return{...super.done(),offsets:this.offsets.array(this.index+1)}}}class xs extends rs{set(t,e){super.set(t&&Yt(t),e)}}class Ls extends Qn{constructor(t,e){super(t,e),this.values=Zn(t.values)}init(){return this.values=Zn(this.type.values),super.init()}set(t,e){super.set(t,e)&&this.values.set(t,e)}done(){return{...super.done(),values:this.values.array(this.index+1)}}}class Os extends Ls{set(t,e){super.set(null==t?t:Bt(t),e)}}class Es extends Ls{constructor(t,e,n){super(t,e),this.transform=n}set(t,e){super.set(null==t?t:this.transform(t),e)}}function As(t={},e=ts()){return{batchType:e=>nn(e,t),builder(t){return Ds(t,this)},dictionary(t){return e.get(t,this)},finish:()=>e.finish(t)}}function Ds(t,e=As()){const{typeId:n}=t;switch(n){case a.Int:case a.Time:case a.Duration:return S(t.values)?new Os(t,e):new Ls(t,e);case a.Float:return t.precision?new Ls(t,e):new Es(t,e,kt);case a.Binary:case a.LargeBinary:return new rs(t,e);case a.Utf8:case a.LargeUtf8:return new xs(t,e);case a.Bool:return new is(t,e);case a.Decimal:return 32===t.bitWidth?new Es(t,e,(r=t.scale,t=>"bigint"==typeof t?Number(t):Math.trunc(t*r))):new as(t,e);case a.Date:return new Es(t,e,t.unit?Bt:Ct);case a.Timestamp:return new Es(t,e,(s=t.unit)===o.SECOND?t=>Bt(t/1e3):s===o.MILLISECOND?Bt:s===o.MICROSECOND?t=>Bt(1e3*t):t=>Bt(1e6*t));case a.Interval:switch(t.unit){case d.DAY_TIME:return new os(t,e);case d.MONTH_DAY_NANO:return new ds(t,e)}return new Ls(t,e);case a.List:case a.LargeList:return new fs(t,e);case a.Struct:return new ys(t,e);case a.Union:return t.mode?new vs(t,e):new ws(t,e);case a.FixedSizeBinary:return new cs(t,e);case a.FixedSizeList:return new us(t,e);case a.Map:return new ps(t,e);case a.RunEndEncoded:return new Is(t,e);case a.Dictionary:return new es(t,e)}var s,r;throw new Error(V(n))}function Ss(t,e,n={},s){const r="function"==typeof t[Symbol.iterator]?e=>{for(const n of t)e(n)}:t;e??=ns(r);const{maxBatchRows:i=1/0,...c}=n;let u;if(e.typeId===a.Null){let t=0;r(()=>++t),u=function(t,e,n){const s=[],r=e=>new le({length:e,nullCount:e,type:t}),i=Math.floor(e/n);for(let t=0;t<i;++t)s.push(r(n));const a=e%n;a&&s.push(r(a));return s}(e,t,i)}else{const t=As(c,s),n=Ds(e,t).init(),a=t=>u.push(t.batch());u=[];let o=0;r(t=>{n.set(t,o++),o>=i&&(a(n),o=0)}),o&&a(n),t.finish()}return new tn(u,e)}function Ns(t,e,n={},s){return!e&&A(t)?function(t,{maxBatchRows:e,useBigInt:n}){const s=t.constructor,r=function(t){switch(t){case x:return Q();case L:return tt();case I:return P();case m:return H();case w:return W();case v:return G();case y:return Z();case p:return J();case b:return X();case g:return q()}}(s),i=t.length,a=Math.min(e||1/0,i),c=Math.floor(i/a),u=[],o=S(s)&&!n?fe:ue,d=(e,n)=>u.push(new o({length:n-e,nullCount:0,type:r,validity:new y(0),values:t.subarray(e,n)}));let l=0;for(let t=0;t<c;++t)d(l,l+=a);l<i&&d(l,i);return new tn(u)}(t,n):Ss(e=>t.forEach(e),e,n,s)}function Bs(t,e){const n=[],i=Array.isArray(t)?t:Object.entries(t),a=i[0]?.[1].length,c=i.map(([t,e])=>{if(e.length!==a)throw new Error("All columns must have the same length.");return n.push(F(t,e.type)),e});return new en({version:s.V5,endianness:r.Little,fields:n,metadata:null},c,e)}t.Batch=ce,t.Column=tn,t.CompressionType=f,t.DateUnit=u,t.Endianness=r,t.IntervalUnit=d,t.Precision=c,t.RowIndex=se,t.Table=en,t.TimeUnit=o,t.Type=a,t.UnionMode=l,t.Version=s,t.batchType=nn,t.binary=et,t.binaryView=()=>z(a.BinaryView),t.bool=st,t.columnFromArray=Ns,t.columnFromValues=Ss,t.date=it,t.dateDay=at,t.dateMillisecond=()=>it(u.MILLISECOND),t.decimal=rt,t.decimal128=(t,e)=>rt(t,e,128),t.decimal256=(t,e)=>rt(t,e,256),t.decimal32=(t,e)=>rt(t,e,32),t.decimal64=(t,e)=>rt(t,e,64),t.dictionary=_,t.dictionaryContext=ts,t.duration=bt,t.field=F,t.fixedSizeBinary=ht,t.fixedSizeList=yt,t.float=K,t.float16=()=>K(c.HALF),t.float32=Q,t.float64=tt,t.getCompressionCodec=fn,t.int=Y,t.int16=H,t.int32=W,t.int64=G,t.int8=P,t.interval=ot,t.largeBinary=gt,t.largeList=mt,t.largeListView=xt,t.largeUtf8=It,t.list=dt,t.listView=vt,t.map=(t,e,n=!1)=>pt(n,F("entries",lt([k(t,"key",!1),k(e,"value")]),!1)),t.nullType=$,t.runEndEncoded=wt,t.setCompressionCodec=function(t,e){ln.set(t,e)},t.struct=lt,t.tableFromArrays=function(t,e={}){const{types:n={},...s}=e,r=ts();return Bs((Array.isArray(t)?t:Object.entries(t)).map(([t,e])=>[t,Ns(e,n[t],s,r)]),e.useProxy)},t.tableFromColumns=Bs,t.tableFromIPC=function(t,e){return function(t,e={}){const{schema:n={fields:[]},dictionaries:s,records:r}=t,{version:i,fields:c}=n,u=new Map,o=function(t,e,n){const s={version:e,options:t,dictionary:t=>n.get(t)};return t=>{const{length:e,nodes:n,regions:r,compression:i,variadic:a,body:c}=t;let u=-1,o=-1,d=-1;return{...s,length:e,node:()=>n[++u],buffer:t=>{const{bytes:e,length:n,offset:s}=function(t,e,n){if(n){if(n.method!==h)throw new Error(`Unknown compression method (${n.method})`);{const s=n.codec,r=fn(s);if(!r)throw new Error(dn(s));return function(t,{offset:e,length:n},s){if(0===n)return{bytes:new Uint8Array(0),offset:0,length:0};const r=te(t,e),i=t.subarray(e+8,e+n),a=-1===r?i:s.decode(i);return{bytes:a,offset:0,length:a.length}}(t,e,r)}}return{bytes:t,...e}}(c,r[++o],i);return t?new t(e.buffer,e.byteOffset+s,n/t.BYTES_PER_ELEMENT):e.subarray(s,s+n)},variadic:()=>a[++d],visit(t){return t.map(t=>Sn(t.type,this))}}}}(e,i,u),d=new Map;!function(t,e){t.fields.forEach(function t(n){e(n),n.type.dictionary?.children?.forEach(t),n.type.children?.forEach(t)})}(n,t=>{const e=t.type;e.typeId===a.Dictionary&&d.set(e.id,e.dictionary)});const l=new Map;for(const t of s){const{id:e,data:n,isDelta:s,body:r}=t,i=d.get(e),a=Sn(i,o({...n,body:r}));if(l.has(e)){const t=l.get(e);s||t.clear(),t.add(a)}else{if(s)throw new Error("Delta update can not be first dictionary batch.");l.set(e,Qe(i).add(a))}}l.forEach((t,e)=>u.set(e,t.done()));const f=c.map(t=>Qe(t.type));for(const t of r){const e=o(t);c.forEach((t,n)=>f[n].add(Sn(t.type,e)))}return new en(n,f.map(t=>t.done()),e.useProxy)}(Dn(t),e)},t.tableToIPC=function(t,e){"string"==typeof e&&(e={format:e});const n=e?.codec,s=fn(n);if(null!=n&&!s)throw new Error(dn(n));const r=t.children;!function(t){const e=t[0]?.data.map(t=>t.length);t.forEach(({data:t})=>{if(t.length!==e.length||t.some((t,n)=>t.length!==e[n]))throw new Error("Columns have inconsistent batch sizes.")})}(r);const{dictionaries:i,idMap:c}=function(t,e){const n=[],s=new Map,r=new Map;let i=-1;const a=t=>{if(s.has(t))r.set(t.type,s.get(t));else{s.set(t,++i);for(let s=0;s<t.data.length;++s)n.push({id:i,isDelta:s>0,data:Wn([t],s,e)});r.set(t.type,i)}};return t.forEach(t=>Hn(t.data[0],a)),{dictionaries:n,idMap:r}}(r,s),u=function(t,e){return(t[0]?.data||[]).map((n,s)=>Wn(t,s,e))}(r,s),o=function(t,e){if(!e.size)return t;const n=t=>{t.typeId===a.Dictionary&&(t.id=e.get(t.dictionary),r(t)),t.children&&(t.children=t.children.slice()).forEach(s)},s=(t,e,s)=>{const r={...t.type};s[e]={...t,type:r},n(r)},r=t=>{const e={...t.dictionary};t.dictionary=e,n(e)};return t={...t,fields:t.fields.slice()},t.fields.forEach(s),t}(t.schema,c);return $n({schema:o,dictionaries:i,records:u},{...e,codec:n}).finish()},t.time=ct,t.timeMicrosecond=()=>ct(o.MICROSECOND),t.timeMillisecond=()=>ct(o.MILLISECOND),t.timeNanosecond=()=>ct(o.NANOSECOND),t.timeSecond=()=>ct(o.SECOND),t.timestamp=ut,t.uint16=J,t.uint32=X,t.uint64=q,t.uint8=Z,t.union=ft,t.utf8=nt,t.utf8View=()=>z(a.Utf8View)}); //# sourceMappingURL=flechette.min.js.map