From 4abb2b282c2286eed19320f985bc919687b29092 Mon Sep 17 00:00:00 2001 From: Divyam Talwar Date: Mon, 30 Nov 2026 17:43:11 +0530 Subject: [PATCH] fix(node): retain deferred query promise failures until execution (#5280) ## Summary Observe Promise-backed query preparation when the builder stores it, including promises produced by fluent modifiers. Terminal operations still await the original promise and receive its original rejection. ## Validation - Both delayed-execution regressions fail on main. The new tests exercise a plain Promise-backed builder and a builder with chained filters, and check rejection identity. - Native Jest query suite: 43 passed. TypeScript compilation with `--noEmit` and Biome checks passed. - Ran real embedded direct-Promise and forced string-vector queries with Node's strict unhandled rejection mode. Both baseline processes exit before execution. With the fix each process survives the delay, receives the expected error when awaited, and successfully executes a control query. Fixes https://github.com/lancedb/lancedb/issues/5279 --------- Co-authored-by: Divyam Talwar Co-authored-by: Divyam Talwar <152021174+DivyamTalwar@users.noreply.github.com> --- nodejs/__test__/query.test.ts | 57 +++++++++++++++++++++++++++++++++++ nodejs/lancedb/query.ts | 21 ++++++++++--- nodejs/lancedb/table.ts | 4 +-- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/nodejs/__test__/query.test.ts b/nodejs/__test__/query.test.ts index c0b30ac63..a911372ec 100644 --- a/nodejs/__test__/query.test.ts +++ b/nodejs/__test__/query.test.ts @@ -78,6 +78,20 @@ describe("Query outputSchema", () => { expect(schema.fields[1].type.toString()).toBe("Float32"); }); + it.each([false, true])( + "reports a rejected vector when outputSchema is called later (with filters: %s)", + async (withFilters) => { + const failure = new Error("vector preparation failed"); + const query = table.query().nearestTo(Promise.reject(failure)); + if (withFilters) { + query.where("a > 1").limit(1); + } + + await new Promise((resolve) => setImmediate(resolve)); + await expect(query.outputSchema()).rejects.toBe(failure); + }, + ); + it("should return schema for FTS search", async () => { await table.createIndex("text", { config: Index.fts() }); @@ -110,6 +124,49 @@ describe("Query outputSchema", () => { }); }); +describe("vectorSearch with promised additional vectors", () => { + let tmpDir: tmp.DirResult; + let table: Table; + + beforeEach(async () => { + tmpDir = tmp.dirSync({ unsafeCleanup: true }); + const db = await connect(tmpDir.name); + table = await db.createTable("vectors", [ + { label: "first", vector: [1, 2] }, + { label: "second", vector: [3, 4] }, + ]); + }); + + afterEach(() => { + tmpDir.removeCallback(); + }); + + it("rejects with the original error when the promised vector fails", async () => { + const failure = new Error("additional vector rejected"); + const query = table.vectorSearch([[1, 2], Promise.reject(failure)]); + + await new Promise((resolve) => setImmediate(resolve)); + await expect(query.toArray()).rejects.toBe(failure); + }); + + it("returns results for both vectors when the promised vector resolves", async () => { + const results = await table + .vectorSearch([[1, 2], Promise.resolve([3, 4])]) + .limit(1) + .toArray(); + + expect(results).toHaveLength(2); + expect( + results.map(({ query_index, label }) => [query_index, label]), + ).toEqual( + expect.arrayContaining([ + [0, "first"], + [1, "second"], + ]), + ); + }); +}); + describe("Query empty results", () => { let tmpDir: tmp.DirResult; let table: Table; diff --git a/nodejs/lancedb/query.ts b/nodejs/lancedb/query.ts index 7ac58c3bb..51d4ae2d8 100644 --- a/nodejs/lancedb/query.ts +++ b/nodejs/lancedb/query.ts @@ -374,21 +374,32 @@ export class QueryBase< blobContext?: QueryBlobContext, ) { if (inner !== undefined) { - this.inner = inner; + this.setInner(inner); } this.blobContext = blobContext; } + private setInner(inner: NativeQueryType | Promise): void { + this.inner = inner; + if (inner instanceof Promise) { + // Preparation can fail before a terminal call. Keep the original + // rejection available to that call without leaving it unhandled. + void inner.catch(() => undefined); + } + } + // call a function on the inner (either a promise or the actual object) /** * @hidden */ protected doCall(fn: (inner: NativeQueryType) => void) { if (this.inner instanceof Promise) { - this.inner = this.inner.then((inner) => { - fn(inner); - return inner; - }); + this.setInner( + this.inner.then((inner) => { + fn(inner); + return inner; + }), + ); } else { fn(this.inner); } diff --git a/nodejs/lancedb/table.ts b/nodejs/lancedb/table.ts index 3a28a5a9c..1dad81719 100644 --- a/nodejs/lancedb/table.ts +++ b/nodejs/lancedb/table.ts @@ -2113,9 +2113,9 @@ export class LocalTable extends Table { vectorSearch(vector: IntoVector | MultiVector): VectorQuery { if (isMultiVector(vector)) { - const query = this.query().nearestTo(vector[0]); + let query = this.query().nearestTo(vector[0]); for (const v of vector.slice(1)) { - query.addQueryVector(v); + query = query.addQueryVector(v); } return query; }