@kanunilabs/pivotgrid-core 1.11.9 → 1.11.10

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.
@@ -1 +1 @@
1
- 'use strict';var pt="-";function ut(B,t,e){return t?!B.has(pt+e):B.has(e)}var nt=class{size=1;nodeToParent;nodeToVal;nodeToDepth;nodeChildCount;childrenMap;constructor(t){this.nodeToParent=new Int32Array(t).fill(-1),this.nodeToVal=new Int32Array(t).fill(-1),this.nodeToDepth=new Int32Array(t).fill(0),this.nodeChildCount=new Int32Array(t).fill(0),this.childrenMap=new Map;}expandCapacity(){let t=this.nodeToParent.length*2,e=new Int32Array(t).fill(-1);e.set(this.nodeToParent),this.nodeToParent=e;let n=new Int32Array(t).fill(-1);n.set(this.nodeToVal),this.nodeToVal=n;let r=new Int32Array(t).fill(0);r.set(this.nodeToDepth),this.nodeToDepth=r;let i=new Int32Array(t).fill(0);i.set(this.nodeChildCount),this.nodeChildCount=i;}insert(t,e){let n=0;for(let r=0;r<e;r++){let i=t[r],s=this.childrenMap.get(n);s||(s=new Map,this.childrenMap.set(n,s));let c=s.get(i);c===void 0&&(this.size>=this.nodeToParent.length&&this.expandCapacity(),c=this.size++,this.nodeToParent[c]=n,this.nodeToVal[c]=i,this.nodeToDepth[c]=r+1,this.nodeChildCount[n]++,s.set(i,c)),n=c;}return n}};function _(B,t){if(B.length>=t)return B;let e=new B.constructor(Math.max(t,Math.ceil(B.length*1.5)));return e.set(B),e}function dt(B,t,e){let n=t[0];for(let r=0;r<t.length;r++){let i=t[r]+1,s=r+1<t.length?t[r+1]:e;B.copyWithin(n,i,s),n+=s-i;}}var lt=class{dictionary=[];rowFields=[];colFields=[];dataFields=[];storedColumns={};storedRowCount=0;lastMask=null;isBuilt=false;buildState=null;activeFilters=null;activePrefilter=null;tracking=false;groupRows=new Int32Array(0);groupMap=new Map;rowLive=null;colLive=null;rowTrie;colTrie;cardinalityMap={};rowPathStrs=[];colPathStrs=[];rowIndexMapCache={};columnIndexMapCache={};buildVersion=0;groupCount=0;leafGroupRowIds;leafGroupColIds;leafGroupMeasures;leafGroupCounts;leafGroupMins;leafGroupMaxes;leafGroupDistinctSets=new Map;rowGroupOf=null;init(t){this.dictionary=t.dictionary,this.rowFields=t.rowFields,this.colFields=t.colFields,this.dataFields=t.dataFields,t.workerBootstrap&&t.workerBootstrap.length>0&&console.warn("[PivotGrid Worker] workerBootstrap is deprecated for security reasons. All calculations are executed on the main thread."),this.storedRowCount=t.rowCount,this.storedColumns={};for(let[e,n]of Object.entries(t.columnarData))n.type==="int32"&&(this.storedColumns[e]={type:"int32",array:new Int32Array(n.buffer)}),n.type==="float64"&&(this.storedColumns[e]={type:"float64",array:new Float64Array(n.buffer)});this.lastMask=null,this.activeFilters=this.activePrefilter=null,t.deferBuild?this.isBuilt=false:this.buildFromMask(null);}reconfigure(t){if(this.rowFields=t.rowFields,this.colFields=t.colFields,this.dataFields=t.dataFields,this.appendDictionary(t.dictionaryAppend),t.dropColumns)for(let e of t.dropColumns)delete this.storedColumns[e];if(t.newColumns)for(let[e,n]of Object.entries(t.newColumns))n.type==="int32"&&(this.storedColumns[e]={type:"int32",array:new Int32Array(n.buffer)}),n.type==="float64"&&(this.storedColumns[e]={type:"float64",array:new Float64Array(n.buffer)});this.buildFromMask(this.lastMask);}initStart(t){this.dictionary=t.dictionary,this.rowFields=t.rowFields,this.colFields=t.colFields,this.dataFields=t.dataFields,t.workerBootstrap&&t.workerBootstrap.length>0&&console.warn("[PivotGrid Worker] workerBootstrap is deprecated for security reasons. All calculations are executed on the main thread."),this.storedRowCount=t.rowCountTotal,this.storedColumns={};for(let[e,n]of Object.entries(t.columnTypes))n==="int32"?this.storedColumns[e]={type:"int32",array:new Int32Array(t.rowCountTotal)}:this.storedColumns[e]={type:"float64",array:new Float64Array(t.rowCountTotal)};this.setFilters((t.filters||[]).map(e=>({fieldId:e.fieldId,filterType:e.filterType,ids:new Set,pending:new Set(e.valueStrings)})),t.prefilter),this.resolveIncrementalFilterStrings(0),this.isBuilt=false,this.beginBuild();}initChunk(t){this.appendDictionary(t.dictionaryAppend)&&this.ensureCardCapacity();for(let[e,n]of Object.entries(t.columns)){let r=this.storedColumns[e];r&&(r.type==="int32"?r.array.set(new Int32Array(n),t.start):r.array.set(new Float64Array(n),t.start));}this.lastMask&&this.computeMaskRange(t.start,t.end),this.processRange(t.start,t.end,this.lastMask);}initEnd(){this.finalizeBuild();}appendDictionary(t){if(!t||t.length===0)return false;let e=this.dictionary.length;for(let n of t)this.dictionary.push(n);return this.resolveIncrementalFilterStrings(e),true}setFilters(t,e){this.activeFilters=t.length>0?t:null,this.activePrefilter=e&&e.conditions.length>0?e:null,this.lastMask=this.activeFilters||this.activePrefilter?new Uint8Array(this.storedRowCount):null;}resolveIncrementalFilterStrings(t){let e=this.activeFilters;if(!e)return;let n=0;for(let r of e)n+=r.pending.size;if(n!==0)for(let r=t;r<this.dictionary.length;r++){let i=this.dictionary[r];for(let s of e)s.pending.delete(i)&&s.ids.add(r);}}computeMaskRange(t,e){let n=this.lastMask;if(n.fill(1,t,e),this.activeFilters)for(let r of this.activeFilters){let i=this.storedColumns[r.fieldId];if(!i||i.type!=="int32")continue;let s=i.array,c=r.ids;if(r.filterType==="exclude")for(let a=t;a<e;a++)n[a]&&c.has(s[a])&&(n[a]=0);else for(let a=t;a<e;a++)n[a]&&!c.has(s[a])&&(n[a]=0);}if(this.activePrefilter)for(let r=t;r<e;r++)n[r]&&!this.evaluatePrefilterGroup(this.activePrefilter,r)&&(n[r]=0);}applyFilter(t){this.setFilters(t.filters.map(e=>({fieldId:e.fieldId,filterType:e.filterType,ids:new Set(e.dictIds),pending:new Set(e.valueStrings)})),t.prefilter),this.lastMask&&this.computeMaskRange(0,this.storedRowCount),this.buildFromMask(this.lastMask);}evaluatePrefilterGroup(t,e){if(!t.conditions||t.conditions.length===0)return true;if(t.logicalOperator==="and"){for(let n of t.conditions)if("logicalOperator"in n){if(!this.evaluatePrefilterGroup(n,e))return false}else if(!this.evaluatePrefilterCondition(n,e))return false;return true}else {for(let n of t.conditions)if("logicalOperator"in n){if(this.evaluatePrefilterGroup(n,e))return true}else if(this.evaluatePrefilterCondition(n,e))return true;return false}}evaluatePrefilterCondition(t,e){let n=this.storedColumns[t.fieldId];if(!n)return true;if(n.type==="float64"){let f=n.array[e],y=Number(t.value);switch(t.operator){case "=":return f===y;case "<>":return f!==y;case ">":return f>y;case ">=":return f>=y;case "<":return f<y;case "<=":return f<=y;case "isnull":return isNaN(f)||f===0;case "isnotnull":return !isNaN(f)&&f!==0;default:return true}}let r=n.array[e],i=this.dictionary[r]||"",s=String(t.value??""),c=Number(i),a=Number(s),d=i!==""&&s!==""&&!isNaN(c)&&!isNaN(a);switch(t.operator){case "=":return i===s;case "<>":return i!==s;case ">":return d?c>a:i>s;case ">=":return d?c>=a:i>=s;case "<":return d?c<a:i<s;case "<=":return d?c<=a:i<=s;case "contains":return i.toLowerCase().includes(s.toLowerCase());case "notcontains":return !i.toLowerCase().includes(s.toLowerCase());case "startswith":return i.toLowerCase().startsWith(s.toLowerCase());case "endswith":return i.toLowerCase().endsWith(s.toLowerCase());case "isnull":return i===""||i==="(Blank)";case "isnotnull":return i!==""&&i!=="(Blank)";case "in":return Array.isArray(t.value)?t.value.map(String).includes(i):true;case "notin":return Array.isArray(t.value)?!t.value.map(String).includes(i):true;default:return true}}buildFromMask(t){this.beginBuild(),this.processRange(0,this.storedRowCount,t),this.finalizeBuild();}beginBuild(){let t=this.storedRowCount,e=this.storedColumns,n=this.rowFields.length,r=this.colFields.length,i=new Array(n);for(let u=0;u<n;u++)i[u]=e[this.rowFields[u].id].array;let s=new Array(r);for(let u=0;u<r;u++)s[u]=e[this.colFields[u].id].array;let c=this.dictionary.length,a=this.rowFields.map(u=>{let v=e[u.id];return v&&v.type==="int32"?new Uint8Array(c):null}),d=this.colFields.map(u=>{let v=e[u.id];return v&&v.type==="int32"?new Uint8Array(c):null});this.rowTrie=new nt(2e5),this.colTrie=new nt(2e5);let f=this.dataFields.length,y=this.dataFields.map(u=>u.summaryType||"sum"),p=y.some(u=>u==="count"||u==="avg"),G=y.some(u=>u==="min"),g=y.some(u=>u==="max"),F=y.some(u=>u==="distinct"),R=new Array(f);for(let u=0;u<f;u++)R[u]=e[this.dataFields[u].id].array;let b=Math.min(t,2e5),l=G?new Float64Array(b*f):new Float64Array(0),w=g?new Float64Array(b*f):new Float64Array(0);this.leafGroupDistinctSets=F?new Map:new Map,G&&l.fill(1/0),g&&w.fill(-1/0),this.buildState={rPath:new Int32Array(n),cPath:new Int32Array(r),rowColArrays:i,colColArrays:s,dataColArrays:R,rowCardSeen:a,colCardSeen:d,rowCardCounts:new Int32Array(n),colCardCounts:new Int32Array(r),summaryTypes:y,allSums:y.every(u=>u==="sum"),needsCounts:p,needsMins:G,needsMaxes:g,groupMap:new Map,leafRowIds:new Int32Array(b),leafColIds:new Int32Array(b),leafMeasures:new Float64Array(b*f),leafCounts:p?new Float64Array(b*f):new Float64Array(0),leafMins:l,leafMaxes:w,rowGroupOf:this.tracking||y.includes("custom")?new Int32Array(t).fill(-1):null,gCount:0};}ensureCardCapacity(){let t=this.buildState;if(!t)return;let e=this.dictionary.length,n=r=>{if(r===null||r.length>=e)return r;let i=new Uint8Array(Math.max(e,r.length*2));return i.set(r),i};for(let r=0;r<t.rowCardSeen.length;r++)t.rowCardSeen[r]=n(t.rowCardSeen[r]);for(let r=0;r<t.colCardSeen.length;r++)t.colCardSeen[r]=n(t.colCardSeen[r]);}processRange(t,e,n){let r=this.buildState,{rPath:i,cPath:s,rowColArrays:c,colColArrays:a,dataColArrays:d,rowCardSeen:f,colCardSeen:y,rowCardCounts:p,colCardCounts:G,summaryTypes:g,allSums:F,needsCounts:R,needsMins:b,needsMaxes:l,groupMap:w,rowGroupOf:u}=r,v=c.length,m=a.length,M=d.length,E=this.rowTrie,L=this.colTrie,W=r.leafRowIds,$=r.leafColIds,K=r.leafMeasures,J=r.leafCounts,Q=r.leafMins,Y=r.leafMaxes,X=r.gCount;for(let T=t;T<e;T++){if(n&&!n[T])continue;for(let h=0;h<v;h++){let A=c[h][T];i[h]=A;let C=f[h];C!==null&&C[A]===0&&(C[A]=1,p[h]++);}let Z=E.insert(i,v);for(let h=0;h<m;h++){let A=a[h][T];s[h]=A;let C=y[h];C!==null&&C[A]===0&&(C[A]=1,G[h]++);}let rt=L.insert(s,m),it=Z*67108864+rt,z=w.get(it);if(z===void 0){if(z=X++,w.set(it,z),z>=W.length){let h=z*2,A=new Int32Array(h);A.set(W),W=A;let C=new Int32Array(h);C.set($),$=C;let H=new Float64Array(h*M);if(H.set(K),K=H,R){let o=new Float64Array(h*M);o.set(J),J=o;}if(b){let o=new Float64Array(h*M);o.fill(1/0),o.set(Q),Q=o;}if(l){let o=new Float64Array(h*M);o.fill(-1/0),o.set(Y),Y=o;}}W[z]=Z,$[z]=rt;}u!==null&&(u[T]=z);let st=z*M;if(F)for(let h=0;h<M;h++)K[st+h]+=d[h][T];else for(let h=0;h<M;h++){let A=d[h][T],C=st+h;switch(g[h]){case "sum":K[C]+=A;break;case "count":J[C]+=1;break;case "avg":K[C]+=A,J[C]+=1;break;case "min":A<Q[C]&&(Q[C]=A);break;case "max":A>Y[C]&&(Y[C]=A);break;case "distinct":{if(A===0&&d[h]instanceof Int32Array)break;let H=this.leafGroupDistinctSets.get(C);H||(H=new Set,this.leafGroupDistinctSets.set(C,H)),H.add(A);break}default:K[C]+=A;break}}}r.leafRowIds=W,r.leafColIds=$,r.leafMeasures=K,r.leafCounts=J,r.leafMins=Q,r.leafMaxes=Y,r.gCount=X;}finalizeBuild(){let t=this.buildState,{needsCounts:e,needsMins:n,needsMaxes:r,gCount:i}=t,s=t.dataColArrays.length,c=t.rowColArrays.length,a=t.colColArrays.length;this.groupCount=i,this.leafGroupRowIds=t.leafRowIds.slice(0,i),this.leafGroupColIds=t.leafColIds.slice(0,i),this.leafGroupMeasures=t.leafMeasures.slice(0,i*s),this.leafGroupCounts=e?t.leafCounts.slice(0,i*s):t.leafCounts,this.leafGroupMins=n?t.leafMins.slice(0,i*s):t.leafMins,this.leafGroupMaxes=r?t.leafMaxes.slice(0,i*s):t.leafMaxes,this.rowGroupOf=t.rowGroupOf,this.cardinalityMap={};for(let d=0;d<c;d++)t.rowCardSeen[d]!==null&&(this.cardinalityMap[this.rowFields[d].id]=t.rowCardCounts[d]);for(let d=0;d<a;d++)t.colCardSeen[d]!==null&&(this.cardinalityMap[this.colFields[d].id]=t.colCardCounts[d]);if(this.buildState=null,this.rowLive=this.colLive=null,this.tracking){this.groupMap=t.groupMap;let d=this.groupRows=new Int32Array(i),f=t.rowGroupOf;for(let y=0;y<this.storedRowCount;y++)f[y]>=0&&d[f[y]]++;}this.buildVersion++,this.isBuilt=true,this.rowPathStrs=this.buildPathStrs(this.rowTrie),this.colPathStrs=this.buildPathStrs(this.colTrie),this.rowIndexMapCache=this.buildIndexMap(this.rowPathStrs,null),this.columnIndexMapCache=this.buildIndexMap(this.colPathStrs,null);}buildPathStrs(t,e=[""]){let n=e;for(let r=e.length;r<t.size;r++){let i=t.nodeToParent[r],s=this.dictionary[t.nodeToVal[r]],c=n[i];n[r]=i===0?s===""?"|":s:c+"|"+s;}return n}buildIndexMap(t,e){let n={};for(let r=0;r<t.length;r++)(!e||e[r])&&(n[t[r]]=r);return n}applyTransaction(t){this.tracking||(this.tracking=true,this.buildFromMask(this.lastMask)),this.appendDictionary(t.dictionaryAppend);let{update:e,remove:n}=t,r=e.length,i=this.storedRowCount,s=i+t.addCount,c=this.storedColumns,a=new Set,d=false,f=this.rowGroupOf,y=l=>{let w=f[l];w<0||(f[l]=-1,--this.groupRows[w]===0&&(d=true),a.add(w));};e.forEach(y),n.forEach(y);for(let l in c){let w=c[l],u=t.columns[l];if(!u)throw new Error("transaction: no values for column "+l);let v=w.type==="int32"?new Int32Array(u):new Float64Array(u),m=w.array=_(w.array,s);for(let M=0;M<r;M++)m[e[M]]=v[M];m.set(v.subarray(r),i);}f=this.rowGroupOf=_(f,s),f.fill(-1,i,s);let p=this.lastMask&&(this.lastMask=_(this.lastMask,s));p&&(e.forEach(l=>this.computeMaskRange(l,l+1)),this.computeMaskRange(i,s));let G=this.rowFields.map(l=>c[l.id].array),g=this.colFields.map(l=>c[l.id].array),F=new Int32Array(G.length),R=new Int32Array(g.length),b=l=>{if(p&&!p[l])return;G.forEach((M,E)=>{F[E]=M[l];}),g.forEach((M,E)=>{R[E]=M[l];});let w=this.rowTrie.insert(F,F.length),u=this.colTrie.insert(R,R.length),v=w*67108864+u,m=this.groupMap.get(v);m===void 0&&(this.groupMap.set(v,m=this.groupCount++),(this.leafGroupRowIds=_(this.leafGroupRowIds,m+1))[m]=w,(this.leafGroupColIds=_(this.leafGroupColIds,m+1))[m]=u,this.groupRows=_(this.groupRows,m+1)),this.groupRows[m]++===0&&(d=true),f[l]=m,a.add(m);};e.forEach(b);for(let l=i;l<s;l++)b(l);if(this.storedRowCount=s-n.length,n.length>0){for(let l in c)dt(c[l].array,n,s);dt(f,n,s),p&&dt(p,n,s);}if(this.resumGroups(a),d){let l=0;for(let w=0;w<this.groupCount;w++)this.groupRows[w]===0&&l++;l>4096&&l>this.groupCount-l?this.buildFromMask(this.lastMask):(this.cardinalityMap={},[this.rowLive,this.rowPathStrs,this.rowIndexMapCache]=this.relive(this.rowTrie,this.leafGroupRowIds,this.rowFields,this.rowPathStrs),[this.colLive,this.colPathStrs,this.columnIndexMapCache]=this.relive(this.colTrie,this.leafGroupColIds,this.colFields,this.colPathStrs),this.buildVersion++);}return this.storedRowCount}resumGroups(t){if(t.size===0)return;let e=this.dataFields.length,n=this.dataFields.map(g=>g.summaryType||"sum"),r=this.dataFields.map(g=>this.storedColumns[g.id].array),i=this.groupCount*e,s=this.leafGroupMeasures=_(this.leafGroupMeasures,i),c=n.some(g=>g==="count"||g==="avg")?this.leafGroupCounts=_(this.leafGroupCounts,i):null,a=n.includes("min")?this.leafGroupMins=_(this.leafGroupMins,i):null,d=n.includes("max")?this.leafGroupMaxes=_(this.leafGroupMaxes,i):null,f=this.leafGroupDistinctSets,y=new Uint8Array(this.groupCount),p=0;for(let g of t){y[g]=1,p+=this.groupRows[g];for(let F=g*e;F<(g+1)*e;F++)s[F]=0,c&&(c[F]=0),a&&(a[F]=1/0),d&&(d[F]=-1/0),f.delete(F);}let G=this.rowGroupOf;for(let g=0;p>0&&g<this.storedRowCount;g++){let F=G[g];if(!(F<0||!y[F])){p--;for(let R=0;R<e;R++){let b=r[R][g],l=F*e+R;switch(n[R]){case "count":c[l]++;break;case "avg":s[l]+=b,c[l]++;break;case "min":b<a[l]&&(a[l]=b);break;case "max":b>d[l]&&(d[l]=b);break;case "distinct":{if(b===0&&r[R]instanceof Int32Array)break;let w=f.get(l);w||f.set(l,w=new Set),w.add(b);break}default:s[l]+=b;}}}}}relive(t,e,n,r){let i=new Uint8Array(t.size);i[0]=1;for(let a=0;a<this.groupCount;a++)if(this.groupRows[a]!==0)for(let d=e[a];!i[d];d=t.nodeToParent[d])i[d]=1;let s=n.map(()=>new Set);t.nodeChildCount.fill(0,0,t.size);for(let a=1;a<t.size;a++)i[a]&&(t.nodeChildCount[t.nodeToParent[a]]++,s[t.nodeToDepth[a]-1]?.add(t.nodeToVal[a]));n.forEach((a,d)=>{this.cardinalityMap[a.id]=s[d].size;});let c=this.buildPathStrs(t,r);return [i,c,this.buildIndexMap(c,i)]}compute(t){this.isBuilt||this.buildFromMask(this.lastMask);let e=this.rowPathStrs,n=this.colPathStrs,r=new Uint8Array(this.rowTrie.size),i=new Set(t.expandedPaths),s=i.has(".*")||i.has("*");for(let o=0;o<this.rowTrie.size;o++)r[o]=ut(i,s,e[o])?1:0;let c=new Uint8Array(this.colTrie.size),a=new Set(t.expandedColPaths),d=a.has(".*")||a.has("*");for(let o=0;o<this.colTrie.size;o++)c[o]=ut(a,d,n[o])?1:0;let f=new Uint8Array(this.rowTrie.size);f[0]=1;for(let o=1;o<this.rowTrie.size;o++){let S=this.rowTrie.nodeToParent[o];(S===0||r[S])&&(!this.rowLive||this.rowLive[o])&&(f[o]=1);}let y=new Uint8Array(this.colTrie.size);y[0]=1;for(let o=1;o<this.colTrie.size;o++){let S=this.colTrie.nodeToParent[o];(S===0||c[S])&&(!this.colLive||this.colLive[o])&&(y[o]=1);}let p=this.dataFields.length,G=this.dataFields.map(o=>o.summaryType||"sum"),g=this.leafGroupMins.length>0,F=this.leafGroupMaxes.length>0,R=this.leafGroupCounts.length>0,b=this.leafGroupDistinctSets.size>0,l=new Map,w=0,u=1e4,v=new Float64Array(u),m=new Float64Array(u*p),M=new Int32Array(u*p),E=R?new Float64Array(u*p):null,L=g?new Float64Array(u*p):null,W=F?new Float64Array(u*p):null,$=b?new Map:new Map;L&&L.fill(1/0),W&&W.fill(-1/0);let K=new Int32Array(this.rowFields.length+1),J=new Int32Array(this.colFields.length+1),Q=(o,S,I,O)=>{let tt=0,U=o;for(;U!==-1;)I[U]&&(O[tt++]=U),U=S.nodeToParent[U];return tt},Y=this.rowGroupOf!==null&&G.includes("custom"),X=Y?new Int32Array(this.groupCount+1):null,T=Y?new Int32Array(Math.max(16,this.groupCount*4)):null,Z=0,rt=this.tracking?this.groupRows:null;for(let o=0;o<this.groupCount;o++){if(rt!==null&&rt[o]===0){X!==null&&(X[o+1]=Z);continue}let S=this.leafGroupRowIds[o],I=this.leafGroupColIds[o],O=Q(S,this.rowTrie,f,K),tt=Q(I,this.colTrie,y,J);for(let U=0;U<O;U++){let ot=K[U];for(let j=0;j<tt;j++){let at=J[j],et=ot*67108864+at,P=l.get(et);if(P===void 0){if(P=w++,l.set(et,P),P>=v.length){let x=P*2,k=new Float64Array(x);k.set(v),v=k;let V=new Float64Array(x*p);V.set(m),m=V;let N=new Int32Array(x*p);if(N.set(M),M=N,E){let D=new Float64Array(x*p);D.set(E),E=D;}if(L){let D=new Float64Array(x*p);D.fill(1/0),D.set(L),L=D;}if(W){let D=new Float64Array(x*p);D.fill(-1/0),D.set(W),W=D;}}v[P]=et;}if(T!==null){if(Z>=T.length){let x=new Int32Array(T.length*2);x.set(T),T=x;}T[Z++]=P;}for(let x=0;x<p;x++){let k=P*p+x,V=o*p+x;switch(G[x]){case "sum":m[k]+=this.leafGroupMeasures[V];break;case "count":m[k]+=this.leafGroupCounts[V];break;case "avg":m[k]+=this.leafGroupMeasures[V],E[k]+=this.leafGroupCounts[V];break;case "min":{let N=this.leafGroupMins[V];N<L[k]&&(L[k]=N);break}case "max":{let N=this.leafGroupMaxes[V];N>W[k]&&(W[k]=N);break}case "distinct":{let N=this.leafGroupDistinctSets.get(V);if(N){let D=$.get(k);D||(D=new Set,$.set(k,D));for(let ht of N)D.add(ht);}break}default:m[k]+=this.leafGroupMeasures[V];break}M[k]+=1;}}}X!==null&&(X[o+1]=Z);}for(let o=0;o<w;o++)for(let S=0;S<p;S++){let I=o*p+S;switch(G[S]){case "avg":m[I]=E[I]>0?m[I]/E[I]:0;break;case "min":m[I]=L[I]!==1/0?L[I]:0;break;case "max":m[I]=W[I]!==-1/0?W[I]:0;break;case "distinct":{let O=$.get(I);m[I]=O?O.size:0;break}}}let it=v.slice(0,w),z=m.slice(0,w*p),st=M.slice(0,w*p),h=(o,S,I,O,tt)=>{let U=[];for(let j=0;j<O.length;j++)U[j]=O[j]?.id||"";let ot=j=>{let at=o.childrenMap.get(j);if(!at)return [];let et=[];for(let P of at.values())if(S[P]){let x=o.nodeToDepth[P]-1,k=I[P]===1;et.push({key:tt[P],value:this.dictionary[o.nodeToVal[P]],fieldId:U[x]||"",children:k?ot(P):[],isExpanded:k,isTotal:false,depth:x,hasChildren:o.nodeChildCount[P]>0});}return et};return ot(0)},A;if(this.rowGroupOf!==null&&X!==null&&T!==null){let o=this.storedRowCount,S={};for(let I=0;I<p;I++){if(G[I]!=="custom")continue;let O=this.dataFields[I].id;S[O]=this.storedColumns[O].array.slice(0,o);}A={rowGroupOf:this.rowGroupOf.slice(0,o),groupCellOffsets:X,groupCells:T.slice(0,Z),values:S};}let C=h(this.rowTrie,f,r,this.rowFields,e),H=h(this.colTrie,y,c,this.colFields,n);return {id:t.id,rowTree:C,colTree:H,cellMatrix:{keys:it,sums:z,counts:st},customSummary:A,rowIndexMap:this.rowIndexMapCache,columnIndexMap:this.columnIndexMapCache,cardinalityMap:this.cardinalityMap,indexMapsVersion:this.buildVersion}}};var q=new lt,ft=-1;self.onmessage=B=>{let t=B.data;try{if(t.action==="init")q.init(t),self.postMessage({id:t.id,isInitComplete:!0});else if(t.action==="initStart")q.initStart(t);else if(t.action==="initChunk")q.initChunk(t);else if(t.action==="initEnd")q.initEnd(),self.postMessage({id:t.id,isInitComplete:!0});else if(t.action==="filter")q.applyFilter(t),self.postMessage({id:t.id,isFilterComplete:!0});else if(t.action==="reconfigure")q.reconfigure(t),self.postMessage({id:t.id,isReconfigureComplete:!0});else if(t.action==="transaction")self.postMessage({id:t.id,isTransactionComplete:!0,rowCount:q.applyTransaction(t)});else if(t.action==="compute"){let e=q.compute(t),n=[e.cellMatrix.keys.buffer,e.cellMatrix.sums.buffer,e.cellMatrix.counts.buffer];if(e.indexMapsVersion!==void 0&&e.indexMapsVersion===ft){let r={...e,rowIndexMap:void 0,columnIndexMap:void 0,cardinalityMap:void 0,indexMapsOmitted:!0};postMessage(r,n);}else e.indexMapsVersion!==void 0&&(ft=e.indexMapsVersion),postMessage(e,n);}}catch(e){self.postMessage({id:t.id,error:e instanceof Error?e.message:String(e)});}};
1
+ 'use strict';var wt="-";function ct(v,t,e){return t?!v.has(wt+e):v.has(e)}var nt=class{size=1;nodeToParent;nodeToVal;nodeToDepth;nodeChildCount;childrenMap;constructor(t){this.nodeToParent=new Int32Array(t).fill(-1),this.nodeToVal=new Int32Array(t).fill(-1),this.nodeToDepth=new Int32Array(t).fill(0),this.nodeChildCount=new Int32Array(t).fill(0),this.childrenMap=new Map;}expandCapacity(){let t=this.nodeToParent.length*2,e=new Int32Array(t).fill(-1);e.set(this.nodeToParent),this.nodeToParent=e;let n=new Int32Array(t).fill(-1);n.set(this.nodeToVal),this.nodeToVal=n;let r=new Int32Array(t).fill(0);r.set(this.nodeToDepth),this.nodeToDepth=r;let i=new Int32Array(t).fill(0);i.set(this.nodeChildCount),this.nodeChildCount=i;}insert(t,e){let n=0;for(let r=0;r<e;r++){let i=t[r],s=this.childrenMap.get(n);s||(s=new Map,this.childrenMap.set(n,s));let c=s.get(i);c===void 0&&(this.size>=this.nodeToParent.length&&this.expandCapacity(),c=this.size++,this.nodeToParent[c]=n,this.nodeToVal[c]=i,this.nodeToDepth[c]=r+1,this.nodeChildCount[n]++,s.set(i,c)),n=c;}return n}};var gt=new Set(["sum","avg","min","max"]);function ht(v){let t=v.precision;return typeof t!="number"||!Number.isInteger(t)||t<0||t>10?0:gt.has(v.summaryType||"sum")?10**t:0}function dt(v,t){let e=v*t;return e<0?-Math.round(-e):Math.round(e)}function H(v,t){if(v.length>=t)return v;let e=new v.constructor(Math.max(t,Math.ceil(v.length*1.5)));return e.set(v),e}function pt(v,t,e){let n=t[0];for(let r=0;r<t.length;r++){let i=t[r]+1,s=r+1<t.length?t[r+1]:e;v.copyWithin(n,i,s),n+=s-i;}}var ut=class{dictionary=[];rowFields=[];colFields=[];dataFields=[];storedColumns={};storedRowCount=0;lastMask=null;isBuilt=false;buildState=null;activeFilters=null;activePrefilter=null;tracking=false;groupRows=new Int32Array(0);groupMap=new Map;rowLive=null;colLive=null;rowTrie;colTrie;cardinalityMap={};rowPathStrs=[];colPathStrs=[];rowIndexMapCache={};columnIndexMapCache={};buildVersion=0;groupCount=0;leafGroupRowIds;leafGroupColIds;leafGroupMeasures;leafGroupCounts;leafGroupMins;leafGroupMaxes;leafGroupDistinctSets=new Map;rowGroupOf=null;init(t){this.dictionary=t.dictionary,this.rowFields=t.rowFields,this.colFields=t.colFields,this.dataFields=t.dataFields,t.workerBootstrap&&t.workerBootstrap.length>0&&console.warn("[PivotGrid Worker] workerBootstrap is deprecated for security reasons. All calculations are executed on the main thread."),this.storedRowCount=t.rowCount,this.storedColumns={};for(let[e,n]of Object.entries(t.columnarData))n.type==="int32"&&(this.storedColumns[e]={type:"int32",array:new Int32Array(n.buffer)}),n.type==="float64"&&(this.storedColumns[e]={type:"float64",array:new Float64Array(n.buffer)});this.lastMask=null,this.activeFilters=this.activePrefilter=null,t.deferBuild?this.isBuilt=false:this.buildFromMask(null);}reconfigure(t){if(this.rowFields=t.rowFields,this.colFields=t.colFields,this.dataFields=t.dataFields,this.appendDictionary(t.dictionaryAppend),t.dropColumns)for(let e of t.dropColumns)delete this.storedColumns[e];if(t.newColumns)for(let[e,n]of Object.entries(t.newColumns))n.type==="int32"&&(this.storedColumns[e]={type:"int32",array:new Int32Array(n.buffer)}),n.type==="float64"&&(this.storedColumns[e]={type:"float64",array:new Float64Array(n.buffer)});this.buildFromMask(this.lastMask);}initStart(t){this.dictionary=t.dictionary,this.rowFields=t.rowFields,this.colFields=t.colFields,this.dataFields=t.dataFields,t.workerBootstrap&&t.workerBootstrap.length>0&&console.warn("[PivotGrid Worker] workerBootstrap is deprecated for security reasons. All calculations are executed on the main thread."),this.storedRowCount=t.rowCountTotal,this.storedColumns={};for(let[e,n]of Object.entries(t.columnTypes))n==="int32"?this.storedColumns[e]={type:"int32",array:new Int32Array(t.rowCountTotal)}:this.storedColumns[e]={type:"float64",array:new Float64Array(t.rowCountTotal)};this.setFilters((t.filters||[]).map(e=>({fieldId:e.fieldId,filterType:e.filterType,ids:new Set,pending:new Set(e.valueStrings)})),t.prefilter),this.resolveIncrementalFilterStrings(0),this.isBuilt=false,this.beginBuild();}initChunk(t){this.appendDictionary(t.dictionaryAppend)&&this.ensureCardCapacity();for(let[e,n]of Object.entries(t.columns)){let r=this.storedColumns[e];r&&(r.type==="int32"?r.array.set(new Int32Array(n),t.start):r.array.set(new Float64Array(n),t.start));}this.lastMask&&this.computeMaskRange(t.start,t.end),this.processRange(t.start,t.end,this.lastMask);}initEnd(){this.finalizeBuild();}appendDictionary(t){if(!t||t.length===0)return false;let e=this.dictionary.length;for(let n of t)this.dictionary.push(n);return this.resolveIncrementalFilterStrings(e),true}setFilters(t,e){this.activeFilters=t.length>0?t:null,this.activePrefilter=e&&e.conditions.length>0?e:null,this.lastMask=this.activeFilters||this.activePrefilter?new Uint8Array(this.storedRowCount):null;}resolveIncrementalFilterStrings(t){let e=this.activeFilters;if(!e)return;let n=0;for(let r of e)n+=r.pending.size;if(n!==0)for(let r=t;r<this.dictionary.length;r++){let i=this.dictionary[r];for(let s of e)s.pending.delete(i)&&s.ids.add(r);}}computeMaskRange(t,e){let n=this.lastMask;if(n.fill(1,t,e),this.activeFilters)for(let r of this.activeFilters){let i=this.storedColumns[r.fieldId];if(!i||i.type!=="int32")continue;let s=i.array,c=r.ids;if(r.filterType==="exclude")for(let a=t;a<e;a++)n[a]&&c.has(s[a])&&(n[a]=0);else for(let a=t;a<e;a++)n[a]&&!c.has(s[a])&&(n[a]=0);}if(this.activePrefilter)for(let r=t;r<e;r++)n[r]&&!this.evaluatePrefilterGroup(this.activePrefilter,r)&&(n[r]=0);}applyFilter(t){this.setFilters(t.filters.map(e=>({fieldId:e.fieldId,filterType:e.filterType,ids:new Set(e.dictIds),pending:new Set(e.valueStrings)})),t.prefilter),this.lastMask&&this.computeMaskRange(0,this.storedRowCount),this.buildFromMask(this.lastMask);}evaluatePrefilterGroup(t,e){if(!t.conditions||t.conditions.length===0)return true;if(t.logicalOperator==="and"){for(let n of t.conditions)if("logicalOperator"in n){if(!this.evaluatePrefilterGroup(n,e))return false}else if(!this.evaluatePrefilterCondition(n,e))return false;return true}else {for(let n of t.conditions)if("logicalOperator"in n){if(this.evaluatePrefilterGroup(n,e))return true}else if(this.evaluatePrefilterCondition(n,e))return true;return false}}evaluatePrefilterCondition(t,e){let n=this.storedColumns[t.fieldId];if(!n)return true;if(n.type==="float64"){let f=n.array[e],w=Number(t.value);switch(t.operator){case "=":return f===w;case "<>":return f!==w;case ">":return f>w;case ">=":return f>=w;case "<":return f<w;case "<=":return f<=w;case "isnull":return isNaN(f)||f===0;case "isnotnull":return !isNaN(f)&&f!==0;default:return true}}let r=n.array[e],i=this.dictionary[r]||"",s=String(t.value??""),c=Number(i),a=Number(s),d=i!==""&&s!==""&&!isNaN(c)&&!isNaN(a);switch(t.operator){case "=":return i===s;case "<>":return i!==s;case ">":return d?c>a:i>s;case ">=":return d?c>=a:i>=s;case "<":return d?c<a:i<s;case "<=":return d?c<=a:i<=s;case "contains":return i.toLowerCase().includes(s.toLowerCase());case "notcontains":return !i.toLowerCase().includes(s.toLowerCase());case "startswith":return i.toLowerCase().startsWith(s.toLowerCase());case "endswith":return i.toLowerCase().endsWith(s.toLowerCase());case "isnull":return i===""||i==="(Blank)";case "isnotnull":return i!==""&&i!=="(Blank)";case "in":return Array.isArray(t.value)?t.value.map(String).includes(i):true;case "notin":return Array.isArray(t.value)?!t.value.map(String).includes(i):true;default:return true}}buildFromMask(t){this.beginBuild(),this.processRange(0,this.storedRowCount,t),this.finalizeBuild();}beginBuild(){let t=this.storedRowCount,e=this.storedColumns,n=this.rowFields.length,r=this.colFields.length,i=new Array(n);for(let u=0;u<n;u++)i[u]=e[this.rowFields[u].id].array;let s=new Array(r);for(let u=0;u<r;u++)s[u]=e[this.colFields[u].id].array;let c=this.dictionary.length,a=this.rowFields.map(u=>{let y=e[u.id];return y&&y.type==="int32"?new Uint8Array(c):null}),d=this.colFields.map(u=>{let y=e[u.id];return y&&y.type==="int32"?new Uint8Array(c):null});this.rowTrie=new nt(2e5),this.colTrie=new nt(2e5);let f=this.dataFields.length,w=this.dataFields.map(u=>u.summaryType||"sum"),g=w.some(u=>u==="count"||u==="avg"),R=w.some(u=>u==="min"),B=w.some(u=>u==="max"),C=w.some(u=>u==="distinct"),b=this.precisionScales(),M=new Array(f);for(let u=0;u<f;u++)M[u]=e[this.dataFields[u].id].array;let l=Math.min(t,2e5),h=R?new Float64Array(l*f):new Float64Array(0),I=B?new Float64Array(l*f):new Float64Array(0);this.leafGroupDistinctSets=C?new Map:new Map,R&&h.fill(1/0),B&&I.fill(-1/0),this.buildState={rPath:new Int32Array(n),cPath:new Int32Array(r),rowColArrays:i,colColArrays:s,dataColArrays:M,rowCardSeen:a,colCardSeen:d,rowCardCounts:new Int32Array(n),colCardCounts:new Int32Array(r),summaryTypes:w,allSums:w.every(u=>u==="sum"),scales:b,anyScaled:b.some(u=>u!==0),needsCounts:g,needsMins:R,needsMaxes:B,groupMap:new Map,leafRowIds:new Int32Array(l),leafColIds:new Int32Array(l),leafMeasures:new Float64Array(l*f),leafCounts:g?new Float64Array(l*f):new Float64Array(0),leafMins:h,leafMaxes:I,rowGroupOf:this.tracking||w.includes("custom")?new Int32Array(t).fill(-1):null,gCount:0};}ensureCardCapacity(){let t=this.buildState;if(!t)return;let e=this.dictionary.length,n=r=>{if(r===null||r.length>=e)return r;let i=new Uint8Array(Math.max(e,r.length*2));return i.set(r),i};for(let r=0;r<t.rowCardSeen.length;r++)t.rowCardSeen[r]=n(t.rowCardSeen[r]);for(let r=0;r<t.colCardSeen.length;r++)t.colCardSeen[r]=n(t.colCardSeen[r]);}processRange(t,e,n){let r=this.buildState,{rPath:i,cPath:s,rowColArrays:c,colColArrays:a,dataColArrays:d,rowCardSeen:f,colCardSeen:w,rowCardCounts:g,colCardCounts:R,summaryTypes:B,allSums:C,scales:b,anyScaled:M,needsCounts:l,needsMins:h,needsMaxes:I,groupMap:u,rowGroupOf:y}=r,P=c.length,D=a.length,S=d.length,U=this.rowTrie,et=this.colTrie,$=r.leafRowIds,q=r.leafColIds,X=r.leafMeasures,J=r.leafCounts,L=r.leafMins,O=r.leafMaxes,Q=r.gCount;for(let V=t;V<e;V++){if(n&&!n[V])continue;for(let p=0;p<P;p++){let E=c[p][V];i[p]=E;let F=f[p];F!==null&&F[E]===0&&(F[E]=1,g[p]++);}let it=U.insert(i,P);for(let p=0;p<D;p++){let E=a[p][V];s[p]=E;let F=w[p];F!==null&&F[E]===0&&(F[E]=1,R[p]++);}let st=et.insert(s,D),ot=it*67108864+st,N=u.get(ot);if(N===void 0){if(N=Q++,u.set(ot,N),N>=$.length){let p=N*2,E=new Int32Array(p);E.set($),$=E;let F=new Int32Array(p);F.set(q),q=F;let o=new Float64Array(p*S);if(o.set(X),X=o,l){let m=new Float64Array(p*S);m.set(J),J=m;}if(h){let m=new Float64Array(p*S);m.fill(1/0),m.set(L),L=m;}if(I){let m=new Float64Array(p*S);m.fill(-1/0),m.set(O),O=m;}}$[N]=it,q[N]=st;}y!==null&&(y[V]=N);let rt=N*S;if(C&&!M)for(let p=0;p<S;p++)X[rt+p]+=d[p][V];else for(let p=0;p<S;p++){let E=d[p][V],F=M&&b[p]!==0?dt(E,b[p]):E,o=rt+p;switch(B[p]){case "sum":X[o]+=F;break;case "count":J[o]+=1;break;case "avg":X[o]+=F,J[o]+=1;break;case "min":F<L[o]&&(L[o]=F);break;case "max":F>O[o]&&(O[o]=F);break;case "distinct":{if(F===0&&d[p]instanceof Int32Array)break;let m=this.leafGroupDistinctSets.get(o);m||(m=new Set,this.leafGroupDistinctSets.set(o,m)),m.add(F);break}default:X[o]+=F;break}}}r.leafRowIds=$,r.leafColIds=q,r.leafMeasures=X,r.leafCounts=J,r.leafMins=L,r.leafMaxes=O,r.gCount=Q;}finalizeBuild(){let t=this.buildState,{needsCounts:e,needsMins:n,needsMaxes:r,gCount:i}=t,s=t.dataColArrays.length,c=t.rowColArrays.length,a=t.colColArrays.length;this.groupCount=i,this.leafGroupRowIds=t.leafRowIds.slice(0,i),this.leafGroupColIds=t.leafColIds.slice(0,i),this.leafGroupMeasures=t.leafMeasures.slice(0,i*s),this.leafGroupCounts=e?t.leafCounts.slice(0,i*s):t.leafCounts,this.leafGroupMins=n?t.leafMins.slice(0,i*s):t.leafMins,this.leafGroupMaxes=r?t.leafMaxes.slice(0,i*s):t.leafMaxes,this.rowGroupOf=t.rowGroupOf,this.cardinalityMap={};for(let d=0;d<c;d++)t.rowCardSeen[d]!==null&&(this.cardinalityMap[this.rowFields[d].id]=t.rowCardCounts[d]);for(let d=0;d<a;d++)t.colCardSeen[d]!==null&&(this.cardinalityMap[this.colFields[d].id]=t.colCardCounts[d]);if(this.buildState=null,this.rowLive=this.colLive=null,this.tracking){this.groupMap=t.groupMap;let d=this.groupRows=new Int32Array(i),f=t.rowGroupOf;for(let w=0;w<this.storedRowCount;w++)f[w]>=0&&d[f[w]]++;}this.buildVersion++,this.isBuilt=true,this.rowPathStrs=this.buildPathStrs(this.rowTrie),this.colPathStrs=this.buildPathStrs(this.colTrie),this.rowIndexMapCache=this.buildIndexMap(this.rowPathStrs,null),this.columnIndexMapCache=this.buildIndexMap(this.colPathStrs,null);}buildPathStrs(t,e=[""]){let n=e;for(let r=e.length;r<t.size;r++){let i=t.nodeToParent[r],s=this.dictionary[t.nodeToVal[r]],c=n[i];n[r]=i===0?s===""?"|":s:c+"|"+s;}return n}buildIndexMap(t,e){let n={};for(let r=0;r<t.length;r++)(!e||e[r])&&(n[t[r]]=r);return n}applyTransaction(t){this.tracking||(this.tracking=true,this.buildFromMask(this.lastMask)),this.appendDictionary(t.dictionaryAppend);let{update:e,remove:n}=t,r=e.length,i=this.storedRowCount,s=i+t.addCount,c=this.storedColumns,a=new Set,d=false,f=this.rowGroupOf,w=l=>{let h=f[l];h<0||(f[l]=-1,--this.groupRows[h]===0&&(d=true),a.add(h));};e.forEach(w),n.forEach(w);for(let l in c){let h=c[l],I=t.columns[l];if(!I)throw new Error("transaction: no values for column "+l);let u=h.type==="int32"?new Int32Array(I):new Float64Array(I),y=h.array=H(h.array,s);for(let P=0;P<r;P++)y[e[P]]=u[P];y.set(u.subarray(r),i);}f=this.rowGroupOf=H(f,s),f.fill(-1,i,s);let g=this.lastMask&&(this.lastMask=H(this.lastMask,s));g&&(e.forEach(l=>this.computeMaskRange(l,l+1)),this.computeMaskRange(i,s));let R=this.rowFields.map(l=>c[l.id].array),B=this.colFields.map(l=>c[l.id].array),C=new Int32Array(R.length),b=new Int32Array(B.length),M=l=>{if(g&&!g[l])return;R.forEach((P,D)=>{C[D]=P[l];}),B.forEach((P,D)=>{b[D]=P[l];});let h=this.rowTrie.insert(C,C.length),I=this.colTrie.insert(b,b.length),u=h*67108864+I,y=this.groupMap.get(u);y===void 0&&(this.groupMap.set(u,y=this.groupCount++),(this.leafGroupRowIds=H(this.leafGroupRowIds,y+1))[y]=h,(this.leafGroupColIds=H(this.leafGroupColIds,y+1))[y]=I,this.groupRows=H(this.groupRows,y+1)),this.groupRows[y]++===0&&(d=true),f[l]=y,a.add(y);};e.forEach(M);for(let l=i;l<s;l++)M(l);if(this.storedRowCount=s-n.length,n.length>0){for(let l in c)pt(c[l].array,n,s);pt(f,n,s),g&&pt(g,n,s);}if(this.resumGroups(a),d){let l=0;for(let h=0;h<this.groupCount;h++)this.groupRows[h]===0&&l++;l>4096&&l>this.groupCount-l?this.buildFromMask(this.lastMask):(this.cardinalityMap={},[this.rowLive,this.rowPathStrs,this.rowIndexMapCache]=this.relive(this.rowTrie,this.leafGroupRowIds,this.rowFields,this.rowPathStrs),[this.colLive,this.colPathStrs,this.columnIndexMapCache]=this.relive(this.colTrie,this.leafGroupColIds,this.colFields,this.colPathStrs),this.buildVersion++);}return this.storedRowCount}precisionScales(){return Float64Array.from(this.dataFields,t=>ht(t))}resumGroups(t){if(t.size===0)return;let e=this.dataFields.length,n=this.dataFields.map(C=>C.summaryType||"sum"),r=this.dataFields.map(C=>this.storedColumns[C.id].array),i=this.precisionScales(),s=this.groupCount*e,c=this.leafGroupMeasures=H(this.leafGroupMeasures,s),a=n.some(C=>C==="count"||C==="avg")?this.leafGroupCounts=H(this.leafGroupCounts,s):null,d=n.includes("min")?this.leafGroupMins=H(this.leafGroupMins,s):null,f=n.includes("max")?this.leafGroupMaxes=H(this.leafGroupMaxes,s):null,w=this.leafGroupDistinctSets,g=new Uint8Array(this.groupCount),R=0;for(let C of t){g[C]=1,R+=this.groupRows[C];for(let b=C*e;b<(C+1)*e;b++)c[b]=0,a&&(a[b]=0),d&&(d[b]=1/0),f&&(f[b]=-1/0),w.delete(b);}let B=this.rowGroupOf;for(let C=0;R>0&&C<this.storedRowCount;C++){let b=B[C];if(!(b<0||!g[b])){R--;for(let M=0;M<e;M++){let l=i[M]!==0?dt(r[M][C],i[M]):r[M][C],h=b*e+M;switch(n[M]){case "count":a[h]++;break;case "avg":c[h]+=l,a[h]++;break;case "min":l<d[h]&&(d[h]=l);break;case "max":l>f[h]&&(f[h]=l);break;case "distinct":{if(l===0&&r[M]instanceof Int32Array)break;let I=w.get(h);I||w.set(h,I=new Set),I.add(l);break}default:c[h]+=l;}}}}}relive(t,e,n,r){let i=new Uint8Array(t.size);i[0]=1;for(let a=0;a<this.groupCount;a++)if(this.groupRows[a]!==0)for(let d=e[a];!i[d];d=t.nodeToParent[d])i[d]=1;let s=n.map(()=>new Set);t.nodeChildCount.fill(0,0,t.size);for(let a=1;a<t.size;a++)i[a]&&(t.nodeChildCount[t.nodeToParent[a]]++,s[t.nodeToDepth[a]-1]?.add(t.nodeToVal[a]));n.forEach((a,d)=>{this.cardinalityMap[a.id]=s[d].size;});let c=this.buildPathStrs(t,r);return [i,c,this.buildIndexMap(c,i)]}compute(t){this.isBuilt||this.buildFromMask(this.lastMask);let e=this.rowPathStrs,n=this.colPathStrs,r=new Uint8Array(this.rowTrie.size),i=new Set(t.expandedPaths),s=i.has(".*")||i.has("*");for(let o=0;o<this.rowTrie.size;o++)r[o]=ct(i,s,e[o])?1:0;let c=new Uint8Array(this.colTrie.size),a=new Set(t.expandedColPaths),d=a.has(".*")||a.has("*");for(let o=0;o<this.colTrie.size;o++)c[o]=ct(a,d,n[o])?1:0;let f=new Uint8Array(this.rowTrie.size);f[0]=1;for(let o=1;o<this.rowTrie.size;o++){let m=this.rowTrie.nodeToParent[o];(m===0||r[m])&&(!this.rowLive||this.rowLive[o])&&(f[o]=1);}let w=new Uint8Array(this.colTrie.size);w[0]=1;for(let o=1;o<this.colTrie.size;o++){let m=this.colTrie.nodeToParent[o];(m===0||c[m])&&(!this.colLive||this.colLive[o])&&(w[o]=1);}let g=this.dataFields.length,R=this.dataFields.map(o=>o.summaryType||"sum"),B=this.leafGroupMins.length>0,C=this.leafGroupMaxes.length>0,b=this.leafGroupCounts.length>0,M=this.leafGroupDistinctSets.size>0,l=new Map,h=0,I=1e4,u=new Float64Array(I),y=new Float64Array(I*g),P=new Int32Array(I*g),D=b?new Float64Array(I*g):null,S=B?new Float64Array(I*g):null,U=C?new Float64Array(I*g):null,et=M?new Map:new Map;S&&S.fill(1/0),U&&U.fill(-1/0);let $=new Int32Array(this.rowFields.length+1),q=new Int32Array(this.colFields.length+1),X=(o,m,A,x)=>{let Y=0,z=o;for(;z!==-1;)A[z]&&(x[Y++]=z),z=m.nodeToParent[z];return Y},J=this.rowGroupOf!==null&&R.includes("custom"),L=J?new Int32Array(this.groupCount+1):null,O=J?new Int32Array(Math.max(16,this.groupCount*4)):null,Q=0,V=this.tracking?this.groupRows:null;for(let o=0;o<this.groupCount;o++){if(V!==null&&V[o]===0){L!==null&&(L[o+1]=Q);continue}let m=this.leafGroupRowIds[o],A=this.leafGroupColIds[o],x=X(m,this.rowTrie,f,$),Y=X(A,this.colTrie,w,q);for(let z=0;z<x;z++){let at=$[z];for(let j=0;j<Y;j++){let lt=q[j],tt=at*67108864+lt,G=l.get(tt);if(G===void 0){if(G=h++,l.set(tt,G),G>=u.length){let k=G*2,T=new Float64Array(k);T.set(u),u=T;let _=new Float64Array(k*g);_.set(y),y=_;let K=new Int32Array(k*g);if(K.set(P),P=K,D){let W=new Float64Array(k*g);W.set(D),D=W;}if(S){let W=new Float64Array(k*g);W.fill(1/0),W.set(S),S=W;}if(U){let W=new Float64Array(k*g);W.fill(-1/0),W.set(U),U=W;}}u[G]=tt;}if(O!==null){if(Q>=O.length){let k=new Int32Array(O.length*2);k.set(O),O=k;}O[Q++]=G;}for(let k=0;k<g;k++){let T=G*g+k,_=o*g+k;switch(R[k]){case "sum":y[T]+=this.leafGroupMeasures[_];break;case "count":y[T]+=this.leafGroupCounts[_];break;case "avg":y[T]+=this.leafGroupMeasures[_],D[T]+=this.leafGroupCounts[_];break;case "min":{let K=this.leafGroupMins[_];K<S[T]&&(S[T]=K);break}case "max":{let K=this.leafGroupMaxes[_];K>U[T]&&(U[T]=K);break}case "distinct":{let K=this.leafGroupDistinctSets.get(_);if(K){let W=et.get(T);W||(W=new Set,et.set(T,W));for(let mt of K)W.add(mt);}break}default:y[T]+=this.leafGroupMeasures[_];break}P[T]+=1;}}}L!==null&&(L[o+1]=Q);}let it=this.precisionScales();for(let o=0;o<h;o++)for(let m=0;m<g;m++){let A=o*g+m,x=it[m];switch(R[m]){case "sum":x!==0&&(y[A]/=x);break;case "avg":y[A]=D[A]>0?y[A]/(x!==0?D[A]*x:D[A]):0;break;case "min":y[A]=S[A]!==1/0?x!==0?S[A]/x:S[A]:0;break;case "max":y[A]=U[A]!==-1/0?x!==0?U[A]/x:U[A]:0;break;case "distinct":{let Y=et.get(A);y[A]=Y?Y.size:0;break}}}let st=u.slice(0,h),ot=y.slice(0,h*g),N=P.slice(0,h*g),rt=(o,m,A,x,Y)=>{let z=[];for(let j=0;j<x.length;j++)z[j]=x[j]?.id||"";let at=j=>{let lt=o.childrenMap.get(j);if(!lt)return [];let tt=[];for(let G of lt.values())if(m[G]){let k=o.nodeToDepth[G]-1,T=A[G]===1;tt.push({key:Y[G],value:this.dictionary[o.nodeToVal[G]],fieldId:z[k]||"",children:T?at(G):[],isExpanded:T,isTotal:false,depth:k,hasChildren:o.nodeChildCount[G]>0});}return tt};return at(0)},p;if(this.rowGroupOf!==null&&L!==null&&O!==null){let o=this.storedRowCount,m={};for(let A=0;A<g;A++){if(R[A]!=="custom")continue;let x=this.dataFields[A].id;m[x]=this.storedColumns[x].array.slice(0,o);}p={rowGroupOf:this.rowGroupOf.slice(0,o),groupCellOffsets:L,groupCells:O.slice(0,Q),values:m};}let E=rt(this.rowTrie,f,r,this.rowFields,e),F=rt(this.colTrie,w,c,this.colFields,n);return {id:t.id,rowTree:E,colTree:F,cellMatrix:{keys:st,sums:ot,counts:N},customSummary:p,rowIndexMap:this.rowIndexMapCache,columnIndexMap:this.columnIndexMapCache,cardinalityMap:this.cardinalityMap,indexMapsVersion:this.buildVersion}}};var Z=new ut,yt=-1;self.onmessage=v=>{let t=v.data;try{if(t.action==="init")Z.init(t),self.postMessage({id:t.id,isInitComplete:!0});else if(t.action==="initStart")Z.initStart(t);else if(t.action==="initChunk")Z.initChunk(t);else if(t.action==="initEnd")Z.initEnd(),self.postMessage({id:t.id,isInitComplete:!0});else if(t.action==="filter")Z.applyFilter(t),self.postMessage({id:t.id,isFilterComplete:!0});else if(t.action==="reconfigure")Z.reconfigure(t),self.postMessage({id:t.id,isReconfigureComplete:!0});else if(t.action==="transaction")self.postMessage({id:t.id,isTransactionComplete:!0,rowCount:Z.applyTransaction(t)});else if(t.action==="compute"){let e=Z.compute(t),n=[e.cellMatrix.keys.buffer,e.cellMatrix.sums.buffer,e.cellMatrix.counts.buffer];if(e.indexMapsVersion!==void 0&&e.indexMapsVersion===yt){let r={...e,rowIndexMap:void 0,columnIndexMap:void 0,cardinalityMap:void 0,indexMapsOmitted:!0};postMessage(r,n);}else e.indexMapsVersion!==void 0&&(yt=e.indexMapsVersion),postMessage(e,n);}}catch(e){self.postMessage({id:t.id,error:e instanceof Error?e.message:String(e)});}};
@@ -1 +1 @@
1
- var pt="-";function ut(B,t,e){return t?!B.has(pt+e):B.has(e)}var nt=class{size=1;nodeToParent;nodeToVal;nodeToDepth;nodeChildCount;childrenMap;constructor(t){this.nodeToParent=new Int32Array(t).fill(-1),this.nodeToVal=new Int32Array(t).fill(-1),this.nodeToDepth=new Int32Array(t).fill(0),this.nodeChildCount=new Int32Array(t).fill(0),this.childrenMap=new Map;}expandCapacity(){let t=this.nodeToParent.length*2,e=new Int32Array(t).fill(-1);e.set(this.nodeToParent),this.nodeToParent=e;let n=new Int32Array(t).fill(-1);n.set(this.nodeToVal),this.nodeToVal=n;let r=new Int32Array(t).fill(0);r.set(this.nodeToDepth),this.nodeToDepth=r;let i=new Int32Array(t).fill(0);i.set(this.nodeChildCount),this.nodeChildCount=i;}insert(t,e){let n=0;for(let r=0;r<e;r++){let i=t[r],s=this.childrenMap.get(n);s||(s=new Map,this.childrenMap.set(n,s));let c=s.get(i);c===void 0&&(this.size>=this.nodeToParent.length&&this.expandCapacity(),c=this.size++,this.nodeToParent[c]=n,this.nodeToVal[c]=i,this.nodeToDepth[c]=r+1,this.nodeChildCount[n]++,s.set(i,c)),n=c;}return n}};function _(B,t){if(B.length>=t)return B;let e=new B.constructor(Math.max(t,Math.ceil(B.length*1.5)));return e.set(B),e}function dt(B,t,e){let n=t[0];for(let r=0;r<t.length;r++){let i=t[r]+1,s=r+1<t.length?t[r+1]:e;B.copyWithin(n,i,s),n+=s-i;}}var lt=class{dictionary=[];rowFields=[];colFields=[];dataFields=[];storedColumns={};storedRowCount=0;lastMask=null;isBuilt=false;buildState=null;activeFilters=null;activePrefilter=null;tracking=false;groupRows=new Int32Array(0);groupMap=new Map;rowLive=null;colLive=null;rowTrie;colTrie;cardinalityMap={};rowPathStrs=[];colPathStrs=[];rowIndexMapCache={};columnIndexMapCache={};buildVersion=0;groupCount=0;leafGroupRowIds;leafGroupColIds;leafGroupMeasures;leafGroupCounts;leafGroupMins;leafGroupMaxes;leafGroupDistinctSets=new Map;rowGroupOf=null;init(t){this.dictionary=t.dictionary,this.rowFields=t.rowFields,this.colFields=t.colFields,this.dataFields=t.dataFields,t.workerBootstrap&&t.workerBootstrap.length>0&&console.warn("[PivotGrid Worker] workerBootstrap is deprecated for security reasons. All calculations are executed on the main thread."),this.storedRowCount=t.rowCount,this.storedColumns={};for(let[e,n]of Object.entries(t.columnarData))n.type==="int32"&&(this.storedColumns[e]={type:"int32",array:new Int32Array(n.buffer)}),n.type==="float64"&&(this.storedColumns[e]={type:"float64",array:new Float64Array(n.buffer)});this.lastMask=null,this.activeFilters=this.activePrefilter=null,t.deferBuild?this.isBuilt=false:this.buildFromMask(null);}reconfigure(t){if(this.rowFields=t.rowFields,this.colFields=t.colFields,this.dataFields=t.dataFields,this.appendDictionary(t.dictionaryAppend),t.dropColumns)for(let e of t.dropColumns)delete this.storedColumns[e];if(t.newColumns)for(let[e,n]of Object.entries(t.newColumns))n.type==="int32"&&(this.storedColumns[e]={type:"int32",array:new Int32Array(n.buffer)}),n.type==="float64"&&(this.storedColumns[e]={type:"float64",array:new Float64Array(n.buffer)});this.buildFromMask(this.lastMask);}initStart(t){this.dictionary=t.dictionary,this.rowFields=t.rowFields,this.colFields=t.colFields,this.dataFields=t.dataFields,t.workerBootstrap&&t.workerBootstrap.length>0&&console.warn("[PivotGrid Worker] workerBootstrap is deprecated for security reasons. All calculations are executed on the main thread."),this.storedRowCount=t.rowCountTotal,this.storedColumns={};for(let[e,n]of Object.entries(t.columnTypes))n==="int32"?this.storedColumns[e]={type:"int32",array:new Int32Array(t.rowCountTotal)}:this.storedColumns[e]={type:"float64",array:new Float64Array(t.rowCountTotal)};this.setFilters((t.filters||[]).map(e=>({fieldId:e.fieldId,filterType:e.filterType,ids:new Set,pending:new Set(e.valueStrings)})),t.prefilter),this.resolveIncrementalFilterStrings(0),this.isBuilt=false,this.beginBuild();}initChunk(t){this.appendDictionary(t.dictionaryAppend)&&this.ensureCardCapacity();for(let[e,n]of Object.entries(t.columns)){let r=this.storedColumns[e];r&&(r.type==="int32"?r.array.set(new Int32Array(n),t.start):r.array.set(new Float64Array(n),t.start));}this.lastMask&&this.computeMaskRange(t.start,t.end),this.processRange(t.start,t.end,this.lastMask);}initEnd(){this.finalizeBuild();}appendDictionary(t){if(!t||t.length===0)return false;let e=this.dictionary.length;for(let n of t)this.dictionary.push(n);return this.resolveIncrementalFilterStrings(e),true}setFilters(t,e){this.activeFilters=t.length>0?t:null,this.activePrefilter=e&&e.conditions.length>0?e:null,this.lastMask=this.activeFilters||this.activePrefilter?new Uint8Array(this.storedRowCount):null;}resolveIncrementalFilterStrings(t){let e=this.activeFilters;if(!e)return;let n=0;for(let r of e)n+=r.pending.size;if(n!==0)for(let r=t;r<this.dictionary.length;r++){let i=this.dictionary[r];for(let s of e)s.pending.delete(i)&&s.ids.add(r);}}computeMaskRange(t,e){let n=this.lastMask;if(n.fill(1,t,e),this.activeFilters)for(let r of this.activeFilters){let i=this.storedColumns[r.fieldId];if(!i||i.type!=="int32")continue;let s=i.array,c=r.ids;if(r.filterType==="exclude")for(let a=t;a<e;a++)n[a]&&c.has(s[a])&&(n[a]=0);else for(let a=t;a<e;a++)n[a]&&!c.has(s[a])&&(n[a]=0);}if(this.activePrefilter)for(let r=t;r<e;r++)n[r]&&!this.evaluatePrefilterGroup(this.activePrefilter,r)&&(n[r]=0);}applyFilter(t){this.setFilters(t.filters.map(e=>({fieldId:e.fieldId,filterType:e.filterType,ids:new Set(e.dictIds),pending:new Set(e.valueStrings)})),t.prefilter),this.lastMask&&this.computeMaskRange(0,this.storedRowCount),this.buildFromMask(this.lastMask);}evaluatePrefilterGroup(t,e){if(!t.conditions||t.conditions.length===0)return true;if(t.logicalOperator==="and"){for(let n of t.conditions)if("logicalOperator"in n){if(!this.evaluatePrefilterGroup(n,e))return false}else if(!this.evaluatePrefilterCondition(n,e))return false;return true}else {for(let n of t.conditions)if("logicalOperator"in n){if(this.evaluatePrefilterGroup(n,e))return true}else if(this.evaluatePrefilterCondition(n,e))return true;return false}}evaluatePrefilterCondition(t,e){let n=this.storedColumns[t.fieldId];if(!n)return true;if(n.type==="float64"){let f=n.array[e],y=Number(t.value);switch(t.operator){case "=":return f===y;case "<>":return f!==y;case ">":return f>y;case ">=":return f>=y;case "<":return f<y;case "<=":return f<=y;case "isnull":return isNaN(f)||f===0;case "isnotnull":return !isNaN(f)&&f!==0;default:return true}}let r=n.array[e],i=this.dictionary[r]||"",s=String(t.value??""),c=Number(i),a=Number(s),d=i!==""&&s!==""&&!isNaN(c)&&!isNaN(a);switch(t.operator){case "=":return i===s;case "<>":return i!==s;case ">":return d?c>a:i>s;case ">=":return d?c>=a:i>=s;case "<":return d?c<a:i<s;case "<=":return d?c<=a:i<=s;case "contains":return i.toLowerCase().includes(s.toLowerCase());case "notcontains":return !i.toLowerCase().includes(s.toLowerCase());case "startswith":return i.toLowerCase().startsWith(s.toLowerCase());case "endswith":return i.toLowerCase().endsWith(s.toLowerCase());case "isnull":return i===""||i==="(Blank)";case "isnotnull":return i!==""&&i!=="(Blank)";case "in":return Array.isArray(t.value)?t.value.map(String).includes(i):true;case "notin":return Array.isArray(t.value)?!t.value.map(String).includes(i):true;default:return true}}buildFromMask(t){this.beginBuild(),this.processRange(0,this.storedRowCount,t),this.finalizeBuild();}beginBuild(){let t=this.storedRowCount,e=this.storedColumns,n=this.rowFields.length,r=this.colFields.length,i=new Array(n);for(let u=0;u<n;u++)i[u]=e[this.rowFields[u].id].array;let s=new Array(r);for(let u=0;u<r;u++)s[u]=e[this.colFields[u].id].array;let c=this.dictionary.length,a=this.rowFields.map(u=>{let v=e[u.id];return v&&v.type==="int32"?new Uint8Array(c):null}),d=this.colFields.map(u=>{let v=e[u.id];return v&&v.type==="int32"?new Uint8Array(c):null});this.rowTrie=new nt(2e5),this.colTrie=new nt(2e5);let f=this.dataFields.length,y=this.dataFields.map(u=>u.summaryType||"sum"),p=y.some(u=>u==="count"||u==="avg"),G=y.some(u=>u==="min"),g=y.some(u=>u==="max"),F=y.some(u=>u==="distinct"),R=new Array(f);for(let u=0;u<f;u++)R[u]=e[this.dataFields[u].id].array;let b=Math.min(t,2e5),l=G?new Float64Array(b*f):new Float64Array(0),w=g?new Float64Array(b*f):new Float64Array(0);this.leafGroupDistinctSets=F?new Map:new Map,G&&l.fill(1/0),g&&w.fill(-1/0),this.buildState={rPath:new Int32Array(n),cPath:new Int32Array(r),rowColArrays:i,colColArrays:s,dataColArrays:R,rowCardSeen:a,colCardSeen:d,rowCardCounts:new Int32Array(n),colCardCounts:new Int32Array(r),summaryTypes:y,allSums:y.every(u=>u==="sum"),needsCounts:p,needsMins:G,needsMaxes:g,groupMap:new Map,leafRowIds:new Int32Array(b),leafColIds:new Int32Array(b),leafMeasures:new Float64Array(b*f),leafCounts:p?new Float64Array(b*f):new Float64Array(0),leafMins:l,leafMaxes:w,rowGroupOf:this.tracking||y.includes("custom")?new Int32Array(t).fill(-1):null,gCount:0};}ensureCardCapacity(){let t=this.buildState;if(!t)return;let e=this.dictionary.length,n=r=>{if(r===null||r.length>=e)return r;let i=new Uint8Array(Math.max(e,r.length*2));return i.set(r),i};for(let r=0;r<t.rowCardSeen.length;r++)t.rowCardSeen[r]=n(t.rowCardSeen[r]);for(let r=0;r<t.colCardSeen.length;r++)t.colCardSeen[r]=n(t.colCardSeen[r]);}processRange(t,e,n){let r=this.buildState,{rPath:i,cPath:s,rowColArrays:c,colColArrays:a,dataColArrays:d,rowCardSeen:f,colCardSeen:y,rowCardCounts:p,colCardCounts:G,summaryTypes:g,allSums:F,needsCounts:R,needsMins:b,needsMaxes:l,groupMap:w,rowGroupOf:u}=r,v=c.length,m=a.length,M=d.length,E=this.rowTrie,L=this.colTrie,W=r.leafRowIds,$=r.leafColIds,K=r.leafMeasures,J=r.leafCounts,Q=r.leafMins,Y=r.leafMaxes,X=r.gCount;for(let T=t;T<e;T++){if(n&&!n[T])continue;for(let h=0;h<v;h++){let A=c[h][T];i[h]=A;let C=f[h];C!==null&&C[A]===0&&(C[A]=1,p[h]++);}let Z=E.insert(i,v);for(let h=0;h<m;h++){let A=a[h][T];s[h]=A;let C=y[h];C!==null&&C[A]===0&&(C[A]=1,G[h]++);}let rt=L.insert(s,m),it=Z*67108864+rt,z=w.get(it);if(z===void 0){if(z=X++,w.set(it,z),z>=W.length){let h=z*2,A=new Int32Array(h);A.set(W),W=A;let C=new Int32Array(h);C.set($),$=C;let H=new Float64Array(h*M);if(H.set(K),K=H,R){let o=new Float64Array(h*M);o.set(J),J=o;}if(b){let o=new Float64Array(h*M);o.fill(1/0),o.set(Q),Q=o;}if(l){let o=new Float64Array(h*M);o.fill(-1/0),o.set(Y),Y=o;}}W[z]=Z,$[z]=rt;}u!==null&&(u[T]=z);let st=z*M;if(F)for(let h=0;h<M;h++)K[st+h]+=d[h][T];else for(let h=0;h<M;h++){let A=d[h][T],C=st+h;switch(g[h]){case "sum":K[C]+=A;break;case "count":J[C]+=1;break;case "avg":K[C]+=A,J[C]+=1;break;case "min":A<Q[C]&&(Q[C]=A);break;case "max":A>Y[C]&&(Y[C]=A);break;case "distinct":{if(A===0&&d[h]instanceof Int32Array)break;let H=this.leafGroupDistinctSets.get(C);H||(H=new Set,this.leafGroupDistinctSets.set(C,H)),H.add(A);break}default:K[C]+=A;break}}}r.leafRowIds=W,r.leafColIds=$,r.leafMeasures=K,r.leafCounts=J,r.leafMins=Q,r.leafMaxes=Y,r.gCount=X;}finalizeBuild(){let t=this.buildState,{needsCounts:e,needsMins:n,needsMaxes:r,gCount:i}=t,s=t.dataColArrays.length,c=t.rowColArrays.length,a=t.colColArrays.length;this.groupCount=i,this.leafGroupRowIds=t.leafRowIds.slice(0,i),this.leafGroupColIds=t.leafColIds.slice(0,i),this.leafGroupMeasures=t.leafMeasures.slice(0,i*s),this.leafGroupCounts=e?t.leafCounts.slice(0,i*s):t.leafCounts,this.leafGroupMins=n?t.leafMins.slice(0,i*s):t.leafMins,this.leafGroupMaxes=r?t.leafMaxes.slice(0,i*s):t.leafMaxes,this.rowGroupOf=t.rowGroupOf,this.cardinalityMap={};for(let d=0;d<c;d++)t.rowCardSeen[d]!==null&&(this.cardinalityMap[this.rowFields[d].id]=t.rowCardCounts[d]);for(let d=0;d<a;d++)t.colCardSeen[d]!==null&&(this.cardinalityMap[this.colFields[d].id]=t.colCardCounts[d]);if(this.buildState=null,this.rowLive=this.colLive=null,this.tracking){this.groupMap=t.groupMap;let d=this.groupRows=new Int32Array(i),f=t.rowGroupOf;for(let y=0;y<this.storedRowCount;y++)f[y]>=0&&d[f[y]]++;}this.buildVersion++,this.isBuilt=true,this.rowPathStrs=this.buildPathStrs(this.rowTrie),this.colPathStrs=this.buildPathStrs(this.colTrie),this.rowIndexMapCache=this.buildIndexMap(this.rowPathStrs,null),this.columnIndexMapCache=this.buildIndexMap(this.colPathStrs,null);}buildPathStrs(t,e=[""]){let n=e;for(let r=e.length;r<t.size;r++){let i=t.nodeToParent[r],s=this.dictionary[t.nodeToVal[r]],c=n[i];n[r]=i===0?s===""?"|":s:c+"|"+s;}return n}buildIndexMap(t,e){let n={};for(let r=0;r<t.length;r++)(!e||e[r])&&(n[t[r]]=r);return n}applyTransaction(t){this.tracking||(this.tracking=true,this.buildFromMask(this.lastMask)),this.appendDictionary(t.dictionaryAppend);let{update:e,remove:n}=t,r=e.length,i=this.storedRowCount,s=i+t.addCount,c=this.storedColumns,a=new Set,d=false,f=this.rowGroupOf,y=l=>{let w=f[l];w<0||(f[l]=-1,--this.groupRows[w]===0&&(d=true),a.add(w));};e.forEach(y),n.forEach(y);for(let l in c){let w=c[l],u=t.columns[l];if(!u)throw new Error("transaction: no values for column "+l);let v=w.type==="int32"?new Int32Array(u):new Float64Array(u),m=w.array=_(w.array,s);for(let M=0;M<r;M++)m[e[M]]=v[M];m.set(v.subarray(r),i);}f=this.rowGroupOf=_(f,s),f.fill(-1,i,s);let p=this.lastMask&&(this.lastMask=_(this.lastMask,s));p&&(e.forEach(l=>this.computeMaskRange(l,l+1)),this.computeMaskRange(i,s));let G=this.rowFields.map(l=>c[l.id].array),g=this.colFields.map(l=>c[l.id].array),F=new Int32Array(G.length),R=new Int32Array(g.length),b=l=>{if(p&&!p[l])return;G.forEach((M,E)=>{F[E]=M[l];}),g.forEach((M,E)=>{R[E]=M[l];});let w=this.rowTrie.insert(F,F.length),u=this.colTrie.insert(R,R.length),v=w*67108864+u,m=this.groupMap.get(v);m===void 0&&(this.groupMap.set(v,m=this.groupCount++),(this.leafGroupRowIds=_(this.leafGroupRowIds,m+1))[m]=w,(this.leafGroupColIds=_(this.leafGroupColIds,m+1))[m]=u,this.groupRows=_(this.groupRows,m+1)),this.groupRows[m]++===0&&(d=true),f[l]=m,a.add(m);};e.forEach(b);for(let l=i;l<s;l++)b(l);if(this.storedRowCount=s-n.length,n.length>0){for(let l in c)dt(c[l].array,n,s);dt(f,n,s),p&&dt(p,n,s);}if(this.resumGroups(a),d){let l=0;for(let w=0;w<this.groupCount;w++)this.groupRows[w]===0&&l++;l>4096&&l>this.groupCount-l?this.buildFromMask(this.lastMask):(this.cardinalityMap={},[this.rowLive,this.rowPathStrs,this.rowIndexMapCache]=this.relive(this.rowTrie,this.leafGroupRowIds,this.rowFields,this.rowPathStrs),[this.colLive,this.colPathStrs,this.columnIndexMapCache]=this.relive(this.colTrie,this.leafGroupColIds,this.colFields,this.colPathStrs),this.buildVersion++);}return this.storedRowCount}resumGroups(t){if(t.size===0)return;let e=this.dataFields.length,n=this.dataFields.map(g=>g.summaryType||"sum"),r=this.dataFields.map(g=>this.storedColumns[g.id].array),i=this.groupCount*e,s=this.leafGroupMeasures=_(this.leafGroupMeasures,i),c=n.some(g=>g==="count"||g==="avg")?this.leafGroupCounts=_(this.leafGroupCounts,i):null,a=n.includes("min")?this.leafGroupMins=_(this.leafGroupMins,i):null,d=n.includes("max")?this.leafGroupMaxes=_(this.leafGroupMaxes,i):null,f=this.leafGroupDistinctSets,y=new Uint8Array(this.groupCount),p=0;for(let g of t){y[g]=1,p+=this.groupRows[g];for(let F=g*e;F<(g+1)*e;F++)s[F]=0,c&&(c[F]=0),a&&(a[F]=1/0),d&&(d[F]=-1/0),f.delete(F);}let G=this.rowGroupOf;for(let g=0;p>0&&g<this.storedRowCount;g++){let F=G[g];if(!(F<0||!y[F])){p--;for(let R=0;R<e;R++){let b=r[R][g],l=F*e+R;switch(n[R]){case "count":c[l]++;break;case "avg":s[l]+=b,c[l]++;break;case "min":b<a[l]&&(a[l]=b);break;case "max":b>d[l]&&(d[l]=b);break;case "distinct":{if(b===0&&r[R]instanceof Int32Array)break;let w=f.get(l);w||f.set(l,w=new Set),w.add(b);break}default:s[l]+=b;}}}}}relive(t,e,n,r){let i=new Uint8Array(t.size);i[0]=1;for(let a=0;a<this.groupCount;a++)if(this.groupRows[a]!==0)for(let d=e[a];!i[d];d=t.nodeToParent[d])i[d]=1;let s=n.map(()=>new Set);t.nodeChildCount.fill(0,0,t.size);for(let a=1;a<t.size;a++)i[a]&&(t.nodeChildCount[t.nodeToParent[a]]++,s[t.nodeToDepth[a]-1]?.add(t.nodeToVal[a]));n.forEach((a,d)=>{this.cardinalityMap[a.id]=s[d].size;});let c=this.buildPathStrs(t,r);return [i,c,this.buildIndexMap(c,i)]}compute(t){this.isBuilt||this.buildFromMask(this.lastMask);let e=this.rowPathStrs,n=this.colPathStrs,r=new Uint8Array(this.rowTrie.size),i=new Set(t.expandedPaths),s=i.has(".*")||i.has("*");for(let o=0;o<this.rowTrie.size;o++)r[o]=ut(i,s,e[o])?1:0;let c=new Uint8Array(this.colTrie.size),a=new Set(t.expandedColPaths),d=a.has(".*")||a.has("*");for(let o=0;o<this.colTrie.size;o++)c[o]=ut(a,d,n[o])?1:0;let f=new Uint8Array(this.rowTrie.size);f[0]=1;for(let o=1;o<this.rowTrie.size;o++){let S=this.rowTrie.nodeToParent[o];(S===0||r[S])&&(!this.rowLive||this.rowLive[o])&&(f[o]=1);}let y=new Uint8Array(this.colTrie.size);y[0]=1;for(let o=1;o<this.colTrie.size;o++){let S=this.colTrie.nodeToParent[o];(S===0||c[S])&&(!this.colLive||this.colLive[o])&&(y[o]=1);}let p=this.dataFields.length,G=this.dataFields.map(o=>o.summaryType||"sum"),g=this.leafGroupMins.length>0,F=this.leafGroupMaxes.length>0,R=this.leafGroupCounts.length>0,b=this.leafGroupDistinctSets.size>0,l=new Map,w=0,u=1e4,v=new Float64Array(u),m=new Float64Array(u*p),M=new Int32Array(u*p),E=R?new Float64Array(u*p):null,L=g?new Float64Array(u*p):null,W=F?new Float64Array(u*p):null,$=b?new Map:new Map;L&&L.fill(1/0),W&&W.fill(-1/0);let K=new Int32Array(this.rowFields.length+1),J=new Int32Array(this.colFields.length+1),Q=(o,S,I,O)=>{let tt=0,U=o;for(;U!==-1;)I[U]&&(O[tt++]=U),U=S.nodeToParent[U];return tt},Y=this.rowGroupOf!==null&&G.includes("custom"),X=Y?new Int32Array(this.groupCount+1):null,T=Y?new Int32Array(Math.max(16,this.groupCount*4)):null,Z=0,rt=this.tracking?this.groupRows:null;for(let o=0;o<this.groupCount;o++){if(rt!==null&&rt[o]===0){X!==null&&(X[o+1]=Z);continue}let S=this.leafGroupRowIds[o],I=this.leafGroupColIds[o],O=Q(S,this.rowTrie,f,K),tt=Q(I,this.colTrie,y,J);for(let U=0;U<O;U++){let ot=K[U];for(let j=0;j<tt;j++){let at=J[j],et=ot*67108864+at,P=l.get(et);if(P===void 0){if(P=w++,l.set(et,P),P>=v.length){let x=P*2,k=new Float64Array(x);k.set(v),v=k;let V=new Float64Array(x*p);V.set(m),m=V;let N=new Int32Array(x*p);if(N.set(M),M=N,E){let D=new Float64Array(x*p);D.set(E),E=D;}if(L){let D=new Float64Array(x*p);D.fill(1/0),D.set(L),L=D;}if(W){let D=new Float64Array(x*p);D.fill(-1/0),D.set(W),W=D;}}v[P]=et;}if(T!==null){if(Z>=T.length){let x=new Int32Array(T.length*2);x.set(T),T=x;}T[Z++]=P;}for(let x=0;x<p;x++){let k=P*p+x,V=o*p+x;switch(G[x]){case "sum":m[k]+=this.leafGroupMeasures[V];break;case "count":m[k]+=this.leafGroupCounts[V];break;case "avg":m[k]+=this.leafGroupMeasures[V],E[k]+=this.leafGroupCounts[V];break;case "min":{let N=this.leafGroupMins[V];N<L[k]&&(L[k]=N);break}case "max":{let N=this.leafGroupMaxes[V];N>W[k]&&(W[k]=N);break}case "distinct":{let N=this.leafGroupDistinctSets.get(V);if(N){let D=$.get(k);D||(D=new Set,$.set(k,D));for(let ht of N)D.add(ht);}break}default:m[k]+=this.leafGroupMeasures[V];break}M[k]+=1;}}}X!==null&&(X[o+1]=Z);}for(let o=0;o<w;o++)for(let S=0;S<p;S++){let I=o*p+S;switch(G[S]){case "avg":m[I]=E[I]>0?m[I]/E[I]:0;break;case "min":m[I]=L[I]!==1/0?L[I]:0;break;case "max":m[I]=W[I]!==-1/0?W[I]:0;break;case "distinct":{let O=$.get(I);m[I]=O?O.size:0;break}}}let it=v.slice(0,w),z=m.slice(0,w*p),st=M.slice(0,w*p),h=(o,S,I,O,tt)=>{let U=[];for(let j=0;j<O.length;j++)U[j]=O[j]?.id||"";let ot=j=>{let at=o.childrenMap.get(j);if(!at)return [];let et=[];for(let P of at.values())if(S[P]){let x=o.nodeToDepth[P]-1,k=I[P]===1;et.push({key:tt[P],value:this.dictionary[o.nodeToVal[P]],fieldId:U[x]||"",children:k?ot(P):[],isExpanded:k,isTotal:false,depth:x,hasChildren:o.nodeChildCount[P]>0});}return et};return ot(0)},A;if(this.rowGroupOf!==null&&X!==null&&T!==null){let o=this.storedRowCount,S={};for(let I=0;I<p;I++){if(G[I]!=="custom")continue;let O=this.dataFields[I].id;S[O]=this.storedColumns[O].array.slice(0,o);}A={rowGroupOf:this.rowGroupOf.slice(0,o),groupCellOffsets:X,groupCells:T.slice(0,Z),values:S};}let C=h(this.rowTrie,f,r,this.rowFields,e),H=h(this.colTrie,y,c,this.colFields,n);return {id:t.id,rowTree:C,colTree:H,cellMatrix:{keys:it,sums:z,counts:st},customSummary:A,rowIndexMap:this.rowIndexMapCache,columnIndexMap:this.columnIndexMapCache,cardinalityMap:this.cardinalityMap,indexMapsVersion:this.buildVersion}}};var q=new lt,ft=-1;self.onmessage=B=>{let t=B.data;try{if(t.action==="init")q.init(t),self.postMessage({id:t.id,isInitComplete:!0});else if(t.action==="initStart")q.initStart(t);else if(t.action==="initChunk")q.initChunk(t);else if(t.action==="initEnd")q.initEnd(),self.postMessage({id:t.id,isInitComplete:!0});else if(t.action==="filter")q.applyFilter(t),self.postMessage({id:t.id,isFilterComplete:!0});else if(t.action==="reconfigure")q.reconfigure(t),self.postMessage({id:t.id,isReconfigureComplete:!0});else if(t.action==="transaction")self.postMessage({id:t.id,isTransactionComplete:!0,rowCount:q.applyTransaction(t)});else if(t.action==="compute"){let e=q.compute(t),n=[e.cellMatrix.keys.buffer,e.cellMatrix.sums.buffer,e.cellMatrix.counts.buffer];if(e.indexMapsVersion!==void 0&&e.indexMapsVersion===ft){let r={...e,rowIndexMap:void 0,columnIndexMap:void 0,cardinalityMap:void 0,indexMapsOmitted:!0};postMessage(r,n);}else e.indexMapsVersion!==void 0&&(ft=e.indexMapsVersion),postMessage(e,n);}}catch(e){self.postMessage({id:t.id,error:e instanceof Error?e.message:String(e)});}};
1
+ var wt="-";function ct(v,t,e){return t?!v.has(wt+e):v.has(e)}var nt=class{size=1;nodeToParent;nodeToVal;nodeToDepth;nodeChildCount;childrenMap;constructor(t){this.nodeToParent=new Int32Array(t).fill(-1),this.nodeToVal=new Int32Array(t).fill(-1),this.nodeToDepth=new Int32Array(t).fill(0),this.nodeChildCount=new Int32Array(t).fill(0),this.childrenMap=new Map;}expandCapacity(){let t=this.nodeToParent.length*2,e=new Int32Array(t).fill(-1);e.set(this.nodeToParent),this.nodeToParent=e;let n=new Int32Array(t).fill(-1);n.set(this.nodeToVal),this.nodeToVal=n;let r=new Int32Array(t).fill(0);r.set(this.nodeToDepth),this.nodeToDepth=r;let i=new Int32Array(t).fill(0);i.set(this.nodeChildCount),this.nodeChildCount=i;}insert(t,e){let n=0;for(let r=0;r<e;r++){let i=t[r],s=this.childrenMap.get(n);s||(s=new Map,this.childrenMap.set(n,s));let c=s.get(i);c===void 0&&(this.size>=this.nodeToParent.length&&this.expandCapacity(),c=this.size++,this.nodeToParent[c]=n,this.nodeToVal[c]=i,this.nodeToDepth[c]=r+1,this.nodeChildCount[n]++,s.set(i,c)),n=c;}return n}};var gt=new Set(["sum","avg","min","max"]);function ht(v){let t=v.precision;return typeof t!="number"||!Number.isInteger(t)||t<0||t>10?0:gt.has(v.summaryType||"sum")?10**t:0}function dt(v,t){let e=v*t;return e<0?-Math.round(-e):Math.round(e)}function H(v,t){if(v.length>=t)return v;let e=new v.constructor(Math.max(t,Math.ceil(v.length*1.5)));return e.set(v),e}function pt(v,t,e){let n=t[0];for(let r=0;r<t.length;r++){let i=t[r]+1,s=r+1<t.length?t[r+1]:e;v.copyWithin(n,i,s),n+=s-i;}}var ut=class{dictionary=[];rowFields=[];colFields=[];dataFields=[];storedColumns={};storedRowCount=0;lastMask=null;isBuilt=false;buildState=null;activeFilters=null;activePrefilter=null;tracking=false;groupRows=new Int32Array(0);groupMap=new Map;rowLive=null;colLive=null;rowTrie;colTrie;cardinalityMap={};rowPathStrs=[];colPathStrs=[];rowIndexMapCache={};columnIndexMapCache={};buildVersion=0;groupCount=0;leafGroupRowIds;leafGroupColIds;leafGroupMeasures;leafGroupCounts;leafGroupMins;leafGroupMaxes;leafGroupDistinctSets=new Map;rowGroupOf=null;init(t){this.dictionary=t.dictionary,this.rowFields=t.rowFields,this.colFields=t.colFields,this.dataFields=t.dataFields,t.workerBootstrap&&t.workerBootstrap.length>0&&console.warn("[PivotGrid Worker] workerBootstrap is deprecated for security reasons. All calculations are executed on the main thread."),this.storedRowCount=t.rowCount,this.storedColumns={};for(let[e,n]of Object.entries(t.columnarData))n.type==="int32"&&(this.storedColumns[e]={type:"int32",array:new Int32Array(n.buffer)}),n.type==="float64"&&(this.storedColumns[e]={type:"float64",array:new Float64Array(n.buffer)});this.lastMask=null,this.activeFilters=this.activePrefilter=null,t.deferBuild?this.isBuilt=false:this.buildFromMask(null);}reconfigure(t){if(this.rowFields=t.rowFields,this.colFields=t.colFields,this.dataFields=t.dataFields,this.appendDictionary(t.dictionaryAppend),t.dropColumns)for(let e of t.dropColumns)delete this.storedColumns[e];if(t.newColumns)for(let[e,n]of Object.entries(t.newColumns))n.type==="int32"&&(this.storedColumns[e]={type:"int32",array:new Int32Array(n.buffer)}),n.type==="float64"&&(this.storedColumns[e]={type:"float64",array:new Float64Array(n.buffer)});this.buildFromMask(this.lastMask);}initStart(t){this.dictionary=t.dictionary,this.rowFields=t.rowFields,this.colFields=t.colFields,this.dataFields=t.dataFields,t.workerBootstrap&&t.workerBootstrap.length>0&&console.warn("[PivotGrid Worker] workerBootstrap is deprecated for security reasons. All calculations are executed on the main thread."),this.storedRowCount=t.rowCountTotal,this.storedColumns={};for(let[e,n]of Object.entries(t.columnTypes))n==="int32"?this.storedColumns[e]={type:"int32",array:new Int32Array(t.rowCountTotal)}:this.storedColumns[e]={type:"float64",array:new Float64Array(t.rowCountTotal)};this.setFilters((t.filters||[]).map(e=>({fieldId:e.fieldId,filterType:e.filterType,ids:new Set,pending:new Set(e.valueStrings)})),t.prefilter),this.resolveIncrementalFilterStrings(0),this.isBuilt=false,this.beginBuild();}initChunk(t){this.appendDictionary(t.dictionaryAppend)&&this.ensureCardCapacity();for(let[e,n]of Object.entries(t.columns)){let r=this.storedColumns[e];r&&(r.type==="int32"?r.array.set(new Int32Array(n),t.start):r.array.set(new Float64Array(n),t.start));}this.lastMask&&this.computeMaskRange(t.start,t.end),this.processRange(t.start,t.end,this.lastMask);}initEnd(){this.finalizeBuild();}appendDictionary(t){if(!t||t.length===0)return false;let e=this.dictionary.length;for(let n of t)this.dictionary.push(n);return this.resolveIncrementalFilterStrings(e),true}setFilters(t,e){this.activeFilters=t.length>0?t:null,this.activePrefilter=e&&e.conditions.length>0?e:null,this.lastMask=this.activeFilters||this.activePrefilter?new Uint8Array(this.storedRowCount):null;}resolveIncrementalFilterStrings(t){let e=this.activeFilters;if(!e)return;let n=0;for(let r of e)n+=r.pending.size;if(n!==0)for(let r=t;r<this.dictionary.length;r++){let i=this.dictionary[r];for(let s of e)s.pending.delete(i)&&s.ids.add(r);}}computeMaskRange(t,e){let n=this.lastMask;if(n.fill(1,t,e),this.activeFilters)for(let r of this.activeFilters){let i=this.storedColumns[r.fieldId];if(!i||i.type!=="int32")continue;let s=i.array,c=r.ids;if(r.filterType==="exclude")for(let a=t;a<e;a++)n[a]&&c.has(s[a])&&(n[a]=0);else for(let a=t;a<e;a++)n[a]&&!c.has(s[a])&&(n[a]=0);}if(this.activePrefilter)for(let r=t;r<e;r++)n[r]&&!this.evaluatePrefilterGroup(this.activePrefilter,r)&&(n[r]=0);}applyFilter(t){this.setFilters(t.filters.map(e=>({fieldId:e.fieldId,filterType:e.filterType,ids:new Set(e.dictIds),pending:new Set(e.valueStrings)})),t.prefilter),this.lastMask&&this.computeMaskRange(0,this.storedRowCount),this.buildFromMask(this.lastMask);}evaluatePrefilterGroup(t,e){if(!t.conditions||t.conditions.length===0)return true;if(t.logicalOperator==="and"){for(let n of t.conditions)if("logicalOperator"in n){if(!this.evaluatePrefilterGroup(n,e))return false}else if(!this.evaluatePrefilterCondition(n,e))return false;return true}else {for(let n of t.conditions)if("logicalOperator"in n){if(this.evaluatePrefilterGroup(n,e))return true}else if(this.evaluatePrefilterCondition(n,e))return true;return false}}evaluatePrefilterCondition(t,e){let n=this.storedColumns[t.fieldId];if(!n)return true;if(n.type==="float64"){let f=n.array[e],w=Number(t.value);switch(t.operator){case "=":return f===w;case "<>":return f!==w;case ">":return f>w;case ">=":return f>=w;case "<":return f<w;case "<=":return f<=w;case "isnull":return isNaN(f)||f===0;case "isnotnull":return !isNaN(f)&&f!==0;default:return true}}let r=n.array[e],i=this.dictionary[r]||"",s=String(t.value??""),c=Number(i),a=Number(s),d=i!==""&&s!==""&&!isNaN(c)&&!isNaN(a);switch(t.operator){case "=":return i===s;case "<>":return i!==s;case ">":return d?c>a:i>s;case ">=":return d?c>=a:i>=s;case "<":return d?c<a:i<s;case "<=":return d?c<=a:i<=s;case "contains":return i.toLowerCase().includes(s.toLowerCase());case "notcontains":return !i.toLowerCase().includes(s.toLowerCase());case "startswith":return i.toLowerCase().startsWith(s.toLowerCase());case "endswith":return i.toLowerCase().endsWith(s.toLowerCase());case "isnull":return i===""||i==="(Blank)";case "isnotnull":return i!==""&&i!=="(Blank)";case "in":return Array.isArray(t.value)?t.value.map(String).includes(i):true;case "notin":return Array.isArray(t.value)?!t.value.map(String).includes(i):true;default:return true}}buildFromMask(t){this.beginBuild(),this.processRange(0,this.storedRowCount,t),this.finalizeBuild();}beginBuild(){let t=this.storedRowCount,e=this.storedColumns,n=this.rowFields.length,r=this.colFields.length,i=new Array(n);for(let u=0;u<n;u++)i[u]=e[this.rowFields[u].id].array;let s=new Array(r);for(let u=0;u<r;u++)s[u]=e[this.colFields[u].id].array;let c=this.dictionary.length,a=this.rowFields.map(u=>{let y=e[u.id];return y&&y.type==="int32"?new Uint8Array(c):null}),d=this.colFields.map(u=>{let y=e[u.id];return y&&y.type==="int32"?new Uint8Array(c):null});this.rowTrie=new nt(2e5),this.colTrie=new nt(2e5);let f=this.dataFields.length,w=this.dataFields.map(u=>u.summaryType||"sum"),g=w.some(u=>u==="count"||u==="avg"),R=w.some(u=>u==="min"),B=w.some(u=>u==="max"),C=w.some(u=>u==="distinct"),b=this.precisionScales(),M=new Array(f);for(let u=0;u<f;u++)M[u]=e[this.dataFields[u].id].array;let l=Math.min(t,2e5),h=R?new Float64Array(l*f):new Float64Array(0),I=B?new Float64Array(l*f):new Float64Array(0);this.leafGroupDistinctSets=C?new Map:new Map,R&&h.fill(1/0),B&&I.fill(-1/0),this.buildState={rPath:new Int32Array(n),cPath:new Int32Array(r),rowColArrays:i,colColArrays:s,dataColArrays:M,rowCardSeen:a,colCardSeen:d,rowCardCounts:new Int32Array(n),colCardCounts:new Int32Array(r),summaryTypes:w,allSums:w.every(u=>u==="sum"),scales:b,anyScaled:b.some(u=>u!==0),needsCounts:g,needsMins:R,needsMaxes:B,groupMap:new Map,leafRowIds:new Int32Array(l),leafColIds:new Int32Array(l),leafMeasures:new Float64Array(l*f),leafCounts:g?new Float64Array(l*f):new Float64Array(0),leafMins:h,leafMaxes:I,rowGroupOf:this.tracking||w.includes("custom")?new Int32Array(t).fill(-1):null,gCount:0};}ensureCardCapacity(){let t=this.buildState;if(!t)return;let e=this.dictionary.length,n=r=>{if(r===null||r.length>=e)return r;let i=new Uint8Array(Math.max(e,r.length*2));return i.set(r),i};for(let r=0;r<t.rowCardSeen.length;r++)t.rowCardSeen[r]=n(t.rowCardSeen[r]);for(let r=0;r<t.colCardSeen.length;r++)t.colCardSeen[r]=n(t.colCardSeen[r]);}processRange(t,e,n){let r=this.buildState,{rPath:i,cPath:s,rowColArrays:c,colColArrays:a,dataColArrays:d,rowCardSeen:f,colCardSeen:w,rowCardCounts:g,colCardCounts:R,summaryTypes:B,allSums:C,scales:b,anyScaled:M,needsCounts:l,needsMins:h,needsMaxes:I,groupMap:u,rowGroupOf:y}=r,P=c.length,D=a.length,S=d.length,U=this.rowTrie,et=this.colTrie,$=r.leafRowIds,q=r.leafColIds,X=r.leafMeasures,J=r.leafCounts,L=r.leafMins,O=r.leafMaxes,Q=r.gCount;for(let V=t;V<e;V++){if(n&&!n[V])continue;for(let p=0;p<P;p++){let E=c[p][V];i[p]=E;let F=f[p];F!==null&&F[E]===0&&(F[E]=1,g[p]++);}let it=U.insert(i,P);for(let p=0;p<D;p++){let E=a[p][V];s[p]=E;let F=w[p];F!==null&&F[E]===0&&(F[E]=1,R[p]++);}let st=et.insert(s,D),ot=it*67108864+st,N=u.get(ot);if(N===void 0){if(N=Q++,u.set(ot,N),N>=$.length){let p=N*2,E=new Int32Array(p);E.set($),$=E;let F=new Int32Array(p);F.set(q),q=F;let o=new Float64Array(p*S);if(o.set(X),X=o,l){let m=new Float64Array(p*S);m.set(J),J=m;}if(h){let m=new Float64Array(p*S);m.fill(1/0),m.set(L),L=m;}if(I){let m=new Float64Array(p*S);m.fill(-1/0),m.set(O),O=m;}}$[N]=it,q[N]=st;}y!==null&&(y[V]=N);let rt=N*S;if(C&&!M)for(let p=0;p<S;p++)X[rt+p]+=d[p][V];else for(let p=0;p<S;p++){let E=d[p][V],F=M&&b[p]!==0?dt(E,b[p]):E,o=rt+p;switch(B[p]){case "sum":X[o]+=F;break;case "count":J[o]+=1;break;case "avg":X[o]+=F,J[o]+=1;break;case "min":F<L[o]&&(L[o]=F);break;case "max":F>O[o]&&(O[o]=F);break;case "distinct":{if(F===0&&d[p]instanceof Int32Array)break;let m=this.leafGroupDistinctSets.get(o);m||(m=new Set,this.leafGroupDistinctSets.set(o,m)),m.add(F);break}default:X[o]+=F;break}}}r.leafRowIds=$,r.leafColIds=q,r.leafMeasures=X,r.leafCounts=J,r.leafMins=L,r.leafMaxes=O,r.gCount=Q;}finalizeBuild(){let t=this.buildState,{needsCounts:e,needsMins:n,needsMaxes:r,gCount:i}=t,s=t.dataColArrays.length,c=t.rowColArrays.length,a=t.colColArrays.length;this.groupCount=i,this.leafGroupRowIds=t.leafRowIds.slice(0,i),this.leafGroupColIds=t.leafColIds.slice(0,i),this.leafGroupMeasures=t.leafMeasures.slice(0,i*s),this.leafGroupCounts=e?t.leafCounts.slice(0,i*s):t.leafCounts,this.leafGroupMins=n?t.leafMins.slice(0,i*s):t.leafMins,this.leafGroupMaxes=r?t.leafMaxes.slice(0,i*s):t.leafMaxes,this.rowGroupOf=t.rowGroupOf,this.cardinalityMap={};for(let d=0;d<c;d++)t.rowCardSeen[d]!==null&&(this.cardinalityMap[this.rowFields[d].id]=t.rowCardCounts[d]);for(let d=0;d<a;d++)t.colCardSeen[d]!==null&&(this.cardinalityMap[this.colFields[d].id]=t.colCardCounts[d]);if(this.buildState=null,this.rowLive=this.colLive=null,this.tracking){this.groupMap=t.groupMap;let d=this.groupRows=new Int32Array(i),f=t.rowGroupOf;for(let w=0;w<this.storedRowCount;w++)f[w]>=0&&d[f[w]]++;}this.buildVersion++,this.isBuilt=true,this.rowPathStrs=this.buildPathStrs(this.rowTrie),this.colPathStrs=this.buildPathStrs(this.colTrie),this.rowIndexMapCache=this.buildIndexMap(this.rowPathStrs,null),this.columnIndexMapCache=this.buildIndexMap(this.colPathStrs,null);}buildPathStrs(t,e=[""]){let n=e;for(let r=e.length;r<t.size;r++){let i=t.nodeToParent[r],s=this.dictionary[t.nodeToVal[r]],c=n[i];n[r]=i===0?s===""?"|":s:c+"|"+s;}return n}buildIndexMap(t,e){let n={};for(let r=0;r<t.length;r++)(!e||e[r])&&(n[t[r]]=r);return n}applyTransaction(t){this.tracking||(this.tracking=true,this.buildFromMask(this.lastMask)),this.appendDictionary(t.dictionaryAppend);let{update:e,remove:n}=t,r=e.length,i=this.storedRowCount,s=i+t.addCount,c=this.storedColumns,a=new Set,d=false,f=this.rowGroupOf,w=l=>{let h=f[l];h<0||(f[l]=-1,--this.groupRows[h]===0&&(d=true),a.add(h));};e.forEach(w),n.forEach(w);for(let l in c){let h=c[l],I=t.columns[l];if(!I)throw new Error("transaction: no values for column "+l);let u=h.type==="int32"?new Int32Array(I):new Float64Array(I),y=h.array=H(h.array,s);for(let P=0;P<r;P++)y[e[P]]=u[P];y.set(u.subarray(r),i);}f=this.rowGroupOf=H(f,s),f.fill(-1,i,s);let g=this.lastMask&&(this.lastMask=H(this.lastMask,s));g&&(e.forEach(l=>this.computeMaskRange(l,l+1)),this.computeMaskRange(i,s));let R=this.rowFields.map(l=>c[l.id].array),B=this.colFields.map(l=>c[l.id].array),C=new Int32Array(R.length),b=new Int32Array(B.length),M=l=>{if(g&&!g[l])return;R.forEach((P,D)=>{C[D]=P[l];}),B.forEach((P,D)=>{b[D]=P[l];});let h=this.rowTrie.insert(C,C.length),I=this.colTrie.insert(b,b.length),u=h*67108864+I,y=this.groupMap.get(u);y===void 0&&(this.groupMap.set(u,y=this.groupCount++),(this.leafGroupRowIds=H(this.leafGroupRowIds,y+1))[y]=h,(this.leafGroupColIds=H(this.leafGroupColIds,y+1))[y]=I,this.groupRows=H(this.groupRows,y+1)),this.groupRows[y]++===0&&(d=true),f[l]=y,a.add(y);};e.forEach(M);for(let l=i;l<s;l++)M(l);if(this.storedRowCount=s-n.length,n.length>0){for(let l in c)pt(c[l].array,n,s);pt(f,n,s),g&&pt(g,n,s);}if(this.resumGroups(a),d){let l=0;for(let h=0;h<this.groupCount;h++)this.groupRows[h]===0&&l++;l>4096&&l>this.groupCount-l?this.buildFromMask(this.lastMask):(this.cardinalityMap={},[this.rowLive,this.rowPathStrs,this.rowIndexMapCache]=this.relive(this.rowTrie,this.leafGroupRowIds,this.rowFields,this.rowPathStrs),[this.colLive,this.colPathStrs,this.columnIndexMapCache]=this.relive(this.colTrie,this.leafGroupColIds,this.colFields,this.colPathStrs),this.buildVersion++);}return this.storedRowCount}precisionScales(){return Float64Array.from(this.dataFields,t=>ht(t))}resumGroups(t){if(t.size===0)return;let e=this.dataFields.length,n=this.dataFields.map(C=>C.summaryType||"sum"),r=this.dataFields.map(C=>this.storedColumns[C.id].array),i=this.precisionScales(),s=this.groupCount*e,c=this.leafGroupMeasures=H(this.leafGroupMeasures,s),a=n.some(C=>C==="count"||C==="avg")?this.leafGroupCounts=H(this.leafGroupCounts,s):null,d=n.includes("min")?this.leafGroupMins=H(this.leafGroupMins,s):null,f=n.includes("max")?this.leafGroupMaxes=H(this.leafGroupMaxes,s):null,w=this.leafGroupDistinctSets,g=new Uint8Array(this.groupCount),R=0;for(let C of t){g[C]=1,R+=this.groupRows[C];for(let b=C*e;b<(C+1)*e;b++)c[b]=0,a&&(a[b]=0),d&&(d[b]=1/0),f&&(f[b]=-1/0),w.delete(b);}let B=this.rowGroupOf;for(let C=0;R>0&&C<this.storedRowCount;C++){let b=B[C];if(!(b<0||!g[b])){R--;for(let M=0;M<e;M++){let l=i[M]!==0?dt(r[M][C],i[M]):r[M][C],h=b*e+M;switch(n[M]){case "count":a[h]++;break;case "avg":c[h]+=l,a[h]++;break;case "min":l<d[h]&&(d[h]=l);break;case "max":l>f[h]&&(f[h]=l);break;case "distinct":{if(l===0&&r[M]instanceof Int32Array)break;let I=w.get(h);I||w.set(h,I=new Set),I.add(l);break}default:c[h]+=l;}}}}}relive(t,e,n,r){let i=new Uint8Array(t.size);i[0]=1;for(let a=0;a<this.groupCount;a++)if(this.groupRows[a]!==0)for(let d=e[a];!i[d];d=t.nodeToParent[d])i[d]=1;let s=n.map(()=>new Set);t.nodeChildCount.fill(0,0,t.size);for(let a=1;a<t.size;a++)i[a]&&(t.nodeChildCount[t.nodeToParent[a]]++,s[t.nodeToDepth[a]-1]?.add(t.nodeToVal[a]));n.forEach((a,d)=>{this.cardinalityMap[a.id]=s[d].size;});let c=this.buildPathStrs(t,r);return [i,c,this.buildIndexMap(c,i)]}compute(t){this.isBuilt||this.buildFromMask(this.lastMask);let e=this.rowPathStrs,n=this.colPathStrs,r=new Uint8Array(this.rowTrie.size),i=new Set(t.expandedPaths),s=i.has(".*")||i.has("*");for(let o=0;o<this.rowTrie.size;o++)r[o]=ct(i,s,e[o])?1:0;let c=new Uint8Array(this.colTrie.size),a=new Set(t.expandedColPaths),d=a.has(".*")||a.has("*");for(let o=0;o<this.colTrie.size;o++)c[o]=ct(a,d,n[o])?1:0;let f=new Uint8Array(this.rowTrie.size);f[0]=1;for(let o=1;o<this.rowTrie.size;o++){let m=this.rowTrie.nodeToParent[o];(m===0||r[m])&&(!this.rowLive||this.rowLive[o])&&(f[o]=1);}let w=new Uint8Array(this.colTrie.size);w[0]=1;for(let o=1;o<this.colTrie.size;o++){let m=this.colTrie.nodeToParent[o];(m===0||c[m])&&(!this.colLive||this.colLive[o])&&(w[o]=1);}let g=this.dataFields.length,R=this.dataFields.map(o=>o.summaryType||"sum"),B=this.leafGroupMins.length>0,C=this.leafGroupMaxes.length>0,b=this.leafGroupCounts.length>0,M=this.leafGroupDistinctSets.size>0,l=new Map,h=0,I=1e4,u=new Float64Array(I),y=new Float64Array(I*g),P=new Int32Array(I*g),D=b?new Float64Array(I*g):null,S=B?new Float64Array(I*g):null,U=C?new Float64Array(I*g):null,et=M?new Map:new Map;S&&S.fill(1/0),U&&U.fill(-1/0);let $=new Int32Array(this.rowFields.length+1),q=new Int32Array(this.colFields.length+1),X=(o,m,A,x)=>{let Y=0,z=o;for(;z!==-1;)A[z]&&(x[Y++]=z),z=m.nodeToParent[z];return Y},J=this.rowGroupOf!==null&&R.includes("custom"),L=J?new Int32Array(this.groupCount+1):null,O=J?new Int32Array(Math.max(16,this.groupCount*4)):null,Q=0,V=this.tracking?this.groupRows:null;for(let o=0;o<this.groupCount;o++){if(V!==null&&V[o]===0){L!==null&&(L[o+1]=Q);continue}let m=this.leafGroupRowIds[o],A=this.leafGroupColIds[o],x=X(m,this.rowTrie,f,$),Y=X(A,this.colTrie,w,q);for(let z=0;z<x;z++){let at=$[z];for(let j=0;j<Y;j++){let lt=q[j],tt=at*67108864+lt,G=l.get(tt);if(G===void 0){if(G=h++,l.set(tt,G),G>=u.length){let k=G*2,T=new Float64Array(k);T.set(u),u=T;let _=new Float64Array(k*g);_.set(y),y=_;let K=new Int32Array(k*g);if(K.set(P),P=K,D){let W=new Float64Array(k*g);W.set(D),D=W;}if(S){let W=new Float64Array(k*g);W.fill(1/0),W.set(S),S=W;}if(U){let W=new Float64Array(k*g);W.fill(-1/0),W.set(U),U=W;}}u[G]=tt;}if(O!==null){if(Q>=O.length){let k=new Int32Array(O.length*2);k.set(O),O=k;}O[Q++]=G;}for(let k=0;k<g;k++){let T=G*g+k,_=o*g+k;switch(R[k]){case "sum":y[T]+=this.leafGroupMeasures[_];break;case "count":y[T]+=this.leafGroupCounts[_];break;case "avg":y[T]+=this.leafGroupMeasures[_],D[T]+=this.leafGroupCounts[_];break;case "min":{let K=this.leafGroupMins[_];K<S[T]&&(S[T]=K);break}case "max":{let K=this.leafGroupMaxes[_];K>U[T]&&(U[T]=K);break}case "distinct":{let K=this.leafGroupDistinctSets.get(_);if(K){let W=et.get(T);W||(W=new Set,et.set(T,W));for(let mt of K)W.add(mt);}break}default:y[T]+=this.leafGroupMeasures[_];break}P[T]+=1;}}}L!==null&&(L[o+1]=Q);}let it=this.precisionScales();for(let o=0;o<h;o++)for(let m=0;m<g;m++){let A=o*g+m,x=it[m];switch(R[m]){case "sum":x!==0&&(y[A]/=x);break;case "avg":y[A]=D[A]>0?y[A]/(x!==0?D[A]*x:D[A]):0;break;case "min":y[A]=S[A]!==1/0?x!==0?S[A]/x:S[A]:0;break;case "max":y[A]=U[A]!==-1/0?x!==0?U[A]/x:U[A]:0;break;case "distinct":{let Y=et.get(A);y[A]=Y?Y.size:0;break}}}let st=u.slice(0,h),ot=y.slice(0,h*g),N=P.slice(0,h*g),rt=(o,m,A,x,Y)=>{let z=[];for(let j=0;j<x.length;j++)z[j]=x[j]?.id||"";let at=j=>{let lt=o.childrenMap.get(j);if(!lt)return [];let tt=[];for(let G of lt.values())if(m[G]){let k=o.nodeToDepth[G]-1,T=A[G]===1;tt.push({key:Y[G],value:this.dictionary[o.nodeToVal[G]],fieldId:z[k]||"",children:T?at(G):[],isExpanded:T,isTotal:false,depth:k,hasChildren:o.nodeChildCount[G]>0});}return tt};return at(0)},p;if(this.rowGroupOf!==null&&L!==null&&O!==null){let o=this.storedRowCount,m={};for(let A=0;A<g;A++){if(R[A]!=="custom")continue;let x=this.dataFields[A].id;m[x]=this.storedColumns[x].array.slice(0,o);}p={rowGroupOf:this.rowGroupOf.slice(0,o),groupCellOffsets:L,groupCells:O.slice(0,Q),values:m};}let E=rt(this.rowTrie,f,r,this.rowFields,e),F=rt(this.colTrie,w,c,this.colFields,n);return {id:t.id,rowTree:E,colTree:F,cellMatrix:{keys:st,sums:ot,counts:N},customSummary:p,rowIndexMap:this.rowIndexMapCache,columnIndexMap:this.columnIndexMapCache,cardinalityMap:this.cardinalityMap,indexMapsVersion:this.buildVersion}}};var Z=new ut,yt=-1;self.onmessage=v=>{let t=v.data;try{if(t.action==="init")Z.init(t),self.postMessage({id:t.id,isInitComplete:!0});else if(t.action==="initStart")Z.initStart(t);else if(t.action==="initChunk")Z.initChunk(t);else if(t.action==="initEnd")Z.initEnd(),self.postMessage({id:t.id,isInitComplete:!0});else if(t.action==="filter")Z.applyFilter(t),self.postMessage({id:t.id,isFilterComplete:!0});else if(t.action==="reconfigure")Z.reconfigure(t),self.postMessage({id:t.id,isReconfigureComplete:!0});else if(t.action==="transaction")self.postMessage({id:t.id,isTransactionComplete:!0,rowCount:Z.applyTransaction(t)});else if(t.action==="compute"){let e=Z.compute(t),n=[e.cellMatrix.keys.buffer,e.cellMatrix.sums.buffer,e.cellMatrix.counts.buffer];if(e.indexMapsVersion!==void 0&&e.indexMapsVersion===yt){let r={...e,rowIndexMap:void 0,columnIndexMap:void 0,cardinalityMap:void 0,indexMapsOmitted:!0};postMessage(r,n);}else e.indexMapsVersion!==void 0&&(yt=e.indexMapsVersion),postMessage(e,n);}}catch(e){self.postMessage({id:t.id,error:e instanceof Error?e.message:String(e)});}};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kanunilabs/pivotgrid-core",
3
- "version": "1.11.9",
3
+ "version": "1.11.10",
4
4
  "description": "Framework-agnostic PivotGrid engine with Web Worker, columnar storage, trie-based grouping, and headless controller",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "author": "KanuniLabs <hello@kanunilabs.com> (https://www.kanunilabs.com)",