diff --git a/python/python/lancedb/query.py b/python/python/lancedb/query.py index 94f8f0149..ad456f635 100644 --- a/python/python/lancedb/query.py +++ b/python/python/lancedb/query.py @@ -3247,8 +3247,8 @@ class LanceHybridQueryBuilder(LanceQueryBuilder): # WAL-PK-FUSION: delete both, and the `with_row_id` override below. # Set by `_create_query_builders` when the legs are fused on the key. self._pk_fusion: Optional[_wal_hybrid.PkFusion] = None - # `_with_row_id` is also turned on by `rerank(return_score="all")` for - # the reranker's own use, so it cannot answer "did the caller ask?". + # Whether the caller asked for `_rowid`. Distinct from the row ids + # `_run_hybrid` always requests on both legs for fusion/reranking. self._caller_requested_row_id = False def order_by(self, ordering: _OrderingInput = None) -> Self: @@ -3305,9 +3305,10 @@ class LanceHybridQueryBuilder(LanceQueryBuilder): def with_row_id(self, with_row_id: bool) -> Self: # WAL-PK-FUSION: delete. """Set whether to return row ids. - Recorded separately from `_with_row_id`, which `rerank(return_score= - "all")` also sets for its own use — on an RTE table only a caller who - asked is refused, and the reranker still falls back to the primary key. + Recorded separately from the row ids `_run_hybrid` requests on both + search legs for fusion and reranking — on an RTE table only a caller + who asked is refused, and the reranker still falls back to the primary + key. """ self._caller_requested_row_id = with_row_id return super().with_row_id(with_row_id) @@ -3604,11 +3605,6 @@ class LanceHybridQueryBuilder(LanceQueryBuilder): self._norm = normalize self._reranker = reranker - if reranker.score == "all": - # WAL-PK-FUSION: without the fallback, `self.with_row_id(True)`. - # Not through `with_row_id`, which records caller intent: the - # reranker's need for row ids must not refuse an RTE table. - self._with_row_id = True return self diff --git a/python/python/tests/test_hybrid_query.py b/python/python/tests/test_hybrid_query.py index 7d5fbb3e2..7ed2e3292 100644 --- a/python/python/tests/test_hybrid_query.py +++ b/python/python/tests/test_hybrid_query.py @@ -81,6 +81,55 @@ async def test_async_hybrid_query_with_row_ids(table: AsyncTable): assert result["_rowid"].to_pylist() == [0, 3] +def test_hybrid_rerank_return_score_all_omits_unrequested_rowid(sync_table: Table): + reranker = RRFReranker(return_score="all") + + def hybrid(): + return sync_table.search(query_type="hybrid").vector([0.0, 0.4]).text("dog") + + default_cols = hybrid().rerank(reranker).to_arrow().column_names + assert "_rowid" not in default_cols + assert "_distance" in default_cols + assert "_score" in default_cols + assert "_relevance_score" in default_cols + + explicit_false = ( + hybrid().with_row_id(False).rerank(reranker).to_arrow().column_names + ) + assert "_rowid" not in explicit_false + assert "_distance" in explicit_false + assert "_score" in explicit_false + assert "_relevance_score" in explicit_false + + requested = hybrid().with_row_id(True).rerank(reranker).to_arrow().column_names + assert "_rowid" in requested + assert "_distance" in requested + assert "_score" in requested + assert "_relevance_score" in requested + + +@pytest.mark.asyncio +async def test_async_hybrid_rerank_return_score_all_omits_unrequested_rowid( + table: AsyncTable, +): + reranker = RRFReranker(return_score="all") + + def hybrid(): + return table.query().nearest_to([0.0, 0.4]).nearest_to_text("dog") + + result = await hybrid().rerank(reranker).to_arrow() + assert "_rowid" not in result.column_names + assert "_distance" in result.column_names + assert "_score" in result.column_names + assert "_relevance_score" in result.column_names + + requested = await hybrid().with_row_id().rerank(reranker).to_arrow() + assert "_rowid" in requested.column_names + assert "_distance" in requested.column_names + assert "_score" in requested.column_names + assert "_relevance_score" in requested.column_names + + def test_hybrid_fragment_constraints_fail_loud(sync_table: Table): query = ( sync_table.search("dog", query_type="hybrid")