mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
fix(python): reject unsupported fragment constraints (#5120)
`with_fragments()` / `fragment_ids()` were only honored by the embedded plain-scan blob Pandas path. Every other sync, async, and remote terminal could silently ignore the selection and scan the full table, and async query conversions plus FTS `count_rows()` could drop the constraint before execution. This change makes unsupported paths fail with `NotImplementedError` before I/O, preserves fragment state across async Standard/Vector/FTS/Hybrid conversions, and keeps the supported embedded blob `to_pandas()` behavior unchanged. The FTS `count_rows()` sync and async entry points now reject fragment constraints explicitly. Refs OSS-2859
This commit is contained in:
@@ -81,6 +81,31 @@ async def test_async_hybrid_query_with_row_ids(table: AsyncTable):
|
||||
assert result["_rowid"].to_pylist() == [0, 3]
|
||||
|
||||
|
||||
def test_hybrid_fragment_constraints_fail_loud(sync_table: Table):
|
||||
query = (
|
||||
sync_table.search("dog", query_type="hybrid")
|
||||
.vector([0.5, -0.5])
|
||||
.fragment_ids([0])
|
||||
)
|
||||
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
query.to_arrow()
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
query.explain_plan()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_hybrid_fragment_constraints_fail_loud(table: AsyncTable):
|
||||
query = (
|
||||
table.query().nearest_to([0.5, -0.5]).nearest_to_text("dog").fragment_ids([0])
|
||||
)
|
||||
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
await query.to_arrow()
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
await query.analyze_plan()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_hybrid_query_filters(table: AsyncTable):
|
||||
# test that query params are passed down from the regular builder to
|
||||
|
||||
@@ -69,6 +69,36 @@ def _blob_query_data():
|
||||
)
|
||||
|
||||
|
||||
def _fragment_query_data():
|
||||
return pa.table(
|
||||
{
|
||||
"id": pa.array([1, 2, 3, 4], pa.int64()),
|
||||
"text": pa.array(["one", "two", "three", "four"], pa.utf8()),
|
||||
"vector": pa.array(
|
||||
[[1.0, 0.0], [2.0, 0.0], [3.0, 0.0], [4.0, 0.0]],
|
||||
type=pa.list_(pa.float32(), list_size=2),
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _fragment_query_table(db, name):
|
||||
data = _fragment_query_data()
|
||||
table = db.create_table(name, data.slice(0, 2))
|
||||
table.add(data.slice(2, 2))
|
||||
return table
|
||||
|
||||
|
||||
def _apply_fragment_restriction(query, restriction, fragment):
|
||||
if restriction == "ids":
|
||||
return query.fragment_ids([fragment.fragment_id])
|
||||
if restriction == "empty":
|
||||
return query.fragment_ids([])
|
||||
if restriction == "fragments":
|
||||
return query.with_fragments([fragment])
|
||||
raise AssertionError(f"unknown restriction: {restriction}")
|
||||
|
||||
|
||||
def _create_blob_v2_query_table(db, name):
|
||||
schema = pa.schema(
|
||||
[
|
||||
@@ -435,6 +465,207 @@ def test_plain_scan_query_to_pandas_scanner_state(tmp_db):
|
||||
)
|
||||
assert df_by_fragment["id"].tolist() == [1, 2]
|
||||
|
||||
empty = (
|
||||
table.search()
|
||||
.select(["id", "blob"])
|
||||
.fragment_ids([])
|
||||
.to_pandas(blob_mode="descriptions")
|
||||
)
|
||||
assert empty.empty
|
||||
with pytest.raises(ValueError, match="fragment_ids not found in dataset: 999"):
|
||||
(
|
||||
table.search()
|
||||
.select(["id", "blob"])
|
||||
.fragment_ids([999])
|
||||
.to_pandas(blob_mode="descriptions")
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"terminal",
|
||||
[
|
||||
"to_arrow",
|
||||
"to_batches",
|
||||
"to_pandas",
|
||||
"to_polars",
|
||||
"to_list",
|
||||
"explain_plan",
|
||||
"analyze_plan",
|
||||
],
|
||||
)
|
||||
def test_fragment_constraints_fail_loud_on_unsupported_paths(tmp_db, terminal):
|
||||
pytest.importorskip("lance")
|
||||
table = _fragment_query_table(tmp_db, f"fragment_guard_{terminal}")
|
||||
fragment_id = table.to_lance().get_fragments()[0].fragment_id
|
||||
query = table.search().fragment_ids([fragment_id])
|
||||
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
getattr(query, terminal)()
|
||||
|
||||
|
||||
def test_with_fragments_fails_loud_on_plain_and_vector_queries(tmp_db):
|
||||
pytest.importorskip("lance")
|
||||
table = _fragment_query_table(tmp_db, "fragment_guard_with_fragments")
|
||||
fragment = table.to_lance().get_fragments()[0]
|
||||
|
||||
for query in (
|
||||
table.search().with_fragments([fragment]),
|
||||
table.search([0.0, 0.0]).with_fragments([fragment]),
|
||||
):
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
query.to_arrow()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("restriction", ["ids", "empty", "fragments"])
|
||||
def test_fts_count_rows_rejects_fragment_constraints(tmp_db, restriction):
|
||||
pytest.importorskip("lance")
|
||||
table = _fragment_query_table(tmp_db, f"fts_count_rows_{restriction}")
|
||||
table.create_index("text", config=FTS(with_position=False))
|
||||
fragment = table.to_lance().get_fragments()[1]
|
||||
query = _apply_fragment_restriction(
|
||||
table.search("one", query_type="fts"), restriction, fragment
|
||||
)
|
||||
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
query.count_rows()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"terminal",
|
||||
[
|
||||
"to_arrow",
|
||||
"to_batches",
|
||||
"to_pandas",
|
||||
"to_polars",
|
||||
"to_list",
|
||||
"explain_plan",
|
||||
"analyze_plan",
|
||||
],
|
||||
)
|
||||
async def test_async_fragment_constraints_fail_loud_on_unsupported_paths(
|
||||
tmp_db_async, terminal
|
||||
):
|
||||
pytest.importorskip("lance")
|
||||
data = _fragment_query_data()
|
||||
table = await tmp_db_async.create_table(
|
||||
f"async_fragment_guard_{terminal}", data.slice(0, 2)
|
||||
)
|
||||
await table.add(data.slice(2, 2))
|
||||
fragments = (await table.to_lance()).get_fragments()
|
||||
query = table.query().fragment_ids([fragments[0].fragment_id])
|
||||
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
await getattr(query, terminal)()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_with_fragments_fails_loud_on_plain_and_vector_queries(
|
||||
tmp_db_async,
|
||||
):
|
||||
pytest.importorskip("lance")
|
||||
data = _fragment_query_data()
|
||||
table = await tmp_db_async.create_table(
|
||||
"async_fragment_guard_with_fragments", data.slice(0, 2)
|
||||
)
|
||||
await table.add(data.slice(2, 2))
|
||||
fragments = (await table.to_lance()).get_fragments()
|
||||
|
||||
for query in (
|
||||
table.query().with_fragments([fragments[0]]),
|
||||
table.query().nearest_to([0.0, 0.0]).with_fragments([fragments[0]]),
|
||||
):
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
await query.to_arrow()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_plain_scan_fragment_constraints_preserved(tmp_db_async):
|
||||
pytest.importorskip("lance")
|
||||
data = _blob_query_data()
|
||||
table = await tmp_db_async.create_table(
|
||||
"async_fragment_supported",
|
||||
data.slice(0, 2),
|
||||
storage_options=LEGACY_BLOB_STORAGE_OPTIONS,
|
||||
)
|
||||
await table.add(data.slice(2, 2))
|
||||
fragments = (await table.to_lance()).get_fragments()
|
||||
|
||||
df = await (
|
||||
table.query()
|
||||
.select(["id", "blob"])
|
||||
.fragment_ids([fragments[1].fragment_id])
|
||||
.to_pandas(blob_mode="descriptions")
|
||||
)
|
||||
|
||||
assert df["id"].tolist() == [3, 4]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("restriction", ["ids", "empty", "fragments"])
|
||||
async def test_async_fts_count_rows_rejects_fragment_constraints(
|
||||
tmp_db_async, restriction
|
||||
):
|
||||
pytest.importorskip("lance")
|
||||
data = _fragment_query_data()
|
||||
table = await tmp_db_async.create_table(
|
||||
f"async_fts_count_rows_{restriction}", data.slice(0, 2)
|
||||
)
|
||||
await table.add(data.slice(2, 2))
|
||||
await table.create_index("text", config=FTS(with_position=False))
|
||||
fragment = (await table.to_lance()).get_fragments()[1]
|
||||
query = _apply_fragment_restriction(
|
||||
table.query().nearest_to_text("one"), restriction, fragment
|
||||
)
|
||||
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
await query.count_rows()
|
||||
|
||||
transitioned = _apply_fragment_restriction(
|
||||
table.query(), restriction, fragment
|
||||
).nearest_to_text("one")
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
await transitioned.count_rows()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("restriction", ["ids", "empty", "fragments"])
|
||||
@pytest.mark.parametrize(
|
||||
"transition",
|
||||
["query_to_vector", "query_to_fts", "fts_to_hybrid", "vector_to_hybrid"],
|
||||
)
|
||||
async def test_async_fragment_constraints_survive_query_transitions(
|
||||
tmp_db_async, transition, restriction
|
||||
):
|
||||
pytest.importorskip("lance")
|
||||
data = _fragment_query_data()
|
||||
table = await tmp_db_async.create_table(
|
||||
f"async_transition_{transition}_{restriction}", data.slice(0, 2)
|
||||
)
|
||||
await table.add(data.slice(2, 2))
|
||||
await table.create_index("text", config=FTS(with_position=False))
|
||||
fragment = (await table.to_lance()).get_fragments()[1]
|
||||
|
||||
if transition == "query_to_vector":
|
||||
query = _apply_fragment_restriction(
|
||||
table.query(), restriction, fragment
|
||||
).nearest_to([0.0, 0.0])
|
||||
elif transition == "query_to_fts":
|
||||
query = _apply_fragment_restriction(
|
||||
table.query(), restriction, fragment
|
||||
).nearest_to_text("one")
|
||||
elif transition == "fts_to_hybrid":
|
||||
query = _apply_fragment_restriction(
|
||||
table.query().nearest_to_text("one"), restriction, fragment
|
||||
).nearest_to([0.0, 0.0])
|
||||
else:
|
||||
query = _apply_fragment_restriction(
|
||||
table.query().nearest_to([0.0, 0.0]), restriction, fragment
|
||||
).nearest_to_text("one")
|
||||
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
await query.to_arrow()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_plain_scan_query_to_pandas_blob_projection(tmp_db_async):
|
||||
|
||||
@@ -4163,6 +4163,36 @@ def query_test_table(
|
||||
yield table
|
||||
|
||||
|
||||
def test_remote_fragment_constraints_fail_before_query():
|
||||
query_calls = 0
|
||||
|
||||
def query_handler(body):
|
||||
nonlocal query_calls
|
||||
query_calls += 1
|
||||
return pa.table({"text": ["unexpected"]})
|
||||
|
||||
with query_test_table(query_handler) as table:
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
table.search().fragment_ids([0]).to_arrow()
|
||||
|
||||
assert query_calls == 0
|
||||
|
||||
|
||||
def test_remote_fts_count_rows_rejects_fragment_constraints():
|
||||
query_calls = 0
|
||||
|
||||
def query_handler(body):
|
||||
nonlocal query_calls
|
||||
query_calls += 1
|
||||
return pa.table({"text": ["unexpected"]})
|
||||
|
||||
with query_test_table(query_handler) as table:
|
||||
with pytest.raises(NotImplementedError, match="fragment selection"):
|
||||
table.search("hello", query_type="fts").fragment_ids([0]).count_rows()
|
||||
|
||||
assert query_calls == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"query,search_kwargs,expected_column",
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user