-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathvectorloader.ts
More file actions
207 lines (199 loc) · 12.4 KB
/
Copy pathvectorloader.ts
File metadata and controls
207 lines (199 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
import { Data, makeData } from '../data.js';
import * as type from '../type.js';
import { Field } from '../schema.js';
import { Vector } from '../vector.js';
import { DataType } from '../type.js';
import { Visitor } from '../visitor.js';
import { packBools } from '../util/bit.js';
import { encodeUtf8 } from '../util/utf8.js';
import { Int64, Int128 } from '../util/int.js';
import { UnionMode, DateUnit, MetadataVersion, IntervalUnit } from '../enum.js';
import { toArrayBufferView } from '../util/buffer.js';
import { BufferRegion, FieldNode } from '../ipc/metadata/message.js';
import { toIntervalDayTimeInt32Array, toIntervalMonthDayNanoInt32Array } from '../util/interval.js';
/** @ignore */
export interface VectorLoader extends Visitor {
visit<T extends DataType>(node: Field<T> | T): Data<T>;
visitMany<T extends DataType>(nodes: (Field<T> | T)[]): Data<T>[];
}
/** @ignore */
export class VectorLoader extends Visitor {
private bytes: Uint8Array;
private nodes: FieldNode[];
private nodesIndex = -1;
private buffers: BufferRegion[];
private buffersIndex = -1;
private dictionaries: Map<number, Vector<any>>;
private readonly metadataVersion: MetadataVersion;
constructor(bytes: Uint8Array, nodes: FieldNode[], buffers: BufferRegion[], dictionaries: Map<number, Vector<any>>, metadataVersion = MetadataVersion.V5) {
super();
this.bytes = bytes;
this.nodes = nodes;
this.buffers = buffers;
this.dictionaries = dictionaries;
this.metadataVersion = metadataVersion;
}
public visit<T extends DataType>(node: Field<T> | T): Data<T> {
return super.visit(node instanceof Field ? node.type : node);
}
public visitNull<T extends type.Null>(type: T, { length } = this.nextFieldNode()) {
return makeData({ type, length });
}
public visitBool<T extends type.Bool>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), data: this.readData(type) });
}
public visitInt<T extends type.Int>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), data: this.readData(type) });
}
public visitFloat<T extends type.Float>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), data: this.readData(type) });
}
public visitUtf8<T extends type.Utf8>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), valueOffsets: this.readOffsets(type), data: this.readData(type) });
}
public visitLargeUtf8<T extends type.LargeUtf8>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), valueOffsets: this.readOffsets(type), data: this.readData(type) });
}
public visitBinary<T extends type.Binary>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), valueOffsets: this.readOffsets(type), data: this.readData(type) });
}
public visitLargeBinary<T extends type.LargeBinary>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), valueOffsets: this.readOffsets(type), data: this.readData(type) });
}
public visitFixedSizeBinary<T extends type.FixedSizeBinary>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), data: this.readData(type) });
}
public visitDate<T extends type.Date_>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), data: this.readData(type) });
}
public visitTimestamp<T extends type.Timestamp>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), data: this.readData(type) });
}
public visitTime<T extends type.Time>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), data: this.readData(type) });
}
public visitDecimal<T extends type.Decimal>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), data: this.readData(type) });
}
public visitList<T extends type.List>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), valueOffsets: this.readOffsets(type), 'child': this.visit(type.children[0]) });
}
public visitStruct<T extends type.Struct>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), children: this.visitMany(type.children) });
}
public visitUnion<T extends type.Union>(type: T, { length, nullCount } = this.nextFieldNode()) {
if (this.metadataVersion < MetadataVersion.V5) {
this.readNullBitmap(type, nullCount);
}
return type.mode === UnionMode.Sparse
? this.visitSparseUnion(type as type.SparseUnion, { length, nullCount })
: this.visitDenseUnion(type as type.DenseUnion, { length, nullCount });
}
public visitDenseUnion<T extends type.DenseUnion>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, typeIds: this.readTypeIds(type), valueOffsets: this.readOffsets(type), children: this.visitMany(type.children) });
}
public visitSparseUnion<T extends type.SparseUnion>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, typeIds: this.readTypeIds(type), children: this.visitMany(type.children) });
}
public visitDictionary<T extends type.Dictionary>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), data: this.readData(type.indices), dictionary: this.readDictionary(type) });
}
public visitInterval<T extends type.Interval>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), data: this.readData(type) });
}
public visitDuration<T extends type.Duration>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), data: this.readData(type) });
}
public visitFixedSizeList<T extends type.FixedSizeList>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), 'child': this.visit(type.children[0]) });
}
public visitMap<T extends type.Map_>(type: T, { length, nullCount } = this.nextFieldNode()) {
return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), valueOffsets: this.readOffsets(type), 'child': this.visit(type.children[0]) });
}
protected nextFieldNode() { return this.nodes[++this.nodesIndex]; }
protected nextBufferRange() { return this.buffers[++this.buffersIndex]; }
protected readNullBitmap<T extends DataType>(type: T, nullCount: number, buffer = this.nextBufferRange()) {
return nullCount > 0 && this.readData(type, buffer) || new Uint8Array(0);
}
protected readOffsets<T extends DataType>(type: T, buffer?: BufferRegion) { return this.readData(type, buffer); }
protected readTypeIds<T extends DataType>(type: T, buffer?: BufferRegion) { return this.readData(type, buffer); }
protected readData<T extends DataType>(_type: T, { length, offset } = this.nextBufferRange()) {
return this.bytes.subarray(offset, offset + length);
}
protected readDictionary<T extends type.Dictionary>(type: T): Vector<T['dictionary']> {
return this.dictionaries.get(type.id)!;
}
}
/** @ignore */
export class JSONVectorLoader extends VectorLoader {
private sources: any[][];
constructor(sources: any[][], nodes: FieldNode[], buffers: BufferRegion[], dictionaries: Map<number, Vector<any>>, metadataVersion: MetadataVersion) {
super(new Uint8Array(0), nodes, buffers, dictionaries, metadataVersion);
this.sources = sources;
}
protected readNullBitmap<T extends DataType>(_type: T, nullCount: number, { offset } = this.nextBufferRange()) {
return nullCount <= 0 ? new Uint8Array(0) : packBools(this.sources[offset]);
}
protected readOffsets<T extends DataType>(_type: T, { offset } = this.nextBufferRange()) {
return toArrayBufferView(Uint8Array, toArrayBufferView(_type.OffsetArrayType, this.sources[offset]));
}
protected readTypeIds<T extends DataType>(type: T, { offset } = this.nextBufferRange()) {
return toArrayBufferView(Uint8Array, toArrayBufferView(type.ArrayType, this.sources[offset]));
}
protected readData<T extends DataType>(type: T, { offset } = this.nextBufferRange()) {
const { sources } = this;
if (DataType.isTimestamp(type)) {
return toArrayBufferView(Uint8Array, Int64.convertArray(sources[offset] as string[]));
} else if ((DataType.isInt(type) || DataType.isTime(type)) && type.bitWidth === 64 || DataType.isDuration(type)) {
return toArrayBufferView(Uint8Array, Int64.convertArray(sources[offset] as string[]));
} else if (DataType.isDate(type) && type.unit === DateUnit.MILLISECOND) {
return toArrayBufferView(Uint8Array, Int64.convertArray(sources[offset] as string[]));
} else if (DataType.isDecimal(type)) {
return toArrayBufferView(Uint8Array, Int128.convertArray(sources[offset] as string[]));
} else if (DataType.isBinary(type) || DataType.isLargeBinary(type) || DataType.isFixedSizeBinary(type)) {
return binaryDataFromJSON(sources[offset] as string[]);
} else if (DataType.isBool(type)) {
return packBools(sources[offset] as number[]);
} else if (DataType.isUtf8(type) || DataType.isLargeUtf8(type)) {
return encodeUtf8((sources[offset] as string[]).join(''));
} else if (DataType.isInterval(type)) {
switch (type.unit) {
case IntervalUnit.DAY_TIME:
return toIntervalDayTimeInt32Array(sources[offset]);
case IntervalUnit.MONTH_DAY_NANO:
return toIntervalMonthDayNanoInt32Array(sources[offset]);
default:
break;
}
}
return toArrayBufferView(Uint8Array, toArrayBufferView(type.ArrayType, sources[offset].map((x) => +x)));
}
}
/** @ignore */
function binaryDataFromJSON(values: string[]) {
// "DATA": ["49BC7D5B6C47D2","3F5FB6D9322026"]
// There are definitely more efficient ways to do this... but it gets the
// job done.
const joined = values.join('');
const data = new Uint8Array(joined.length / 2);
for (let i = 0; i < joined.length; i += 2) {
data[i >> 1] = Number.parseInt(joined.slice(i, i + 2), 16);
}
return data;
}