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 <github@xuanwo.io>
This commit is contained in:
Muhammad Abdullah Butt
2026-11-30 16:49:51 +08:00
committed by GitHub
co-authored by Xuanwo
parent 9a79107651
commit 8a00ca58ea
3 changed files with 77 additions and 2 deletions
+32
View File
@@ -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);
});