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
+26
View File
@@ -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 () {