mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
refactor(sdk)!: standardize compaction APIs on Optimize
BREAKING CHANGE: Replace compact_async/compactAsync with optimize_async/optimizeAsync, Rust compact(overrides) with optimize(overrides), and Python compact_files() with optimize(action="rewrite_data"). No compatibility aliases are retained.
This commit is contained in:
@@ -4111,7 +4111,12 @@ it("passes cleanupOlderThan to the native binding as an absolute timestamp", asy
|
||||
|
||||
await table.optimize({ cleanupOlderThan: cutoff, deleteUnverified: true });
|
||||
|
||||
expect(optimize).toHaveBeenCalledWith(cutoff.getTime(), true, undefined);
|
||||
expect(optimize).toHaveBeenCalledWith(
|
||||
cutoff.getTime(),
|
||||
true,
|
||||
undefined,
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it("forwards per-job compaction options to the native binding", async () => {
|
||||
@@ -4128,6 +4133,7 @@ it("forwards per-job compaction options to the native binding", async () => {
|
||||
undefined,
|
||||
undefined,
|
||||
compactionOptions,
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -6000,14 +6006,14 @@ describe("durable compaction validation", () => {
|
||||
test("rejects local tables and unknown options", async () => {
|
||||
const db = await connect(`memory://compaction-job-${Date.now()}`);
|
||||
const table = await db.createTable("test", [{ id: 1 }]);
|
||||
await expect(table.compactAsync()).rejects.toThrow(
|
||||
await expect(table.optimizeAsync()).rejects.toThrow(
|
||||
"only supported on remote tables",
|
||||
);
|
||||
await expect(
|
||||
table.compactAsync({ unrecognizedLimit: 1 } as never),
|
||||
table.optimizeAsync({ unrecognizedLimit: 1 } as never),
|
||||
).rejects.toThrow("Unsupported compaction option");
|
||||
await expect(
|
||||
table.compactAsync({ targetRowsPerFragment: 0 }),
|
||||
table.optimizeAsync({ targetRowsPerFragment: 0 }),
|
||||
).rejects.toThrow("safe integer");
|
||||
});
|
||||
});
|
||||
@@ -6057,3 +6063,39 @@ describe("Enterprise table overrides", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("Optimize action selection", () => {
|
||||
test("rewrites data while retaining versions and deferring index maintenance", async () => {
|
||||
const db = await connect(`memory://optimize-action-${Date.now()}`);
|
||||
const table = await db.createTable("test", [{ id: 1 }]);
|
||||
await table.add([{ id: 2 }]);
|
||||
await table.createIndex("id", { config: Index.btree() });
|
||||
await table.add([{ id: 3 }]);
|
||||
await table.add([{ id: 4 }]);
|
||||
const before = await table.listVersions();
|
||||
const stats = await table.optimize({ action: "rewrite_data" });
|
||||
expect(stats.compaction.fragmentsRemoved).toBe(4);
|
||||
expect(stats.compaction.fragmentsAdded).toBe(2);
|
||||
expect(stats.prune.oldVersionsRemoved).toBe(0);
|
||||
expect((await table.listVersions()).length).toBeGreaterThan(before.length);
|
||||
expect((await table.indexStats("id_idx"))?.numUnindexedRows).toBe(2);
|
||||
await table.optimize();
|
||||
expect((await table.indexStats("id_idx"))?.numUnindexedRows).toBe(0);
|
||||
await table.checkout(1);
|
||||
expect(await table.countRows()).toBe(1);
|
||||
expect("compactAsync" in table).toBe(false);
|
||||
});
|
||||
|
||||
test("rejects unknown actions and conflicting cleanup requests before changes", async () => {
|
||||
const db = await connect(`memory://optimize-invalid-${Date.now()}`);
|
||||
const table = await db.createTable("test", [{ id: 1 }]);
|
||||
for (const options of [
|
||||
{ action: "unknown" },
|
||||
{ action: "rewrite_data", cleanupOlderThan: new Date() },
|
||||
{ action: "rewrite_data", deleteUnverified: true },
|
||||
]) {
|
||||
await expect(table.optimize(options as never)).rejects.toThrow("action");
|
||||
}
|
||||
expect(await table.version()).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
+26
-8
@@ -199,6 +199,11 @@ export interface CompactionOptions {
|
||||
}
|
||||
|
||||
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. */
|
||||
action?: "rewrite_data";
|
||||
|
||||
/**
|
||||
* Compaction settings for this invocation. Omitted remote values inherit table
|
||||
* tuning, then deployment/built-in defaults.
|
||||
@@ -1063,8 +1068,8 @@ export abstract class Table {
|
||||
*
|
||||
* This is a metadata-only operation and does not remove the data from the
|
||||
* underlying storage. In order to remove the data, you must subsequently
|
||||
* call ``compact_files`` to rewrite the data without the removed columns and
|
||||
* then call ``cleanup_files`` to remove the old files.
|
||||
* call `optimize({ action: "rewrite_data" })` to rewrite the data without the
|
||||
* removed columns, then prune old versions using Optimize cleanup options.
|
||||
*
|
||||
* Local tables reject dropping source or vector columns referenced by
|
||||
* `embedding_functions` schema metadata, preserving the embedding binding.
|
||||
@@ -1353,11 +1358,11 @@ export abstract class Table {
|
||||
* Reopen with db.openJob<CompactionResult>(job.id!). A wait timeout does not cancel work.
|
||||
*
|
||||
* @example
|
||||
* const job = await table.compactAsync({ targetRowsPerFragment: 1000 });
|
||||
* const job = await table.optimizeAsync({ targetRowsPerFragment: 1000 });
|
||||
* const result = await job.wait();
|
||||
* console.log(result.rowsSucceeded, result.fragmentsRemoved);
|
||||
*/
|
||||
async compactAsync(
|
||||
async optimizeAsync(
|
||||
_options?: CompactionOptions,
|
||||
): Promise<Job<CompactionResult>> {
|
||||
throw new Error("compaction jobs are only supported on remote tables");
|
||||
@@ -1384,8 +1389,8 @@ export abstract class Table {
|
||||
* On a LanceDB Enterprise (remote) table, only compaction is currently
|
||||
* supported. Pruning old versions and index optimization are silently
|
||||
* skipped there instead of being attempted, so this call resolves
|
||||
* successfully, with zeroed pruning statistics, once the compaction job
|
||||
* has been submitted; it does not wait for that job to finish.
|
||||
* 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
|
||||
* retrying with the settings removed.
|
||||
@@ -2081,22 +2086,35 @@ export class LocalTable extends Table {
|
||||
return this.inner.currentBranch() ?? null;
|
||||
}
|
||||
|
||||
async compactAsync(
|
||||
async optimizeAsync(
|
||||
options?: CompactionOptions,
|
||||
): Promise<Job<CompactionResult>> {
|
||||
validateCompactionOptions(options);
|
||||
return new Job(
|
||||
await this.inner.compactAsync(options),
|
||||
await this.inner.optimizeAsync(options),
|
||||
decodeCompactionResult,
|
||||
);
|
||||
}
|
||||
|
||||
async optimize(options?: Partial<OptimizeOptions>): Promise<OptimizeStats> {
|
||||
validateCompactionOptions(options?.compactionOptions);
|
||||
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?.compactionOptions,
|
||||
options?.action,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+30
-15
@@ -1002,7 +1002,7 @@ impl Table {
|
||||
}
|
||||
|
||||
#[napi(catch_unwind)]
|
||||
pub async fn compact_async(
|
||||
pub async fn optimize_async(
|
||||
&self,
|
||||
options: Option<CompactionOptions>,
|
||||
) -> napi::Result<crate::job::Job> {
|
||||
@@ -1012,7 +1012,7 @@ impl Table {
|
||||
.unwrap_or_default();
|
||||
let job = self
|
||||
.inner_ref()?
|
||||
.compact_async(options)
|
||||
.optimize_async(options)
|
||||
.await
|
||||
.default_error()?;
|
||||
Ok(crate::job::Job::new_typed(job))
|
||||
@@ -1024,7 +1024,22 @@ impl Table {
|
||||
before_timestamp_ms: Option<i64>,
|
||||
delete_unverified: Option<bool>,
|
||||
compaction_options: Option<CompactionOptions>,
|
||||
action: Option<String>,
|
||||
) -> napi::Result<OptimizeStats> {
|
||||
let rewrite_only = match action.as_deref() {
|
||||
None => false,
|
||||
Some("rewrite_data") => true,
|
||||
Some(_) => {
|
||||
return Err(napi::Error::from_reason(
|
||||
"action must be 'rewrite_data' or undefined",
|
||||
));
|
||||
}
|
||||
};
|
||||
if rewrite_only && (before_timestamp_ms.is_some() || delete_unverified == Some(true)) {
|
||||
return Err(napi::Error::from_reason(
|
||||
"action='rewrite_data' cannot be combined with cleanup options",
|
||||
));
|
||||
}
|
||||
let inner = self.inner_ref()?;
|
||||
|
||||
let before_timestamp = before_timestamp_ms
|
||||
@@ -1037,20 +1052,20 @@ impl Table {
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
let stats = inner
|
||||
.optimize_full_with_overrides(
|
||||
compaction_options
|
||||
.map(CompactionOptions::into_overrides)
|
||||
.transpose()?
|
||||
.unwrap_or_default(),
|
||||
None,
|
||||
before_timestamp,
|
||||
delete_unverified,
|
||||
)
|
||||
.await
|
||||
.default_error()?;
|
||||
let overrides = compaction_options
|
||||
.map(CompactionOptions::into_overrides)
|
||||
.transpose()?
|
||||
.unwrap_or_default();
|
||||
let stats = if rewrite_only {
|
||||
inner.optimize(overrides).await
|
||||
} else {
|
||||
inner
|
||||
.optimize_full_with_overrides(overrides, None, before_timestamp, delete_unverified)
|
||||
.await
|
||||
}
|
||||
.default_error()?;
|
||||
let compaction_stats = stats.compaction.unwrap();
|
||||
let prune_stats = stats.prune.unwrap();
|
||||
let prune_stats = stats.prune.unwrap_or_default();
|
||||
Ok(OptimizeStats {
|
||||
compaction: CompactionStats {
|
||||
files_added: compaction_stats.files_added as i64,
|
||||
|
||||
Reference in New Issue
Block a user