From 8a00ca58ea3736a7c6ef751ce8485e4b447bc64d Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Butt <168034895+1AbdullahButt@users.noreply.github.com> Date: Mon, 30 Nov 2026 13:49:51 +0500 Subject: [PATCH] fix(nodejs): send Lance time units when altering timestamp or duration columns (#5196) `alterColumns` serializes an Arrow `dataType` with `dataTypeToJson`. For `Timestamp` and `Duration` it wrote apache-arrow's numeric `TimeUnit` value into the type string, so a cast to `new TimestampMicrosecond()` was sent as `timestamp:2:-` and `Duration(MICROSECOND)` as `duration:2`. Lance only parses the units `s`, `ms`, `us` and `ns`, so every such alteration failed with `Invalid data type: {"type":"timestamp:2:-"}`. This maps `TimeUnit` to Lance's unit strings for both types. Tests: - arrow.test.ts: checks `dataTypeToJson` output for all four units of both types, with and without a timezone. It fails on main (`timestamp:0:-`) and passes with the fix. - table.test.ts: "can cast to various types" now also alters timestamp[ms] and duration[ms] columns to microseconds and checks the schema. I couldn't build the native module locally, so the arrow.test.ts run used the released 0.37.1 binary, and the table.test.ts steps were checked as a standalone script against that package rather than under jest. I made this change with help from Claude Code, and I reviewed the diff and the test results myself. --------- Co-authored-by: Xuanwo --- nodejs/__test__/arrow.test.ts | 32 ++++++++++++++++++++++++++++++++ nodejs/__test__/table.test.ts | 26 ++++++++++++++++++++++++++ nodejs/lancedb/arrow.ts | 21 +++++++++++++++++++-- 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/nodejs/__test__/arrow.test.ts b/nodejs/__test__/arrow.test.ts index 27dd14954..1dbb14f5a 100644 --- a/nodejs/__test__/arrow.test.ts +++ b/nodejs/__test__/arrow.test.ts @@ -6,12 +6,16 @@ import * as vm from "node:vm"; import { type ApacheArrow, arrow15, arrowVersions } from "./arrow_versions"; import { + Duration as CurrentDuration, Field as CurrentField, LargeBinary as CurrentLargeBinary, Schema as CurrentSchema, + TimeUnit as CurrentTimeUnit, + Timestamp as CurrentTimestamp, Vector as CurrentVector, convertToTable, tableFromIPC as currentTableFromIPC, + dataTypeToJson, fromBufferToRecordBatch, fromDataToBuffer, fromRecordBatchToBuffer, @@ -38,6 +42,34 @@ it("creates a nullable JSON field with the Arrow extension metadata", () => { ); }); +it("serializes timestamp and duration units the way Lance parses them", () => { + expect(dataTypeToJson(new CurrentTimestamp(CurrentTimeUnit.SECOND))).toEqual({ + type: "timestamp:s:-", + }); + expect( + dataTypeToJson(new CurrentTimestamp(CurrentTimeUnit.MILLISECOND, "UTC")), + ).toEqual({ type: "timestamp:ms:UTC" }); + expect( + dataTypeToJson(new CurrentTimestamp(CurrentTimeUnit.MICROSECOND, "+08:00")), + ).toEqual({ type: "timestamp:us:+08:00" }); + expect( + dataTypeToJson(new CurrentTimestamp(CurrentTimeUnit.NANOSECOND)), + ).toEqual({ type: "timestamp:ns:-" }); + + expect(dataTypeToJson(new CurrentDuration(CurrentTimeUnit.SECOND))).toEqual({ + type: "duration:s", + }); + expect( + dataTypeToJson(new CurrentDuration(CurrentTimeUnit.MILLISECOND)), + ).toEqual({ type: "duration:ms" }); + expect( + dataTypeToJson(new CurrentDuration(CurrentTimeUnit.MICROSECOND)), + ).toEqual({ type: "duration:us" }); + expect( + dataTypeToJson(new CurrentDuration(CurrentTimeUnit.NANOSECOND)), + ).toEqual({ type: "duration:ns" }); +}); + it("allows JSON fields to be non-nullable", () => { expect(makeJsonField("metadata", false).nullable).toBe(false); }); diff --git a/nodejs/__test__/table.test.ts b/nodejs/__test__/table.test.ts index 0971d66f9..f790bb6cc 100644 --- a/nodejs/__test__/table.test.ts +++ b/nodejs/__test__/table.test.ts @@ -2743,6 +2743,32 @@ describe("schema evolution", function () { const schema = new Schema([new Field("val", listType, true)]); expect(await tableLists.schema()).toEqual(schema); } + + // timestamps and durations in another time unit + const tableTimes = await con.createTable( + "times", + [{ ts: new Date(0), dur: 1000n }], + { + schema: new Schema([ + new Field("ts", new arrow.TimestampMillisecond(), true), + new Field( + "dur", + new arrow.Duration(arrow.TimeUnit.MILLISECOND), + true, + ), + ]), + }, + ); + await tableTimes.alterColumns([ + { path: "ts", dataType: new arrow.TimestampMicrosecond() }, + { path: "dur", dataType: new arrow.Duration(arrow.TimeUnit.MICROSECOND) }, + ]); + expect(await tableTimes.schema()).toEqual( + new Schema([ + new Field("ts", new arrow.TimestampMicrosecond(null), true), + new Field("dur", new arrow.Duration(arrow.TimeUnit.MICROSECOND), true), + ]), + ); }); it("can drop a column from the schema", async function () { diff --git a/nodejs/lancedb/arrow.ts b/nodejs/lancedb/arrow.ts index f920eecb8..b8189fa2a 100644 --- a/nodejs/lancedb/arrow.ts +++ b/nodejs/lancedb/arrow.ts @@ -28,6 +28,7 @@ import { RecordBatchStreamWriter, Schema, Struct, + TimeUnit, Timestamp, Type, Utf8, @@ -1634,6 +1635,22 @@ interface JsonField { metadata: Map; } +// Lance spells time units as "s", "ms", "us" and "ns", not as arrow's +// numeric `TimeUnit` enum values. +function timeUnitToJson(unit: TimeUnit): string { + switch (unit) { + case TimeUnit.SECOND: + return "s"; + case TimeUnit.MILLISECOND: + return "ms"; + case TimeUnit.MICROSECOND: + return "us"; + case TimeUnit.NANOSECOND: + return "ns"; + } + throw new Error(`Unsupported time unit: ${unit}`); +} + // Matches format of https://github.com/lancedb/lance/blob/main/rust/lance/src/arrow/json.rs export function dataTypeToJson(dataType: DataType): JsonDataType { switch (dataType.typeId) { @@ -1717,7 +1734,7 @@ export function dataTypeToJson(dataType: DataType): JsonDataType { const timestamp = dataType as Timestamp; const timezone = timestamp.timezone || "-"; return { - type: `timestamp:${timestamp.unit}:${timezone}`, + type: `timestamp:${timeUnitToJson(timestamp.unit)}:${timezone}`, }; } case Type.Decimal: { @@ -1728,7 +1745,7 @@ export function dataTypeToJson(dataType: DataType): JsonDataType { } case Type.Duration: { const duration = dataType as Duration; - return { type: `duration:${duration.unit}` }; + return { type: `duration:${timeUnitToJson(duration.unit)}` }; } case Type.FixedSizeBinary: { const byteWidth = (dataType as FixedSizeBinary).byteWidth;