fix(sdk): retain deprecated compaction API compatibility

This commit is contained in:
ChilePiquin
2026-11-19 16:07:34 -08:00
parent 7a690bac71
commit 89f3de253f
31 changed files with 756 additions and 53 deletions
+41 -6
View File
@@ -11,7 +11,10 @@ import {
AutoQuery,
type BlobInput,
type BlobMode,
type CompactionOptions,
type CompactionResult,
Connection,
type Job,
MatchQuery,
type OptimizeOptions,
PhraseQuery,
@@ -6081,7 +6084,7 @@ describe("Optimize action selection", () => {
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);
expect("compactAsync" in table).toBe(true);
});
test("rejects unknown actions and conflicting cleanup requests before changes", async () => {
@@ -6098,15 +6101,47 @@ describe("Optimize action selection", () => {
});
});
test("rejects removed compactionOptions without silently submitting defaults", async () => {
test("preserves nested compactionOptions including explicit zero", 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();
await table.optimize({
compactionOptions: { maxSourceRows: 7, ioBufferSize: 0 },
});
expect(optimize).toHaveBeenCalledTimes(1);
expect(optimize).toHaveBeenCalledWith(
undefined,
undefined,
{ maxSourceRows: 7, ioBufferSize: 0 },
undefined,
);
});
it("keeps the deprecated durable method on the same submission path", async () => {
const optimizeAsync = jest.fn().mockResolvedValue({});
const table = new LocalTable({ optimizeAsync } as never);
const options: CompactionOptions = { maxSourceRows: 7, ioBufferSize: 0 };
const job: Job<CompactionResult> = await table.compactAsync(options);
expect(job).toBeDefined();
expect(optimizeAsync).toHaveBeenCalledTimes(1);
expect(optimizeAsync).toHaveBeenCalledWith({
maxSourceRows: 7,
ioBufferSize: 0,
});
});
it.each([
{ compactionOptions: { maxSourceRows: 7 }, batchSize: 16 },
{ compactionOptions: { unknownLimit: 7 } },
])(
"rejects ambiguous or unknown legacy settings before submission",
async (options) => {
const optimize = jest.fn();
const table = new LocalTable({ optimize } as never);
await expect(table.optimize(options as never)).rejects.toThrow();
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);
+8
View File
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
import type { OptimizeResult } from "./optimize";
/** @deprecated Use OptimizeResult. */
export type CompactionResult = OptimizeResult;
export { decodeOptimizeResult as decodeCompactionResult } from "./optimize";
+2
View File
@@ -175,6 +175,7 @@ export {
AddDataOptions,
UpdateOptions,
OptimizeOptions,
CompactionOptions,
PrewarmDataOptions,
Version,
WriteProgress,
@@ -667,6 +668,7 @@ export {
export { Listing, ListingOptions } from "./listing";
export type { OptimizeResult } from "./optimize";
export type { CompactionResult } from "./compaction";
export type {
PrewarmDataPlan,
+48
View File
@@ -197,6 +197,8 @@ export interface OptimizeOptions {
ioBufferSize?: number;
/** Positive scanner batch size in rows. */
batchSize?: number;
/** @deprecated Pass rewrite settings directly on OptimizeOptions. */
compactionOptions?: CompactionOptions;
/** Run only data rewriting, retaining history and skipping separate index maintenance. */
action?: "rewrite_data";
/** Embedded: remove versions committed before this cutoff. Defaults to seven days ago.
@@ -207,6 +209,13 @@ export interface OptimizeOptions {
deleteUnverified?: boolean;
}
/** @deprecated Use OptimizeOptions with flat rewrite settings. */
export interface CompactionOptions
extends Pick<
OptimizeOptions,
"targetRowsPerFragment" | "maxSourceRows" | "ioBufferSize" | "batchSize"
> {}
export interface Version {
version: number;
timestamp: Date;
@@ -1331,6 +1340,13 @@ export abstract class Table {
throw new Error("Optimize jobs are only supported on remote tables");
}
/** @deprecated Use optimizeAsync() instead. */
async compactAsync(
options?: CompactionOptions,
): Promise<Job<OptimizeResult>> {
return this.optimizeAsync(options);
}
/**
* Optimize the on-disk data and indices for better performance.
*
@@ -2050,6 +2066,7 @@ export class LocalTable extends Table {
}
async optimizeAsync(options?: OptimizeOptions): Promise<Job<OptimizeResult>> {
options = normalizeOptimizeOptions(options);
validateOptimizeOptions(options, true);
return new Job(
await this.inner.optimizeAsync(optimizeRewriteSettings(options)),
@@ -2058,6 +2075,7 @@ export class LocalTable extends Table {
}
async optimize(options?: OptimizeOptions): Promise<OptimizeStats> {
options = normalizeOptimizeOptions(options);
validateOptimizeOptions(options);
return await this.inner.optimize(
options?.cleanupOlderThan?.getTime(),
@@ -2360,6 +2378,35 @@ function rowIdsToBigInts(rowIds: readonly (bigint | number)[]): bigint[] {
});
}
function normalizeOptimizeOptions(
options?: OptimizeOptions,
): OptimizeOptions | undefined {
if (options?.compactionOptions === undefined) return options;
const { compactionOptions, ...flat } = options;
if (
compactionOptions === null ||
typeof compactionOptions !== "object" ||
Array.isArray(compactionOptions)
)
throw new Error("compactionOptions must be an object");
const rewriteKeys = new Set([
"targetRowsPerFragment",
"maxSourceRows",
"ioBufferSize",
"batchSize",
]);
for (const key of Object.keys(compactionOptions)) {
if (!rewriteKeys.has(key))
throw new Error(`Unsupported Optimize option: ${key}`);
}
if (Object.keys(flat).some((key) => rewriteKeys.has(key))) {
throw new Error(
"cannot combine compactionOptions with flat rewrite settings",
);
}
return { ...flat, ...compactionOptions };
}
function validateOptimizeOptions(
options?: OptimizeOptions,
durable = false,
@@ -2372,6 +2419,7 @@ function validateOptimizeOptions(
"action",
"cleanupOlderThan",
"deleteUnverified",
"compactionOptions",
]);
for (const key of Object.keys(options ?? {})) {
if (!supported.has(key))