fix(python): restore blob extensions in table schemas (#4728)

Reading a Blob v2 table in a fresh process could return its storage
struct if neither `blob()` nor pylance had registered the extension.
Both local and remote schema reads were affected, so `field.type` could
depend on import order.

Restore metadata-only blob fields on every table schema read through the
existing BlobType resolver and Arrow's schema deserializer. Preserve
nested fields, nullability, custom metadata, and both supported storage
layouts. The fallback deserializer now honors the stored shape too.

Fresh-process regressions cover local and remote tables, sync/async
reads, and operation with and without pylance. The four native cases
failed before removing the native-table guard. Existing merge-insert
tests now require the restored extension type while continuing to verify
updated and inserted blob contents.

Validation:

- All 12 fresh-process local/remote schema regression cases pass.
- `uv run --extra tests pytest python/tests/test_table.py
python/tests/test_blob.py python/tests/test_namespace.py
python/tests/test_util.py -q -m 'not slow and not s3_test'`: 835 passed,
11 skipped.
- Python package Ruff lint and formatting checks pass.
- Repository-wide `ruff check .` reports 48 pre-existing diagnostics in
unchanged `docs/web-tests` files.

Fixes #4719

<!-- lance-gatekeeper-fix:v1 agent=67c656bb5889e698efce5f2fd9d58ea7
generation=1 -->

---------

Co-authored-by: Gatefixer <313497061+lancedb-gatefixer[bot]@users.noreply.github.com>
This commit is contained in:
lancedb-gatefixer[bot]
2026-10-28 16:02:41 -07:00
committed by GitHub
parent ca60b402aa
commit 3035636bed
4 changed files with 229 additions and 14 deletions
+81 -9
View File
@@ -231,6 +231,78 @@ def test_blob_type_works_without_pylance():
assert result.returncode == 0, result.stderr
@pytest.mark.parametrize("use_async", [False, True])
@pytest.mark.parametrize("with_pylance", [False, True])
def test_local_schema_restores_blob_extensions(tmp_path, use_async, with_pylance):
db = lancedb.connect(tmp_path)
db.create_table(
"images",
schema=pa.schema(
[
pa.field("id", pa.int64()),
lancedb.blob("image", nullable=False, inline_size_threshold=8192),
],
metadata={"schema-key": "schema-value"},
),
)
# Reopening must restore the type before blob() or pylance registers it in
# the reader process, including when pylance is not installed.
script = textwrap.dedent(
"""\
import asyncio
import importlib.abc
import sys
import pyarrow as pa
if sys.argv[3] == "False":
class NoLance(importlib.abc.MetaPathFinder):
def find_spec(self, fullname, path, target=None):
if fullname == "lance" or fullname.startswith("lance."):
raise ModuleNotFoundError(fullname, name="lance")
sys.meta_path.insert(0, NoLance())
import lancedb
assert "lance.blob" not in sys.modules
async def read_schema():
db = await lancedb.connect_async(sys.argv[1])
table = await db.open_table("images")
return await table.schema()
if sys.argv[2] == "True":
schema = asyncio.run(read_schema())
else:
schema = lancedb.connect(sys.argv[1]).open_table("images").schema
image = schema.field("image")
assert isinstance(image.type, pa.ExtensionType), image
assert image.type == lancedb.blob("x").type
assert type(image.type) is lancedb.BlobType
assert not image.nullable
assert image.metadata == {
b"lance-encoding:blob-inline-size-threshold": b"8192"
}
assert schema.field("id") == pa.field("id", pa.int64())
assert schema.metadata == {b"schema-key": b"schema-value"}
"""
)
result = subprocess.run(
[
sys.executable,
"-c",
script,
str(tmp_path),
str(use_async),
str(with_pylance),
],
capture_output=True,
text=True,
timeout=30,
)
assert result.returncode == 0, result.stderr
def test_blob_resolves_pylance_type_without_eager_import():
script = textwrap.dedent(
"""\
@@ -1295,12 +1367,13 @@ def test_merge_insert_bytes_after_reopen_without_touching_blob_type(tmp_path):
script = textwrap.dedent(
f"""\
import lancedb
import pyarrow as pa
db = lancedb.connect({str(tmp_path)!r})
table = db.open_table("images")
image_type = table.schema.field("image").type
if type(image_type).__name__ != "StructType":
raise SystemExit(f"expected StructType, got {{type(image_type)}}")
if not isinstance(image_type, pa.ExtensionType):
raise SystemExit(f"expected ExtensionType, got {{type(image_type)}}")
result = (
table.merge_insert("id")
.when_matched_update_all()
@@ -1339,12 +1412,13 @@ def test_merge_insert_bytes_after_reopen_without_pylance(tmp_path):
script = _HIDE_LANCE_BLOB + textwrap.dedent(
f"""\
import lancedb
import pyarrow as pa
db = lancedb.connect({str(tmp_path)!r})
table = db.open_table("images")
image_type = table.schema.field("image").type
if type(image_type).__name__ != "StructType":
raise SystemExit(f"expected StructType, got {{type(image_type)}}")
if not isinstance(image_type, pa.ExtensionType):
raise SystemExit(f"expected ExtensionType, got {{type(image_type)}}")
result = (
table.merge_insert("id")
.when_matched_update_all()
@@ -1374,7 +1448,7 @@ def test_merge_insert_bytes_after_reopen_without_pylance(tmp_path):
assert result.returncode == 0, result.stderr
def test_merge_insert_blob_array_into_reopened_unregistered_table(tmp_path):
def test_merge_insert_blob_array_into_reopened_table(tmp_path):
db = lancedb.connect(tmp_path)
schema = pa.schema([pa.field("id", pa.int64()), lancedb.blob("image")])
table = db.create_table("images", schema=schema)
@@ -1388,10 +1462,8 @@ def test_merge_insert_blob_array_into_reopened_unregistered_table(tmp_path):
db = lancedb.connect({str(tmp_path)!r})
table = db.open_table("images")
image_type = table.schema.field("image").type
if type(image_type).__name__ != "StructType":
raise SystemExit(
f"expected StructType before lance import, got {{type(image_type)}}"
)
if not isinstance(image_type, pa.ExtensionType):
raise SystemExit(f"expected ExtensionType, got {{type(image_type)}}")
import lance