|
1 | 1 | // SPDX-License-Identifier: MIT |
2 | 2 | // Copyright contributors to the kepler.gl project |
3 | 3 |
|
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'; |
6 | 6 | 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'; |
9 | 9 |
|
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'; |
12 | 12 |
|
13 | 13 | type ArrowDataContainerInput = { |
14 | 14 | cols: arrow.Vector[]; |
15 | 15 | fields?: ProtoDatasetField[]; |
16 | 16 | arrowTable?: arrow.Table; |
17 | 17 | }; |
18 | 18 |
|
| 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 | +} |
19 | 35 |
|
20 | 36 | /** |
21 | 37 | * @param dataContainer |
@@ -89,13 +105,24 @@ export class ArrowDataContainer implements DataContainerInterface { |
89 | 105 | return this._arrowTable; |
90 | 106 | } |
91 | 107 |
|
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 | + } |
94 | 118 | this._numColumns = this._cols.length; |
95 | 119 | this._numRows = this._cols[0].length; |
96 | 120 | 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 | + } |
99 | 126 |
|
100 | 127 | // cache column data to make valueAt() faster |
101 | 128 | // this._colData = this._cols.map(c => c.toArray()); |
|
0 commit comments