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:
@@ -8212,6 +8212,8 @@ async def test_remote_data_prewarm_sql_receipt_and_durable_job(asynchronous, mod
|
||||
{"concurrency": 2**32},
|
||||
{"concurrency": True},
|
||||
{"concurrency": 1.5},
|
||||
{"parameters": [1]},
|
||||
{"filter": "id > $1", "parameters": pa.table({"1": [1, 2]})},
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
@@ -8236,6 +8238,88 @@ def test_remote_data_prewarm_validates_before_submission(options, method):
|
||||
assert not server.tickets
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("asynchronous", [False, True])
|
||||
@pytest.mark.parametrize(
|
||||
"method", ["prewarm_data", "prewarm_data_async", "explain_prewarm_data"]
|
||||
)
|
||||
@pytest.mark.parametrize("named", [False, True])
|
||||
async def test_prewarm_bound_parameters_reach_flight(asynchronous, method, named):
|
||||
flight = pytest.importorskip("pyarrow.flight")
|
||||
attack = "training' OR TRUE; DROP TABLE items; --"
|
||||
|
||||
class ParameterServer(flight.FlightServerBase):
|
||||
def __init__(self):
|
||||
super().__init__("grpc://127.0.0.1:0")
|
||||
self.calls = []
|
||||
|
||||
def do_exchange(self, context, descriptor, reader, writer):
|
||||
self.calls.append((descriptor.command, reader.read_all()))
|
||||
if method == "explain_prewarm_data":
|
||||
result = pa.table(
|
||||
{
|
||||
"plan_type": ["logical_plan"],
|
||||
"plan": [
|
||||
json.dumps(
|
||||
{
|
||||
"format_version": 1,
|
||||
"table_version": 42,
|
||||
"plan": "metadata preview",
|
||||
"pruning": {
|
||||
"mode": "zonemap_ranges",
|
||||
"input_fragments": 3,
|
||||
"selected_fragments": 1,
|
||||
"pruned_fragments": 2,
|
||||
"selected_row_ranges": 2,
|
||||
},
|
||||
}
|
||||
)
|
||||
],
|
||||
}
|
||||
)
|
||||
else:
|
||||
result = pa.table(
|
||||
{
|
||||
"job_id": ["bound-job"],
|
||||
"table_version": pa.array([42], pa.uint64()),
|
||||
}
|
||||
)
|
||||
writer.begin(result.schema)
|
||||
writer.write_table(result)
|
||||
writer.close()
|
||||
|
||||
server = ParameterServer()
|
||||
with _mock_remote_sql_connection(server, _create_table_handler) as kwargs:
|
||||
parameters = {"min": 8, "tag": attack} if named else [8, attack]
|
||||
predicate = "id >= $min AND tag = $tag" if named else "id >= $1 AND tag = $2"
|
||||
if asynchronous:
|
||||
db = await lancedb.connect_async(**kwargs)
|
||||
table = await db.create_table("test", [{"id": 1}])
|
||||
result = await getattr(table, method)(
|
||||
["payload"], filter=predicate, parameters=parameters
|
||||
)
|
||||
else:
|
||||
db = lancedb.connect(**kwargs)
|
||||
table = db.create_table("test", [{"id": 1}])
|
||||
result = getattr(table, method)(
|
||||
["payload"], filter=predicate, parameters=parameters
|
||||
)
|
||||
if method == "prewarm_data":
|
||||
assert result is None
|
||||
elif method == "explain_prewarm_data":
|
||||
assert result.table_version == 42
|
||||
assert result.pruning.selected_fragments == 1
|
||||
else:
|
||||
assert result.id == "bound-job"
|
||||
((command, received),) = server.calls
|
||||
assert b"PREWARM" in command and predicate.encode() in command
|
||||
if method == "explain_prewarm_data":
|
||||
assert b"EXPLAIN (FORMAT JSON) PREWARM" in command
|
||||
assert attack.encode() not in command
|
||||
assert received.column(0).to_pylist() == [8]
|
||||
assert received.column(1).to_pylist() == [attack]
|
||||
|
||||
|
||||
def test_remote_data_prewarm_malformed_receipt_is_not_retried():
|
||||
flight = pytest.importorskip("pyarrow.flight")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user