mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-29 22:13:58 +00:00
feat(nodejs): expose Function failure recovery APIs (#5209)
TypeScript callers can now inspect and recover Function row failures through the native workflow. `functionErrors()` groups returned failures and can show retained attempt history. `functionErrorRows()` looks up current failed inputs using row IDs and physical row addresses represented as `bigint`. Older deployments can continue to use the existing error listing. `retryFailed()` selects the exact failed rows on the server, with optional error type and job filters. `upgradeColumn()` binds an existing immutable Function version and can recompute failed rows, all eligible rows, or no rows. Callers can quarantine and release failures, explicitly include quarantined rows in a retry, and use `functionProvenance()` to see which Function version produced a recorded value. The native operations reuse the existing Rust workflow v1 capability checks. The Function failures guide includes TypeScript examples for inspecting, retrying, upgrading, and quarantining failures.
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import type {
|
||||
FunctionErrorGroup,
|
||||
FunctionErrorRecord,
|
||||
FunctionErrors,
|
||||
FunctionErrorsOptions,
|
||||
RefreshColumnOptions,
|
||||
} from "./native";
|
||||
import type { Table } from "./table";
|
||||
|
||||
/** Options for retrying exactly the current failures of one Function column. */
|
||||
export interface RetryFailedOptions
|
||||
extends Omit<RefreshColumnOptions, "where"> {
|
||||
/** Only retry failures with this exception class. */
|
||||
errorType?: string;
|
||||
/** Only retry current failures recorded by this job. */
|
||||
jobId?: string;
|
||||
/** Explicitly release and retry matching quarantined failures. */
|
||||
includeQuarantined?: boolean;
|
||||
/** Upgrade to this immutable Function version while retrying failures. */
|
||||
functionVersion?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Which existing values to recompute during a Function version upgrade.
|
||||
* `"failed"` selects current failures. `"all"` refreshes every eligible live
|
||||
* row. `"none"` preserves existing values and only changes the binding.
|
||||
*/
|
||||
export type FunctionRecompute = "failed" | "all" | "none";
|
||||
|
||||
/** Options for assigning a new immutable Function version to a column. */
|
||||
export interface UpgradeColumnOptions
|
||||
extends Omit<RefreshColumnOptions, "where"> {
|
||||
/** An existing immutable version of the bound Function. */
|
||||
version: string;
|
||||
/** Defaults to `"all"`. Quarantined failures are excluded. */
|
||||
recompute?: FunctionRecompute;
|
||||
}
|
||||
|
||||
/** The terminal outcome of a Function retry or upgrade job. */
|
||||
export interface RefreshColumnJobResult {
|
||||
/** Logical rows successfully assigned a value. */
|
||||
rowsAssigned: bigint;
|
||||
/** Logical rows whose computation failed. */
|
||||
rowsFailed: bigint;
|
||||
/** Logical rows still needing a value, excluding rows counted in `rowsFailed`. */
|
||||
rowsRemaining: bigint;
|
||||
/** The exact table version read by the job. */
|
||||
sourceVersion: bigint;
|
||||
/** The table version made visible by the job, if it published one. */
|
||||
publishedVersion?: bigint;
|
||||
/** Grouped causes reported by the completed job. */
|
||||
errorGroups: FunctionErrorGroup[];
|
||||
/** Whether the reported cause counts omit any failures. */
|
||||
errorGroupsTruncated: boolean;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function decodeRefreshColumnJobResult(
|
||||
value: unknown,
|
||||
): RefreshColumnJobResult {
|
||||
// The Rust typed job validates the terminal result and preserves u64 values.
|
||||
const result = value as {
|
||||
["rows_assigned"]: number | bigint;
|
||||
["rows_failed"]: number | bigint;
|
||||
["rows_remaining"]: number | bigint;
|
||||
["source_version"]: number | bigint;
|
||||
["published_version"]?: number | bigint;
|
||||
["error_groups"]?: {
|
||||
column: string;
|
||||
["error_type"]: string;
|
||||
["error_message"]: string;
|
||||
["rows_failed"]: number | bigint;
|
||||
}[];
|
||||
["error_groups_truncated"]?: boolean;
|
||||
};
|
||||
return {
|
||||
rowsAssigned: BigInt(result.rows_assigned),
|
||||
rowsFailed: BigInt(result.rows_failed),
|
||||
rowsRemaining: BigInt(result.rows_remaining),
|
||||
sourceVersion: BigInt(result.source_version),
|
||||
publishedVersion:
|
||||
result.published_version === undefined
|
||||
? undefined
|
||||
: BigInt(result.published_version),
|
||||
errorGroups: (result.error_groups ?? []).map((group) => ({
|
||||
column: group.column,
|
||||
errorType: group.error_type,
|
||||
errorMessage: group.error_message,
|
||||
rowsFailed: BigInt(group.rows_failed),
|
||||
})),
|
||||
errorGroupsTruncated: result.error_groups_truncated ?? false,
|
||||
};
|
||||
}
|
||||
|
||||
/** A failed Function-column row and its source data at the table's read snapshot. */
|
||||
export interface FunctionFailedRow {
|
||||
/** Values of the requested source columns. */
|
||||
input: Record<string, unknown>;
|
||||
/** The failure for this row and Function column. */
|
||||
error: FunctionErrorRecord;
|
||||
}
|
||||
|
||||
/** Filters and projection for current failing inputs. */
|
||||
export interface FunctionErrorRowsOptions
|
||||
extends Pick<FunctionErrorsOptions, "column" | "jobId" | "limit"> {
|
||||
/** Source columns to return. Defaults to all source columns. */
|
||||
columns?: string[];
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export async function readFailedRows(
|
||||
table: Table,
|
||||
errors: FunctionErrors,
|
||||
columns?: string[],
|
||||
): Promise<FunctionFailedRow[]> {
|
||||
if ((errors.workflowVersion ?? 0) < 1) {
|
||||
throw new Error(
|
||||
"The server does not support the Function row-failure workflow",
|
||||
);
|
||||
}
|
||||
if (
|
||||
errors.truncated ||
|
||||
errors.fragments.length > 0 ||
|
||||
errors.records.some(
|
||||
(record) => record.rowOffset == null && record.input == null,
|
||||
)
|
||||
) {
|
||||
throw new Error(
|
||||
"Function errors are incomplete; narrow the request or raise limit",
|
||||
);
|
||||
}
|
||||
const { records } = errors;
|
||||
if (records.length === 0) return [];
|
||||
|
||||
const stableIds = new Set<bigint>();
|
||||
const addresses = new Set<bigint>();
|
||||
const keys = records.map((record) => {
|
||||
if (record.input != null) {
|
||||
throw new Error(
|
||||
"Inspect the input source identities for a materialized-view Function error",
|
||||
);
|
||||
}
|
||||
if (typeof record.rowId === "bigint") {
|
||||
stableIds.add(record.rowId);
|
||||
return `_rowid:${record.rowId}`;
|
||||
}
|
||||
if (typeof record.rowAddress === "bigint") {
|
||||
addresses.add(record.rowAddress);
|
||||
return `_rowaddr:${record.rowAddress}`;
|
||||
}
|
||||
throw new Error("This Function error has no source row identity");
|
||||
});
|
||||
const identities = (
|
||||
[
|
||||
["_rowid", stableIds],
|
||||
["_rowaddr", addresses],
|
||||
] as const
|
||||
).filter(([, values]) => values.size > 0);
|
||||
const identityColumns = identities.map(([column]) => column);
|
||||
const selected =
|
||||
columns ?? (await table.schema()).fields.map((field) => field.name);
|
||||
const projection = [...new Set([...selected, ...identityColumns])];
|
||||
const filter = identities
|
||||
.map(([column, values]) => `${column} IN (${[...values].join(",")})`)
|
||||
.join(" OR ");
|
||||
const source = await table
|
||||
.query()
|
||||
.select(projection)
|
||||
.where(filter)
|
||||
.limit(stableIds.size + addresses.size)
|
||||
.toArrow();
|
||||
const selectedNames = new Set(selected);
|
||||
const inputColumns = source.schema.fields
|
||||
.map((field) => field.name)
|
||||
.filter(
|
||||
(name) =>
|
||||
(name !== "_rowid" && name !== "_rowaddr") || selectedNames.has(name),
|
||||
);
|
||||
const byIdentity = new Map<string, Record<string, unknown>>();
|
||||
for (const row of source) {
|
||||
if (row === null) continue;
|
||||
const input = Object.fromEntries(
|
||||
inputColumns.map((name) => [name, row[name]]),
|
||||
);
|
||||
for (const column of identityColumns) {
|
||||
const identity: unknown = row[column];
|
||||
if (typeof identity === "bigint") {
|
||||
byIdentity.set(`${column}:${identity}`, input);
|
||||
}
|
||||
}
|
||||
}
|
||||
return records.map((error, index) => {
|
||||
const input = byIdentity.get(keys[index]);
|
||||
if (input === undefined) {
|
||||
throw new Error(
|
||||
"Some failed inputs are no longer addressable in this snapshot; refresh the error listing",
|
||||
);
|
||||
}
|
||||
return { input, error };
|
||||
});
|
||||
}
|
||||
@@ -63,8 +63,13 @@ export {
|
||||
RefreshColumnResult,
|
||||
FunctionErrors,
|
||||
FunctionErrorsOptions,
|
||||
FunctionErrorGroup,
|
||||
FunctionErrorRecord,
|
||||
FunctionErrorFragment,
|
||||
FunctionProvenance,
|
||||
FunctionProvenanceOptions,
|
||||
FunctionQuarantineOptions,
|
||||
FunctionValueProvenance,
|
||||
RefreshMaterializedViewResult,
|
||||
AlterColumnsResult,
|
||||
UpdateFieldMetadataResult,
|
||||
@@ -111,6 +116,15 @@ export {
|
||||
RenameTableOptions,
|
||||
} from "./connection";
|
||||
|
||||
export type {
|
||||
FunctionErrorRowsOptions,
|
||||
FunctionFailedRow,
|
||||
FunctionRecompute,
|
||||
RefreshColumnJobResult,
|
||||
RetryFailedOptions,
|
||||
UpgradeColumnOptions,
|
||||
} from "./function_recovery";
|
||||
|
||||
export { JobFailureInfo, JobInfo, Session } from "./native.js";
|
||||
|
||||
export {
|
||||
|
||||
+134
-12
@@ -41,6 +41,15 @@ import {
|
||||
runWithSignal,
|
||||
} from "./blob";
|
||||
import { EmbeddingFunctionConfig, getRegistry } from "./embedding/registry";
|
||||
import {
|
||||
type FunctionErrorRowsOptions,
|
||||
type FunctionFailedRow,
|
||||
type RefreshColumnJobResult,
|
||||
type RetryFailedOptions,
|
||||
type UpgradeColumnOptions,
|
||||
decodeRefreshColumnJobResult,
|
||||
readFailedRows,
|
||||
} from "./function_recovery";
|
||||
import { IndexOptions } from "./indices";
|
||||
import { Job } from "./job";
|
||||
import { validateNonNegativeInteger } from "./materialized_view";
|
||||
@@ -55,6 +64,9 @@ import {
|
||||
DropColumnsResult,
|
||||
FunctionErrors,
|
||||
FunctionErrorsOptions,
|
||||
FunctionProvenance,
|
||||
FunctionProvenanceOptions,
|
||||
FunctionQuarantineOptions,
|
||||
IndexConfig,
|
||||
IndexStatistics,
|
||||
JobFailureInfo,
|
||||
@@ -1001,26 +1013,87 @@ export abstract class Table {
|
||||
): Promise<Job>;
|
||||
|
||||
/**
|
||||
* The per-row errors Function refreshes recorded on this table.
|
||||
* List the per-row errors recorded by Function refreshes on this table.
|
||||
*
|
||||
* On LanceDB Enterprise with workflow version 1 or later, the default
|
||||
* lists current failures. Set `history: true` to include retained attempts
|
||||
* for repaired rows. Older deployments keep their existing default listing.
|
||||
* Reading errors requires table read access because messages can include the
|
||||
* input that failed. `errorGroups` describes the returned records.
|
||||
*
|
||||
* A refresh running under a skip policy records each row it skipped with
|
||||
* the input that failed and the error. This lists those records, newest
|
||||
* job first, plus a summary for any fragment whose per-row detail was
|
||||
* capped. LanceDB Enterprise only; reading errors needs read
|
||||
* access to the table, since a message carries the value that failed.
|
||||
* @param {FunctionErrorsOptions} options Optional filters: `jobId`,
|
||||
* `column`, and `limit` (server default 10000, cap 100000).
|
||||
* @returns {Promise<FunctionErrors>} The records, the capped fragments,
|
||||
* and whether the listing stopped at its limit.
|
||||
* @example
|
||||
* ```ts
|
||||
* const { records, truncated } = await table.functionErrors({ column: "embedding" });
|
||||
* const errors = await table.functionErrors({ column: "embedding" });
|
||||
* console.log(errors.records, errors.errorGroups);
|
||||
* ```
|
||||
*/
|
||||
abstract functionErrors(
|
||||
options?: FunctionErrorsOptions,
|
||||
): Promise<FunctionErrors>;
|
||||
|
||||
/**
|
||||
* Read current failing inputs, one result per error record.
|
||||
* Requires a complete LanceDB Enterprise workflow version 1 listing with
|
||||
* row identities. Throws if a failed row is no longer addressable in the
|
||||
* table's read snapshot. For materialized views, see the source identities
|
||||
* on each error record returned by {@link Table#functionErrors}.
|
||||
*/
|
||||
async functionErrorRows(
|
||||
options?: FunctionErrorRowsOptions,
|
||||
): Promise<FunctionFailedRow[]> {
|
||||
const { columns, column, jobId, limit } = options ?? {};
|
||||
const errors = await this.functionErrors({ column, jobId, limit });
|
||||
return readFailedRows(this, errors, columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a job to retry exactly the current failures of one Function column.
|
||||
* Filters apply on the server. Quarantined rows are excluded by default.
|
||||
* Requires LanceDB Enterprise Function failure workflow version 1 or later.
|
||||
*/
|
||||
async retryFailed(
|
||||
_column: string,
|
||||
_options?: RetryFailedOptions,
|
||||
): Promise<Job<RefreshColumnJobResult>> {
|
||||
throw new Error("retryFailed is not supported on this table type");
|
||||
}
|
||||
|
||||
/**
|
||||
* Start an upgrade to an existing immutable Function version and choose
|
||||
* which rows to recompute. New and changed inputs use the new version.
|
||||
* Requires LanceDB Enterprise Function failure workflow version 1 or later.
|
||||
*/
|
||||
async upgradeColumn(
|
||||
_column: string,
|
||||
_options: UpgradeColumnOptions,
|
||||
): Promise<Job<RefreshColumnJobResult>> {
|
||||
throw new Error("upgradeColumn is not supported on this table type");
|
||||
}
|
||||
|
||||
/**
|
||||
* Quarantine current failures and return the number of affected rows.
|
||||
* Set `quarantined: false` to release them for future refreshes.
|
||||
* Requires LanceDB Enterprise Function failure workflow version 1 or later.
|
||||
*/
|
||||
async quarantineFailed(
|
||||
_column: string,
|
||||
_options?: FunctionQuarantineOptions,
|
||||
): Promise<number> {
|
||||
throw new Error("quarantineFailed is not supported on this table type");
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the producing Function version and job for each returned value.
|
||||
* Check `truncated` before treating the result as a complete listing.
|
||||
* Requires LanceDB Enterprise Function failure workflow version 1 or later.
|
||||
*/
|
||||
async functionProvenance(
|
||||
_column: string,
|
||||
_options?: FunctionProvenanceOptions,
|
||||
): Promise<FunctionProvenance> {
|
||||
throw new Error("functionProvenance is not supported on this table type");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return exact freshness status for Function-backed output columns.
|
||||
*
|
||||
@@ -2012,7 +2085,56 @@ export class LocalTable extends Table {
|
||||
options?: FunctionErrorsOptions,
|
||||
): Promise<FunctionErrors> {
|
||||
validateNonNegativeInteger(options?.limit, "limit");
|
||||
return await this.inner.functionErrors(options);
|
||||
return this.inner.functionErrors(options);
|
||||
}
|
||||
|
||||
override async retryFailed(
|
||||
column: string,
|
||||
options?: RetryFailedOptions,
|
||||
): Promise<Job<RefreshColumnJobResult>> {
|
||||
const {
|
||||
errorType,
|
||||
jobId,
|
||||
includeQuarantined,
|
||||
functionVersion,
|
||||
...refresh
|
||||
} = options ?? {};
|
||||
const job = await this.inner.refreshColumnAsync(column, refresh, {
|
||||
retryErrors: true,
|
||||
retryErrorType: errorType,
|
||||
retryJobId: jobId,
|
||||
includeQuarantined,
|
||||
functionVersion,
|
||||
recompute: functionVersion === undefined ? undefined : "failed",
|
||||
});
|
||||
return new Job(job, decodeRefreshColumnJobResult);
|
||||
}
|
||||
|
||||
override async upgradeColumn(
|
||||
column: string,
|
||||
options: UpgradeColumnOptions,
|
||||
): Promise<Job<RefreshColumnJobResult>> {
|
||||
const { version, recompute = "all", ...refresh } = options;
|
||||
const job = await this.inner.refreshColumnAsync(column, refresh, {
|
||||
functionVersion: version,
|
||||
recompute,
|
||||
});
|
||||
return new Job(job, decodeRefreshColumnJobResult);
|
||||
}
|
||||
|
||||
override async quarantineFailed(
|
||||
column: string,
|
||||
options?: FunctionQuarantineOptions,
|
||||
): Promise<number> {
|
||||
return this.inner.quarantineFunctionErrors(column, options);
|
||||
}
|
||||
|
||||
override async functionProvenance(
|
||||
column: string,
|
||||
options?: FunctionProvenanceOptions,
|
||||
): Promise<FunctionProvenance> {
|
||||
validateNonNegativeInteger(options?.limit, "limit");
|
||||
return this.inner.functionProvenance(column, options);
|
||||
}
|
||||
|
||||
async functionColumnStatuses(
|
||||
|
||||
Reference in New Issue
Block a user