mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
feat: support nullable scalar Function declarations (#5030)
Function declarations currently reject nullable scalar returns. Accept `Optional[T]`, `T | None`, and explicit nullable Arrow fields, preserving `nullable: true` in registration signatures, JSON round trips, and remote `add_columns` requests. The Rust planner accepts nullable scalar outputs, and binding readback recognizes the server-owned assignment marker for a single nullable scalar result. Clients do not allocate that marker. Non-nullable whole results, whole structs, and Blob v2 retain their existing assignment restrictions. Validated with 127 Python tests, 30 documentation tests (1 skipped), and seven focused Rust tests covering planning, binding readback, rejection boundaries, and the remote request body. The final condition-collapse change addresses strict Clippy; its delta check and CI are pending. Python tests use a source overlay on a retained native extension, and Rust remote coverage uses HTTP mocks. Server support and end-to-end validation remain separate. This PR does not claim that server publication distinguishes computed NULL values from pending rows.
This commit is contained in:
@@ -28,6 +28,7 @@ import lancedb
|
||||
from lancedb import BlobReader
|
||||
from lancedb.db import AsyncConnection
|
||||
from lancedb.functions import (
|
||||
FunctionSignature,
|
||||
FunctionVersion,
|
||||
HuggingFaceModel,
|
||||
PythonRuntimeSpec,
|
||||
@@ -1214,6 +1215,42 @@ def test_explicit_arrow_schema_is_deterministic():
|
||||
assert signature.output.nullable is False
|
||||
|
||||
|
||||
def test_nullable_scalar_output_annotation_and_explicit_schema():
|
||||
input_schema = pa.schema([pa.field("value", pa.int64(), nullable=False)])
|
||||
|
||||
@function(input_schema=input_schema)
|
||||
def optional_annotation(value: int) -> Optional[int]:
|
||||
return None if value == 0 else value * 2
|
||||
|
||||
@function(input_schema=input_schema)
|
||||
def optional_union(value: int) -> int | None:
|
||||
return None if value == 0 else value * 2
|
||||
|
||||
@function(
|
||||
input_schema=input_schema,
|
||||
output_schema=pa.field("result", pa.int64(), nullable=True),
|
||||
)
|
||||
def explicit_schema(value: int):
|
||||
return None if value == 0 else value * 2
|
||||
|
||||
for definition in (optional_annotation, optional_union, explicit_schema):
|
||||
signature = definition.registration_request.signature
|
||||
assert (signature.output.kind, signature.output.arrow_type) == (
|
||||
"scalar",
|
||||
"int64",
|
||||
)
|
||||
assert signature.output.nullable is True
|
||||
assert json.loads(signature.to_canonical_json())["output"] == {
|
||||
"kind": "scalar",
|
||||
"arrow_type": "int64",
|
||||
"nullable": True,
|
||||
}
|
||||
assert (
|
||||
FunctionSignature.from_json(signature.to_canonical_json()).output.nullable
|
||||
is True
|
||||
)
|
||||
|
||||
|
||||
def test_blob_fields_use_the_scalar_function_semantic_type():
|
||||
@function(
|
||||
input_schema=pa.schema([lancedb.blob("image", nullable=False)]),
|
||||
@@ -1848,12 +1885,6 @@ def test_annotation_and_explicit_schema_validation_fail_closed():
|
||||
def unsupported(value: set[str]) -> str:
|
||||
return ""
|
||||
|
||||
with pytest.raises(ValueError, match="output must be non-nullable"):
|
||||
|
||||
@function
|
||||
def nullable_output(value: int) -> Optional[int]:
|
||||
return value
|
||||
|
||||
with pytest.raises(TypeError, match=r"missing Function annotations: \['return'\]"):
|
||||
|
||||
@function(input_schema=pa.schema([pa.field("value", pa.int64())]))
|
||||
@@ -1873,10 +1904,25 @@ def test_annotation_and_explicit_schema_validation_fail_closed():
|
||||
|
||||
@function(
|
||||
input_schema=pa.schema([pa.field("value", pa.int64())]),
|
||||
output_schema=pa.field("result", pa.int64(), nullable=True),
|
||||
output_schema=pa.field(
|
||||
"result",
|
||||
pa.struct([pa.field("value", pa.int64(), nullable=False)]),
|
||||
nullable=True,
|
||||
),
|
||||
)
|
||||
def nullable_explicit(value):
|
||||
return value
|
||||
def nullable_struct(value):
|
||||
return {"value": value}
|
||||
|
||||
with pytest.raises(
|
||||
ValueError, match="Blob v2 Function output must be non-nullable"
|
||||
):
|
||||
|
||||
@function(
|
||||
input_schema=pa.schema([lancedb.blob("image", nullable=False)]),
|
||||
output_schema=lancedb.blob("result", nullable=True),
|
||||
)
|
||||
def nullable_blob(image):
|
||||
return image
|
||||
|
||||
for invalid_field in [
|
||||
pa.field("", pa.int32(), nullable=False),
|
||||
|
||||
Reference in New Issue
Block a user