From 6f96172e5d7de628158898bf11b459d6508441e1 Mon Sep 17 00:00:00 2001 From: Rusty Conover Date: Thu, 22 Oct 2026 12:55:12 -0400 Subject: [PATCH] fix: preserve native-unit bigint timestamp inputs --- docs/api/data-types.md | 1 + src/util/numbers.js | 10 ++++--- test/timestamp-bigint.test.js | 49 +++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 test/timestamp-bigint.test.js diff --git a/docs/api/data-types.md b/docs/api/data-types.md index 05c2f8b..e3e088f 100644 --- a/docs/api/data-types.md +++ b/docs/api/data-types.md @@ -400,6 +400,7 @@ Create a Timestamp data type instance. Timestamp values are 64-bit signed intege Timestamp values are stored in a `BigInt64Array` and converted to millisecond-based JavaScript `number` values (potentially with fractional digits) upon extraction. An error is raised if a value exceeds either `Number.MIN_SAFE_INTEGER` or `Number.MAX_SAFE_INTEGER`. Pass the `useDate` extraction option (e.g., to [`tableFromIPC`](/flechette/api/#tableFromIPC) or [`tableFromArrays`](/flechette/api/#tableFromArrays)) to instead extract timestamp values as JavaScript `Date` objects. Alternatively, pass the `useBigIntTimestamp` extraction option to extract timestamp values as JavaScript `bigint` (bypass float conversion). +When building timestamp columns, `number` and `Date` inputs represent milliseconds since the epoch and are converted to the column's unit. A `bigint` input already represents the column's unit and is stored without conversion. For example, `1700000000000000001n` in a nanosecond column preserves the final nanosecond. This allows values extracted with `useBigIntTimestamp` to be used as inputs without losing precision. The extraction options control the returned values, not the interpretation of inputs. * *unit* (`number`): The time unit, one of `TimeUnit.SECOND`, `TimeUnit.MILLISECOND` (default), `TimeUnit.MICROSECOND`, or `TimeUnit.NANOSECOND`. * *timezone* (`string`): An optional string for the name of a timezone. If provided, the value should either be a string as used in the Olson timezone database (the "tz database" or "tzdata"), such as "America/New_York", or an absolute timezone offset of the form "+XX:XX" or "-XX:XX", such as "+07:30". Whether a timezone string is present indicates different semantics about the data. That said, Flechette does not process the timezone information. diff --git a/src/util/numbers.js b/src/util/numbers.js index 5567b16..a5e22d8 100644 --- a/src/util/numbers.js +++ b/src/util/numbers.js @@ -59,14 +59,16 @@ export function toDateMillisecond(value) { /** * Return a timestamp conversion method for the given time unit. + * Number inputs use milliseconds; bigint inputs already use the target unit, + * matching values extracted with useBigIntTimestamp. * @param {TimeUnit_} unit The time unit. - * @returns {(value: number) => bigint} The conversion method. + * @returns {(value: number | bigint) => bigint} The conversion method. */ export function toTimestamp(unit) { - return unit === TimeUnit.SECOND ? value => toBigInt(value / 1e3) + return unit === TimeUnit.SECOND ? value => typeof value === 'bigint' ? value : toBigInt(value / 1e3) : unit === TimeUnit.MILLISECOND ? toBigInt - : unit === TimeUnit.MICROSECOND ? value => toBigInt(value * 1e3) - : value => toBigInt(value * 1e6); + : unit === TimeUnit.MICROSECOND ? value => typeof value === 'bigint' ? value : toBigInt(value * 1e3) + : value => typeof value === 'bigint' ? value : toBigInt(value * 1e6); } /** diff --git a/test/timestamp-bigint.test.js b/test/timestamp-bigint.test.js new file mode 100644 index 0000000..efd52b5 --- /dev/null +++ b/test/timestamp-bigint.test.js @@ -0,0 +1,49 @@ +import { describe, it, expect } from 'vitest'; +import { tableFromIPC as arrowTableFromIPC } from 'apache-arrow'; +import { TimeUnit, columnFromArray, columnFromValues, tableFromColumns, tableFromIPC, tableToIPC, timestamp } from '../src/index.js'; + +const options = { useBigIntTimestamp: true, maxBatchRows: 3 }; +const values = [0n, 1n, -1n, null, 1700000000000000001n, -1700000000000000001n, + 9223372036854775807n, -9223372036854775808n]; + +describe('bigint timestamp inputs', () => { + for (const [name, unit, scale] of [ + ['second', TimeUnit.SECOND, 0.001], + ['millisecond', TimeUnit.MILLISECOND, 1], + ['microsecond', TimeUnit.MICROSECOND, 1000], + ['nanosecond', TimeUnit.NANOSECOND, 1000000] + ]) { + it(`preserves native ${name} values across builder batches`, () => { + const type = timestamp(unit, 'UTC'); + expect([...columnFromArray(values, type, options)]).toStrictEqual(values); + expect([...columnFromValues(values.values(), type, options)]).toStrictEqual(values); + const typed = BigInt64Array.from(values.filter(v => v !== null)); + expect([...columnFromArray(typed, type, options)]).toStrictEqual([...typed]); + }); + + it(`keeps number and Date inputs in milliseconds for ${name} columns`, () => { + const input = [1000, new Date(-1000), 1n, null]; + const col = columnFromArray(input, timestamp(unit), options); + expect([...col]).toStrictEqual([BigInt(1000 * scale), BigInt(-1000 * scale), 1n, null]); + expect([...columnFromArray([2n], timestamp(unit))]).toStrictEqual([2 / scale]); + }); + + for (const format of ['stream', 'file']) { + it(`round-trips native ${name} values through IPC ${format}`, () => { + const column = columnFromArray(values, timestamp(unit, 'UTC'), options); + const bytes = tableToIPC(tableFromColumns({ time: column }), { format }); + const decoded = tableFromIPC(bytes, options).getChild('time'); + expect([...decoded]).toStrictEqual(values); + expect(decoded.type.unit).toBe(unit); + expect(decoded.type.timezone).toBe('UTC'); + + // Arrow JS getters convert to milliseconds; inspect its decoded int64 + // buffers to check every stored bit, including sub-millisecond values. + const arrow = arrowTableFromIPC(bytes).getChild('time'); + const raw = arrow.data.flatMap(d => [...d.values.subarray(0, d.length)]); + expect(raw).toStrictEqual(values.map(v => v ?? 0n)); + expect(values.map((_, i) => arrow.isValid(i))).toStrictEqual(values.map(v => v !== null)); + }); + } + } +});