mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-29 22:13:58 +00:00
feat(sdk): support bound parameters in data prewarm (#5076)
Data prewarm methods accept filter expressions but cannot bind their
values, which forces applications to build literal SQL themselves. This
adds optional positional and named parameters to data-prewarm submission
and typed previews across Rust, Python, and TypeScript. Values use the
existing typed Arrow SQL transport and remain separate from the filter
text. Existing calls and job-handle behavior are preserved;
parameterized filters require a compatible LanceDB Enterprise server.
This was accomplished with the following changes:
- Add `PrewarmDataOptions::parameters(RecordBatch)` and forward the
batch through SQL submission and `EXPLAIN PREWARM` requests.
- Reuse Python's `QueryParameters` conversion for
synchronous/asynchronous, legacy/job-returning, and typed preview
methods.
- Add TypeScript positional-array, named-object, and explicit Arrow
parameter inputs, with safe scalar encoding and native IPC forwarding.
- Keep parameter values out of Rust option debug output and reject
invalid input before submission where possible.
- Add transport and codec regression tests, including injection-shaped
strings, typed Arrow values, legacy method forwarding, and typed preview
bindings.
- Document bound filters in the cache-prewarm guide with Python,
TypeScript, and Rust examples; update generic SQL parameter
documentation.
### Usage and behaviour
```python
job = table.prewarm_data_async(
["payload"], filter="id >= $minimum", parameters={"minimum": 1000}
)
job.wait()
```
```typescript
const job = await table.prewarmDataAsync({
columns: ["payload"], filter: "id >= $1", parameters: [1000],
});
await job.wait();
```
Python accepts a sequence, mapping, or one-row Arrow batch/table.
TypeScript accepts scalar arrays/objects or one-row Arrow input; safe
integers and bigint use Int64, fractional numbers use Float64. Rust
accepts a one-row Arrow batch. Parameters bind filter values, while
table identity, branch, and concurrency keep their existing interfaces.
Submission-only methods also forward bindings and retain their existing
return behavior. Typed preview helpers accept the same parameters
without creating a job.
### Testing
Python prewarm Flight transport tests pass (65 cases), Embedded
compatibility tests pass (6 cases), and focused Node parameter/prewarm
tests pass (45 cases, including ten codec and native-boundary cases).
Earlier matching-server verification passed 57 Python/SQL scenarios, 14
Node scenarios, and Rust SDK checks, covering selective and empty plans,
typed values, authorization, pinned branches, legacy methods, and
injection-shaped strings. A bound job also survives a full runtime
restart. The guide submission examples were also verified in Python,
TypeScript, and Rust; the TypeScript example type-checks. Live-server
checks used filesystem storage.
This commit is contained in:
@@ -673,3 +673,5 @@ export type {
|
||||
PrewarmDataResult,
|
||||
PrewarmPruningSummary,
|
||||
} from "./prewarm";
|
||||
|
||||
export type { QueryParameters, QueryParameterValue } from "./sql";
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import {
|
||||
Binary,
|
||||
Bool,
|
||||
type DataType,
|
||||
Float64,
|
||||
Int64,
|
||||
Null,
|
||||
RecordBatchFileWriter,
|
||||
type RecordBatchLike,
|
||||
Table,
|
||||
type TableLike,
|
||||
TimestampMillisecond,
|
||||
Utf8,
|
||||
vectorFromArray,
|
||||
} from "./arrow";
|
||||
import { sanitizeTable } from "./sanitize";
|
||||
|
||||
/** Scalar SQL values. Use a one-row Arrow table for explicit Arrow types. */
|
||||
export type QueryParameterValue =
|
||||
| string
|
||||
| number
|
||||
| bigint
|
||||
| boolean
|
||||
| Date
|
||||
| Uint8Array
|
||||
| null;
|
||||
|
||||
/** Values for `$1`/`$name` placeholders, sent separately from SQL text.
|
||||
* Arrays bind by position; objects bind by name. Arrow columns bind both ways.
|
||||
* Safe integers and bigint use Int64; fractional numbers use Float64.
|
||||
* Arrow input preserves explicit types.
|
||||
*/
|
||||
export type QueryParameters =
|
||||
| readonly QueryParameterValue[]
|
||||
| Readonly<Record<string, QueryParameterValue>>
|
||||
| TableLike
|
||||
| RecordBatchLike;
|
||||
|
||||
/** @internal */
|
||||
export async function toParameterBuffer(
|
||||
parameters: QueryParameters,
|
||||
): Promise<Buffer> {
|
||||
let table: Table;
|
||||
if (
|
||||
"schema" in parameters &&
|
||||
parameters.schema !== null &&
|
||||
typeof parameters.schema === "object" &&
|
||||
"fields" in parameters.schema
|
||||
) {
|
||||
const arrow = parameters as TableLike | RecordBatchLike;
|
||||
table = sanitizeTable(
|
||||
"batches" in arrow ? arrow : { schema: arrow.schema, batches: [arrow] },
|
||||
);
|
||||
} else {
|
||||
const entries = Array.isArray(parameters)
|
||||
? parameters.map((value, index) => [String(index + 1), value] as const)
|
||||
: Object.entries(parameters);
|
||||
const columns = Object.fromEntries(
|
||||
entries.map(([name, input]) => {
|
||||
let value = input;
|
||||
let type: DataType;
|
||||
if (value === null) type = new Null();
|
||||
else if (typeof value === "string") type = new Utf8();
|
||||
else if (typeof value === "boolean") type = new Bool();
|
||||
else if (typeof value === "number") {
|
||||
if (
|
||||
!Number.isFinite(value) ||
|
||||
(Number.isInteger(value) && !Number.isSafeInteger(value))
|
||||
) {
|
||||
throw new Error(
|
||||
"Numeric parameters must be finite and integers must be safe; use bigint for Int64",
|
||||
);
|
||||
}
|
||||
type = Number.isInteger(value) ? new Int64() : new Float64();
|
||||
if (Number.isInteger(value)) value = BigInt(value);
|
||||
} else if (typeof value === "bigint") {
|
||||
if (value < -(1n << 63n) || value >= 1n << 63n) {
|
||||
throw new Error("bigint parameters must fit Int64");
|
||||
}
|
||||
type = new Int64();
|
||||
} else if (value instanceof Date && Number.isFinite(value.getTime())) {
|
||||
type = new TimestampMillisecond();
|
||||
} else if (value instanceof Uint8Array) type = new Binary();
|
||||
else
|
||||
throw new Error(
|
||||
"Query parameters must be scalar values or a one-row Arrow table",
|
||||
);
|
||||
return [name, vectorFromArray([value], type)];
|
||||
}),
|
||||
);
|
||||
table = new Table(columns);
|
||||
}
|
||||
if (table.numRows !== 1) {
|
||||
throw new Error(
|
||||
`Query parameters must contain exactly one row, got ${table.numRows}`,
|
||||
);
|
||||
}
|
||||
return Buffer.from(
|
||||
await RecordBatchFileWriter.writeAll(table).toUint8Array(),
|
||||
);
|
||||
}
|
||||
+30
-7
@@ -71,6 +71,7 @@ import {
|
||||
instanceOfFullTextQuery,
|
||||
} from "./query";
|
||||
import { sanitizeType } from "./sanitize";
|
||||
import { QueryParameters, toParameterBuffer } from "./sql";
|
||||
import { IntoSql, toSQL } from "./util";
|
||||
export { IndexConfig } from "./native";
|
||||
export {
|
||||
@@ -179,6 +180,8 @@ export interface PrewarmDataOptions {
|
||||
columns?: string[];
|
||||
/** A single SQL predicate for conservative metadata pruning. */
|
||||
filter?: string;
|
||||
/** Typed positional or named filter values, sent separately from SQL. */
|
||||
parameters?: QueryParameters;
|
||||
/** Positive per-executor concurrency, at most 4294967295. */
|
||||
concurrency?: number;
|
||||
}
|
||||
@@ -624,7 +627,8 @@ export abstract class Table {
|
||||
*
|
||||
* @example
|
||||
* const job = await table.prewarmDataAsync({
|
||||
* columns: ["video"], filter: "quality > 0.7", concurrency: 4,
|
||||
* columns: ["video"], filter: "quality > $minimum",
|
||||
* parameters: { minimum: 0.7 }, concurrency: 4,
|
||||
* });
|
||||
* await job.wait();
|
||||
*/
|
||||
@@ -643,7 +647,9 @@ export abstract class Table {
|
||||
* with an unsupported-operation error.
|
||||
*
|
||||
* @example
|
||||
* const plan = await table.explainPrewarmData({columns: ["video"], filter: "quality > 0.7"});
|
||||
* const plan = await table.explainPrewarmData({
|
||||
* columns: ["video"], filter: "quality > $minimum", parameters: {minimum: 0.7},
|
||||
* });
|
||||
* console.log(plan.pruning.selectedFragments);
|
||||
*/
|
||||
async explainPrewarmData(
|
||||
@@ -1668,9 +1674,10 @@ export class LocalTable extends Table {
|
||||
async prewarmDataAsync(
|
||||
options?: PrewarmDataOptions,
|
||||
): Promise<Job<PrewarmDataResult>> {
|
||||
validatePrewarmDataOptions(options);
|
||||
return new Job(
|
||||
await this.inner.prewarmDataAsync(options),
|
||||
await this.inner.prewarmDataAsync(
|
||||
await toNativePrewarmDataOptions(options),
|
||||
),
|
||||
decodePrewarmDataResult,
|
||||
);
|
||||
}
|
||||
@@ -1678,8 +1685,11 @@ export class LocalTable extends Table {
|
||||
async explainPrewarmData(
|
||||
options?: PrewarmDataOptions,
|
||||
): Promise<PrewarmDataPlan> {
|
||||
validatePrewarmDataOptions(options);
|
||||
return decodePrewarmDataPlan(await this.inner.explainPrewarmData(options));
|
||||
return decodePrewarmDataPlan(
|
||||
await this.inner.explainPrewarmData(
|
||||
await toNativePrewarmDataOptions(options),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async waitForIndex(
|
||||
@@ -2399,9 +2409,22 @@ function validateCompactionOptions(options?: CompactionOptions): void {
|
||||
}
|
||||
}
|
||||
|
||||
async function toNativePrewarmDataOptions(options?: PrewarmDataOptions) {
|
||||
validatePrewarmDataOptions(options);
|
||||
return options === undefined
|
||||
? undefined
|
||||
: {
|
||||
...options,
|
||||
parameters:
|
||||
options.parameters === undefined
|
||||
? undefined
|
||||
: await toParameterBuffer(options.parameters),
|
||||
};
|
||||
}
|
||||
|
||||
function validatePrewarmDataOptions(options?: PrewarmDataOptions): void {
|
||||
for (const key of Object.keys(options ?? {})) {
|
||||
if (!["columns", "filter", "concurrency"].includes(key)) {
|
||||
if (!["columns", "filter", "concurrency", "parameters"].includes(key)) {
|
||||
throw new Error(`Unsupported data prewarm option: ${key}`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user