mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
fix(node): accept shared and cross-realm ArrayBuffers in table updates (#5325)
`Table.update({ values })` accepts `ArrayBufferLike` in TypeScript, but
passing a `SharedArrayBuffer` to update a binary column throws
`Unsupported value type: object value: ([object SharedArrayBuffer])`. An
ordinary `ArrayBuffer` created in another Node.js VM context also fails
the serializer's `instanceof ArrayBuffer` check.
Use Node's `isAnyArrayBuffer` to recognize both kinds of buffer across
contexts, then pass their bytes through the existing SQL binary literal
encoder.
The table regression writes `00 80 ff` from shared memory and reads the
bytes back on each supported Arrow major (15–21). A separate serializer
test covers an `ArrayBuffer` from another VM context. All eight cases
failed before the fix.
### Validation
- Node 22 on macOS arm64, using the upstream native build from
`0d735c9`: both complete Jest files passed (701 passed, 3 skipped).
- Node SDK Biome checks and strict TypeScript checks for the serializer
and its unit tests passed. The full SDK type check reported the same ten
diagnostics on the base commit and patched tree with this local setup
(optional embedding packages and older native reranker declarations).
This commit is contained in:
@@ -501,6 +501,23 @@ describe.each(arrowVersions)(
|
||||
expect(await table.countRows("id == 10")).toBe(1);
|
||||
});
|
||||
|
||||
it("updates a binary column from a SharedArrayBuffer", async () => {
|
||||
const db = await connect(tmpDir.name);
|
||||
const binaries = await db.createTable(
|
||||
"binaries",
|
||||
new arrow.Table({
|
||||
bytes: arrow.vectorFromArray([Buffer.from([1])], new arrow.Binary()),
|
||||
}),
|
||||
);
|
||||
const shared = new SharedArrayBuffer(3);
|
||||
new Uint8Array(shared).set([0, 128, 255]);
|
||||
|
||||
await binaries.update({ values: { bytes: shared } });
|
||||
|
||||
const [row] = await binaries.query().toArray();
|
||||
expect(Array.from(row.bytes)).toEqual([0, 128, 255]);
|
||||
});
|
||||
|
||||
it("should let me update float values to NaN and Infinity", async () => {
|
||||
const db = await connect(tmpDir.name);
|
||||
const floats = await db.createTable("floats", [
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import { runInNewContext } from "node:vm";
|
||||
import { IntoSql, toSQL } from "../lancedb/util";
|
||||
test.each([
|
||||
["string", "'string'"],
|
||||
@@ -21,6 +22,13 @@ test.each([
|
||||
expect(toSQL(value)).toBe(expected);
|
||||
});
|
||||
|
||||
test("toSQL serializes an ArrayBuffer from another realm", () => {
|
||||
const buffer: ArrayBuffer = runInNewContext(
|
||||
"Uint8Array.of(0, 128, 255).buffer",
|
||||
);
|
||||
expect(toSQL(buffer)).toBe("X'0080ff'");
|
||||
});
|
||||
|
||||
test("toSQL({}) throws on unsupported value type", () => {
|
||||
expect(() => toSQL({} as unknown as IntoSql)).toThrow(
|
||||
"Unsupported value type: object value: ([object Object])",
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import { isAnyArrayBuffer } from "node:util/types";
|
||||
|
||||
export type IntoSql =
|
||||
| string
|
||||
| number
|
||||
@@ -34,7 +36,7 @@ export function toSQL(value: IntoSql): string {
|
||||
return `[${value.map(toSQL).join(", ")}]`;
|
||||
} else if (Buffer.isBuffer(value)) {
|
||||
return `X'${value.toString("hex")}'`;
|
||||
} else if (value instanceof ArrayBuffer) {
|
||||
} else if (isAnyArrayBuffer(value)) {
|
||||
return `X'${Buffer.from(value).toString("hex")}'`;
|
||||
} else {
|
||||
throw new Error(
|
||||
|
||||
Reference in New Issue
Block a user