mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
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:
co-authored by
Xuanwo
parent
9a79107651
commit
8a00ca58ea
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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 () {
|
||||
|
||||
+19
-2
@@ -28,6 +28,7 @@ import {
|
||||
RecordBatchStreamWriter,
|
||||
Schema,
|
||||
Struct,
|
||||
TimeUnit,
|
||||
Timestamp,
|
||||
Type,
|
||||
Utf8,
|
||||
@@ -1634,6 +1635,22 @@ interface JsonField {
|
||||
metadata: Map<string, string>;
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
Reference in New Issue
Block a user