Skip to content

fix(lance): expose nearest search distances and keep generated-column filters post-scan - #97

Open
irenjj wants to merge 4 commits into
daft-engine:mainfrom
irenjj:fix/lance-nearest-distance
Open

irenjj wants to merge 4 commits into
daft-engine:mainfrom
irenjj:fix/lance-nearest-distance

Conversation

@irenjj

@irenjj irenjj commented Nov 23, 2026 •

Copy link
Copy Markdown

Problem

read_lance(..., default_scan_options={"nearest": {...}}) runs a nearest-neighbour search, but the resulting DataFrame cannot observe the result:

  • LanceScanOperator.__init__ builds its schema from ds.schema only, and _distance is produced at scan time, so any reference to _distance fails during planning with FieldNotFound.
  • The column only reached the output through Lance's scoring auto-projection, which is already marked deprecated. Once that behaviour changes, the column silently disappears while the operator schema still advertises it.
  • With include_fragment_id=True plus nearest, fragment_id is a synthetic column, but Daft pushes predicates on it into the Lance filter, where the column does not exist, and the read fails.

Changes

  • Declare _distance (Float32) in the operator schema when nearest is configured.
  • Set disable_scoring_autoprojection=True and project _distance explicitly instead of relying on Lance's deprecated default. When Daft requests an empty projection (e.g. literal-only select) without fragment_id, request _distance so matched row counts are preserved.
  • Derive fragment_id from the physical row address (_rowaddr >> 32) for index-driven searches, dropping _rowaddr unless the caller asked for it.
  • Keep predicates on generated columns (_distance, synthetic fragment_id) in the post-scan remainder so Daft evaluates them after the nearest selection.
  • Disable count pushdown while nearest is active.

Usage

df = read_lance(
    "data.lance",
    default_scan_options={"nearest": {
        "column": "embedding", "q": query, "k": 10, "metric": "l2",
        "distance_range": (None, 0.5),
    }},
)
result = df.select("id", "_distance").sort("_distance")

README documents that distance_range is applied before the Top-K cutoff (inclusive lower, exclusive upper bound), while a Daft filter on _distance runs after the nearest selection, and that L2 reports squared Euclidean distance.

Testing

  • pytest tests/io/lance/test_lance_vector_search.py: 29 passed locally on both pylance==12.0.0 (locked) and pylance==14.0.2.
  • Added coverage for distance projection and dtype, literal-only / empty projections (including include_fragment_id, with_row_address, and empty matches), distance with fragment_id, post-scan fragment_id filtering under strict filter pushdown, distance_range before Top-K including the exclusive upper bound, Hamming distance dtype, and merging query distances back via merge_columns_df.

Notes

  • Index-based searches return approximate distances from the index; values are not directly comparable with the exact distances returned when use_index=False.
  • Related: Add distributed vector search with global top-k merging #87 proposes a separate distributed vector_search API. This change only makes the existing default_scan_options["nearest"] path observable; it does not add query-parallel execution.

🤖 Generated with Claude Code

Comment thread daft_lance/lance_scan.py
if nearest is not None:
scanner_options["disable_scoring_autoprojection"] = True
# With no Daft projection, expose the full nearest scan schema.
if columns is None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Handle empty scan projections in nearest searches

If the query only selects a literal, such as df.select(daft.lit(1).alias("one")), Daft asks Lance for columns=[]. With scoring auto-projection disabled, Lance returns a batch with rows but no columns, which Daft cannot convert. On a search with two matches, the base commit returns {"one": [1, 1]}, while this PR raises on both pylance 12.0.0 and 14.0.2.

Could we explicitly request _distance when the final Lance projection is empty, and add a regression test that verifies one output row per match?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks! Fixed in e2a1f1e to request _distance on empty projections, and added a regression test for literal-only selects.

Comment thread .github/workflows/pr-test-suite.yml Outdated
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v5
- name: Run vector search tests on newer Lance
run: uv run --locked --python 3.12 --with 'pylance==14.0.2' pytest tests/io/lance/test_lance_vector_search.py -v

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to add the test for pylance 14?

@irenjj
irenjj force-pushed the fix/lance-nearest-distance branch from 9b8c95e to e2a1f1e Compare November 27, 2026 02:00

@irenjj irenjj left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL, cc @FANNG1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants