-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathrecordbatch.ts
More file actions
386 lines (346 loc) · 14.9 KB
/
recordbatch.ts
File metadata and controls
386 lines (346 loc) · 14.9 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// 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 { Table } from './table.js';
import { Vector } from './vector.js';
import { Schema, Field } from './schema.js';
import { DataType, Struct, Null, TypeMap } from './type.js';
import { wrapIndex } from './util/vector.js';
import { instance as getVisitor } from './visitor/get.js';
import { instance as setVisitor } from './visitor/set.js';
import { instance as indexOfVisitor } from './visitor/indexof.js';
import { instance as iteratorVisitor } from './visitor/iterator.js';
/** @ignore */
const kRecordBatchSymbol = Symbol.for('apache-arrow/RecordBatch');
/** @ignore */
export interface RecordBatch<T extends TypeMap = any> {
///
// Virtual properties for the TypeScript compiler.
// These do not exist at runtime.
///
readonly TType: Struct<T>;
readonly TArray: Struct<T>['TArray'];
readonly TValue: Struct<T>['TValue'];
/**
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/isConcatSpreadable
*/
[Symbol.isConcatSpreadable]: true;
}
/** @ignore */
export class RecordBatch<T extends TypeMap = any> {
/**
* Check if an object is an instance of RecordBatch.
* This works across different instances of the Arrow library.
*/
/** @nocollapse */ static isRecordBatch(x: any): x is RecordBatch {
return x?.[kRecordBatchSymbol] === true;
}
/** @internal */
declare public readonly [kRecordBatchSymbol]: true;
constructor(columns: { [P in keyof T]: Data<T[P]> });
constructor(schema: Schema<T>, data?: Data<Struct<T>>, metadata?: Map<string, string>);
constructor(...args: any[]) {
switch (args.length) {
case 3:
case 2: {
[this.schema] = args;
if (!(this.schema instanceof Schema)) {
throw new TypeError('RecordBatch constructor expects a [Schema, Data] pair.');
}
[,
this.data = makeData({
nullCount: 0,
type: new Struct<T>(this.schema.fields),
children: this.schema.fields.map((f) => makeData({ type: f.type, nullCount: 0 }))
}),
this._metadata = new Map()
] = args;
if (!(this.data instanceof Data)) {
throw new TypeError('RecordBatch constructor expects a [Schema, Data] pair.');
}
[this.schema, this.data] = ensureSameLengthData<T>(this.schema, this.data.children as Data<T[keyof T]>[]);
break;
}
case 1: {
const [obj] = args;
const { fields, children, length } = Object.keys(obj).reduce((memo, name, i) => {
memo.children[i] = obj[name];
memo.length = Math.max(memo.length, obj[name].length);
memo.fields[i] = Field.new({ name, type: obj[name].type, nullable: true });
return memo;
}, {
length: 0,
fields: new Array<Field<T[keyof T]>>(),
children: new Array<Data<T[keyof T]>>(),
});
const schema = new Schema<T>(fields);
const data = makeData({ type: new Struct<T>(fields), length, children, nullCount: 0 });
[this.schema, this.data] = ensureSameLengthData<T>(schema, data.children as Data<T[keyof T]>[], length);
this._metadata = new Map();
break;
}
default: throw new TypeError('RecordBatch constructor expects an Object mapping names to child Data, or a [Schema, Data] pair.');
}
}
protected _dictionaries?: Map<number, Vector>;
protected _metadata: Map<string, string>;
public readonly schema: Schema<T>;
public readonly data: Data<Struct<T>>;
/**
* Custom metadata for this RecordBatch.
*/
public get metadata() { return this._metadata; }
public get dictionaries() {
return this._dictionaries || (this._dictionaries = collectDictionaries(this.schema.fields, this.data.children));
}
/**
* The number of columns in this RecordBatch.
*/
public get numCols() { return this.schema.fields.length; }
/**
* The number of rows in this RecordBatch.
*/
public get numRows() { return this.data.length; }
/**
* The number of null rows in this RecordBatch.
*/
public get nullCount() {
return this.data.nullCount;
}
/**
* Check whether an row is null.
* @param index The index at which to read the validity bitmap.
*/
public isValid(index: number) {
return this.data.getValid(index);
}
/**
* Get a row by position.
* @param index The index of the row to read.
*/
public get(index: number) {
return getVisitor.visit(this.data, index);
}
/**
* Get a row value by position.
* @param index The index of the row to read. A negative index will count back from the last row.
*/
public at(index: number) {
return this.get(wrapIndex(index, this.numRows));
}
/**
* Set a row by position.
* @param index The index of the row to write.
* @param value The value to set.
*/
public set(index: number, value: Struct<T>['TValue']) {
return setVisitor.visit(this.data, index, value);
}
/**
* Retrieve the index of the first occurrence of a row in an RecordBatch.
* @param element The row to locate in the RecordBatch.
* @param offset The index at which to begin the search. If offset is omitted, the search starts at index 0.
*/
public indexOf(element: Struct<T>['TValue'], offset?: number): number {
return indexOfVisitor.visit(this.data, element, offset);
}
/**
* Iterator for rows in this RecordBatch.
*/
public [Symbol.iterator]() {
return iteratorVisitor.visit(new Vector([this.data])) as IterableIterator<Struct<T>['TValue']>;
}
/**
* Return a JavaScript Array of the RecordBatch rows.
* @returns An Array of RecordBatch rows.
*/
public toArray() {
return [...this];
}
/**
* Combines two or more RecordBatch of the same schema.
* @param others Additional RecordBatch to add to the end of this RecordBatch.
*/
public concat(...others: RecordBatch<T>[]) {
return new Table(this.schema, [this, ...others]);
}
/**
* Return a zero-copy sub-section of this RecordBatch.
* @param start The beginning of the specified portion of the RecordBatch.
* @param end The end of the specified portion of the RecordBatch. This is exclusive of the row at the index 'end'.
*/
public slice(begin?: number, end?: number): RecordBatch<T> {
const [slice] = new Vector([this.data]).slice(begin, end).data;
return new RecordBatch(this.schema, slice, this._metadata);
}
/**
* Returns a child Vector by name, or null if this Vector has no child with the given name.
* @param name The name of the child to retrieve.
*/
public getChild<P extends keyof T>(name: P) {
return this.getChildAt<T[P]>(this.schema.fields?.findIndex((f) => f.name === name));
}
/**
* Returns a child Vector by index, or null if this Vector has no child at the supplied index.
* @param index The index of the child to retrieve.
*/
public getChildAt<R extends T[keyof T] = any>(index: number): Vector<R> | null {
if (index > -1 && index < this.schema.fields.length) {
return new Vector([this.data.children[index]]) as Vector<R>;
}
return null;
}
/**
* Sets a child Vector by name.
* @param name The name of the child to overwrite.
* @returns A new RecordBatch with the new child for the specified name.
*/
public setChild<P extends keyof T, R extends DataType>(name: P, child: Vector<R>) {
return this.setChildAt(this.schema.fields?.findIndex((f) => f.name === name), child) as RecordBatch<T & { [K in P]: R }>;
}
/**
* Sets a child Vector by index.
* @param index The index of the child to overwrite.
* @returns A new RecordBatch with the new child at the specified index.
*/
public setChildAt(index: number, child?: null): RecordBatch;
public setChildAt<R extends DataType = any>(index: number, child: Vector<R>): RecordBatch;
public setChildAt(index: number, child: any) {
let schema: Schema = this.schema;
let data: Data<Struct> = this.data;
if (index > -1 && index < this.numCols) {
if (!child) {
child = new Vector([makeData({ type: new Null, length: this.numRows })]);
}
const fields = schema.fields.slice() as Field<any>[];
const children = data.children.slice() as Data<any>[];
const field = fields[index].clone({ type: child.type });
[fields[index], children[index]] = [field, child.data[0]];
schema = new Schema(fields, new Map(this.schema.metadata));
data = makeData({ type: new Struct<T>(fields), length: data.length, children });
}
return new RecordBatch(schema, data, this._metadata);
}
/**
* Construct a new RecordBatch containing only specified columns.
*
* @param columnNames Names of columns to keep.
* @returns A new RecordBatch of columns matching the specified names.
*/
public select<K extends keyof T = any>(columnNames: K[]) {
const schema = this.schema.select(columnNames);
const type = new Struct(schema.fields);
const children = [] as Data<T[K]>[];
for (const name of columnNames) {
const index = this.schema.fields.findIndex((f) => f.name === name);
if (~index) {
children[index] = this.data.children[index] as Data<T[K]>;
}
}
return new RecordBatch(schema, makeData({ type, length: this.numRows, children }), this._metadata);
}
/**
* Construct a new RecordBatch containing only columns at the specified indices.
*
* @param columnIndices Indices of columns to keep.
* @returns A new RecordBatch of columns matching at the specified indices.
*/
public selectAt<K extends T = any>(columnIndices: number[]) {
const schema = this.schema.selectAt<K>(columnIndices);
const children = columnIndices.map((i) => this.data.children[i]).filter(Boolean);
const subset = makeData({ type: new Struct(schema.fields), length: this.numRows, children });
return new RecordBatch<{ [P in keyof K]: K[P] }>(schema, subset, this._metadata);
}
// Initialize this static property via an IIFE so bundlers don't tree-shake
// out this logic, but also so we're still compliant with `"sideEffects": false`
protected static [Symbol.toStringTag] = ((proto: RecordBatch) => {
(proto as any)._nullCount = -1;
(proto as any)[Symbol.isConcatSpreadable] = true;
(proto as any)[kRecordBatchSymbol] = true;
return 'RecordBatch';
})(RecordBatch.prototype);
}
Object.defineProperty(RecordBatch, Symbol.hasInstance, {
value: function isRecordBatchInstance(instance: any): instance is RecordBatch {
return Function.prototype[Symbol.hasInstance].call(this, instance)
|| (this === RecordBatch && RecordBatch.isRecordBatch(instance));
},
});
/** @ignore */
function ensureSameLengthData<T extends TypeMap = any>(
schema: Schema<T>,
chunks: Data<T[keyof T]>[],
maxLength = chunks.reduce((max, col) => Math.max(max, col.length), 0)
) {
const fields = [...schema.fields];
const children = [...chunks] as Data<T[keyof T]>[];
const nullBitmapSize = ((maxLength + 63) & ~63) >> 3;
for (const [idx, field] of schema.fields.entries()) {
const chunk = chunks[idx];
if (!chunk || chunk.length !== maxLength) {
fields[idx] = field.clone({ nullable: true });
children[idx] = chunk?._changeLengthAndBackfillNullBitmap(maxLength) ?? makeData({
type: field.type,
length: maxLength,
nullCount: maxLength,
nullBitmap: new Uint8Array(nullBitmapSize)
});
}
}
return [
schema.assign(fields),
makeData({ type: new Struct<T>(fields), length: maxLength, children })
] as [Schema<T>, Data<Struct<T>>];
}
/** @ignore */
function collectDictionaries(fields: Field[], children: readonly Data[], dictionaries = new Map<number, Vector>()): Map<number, Vector> {
if ((fields?.length ?? 0) > 0 && (fields?.length === children?.length)) {
for (let i = -1, n = fields.length; ++i < n;) {
const { type } = fields[i];
const data = children[i];
for (const next of [data, ...(data?.dictionary?.data || [])]) {
collectDictionaries(type.children, next?.children, dictionaries);
}
if (DataType.isDictionary(type)) {
const { id } = type;
if (!dictionaries.has(id)) {
if (data?.dictionary) {
dictionaries.set(id, data.dictionary);
}
} else if (dictionaries.get(id) !== data.dictionary) {
throw new Error(`Cannot create Schema containing two different dictionaries with the same Id`);
}
}
}
}
return dictionaries;
}
/**
* An internal class used by the `RecordBatchReader` and `RecordBatchWriter`
* implementations to differentiate between a stream with valid zero-length
* RecordBatches, and a stream with a Schema message, but no RecordBatches.
* @see https://github.com/apache/arrow/pull/4373
* @ignore
* @private
*/
export class _InternalEmptyPlaceholderRecordBatch<T extends TypeMap = any> extends RecordBatch<T> {
constructor(schema: Schema<T>, metadata?: Map<string, string>) {
const children = schema.fields.map((f) => makeData({ type: f.type }));
const data = makeData({ type: new Struct<T>(schema.fields), length: 0, nullCount: 0, children });
super(schema, data, metadata || new Map());
}
}