fix(python): drop unrequested _rowid from hybrid return_score=all (#5206)

Fixes #5200

A sync hybrid search reranked with `RRFReranker(return_score="all")`
returned `_rowid` even when the caller never asked for it. An explicit
`with_row_id(False)` before `rerank()` did not remove it.

`LanceHybridQueryBuilder.rerank()` set `self._with_row_id = True` when
the reranker used `return_score="all"`. `_finish_hybrid_results` then
treated `_rowid` as user-requested, because `_user_requested_row_id()`
only looks at that flag. The reranker does need row ids internally to
fuse the two legs and restore `_distance` / `_score`, but `_run_hybrid`
already requests them on both legs. The column should be dropped at
the end unless the caller asked for it.

The async builder already did this: `AsyncHybridQuery.rerank()` never
set `_with_row_id`, and `_run_hybrid` drops `_rowid` unless
`_user_requested_row_id()` is true. This PR aligns the sync path with
that behavior by removing the assignment. I also updated the nearby
comments that said `rerank(return_score="all")` turned `_with_row_id`
on.

## Tests

Added sync and async regressions in `python/tests/test_hybrid_query.py`:

- `return_score="all"` without `with_row_id` keeps `_distance`,
  `_score`, and `_relevance_score`, and omits `_rowid`.
- Sync also checks an explicit `with_row_id(False)`.
- `with_row_id(True)` (async: `with_row_id()`) still returns `_rowid`.

## How tested

Python 3.13.5, `lancedb==0.42.0b30` wheel from
`https://pypi.fury.io/lancedb/`, with `PYTHONPATH` pointed at
`python/python` from this checkout so the edited files were the ones
under test. The Rust extension was not rebuilt.

Issue repro, before this change:

- default `RRFReranker()`: `['text', 'vector', '_relevance_score']`
- `return_score="all"`: included `_rowid`
- `with_row_id(False).rerank(... return_score="all")`: still included
`_rowid`

After this change, the same two `return_score="all"` calls return
`['text', 'vector', '_distance', '_score', '_relevance_score']`.
`with_row_id(True)` still returns `_rowid`.

- `pytest python/tests/test_hybrid_query.py
python/tests/test_rerankers.py python/tests/test_wal_hybrid.py`: 82
passed, 14 skipped
- `ruff format --check python` and `ruff check python`: passed

Not run: a remote/Enterprise client, or an RTE table on the
primary-key fallback path.

## AI assistance

I used an AI coding assistant (Grok Bot) to investigate the issue,
draft the patch and tests, and help write this description. I reviewed
the change, reproduced the bug before and after, and ran the tests and
linters above myself.
This commit is contained in:
adityashankarv
2026-11-30 20:07:55 +08:00
committed by GitHub
parent 058d5c3f4c
commit af34021f32
2 changed files with 55 additions and 10 deletions
+6 -10
View File
@@ -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
+49
View File
@@ -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")