UNPKG

arquero-arrow

Version:

Arrow serialization support for Arquero.

3 lines (2 loc) 8.15 kB
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("apache-arrow")):"function"==typeof define&&define.amd?define(["exports","apache-arrow"],e):e(((n="undefined"!=typeof globalThis?globalThis:n||self).aq=n.aq||{},n.aq.arrow={}),n.Arrow)}(this,(function(n,e){"use strict";function t(n,e=1){return(n*e+63&-64||64)/e}function r(n,e,r=n.BYTES_PER_ELEMENT){return new n(t(e,r))}function a(n){return n instanceof e.Data?n:e.Data.new(n.type,0,n.length,n.nulls,n.buffers,null,n.dict)}const i=new TextEncoder;const o=i.encodeInto?function(n,e,t){return i.encodeInto(t,n.subarray(e)).written}:function(n,e,t){const r=i.encode(t);return n.set(r,e),r.length};function s(n,e){const t=r(n.ArrayType,e);return{set(n,e){t[e]=n},data:()=>({type:n,length:e,buffers:[null,t]})}}function u(n,e){const t=r(n.ArrayType,e/8);return{set(n,e){n&&(t[e>>3]|=1<<e%8)},data:()=>({type:n,length:e,buffers:[null,t]})}}function c(n,e){const t=r(n.ArrayType,e);return{set(n,e){t[e]=n/864e5|0},data:()=>({type:n,length:e,buffers:[null,t]})}}function l(n,e){const t=r(n.ArrayType,e);return{set(n,e){const r=e<<1;t[r]=n%4294967296|0,t[r+1]=n/4294967296|0},data:()=>({type:n,length:e,buffers:[null,t]})}}function y(n,e){const t=[],a=r(n.indices.ArrayType,e),i=Object.create(null);let o=-1,s=0;return{set(n,e){const r=String(n);let u=i[r];void 0===u&&(s+=r.length,i[r]=u=++o,t.push(r)),a[e]=u},data:()=>({type:n,length:e,buffers:[null,a],dict:f(n.dictionary,t,s)})}}function f(n,i,s){const u=function(n,e,a){const i=r(Int32Array,e+1),s=r(Uint8Array,3*a);let u=0;return{set(n,e){u+=o(s,u,n),i[e+1]=u},data:()=>{const r=t(u),a=s.length>r?s.subarray(0,r):s;return{type:n,length:e,buffers:[i,a]}}}}(n,i.length,s);return i.forEach(u.set),c=u.data(),e.Vector.new(a(c));var c}function p(n,t,a=!0){let i;switch(n.typeId){case e.Type.Int:i=n.bitWidth<64?s:null;break;case e.Type.Float:i=n.precision>0?s:null;break;case e.Type.Dictionary:i=n.dictionary.typeId===e.Type.Utf8&&n.indices.typeId===e.Type.Int&&n.indices.bitWidth<64?y:null;break;case e.Type.Bool:i=u;break;case e.Type.Date:i=n.unit?l:c}return null==i?function(n){const t=e.Builder.new({type:n,nullValues:[null,void 0],highWaterMark:1/0});return{set(n,e){t.set(e,n)},data:()=>t.finish().flush()}}(n):a?function(n,e){const t=r(Uint8Array,e/8);let a=0;return{set(e,r){null==e?++a:(n.set(e,r),t[r>>3]|=1<<r%8)},data:()=>{const e=n.data();return a&&(e.nulls=a,e.buffers[2]=t),e}}}(i(n,t),t):i(n,t)}function d(n,e,t,r,i=!0){const o=p(r,n,i);return e(t,o.set),a(o.data())}var T=Array.isArray;function m(n){return n&&n.map===Int8Array.prototype.map}function w(n){return T(n)||m(n)}function h(n,e,t){const r=0===t&&n.numRows()<=e&&!n.isFiltered()&&!n.isOrdered();return(a,i)=>{let o=-1;r&&w(a.data)?a.data.forEach(i):n.scan((n=>i(a.get(n),++o)),!0,e,t)}}function g(n){throw Error(n)}function b(n){if(n instanceof e.DataType||null==n)return n;switch(n){case e.Type.Binary:return new e.Binary;case e.Type.Bool:return new e.Bool;case e.Type.DateDay:return new e.DateDay;case e.Type.DateMillisecond:case e.Type.Date:return new e.DateMillisecond;case e.Type.Dictionary:return new e.Dictionary(new e.Utf8,new e.Int32);case e.Type.Float16:return new e.Float16;case e.Type.Float32:return new e.Float32;case e.Type.Float64:case e.Type.Float:return new e.Float64;case e.Type.Int8:return new e.Int8;case e.Type.Int16:return new e.Int16;case e.Type.Int32:case e.Type.Int:return new e.Int32;case e.Type.Int64:return new e.Int64;case e.Type.IntervalDayTime:return new e.IntervalDayTime;case e.Type.Interval:case e.Type.IntervalYearMonth:return new e.IntervalYearMonth;case e.Type.Null:return new e.Null;case e.Type.TimeMicrosecond:return new e.TimeMicrosecond;case e.Type.TimeMillisecond:case e.Type.Time:return new e.TimeMillisecond;case e.Type.TimeNanosecond:return new e.TimeNanosecond;case e.Type.TimeSecond:return new e.TimeSecond;case e.Type.Uint8:return new e.Uint8;case e.Type.Uint16:return new e.Uint16;case e.Type.Uint32:return new e.Uint32;case e.Type.Uint64:return new e.Uint64;case e.Type.Utf8:return new e.Utf8;default:g(`Unsupported type code: ${t=n,void 0===t?t+"":"bigint"==typeof t?t+"n":JSON.stringify(t)}. Use a data type constructor instead?`)}var t}function I(n,e){const t=U();return n(e,t.add),t}function U(){const n={count:0,nulls:0,bools:0,nums:0,ints:0,bigints:0,min:1/0,max:-1/0,digits:0,dates:0,utcdays:0,strings:0,strlen:0,arrays:0,minlen:1/0,maxlen:0,structs:0,add(e){if(++n.count,null==e)return void++n.nulls;const t=typeof e;if("string"===t)++n.strings;else if("number"===t)++n.nums,e<n.min&&(n.min=e),e>n.max&&(n.max=e),Number.isInteger(e)&&++n.ints;else if("boolean"===t)++n.bools;else if("object"===t)if(function(n){return n instanceof Date}(e))++n.dates,0===(r=e).getUTCHours()&&0===r.getUTCMinutes()&&0===r.getUTCSeconds()&&0===r.getUTCMilliseconds()&&++n.utcdays;else if(w(e)){++n.arrays,e.length<n.minlen&&(n.minlen=e.length),e.length>n.maxlen&&(n.maxlen=e.length);const t=n.array_prof||(n.array_prof=U());e.forEach(t.add)}else{++n.structs;const t=n.struct_prof||(n.struct_prof={});for(const n in e){(t[n]||(t[n]=U())).add(e[n])}}else"bigint"===t&&(++n.bigints,e<n.min&&(n.min=e),e>n.max&&(n.max=e));var r},type:()=>b(function(n){const t=n.count-n.nulls;if(0===t)return e.Type.Null;if(n.ints===t){const t=Math.max(Math.abs(n.min)-1,n.max);return n.min<0?t>=2**31?e.Type.Float64:t<128?e.Type.Int8:t<32768?e.Type.Int16:e.Type.Int32:t>=2**32?e.Type.Float64:t<256?e.Type.Uint8:t<65536?e.Type.Uint16:e.Type.Uint32}if(n.nums===t)return e.Type.Float64;if(n.bigints===t){const t=-n.min>n.max?-n.min-1n:n.max;return n.min<0?t<2**63?e.Type.Int64:g(`BigInt exceeds 64 bits: ${t}`):n.max<2**64?e.Type.Uint64:g(`BigInt exceeds 64 bits: ${n.max}`)}if(n.bools===t)return e.Type.Bool;if(n.utcdays===t)return e.Type.DateDay;if(n.dates===t)return e.Type.DateMillisecond;if(n.arrays===t){const t=e.Field.new("value",n.array_prof.type(),!0);return n.minlen===n.maxlen?new e.FixedSizeList(n.minlen,t):new e.List(t)}if(n.structs===t){const t=n.struct_prof;return new e.Struct(Object.keys(t).map((n=>e.Field.new(n,t[n].type(),!0))))}if(n.strings>0)return e.Type.Dictionary;g("Type inference failure")}(n))};return n}function A(n,t,r,a,i,o=!0){if(!(i=b(i))){const n=I(a,t);o=n.nulls>0,i=n.type()}return e.Column.new(t,d(r,a,t,i,o))}function F(n,r,i,o,s,u=!0){s=b(s);const c=n.column(r),l=!(n.isFiltered()||n.isOrdered()),y=(f=c)instanceof e.Vector?f:f.vector instanceof e.Vector?f.vector:null;var f;if(y&&l&&D(y.type,s))return e.Column.new(r,y.chunks||y);const p=c.data;if(m(p)){const n=function(n){const e=x[n.constructor.name];return e?new e:null}(p);if(l&&n&&D(n,s))return e.Column.new(r,function(n,e){const r=n.length,i=t(r,n.BYTES_PER_ELEMENT);let o=n;return r!==i&&(o=new n.constructor(i),o.set(n)),a({type:e,length:r,buffers:[null,o]})}(p,n));s=s||n,u=!1}if(!s){const n=I(o,c);u=n.nulls>0,s=n.type()}return e.Column.new(r,d(i,o,c,s,u))}const x={Float32Array:e.Float32,Float64Array:e.Float64,Int8Array:e.Int8,Int16Array:e.Int16,Int32Array:e.Int32,Uint8Array:e.Uint8,Uint16Array:e.Uint16,Uint32Array:e.Uint32,BigInt64Array:e.Int64,BigUint64Array:e.Uint64};function D(n,e){return!n||!e||n.compareTo(e)}function M(n){return"function"==typeof n}function v(n,e,t){const r=Math.min(n.length,t+e);return(e,a)=>{for(let i=t;i<r;++i)a(n[i][e],i)}}function E(n,t={}){const r=t.types||{},{columnFrom:a,names:i,nrows:o,scan:s}=function(n,e){const{columns:t,limit:r=1/0,offset:a=0}=e,i=M(t)?t(n):T(t)?t:null;if(T(n))return{columnFrom:A,names:i||Object.keys(n[0]),nrows:Math.min(r,n.length-a),scan:v(n,r,a)};if(function(n){return n&&M(n.reify)}(n))return{columnFrom:F,names:i||n.columnNames(),nrows:Math.min(r,n.numRows()-a),scan:h(n,r,a)};g("Unsupported input data type")}(n,t);return e.Table.new(i.map((e=>{const t=a(n,e,o,s,r[e]);return t.length===o?t:g("Column length mismatch")})))}const B={tableMethods:{toArrow:E}};Object.defineProperty(n,"Type",{enumerable:!0,get:function(){return e.Type}}),n.arquero_package=B,n.dataFromTable=function(n,e,t,r){return d(n.numRows(),h(n,1/0,0),e,t,r)},n.profiler=U,n.toArrow=E,Object.defineProperty(n,"__esModule",{value:!0})})); //# sourceMappingURL=arquero-arrow.min.js.map