mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 20:44:00 +00:00
refactor(sdk)!: align Optimize results and data options
BREAKING CHANGE: Replace CompactionResult and SDK compaction option types with OptimizeResult and OptimizeDataOptions/OptimizeDataOverrides, rename compaction_options/compactionOptions to data_options/dataOptions, move the Python result module to lancedb.optimize, and use RewriteDataWithOptions for the Rust action.
This commit is contained in:
@@ -2775,7 +2775,7 @@ describe("remote compaction job results", () => {
|
||||
},
|
||||
async (db) => {
|
||||
const job =
|
||||
await db.openJob<import("../lancedb").CompactionResult>(
|
||||
await db.openJob<import("../lancedb").OptimizeResult>(
|
||||
"compacting-job",
|
||||
);
|
||||
const result = await job.wait();
|
||||
|
||||
@@ -11,9 +11,9 @@ import {
|
||||
AutoQuery,
|
||||
type BlobInput,
|
||||
type BlobMode,
|
||||
type CompactionOptions,
|
||||
Connection,
|
||||
MatchQuery,
|
||||
type OptimizeDataOptions,
|
||||
PhraseQuery,
|
||||
Query,
|
||||
Table,
|
||||
@@ -4122,17 +4122,17 @@ it("passes cleanupOlderThan to the native binding as an absolute timestamp", asy
|
||||
it("forwards per-job compaction options to the native binding", async () => {
|
||||
const optimize = jest.fn().mockResolvedValue({});
|
||||
const table = new LocalTable({ optimize } as never);
|
||||
const compactionOptions = {
|
||||
const dataOptions = {
|
||||
targetRowsPerFragment: 20,
|
||||
maxSourceRows: 25,
|
||||
ioBufferSize: 0,
|
||||
batchSize: 8,
|
||||
};
|
||||
await table.optimize({ compactionOptions });
|
||||
await table.optimize({ dataOptions });
|
||||
expect(optimize).toHaveBeenCalledWith(
|
||||
undefined,
|
||||
undefined,
|
||||
compactionOptions,
|
||||
dataOptions,
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
@@ -4142,11 +4142,11 @@ it("rejects unsupported JavaScript compaction options before native submission",
|
||||
const table = new LocalTable({ optimize } as never);
|
||||
await expect(
|
||||
table.optimize({
|
||||
compactionOptions: {
|
||||
dataOptions: {
|
||||
maxSourceBytes: 1024,
|
||||
} as unknown as CompactionOptions,
|
||||
} as unknown as OptimizeDataOptions,
|
||||
}),
|
||||
).rejects.toThrow("Unsupported compaction option: maxSourceBytes");
|
||||
).rejects.toThrow("Unsupported Optimize data option: maxSourceBytes");
|
||||
expect(optimize).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -4171,7 +4171,7 @@ describe("per-job compaction options", () => {
|
||||
});
|
||||
it("bounds source work and preserves zero I/O buffering", async () => {
|
||||
const stats = await table.optimize({
|
||||
compactionOptions: {
|
||||
dataOptions: {
|
||||
targetRowsPerFragment: 20,
|
||||
maxSourceRows: 25,
|
||||
ioBufferSize: 0,
|
||||
@@ -4186,7 +4186,7 @@ describe("per-job compaction options", () => {
|
||||
expect(defaults.compaction.fragmentsAdded).toBe(1);
|
||||
expect(await table.countRows()).toBe(80);
|
||||
});
|
||||
const invalid: CompactionOptions[] = [
|
||||
const invalid: OptimizeDataOptions[] = [
|
||||
{ targetRowsPerFragment: 0 },
|
||||
{ maxSourceRows: 0 },
|
||||
{ batchSize: 0 },
|
||||
@@ -4198,8 +4198,8 @@ describe("per-job compaction options", () => {
|
||||
{ targetRowsPerFragment: 2 ** 32 },
|
||||
{ batchSize: 2 ** 32 },
|
||||
];
|
||||
it.each(invalid)("rejects invalid settings %p", async (compactionOptions) => {
|
||||
await expect(table.optimize({ compactionOptions })).rejects.toThrow(
|
||||
it.each(invalid)("rejects invalid settings %p", async (dataOptions) => {
|
||||
await expect(table.optimize({ dataOptions })).rejects.toThrow(
|
||||
/safe integer/,
|
||||
);
|
||||
expect(await table.countRows()).toBe(80);
|
||||
@@ -6011,7 +6011,7 @@ describe("durable compaction validation", () => {
|
||||
);
|
||||
await expect(
|
||||
table.optimizeAsync({ unrecognizedLimit: 1 } as never),
|
||||
).rejects.toThrow("Unsupported compaction option");
|
||||
).rejects.toThrow("Unsupported Optimize data option");
|
||||
await expect(
|
||||
table.optimizeAsync({ targetRowsPerFragment: 0 }),
|
||||
).rejects.toThrow("safe integer");
|
||||
@@ -6099,3 +6099,12 @@ describe("Optimize action selection", () => {
|
||||
expect(await table.version()).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
test("rejects removed compactionOptions without silently submitting defaults", async () => {
|
||||
const optimize = jest.fn();
|
||||
const table = new LocalTable({ optimize } as never);
|
||||
await expect(
|
||||
table.optimize({ compactionOptions: { maxSourceRows: 1 } } as never),
|
||||
).rejects.toThrow("Unsupported Optimize option: compactionOptions");
|
||||
expect(optimize).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -175,7 +175,7 @@ export {
|
||||
AddDataOptions,
|
||||
UpdateOptions,
|
||||
OptimizeOptions,
|
||||
CompactionOptions,
|
||||
OptimizeDataOptions,
|
||||
PrewarmDataOptions,
|
||||
Version,
|
||||
WriteProgress,
|
||||
@@ -667,7 +667,7 @@ export {
|
||||
|
||||
export { Listing, ListingOptions } from "./listing";
|
||||
|
||||
export type { CompactionResult } from "./compaction";
|
||||
export type { OptimizeResult } from "./optimize";
|
||||
|
||||
export type {
|
||||
PrewarmDataPlan,
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import { Table as ArrowTable, tableFromIPC } from "apache-arrow";
|
||||
import { decodeCompactionResult } from "./compaction";
|
||||
import { JobFailureInfo, Job as NativeJob } from "./native";
|
||||
import { decodeOptimizeResult } from "./optimize";
|
||||
import { decodePrewarmDataResult } from "./prewarm";
|
||||
|
||||
/** Which of a job's events {@link Job.events} returns. */
|
||||
@@ -122,7 +122,7 @@ export class Job<T = void> {
|
||||
(inner.jobType === "prewarm_page_cache"
|
||||
? (decodePrewarmDataResult as (value: unknown) => T)
|
||||
: inner.jobType === "compact"
|
||||
? (decodeCompactionResult as (value: unknown) => T)
|
||||
? (decodeOptimizeResult as (value: unknown) => T)
|
||||
: undefined);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
/** Actual committed outcome of one remote compaction job. */
|
||||
export interface CompactionResult {
|
||||
/** Actual committed outcome of one remote Optimize data rewrite. */
|
||||
export interface OptimizeResult {
|
||||
/** A bounded run left eligible work for another invocation. */
|
||||
moreWork: boolean;
|
||||
/** Some planned groups were not committed. */
|
||||
@@ -24,7 +24,7 @@ export interface CompactionResult {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function decodeCompactionResult(value: unknown): CompactionResult {
|
||||
export function decodeOptimizeResult(value: unknown): OptimizeResult {
|
||||
// The Rust typed-job decoder validates the wire payload before it reaches JS.
|
||||
const result = value as Record<string, number | boolean | null>;
|
||||
return {
|
||||
+32
-17
@@ -1,7 +1,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import { CompactionResult, decodeCompactionResult } from "./compaction";
|
||||
import { OptimizeResult, decodeOptimizeResult } from "./optimize";
|
||||
import {
|
||||
PrewarmDataPlan,
|
||||
PrewarmDataResult,
|
||||
@@ -187,7 +187,7 @@ export interface PrewarmDataOptions {
|
||||
}
|
||||
|
||||
/** Per-invocation compaction settings. Values must be safe integers. */
|
||||
export interface CompactionOptions {
|
||||
export interface OptimizeDataOptions {
|
||||
/** Positive target rows per output fragment; also affects candidate selection. */
|
||||
targetRowsPerFragment?: number;
|
||||
/** Positive live-source-row budget across whole tasks; an oversized first task can produce no work. */
|
||||
@@ -210,12 +210,12 @@ export interface OptimizeOptions {
|
||||
* Remote tables require a server supporting these SQL options; settings do
|
||||
* not change table policy. This is not a runtime or total-memory limit.
|
||||
* @example
|
||||
* await tbl.optimize({ compactionOptions: {
|
||||
* await tbl.optimize({ dataOptions: {
|
||||
* targetRowsPerFragment: 1000, maxSourceRows: 5000,
|
||||
* ioBufferSize: 64 * 1024 * 1024, batchSize: 128,
|
||||
* } });
|
||||
*/
|
||||
compactionOptions?: CompactionOptions;
|
||||
dataOptions?: OptimizeDataOptions;
|
||||
/**
|
||||
* If set then all versions older than the given date
|
||||
* be removed. The current version will never be removed.
|
||||
@@ -1355,7 +1355,7 @@ export abstract class Table {
|
||||
* Waiting tracks the rewrite itself and returns actual row/file statistics.
|
||||
* Missing result payloads are errors; older workers can omit file counts.
|
||||
* Status, progress, events and cancellation reuse the generic HTTP job APIs.
|
||||
* Reopen with db.openJob<CompactionResult>(job.id!). A wait timeout does not cancel work.
|
||||
* Reopen with db.openJob<OptimizeResult>(job.id!). A wait timeout does not cancel work.
|
||||
*
|
||||
* @example
|
||||
* const job = await table.optimizeAsync({ targetRowsPerFragment: 1000 });
|
||||
@@ -1363,8 +1363,8 @@ export abstract class Table {
|
||||
* console.log(result.rowsSucceeded, result.fragmentsRemoved);
|
||||
*/
|
||||
async optimizeAsync(
|
||||
_options?: CompactionOptions,
|
||||
): Promise<Job<CompactionResult>> {
|
||||
_options?: OptimizeDataOptions,
|
||||
): Promise<Job<OptimizeResult>> {
|
||||
throw new Error("compaction jobs are only supported on remote tables");
|
||||
}
|
||||
|
||||
@@ -1391,8 +1391,8 @@ export abstract class Table {
|
||||
* skipped there instead of being attempted, so this call resolves
|
||||
* successfully with placeholder statistics (not completed work) once the
|
||||
* compaction job has been submitted; it does not wait for that job to finish.
|
||||
* `compactionOptions` can tune the four per-job settings documented in
|
||||
* {@link CompactionOptions}. Older-server errors are propagated without
|
||||
* `dataOptions` can tune the four per-job settings documented in
|
||||
* {@link OptimizeDataOptions}. Older-server errors are propagated without
|
||||
* retrying with the settings removed.
|
||||
*/
|
||||
abstract optimize(options?: Partial<OptimizeOptions>): Promise<OptimizeStats>;
|
||||
@@ -2087,17 +2087,17 @@ export class LocalTable extends Table {
|
||||
}
|
||||
|
||||
async optimizeAsync(
|
||||
options?: CompactionOptions,
|
||||
): Promise<Job<CompactionResult>> {
|
||||
validateCompactionOptions(options);
|
||||
options?: OptimizeDataOptions,
|
||||
): Promise<Job<OptimizeResult>> {
|
||||
validateOptimizeDataOptions(options);
|
||||
return new Job(
|
||||
await this.inner.optimizeAsync(options),
|
||||
decodeCompactionResult,
|
||||
decodeOptimizeResult,
|
||||
);
|
||||
}
|
||||
|
||||
async optimize(options?: Partial<OptimizeOptions>): Promise<OptimizeStats> {
|
||||
validateCompactionOptions(options?.compactionOptions);
|
||||
validateOptimizeOptions(options);
|
||||
if (options?.action !== undefined && options.action !== "rewrite_data") {
|
||||
throw new Error('action must be "rewrite_data" or undefined');
|
||||
}
|
||||
@@ -2113,7 +2113,7 @@ export class LocalTable extends Table {
|
||||
return await this.inner.optimize(
|
||||
options?.cleanupOlderThan?.getTime(),
|
||||
options?.deleteUnverified,
|
||||
options?.compactionOptions,
|
||||
options?.dataOptions,
|
||||
options?.action,
|
||||
);
|
||||
}
|
||||
@@ -2411,7 +2411,22 @@ function rowIdsToBigInts(rowIds: readonly (bigint | number)[]): bigint[] {
|
||||
});
|
||||
}
|
||||
|
||||
function validateCompactionOptions(options?: CompactionOptions): void {
|
||||
function validateOptimizeOptions(options?: Partial<OptimizeOptions>): void {
|
||||
const supported = new Set([
|
||||
"action",
|
||||
"dataOptions",
|
||||
"cleanupOlderThan",
|
||||
"deleteUnverified",
|
||||
]);
|
||||
for (const key of Object.keys(options ?? {})) {
|
||||
if (!supported.has(key)) {
|
||||
throw new Error(`Unsupported Optimize option: ${key}`);
|
||||
}
|
||||
}
|
||||
validateOptimizeDataOptions(options?.dataOptions);
|
||||
}
|
||||
|
||||
function validateOptimizeDataOptions(options?: OptimizeDataOptions): void {
|
||||
// JavaScript callers can supply extra keys that TypeScript would reject.
|
||||
// Refuse them before the native binding can silently discard a work limit.
|
||||
const supported = new Set([
|
||||
@@ -2422,7 +2437,7 @@ function validateCompactionOptions(options?: CompactionOptions): void {
|
||||
]);
|
||||
for (const key of Object.keys(options ?? {})) {
|
||||
if (!supported.has(key)) {
|
||||
throw new Error(`Unsupported compaction option: ${key}`);
|
||||
throw new Error(`Unsupported Optimize data option: ${key}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -574,7 +574,7 @@ impl Connection {
|
||||
))
|
||||
} else if job.job_type().as_deref() == Some("compact") {
|
||||
Ok(crate::job::Job::new_typed(
|
||||
job.into_typed::<lancedb::table::CompactionResult>()
|
||||
job.into_typed::<lancedb::table::OptimizeResult>()
|
||||
.default_error()?,
|
||||
))
|
||||
} else {
|
||||
|
||||
+9
-9
@@ -71,15 +71,15 @@ impl PrewarmDataOptions {
|
||||
|
||||
/// Per-invocation file-compaction settings. All numbers must be safe integers.
|
||||
#[napi(object)]
|
||||
pub struct CompactionOptions {
|
||||
pub struct OptimizeDataOptions {
|
||||
pub target_rows_per_fragment: Option<f64>,
|
||||
pub max_source_rows: Option<f64>,
|
||||
pub io_buffer_size: Option<f64>,
|
||||
pub batch_size: Option<f64>,
|
||||
}
|
||||
|
||||
impl CompactionOptions {
|
||||
fn into_overrides(self) -> napi::Result<lancedb::table::optimize::CompactionOverrides> {
|
||||
impl OptimizeDataOptions {
|
||||
fn into_overrides(self) -> napi::Result<lancedb::table::optimize::OptimizeDataOverrides> {
|
||||
const MAX_SAFE_INTEGER: u64 = 9_007_199_254_740_991;
|
||||
fn integer(
|
||||
value: Option<f64>,
|
||||
@@ -103,7 +103,7 @@ impl CompactionOptions {
|
||||
}
|
||||
Ok(Some(value as u64))
|
||||
}
|
||||
let mut options = lancedb::table::optimize::CompactionOverrides::default();
|
||||
let mut options = lancedb::table::optimize::OptimizeDataOverrides::default();
|
||||
if let Some(target) = integer(
|
||||
self.target_rows_per_fragment,
|
||||
"targetRowsPerFragment",
|
||||
@@ -1004,10 +1004,10 @@ impl Table {
|
||||
#[napi(catch_unwind)]
|
||||
pub async fn optimize_async(
|
||||
&self,
|
||||
options: Option<CompactionOptions>,
|
||||
options: Option<OptimizeDataOptions>,
|
||||
) -> napi::Result<crate::job::Job> {
|
||||
let options = options
|
||||
.map(CompactionOptions::into_overrides)
|
||||
.map(OptimizeDataOptions::into_overrides)
|
||||
.transpose()?
|
||||
.unwrap_or_default();
|
||||
let job = self
|
||||
@@ -1023,7 +1023,7 @@ impl Table {
|
||||
&self,
|
||||
before_timestamp_ms: Option<i64>,
|
||||
delete_unverified: Option<bool>,
|
||||
compaction_options: Option<CompactionOptions>,
|
||||
data_options: Option<OptimizeDataOptions>,
|
||||
action: Option<String>,
|
||||
) -> napi::Result<OptimizeStats> {
|
||||
let rewrite_only = match action.as_deref() {
|
||||
@@ -1052,8 +1052,8 @@ impl Table {
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
let overrides = compaction_options
|
||||
.map(CompactionOptions::into_overrides)
|
||||
let overrides = data_options
|
||||
.map(OptimizeDataOptions::into_overrides)
|
||||
.transpose()?
|
||||
.unwrap_or_default();
|
||||
let stats = if rewrite_only {
|
||||
|
||||
Reference in New Issue
Block a user