mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
feat(sdk): add typed Optimize previews
This commit is contained in:
@@ -6174,3 +6174,77 @@ it("rejects the removed dataOptions wrapper before submission", async () => {
|
||||
).rejects.toThrow("Unsupported Optimize option: dataOptions");
|
||||
expect(optimize).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe("Optimize preview", () => {
|
||||
it.each([false, true])(
|
||||
"decodes a typed preview with legacy wrapper=%p",
|
||||
async (legacy) => {
|
||||
const explainOptimize = jest.fn().mockResolvedValue(
|
||||
JSON.parse(`{
|
||||
"operation":"rewrite_data", "table":"events", "namespace":["team"], "branch":"main",
|
||||
"source_version":42, "planned_groups":0, "planned_fragments":0, "planned_source_rows":0,
|
||||
"more_work":false,
|
||||
"effective_options":{"target_rows_per_fragment":1000,"io_buffer_size":0,"max_source_rows":null,"batch_size":null}
|
||||
}`),
|
||||
);
|
||||
const table = new LocalTable({ explainOptimize } as never);
|
||||
const plan = await table.explainOptimize(
|
||||
legacy
|
||||
? {
|
||||
compactionOptions: { maxSourceRows: 500, ioBufferSize: 0 },
|
||||
action: "rewrite_data",
|
||||
}
|
||||
: {
|
||||
maxSourceRows: 500,
|
||||
ioBufferSize: 0,
|
||||
action: "rewrite_data",
|
||||
},
|
||||
);
|
||||
expect(explainOptimize).toHaveBeenCalledTimes(1);
|
||||
expect(explainOptimize).toHaveBeenCalledWith({
|
||||
maxSourceRows: 500,
|
||||
ioBufferSize: 0,
|
||||
});
|
||||
expect(plan).toEqual({
|
||||
operation: "rewrite_data",
|
||||
table: "events",
|
||||
namespace: ["team"],
|
||||
branch: "main",
|
||||
sourceVersion: 42,
|
||||
plannedGroups: 0,
|
||||
plannedFragments: 0,
|
||||
plannedSourceRows: 0,
|
||||
moreWork: false,
|
||||
effectiveOptions: {
|
||||
targetRowsPerFragment: 1000,
|
||||
ioBufferSize: 0,
|
||||
maxSourceRows: null,
|
||||
batchSize: null,
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
test.each([
|
||||
{ cleanupOlderThan: new Date() },
|
||||
{ deleteUnverified: true },
|
||||
{ action: "cleanup" },
|
||||
{ dataOptions: {} },
|
||||
{ unknown: 1 },
|
||||
])(
|
||||
"rejects unsupported preview settings before native calls: %j",
|
||||
async (options) => {
|
||||
const explainOptimize = jest.fn();
|
||||
const table = new LocalTable({ explainOptimize } as never);
|
||||
await expect(table.explainOptimize(options as never)).rejects.toThrow();
|
||||
expect(explainOptimize).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
it("rejects Embedded previews", async () => {
|
||||
const db = await connect(`memory://optimize-preview-${Date.now()}`);
|
||||
const table = await db.createTable("events", [{ id: 1 }]);
|
||||
await expect(table.explainOptimize()).rejects.toThrow("Enterprise");
|
||||
await expect(table.explainOptimize({ batchSize: 0 })).rejects.toThrow(
|
||||
"safe integer",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -667,7 +667,11 @@ export {
|
||||
|
||||
export { Listing, ListingOptions } from "./listing";
|
||||
|
||||
export type { OptimizeResult } from "./optimize";
|
||||
export type {
|
||||
OptimizePlan,
|
||||
OptimizeResolvedOptions,
|
||||
OptimizeResult,
|
||||
} from "./optimize";
|
||||
export type { CompactionResult } from "./compaction";
|
||||
|
||||
export type {
|
||||
|
||||
@@ -39,3 +39,52 @@ export function decodeOptimizeResult(value: unknown): OptimizeResult {
|
||||
fragmentsRemoved: (result.fragments_removed as number | null) ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
/** Effective settings after Enterprise policy resolution. */
|
||||
export interface OptimizeResolvedOptions {
|
||||
targetRowsPerFragment: number;
|
||||
maxSourceRows: number | null;
|
||||
/** Zero disables byte-based I/O backpressure; null uses the worker default. */
|
||||
ioBufferSize: number | null;
|
||||
batchSize: number | null;
|
||||
}
|
||||
|
||||
/** Data-rewrite preview without creating a maintenance job.
|
||||
* Counts describe planned work, not committed results, runtime, or memory use.
|
||||
* A later submission chooses its own snapshot and may select different work. */
|
||||
export interface OptimizePlan {
|
||||
operation: "rewrite_data";
|
||||
table: string;
|
||||
namespace: string[];
|
||||
branch: string;
|
||||
sourceVersion: number;
|
||||
effectiveOptions: OptimizeResolvedOptions;
|
||||
plannedGroups: number;
|
||||
plannedFragments: number;
|
||||
plannedSourceRows: number;
|
||||
moreWork: boolean;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function decodeOptimizePlan(value: unknown): OptimizePlan {
|
||||
// Rust validates the versioned SQL response before exposing it to JS.
|
||||
const plan = value as Record<string, unknown>;
|
||||
const options = plan.effective_options as Record<string, number | null>;
|
||||
return {
|
||||
operation: plan.operation as "rewrite_data",
|
||||
table: plan.table as string,
|
||||
namespace: plan.namespace as string[],
|
||||
branch: plan.branch as string,
|
||||
sourceVersion: plan.source_version as number,
|
||||
plannedGroups: plan.planned_groups as number,
|
||||
plannedFragments: plan.planned_fragments as number,
|
||||
plannedSourceRows: plan.planned_source_rows as number,
|
||||
moreWork: plan.more_work as boolean,
|
||||
effectiveOptions: {
|
||||
targetRowsPerFragment: options.target_rows_per_fragment as number,
|
||||
maxSourceRows: options.max_source_rows ?? null,
|
||||
ioBufferSize: options.io_buffer_size ?? null,
|
||||
batchSize: options.batch_size ?? null,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
+29
-1
@@ -1,7 +1,12 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import { OptimizeResult, decodeOptimizeResult } from "./optimize";
|
||||
import {
|
||||
OptimizePlan,
|
||||
OptimizeResult,
|
||||
decodeOptimizePlan,
|
||||
decodeOptimizeResult,
|
||||
} from "./optimize";
|
||||
import {
|
||||
PrewarmDataPlan,
|
||||
PrewarmDataResult,
|
||||
@@ -1320,6 +1325,21 @@ export abstract class Table {
|
||||
* out state and the read_consistency_interval, if any, will apply.
|
||||
*/
|
||||
abstract restore(): Promise<void>;
|
||||
/** Preview Enterprise data rewriting using the same flat settings as optimizeAsync().
|
||||
* Requires structured SQL preview support, table Ownership and parent Usage.
|
||||
* Creates no maintenance job and does not poll job APIs. Cleanup, separate index
|
||||
* maintenance, Embedded and historical-version handles are unsupported.
|
||||
* A later submission plans its own snapshot.
|
||||
* @example
|
||||
* const plan = await table.explainOptimize({ maxSourceRows: 1_000_000 });
|
||||
* console.log(plan.sourceVersion, plan.plannedSourceRows);
|
||||
*/
|
||||
async explainOptimize(_options?: OptimizeOptions): Promise<OptimizePlan> {
|
||||
throw new Error(
|
||||
"Optimize previews are only supported by LanceDB Enterprise",
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit Enterprise Optimize and return its durable job after acceptance.
|
||||
*
|
||||
@@ -2067,6 +2087,14 @@ export class LocalTable extends Table {
|
||||
return this.inner.currentBranch() ?? null;
|
||||
}
|
||||
|
||||
async explainOptimize(options?: OptimizeOptions): Promise<OptimizePlan> {
|
||||
options = normalizeOptimizeOptions(options);
|
||||
validateOptimizeOptions(options, true);
|
||||
return decodeOptimizePlan(
|
||||
await this.inner.explainOptimize(optimizeRewriteSettings(options)),
|
||||
);
|
||||
}
|
||||
|
||||
async optimizeAsync(options?: OptimizeOptions): Promise<Job<OptimizeResult>> {
|
||||
options = normalizeOptimizeOptions(options);
|
||||
validateOptimizeOptions(options, true);
|
||||
|
||||
@@ -1001,6 +1001,23 @@ impl Table {
|
||||
Ok(self.inner_ref()?.current_branch())
|
||||
}
|
||||
|
||||
#[napi(catch_unwind)]
|
||||
pub async fn explain_optimize(
|
||||
&self,
|
||||
options: Option<OptimizeOptions>,
|
||||
) -> napi::Result<serde_json::Value> {
|
||||
let options = options
|
||||
.map(OptimizeOptions::into_overrides)
|
||||
.transpose()?
|
||||
.unwrap_or_default();
|
||||
let plan = self
|
||||
.inner_ref()?
|
||||
.explain_optimize(options)
|
||||
.await
|
||||
.default_error()?;
|
||||
serde_json::to_value(plan).map_err(|error| napi::Error::from_reason(error.to_string()))
|
||||
}
|
||||
|
||||
#[napi(catch_unwind)]
|
||||
pub async fn optimize_async(
|
||||
&self,
|
||||
|
||||
Reference in New Issue
Block a user