fix(node): preserve incoming Arrow schema on overwrite (#5276)

## Summary

- Use the incoming Arrow schema and metadata for Arrow overwrite input,
even if the previous table had automatic embeddings.
- Preserve current schema conversion for append and row-object
overwrite.

- Resolve the incoming schema's embedding provider only when its vector
column needs generation. Compute missing vectors across every Arrow
batch.

## Validation

- Native regression creates an embedding table, verifies that a row
overwrite computes its vector, then replaces the table with a different
Arrow schema and checks its metadata and stored row. The test fails on
main when the SDK passes a null value to the previous embedding
function.
- Embedding Jest suite with the 0.42.0-beta.38 native binding: 15
passed, 1 skipped (requires an OpenAI key). TypeScript compilation with
`--noEmit` and Biome checks passed.

- Reopened two-batch imports preserve both precomputed vectors with no
provider registered and generate vectors for both rows when inputs are
missing. Both regressions failed before the follow-up. Across
`embedding.test.ts` and `arrow.test.ts`, 511 passed and one OpenAI
integration was skipped. TypeScript and Biome passed.

Fixes https://github.com/lancedb/lancedb/issues/5275

---------

Co-authored-by: Divyam Talwar <DivyamTalwar@users.noreply.github.com>
Co-authored-by: Divyam Talwar <194859478+DivyamTalwar@users.noreply.github.com>
This commit is contained in:
Divyam Talwar
2026-12-02 07:06:30 +08:00
committed by GitHub
co-authored by Divyam Talwar Divyam Talwar
parent 3129573909
commit 0abc7d48be
3 changed files with 139 additions and 30 deletions
+110
View File
@@ -13,9 +13,12 @@ import {
Float64,
Int32,
Schema,
Table,
Utf8,
fromDataToBuffer,
makeArrowTable,
tableFromIPC,
tableToIPC,
} from "../lancedb/arrow";
import { EmbeddingFunction, LanceSchema } from "../lancedb/embedding";
import { getRegistry, register } from "../lancedb/embedding/registry";
@@ -139,6 +142,113 @@ describe("embedding functions", () => {
expect(vectorsDefined).toEqual(new Array(5).fill(true));
});
it("uses the incoming Arrow schema when overwriting an embedding table", async () => {
@register("overwrite-schema")
class TestEmbedding extends EmbeddingFunction<string> {
ndims() {
return 1;
}
embeddingDataType() {
return new Float32();
}
async computeSourceEmbeddings(values: string[]) {
return values.map((value) => [value.length]);
}
}
const db = await connect(tmpDir.name);
const embedding = new TestEmbedding();
const schema = LanceSchema({
text: embedding.sourceField(new Utf8()),
vector: embedding.vectorField(),
});
const table = await db.createTable("embeddings", [{ text: "old" }], {
schema,
});
await table.add([{ text: "new row" }], { mode: "overwrite" });
const [embedded] = await table.query().toArray();
expect([...embedded.vector]).toEqual([7]);
expect((await table.schema()).metadata.has("embedding_functions")).toBe(
true,
);
const incoming = makeArrowTable([{ text: "fresh", vector: null }], {
schema,
});
await table.add(incoming, { mode: "overwrite" });
const reopened = await db.openTable("embeddings");
const [fresh] = await reopened.query().toArray();
expect(fresh.text).toBe("fresh");
expect(fresh.vector?.toJSON()).toEqual([5]);
const replacementSchema = new Schema(
[new Field("replacement", new Utf8(), true)],
new Map([["schema-version", "v2"]]),
);
const replacement = makeArrowTable([{ replacement: "new schema" }], {
schema: replacementSchema,
});
await table.add(replacement, { mode: "overwrite" });
expect(await table.schema()).toEqual(replacementSchema);
const rows = await table.query().toArray();
expect(rows.map((row) => row.toJSON())).toEqual([
{ replacement: "new schema" },
]);
});
it.each(["populated", "missing"])(
"overwrites from two Arrow IPC batches with %s vectors",
async (vectors) => {
class ProducerEmbedding extends EmbeddingFunction<string> {
ndims() {
return 2;
}
embeddingDataType() {
return new Float32();
}
async computeSourceEmbeddings(values: string[]) {
return values.map((value) => [value.length, value.length + 1]);
}
}
if (vectors === "missing") {
register()(ProducerEmbedding);
}
const embedding = new ProducerEmbedding();
const schema = LanceSchema({
text: embedding.sourceField(new Utf8()),
vector: embedding.vectorField(),
});
const first = makeArrowTable(
[{ text: "first", vector: vectors === "missing" ? null : [17, 18] }],
{ schema },
);
const second = makeArrowTable(
[{ text: "next", vector: vectors === "missing" ? null : [77, 78] }],
{ schema },
);
const source = new Table(schema, [...first.batches, ...second.batches]);
const incoming = tableFromIPC(tableToIPC(source, "file"));
expect(incoming.batches).toHaveLength(2);
const db = await connect(tmpDir.name);
const table = await db.createTable("arrow_import", [{ previous: true }]);
await table.add(incoming, { mode: "overwrite" });
const reopened = await db.openTable("arrow_import");
expect((await reopened.schema()).metadata).toEqual(schema.metadata);
const rows = await reopened.query().toArray();
expect(rows).toHaveLength(2);
expect(
Object.fromEntries(rows.map((row) => [row.text, [...row.vector]])),
).toEqual(
vectors === "missing"
? { first: [5, 6], next: [4, 5] }
: { first: [17, 18], next: [77, 78] },
);
},
);
it("should be able to create an empty table with an embedding function", async () => {
@register()
class MockEmbeddingFunction extends EmbeddingFunction<string> {