mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
refactor(sdk)!: flatten Optimize options across bindings
BREAKING CHANGE: Pass Optimize settings as Python keyword arguments or one flat TypeScript OptimizeOptions object. Remove data_options/dataOptions wrappers and OptimizeDataOptions, accept Rust OptimizeOptions directly, retain OptimizeOverrides for field presence, and rename index-maintenance options to IndexOptimizeOptions.
This commit is contained in:
@@ -13,7 +13,7 @@ import {
|
||||
type BlobMode,
|
||||
Connection,
|
||||
MatchQuery,
|
||||
type OptimizeDataOptions,
|
||||
type OptimizeOptions,
|
||||
PhraseQuery,
|
||||
Query,
|
||||
Table,
|
||||
@@ -4128,7 +4128,7 @@ it("forwards per-job compaction options to the native binding", async () => {
|
||||
ioBufferSize: 0,
|
||||
batchSize: 8,
|
||||
};
|
||||
await table.optimize({ dataOptions });
|
||||
await table.optimize({ ...dataOptions });
|
||||
expect(optimize).toHaveBeenCalledWith(
|
||||
undefined,
|
||||
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({
|
||||
dataOptions: {
|
||||
...({
|
||||
maxSourceBytes: 1024,
|
||||
} as unknown as OptimizeDataOptions,
|
||||
} as unknown as OptimizeOptions),
|
||||
}),
|
||||
).rejects.toThrow("Unsupported Optimize data option: maxSourceBytes");
|
||||
).rejects.toThrow("Unsupported Optimize option: maxSourceBytes");
|
||||
expect(optimize).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -4171,12 +4171,10 @@ describe("per-job compaction options", () => {
|
||||
});
|
||||
it("bounds source work and preserves zero I/O buffering", async () => {
|
||||
const stats = await table.optimize({
|
||||
dataOptions: {
|
||||
targetRowsPerFragment: 20,
|
||||
maxSourceRows: 25,
|
||||
ioBufferSize: 0,
|
||||
batchSize: 8,
|
||||
},
|
||||
targetRowsPerFragment: 20,
|
||||
maxSourceRows: 25,
|
||||
ioBufferSize: 0,
|
||||
batchSize: 8,
|
||||
});
|
||||
expect(stats.compaction.fragmentsRemoved).toBe(2);
|
||||
expect(stats.compaction.fragmentsAdded).toBe(1);
|
||||
@@ -4186,7 +4184,7 @@ describe("per-job compaction options", () => {
|
||||
expect(defaults.compaction.fragmentsAdded).toBe(1);
|
||||
expect(await table.countRows()).toBe(80);
|
||||
});
|
||||
const invalid: OptimizeDataOptions[] = [
|
||||
const invalid: OptimizeOptions[] = [
|
||||
{ targetRowsPerFragment: 0 },
|
||||
{ maxSourceRows: 0 },
|
||||
{ batchSize: 0 },
|
||||
@@ -4199,7 +4197,7 @@ describe("per-job compaction options", () => {
|
||||
{ batchSize: 2 ** 32 },
|
||||
];
|
||||
it.each(invalid)("rejects invalid settings %p", async (dataOptions) => {
|
||||
await expect(table.optimize({ dataOptions })).rejects.toThrow(
|
||||
await expect(table.optimize({ ...dataOptions })).rejects.toThrow(
|
||||
/safe integer/,
|
||||
);
|
||||
expect(await table.countRows()).toBe(80);
|
||||
@@ -6011,7 +6009,7 @@ describe("durable compaction validation", () => {
|
||||
);
|
||||
await expect(
|
||||
table.optimizeAsync({ unrecognizedLimit: 1 } as never),
|
||||
).rejects.toThrow("Unsupported Optimize data option");
|
||||
).rejects.toThrow("Unsupported Optimize option");
|
||||
await expect(
|
||||
table.optimizeAsync({ targetRowsPerFragment: 0 }),
|
||||
).rejects.toThrow("safe integer");
|
||||
@@ -6108,3 +6106,38 @@ test("rejects removed compactionOptions without silently submitting defaults", a
|
||||
).rejects.toThrow("Unsupported Optimize option: compactionOptions");
|
||||
expect(optimize).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("uses the same flat OptimizeOptions for durable submission", async () => {
|
||||
const optimizeAsync = jest.fn().mockResolvedValue({});
|
||||
const table = new LocalTable({ optimizeAsync } as never);
|
||||
await table.optimizeAsync({
|
||||
targetRowsPerFragment: 100,
|
||||
ioBufferSize: 0,
|
||||
action: "rewrite_data",
|
||||
});
|
||||
expect(optimizeAsync).toHaveBeenCalledWith({
|
||||
targetRowsPerFragment: 100,
|
||||
ioBufferSize: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it.each([{ cleanupOlderThan: new Date() }, { deleteUnverified: true }])(
|
||||
"rejects unsupported durable maintenance before submission: %p",
|
||||
async (options) => {
|
||||
const optimizeAsync = jest.fn();
|
||||
const table = new LocalTable({ optimizeAsync } as never);
|
||||
await expect(table.optimizeAsync(options)).rejects.toThrow(
|
||||
"unsupported by Optimize jobs",
|
||||
);
|
||||
expect(optimizeAsync).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
it("rejects the removed dataOptions wrapper before submission", async () => {
|
||||
const optimize = jest.fn();
|
||||
const table = new LocalTable({ optimize } as never);
|
||||
await expect(
|
||||
table.optimize({ dataOptions: { maxSourceRows: 1 } } as never),
|
||||
).rejects.toThrow("Unsupported Optimize option: dataOptions");
|
||||
expect(optimize).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -175,7 +175,6 @@ export {
|
||||
AddDataOptions,
|
||||
UpdateOptions,
|
||||
OptimizeOptions,
|
||||
OptimizeDataOptions,
|
||||
PrewarmDataOptions,
|
||||
Version,
|
||||
WriteProgress,
|
||||
|
||||
+60
-94
@@ -186,62 +186,25 @@ export interface PrewarmDataOptions {
|
||||
concurrency?: number;
|
||||
}
|
||||
|
||||
/** Per-invocation compaction settings. Values must be safe integers. */
|
||||
export interface OptimizeDataOptions {
|
||||
/** Positive target rows per output fragment; also affects candidate selection. */
|
||||
/** Flat Optimize settings for submission and durable jobs.
|
||||
* Remote jobs support data rewrites; cleanup options are Embedded-only. */
|
||||
export interface OptimizeOptions {
|
||||
/** Positive target rows per output fragment. Omitted remote values inherit table policy. */
|
||||
targetRowsPerFragment?: number;
|
||||
/** Positive live-source-row budget across whole tasks; an oversized first task can produce no work. */
|
||||
/** Positive live-source-row budget across whole rewrite tasks. */
|
||||
maxSourceRows?: number;
|
||||
/** Scanner I/O read-ahead budget in bytes; zero disables byte-based I/O backpressure. */
|
||||
/** Scanner read-ahead budget in bytes; zero disables byte-based backpressure. */
|
||||
ioBufferSize?: number;
|
||||
/** Positive scanner batch size in rows. */
|
||||
batchSize?: number;
|
||||
}
|
||||
|
||||
export interface OptimizeOptions {
|
||||
/** Run only data compaction, preserving versions and skipping separate index
|
||||
* maintenance. Omit for Embedded's full maintenance cycle. Cannot be combined
|
||||
* with cleanup options. Enterprise always submits data compaction only. */
|
||||
/** Run only data rewriting, retaining history and skipping separate index maintenance. */
|
||||
action?: "rewrite_data";
|
||||
|
||||
/**
|
||||
* Compaction settings for this invocation. Omitted remote values inherit table
|
||||
* tuning, then deployment/built-in defaults.
|
||||
* 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({ dataOptions: {
|
||||
* targetRowsPerFragment: 1000, maxSourceRows: 5000,
|
||||
* ioBufferSize: 64 * 1024 * 1024, batchSize: 128,
|
||||
* } });
|
||||
*/
|
||||
dataOptions?: OptimizeDataOptions;
|
||||
/**
|
||||
* If set then all versions older than the given date
|
||||
* be removed. The current version will never be removed.
|
||||
* The default is 7 days
|
||||
* @example
|
||||
* // Delete all versions older than 1 day
|
||||
* const olderThan = new Date();
|
||||
* olderThan.setDate(olderThan.getDate() - 1));
|
||||
* tbl.optimize({cleanupOlderThan: olderThan});
|
||||
*
|
||||
* // Delete versions committed before this point. Versions created by the
|
||||
* // optimize call itself are newer than the cutoff and will be retained.
|
||||
* tbl.optimize({cleanupOlderThan: new Date()});
|
||||
*/
|
||||
cleanupOlderThan: Date;
|
||||
/**
|
||||
* Because they may be part of an in-progress transaction, files newer than
|
||||
* 7 days old are not deleted by default. If you are sure that there are no
|
||||
* in-progress transactions, then you can set this to true to delete all
|
||||
* files older than `cleanupOlderThan`.
|
||||
*
|
||||
* **WARNING**: This should only be set to true if you can guarantee that
|
||||
* no other process is currently working on this dataset. Otherwise the
|
||||
* dataset could be put into a corrupted state.
|
||||
*/
|
||||
deleteUnverified: boolean;
|
||||
/** Embedded: remove versions committed before this cutoff. Defaults to seven days ago.
|
||||
* Cannot be combined with action="rewrite_data" or used with durable Optimize jobs. */
|
||||
cleanupOlderThan?: Date;
|
||||
/** Embedded: also remove unverified files. Only enable when no other process
|
||||
* can be writing this dataset. Defaults to false; unavailable for durable jobs. */
|
||||
deleteUnverified?: boolean;
|
||||
}
|
||||
|
||||
export interface Version {
|
||||
@@ -1363,7 +1326,7 @@ export abstract class Table {
|
||||
* console.log(result.rowsSucceeded, result.fragmentsRemoved);
|
||||
*/
|
||||
async optimizeAsync(
|
||||
_options?: OptimizeDataOptions,
|
||||
_options?: OptimizeOptions,
|
||||
): Promise<Job<OptimizeResult>> {
|
||||
throw new Error("compaction jobs are only supported on remote tables");
|
||||
}
|
||||
@@ -1391,11 +1354,11 @@ 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.
|
||||
* `dataOptions` can tune the four per-job settings documented in
|
||||
* {@link OptimizeDataOptions}. Older-server errors are propagated without
|
||||
* The flat options tune the four per-job settings documented in
|
||||
* {@link OptimizeOptions}. Older-server errors are propagated without
|
||||
* retrying with the settings removed.
|
||||
*/
|
||||
abstract optimize(options?: Partial<OptimizeOptions>): Promise<OptimizeStats>;
|
||||
abstract optimize(options?: OptimizeOptions): Promise<OptimizeStats>;
|
||||
/**
|
||||
* List all indices that have been created with {@link Table.createIndex}
|
||||
*
|
||||
@@ -2086,34 +2049,20 @@ export class LocalTable extends Table {
|
||||
return this.inner.currentBranch() ?? null;
|
||||
}
|
||||
|
||||
async optimizeAsync(
|
||||
options?: OptimizeDataOptions,
|
||||
): Promise<Job<OptimizeResult>> {
|
||||
validateOptimizeDataOptions(options);
|
||||
async optimizeAsync(options?: OptimizeOptions): Promise<Job<OptimizeResult>> {
|
||||
validateOptimizeOptions(options, true);
|
||||
return new Job(
|
||||
await this.inner.optimizeAsync(options),
|
||||
await this.inner.optimizeAsync(optimizeRewriteSettings(options)),
|
||||
decodeOptimizeResult,
|
||||
);
|
||||
}
|
||||
|
||||
async optimize(options?: Partial<OptimizeOptions>): Promise<OptimizeStats> {
|
||||
async optimize(options?: OptimizeOptions): Promise<OptimizeStats> {
|
||||
validateOptimizeOptions(options);
|
||||
if (options?.action !== undefined && options.action !== "rewrite_data") {
|
||||
throw new Error('action must be "rewrite_data" or undefined');
|
||||
}
|
||||
if (
|
||||
options?.action === "rewrite_data" &&
|
||||
(options.cleanupOlderThan !== undefined ||
|
||||
options.deleteUnverified === true)
|
||||
) {
|
||||
throw new Error(
|
||||
'action="rewrite_data" cannot be combined with cleanup options',
|
||||
);
|
||||
}
|
||||
return await this.inner.optimize(
|
||||
options?.cleanupOlderThan?.getTime(),
|
||||
options?.deleteUnverified,
|
||||
options?.dataOptions,
|
||||
optimizeRewriteSettings(options),
|
||||
options?.action,
|
||||
);
|
||||
}
|
||||
@@ -2411,35 +2360,52 @@ function rowIdsToBigInts(rowIds: readonly (bigint | number)[]): bigint[] {
|
||||
});
|
||||
}
|
||||
|
||||
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.
|
||||
function validateOptimizeOptions(
|
||||
options?: OptimizeOptions,
|
||||
durable = false,
|
||||
): void {
|
||||
const supported = new Set([
|
||||
"targetRowsPerFragment",
|
||||
"maxSourceRows",
|
||||
"ioBufferSize",
|
||||
"batchSize",
|
||||
"action",
|
||||
"cleanupOlderThan",
|
||||
"deleteUnverified",
|
||||
]);
|
||||
for (const key of Object.keys(options ?? {})) {
|
||||
if (!supported.has(key)) {
|
||||
throw new Error(`Unsupported Optimize data option: ${key}`);
|
||||
}
|
||||
if (!supported.has(key))
|
||||
throw new Error(`Unsupported Optimize option: ${key}`);
|
||||
}
|
||||
if (options?.action !== undefined && options.action !== "rewrite_data") {
|
||||
throw new Error('action must be "rewrite_data" or undefined');
|
||||
}
|
||||
const cleanup =
|
||||
options?.cleanupOlderThan !== undefined ||
|
||||
options?.deleteUnverified === true;
|
||||
if (durable && cleanup)
|
||||
throw new Error("cleanup options are unsupported by Optimize jobs");
|
||||
if (options?.action === "rewrite_data" && cleanup) {
|
||||
throw new Error(
|
||||
'action="rewrite_data" cannot be combined with cleanup options',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function optimizeRewriteSettings(
|
||||
options?: OptimizeOptions,
|
||||
): OptimizeOptions | undefined {
|
||||
const tuning: OptimizeOptions = {};
|
||||
for (const key of [
|
||||
"targetRowsPerFragment",
|
||||
"maxSourceRows",
|
||||
"ioBufferSize",
|
||||
"batchSize",
|
||||
] as const) {
|
||||
const value = options?.[key];
|
||||
if (value !== undefined) tuning[key] = value;
|
||||
}
|
||||
return Object.keys(tuning).length ? tuning : undefined;
|
||||
}
|
||||
|
||||
async function toNativePrewarmDataOptions(options?: PrewarmDataOptions) {
|
||||
|
||||
+8
-8
@@ -71,15 +71,15 @@ impl PrewarmDataOptions {
|
||||
|
||||
/// Per-invocation file-compaction settings. All numbers must be safe integers.
|
||||
#[napi(object)]
|
||||
pub struct OptimizeDataOptions {
|
||||
pub struct OptimizeOptions {
|
||||
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 OptimizeDataOptions {
|
||||
fn into_overrides(self) -> napi::Result<lancedb::table::optimize::OptimizeDataOverrides> {
|
||||
impl OptimizeOptions {
|
||||
fn into_overrides(self) -> napi::Result<lancedb::table::optimize::OptimizeOverrides> {
|
||||
const MAX_SAFE_INTEGER: u64 = 9_007_199_254_740_991;
|
||||
fn integer(
|
||||
value: Option<f64>,
|
||||
@@ -103,7 +103,7 @@ impl OptimizeDataOptions {
|
||||
}
|
||||
Ok(Some(value as u64))
|
||||
}
|
||||
let mut options = lancedb::table::optimize::OptimizeDataOverrides::default();
|
||||
let mut options = lancedb::table::optimize::OptimizeOverrides::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<OptimizeDataOptions>,
|
||||
options: Option<OptimizeOptions>,
|
||||
) -> napi::Result<crate::job::Job> {
|
||||
let options = options
|
||||
.map(OptimizeDataOptions::into_overrides)
|
||||
.map(OptimizeOptions::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>,
|
||||
data_options: Option<OptimizeDataOptions>,
|
||||
data_options: Option<OptimizeOptions>,
|
||||
action: Option<String>,
|
||||
) -> napi::Result<OptimizeStats> {
|
||||
let rewrite_only = match action.as_deref() {
|
||||
@@ -1053,7 +1053,7 @@ impl Table {
|
||||
.transpose()?;
|
||||
|
||||
let overrides = data_options
|
||||
.map(OptimizeDataOptions::into_overrides)
|
||||
.map(OptimizeOptions::into_overrides)
|
||||
.transpose()?
|
||||
.unwrap_or_default();
|
||||
let stats = if rewrite_only {
|
||||
|
||||
Reference in New Issue
Block a user