]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/js/src/vector/index.ts
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / js / src / vector / index.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 export { Vector } from '../vector';
19 export { BaseVector } from './base';
20 export { BinaryVector } from './binary';
21 export { BoolVector } from './bool';
22 export { Chunked } from './chunked';
23 export { DateVector, DateDayVector, DateMillisecondVector } from './date';
24 export { DecimalVector } from './decimal';
25 export { DictionaryVector } from './dictionary';
26 export { FixedSizeBinaryVector } from './fixedsizebinary';
27 export { FixedSizeListVector } from './fixedsizelist';
28 export { FloatVector, Float16Vector, Float32Vector, Float64Vector } from './float';
29 export { IntervalVector, IntervalDayTimeVector, IntervalYearMonthVector } from './interval';
30 export { IntVector, Int8Vector, Int16Vector, Int32Vector, Int64Vector, Uint8Vector, Uint16Vector, Uint32Vector, Uint64Vector } from './int';
31 export { ListVector } from './list';
32 export { MapVector } from './map';
33 export { NullVector } from './null';
34 export { StructVector } from './struct';
35 export { TimestampVector, TimestampSecondVector, TimestampMillisecondVector, TimestampMicrosecondVector, TimestampNanosecondVector } from './timestamp';
36 export { TimeVector, TimeSecondVector, TimeMillisecondVector, TimeMicrosecondVector, TimeNanosecondVector } from './time';
37 export { UnionVector, DenseUnionVector, SparseUnionVector } from './union';
38 export { Utf8Vector } from './utf8';
39 export { MapRow, StructRow } from './row';
40
41 import * as fn from '../util/fn';
42 import { Data } from '../data';
43 import { Type } from '../enum';
44 import { Vector } from '../vector';
45 import { DataType } from '../type';
46 import { Chunked } from './chunked';
47 import { BaseVector } from './base';
48 import { setBool } from '../util/bit';
49 import { isIterable, isAsyncIterable } from '../util/compat';
50 import { Builder, IterableBuilderOptions } from '../builder';
51 import { VectorType as V, VectorCtorArgs } from '../interfaces';
52 import { instance as getVisitor } from '../visitor/get';
53 import { instance as setVisitor } from '../visitor/set';
54 import { instance as indexOfVisitor } from '../visitor/indexof';
55 import { instance as toArrayVisitor } from '../visitor/toarray';
56 import { instance as iteratorVisitor } from '../visitor/iterator';
57 import { instance as byteWidthVisitor } from '../visitor/bytewidth';
58 import { instance as getVectorConstructor } from '../visitor/vectorctor';
59
60 declare module '../vector' {
61 namespace Vector {
62 export { newVector as new };
63 export { vectorFrom as from };
64 }
65 }
66
67 declare module './base' {
68 namespace BaseVector {
69 export { vectorFrom as from };
70 }
71 interface BaseVector<T extends DataType> {
72 get(index: number): T['TValue'] | null;
73 set(index: number, value: T['TValue'] | null): void;
74 indexOf(value: T['TValue'] | null, fromIndex?: number): number;
75 toArray(): T['TArray'];
76 getByteWidth(): number;
77 [Symbol.iterator](): IterableIterator<T['TValue'] | null>;
78 }
79 }
80
81 /** @nocollapse */
82 Vector.new = newVector;
83
84 /** @nocollapse */
85 Vector.from = vectorFrom;
86
87 /** @ignore */
88 function newVector<T extends DataType>(data: Data<T>, ...args: VectorCtorArgs<V<T>>): V<T> {
89 return new (getVectorConstructor.getVisitFn<T>(data)())(data, ...args) as V<T>;
90 }
91
92 /** @ignore */
93 export interface VectorBuilderOptions<T extends DataType, TNull = any> extends IterableBuilderOptions<T, TNull> { values: Iterable<T['TValue'] | TNull> }
94 /** @ignore */
95 export interface VectorBuilderOptionsAsync<T extends DataType, TNull = any> extends IterableBuilderOptions<T, TNull> { values: AsyncIterable<T['TValue'] | TNull> }
96
97 /** @ignore */
98 export function vectorFromValuesWithType<T extends DataType, TNull = any>(newDataType: () => T, input: Iterable<T['TValue'] | TNull> | AsyncIterable<T['TValue'] | TNull> | VectorBuilderOptions<T, TNull> | VectorBuilderOptionsAsync<T, TNull>) {
99 if (isIterable(input)) {
100 return Vector.from({ 'nullValues': [null, undefined], type: newDataType(), 'values': input }) as V<T>;
101 } else if (isAsyncIterable(input)) {
102 return Vector.from({ 'nullValues': [null, undefined], type: newDataType(), 'values': input }) as Promise<V<T>>;
103 }
104 const {
105 'values': values = [],
106 'type': type = newDataType(),
107 'nullValues': nullValues = [null, undefined],
108 } = { ...input };
109 return isIterable(values)
110 ? Vector.from({ nullValues, ...input, type } as VectorBuilderOptions<T, TNull>)
111 : Vector.from({ nullValues, ...input, type } as VectorBuilderOptionsAsync<T, TNull>);
112 }
113
114 /** @ignore */
115 function vectorFrom<T extends DataType = any, TNull = any>(input: VectorBuilderOptions<T, TNull>): Vector<T>;
116 function vectorFrom<T extends DataType = any, TNull = any>(input: VectorBuilderOptionsAsync<T, TNull>): Promise<Vector<T>>;
117 function vectorFrom<T extends DataType = any, TNull = any>(input: VectorBuilderOptions<T, TNull> | VectorBuilderOptionsAsync<T, TNull>) {
118 const { 'values': values = [], ...options } = { 'nullValues': [null, undefined], ...input } as VectorBuilderOptions<T, TNull> | VectorBuilderOptionsAsync<T, TNull>;
119 if (isIterable<T['TValue'] | TNull>(values)) {
120 const chunks = [...Builder.throughIterable(options)(values)];
121 return (chunks.length === 1 ? chunks[0] : Chunked.concat<T>(chunks)) as Vector<T>;
122 }
123 return (async (chunks: V<T>[]) => {
124 const transform = Builder.throughAsyncIterable(options);
125 for await (const chunk of transform(values)) {
126 chunks.push(chunk);
127 }
128 return (chunks.length === 1 ? chunks[0] : Chunked.concat<T>(chunks)) as Vector<T>;
129 })([]);
130 }
131
132 //
133 // We provide the following method implementations for code navigability purposes only.
134 // They're overridden at runtime below with the specific Visitor implementation for each type,
135 // short-circuiting the usual Visitor traversal and reducing intermediate lookups and calls.
136 // This comment is here to remind you to not set breakpoints in these function bodies, or to inform
137 // you why the breakpoints you have already set are not being triggered. Have a great day!
138 //
139
140 BaseVector.prototype.get = function baseVectorGet<T extends DataType>(this: BaseVector<T>, index: number): T['TValue'] | null {
141 return getVisitor.visit(this, index);
142 };
143
144 BaseVector.prototype.set = function baseVectorSet<T extends DataType>(this: BaseVector<T>, index: number, value: T['TValue'] | null): void {
145 return setVisitor.visit(this, index, value);
146 };
147
148 BaseVector.prototype.indexOf = function baseVectorIndexOf<T extends DataType>(this: BaseVector<T>, value: T['TValue'] | null, fromIndex?: number): number {
149 return indexOfVisitor.visit(this, value, fromIndex);
150 };
151
152 BaseVector.prototype.toArray = function baseVectorToArray<T extends DataType>(this: BaseVector<T>): T['TArray'] {
153 return toArrayVisitor.visit(this);
154 };
155
156 BaseVector.prototype.getByteWidth = function baseVectorGetByteWidth<T extends DataType>(this: BaseVector<T>): number {
157 return byteWidthVisitor.visit(this.type);
158 };
159
160 BaseVector.prototype[Symbol.iterator] = function baseVectorSymbolIterator<T extends DataType>(this: BaseVector<T>): IterableIterator<T['TValue'] | null> {
161 return iteratorVisitor.visit(this);
162 };
163
164 (BaseVector.prototype as any)._bindDataAccessors = bindBaseVectorDataAccessors;
165
166 // Perf: bind and assign the operator Visitor methods to each of the Vector subclasses for each Type
167 (Object.keys(Type) as any[])
168 .map((T: any) => Type[T] as any)
169 .filter((T: any): T is Type => typeof T === 'number')
170 .filter((typeId) => typeId !== Type.NONE)
171 .forEach((typeId) => {
172 const VectorCtor = getVectorConstructor.visit(typeId);
173 VectorCtor.prototype['get'] = fn.partial1(getVisitor.getVisitFn(typeId));
174 VectorCtor.prototype['set'] = fn.partial2(setVisitor.getVisitFn(typeId));
175 VectorCtor.prototype['indexOf'] = fn.partial2(indexOfVisitor.getVisitFn(typeId));
176 VectorCtor.prototype['toArray'] = fn.partial0(toArrayVisitor.getVisitFn(typeId));
177 VectorCtor.prototype['getByteWidth'] = partialType0(byteWidthVisitor.getVisitFn(typeId));
178 VectorCtor.prototype[Symbol.iterator] = fn.partial0(iteratorVisitor.getVisitFn(typeId));
179 });
180
181 /** @ignore */
182 function partialType0<T extends Vector>(visit: (node: T['type']) => any) {
183 return function(this: T) { return visit(this.type); };
184 }
185
186 /** @ignore */
187 function wrapNullableGet<T extends DataType, V extends Vector<T>, F extends (i: number) => any>(fn: F): (...args: Parameters<F>) => ReturnType<F> {
188 return function(this: V, i: number) { return this.isValid(i) ? fn.call(this, i) : null; };
189 }
190
191 /** @ignore */
192 function wrapNullableSet<T extends DataType, V extends BaseVector<T>, F extends (i: number, a: any) => void>(fn: F): (...args: Parameters<F>) => void {
193 return function(this: V, i: number, a: any) {
194 if (setBool(this.nullBitmap, this.offset + i, !((a == null)))) {
195 fn.call(this, i, a);
196 }
197 };
198 }
199
200 /** @ignore */
201 function bindBaseVectorDataAccessors<T extends DataType>(this: BaseVector<T>) {
202 const nullBitmap = this.nullBitmap;
203 if (nullBitmap && nullBitmap.byteLength > 0) {
204 this.get = wrapNullableGet(this.get);
205 this.set = wrapNullableSet(this.set);
206 }
207 }