From be644e2192dc956a7f3b61d5df4954f3ea483161 Mon Sep 17 00:00:00 2001 From: shadowmodder Date: Fri, 30 Oct 2026 21:01:15 -0500 Subject: [PATCH] fix(python): fetch minimum rank-fusion window per side in hybrid search --- python/python/lancedb/query.py | 23 ++++++-- python/python/tests/test_hybrid_query.py | 67 +++++++++++++++++++----- 2 files changed, 73 insertions(+), 17 deletions(-) diff --git a/python/python/lancedb/query.py b/python/python/lancedb/query.py index c9ca258e7..6d4ca9b13 100644 --- a/python/python/lancedb/query.py +++ b/python/python/lancedb/query.py @@ -88,6 +88,12 @@ _SYNC_VECTOR_QUERY_TYPES = (list, tuple, np.ndarray, pa.Array, pa.ChunkedArray) # mirrors the default the Rust query builder applies to its sub-queries. DEFAULT_HYBRID_LIMIT = 10 +# Minimum number of rows each side of a hybrid query fetches before rank +# fusion. Fusion needs a candidate window larger than the page it returns: +# with only `limit + offset` rows per side, asking for fewer results can +# return a different top result instead of just a shorter list (#4758). +DEFAULT_HYBRID_RANK_WINDOW = 50 + @runtime_checkable class _LanceScanner(Protocol): @@ -3175,9 +3181,12 @@ class LanceHybridQueryBuilder(LanceQueryBuilder): # Apply common configurations # The final offset/limit window is sliced out of the combined, # reranked results, so each sub-query must fetch enough rows to - # cover the skipped prefix as well as the window itself. + # cover the skipped prefix as well as the window itself. Rank fusion + # additionally needs a candidate window larger than the returned page + # (#4758): with only `limit + offset` rows per side, a smaller limit + # can return a different top result instead of just a shorter list. limit = self._limit or DEFAULT_HYBRID_LIMIT - sub_query_limit = limit + (self._offset or 0) + sub_query_limit = max(limit + (self._offset or 0), DEFAULT_HYBRID_RANK_WINDOW) self._vector_query.limit(sub_query_limit) self._fts_query.limit(sub_query_limit) # WAL-PK-FUSION: without the fallback, select `self._columns` as is. @@ -4610,9 +4619,13 @@ class AsyncHybridQuery(AsyncStandardQuery, AsyncVectorQueryBase): # offset() pushes the offset down into both sub-queries, which would make # each of them skip its own first `offset` rows. The window has to be # taken out of the combined, reranked results instead, so fetch the - # skipped prefix here too and slice it off afterwards. - fts_query.limit(limit + offset) - vec_query.limit(limit + offset) + # skipped prefix here too and slice it off afterwards. Rank fusion + # additionally needs a candidate window larger than the returned page + # (#4758), so never fetch fewer than DEFAULT_HYBRID_RANK_WINDOW rows + # per side. + sub_query_limit = max(limit + offset, DEFAULT_HYBRID_RANK_WINDOW) + fts_query.limit(sub_query_limit) + vec_query.limit(sub_query_limit) fts_query.offset(0) vec_query.offset(0) diff --git a/python/python/tests/test_hybrid_query.py b/python/python/tests/test_hybrid_query.py index c432bac59..9fdd2b7bd 100644 --- a/python/python/tests/test_hybrid_query.py +++ b/python/python/tests/test_hybrid_query.py @@ -7,10 +7,12 @@ import lancedb from lancedb.query import LanceHybridQueryBuilder from lancedb.rerankers.rrf import RRFReranker +import numpy as np import pyarrow as pa import pyarrow.compute as pc import pytest import pytest_asyncio +import random from lancedb.index import BTree, FTS, IvfPq from lancedb.table import AsyncTable, Table @@ -58,8 +60,10 @@ async def test_async_hybrid_query(table: AsyncTable): table.query().nearest_to([0.0, 0.4]).nearest_to_text("dog").limit(2).to_arrow() ) assert len(result) == 2 - # ensure we get results that would match well for text and vector - assert result["text"].to_pylist() == ["a", "dog"] + # ensure we get results that would match well for text and vector. + # 'dog' matches both the text query and the vector query, so rank fusion + # over the full candidate window (#4758) puts it first. + assert result["text"].to_pylist() == ["dog", "a"] # ensure there is no rowid by default assert "_rowid" not in result @@ -76,9 +80,11 @@ async def test_async_hybrid_query_with_row_ids(table: AsyncTable): .to_arrow() ) assert len(result) == 2 - # ensure we get results that would match well for text and vector - assert result["text"].to_pylist() == ["a", "dog"] - assert result["_rowid"].to_pylist() == [0, 3] + # ensure we get results that would match well for text and vector. + # 'dog' matches both the text query and the vector query, so rank fusion + # over the full candidate window (#4758) puts it first. + assert result["text"].to_pylist() == ["dog", "a"] + assert result["_rowid"].to_pylist() == [3, 0] @pytest.mark.asyncio @@ -268,10 +274,11 @@ async def test_async_hybrid_query_fts_first_default_limit(table: AsyncTable): @pytest.mark.asyncio async def test_async_hybrid_query_explain_plan_matches_execution(table: AsyncTable): - # Paging rewrites the sub-queries: each one fetches limit + offset rows with - # no offset of its own, and the window is sliced out after fusion. The plans - # have to be built from those rewritten sub-queries, otherwise explain_plan - # and analyze_plan describe a query that is never run. + # Paging rewrites the sub-queries: each one fetches enough rows to cover + # limit + offset, and never fewer than the rank-fusion window (#4758), + # with no offset of its own, and the window is sliced out after fusion. + # The plans have to be built from those rewritten sub-queries, otherwise + # explain_plan and analyze_plan describe a query that is never run. query = ( table.query().nearest_to([0.0, 0.4]).nearest_to_text("dog").offset(2).limit(2) ) @@ -281,12 +288,12 @@ async def test_async_hybrid_query_explain_plan_matches_execution(table: AsyncTab assert [ line.strip() for line in plan.splitlines() if "GlobalLimitExec" in line ] == [ - "GlobalLimitExec: skip=0, fetch=4", - "GlobalLimitExec: skip=0, fetch=4", + "GlobalLimitExec: skip=0, fetch=50", + "GlobalLimitExec: skip=0, fetch=50", ] analyzed = await query.analyze_plan() - assert analyzed.count("skip=0, fetch=4") == 2 + assert analyzed.count("skip=0, fetch=50") == 2 assert "skip=2" not in analyzed @@ -315,6 +322,42 @@ def test_hybrid_query_offset(sync_table: Table): assert offset_result["_rowid"].to_pylist() == full["_rowid"].to_pylist()[2:] +def test_hybrid_query_top_result_stable_across_limits(tmpdir_factory): + # Regression test for #4758: the top result of a hybrid search must not + # depend on `limit`. Each side used to fetch only `limit + offset` rows + # before fusion, so asking for fewer results could return a different + # first result instead of just a shorter list. + rng = np.random.default_rng(0) + random.seed(0) + words = "alpha beta gamma delta epsilon zeta eta theta iota kappa".split() + rows = [ + { + "id": i, + "text": " ".join(random.choice(words) for _ in range(12)), + "vector": rng.random(16).astype("float32"), + } + for i in range(300) + ] + tmp_path = str(tmpdir_factory.mktemp("data")) + table = lancedb.connect(tmp_path).create_table("t", data=rows) + table.create_fts_index("text", with_position=False) + + query_vec = rng.random(16).astype("float32") + + def top_id(limit): + result = ( + table.search(query_type="hybrid") + .vector(query_vec) + .text("alpha beta gamma") + .limit(limit) + .to_list() + ) + assert len(result) == limit + return result[0]["id"] + + assert top_id(1) == top_id(3) == top_id(10) + + @pytest.mark.parametrize( "query_vector", [