mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +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:
@@ -0,0 +1,111 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import { Int32, Table, tableFromIPC, vectorFromArray } from "../lancedb/arrow";
|
||||
import { toParameterBuffer } from "../lancedb/sql";
|
||||
import { LocalTable } from "../lancedb/table";
|
||||
|
||||
describe("SQL parameter encoding", () => {
|
||||
test.each([false, true])(
|
||||
"prewarm submission and preview forward typed bindings (named: %s)",
|
||||
async (named) => {
|
||||
const attack = "training' OR TRUE; DROP TABLE items; --";
|
||||
const parameters = named ? { minimum: 42n, tag: attack } : [42n, attack];
|
||||
const filter = named
|
||||
? "id >= $minimum AND tag = $tag"
|
||||
: "id >= $1 AND tag = $2";
|
||||
const submission = jest.fn().mockResolvedValue({});
|
||||
const preview = jest.fn().mockResolvedValue(
|
||||
JSON.parse(`{
|
||||
"table_version": 42,
|
||||
"plan": "metadata preview",
|
||||
"pruning": {
|
||||
"mode": "zonemap_ranges",
|
||||
"input_fragments": 3,
|
||||
"selected_fragments": 1,
|
||||
"pruned_fragments": 2,
|
||||
"selected_row_ranges": 2
|
||||
}
|
||||
}`),
|
||||
);
|
||||
const table = new LocalTable({
|
||||
prewarmDataAsync: submission,
|
||||
explainPrewarmData: preview,
|
||||
} as never);
|
||||
await table.prewarmDataAsync({ filter, parameters });
|
||||
const plan = await table.explainPrewarmData({ filter, parameters });
|
||||
expect(plan.tableVersion).toBe(42);
|
||||
expect(plan.pruning.selectedFragments).toBe(1);
|
||||
for (const call of [submission, preview]) {
|
||||
expect(call).toHaveBeenCalledTimes(1);
|
||||
const options = call.mock.calls[0][0];
|
||||
expect(options.filter).toBe(filter);
|
||||
expect(options.filter).not.toContain(attack);
|
||||
expect(Buffer.isBuffer(options.parameters)).toBe(true);
|
||||
const batch = tableFromIPC(options.parameters);
|
||||
expect(batch.schema.fields.map((field) => field.name)).toEqual(
|
||||
named ? ["minimum", "tag"] : ["1", "2"],
|
||||
);
|
||||
expect(batch.getChildAt(0)?.get(0)).toBe(42n);
|
||||
expect(batch.getChildAt(1)?.get(0)).toBe(attack);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
test("positional and named bindings preserve values without SQL interpolation", async () => {
|
||||
const attack = "training' OR TRUE; DROP TABLE items; --";
|
||||
const date = new Date("2026-01-02T00:00:00Z");
|
||||
const batch = tableFromIPC(
|
||||
await toParameterBuffer([
|
||||
42n,
|
||||
attack,
|
||||
null,
|
||||
true,
|
||||
date,
|
||||
0.7,
|
||||
new Uint8Array([1, 2]),
|
||||
]),
|
||||
);
|
||||
expect(batch.schema.fields.map((field) => field.name)).toEqual([
|
||||
"1",
|
||||
"2",
|
||||
"3",
|
||||
"4",
|
||||
"5",
|
||||
"6",
|
||||
"7",
|
||||
]);
|
||||
expect(batch.getChildAt(0)?.get(0)).toBe(42n);
|
||||
expect(batch.getChildAt(1)?.get(0)).toBe(attack);
|
||||
expect(batch.getChildAt(2)?.get(0)).toBeNull();
|
||||
expect(batch.getChildAt(4)?.get(0)).toBe(date.getTime());
|
||||
const named = tableFromIPC(
|
||||
await toParameterBuffer({ minimum: 42n, tag: attack }),
|
||||
);
|
||||
expect(named.schema.fields.map((field) => field.name)).toEqual([
|
||||
"minimum",
|
||||
"tag",
|
||||
]);
|
||||
expect(named.getChild("tag")?.get(0)).toBe(attack);
|
||||
});
|
||||
|
||||
test("explicit Arrow input preserves its types", async () => {
|
||||
const input = new Table({ minimum: vectorFromArray([42], new Int32()) });
|
||||
for (const parameters of [input, input.batches[0]]) {
|
||||
const decoded = tableFromIPC(await toParameterBuffer(parameters));
|
||||
expect(decoded.schema.fields[0].type.toString()).toBe("Int32");
|
||||
expect(decoded.getChildAt(0)?.get(0)).toBe(42);
|
||||
}
|
||||
});
|
||||
|
||||
test.each([
|
||||
[],
|
||||
{ value: undefined },
|
||||
{ value: 2 ** 53 },
|
||||
{ value: 1n << 63n },
|
||||
{ value: {} },
|
||||
{ value: new Date("invalid") },
|
||||
])("invalid parameters fail before submission: %p", async (parameters) => {
|
||||
await expect(toParameterBuffer(parameters as never)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -4744,6 +4744,9 @@ describe.each(arrowVersions)(
|
||||
await expect(table.explainPrewarmData()).rejects.toThrow(
|
||||
"only supported on remote tables",
|
||||
);
|
||||
await expect(
|
||||
table.explainPrewarmData({ filter: "id > $1", parameters: [1] }),
|
||||
).rejects.toThrow("only supported on remote tables");
|
||||
await expect(
|
||||
table.explainPrewarmData({ unsupported: true } as never),
|
||||
).rejects.toThrow("Unsupported data prewarm option");
|
||||
@@ -4770,8 +4773,9 @@ describe.each(arrowVersions)(
|
||||
await expect(
|
||||
table.prewarmDataAsync({
|
||||
columns: ["id"],
|
||||
filter: "id > 0",
|
||||
filter: "id > $1",
|
||||
concurrency: 2,
|
||||
parameters: [1],
|
||||
}),
|
||||
).rejects.toThrow("only supported on remote tables");
|
||||
await expect(
|
||||
|
||||
Reference in New Issue
Block a user