mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
fix(nodejs): terminate delta readers on schema errors
Route native schema export and Arrow IPC decode failures through the same terminal cleanup as polling, without reopening the reader or repeating release. Add the Table.delta stream example and focused lifecycle tests.
This commit is contained in:
@@ -17,6 +17,7 @@ import {
|
||||
PhraseQuery,
|
||||
Query,
|
||||
Table,
|
||||
TableDelta,
|
||||
TableDeltaReader,
|
||||
VectorQuery,
|
||||
blob,
|
||||
@@ -4296,6 +4297,50 @@ describe("when dealing with table delta", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("routes schema and Arrow decode failures through terminal cleanup", async () => {
|
||||
let schemaOpens = 0;
|
||||
const schemaFailure = new TableDelta({
|
||||
insertedRows: async () => {
|
||||
schemaOpens += 1;
|
||||
return {
|
||||
schema: async () => {
|
||||
throw new Error("schema export failed");
|
||||
},
|
||||
next: async () => {
|
||||
throw new Error("reader must not poll after schema failure");
|
||||
},
|
||||
};
|
||||
},
|
||||
} as never);
|
||||
|
||||
const schemaReader = schemaFailure.insertedRows();
|
||||
await expect(schemaReader.schema()).rejects.toThrow("schema export failed");
|
||||
await expect(schemaReader.next()).rejects.toThrow("schema export failed");
|
||||
expect(schemaOpens).toBe(1);
|
||||
|
||||
const nextReader = schemaFailure.insertedRows();
|
||||
await expect(nextReader.next()).rejects.toThrow("schema export failed");
|
||||
expect(schemaOpens).toBe(2);
|
||||
|
||||
let decodeOpens = 0;
|
||||
const decodeFailure = new TableDelta({
|
||||
insertedRows: async () => {
|
||||
decodeOpens += 1;
|
||||
return {
|
||||
schema: async () => Buffer.from("not an Arrow IPC schema"),
|
||||
next: async () => {
|
||||
throw new Error("reader must not poll after decode failure");
|
||||
},
|
||||
};
|
||||
},
|
||||
} as never);
|
||||
|
||||
const decodeReader = decodeFailure.insertedRows();
|
||||
await expect(decodeReader.schema()).rejects.toThrow();
|
||||
await expect(decodeReader.next()).rejects.toThrow();
|
||||
expect(decodeOpens).toBe(1);
|
||||
});
|
||||
|
||||
it("uses one reader cursor and releases it on early drop", async () => {
|
||||
const conn = await connect(tmpDir.name);
|
||||
const table = await conn.createTable("delta_reader", [{ id: 1 }], {
|
||||
|
||||
+27
-10
@@ -1408,6 +1408,15 @@ export abstract class Table {
|
||||
* full rescan and reinitialize it at the range end version.
|
||||
*
|
||||
* LanceDB Enterprise tables reject this operation.
|
||||
*
|
||||
* @example
|
||||
* const delta = await table.delta("baseline");
|
||||
* const reader = delta.insertedRows();
|
||||
* console.log((await reader.schema()).fields.map((field) => field.name));
|
||||
* for await (const batch of reader) {
|
||||
* console.log(batch.getChild("id")?.toJSON());
|
||||
* }
|
||||
* console.log(await delta.listTransactions());
|
||||
*/
|
||||
async delta(
|
||||
// biome-ignore lint/correctness/noUnusedVariables: retain the public parameter name in generated docs
|
||||
@@ -2547,16 +2556,22 @@ class TableDeltaReaderImpl implements TableDeltaReader {
|
||||
throw this.#error;
|
||||
}
|
||||
if (this.#schemaSetup === undefined) {
|
||||
this.#schemaSetup = (async () => {
|
||||
const native = await this.#getNative();
|
||||
const schema = tableFromIPC(await native.schema()).schema;
|
||||
this.#schema = schema;
|
||||
return schema;
|
||||
})();
|
||||
this.#schemaSetup = this.#loadSchema();
|
||||
}
|
||||
return this.#schemaSetup;
|
||||
}
|
||||
|
||||
async #loadSchema(): Promise<Schema> {
|
||||
try {
|
||||
const native = await this.#getNative();
|
||||
const schema = tableFromIPC(await native.schema()).schema;
|
||||
this.#schema = schema;
|
||||
return schema;
|
||||
} catch (error) {
|
||||
return this.#fail(error);
|
||||
}
|
||||
}
|
||||
|
||||
next(): Promise<IteratorResult<RecordBatch>> {
|
||||
return this.#serialize(() => this.#pull());
|
||||
}
|
||||
@@ -2630,10 +2645,12 @@ class TableDeltaReaderImpl implements TableDeltaReader {
|
||||
}
|
||||
|
||||
async #fail(error: unknown): Promise<never> {
|
||||
this.#done = true;
|
||||
this.#error = error;
|
||||
await this.#release();
|
||||
throw error;
|
||||
if (this.#error === undefined) {
|
||||
this.#done = true;
|
||||
this.#error = error;
|
||||
await this.#release();
|
||||
}
|
||||
throw this.#error;
|
||||
}
|
||||
|
||||
async #release(): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user