Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
fix: load Parquet/Arrow Int64 columns without BigInt TypeError (#3749)
Igor Dykhta4b8e5de038a2
diff --git a/src/common-utils/src/data-type.ts b/src/common-utils/src/data-type.tsindex 4f94f36ff..a12b5d8f2 100644--- a/src/common-utils/src/data-type.ts+++ b/src/common-utils/src/data-type.ts@@ -11,6 +11,21 @@ import {h3IsValid} from './h3-utils';const H3_ANALYZER_TYPE = 'H3';+/**+ * type-analyzer uses `isNaN(value)`, which throws on BigInt+ * ("Cannot convert a BigInt value to a number"). Arrow/Parquet Int64+ * columns yield BigInt in JS, so stringify those samples first.+ */+function toTypeAnalyzerValue(value: unknown): unknown {+ if (typeof value === 'string') {+ return value.trim();+ }+ if (typeof value === 'bigint') {+ return value.toString();+ }+ return value;+}+export const ACCEPTED_ANALYZER_TYPES = [AnalyzerDATA_TYPES.DATE,AnalyzerDATA_TYPES.TIME,@@ -68,7 +83,7 @@ export function getSampleForTypeAnalyze({j++;} else if (notNullorUndefined(rows[i][isRowObject ? field : fieldIdx])) {const value = rows[i][isRowObject ? field : fieldIdx];- sample[j][field] = typeof value === 'string' ? value.trim() : value;+ sample[j][field] = toTypeAnalyzerValue(value);j++;i++;} else {@@ -116,7 +131,7 @@ export function getSampleForTypeAnalyzeArrow(sampleIndex++;} else if (notNullorUndefined(getVector(fieldIdx)?.get(rowIndex))) {const value = getVector(fieldIdx)?.get(rowIndex);- sample[sampleIndex][field] = typeof value === 'string' ? value.trim() : value;+ sample[sampleIndex][field] = toTypeAnalyzerValue(value);sampleIndex++;rowIndex++;} else {@@ -230,8 +245,15 @@ export function analyzerTypeToFieldType(aType: string): string {*/export function getFieldsFromData(data: RowData, fieldOrder: string[]): Field[] {// add a check for epoch timestamp+ const analyzerData = data.map(row => {+ const next: Record<string, unknown> = {};+ for (const key of fieldOrder) {+ next[key] = toTypeAnalyzerValue(row[key]);+ }+ return next;+ });const metadata = Analyzer.computeColMeta(- data,+ analyzerData,[{regex: /.*geojson|all_points/g, dataType: 'GEOMETRY'},{regex: /.*census/g, dataType: 'STRING'}@@ -254,9 +276,13 @@ export function getFieldsFromData(data: RowData, fieldOrder: string[]): Field[]// quick check if first valid string in column is H3if (type === AnalyzerDATA_TYPES.STRING) {- for (let i = 0, n = data.length; i < n; ++i) {- if (notNullorUndefined(data[i][name])) {- type = h3IsValid(data[i][name] || '') ? H3_ANALYZER_TYPE : type;+ for (let i = 0, n = analyzerData.length; i < n; ++i) {+ const sampleValue = analyzerData[i][name];+ if (typeof sampleValue === 'string') {+ type = h3IsValid(sampleValue) ? H3_ANALYZER_TYPE : type;+ break;+ }+ if (notNullorUndefined(sampleValue)) {break;}}diff --git a/src/processors/src/data-processor.ts b/src/processors/src/data-processor.tsindex 17b9c011f..3b1cc108f 100644--- a/src/processors/src/data-processor.ts+++ b/src/processors/src/data-processor.ts@@ -585,6 +585,9 @@ const CAST_BIGINTS = false;* Cast 64-bit integer Arrow columns (Int64, Uint64) to Float64 to avoid BigInt values* that are incompatible with d3 scales, sorting, and other numeric operations.* Mirrors the DuckDB approach of casting BIGINT/UBIGINT to DOUBLE.+ *+ * Left off so Uint64 values are not rounded through Float64. Samples are+ * stringified for type-analyzer instead.*/function castBigIntColumnsToFloat64(arrowTable: arrow.Table): arrow.Table {if (!CAST_BIGINTS) {diff --git a/test/node/processors/file-handler-test.js b/test/node/processors/file-handler-test.jsindex 8e6ff7ef0..d734fec8d 100644--- a/test/node/processors/file-handler-test.js+++ b/test/node/processors/file-handler-test.js@@ -396,6 +396,32 @@ test('#file-handler -> processArrowBatches skip compact for incremental loads',t.end();});+test('#file-handler -> processArrowBatches Int64/Uint64 columns', async t => {+ const table = arrow.tableFromArrays({+ hexId: new BigUint64Array([610625465232654335n, 610625465081659391n]),+ total: new BigInt64Array([29436887n, 40685227n])+ });++ let result;+ t.doesNotThrow(() => {+ result = processArrowBatches(table.batches);+ }, 'should process Arrow Int64/Uint64 columns without BigInt TypeError');++ t.equal(result.fields[0].name, 'hexId');+ t.equal(result.fields[0].type, 'integer', 'uint64 columns should stay integer, not h3');+ t.equal(result.fields[1].name, 'total');+ t.equal(result.fields[1].type, 'integer', 'int64 counts should stay integer');++ const processed = await processFileData({+ content: {fileName: 'congo.parquet', data: table.batches},+ fileCache: []+ });+ t.equal(processed.length, 1, 'processFileData should accept uint64 Arrow batches');+ t.equal(processed[0].data.fields[0].type, 'integer');++ t.end();+});+test('#file-handler -> processFileData Feature array is geojson', async t => {const features = [{diff --git a/test/node/utils/data-processor-test.js b/test/node/utils/data-processor-test.jsindex 476a9b821..d31859bf8 100644--- a/test/node/utils/data-processor-test.js+++ b/test/node/utils/data-processor-test.js@@ -134,6 +134,31 @@ test('Processor -> getFieldsFromData', t => {t.end();});+test('Processor -> getFieldsFromData BigInt columns', t => {+ const data = [+ {hexId: 610625465232654335n, total: 29436887n},+ {hexId: 610625465081659391n, total: 40685227n}+ ];++ t.doesNotThrow(+ () => getFieldsFromData(data, ['hexId', 'total']),+ 'should not throw when samples contain BigInt (type-analyzer isNaN)'+ );++ const fields = getFieldsFromData(data, ['hexId', 'total']);+ t.notEqual(+ fields[0].type,+ ALL_FIELD_TYPES.h3,+ 'integer BigInt columns should not be typed as h3'+ );+ t.equal(+ fields[1].type,+ ALL_FIELD_TYPES.integer,+ 'int64 counts within safe range should stay integer'+ );+ t.end();+});+test('Processor -> processCsvData', t => {t.throws(() => processCsvData(''), 'should throw if csv is empty');