]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/js/src/visitor/set.ts
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / js / src / visitor / set.ts
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 import { Data } from '../data';
19 import { Field } from '../schema';
20 import { Vector } from '../vector';
21 import { Visitor } from '../visitor';
22 import { encodeUtf8 } from '../util/utf8';
23 import { VectorType } from '../interfaces';
24 import { float64ToUint16 } from '../util/math';
25 import { toArrayBufferView } from '../util/buffer';
26 import { Type, UnionMode, Precision, DateUnit, TimeUnit, IntervalUnit } from '../enum';
27 import {
28 DataType, Dictionary,
29 Bool, Null, Utf8, Binary, Decimal, FixedSizeBinary, List, FixedSizeList, Map_, Struct,
30 Float, Float16, Float32, Float64,
31 Int, Uint8, Uint16, Uint32, Uint64, Int8, Int16, Int32, Int64,
32 Date_, DateDay, DateMillisecond,
33 Interval, IntervalDayTime, IntervalYearMonth,
34 Time, TimeSecond, TimeMillisecond, TimeMicrosecond, TimeNanosecond,
35 Timestamp, TimestampSecond, TimestampMillisecond, TimestampMicrosecond, TimestampNanosecond,
36 Union, DenseUnion, SparseUnion,
37 } from '../type';
38
39 /** @ignore */
40 export interface SetVisitor extends Visitor {
41 visit<T extends VectorType>(node: T, index: number, value: T['TValue']): void;
42 visitMany<T extends VectorType>(nodes: T[], indices: number[], values: T['TValue'][]): void[];
43 getVisitFn<T extends Type>(node: T): (vector: VectorType<T>, index: number, value: VectorType<T>['TValue']) => void;
44 getVisitFn<T extends DataType>(node: VectorType<T> | Data<T> | T): (vector: VectorType<T>, index: number, value: VectorType<T>['TValue']) => void;
45 visitNull <T extends Null> (vector: VectorType<T>, index: number, value: T['TValue']): void;
46 visitBool <T extends Bool> (vector: VectorType<T>, index: number, value: T['TValue']): void;
47 visitInt <T extends Int> (vector: VectorType<T>, index: number, value: T['TValue']): void;
48 visitInt8 <T extends Int8> (vector: VectorType<T>, index: number, value: T['TValue']): void;
49 visitInt16 <T extends Int16> (vector: VectorType<T>, index: number, value: T['TValue']): void;
50 visitInt32 <T extends Int32> (vector: VectorType<T>, index: number, value: T['TValue']): void;
51 visitInt64 <T extends Int64> (vector: VectorType<T>, index: number, value: T['TValue']): void;
52 visitUint8 <T extends Uint8> (vector: VectorType<T>, index: number, value: T['TValue']): void;
53 visitUint16 <T extends Uint16> (vector: VectorType<T>, index: number, value: T['TValue']): void;
54 visitUint32 <T extends Uint32> (vector: VectorType<T>, index: number, value: T['TValue']): void;
55 visitUint64 <T extends Uint64> (vector: VectorType<T>, index: number, value: T['TValue']): void;
56 visitFloat <T extends Float> (vector: VectorType<T>, index: number, value: T['TValue']): void;
57 visitFloat16 <T extends Float16> (vector: VectorType<T>, index: number, value: T['TValue']): void;
58 visitFloat32 <T extends Float32> (vector: VectorType<T>, index: number, value: T['TValue']): void;
59 visitFloat64 <T extends Float64> (vector: VectorType<T>, index: number, value: T['TValue']): void;
60 visitUtf8 <T extends Utf8> (vector: VectorType<T>, index: number, value: T['TValue']): void;
61 visitBinary <T extends Binary> (vector: VectorType<T>, index: number, value: T['TValue']): void;
62 visitFixedSizeBinary <T extends FixedSizeBinary> (vector: VectorType<T>, index: number, value: T['TValue']): void;
63 visitDate <T extends Date_> (vector: VectorType<T>, index: number, value: T['TValue']): void;
64 visitDateDay <T extends DateDay> (vector: VectorType<T>, index: number, value: T['TValue']): void;
65 visitDateMillisecond <T extends DateMillisecond> (vector: VectorType<T>, index: number, value: T['TValue']): void;
66 visitTimestamp <T extends Timestamp> (vector: VectorType<T>, index: number, value: T['TValue']): void;
67 visitTimestampSecond <T extends TimestampSecond> (vector: VectorType<T>, index: number, value: T['TValue']): void;
68 visitTimestampMillisecond <T extends TimestampMillisecond>(vector: VectorType<T>, index: number, value: T['TValue']): void;
69 visitTimestampMicrosecond <T extends TimestampMicrosecond>(vector: VectorType<T>, index: number, value: T['TValue']): void;
70 visitTimestampNanosecond <T extends TimestampNanosecond> (vector: VectorType<T>, index: number, value: T['TValue']): void;
71 visitTime <T extends Time> (vector: VectorType<T>, index: number, value: T['TValue']): void;
72 visitTimeSecond <T extends TimeSecond> (vector: VectorType<T>, index: number, value: T['TValue']): void;
73 visitTimeMillisecond <T extends TimeMillisecond> (vector: VectorType<T>, index: number, value: T['TValue']): void;
74 visitTimeMicrosecond <T extends TimeMicrosecond> (vector: VectorType<T>, index: number, value: T['TValue']): void;
75 visitTimeNanosecond <T extends TimeNanosecond> (vector: VectorType<T>, index: number, value: T['TValue']): void;
76 visitDecimal <T extends Decimal> (vector: VectorType<T>, index: number, value: T['TValue']): void;
77 visitList <T extends List> (vector: VectorType<T>, index: number, value: T['TValue']): void;
78 visitStruct <T extends Struct> (vector: VectorType<T>, index: number, value: T['TValue']): void;
79 visitUnion <T extends Union> (vector: VectorType<T>, index: number, value: T['TValue']): void;
80 visitDenseUnion <T extends DenseUnion> (vector: VectorType<T>, index: number, value: T['TValue']): void;
81 visitSparseUnion <T extends SparseUnion> (vector: VectorType<T>, index: number, value: T['TValue']): void;
82 visitDictionary <T extends Dictionary> (vector: VectorType<T>, index: number, value: T['TValue']): void;
83 visitInterval <T extends Interval> (vector: VectorType<T>, index: number, value: T['TValue']): void;
84 visitIntervalDayTime <T extends IntervalDayTime> (vector: VectorType<T>, index: number, value: T['TValue']): void;
85 visitIntervalYearMonth <T extends IntervalYearMonth> (vector: VectorType<T>, index: number, value: T['TValue']): void;
86 visitFixedSizeList <T extends FixedSizeList> (vector: VectorType<T>, index: number, value: T['TValue']): void;
87 visitMap <T extends Map_> (vector: VectorType<T>, index: number, value: T['TValue']): void;
88 }
89
90 /** @ignore */
91 export class SetVisitor extends Visitor {}
92
93 /** @ignore */
94 const setEpochMsToDays = (data: Int32Array, index: number, epochMs: number) => { data[index] = (epochMs / 86400000) | 0; };
95 /** @ignore */
96 const setEpochMsToMillisecondsLong = (data: Int32Array, index: number, epochMs: number) => {
97 data[index] = (epochMs % 4294967296) | 0;
98 data[index + 1] = (epochMs / 4294967296) | 0;
99 };
100 /** @ignore */
101 const setEpochMsToMicrosecondsLong = (data: Int32Array, index: number, epochMs: number) => {
102 data[index] = ((epochMs * 1000) % 4294967296) | 0;
103 data[index + 1] = ((epochMs * 1000) / 4294967296) | 0;
104 };
105 /** @ignore */
106 const setEpochMsToNanosecondsLong = (data: Int32Array, index: number, epochMs: number) => {
107 data[index] = ((epochMs * 1000000) % 4294967296) | 0;
108 data[index + 1] = ((epochMs * 1000000) / 4294967296) | 0;
109 };
110
111 /** @ignore */
112 const setVariableWidthBytes = (values: Uint8Array, valueOffsets: Int32Array, index: number, value: Uint8Array) => {
113 const { [index]: x, [index + 1]: y } = valueOffsets;
114 if (x != null && y != null) {
115 values.set(value.subarray(0, y - x), x);
116 }
117 };
118
119 /** @ignore */
120 const setBool = <T extends Bool>({ offset, values }: VectorType<T>, index: number, val: boolean) => {
121 const idx = offset + index;
122 val ? (values[idx >> 3] |= (1 << (idx % 8))) // true
123 : (values[idx >> 3] &= ~(1 << (idx % 8))); // false
124
125 };
126
127 /** @ignore */ type Numeric1X = Int8 | Int16 | Int32 | Uint8 | Uint16 | Uint32 | Float32 | Float64;
128 /** @ignore */ type Numeric2X = Int64 | Uint64;
129
130 /** @ignore */
131 const setDateDay = <T extends DateDay> ({ values }: VectorType<T>, index: number, value: T['TValue']): void => { setEpochMsToDays(values, index, value.valueOf()); };
132 /** @ignore */
133 const setDateMillisecond = <T extends DateMillisecond>({ values }: VectorType<T>, index: number, value: T['TValue']): void => { setEpochMsToMillisecondsLong(values, index * 2, value.valueOf()); };
134 /** @ignore */
135 const setNumeric = <T extends Numeric1X> ({ stride, values }: VectorType<T>, index: number, value: T['TValue']): void => { values[stride * index] = value; };
136 /** @ignore */
137 const setFloat16 = <T extends Float16> ({ stride, values }: VectorType<T>, index: number, value: T['TValue']): void => { values[stride * index] = float64ToUint16(value); };
138 /** @ignore */
139 const setNumericX2 = <T extends Numeric2X> (vector: VectorType<T>, index: number, value: T['TValue']): void => {
140 switch (typeof value) {
141 case 'bigint': vector.values64[index] = value; break;
142 case 'number': vector.values[index * vector.stride] = value; break;
143 default: {
144 const val = value as T['TArray'];
145 const { stride, ArrayType } = vector;
146 const long = toArrayBufferView<T['TArray']>(ArrayType, val);
147 vector.values.set(long.subarray(0, stride), stride * index);
148 }
149 }
150 };
151 /** @ignore */
152 const setFixedSizeBinary = <T extends FixedSizeBinary>({ stride, values }: VectorType<T>, index: number, value: T['TValue']): void => { values.set(value.subarray(0, stride), stride * index); };
153
154 /** @ignore */
155 const setBinary = <T extends Binary>({ values, valueOffsets }: VectorType<T>, index: number, value: T['TValue']) => setVariableWidthBytes(values, valueOffsets, index, value);
156 /** @ignore */
157 const setUtf8 = <T extends Utf8>({ values, valueOffsets }: VectorType<T>, index: number, value: T['TValue']) => {
158 setVariableWidthBytes(values, valueOffsets, index, encodeUtf8(value));
159 };
160
161 /* istanbul ignore next */
162 /** @ignore */
163 const setInt = <T extends Int>(vector: VectorType<T>, index: number, value: T['TValue']): void => {
164 vector.type.bitWidth < 64
165 ? setNumeric(vector as VectorType<Numeric1X>, index, value as Numeric1X['TValue'])
166 : setNumericX2(vector as VectorType<Numeric2X>, index, value as Numeric2X['TValue']);
167 };
168
169 /* istanbul ignore next */
170 /** @ignore */
171 const setFloat = <T extends Float>(vector: VectorType<T>, index: number, value: T['TValue']): void => {
172 vector.type.precision !== Precision.HALF
173 ? setNumeric(vector as VectorType<Numeric1X>, index, value)
174 : setFloat16(vector as VectorType<Float16>, index, value);
175 };
176
177 /* istanbul ignore next */
178 const setDate = <T extends Date_> (vector: VectorType<T>, index: number, value: T['TValue']): void => {
179 vector.type.unit === DateUnit.DAY
180 ? setDateDay(vector as VectorType<DateDay>, index, value)
181 : setDateMillisecond(vector as VectorType<DateMillisecond>, index, value);
182 };
183
184 /** @ignore */
185 const setTimestampSecond = <T extends TimestampSecond> ({ values }: VectorType<T>, index: number, value: T['TValue']): void => setEpochMsToMillisecondsLong(values, index * 2, value / 1000);
186 /** @ignore */
187 const setTimestampMillisecond = <T extends TimestampMillisecond>({ values }: VectorType<T>, index: number, value: T['TValue']): void => setEpochMsToMillisecondsLong(values, index * 2, value);
188 /** @ignore */
189 const setTimestampMicrosecond = <T extends TimestampMicrosecond>({ values }: VectorType<T>, index: number, value: T['TValue']): void => setEpochMsToMicrosecondsLong(values, index * 2, value);
190 /** @ignore */
191 const setTimestampNanosecond = <T extends TimestampNanosecond> ({ values }: VectorType<T>, index: number, value: T['TValue']): void => setEpochMsToNanosecondsLong(values, index * 2, value);
192 /* istanbul ignore next */
193 /** @ignore */
194 const setTimestamp = <T extends Timestamp>(vector: VectorType<T>, index: number, value: T['TValue']): void => {
195 switch (vector.type.unit) {
196 case TimeUnit.SECOND: return setTimestampSecond(vector as VectorType<TimestampSecond>, index, value);
197 case TimeUnit.MILLISECOND: return setTimestampMillisecond(vector as VectorType<TimestampMillisecond>, index, value);
198 case TimeUnit.MICROSECOND: return setTimestampMicrosecond(vector as VectorType<TimestampMicrosecond>, index, value);
199 case TimeUnit.NANOSECOND: return setTimestampNanosecond(vector as VectorType<TimestampNanosecond>, index, value);
200 }
201 };
202
203 /** @ignore */
204 const setTimeSecond = <T extends TimeSecond> ({ values, stride }: VectorType<T>, index: number, value: T['TValue']): void => { values[stride * index] = value; };
205 /** @ignore */
206 const setTimeMillisecond = <T extends TimeMillisecond>({ values, stride }: VectorType<T>, index: number, value: T['TValue']): void => { values[stride * index] = value; };
207 /** @ignore */
208 const setTimeMicrosecond = <T extends TimeMicrosecond>({ values }: VectorType<T>, index: number, value: T['TValue']): void => { values.set(value.subarray(0, 2), 2 * index); };
209 /** @ignore */
210 const setTimeNanosecond = <T extends TimeNanosecond> ({ values }: VectorType<T>, index: number, value: T['TValue']): void => { values.set(value.subarray(0, 2), 2 * index); };
211 /* istanbul ignore next */
212 /** @ignore */
213 const setTime = <T extends Time>(vector: VectorType<T>, index: number, value: T['TValue']): void => {
214 switch (vector.type.unit) {
215 case TimeUnit.SECOND: return setTimeSecond(vector as VectorType<TimeSecond>, index, value as TimeSecond['TValue']);
216 case TimeUnit.MILLISECOND: return setTimeMillisecond(vector as VectorType<TimeMillisecond>, index, value as TimeMillisecond['TValue']);
217 case TimeUnit.MICROSECOND: return setTimeMicrosecond(vector as VectorType<TimeMicrosecond>, index, value as TimeMicrosecond['TValue']);
218 case TimeUnit.NANOSECOND: return setTimeNanosecond(vector as VectorType<TimeNanosecond>, index, value as TimeNanosecond['TValue']);
219 }
220 };
221
222 /** @ignore */
223 const setDecimal = <T extends Decimal>({ values }: VectorType<T>, index: number, value: T['TValue']): void => { values.set(value.subarray(0, 4), 4 * index); };
224
225 /** @ignore */
226 const setList = <T extends List>(vector: VectorType<T>, index: number, value: T['TValue']): void => {
227 const values = vector.getChildAt(0)!, valueOffsets = vector.valueOffsets;
228 for (let idx = -1, itr = valueOffsets[index], end = valueOffsets[index + 1]; itr < end;) {
229 values.set(itr++, value.get(++idx));
230 }
231 };
232
233 /** @ignore */
234 const setMap = <T extends Map_>(vector: VectorType<T>, index: number, value: T['TValue']) => {
235 const values = vector.getChildAt(0)!, valueOffsets = vector.valueOffsets;
236 const entries = value instanceof Map ? [...value] : Object.entries(value);
237 for (let idx = -1, itr = valueOffsets[index], end = valueOffsets[index + 1]; itr < end;) {
238 values.set(itr++, entries[++idx]);
239 }
240 };
241
242 /** @ignore */ const _setStructArrayValue = (o: number, v: any[]) => (c: Vector | null, _: Field, i: number) => c?.set(o, v[i]);
243 /** @ignore */ const _setStructVectorValue = (o: number, v: Vector) => (c: Vector | null, _: Field, i: number) => c?.set(o, v.get(i));
244 /** @ignore */ const _setStructMapValue = (o: number, v: Map<string, any>) => (c: Vector | null, f: Field, _: number) => c?.set(o, v.get(f.name));
245 /** @ignore */ const _setStructObjectValue = (o: number, v: { [key: string]: any }) => (c: Vector | null, f: Field, _: number) => c?.set(o, v[f.name]);
246 /** @ignore */
247 const setStruct = <T extends Struct>(vector: VectorType<T>, index: number, value: T['TValue']) => {
248
249 const setValue = value instanceof Map ? _setStructMapValue(index, value) :
250 value instanceof Vector ? _setStructVectorValue(index, value) :
251 Array.isArray(value) ? _setStructArrayValue(index, value) :
252 _setStructObjectValue(index, value) ;
253
254 vector.type.children.forEach((f: Field, i: number) => setValue(vector.getChildAt(i), f, i));
255 };
256
257 /* istanbul ignore next */
258 /** @ignore */
259 const setUnion = <
260 V extends VectorType<Union> | VectorType<DenseUnion> | VectorType<SparseUnion>
261 >(vector: V, index: number, value: V['TValue']) => {
262 vector.type.mode === UnionMode.Dense ?
263 setDenseUnion(vector as VectorType<DenseUnion>, index, value) :
264 setSparseUnion(vector as VectorType<SparseUnion>, index, value);
265 };
266
267 /** @ignore */
268 const setDenseUnion = <T extends DenseUnion>(vector: VectorType<T>, index: number, value: T['TValue']): void => {
269 const childIndex = vector.typeIdToChildIndex[vector.typeIds[index]];
270 const child = vector.getChildAt(childIndex);
271 child && child.set(vector.valueOffsets[index], value);
272 };
273
274 /** @ignore */
275 const setSparseUnion = <T extends SparseUnion>(vector: VectorType<T>, index: number, value: T['TValue']): void => {
276 const childIndex = vector.typeIdToChildIndex[vector.typeIds[index]];
277 const child = vector.getChildAt(childIndex);
278 child && child.set(index, value);
279 };
280
281 /** @ignore */
282 const setDictionary = <T extends Dictionary>(vector: VectorType<T>, index: number, value: T['TValue']): void => {
283 const key = vector.getKey(index);
284 if (key !== null) {
285 vector.setValue(key, value);
286 }
287 };
288
289 /* istanbul ignore next */
290 /** @ignore */
291 const setIntervalValue = <T extends Interval>(vector: VectorType<T>, index: number, value: T['TValue']): void => {
292 (vector.type.unit === IntervalUnit.DAY_TIME)
293 ? setIntervalDayTime(vector as VectorType<IntervalDayTime>, index, value)
294 : setIntervalYearMonth(vector as VectorType<IntervalYearMonth>, index, value);
295 };
296
297 /** @ignore */
298 const setIntervalDayTime = <T extends IntervalDayTime>({ values }: VectorType<T>, index: number, value: T['TValue']): void => { values.set(value.subarray(0, 2), 2 * index); };
299 /** @ignore */
300 const setIntervalYearMonth = <T extends IntervalYearMonth>({ values }: VectorType<T>, index: number, value: T['TValue']): void => { values[index] = (value[0] * 12) + (value[1] % 12); };
301
302 /** @ignore */
303 const setFixedSizeList = <T extends FixedSizeList>(vector: VectorType<T>, index: number, value: T['TValue']): void => {
304 const child = vector.getChildAt(0)!, { stride } = vector;
305 for (let idx = -1, offset = index * stride; ++idx < stride;) {
306 child.set(offset + idx, value.get(idx));
307 }
308 };
309
310 SetVisitor.prototype.visitBool = setBool;
311 SetVisitor.prototype.visitInt = setInt;
312 SetVisitor.prototype.visitInt8 = setNumeric;
313 SetVisitor.prototype.visitInt16 = setNumeric;
314 SetVisitor.prototype.visitInt32 = setNumeric;
315 SetVisitor.prototype.visitInt64 = setNumericX2;
316 SetVisitor.prototype.visitUint8 = setNumeric;
317 SetVisitor.prototype.visitUint16 = setNumeric;
318 SetVisitor.prototype.visitUint32 = setNumeric;
319 SetVisitor.prototype.visitUint64 = setNumericX2;
320 SetVisitor.prototype.visitFloat = setFloat;
321 SetVisitor.prototype.visitFloat16 = setFloat16;
322 SetVisitor.prototype.visitFloat32 = setNumeric;
323 SetVisitor.prototype.visitFloat64 = setNumeric;
324 SetVisitor.prototype.visitUtf8 = setUtf8;
325 SetVisitor.prototype.visitBinary = setBinary;
326 SetVisitor.prototype.visitFixedSizeBinary = setFixedSizeBinary;
327 SetVisitor.prototype.visitDate = setDate;
328 SetVisitor.prototype.visitDateDay = setDateDay;
329 SetVisitor.prototype.visitDateMillisecond = setDateMillisecond;
330 SetVisitor.prototype.visitTimestamp = setTimestamp;
331 SetVisitor.prototype.visitTimestampSecond = setTimestampSecond;
332 SetVisitor.prototype.visitTimestampMillisecond = setTimestampMillisecond;
333 SetVisitor.prototype.visitTimestampMicrosecond = setTimestampMicrosecond;
334 SetVisitor.prototype.visitTimestampNanosecond = setTimestampNanosecond;
335 SetVisitor.prototype.visitTime = setTime;
336 SetVisitor.prototype.visitTimeSecond = setTimeSecond;
337 SetVisitor.prototype.visitTimeMillisecond = setTimeMillisecond;
338 SetVisitor.prototype.visitTimeMicrosecond = setTimeMicrosecond;
339 SetVisitor.prototype.visitTimeNanosecond = setTimeNanosecond;
340 SetVisitor.prototype.visitDecimal = setDecimal;
341 SetVisitor.prototype.visitList = setList;
342 SetVisitor.prototype.visitStruct = setStruct;
343 SetVisitor.prototype.visitUnion = setUnion;
344 SetVisitor.prototype.visitDenseUnion = setDenseUnion;
345 SetVisitor.prototype.visitSparseUnion = setSparseUnion;
346 SetVisitor.prototype.visitDictionary = setDictionary;
347 SetVisitor.prototype.visitInterval = setIntervalValue;
348 SetVisitor.prototype.visitIntervalDayTime = setIntervalDayTime;
349 SetVisitor.prototype.visitIntervalYearMonth = setIntervalYearMonth;
350 SetVisitor.prototype.visitFixedSizeList = setFixedSizeList;
351 SetVisitor.prototype.visitMap = setMap;
352
353 /** @ignore */
354 export const instance = new SetVisitor();