mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
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.
105 lines
3.2 KiB
TypeScript
105 lines
3.2 KiB
TypeScript
// 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(),
|
|
);
|
|
}
|