-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathparse-arrow-in-batches.ts
58 lines (52 loc) · 1.78 KB
/
parse-arrow-in-batches.ts
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
// TODO - this import defeats the sophisticated typescript checking in ArrowJS
import {ArrowTableBatch} from '@loaders.gl/schema';
import {RecordBatchReader} from 'apache-arrow';
// import {isIterable} from '@loaders.gl/core';
/**
*/
export function parseArrowInBatches(
asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>
): AsyncIterable<ArrowTableBatch> {
// Creates the appropriate RecordBatchReader subclasses from the input
// This will also close the underlying source in case of early termination or errors
// As an optimization, return a non-async iterator
/*
if (isIterable(readers)) {
function* makeArrowIterator() {
for (const reader of readers) {
for (const batch of reader) {
yield processBatch(batch, reader);
}
break; // only processing one stream of batches
}
}
const arrowIterator = makeArrowIterator();
}
*/
async function* makeArrowAsyncIterator(): AsyncIterator<ArrowTableBatch> {
// @ts-ignore
const readers = RecordBatchReader.readAll(asyncIterator);
for await (const reader of readers) {
for await (const recordBatch of reader) {
const arrowTabledBatch: ArrowTableBatch = {
shape: 'arrow-table',
batchType: 'data',
data: recordBatch,
length: recordBatch.data.length
};
// processBatch(recordBatch);
yield arrowTabledBatch;
}
break; // only processing one stream of batches
}
}
return makeArrowAsyncIterator() as any; // as AsyncIterator<ArrowTableBatch>;
}
// function processBatch(batch: RecordBatch): ArrowTableBatch {
// const values = {};
// batch.schema.fields.forEach(({name}, index) => {
// values[name] = batch.getChildAt(index)?.toArray();
// });
// return {
// };
// }