Skip to content

Commit addcec4

Browse files
committed
fix: Allow passing arrow tables to ArrowDataContainer
Signed-off-by: Ilya Boyandin <ilyabo@gmail.com>
1 parent df78623 commit addcec4

5 files changed

Lines changed: 45 additions & 23 deletions

File tree

src/processors/src/file-handler.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// SPDX-License-Identifier: MIT
22
// Copyright contributors to the kepler.gl project
33

4-
import * as arrow from 'apache-arrow';
54
import {parseInBatches} from '@loaders.gl/core';
65
import {JSONLoader, _JSONPath} from '@loaders.gl/json';
76
import {CSVLoader} from '@loaders.gl/csv';
@@ -12,7 +11,8 @@ import {
1211
isPlainObject,
1312
generateHashIdFromString,
1413
getApplicationConfig,
15-
getError
14+
getError,
15+
isArrowTable
1616
} from '@kepler.gl/utils';
1717
import {generateHashId} from '@kepler.gl/common-utils';
1818
import {DATASET_FORMATS} from '@kepler.gl/constants';
@@ -68,14 +68,7 @@ export type ProcessFileDataContent = {
6868
metadata?: Map<string, string>;
6969
};
7070

71-
/**
72-
* check if table is an ArrowTable object
73-
* @param table - object to check
74-
* @returns {boolean} - true if table is an ArrowTable object type guarded
75-
*/
76-
export function isArrowTable(table: any): table is arrow.Table {
77-
return Boolean(table instanceof arrow.Table);
78-
}
71+
export {isArrowTable};
7972

8073
/**
8174
* check if data is an ArrowData object, which is an array of RecordBatch

src/table/src/kepler-table.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class KeplerTable<F extends Field = Field> {
241241
* @param data - new data e.g. the arrow data with new batches loaded
242242
*/
243243
async update(data: ProtoDataset['data']) {
244-
const dataContainerData = data.cols ? data.cols : data.rows;
244+
const dataContainerData = data.arrowTable ?? data.cols ?? data.rows;
245245
this.dataContainer.update?.(dataContainerData);
246246
this.allIndexes = this.dataContainer.getPlainIndex();
247247
this.filteredIndex = this.allIndexes;

src/utils/src/arrow-data-container.ts

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
// SPDX-License-Identifier: MIT
22
// Copyright contributors to the kepler.gl project
33

4-
import { ALL_FIELD_TYPES } from '@kepler.gl/constants';
5-
import { ProtoDatasetField } from '@kepler.gl/types';
4+
import {ALL_FIELD_TYPES} from '@kepler.gl/constants';
5+
import {ProtoDatasetField} from '@kepler.gl/types';
66
import * as arrow from 'apache-arrow';
7-
import { console as globalConsole } from 'global/window';
8-
import { DATA_TYPES as AnalyzerDATA_TYPES } from 'type-analyzer';
7+
import {console as globalConsole} from 'global/window';
8+
import {DATA_TYPES as AnalyzerDATA_TYPES} from 'type-analyzer';
99

10-
import { DataContainerInterface, RangeOptions } from './data-container-interface';
11-
import { DataRow, SharedRowOptions } from './data-row';
10+
import {DataContainerInterface, RangeOptions} from './data-container-interface';
11+
import {DataRow, SharedRowOptions} from './data-row';
1212

1313
type ArrowDataContainerInput = {
1414
cols: arrow.Vector[];
1515
fields?: ProtoDatasetField[];
1616
arrowTable?: arrow.Table;
1717
};
1818

19+
/**
20+
* check if table is an ArrowTable object
21+
* @param table - object to check
22+
* @returns {boolean} - true if table is an ArrowTable object type guarded
23+
*/
24+
export function isArrowTable(data: any): data is arrow.Table {
25+
return (
26+
typeof data === 'object' &&
27+
data !== null &&
28+
'schema' in data &&
29+
'getChildAt' in data &&
30+
typeof data.getChildAt === 'function' &&
31+
'batches' in data &&
32+
Array.isArray(data.batches)
33+
);
34+
}
1935

2036
/**
2137
* @param dataContainer
@@ -89,13 +105,24 @@ export class ArrowDataContainer implements DataContainerInterface {
89105
return this._arrowTable;
90106
}
91107

92-
update(updateData: arrow.Vector<any>[]) {
93-
this._cols = updateData;
108+
update(updateData: arrow.Vector<any>[] | arrow.Table) {
109+
const isArrow = isArrowTable(updateData);
110+
if (isArrow) {
111+
this._cols = Array.from(
112+
{length: updateData.numCols},
113+
(_, i) => updateData.getChildAt(i) as arrow.Vector
114+
).filter(col => col);
115+
} else {
116+
this._cols = updateData;
117+
}
94118
this._numColumns = this._cols.length;
95119
this._numRows = this._cols[0].length;
96120
this._numChunks = this._cols[0].data.length;
97-
98-
this._arrowTable = this._createTable();
121+
if (isArrow) {
122+
this._arrowTable = updateData;
123+
} else {
124+
this._arrowTable = this._createTable();
125+
}
99126

100127
// cache column data to make valueAt() faster
101128
// this._colData = this._cols.map(c => c.toArray());

src/utils/src/data-container-interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import {ProtoDatasetField} from '@kepler.gl/types';
55
import {DataRow, SharedRowOptions} from './data-row';
6+
import * as arrow from 'apache-arrow';
67

78
/**
89
* Specifies a range of rows of a data container that should be processed.
@@ -16,7 +17,7 @@ export interface DataContainerInterface {
1617
* Updates the data container with new data.
1718
* @param updateData updated data, e.g. for arrow data container, it's an array of arrow columns; for row data container, it's an array of rows.
1819
*/
19-
update?(updateData: any[]): void;
20+
update?(updateData: any[] | arrow.Table): void;
2021

2122
/**
2223
* Returns the number of rows in the data container.

src/utils/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ export * from './map-utils';
127127
export {
128128
ArrowDataContainer,
129129
arrowDataTypeToAnalyzerDataType,
130-
arrowDataTypeToFieldType
130+
arrowDataTypeToFieldType,
131+
isArrowTable
131132
} from './arrow-data-container';
132133
export type {DataContainerInterface} from './data-container-interface';
133134
export {

0 commit comments

Comments
 (0)