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 <DivyamTalwar@users.noreply.github.com>
Co-authored-by: Divyam Talwar <152021174+DivyamTalwar@users.noreply.github.com>
This commit is contained in:
Divyam Talwar
2026-11-30 20:13:11 +08:00
committed by GitHub
co-authored by Divyam Talwar Divyam Talwar
parent e2f6ba1b69
commit 4abb2b282c
3 changed files with 75 additions and 7 deletions
+57
View File
@@ -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<void>((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<void>((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;
+16 -5
View File
@@ -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<NativeQueryType>): 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);
}
+2 -2
View File
@@ -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;
}