Skip to content

fix(fts): preserve corpus statistics for restricted matches - #9243

Merged
Xuanwo merged 10 commits into
lance-format:mainfrom
ruslan-shaydullin:codex/lance-9058-restricted-match-corpus
Oct 27, 2026
Merged

Xuanwo merged 10 commits into
lance-format:mainfrom
ruslan-shaydullin:codex/lance-9058-restricted-match-corpus

Conversation

@ruslan-shaydullin

@ruslan-shaydullin ruslan-shaydullin commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Related to #9058.

For the two reported single-column Match cases, index optimization changes the winner from alpha to beta after incorporating already appended rows, although the searchable data and query are unchanged.

This partial fix scores eligible restricted matches against committed posting statistics plus the complete unindexed corpus. Flat candidate filtering runs after corpus collection; indexed masks still constrain indexed top-k. A private owner gives each execution a fresh shared scorer. The restricted path handles an effective top-k limit of zero without constructing a zero-sized sort.

The correction covers exact, row-level scalar-string Match queries with a prefilter or explicit fragment selection on the supported append-only corpus. Indexed scoring uses V2/V3 postings. Deletion/stale-text-overlay scoring, legacy postings, fuzzy/phrase/Boolean queries, BM25F/combined_fields, and the unfiltered Hybrid approximation remain outside this correction. #9058 remains open for the broader scoring work.

Integration

Head 17cbb29a786a606cc1c66dd57fedc898edf617ee contains merge commit 3bb7aecd99f71df569e64fd4829c2b05a6adfa6c, which integrates upstream main at c85ad6414715792dfc7797a791a3560a68c3fa44. The resolution preserves #9872's per-segment allowed-row filtering for stable row IDs and #10251's lance-datafusion-exec dependency boundary, while retaining the restricted Match planner and emission-time candidate filtering.

Regression commit 17cbb29a786a606cc1c66dd57fedc898edf617ee adds one focused case with two committed FTS segments, a residual unindexed tail, and a restricted exact root Match under stable row IDs. It compares both returned rows and exact scores with the independent raw-text BM25 oracle.

Validation

Local validation covered the new multi-segment and residual-tail exact-score regression, the full 9058 suite, Legacy/Stable plan variants, the stable-row-id segmented-index paths, and workspace formatting and clippy. GitHub Actions on the current head are the authoritative CI record; results from older heads are not inferred as current.

The global corpus requires reading and tokenizing all unindexed text, including rows outside the candidate set. Corpus construction reuses committed posting statistics without retokenizing indexed text. A scalar prefilter with an overlay-stale index can still contain a consumable OneShotExec; fresh scorer state does not make that existing input replayable. Sequential and concurrent reuse are verified on the append-only fixtures. No performance claim is made.

Thanks to @sbrunk and the automated reviewer for the report and reproductions during #7905; its emission-filter/shared-scorer work informed this follow-up.

@github-actions github-actions Bot added the bug Something isn't working label Sep 15, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Sep 15, 2026
@ruslan-shaydullin

Copy link
Copy Markdown
Contributor Author

Thanks @wjones127 for enabling the previous CI run. It exposed outdated expected plans in test_plans; 5328449 updates both storage-format expectations and the comment without changing production code. All four plan variants and 65 FTS regressions now pass locally, as do formatting and workspace clippy with warnings denied.

Could you approve the five code workflows on this updated head? They currently report action_required, including the Rust run. The PR description now reflects these results. Thanks!

@lance-gatekeeper lance-gatekeeper Bot removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Sep 21, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. and removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Sep 21, 2026
@ruslan-shaydullin

Copy link
Copy Markdown
Contributor Author

Hi @sbrunk, would you be able to check the two single-column Match reproductions from #9058 against this PR (5328449)? The covered cases are exact, row-level scalar-string matches on an append-only corpus with either prefilter(true) / id < 2 or with_fragments([0]), using V2/V3 postings. With the same data and query, the selected row and BM25 score should remain stable before and after optimize_indices().

The updated head has passed Rust, Python and Java CI. This remains a partial fix: combined_fields/BM25F, deletion or stale-text overlays, legacy postings, fuzzy/phrase/Boolean queries, and unfiltered Hybrid scoring are outside its scope. A result for either original single-column reproduction would be useful; #9058 stays open for the broader work. Thanks!

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. and removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Sep 22, 2026
@sbrunk

sbrunk commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

@ruslan-shaydullin I couldn't find the orignal repro code anymore but based on the repro example in the ticket I checked againt and it looks good!

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. and removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Sep 26, 2026
Merge current main while preserving restricted Match corpus scoring.
Adapt the candidate-prefilter constructor and assert exact residual
fragment coverage introduced by upstream scalar-index pruning.

Signed-off-by: Ruslan Shaydullin <[email protected]>
@ruslan-shaydullin

Copy link
Copy Markdown
Contributor Author

Synced with main after #9444 and #9640; only the import lists conflicted, the patch itself is unchanged. The plan, #9058, FTS executor, BM25F and scalar-pruning test groups, fmt and clippy all pass locally on the new head. @wjones127 could you approve the workflow runs again when you get a chance?

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. and removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 7, 2026
Merge upstream main after the external indexed input for hybrid
queries (lance-format#9847) and the scalar index candidate fix (lance-format#9789). Only the
fts.rs test import list conflicted; it now carries both sides, and the
restricted Match patch itself is unchanged.

Signed-off-by: Ruslan Shaydullin <[email protected]>
@lance-gatekeeper lance-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 12, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 12, 2026
@lance-gatekeeper lance-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 27, 2026

@lance-gatekeeper lance-gatekeeper Bot left a comment

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.

⚠️ Gate recommendation: approve with a non-blocking risk.

The sync and new multi-segment regression preserve the correction for exact, row-level single-column Match on append-only data: prefilters and fragment selection do not alter corpus statistics. Broader scoring cases remain tracked by #9058.

Selective prefilters or fragment selections still read and tokenize the full unindexed text tail, so latency and peak memory scale with that tail. Keeping the FTS index current mitigates the cost.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 27, 2026

@Xuanwo Xuanwo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed the latest head and preserved restricted Match scope and the accepted full unindexed-tail cost. Current-head CI and Gatekeeper checks passed; broader scoring remains tracked by #9058.

@Xuanwo
Xuanwo merged commit aa115db into lance-format:main Oct 27, 2026
43 of 45 checks passed
LuQQiu added a commit that referenced this pull request Nov 17, 2026
…11139)

Related to #9058. Follow-up to #9243.

## Problem

#9243 added a plan (`MatchCorpus`) that scores an exact `Match` or
row-level `Phrase` query against one set of corpus statistics: the
committed index statistics plus every unindexed row. Both the indexed
arm and the unindexed arm use it, so scores no longer depend on the
query filter or on how much of the table is indexed.

That plan was limited to append-only tables. The planner skipped it as
soon as any fragment in the dataset had a deletion file. One deleted or
updated row anywhere in the table sent every such query back to the
older plan and the behaviour described in #9058. Deletion files stay
until compaction rewrites the fragment, so a table that receives updates
rarely qualified.

## Change

Remove that one condition from `plan_corpus_query`. A deletion file no
longer decides which plan runs. Every other eligibility rule is
unchanged (primary FTS, exact single-column `Match` or row-level
`Phrase`, default `wand_factor`, no `fast_search`, no data overlay,
V2/V3 postings).

No execution code changes. The two arms already handle deletions:

| | Statistics it contributes | Rows it can return |
| --- | --- | --- |
| Indexed segments | Values stored at index time. Rows deleted after
indexing are still counted. | Deleted rows are masked out. |
| Unindexed fragments | Counted while scanning. The scan skips deleted
rows, so only live rows are counted. | Live rows only. |

## Scoring on a table with deletions

Example: 1000 rows are indexed, 100 of them are later deleted, and 50
rows are appended without indexing. The shared scorer uses a document
count of 1050 (1000 from the index, 50 from the scan); the exact live
count is 950. Document frequency and average length are built the same
way.

This is the approximation the indexed arm of the older plan already
uses, so no score gets less accurate with this PR. What changes is that
both arms now use the same numbers on these tables too. An update counts
twice until the index is rebuilt: the old version in the index
statistics and the new version in the scan.

Making the indexed statistics exclude deleted rows needs a pass over the
posting list of every query term. That stays with #9058.

## Tests

In `rust/lance/src/dataset/tests/dataset_index.rs`, next to the existing
#9058 cases and reusing their helpers:

- `test_fts_9058_deletions_corpus_scores`: one row is deleted in an
indexed fragment and one in an unindexed fragment. The plan contains
`MatchCorpus`, the deleted rows are not returned, and rows and scores
match the raw-text BM25 oracle computed over "indexed rows as of index
time plus live unindexed rows". Cases: no filter, prefilter, filter;
with and without stable row ids; with and without a limit. Checked
before and after an append-mode `optimize_indices`: the existing
segments keep their statistics and the new segment is built from live
rows, so the expected scores are the same at both stages.
- `test_fts_9058_phrase_deletions_corpus_scores`: the same check for a
row-level `Phrase` query. One indexed and one unindexed row that match
the phrase are deleted; the plan contains `MatchCorpus` and
`PhraseQuery`, neither deleted row is returned, and scores match the
raw-text phrase oracle over the same corpus definition. Cases: no
filter, prefilter, a filter that selects only unindexed rows, an
explicit fragment selection; with and without stable row ids; before and
after an append-mode `optimize_indices`.
- `test_fts_9058_fully_deleted_fragment`: a fully deleted indexed
fragment and a fully deleted unindexed fragment return no rows and do
not fail, before and after index optimization.
- `test_fts_9058_deleted_candidates_stay_excluded`: existing test,
assertions unchanged, comment updated.

All of the above run in CI.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants