Skip to content

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

Fix Group By date minimum and maximum (#3893)

Igor Dykhtaa8db7161699d
diff --git a/src/reducers/src/dataset-ops-updaters.spec.ts b/src/reducers/src/dataset-ops-updaters.spec.ts
index f32e003bf..f331616f6 100644
--- a/src/reducers/src/dataset-ops-updaters.spec.ts
+++ b/src/reducers/src/dataset-ops-updaters.spec.ts
@@ -101,6 +101,44 @@ describe('dataset ops vis-state', () => {
expect(state.datasets[resultId].dataContainer.numRows()).toBe(2);
});
+ test.each(['minimum', 'maximum'] as const)(
+ 'runGroupBy selects chronological %s from recognized CSV timestamps',
+ technique => {
+ const december = '12/31/2025 10:00:00';
+ const january = '01/01/2026 10:00:00';
+ const data = processCsvData(
+ `group,time\nwest,${december}\nwest,${january}\neast,${january}\neast,${december}\nmissing,`
+ );
+ if (!data) throw new Error('CSV import failed');
+ expect(data.fields[1]).toMatchObject({type: 'timestamp', format: 'M/D/YYYY H:m:s'});
+ const times = makeTable('times', [], []);
+ times.updateSchema(data);
+ let state: VisState = {...INITIAL_VIS_STATE, datasets: {times}};
+ state = reduceVisState(state, VisStateActions.addGroupBy('times'));
+ const op = state.groupBys[0];
+ state = reduceVisState(
+ state,
+ VisStateActions.setGroupByConfig(op.id, {
+ fieldName: 'group',
+ aggregations: {time: technique}
+ })
+ );
+ state = flushCreateTableTasks(reduceVisState(state, VisStateActions.runGroupBy(op.id)));
+ const result = state.datasets[op.resultId];
+ const expected = technique === 'minimum' ? december : january;
+ expect(
+ result.dataContainer.getPlainIndex().map(i => result.dataContainer.rowAsArray(i))
+ ).toEqual([
+ ['west', expected],
+ ['east', expected],
+ ['missing', null]
+ ]);
+ expect(result.fields[1]).toMatchObject({type: 'timestamp', format: data.fields[1].format});
+ expect(times.dataContainer.valueAt(0, 1)).toBe(december);
+ expect(times.dataContainer.valueAt(1, 1)).toBe(january);
+ }
+ );
+
test('delete parent keeps derived child snapshot', () => {
const cities = makeTable(
'cities',
diff --git a/src/table/src/dataset-ops.spec.ts b/src/table/src/dataset-ops.spec.ts
index c2f36e1f4..123320c1d 100644
--- a/src/table/src/dataset-ops.spec.ts
+++ b/src/table/src/dataset-ops.spec.ts
@@ -126,6 +126,73 @@ describe('dataset-ops engine', () => {
expect(byRegion.east).toEqual(['east', 40, 1]);
});
+ const december = '12/31/2025 10:00:00';
+ const january = '01/01/2026 10:00:00';
+ const januaryDate = new Date('2026-01-01T10:00:00Z');
+ test.each([
+ ['timestamp', 'M/D/YYYY H:m:s', [januaryDate, december, january], december, januaryDate],
+ [
+ 'timestamp',
+ 'M/D/YYYY H:m:s',
+ [null, 'invalid', january, december, Infinity],
+ december,
+ january
+ ],
+ ['timestamp', 'M/D/YYYY H:m:s', [null, undefined, 'invalid', NaN, Infinity], null, null],
+ ['timestamp', 'M/D/YYYY H:m:s', [], null, null],
+ [
+ 'timestamp',
+ 'YYYY-MM-DDTHH:mm:ssZ',
+ ['2026-01-01T00:30:00+02:00', '2025-12-31T23:00:00Z'],
+ '2026-01-01T00:30:00+02:00',
+ '2025-12-31T23:00:00Z'
+ ],
+ [
+ 'timestamp',
+ 'YYYY-MM-DDTHH:mm:ssZ',
+ ['2026-01-01T00:00:00+01:00', '2025-12-31T23:00:00Z'],
+ '2026-01-01T00:00:00+01:00',
+ '2026-01-01T00:00:00+01:00'
+ ],
+ ['timestamp', 'x', [1767261600, 1767175200], 1767175200, 1767261600],
+ ['date', 'MM/DD/YYYY', ['01/01/2026', '12/31/2025'], '12/31/2025', '01/01/2026'],
+ ['integer', '', [20, null, 10], 10, 20],
+ ['string', '', ['z', null, 'a'], 'a', 'z']
+ ] as const)(
+ 'Group By extrema preserve %s values with format %s: %j',
+ (type, format, values, minimum, maximum) => {
+ const dataset = new KeplerTable({info: {id: 'times'}, color: [0, 0, 0]});
+ dataset.updateSchema({
+ fields: [
+ {name: 'group', type: 'string', analyzerType: 'string'},
+ {name: 'value', type, analyzerType: type, format}
+ ],
+ rows: values.map(value => ['west', value])
+ });
+ for (const [technique, expected] of [
+ ['minimum', minimum],
+ ['maximum', maximum]
+ ] as const) {
+ const result = groupByDataset(dataset, {
+ fieldName: 'group',
+ aggregations: {value: technique}
+ });
+ expect(result.data.rows).toEqual(values.length ? [['west', expected]] : []);
+ if (values.length) expect(result.data.rows[0][1]).toBe(expected);
+ if (type === 'timestamp' || type === 'date') {
+ expect(result.data.fields[1]).toMatchObject({type, format});
+ const reloaded = new KeplerTable({info: result.info, color: [0, 0, 0]});
+ reloaded.updateSchema(result.data);
+ expect(reloaded.fields[1].format).toBe(format);
+ if (format === 'x') {
+ expect(reloaded.fields[1].valueAccessor({index: 0})).toBe(Number(expected) * 1000);
+ }
+ }
+ }
+ expect(values.map((_, i) => dataset.dataContainer.valueAt(i, 1))).toEqual(values);
+ }
+ );
+
describe.each([new Int64(), new Uint64()])('Arrow %s Group By', arrowType => {
test.each([
[DATASET_OPS_AGGREGATIONS.sum, 30, 80],
diff --git a/src/table/src/dataset-ops/group-by.ts b/src/table/src/dataset-ops/group-by.ts
index 305ef2e05..5cd423fd6 100644
--- a/src/table/src/dataset-ops/group-by.ts
+++ b/src/table/src/dataset-ops/group-by.ts
@@ -4,6 +4,7 @@
import {generateHashId} from '@kepler.gl/common-utils';
import {ALL_FIELD_TYPES} from '@kepler.gl/constants';
import {ProtoDataset} from '@kepler.gl/types';
+import {timeToUnixMilli} from '@kepler.gl/utils';
import {aggregateValues, resultFieldType} from './aggregations';
import {copyFieldAs, makeResultProtoDataset, uniqueColumnName} from './proto-dataset';
@@ -50,20 +51,44 @@ export function groupByDataset(
throw new Error(`Aggregation field "${fieldName}" was not found`);
}
const name = uniqueColumnName(`${fieldName}_${technique}`, usedNames);
+ const isTimeExtremum =
+ (technique === 'minimum' || technique === 'maximum') &&
+ (sourceField.type === ALL_FIELD_TYPES.timestamp || sourceField.type === ALL_FIELD_TYPES.date);
fields.push({
name,
+ ...(isTimeExtremum ? {format: sourceField.format} : {}),
type: resultFieldType(technique, sourceField.type),
analyzerType:
technique === 'count' || technique === 'countUnique'
? ALL_FIELD_TYPES.integer
: sourceField.analyzerType || sourceField.type
});
- return {sourceField, technique};
+ return {sourceField, technique, isTimeExtremum};
});
const rows = Array.from(grouped.values()).map(({key, indexes}) => {
const row: unknown[] = [key];
- for (const {sourceField, technique} of aggMeta) {
+ for (const {sourceField, technique, isTimeExtremum} of aggMeta) {
+ if (isTimeExtremum) {
+ let selectedValue: unknown = null;
+ let selectedTime: number | null = null;
+ for (const index of indexes) {
+ const value = rowValue(dataset, index, sourceField);
+ const time = timeToUnixMilli(value as string | number | Date, sourceField.format || '');
+ if (
+ time !== null &&
+ Number.isFinite(time) &&
+ (selectedTime === null ||
+ (technique === 'minimum' ? time < selectedTime : time > selectedTime))
+ ) {
+ // Compare normalized instants, preserving the first original value on ties.
+ selectedValue = value;
+ selectedTime = time;
+ }
+ }
+ row.push(selectedValue);
+ continue;
+ }
const values = indexes.map(index => rowValue(dataset, index, sourceField));
row.push(aggregateValues(values, technique));
}