Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 6 additions & 4 deletions src/util/numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
49 changes: 49 additions & 0 deletions test/timestamp-bigint.test.js
Original file line number Diff line number Diff line change
@@ -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));
});
}
}
});
Loading