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:
Xuanwo
2026-11-20 20:22:12 +08:00
committed by GitHub
parent 5e9b17b639
commit 28e9ebb3dd
4 changed files with 456 additions and 35 deletions
+170 -35
View File
@@ -273,6 +273,24 @@ def _query_needs_lance_scanner(builder: Any) -> bool:
)
def _raise_unsupported_fragment_constraints(
fragments: Optional[Any],
fragment_ids: Optional[List[int]],
*,
operation: str,
) -> None:
"""Reject fragment selection where execution would silently scan everything."""
if fragments is None and fragment_ids is None:
return
raise NotImplementedError(
"with_fragments() and fragment_ids() are only supported for embedded "
"plain-scan blob queries executed through to_pandas(). "
f"{operation} would ignore the fragment selection and scan the full "
"table. Remove the fragment constraint, or use table.to_lance() with "
"a Lance scanner for this query shape."
)
def _query_is_plain_scan(query: Query) -> bool:
return (
query.vector is None
@@ -1686,11 +1704,19 @@ class LanceQueryBuilder(ABC):
return self._user_requested_row_id() or self._blob_auto_row_id_enabled()
def _query_for_scan(self, blob_sources: Optional[dict[str, str]] = None) -> Query:
self._ensure_fragment_constraints_supported("this query")
query = self.to_query_object()
if self._scan_needs_row_id() or blob_sources:
query.with_row_id = True
return query
def _ensure_fragment_constraints_supported(self, operation: str) -> None:
_raise_unsupported_fragment_constraints(
self._fragments,
self._fragment_ids,
operation=operation,
)
def _can_fetch_plain_legacy_blobs(self, blob_mode: BlobMode) -> bool:
if blob_mode != "bytes":
return False
@@ -1826,12 +1852,51 @@ class LanceQueryBuilder(ABC):
return self
def with_fragments(self, fragments: Any) -> Self:
"""Set the Lance fragments to scan for plain scanner-backed queries."""
"""Restrict an embedded plain-scan blob query to the given Lance fragments.
Fragment selection is supported only when ``to_pandas()`` uses the
embedded Lance plain-scan path: the table is an embedded, non-memory
Lance dataset, the query is a plain scan (no vector, full-text, take,
postfilter, or ordering), and the selected output includes a blob
column. Other terminals and remote tables raise ``NotImplementedError``
before scanning instead of silently scanning the full table.
Blob-specific ``to_pandas()`` fallbacks keep their existing validation
errors.
Parameters
----------
fragments : list[lance.Fragment]
Fragments from ``table.to_lance().get_fragments()``. Pass fragments
from the same dataset snapshot.
Returns
-------
LanceQueryBuilder
The LanceQueryBuilder object.
"""
self._fragments = fragments
return self
def fragment_ids(self, fragment_ids: List[int]) -> Self:
"""Set the Lance fragment ids to scan for plain scanner-backed queries."""
"""Restrict an embedded plain-scan blob query to the given fragment ids.
Fragment selection uses the same supported ``to_pandas()`` path as
``with_fragments()``. On that path, an empty list selects no rows and
missing ids raise ``ValueError``. Other terminals and remote tables
raise ``NotImplementedError`` before scanning instead of silently
scanning the full table. Blob-specific ``to_pandas()`` fallbacks keep
their existing validation errors.
Parameters
----------
fragment_ids : list[int]
Fragment ids from ``table.to_lance().get_fragments()``.
Returns
-------
LanceQueryBuilder
The LanceQueryBuilder object.
"""
self._fragment_ids = fragment_ids
return self
@@ -1887,6 +1952,7 @@ class LanceQueryBuilder(ABC):
-------
plan : str
""" # noqa: E501
self._ensure_fragment_constraints_supported("explain_plan()")
return self._table._explain_plan(self.to_query_object(), verbose=verbose)
def order_by(self, ordering: Optional[List[ColumnOrdering]]) -> Self:
@@ -1961,6 +2027,7 @@ class LanceQueryBuilder(ABC):
plan : str
The physical query execution plan with runtime metrics.
"""
self._ensure_fragment_constraints_supported("analyze_plan()")
return self._table._analyze_plan(
self.to_query_object(), distributed_metrics=distributed_metrics
)
@@ -2629,6 +2696,7 @@ class LanceFtsQueryBuilder(LanceQueryBuilder):
For example, ``table.search("gripper closed", query_type="fts",``
``operator="AND").where("active = true").count_rows()``.
"""
self._ensure_fragment_constraints_supported("count_rows()")
self._table._ensure_no_legacy_fts_index()
query = self._table._table._sync_query_to_async(self.to_query_object())
assert isinstance(query, AsyncFTSQuery)
@@ -3522,6 +3590,7 @@ class LanceHybridQueryBuilder(LanceQueryBuilder):
def _create_query_builders(self):
"""Set up and configure the vector and FTS query builders."""
self._ensure_fragment_constraints_supported("this hybrid query")
vector_query, fts_query = self._validate_query(
self._query, self._vector, self._text
)
@@ -3640,6 +3709,19 @@ class AsyncQueryBase(object):
def _user_requested_row_id(self) -> bool:
return self._with_row_id is True
def _ensure_fragment_constraints_supported(self, operation: str) -> None:
_raise_unsupported_fragment_constraints(
self._fragments,
self._fragment_ids,
operation=operation,
)
def _inherit_fragment_constraints(self, query: Any) -> Any:
"""Carry a fragment selection across an async query conversion."""
query._fragments = self._fragments
query._fragment_ids = self._fragment_ids
return query
def _blob_auto_row_id_enabled(self) -> bool:
return self._blob_auto_row_id
@@ -3750,15 +3832,30 @@ class AsyncQueryBase(object):
return self
def with_fragments(self, fragments: Any) -> Self:
"""
Restrict scanner-backed plain query results to the given Lance fragments.
"""Restrict an embedded plain-scan blob query to the given Lance fragments.
Fragment selection is supported only when ``to_pandas()`` uses the
embedded Lance plain-scan path: the table is an embedded, non-memory
Lance dataset, the query is a plain scan (no vector, full-text, take,
postfilter, or ordering), and the selected output includes a blob
column. The constraint is preserved when converting between async
query types. Other terminals and remote tables raise
``NotImplementedError`` before scanning instead of silently scanning
the full table. Blob-specific ``to_pandas()`` fallbacks keep their
existing validation errors.
"""
self._fragments = fragments
return self
def fragment_ids(self, fragment_ids: List[int]) -> Self:
"""
Restrict scanner-backed plain query results to the given Lance fragment ids.
"""Restrict an embedded plain-scan blob query to the given fragment ids.
Fragment selection uses the same supported ``to_pandas()`` path as
``with_fragments()``. On that path, an empty list selects no rows and
missing ids raise ``ValueError``. Other terminals and remote tables
raise ``NotImplementedError`` before scanning instead of silently
scanning the full table. Blob-specific ``to_pandas()`` fallbacks keep
their existing validation errors.
"""
self._fragment_ids = fragment_ids
return self
@@ -3838,6 +3935,7 @@ class AsyncQueryBase(object):
timeout: Optional[timedelta] = None,
blob_sources: Optional[dict[str, str]] = None,
) -> AsyncRecordBatchReader:
self._ensure_fragment_constraints_supported("to_batches()")
if (
blob_sources
and not self._user_requested_row_id()
@@ -4245,6 +4343,7 @@ class AsyncQueryBase(object):
-------
plan : str
""" # noqa: E501
self._ensure_fragment_constraints_supported("explain_plan()")
return await self._inner.explain_plan(verbose)
async def analyze_plan(
@@ -4262,6 +4361,7 @@ class AsyncQueryBase(object):
-------
plan : str
"""
self._ensure_fragment_constraints_supported("analyze_plan()")
return await self._inner.analyze_plan(distributed_metrics)
@@ -4501,11 +4601,17 @@ class AsyncQuery(AsyncStandardQuery):
new_self = self._inner.nearest_to(query_vectors[0])
for v in query_vectors[1:]:
new_self.add_query_vector(v)
return AsyncVectorQuery(new_self, self._table)
return self._inherit_fragment_constraints(
AsyncVectorQuery(new_self, self._table)
)
else:
return AsyncVectorQuery(
self._inner.nearest_to(AsyncQuery._query_vec_to_array(query_vector)),
self._table,
return self._inherit_fragment_constraints(
AsyncVectorQuery(
self._inner.nearest_to(
AsyncQuery._query_vec_to_array(query_vector)
),
self._table,
)
)
def nearest_to_text(
@@ -4545,18 +4651,22 @@ class AsyncQuery(AsyncStandardQuery):
columns = []
if isinstance(query, str):
return AsyncFTSQuery(
self._inner.nearest_to_text(
{
"query": query,
"columns": columns,
"operator": fts_query.operator,
}
),
self._table,
return self._inherit_fragment_constraints(
AsyncFTSQuery(
self._inner.nearest_to_text(
{
"query": query,
"columns": columns,
"operator": fts_query.operator,
}
),
self._table,
)
)
# FullTextQuery object
return AsyncFTSQuery(self._inner.nearest_to_text({"query": query}), self._table)
return self._inherit_fragment_constraints(
AsyncFTSQuery(self._inner.nearest_to_text({"query": query}), self._table)
)
class AsyncFTSQuery(AsyncStandardQuery):
@@ -4577,6 +4687,7 @@ class AsyncFTSQuery(AsyncStandardQuery):
For example, ``await table.query().nearest_to_text("gripper closed",``
``operator="AND").where("active = true").count_rows()``.
"""
self._ensure_fragment_constraints_supported("count_rows()")
try:
return await self._inner.count_rows()
except Exception as error:
@@ -4662,11 +4773,17 @@ class AsyncFTSQuery(AsyncStandardQuery):
new_self = self._inner.nearest_to(query_vectors[0])
for v in query_vectors[1:]:
new_self.add_query_vector(v)
return AsyncHybridQuery(new_self, self._table)
return self._inherit_fragment_constraints(
AsyncHybridQuery(new_self, self._table)
)
else:
return AsyncHybridQuery(
self._inner.nearest_to(AsyncQuery._query_vec_to_array(query_vector)),
self._table,
return self._inherit_fragment_constraints(
AsyncHybridQuery(
self._inner.nearest_to(
AsyncQuery._query_vec_to_array(query_vector)
),
self._table,
)
)
async def _execute_batches(
@@ -5002,15 +5119,21 @@ class AsyncVectorQuery(AsyncStandardQuery, AsyncVectorQueryBase):
columns = []
if isinstance(query, str):
return AsyncHybridQuery(
self._inner.nearest_to_text(
{"query": query, "columns": columns, "operator": fts_query.operator}
),
self._table,
return self._inherit_fragment_constraints(
AsyncHybridQuery(
self._inner.nearest_to_text(
{
"query": query,
"columns": columns,
"operator": fts_query.operator,
}
),
self._table,
)
)
# FullTextQuery object
return AsyncHybridQuery(
self._inner.nearest_to_text({"query": query}), self._table
return self._inherit_fragment_constraints(
AsyncHybridQuery(self._inner.nearest_to_text({"query": query}), self._table)
)
async def _execute_batches(
@@ -5098,6 +5221,7 @@ class AsyncHybridQuery(AsyncStandardQuery, AsyncVectorQueryBase):
the hybrid query itself. The key the legs are fused on, if any, is left
on `self._pk_fusion`.
"""
self._ensure_fragment_constraints_supported("this hybrid query")
fts_query = AsyncFTSQuery(self._inner.to_fts_query(), self._table)
vec_query = AsyncVectorQuery(self._inner.to_vector_query(), self._table)
@@ -5394,15 +5518,26 @@ class BaseQueryBuilder(object):
return self
def with_fragments(self, fragments: Any) -> Self:
"""
Restrict scanner-backed plain query results to the given Lance fragments.
"""Restrict execution to the given Lance fragments.
Fragment selection is supported only by embedded plain-scan blob
queries through ``to_pandas()``. Other terminals and remote tables
raise ``NotImplementedError`` before scanning instead of silently
scanning the full table. Blob-specific ``to_pandas()`` fallbacks keep
their existing validation errors.
"""
self._inner.with_fragments(fragments)
return self
def fragment_ids(self, fragment_ids: List[int]) -> Self:
"""
Restrict scanner-backed plain query results to the given Lance fragment ids.
"""Restrict execution to the given Lance fragment ids.
Fragment selection is supported only by embedded plain-scan blob
queries through ``to_pandas()``. On that path, an empty list selects no
rows and missing ids raise ``ValueError``. Other terminals and remote
tables raise ``NotImplementedError`` before scanning instead of
silently scanning the full table. Blob-specific ``to_pandas()``
fallbacks keep their existing validation errors.
"""
self._inner.fragment_ids(fragment_ids)
return self
+25
View File
@@ -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
+231
View File
@@ -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):
+30
View File
@@ -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",
[