mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
fix(python): create an empty table when a vector column has no rows to infer from (#5205)
This PR fixes [issue #5204](https://github.com/lancedb/lancedb/issues/5204). Fixes #5204 `create_table(name, data=...)` hung forever when `data` was an Arrow table with zero rows and a variable-length list column named like a vector (`vector`, `embedding`). With no `schema=`, `_infer_target_schema` peeks the first batch to infer the vector dimension. On a reader with no batches, `peek_reader` raises `StopIteration`. That happens inside `asyncio.to_thread`, where a `StopIteration` can't be set on the Future, so `create_table` never returned. With no rows the dimension can't be inferred, the same case as an all-null vector column, which already keeps the variable-length list. So `_infer_target_schema` now returns the input schema unchanged when the reader has no batches. ## Testing `test_infer_target_schema_empty_reader` covers `list<float32>` `vector`, `large_list<float64>` `embedding` and `list<int32>` `vector`. All three fail on main with `StopIteration` and pass with the fix. ``` pytest python/tests/test_util.py # 142 passed, 1 skipped pytest python/tests/test_table.py python/tests/test_db.py -m "not slow and not s3_test" # 636 passed, 17 skipped pre-commit run --files <changed files> # ruff, typos, whitespace: passed ``` I ran these with main's `python/` over the extension from the 0.42.0b26 wheel, since I don't have a Rust toolchain here, and with pylance 14.0.3 because the pinned 15.0.0b35 wasn't on the indexes I could reach. With the fix, the reproduction from the issue creates the table: 0 rows, `vector: list<item: float>`. The Node.js SDK has no equivalent peek on empty input (its empty-data path requires a schema), so no change there. --------- Co-authored-by: Lenin Mookiah <2639584+leninworld@users.noreply.github.com>
This commit is contained in:
co-authored by
Lenin Mookiah
parent
6907440bc1
commit
7509718f02
@@ -7447,7 +7447,10 @@ def _infer_target_schema(
|
||||
|
||||
if _name_suggests_vector_column(field.name) and is_list_type:
|
||||
if peeked is None:
|
||||
peeked, reader = peek_reader(reader)
|
||||
try:
|
||||
peeked, reader = peek_reader(reader)
|
||||
except StopIteration:
|
||||
return schema, reader
|
||||
# Use the most common length of the list as the dimensions
|
||||
dim = _modal_list_size(peeked.column(i))
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
|
||||
import asyncio
|
||||
import contextlib
|
||||
import inspect
|
||||
import pickle
|
||||
@@ -436,6 +437,27 @@ async def test_create_table_from_iterator_async(mem_db_async: lancedb.AsyncConne
|
||||
assert await table.count_rows() == 10
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_table_empty_vector_column_async(
|
||||
mem_db_async: lancedb.AsyncConnection,
|
||||
):
|
||||
# A zero-row list column named like a vector leaves nothing to infer the
|
||||
# dimension from; create_table used to hang instead of returning.
|
||||
data = pa.table(
|
||||
{
|
||||
"id": pa.array([], type=pa.int64()),
|
||||
"vector": pa.array([], type=pa.list_(pa.float32())),
|
||||
}
|
||||
)
|
||||
|
||||
table = await asyncio.wait_for(
|
||||
mem_db_async.create_table("test", data=data), timeout=10
|
||||
)
|
||||
|
||||
assert await table.count_rows() == 0
|
||||
assert await table.schema() == data.schema
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"batches",
|
||||
[
|
||||
|
||||
@@ -917,6 +917,23 @@ def test_infer_target_schema():
|
||||
assert output == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("name", "data_type"),
|
||||
[
|
||||
("vector", pa.list_(pa.float32())),
|
||||
("embedding", pa.large_list(pa.float64())),
|
||||
("vector", pa.list_(pa.int32())),
|
||||
],
|
||||
)
|
||||
def test_infer_target_schema_empty_reader(name, data_type):
|
||||
data = pa.table({name: pa.array([], type=data_type)})
|
||||
|
||||
schema, reader = _infer_target_schema(data.to_reader())
|
||||
|
||||
assert schema == data.schema
|
||||
assert sum(batch.num_rows for batch in reader) == 0
|
||||
|
||||
|
||||
def test_infer_target_schema_with_vector_embedding_names():
|
||||
"""Test that _infer_target_schema detects vector columns with 'vector'/'embedding'.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user