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;