mirror of
https://github.com/lancedb/lancedb.git
synced 2026-10-25 21:22:29 +00:00
Infer Blob V2 input access from the Python Function signature. `video:
BlobReader` requests a lazy, seekable reader; `video: bytes` requests
the complete payload. Users do not need a separate `blob_inputs`
registration option.
## Python experience
For a video table joined to a table of frame timestamps, the Function
can pass its reader directly to a media decoder:
```python
import io
import av
from lancedb import BlobReader, udf
@udf(pip=["av", "Pillow"])
def extract_frame(video: BlobReader, timestamp_s: float) -> bytes:
with av.open(video) as container:
stream = container.streams.video[0]
container.seek(int(timestamp_s / stream.time_base), stream=stream)
for frame in container.decode(stream):
if frame.time is not None and frame.time >= timestamp_s:
output = io.BytesIO()
frame.to_image().save(output, format="JPEG")
return output.getvalue()
raise ValueError("No frame at or after the requested timestamp")
```
After registering `extract_frame`, the query keeps the usual Function
call:
```sql
SELECT f.video_id, f.timestamp_s,
extract_frame(v.video, f.timestamp_s) AS image
FROM videos AS v
JOIN frames AS f ON v.id = f.video_id
```
The returned JPEG can feed a subsequent image-model step. The reader
provides `read`, `readinto`, `seek`, `tell`, `size()`, and
`read_range(offset, length)`; range reads leave the sequential cursor
unchanged. Each row invocation owns its reader, which closes after the
call. Decoder seeks can request the parts of the MP4 needed to locate
and decode the requested frame; the amount read depends on the file
layout and codec.
Functions that need the whole value can keep a bytes signature:
```python
@udf
def video_length(video: bytes) -> int:
return len(video)
```
## Execution contract
- Signature inference maps `BlobReader` to a Blob V2 field and records
`blob_access="reader"`. `bytes` keeps its binary Arrow type and records
`blob_access="bytes"`; it can also bind to a Blob V2 source. Explicit
schemas support the same choice, including a Blob V2 field annotated as
`bytes`.
- Reader execution carries a compact blob reference into the Function
worker. Payload reads happen when Python calls the reader, with requests
split into at most 1 MiB ranges. The full video does not need to enter
the Arrow input batch just to invoke the Function.
- Bytes execution materializes the full source before the Function runs
and remains subject to eager-input memory admission. Calling
`reader.read()` without a size also materializes the remaining contents
in Python; lazy access depends on how the Function consumes it.
- Input access metadata survives Python/Rust Function serialization and
registration. Packaged Functions import the worker-provided reader
implementation. This PR defines the SDK contract; execution requires
corresponding server support.
- This changes blob inputs. The example still returns ordinary image
bytes; it does not add streamed outputs or run an image model.
Adds coverage for signature inference, explicit schemas, nullable
readers, signature round trips, seeking, bounded range reads, buffering,
and invalid responses.
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
|
|
|
import io
|
|
|
|
import pytest
|
|
|
|
from lancedb import BlobReader
|
|
|
|
|
|
def test_reader_is_lazy_and_seekable():
|
|
requests = []
|
|
size = 64 * 1024 * 1024
|
|
|
|
def read_range(offset, length):
|
|
requests.append((offset, length))
|
|
return bytes((index % 251 for index in range(offset, offset + length)))
|
|
|
|
with BlobReader(read_range, size) as reader:
|
|
assert requests == []
|
|
assert reader.size() == size
|
|
reader.seek(-4, io.SEEK_END)
|
|
assert reader.read(10) == bytes(index % 251 for index in range(size - 4, size))
|
|
assert reader.read(1) == b""
|
|
assert requests == [(size - 4, 4)]
|
|
assert reader.read_range(1, 3) == b"\x01\x02\x03"
|
|
assert reader.tell() == size
|
|
reader.seek(2)
|
|
target = bytearray(3)
|
|
assert reader.readinto(target) == 3
|
|
assert target == b"\x02\x03\x04"
|
|
reader.seek(-1, io.SEEK_CUR)
|
|
assert reader.tell() == 4
|
|
with pytest.raises(ValueError, match="closed"):
|
|
reader.read(1)
|
|
|
|
|
|
def test_reader_supports_buffered_io_and_bounds_range_requests():
|
|
requests = []
|
|
payload = b"x" * (3 * 1024 * 1024 + 7)
|
|
|
|
def read_range(offset, length):
|
|
requests.append((offset, length))
|
|
return payload[offset : offset + length]
|
|
|
|
with io.BufferedReader(BlobReader(read_range, len(payload))) as reader:
|
|
assert reader.read() == payload
|
|
assert requests
|
|
assert all(length <= 1024 * 1024 for _, length in requests)
|
|
|
|
|
|
def test_reader_rejects_invalid_ranges_and_short_reads():
|
|
reader = BlobReader(lambda offset, length: b"", 10)
|
|
with pytest.raises(ValueError):
|
|
reader.seek(-1)
|
|
with pytest.raises(ValueError):
|
|
reader.read_range(-1, 1)
|
|
with pytest.raises(OSError, match="payload length"):
|
|
reader.read(1)
|