]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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 { Type } from '../enum'; | |
20 | import { Visitor } from '../visitor'; | |
21 | import { VectorType } from '../interfaces'; | |
22 | import { BitIterator } from '../util/bit'; | |
23 | import { instance as getVisitor } from './get'; | |
24 | import { | |
25 | DataType, Dictionary, | |
26 | Bool, Null, Utf8, Binary, Decimal, FixedSizeBinary, List, FixedSizeList, Map_, Struct, | |
27 | Float, Float16, Float32, Float64, | |
28 | Int, Uint8, Uint16, Uint32, Uint64, Int8, Int16, Int32, Int64, | |
29 | Date_, DateDay, DateMillisecond, | |
30 | Interval, IntervalDayTime, IntervalYearMonth, | |
31 | Time, TimeSecond, TimeMillisecond, TimeMicrosecond, TimeNanosecond, | |
32 | Timestamp, TimestampSecond, TimestampMillisecond, TimestampMicrosecond, TimestampNanosecond, | |
33 | Union, DenseUnion, SparseUnion, | |
34 | } from '../type'; | |
35 | ||
36 | /** @ignore */ | |
37 | export interface IteratorVisitor extends Visitor { | |
38 | visit<T extends VectorType>(node: T): IterableIterator<T['TValue'] | null>; | |
39 | visitMany <T extends VectorType>(nodes: T[]): IterableIterator<T['TValue'] | null>[]; | |
40 | getVisitFn<T extends Type>(node: T): (vector: VectorType<T>) => IterableIterator<VectorType<T>['TValue'] | null>; | |
41 | getVisitFn<T extends DataType>(node: VectorType<T> | Data<T> | T): (vector: VectorType<T>) => IterableIterator<VectorType<T>['TValue'] | null>; | |
42 | visitNull <T extends Null> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
43 | visitBool <T extends Bool> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
44 | visitInt <T extends Int> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
45 | visitInt8 <T extends Int8> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
46 | visitInt16 <T extends Int16> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
47 | visitInt32 <T extends Int32> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
48 | visitInt64 <T extends Int64> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
49 | visitUint8 <T extends Uint8> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
50 | visitUint16 <T extends Uint16> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
51 | visitUint32 <T extends Uint32> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
52 | visitUint64 <T extends Uint64> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
53 | visitFloat <T extends Float> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
54 | visitFloat16 <T extends Float16> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
55 | visitFloat32 <T extends Float32> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
56 | visitFloat64 <T extends Float64> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
57 | visitUtf8 <T extends Utf8> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
58 | visitBinary <T extends Binary> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
59 | visitFixedSizeBinary <T extends FixedSizeBinary> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
60 | visitDate <T extends Date_> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
61 | visitDateDay <T extends DateDay> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
62 | visitDateMillisecond <T extends DateMillisecond> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
63 | visitTimestamp <T extends Timestamp> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
64 | visitTimestampSecond <T extends TimestampSecond> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
65 | visitTimestampMillisecond <T extends TimestampMillisecond> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
66 | visitTimestampMicrosecond <T extends TimestampMicrosecond> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
67 | visitTimestampNanosecond <T extends TimestampNanosecond> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
68 | visitTime <T extends Time> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
69 | visitTimeSecond <T extends TimeSecond> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
70 | visitTimeMillisecond <T extends TimeMillisecond> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
71 | visitTimeMicrosecond <T extends TimeMicrosecond> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
72 | visitTimeNanosecond <T extends TimeNanosecond> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
73 | visitDecimal <T extends Decimal> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
74 | visitList <T extends List> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
75 | visitStruct <T extends Struct> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
76 | visitUnion <T extends Union> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
77 | visitDenseUnion <T extends DenseUnion> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
78 | visitSparseUnion <T extends SparseUnion> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
79 | visitDictionary <T extends Dictionary> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
80 | visitInterval <T extends Interval> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
81 | visitIntervalDayTime <T extends IntervalDayTime> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
82 | visitIntervalYearMonth <T extends IntervalYearMonth> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
83 | visitFixedSizeList <T extends FixedSizeList> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
84 | visitMap <T extends Map_> (vector: VectorType<T>): IterableIterator<T['TValue'] | null>; | |
85 | } | |
86 | ||
87 | /** @ignore */ | |
88 | export class IteratorVisitor extends Visitor {} | |
89 | ||
90 | /** @ignore */ | |
91 | function nullableIterator<T extends DataType>(vector: VectorType<T>): IterableIterator<T['TValue'] | null> { | |
92 | const getFn = getVisitor.getVisitFn(vector); | |
93 | return new BitIterator<T['TValue'] | null>( | |
94 | vector.data.nullBitmap, vector.data.offset, vector.length, vector, | |
95 | (vec: VectorType<T>, idx: number, nullByte: number, nullBit: number) => | |
96 | ((nullByte & 1 << nullBit) !== 0) ? getFn(vec, idx) : null | |
97 | ); | |
98 | } | |
99 | ||
100 | /** @ignore */ | |
101 | class VectorIterator<T extends DataType> implements IterableIterator<T['TValue'] | null> { | |
102 | private index = 0; | |
103 | ||
104 | constructor( | |
105 | private vector: VectorType<T>, | |
106 | private getFn: (vector: VectorType<T>, index: number) => VectorType<T>['TValue'] | |
107 | ) {} | |
108 | ||
109 | next(): IteratorResult<T['TValue'] | null> { | |
110 | if (this.index < this.vector.length) { | |
111 | return { | |
112 | value: this.getFn(this.vector, this.index++) | |
113 | }; | |
114 | } | |
115 | ||
116 | return {done: true, value: null}; | |
117 | } | |
118 | ||
119 | [Symbol.iterator]() { | |
120 | return this; | |
121 | } | |
122 | } | |
123 | ||
124 | /** @ignore */ | |
125 | function vectorIterator<T extends DataType>(vector: VectorType<T>): IterableIterator<T['TValue'] | null> { | |
126 | ||
127 | // If nullable, iterate manually | |
128 | if (vector.nullCount > 0) { | |
129 | return nullableIterator<T>(vector); | |
130 | } | |
131 | ||
132 | const { type, typeId, length } = vector; | |
133 | ||
134 | // Fast case, defer to native iterators if possible | |
135 | if (vector.stride === 1 && ( | |
136 | (typeId === Type.Timestamp) || | |
137 | (typeId === Type.Int && (type as Int).bitWidth !== 64) || | |
138 | (typeId === Type.Time && (type as Time).bitWidth !== 64) || | |
139 | (typeId === Type.Float && (type as Float).precision > 0 /* Precision.HALF */) | |
140 | )) { | |
141 | return vector.data.values.subarray(0, length)[Symbol.iterator](); | |
142 | } | |
143 | ||
144 | // Otherwise, iterate manually | |
145 | return new VectorIterator(vector, getVisitor.getVisitFn(vector)); | |
146 | } | |
147 | ||
148 | IteratorVisitor.prototype.visitNull = vectorIterator; | |
149 | IteratorVisitor.prototype.visitBool = vectorIterator; | |
150 | IteratorVisitor.prototype.visitInt = vectorIterator; | |
151 | IteratorVisitor.prototype.visitInt8 = vectorIterator; | |
152 | IteratorVisitor.prototype.visitInt16 = vectorIterator; | |
153 | IteratorVisitor.prototype.visitInt32 = vectorIterator; | |
154 | IteratorVisitor.prototype.visitInt64 = vectorIterator; | |
155 | IteratorVisitor.prototype.visitUint8 = vectorIterator; | |
156 | IteratorVisitor.prototype.visitUint16 = vectorIterator; | |
157 | IteratorVisitor.prototype.visitUint32 = vectorIterator; | |
158 | IteratorVisitor.prototype.visitUint64 = vectorIterator; | |
159 | IteratorVisitor.prototype.visitFloat = vectorIterator; | |
160 | IteratorVisitor.prototype.visitFloat16 = vectorIterator; | |
161 | IteratorVisitor.prototype.visitFloat32 = vectorIterator; | |
162 | IteratorVisitor.prototype.visitFloat64 = vectorIterator; | |
163 | IteratorVisitor.prototype.visitUtf8 = vectorIterator; | |
164 | IteratorVisitor.prototype.visitBinary = vectorIterator; | |
165 | IteratorVisitor.prototype.visitFixedSizeBinary = vectorIterator; | |
166 | IteratorVisitor.prototype.visitDate = vectorIterator; | |
167 | IteratorVisitor.prototype.visitDateDay = vectorIterator; | |
168 | IteratorVisitor.prototype.visitDateMillisecond = vectorIterator; | |
169 | IteratorVisitor.prototype.visitTimestamp = vectorIterator; | |
170 | IteratorVisitor.prototype.visitTimestampSecond = vectorIterator; | |
171 | IteratorVisitor.prototype.visitTimestampMillisecond = vectorIterator; | |
172 | IteratorVisitor.prototype.visitTimestampMicrosecond = vectorIterator; | |
173 | IteratorVisitor.prototype.visitTimestampNanosecond = vectorIterator; | |
174 | IteratorVisitor.prototype.visitTime = vectorIterator; | |
175 | IteratorVisitor.prototype.visitTimeSecond = vectorIterator; | |
176 | IteratorVisitor.prototype.visitTimeMillisecond = vectorIterator; | |
177 | IteratorVisitor.prototype.visitTimeMicrosecond = vectorIterator; | |
178 | IteratorVisitor.prototype.visitTimeNanosecond = vectorIterator; | |
179 | IteratorVisitor.prototype.visitDecimal = vectorIterator; | |
180 | IteratorVisitor.prototype.visitList = vectorIterator; | |
181 | IteratorVisitor.prototype.visitStruct = vectorIterator; | |
182 | IteratorVisitor.prototype.visitUnion = vectorIterator; | |
183 | IteratorVisitor.prototype.visitDenseUnion = vectorIterator; | |
184 | IteratorVisitor.prototype.visitSparseUnion = vectorIterator; | |
185 | IteratorVisitor.prototype.visitDictionary = vectorIterator; | |
186 | IteratorVisitor.prototype.visitInterval = vectorIterator; | |
187 | IteratorVisitor.prototype.visitIntervalDayTime = vectorIterator; | |
188 | IteratorVisitor.prototype.visitIntervalYearMonth = vectorIterator; | |
189 | IteratorVisitor.prototype.visitFixedSizeList = vectorIterator; | |
190 | IteratorVisitor.prototype.visitMap = vectorIterator; | |
191 | ||
192 | /** @ignore */ | |
193 | export const instance = new IteratorVisitor(); |