Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

fix(schemas): preserve Arrow integers and null timestamps when saving (#3874)

Igor Dykhta793f7ca9c3f1
diff --git a/src/schemas/src/dataset-schema.ts b/src/schemas/src/dataset-schema.ts
index 829c568c9..d076cd3e7 100644
--- a/src/schemas/src/dataset-schema.ts
+++ b/src/schemas/src/dataset-schema.ts
@@ -112,6 +112,7 @@ export const propertiesV1 = {
* Prepare a data container for export as part of json / html files.
* 1) Arrow tables can store Timestamps as BigInts, so convert numbers to ISOStrings compatible with Kepler.gl's TIMESTAMP.
* 2) Geoarrow binary buffers converted to hex wkb
+ * 3) Int64 values outside the safe integer range saved as exact decimal strings
* @param dataContainer A data container to flatten.
* @returns Row based data.
*/
@@ -131,7 +132,16 @@ const getAllDataForSaving = (dataContainer: DataContainerInterface): any[][] =>
arrow.DataType.isTime(column.type)
) {
allData.forEach(row => {
- row[columnIndex] = new Date(row[columnIndex]).toISOString();
+ const value = row[columnIndex];
+ row[columnIndex] = value == null ? null : new Date(value).toISOString();
+ });
+ } else if (arrow.DataType.isInt(column.type) && column.type.bitWidth === 64) {
+ allData.forEach(row => {
+ const value = row[columnIndex];
+ if (typeof value === 'bigint') {
+ const number = Number(value);
+ row[columnIndex] = Number.isSafeInteger(number) ? number : value.toString();
+ }
});
} else if (field?.type === ALL_FIELD_TYPES.geoarrow) {
const formatter = FIELD_DISPLAY_FORMAT[ALL_FIELD_TYPES.geoarrow];
diff --git a/test/node/schemas/dataset-schema-test.js b/test/node/schemas/dataset-schema-test.js
index 73ade93a1..7d095082d 100644
--- a/test/node/schemas/dataset-schema-test.js
+++ b/test/node/schemas/dataset-schema-test.js
@@ -4,7 +4,11 @@
import test from 'tape';
import cloneDeep from 'es-toolkit/compat/cloneDeep';
import SchemaManager, {datasetSchema, VERSIONS} from '@kepler.gl/schemas';
-import {DatasetType} from '@kepler.gl/constants';
+import {DatasetType, DATASET_FORMATS} from '@kepler.gl/constants';
+import * as arrow from 'apache-arrow';
+import {KeplerTable} from '@kepler.gl/table';
+import {exportToJsonString, exportMapToHTML, validateInputData} from '@kepler.gl/utils';
+import {processArrowTable, processKeplerglJSON} from '@kepler.gl/processors';
// fixtures
import {
@@ -269,3 +273,100 @@ test('#DatasetSchema -> save externally-hosted dataset custom refresh interval',
t.end();
});
+
+function arrowDataset(columns) {
+ const table = new arrow.Table(columns);
+ const data = processArrowTable({shape: 'arrow-table', data: table});
+ return new KeplerTable({
+ color: [1, 2, 3],
+ metadata: {format: DATASET_FORMATS.arrow}
+ }).updateSchema(data);
+}
+
+function reloadDataset(saved) {
+ const map = {datasets: [{version: 'v1', data: saved}], config: {version: 'v1', config: {}}};
+ const parsed = processKeplerglJSON(JSON.parse(exportToJsonString(map)));
+ return new KeplerTable({color: [1, 2, 3]}).updateSchema(
+ validateInputData(parsed.datasets[0].data)
+ );
+}
+
+test('#DatasetSchema -> Int64 values survive JSON, HTML and reload without precision loss', t => {
+ const dataset = arrowDataset({
+ signed: arrow.vectorFromArray(
+ [42n, 0n, 9007199254740991n, -9223372036854775808n, null],
+ new arrow.Int64()
+ ),
+ unsigned: arrow.vectorFromArray(
+ [42n, 0n, 9007199254740992n, 18446744073709551615n, null],
+ new arrow.Uint64()
+ )
+ });
+ const originalRows = dataset.dataContainer.flattenData();
+ const saved = datasetSchema[VERSIONS.v1].save(dataset);
+ const expected = [
+ [42, 42],
+ [0, 0],
+ [9007199254740991, '9007199254740992'],
+ ['-9223372036854775808', '18446744073709551615'],
+ [null, null]
+ ];
+ t.deepEqual(
+ saved.allData,
+ expected,
+ 'uses numbers in the safe range and exact decimal strings outside it'
+ );
+ t.doesNotThrow(() => {
+ const reloaded = reloadDataset(saved);
+ t.deepEqual(reloaded.dataContainer.flattenData(), expected, 'JSON reload retains exact values');
+ t.deepEqual(
+ datasetSchema[VERSIONS.v1].save(reloaded).allData,
+ expected,
+ 'resaving retains exact values'
+ );
+ }, 'JSON export is valid and reloads');
+ t.doesNotThrow(() => {
+ const html = exportMapToHTML({
+ datasets: [{version: 'v1', data: saved}],
+ config: {version: 'v1', config: {}}
+ });
+ const embedded = JSON.parse(html.match(/const datasets = (.*);/)[1]);
+ t.deepEqual(
+ reloadDataset(embedded[0].data).dataContainer.flattenData(),
+ expected,
+ 'HTML embedded data reloads exactly'
+ );
+ }, 'HTML export succeeds');
+ t.deepEqual(
+ dataset.dataContainer.flattenData(),
+ originalRows,
+ 'saving does not mutate Arrow values'
+ );
+ t.end();
+});
+
+test('#DatasetSchema -> null Arrow timestamps remain null through save and reload', t => {
+ const dataset = arrowDataset({
+ time: arrow.vectorFromArray([null, 0, 1700000000123], new arrow.TimestampMillisecond())
+ });
+ const saved = datasetSchema[VERSIONS.v1].save(dataset);
+ const expected = [[null], ['1970-01-01T00:00:00.000Z'], ['2023-11-14T22:13:20.123Z']];
+ t.deepEqual(saved.allData, expected, 'distinguishes missing timestamps from valid epoch zero');
+ const reloaded = reloadDataset(saved);
+ t.deepEqual(
+ reloaded.dataContainer.flattenData(),
+ expected,
+ 'JSON reload preserves missing and nonmissing values'
+ );
+ t.deepEqual(
+ datasetSchema[VERSIONS.v1].save(reloaded).allData,
+ expected,
+ 'resaving preserves nulls'
+ );
+ t.deepEqual(
+ dataset.dataContainer.flattenData(),
+ [[null], [0], [1700000000123]],
+ 'saving leaves source values unchanged'
+ );
+ t.end();
+});