From 0dd9dfdfc745f002afd24facc918744a8fe1ccfc Mon Sep 17 00:00:00 2001 From: "lancedb-gatefixer[bot]" <313497061+lancedb-gatefixer[bot]@users.noreply.github.com> Date: Thu, 27 Aug 2026 17:10:51 +0800 Subject: [PATCH] test(python): cover arithmetic with distance projections (#3862) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary - add Python regression coverage for integer and double arithmetic against the generated _distance column - merge the current main base containing Lance v11.0.0-beta.3 from #3896 - verify both expressions retain the generated scoring field Float32 type and compute the expected values ## Root cause Lance parsed dynamic projection expressions before vector search added its generated Float32 _distance field. Without a typed provisional field, expression discovery rejected mixed numeric arithmetic. Lance upstream fixed discovery and final-schema replanning in lance-format/lance#8163, and the current base consumes that fix through Lance v11.0.0-beta.3. ## Validation - uv run --extra tests pytest python/tests/test_query.py::test_select_arithmetic_with_distance -vv --maxfail=2 — 2 passed - python/.venv/bin/ruff format --check python/python/tests/test_query.py - python/.venv/bin/ruff check . Fixes #2618 --------- Co-authored-by: Gatefixer <313497061+lancedb-gatefixer[bot]@users.noreply.github.com> Co-authored-by: Xuanwo --- python/python/tests/test_query.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/python/python/tests/test_query.py b/python/python/tests/test_query.py index 4758f0e2d..ff62b2b51 100644 --- a/python/python/tests/test_query.py +++ b/python/python/tests/test_query.py @@ -675,6 +675,21 @@ def test_distance_range(table: lancedb.table.Table): assert res["_distance"].to_pylist() == [min_dist, max_dist] +@pytest.mark.parametrize("expression", ["1 - _distance", "1.0 - _distance"]) +def test_select_arithmetic_with_distance(table, expression): + result = ( + table.search([10, 10]) + .select({"similarity": expression, "_distance": "_distance"}) + .distance_type("cosine") + .to_arrow() + ) + + assert result.schema.field("similarity").type == pa.float32() + assert result["similarity"].to_pylist() == pytest.approx( + [1 - distance for distance in result["_distance"].to_pylist()] + ) + + @pytest.mark.asyncio async def test_distance_range_async(table_async: AsyncTable): q = [0, 0]