Registry Mirror
Skip to content

Conversation

@xuanyu-z

@xuanyu-z xuanyu-z commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Bounded consolidation (OptimizeOptions::consolidate(max_rows)) only merges the newest segments of a vector index. When rows are rewritten, their old copies stay as deleted rows in older segments, which keep their entries. Nothing ever rewrites those segments, so on a table whose rows churn, both the segment count and the share of dead entries keep growing.

OptimizeOptions::consolidate_selected(index_name, Vec<Uuid>) lets the caller name the segments of one index to consolidate, wherever they sit. It reuses bounded consolidation's planning, execution and commit, with these differences:

  • Sources: the named segments. Segments the index no longer has are skipped with a warning; a delete that empties a segment removes it.
  • Model: the selected rows are always re-encoded, which drops deleted rows.
    • If the selected segments share one model, they keep it, so nothing about search changes.
    • If their models differ, a new model is trained on the selected rows alone. Encoding data under a model trained on other data loses recall, which is why the selection never borrows another segment's model.
    • Training reuses the split rebuild's path: the planner trains the model on the selected fragments and writes it as a segment outside the manifest, and the tasks encode against it.
    • The partition count is sized from the selected rows, as a new build would size it. IVF_RQ keeps the rotation of the newest selected segment and retrains only the centroids.
  • Output size: one segment by default. With max_rows_per_segment, the output is packed from whole fragments into segments of at most that many live rows, so a single large segment can be split. Deleted rows don't count, since the output doesn't hold them. max_rows_per_task still bounds the physical rows a task reads. Fragments are never split, so a fragment larger than either limit gets a task and an output of its own instead of failing the request. Bounded consolidation rejects a fragment larger than its task limit.
  • No row budget: the caller chose the work, so the selection is read whole. Setting max_rows_to_consolidate with a selection is rejected rather than quietly shortening it.
flowchart TD
    A["consolidate_selected(index, segments)"] --> B{Selected segments<br/>share one model?}
    B -- yes --> C[Re-encode with that model<br/>lossless, drops deleted rows]
    B -- no --> D["Train a model on the selected rows<br/>(written outside the manifest)"]
    D --> E[Encode the selected rows with it]
    C --> F{max_rows_per_segment set?}
    E --> F
    F -- no --> G[One output segment]
    F -- yes --> H[Pack whole fragments into outputs<br/>of at most that many rows]
    G --> I["Commit: outputs cover every surviving<br/>source fragment exactly once"]
    H --> I
Loading

Why never borrow another segment's model. In a benchmark with 1.5M rows, IVF_PQ and strongly drifted data (recall@10 at nprobes 10), keeping each segment's own model gave 0.50. Retraining on all rows gave 0.46–0.48. Re-encoding the older rows with the newest model gave 0.36, and only 0.20 for queries on the older data. With no drift, all three gave 0.48. So a selection uses either its own shared model or one trained on its own rows.

Several outputs replace their sources together, so the commit check now works per index: the outputs must cover every surviving source fragment exactly once between them, and none may overlap.

A selection must be non-empty, have no duplicates and name exactly one index (the constructor sets it). It can't be combined with retrain, partition rebalancing or num_indices_to_merge. max_rows_per_task still bounds the re-encoding tasks. max_rows_per_segment is accepted only with a selection; bounded consolidation still rejects it.

Other changes:

  • uuid moves from a dev-dependency to a dependency of lance-index.
  • OptimizeOptions is #[non_exhaustive], so the new field is not a breaking change.

Tests

  • selected_segments_with_different_models_are_retrained (stable and non-stable row IDs): selects the two older of three separately trained segments after deletes. It checks that:
    • the newest segment is untouched;
    • one model is trained for the selection, outside the manifest;
    • the output's model differs from every source's, and its partition count follows the selected rows;
    • the output holds only live rows, and queries match brute force.
  • a_shared_model_selection_keeps_its_model: a segment and an append encoded under its model are consolidated under that same model.
  • a_retrained_rq_selection_keeps_the_shared_rotation: IVF_RQ segments that share one rotation but train their own centroids are consolidated. The output has new centroids under that same rotation, and queries match brute force.
  • a_selection_packs_its_output_and_splits_a_segment: two fragments are consolidated into one segment, which is then split back into two outputs of one fragment each under its own model.
  • deleted_rows_do_not_count_toward_the_segment_limit: two half-deleted fragments fit one output at a limit equal to one fragment's physical rows.
  • a_fragment_larger_than_the_limits_is_an_output_of_its_own: with both limits at half a fragment, each selected fragment becomes its own output, and queries match brute force.
  • a_selected_segment_already_removed_is_skipped: when a selected segment has been removed, the rest is still rewritten. A selection with nothing left plans nothing.
  • a_selection_commits_over_concurrent_deletes_and_appends: a delete in a selected source and a new appended segment land between plan and commit. The commit still succeeds.
  • incomplete_or_duplicate_results_cannot_replace_sources: now also covers overlapping outputs.
  • invalid_selections_are_rejected: covers each of these:
    • no index name, or two index names;
    • a duplicate segment, or an empty selection;
    • a row budget, or a merge count;
    • a zero segment limit;
    • a segment limit on bounded consolidation.
  • test_fragment_limited_sample_spans_every_fragment (vector columns with and without nulls): training on a listed subset of fragments samples each in proportion to its rows, whatever order the fragments are listed in.

cargo nextest run -p lance --lib 'index::optimize' and the sampling test pass, and clippy (-D warnings) and fmt are clean. I broke each new branch in turn, and every change fails a test:

  • skipping retraining;
  • dropping the IVF_RQ rotation;
  • packing outputs by physical rows;
  • retraining a shared model;
  • ignoring the segment limit;
  • allowing the limit on bounded consolidation;
  • allowing overlapping or incomplete outputs;
  • sampling the first rows of the listed fragments.

Limitations

  • Too few rows to retrain: a selection whose models differ is retrained on its surviving rows. If too few survive to train the quantizer, it fails as a new build on that data would. Selecting each segment on its own re-encodes it under its own model instead.
  • Splitting adds segments: each output carries its own model, and queries rank each segment's centroids, so splitting costs some search CPU and model memory. One output stays the default.
  • Replacement metadata: every output lists every source, so N sources split into M outputs record N×M UUIDs. That is small for the expected selections of a few segments.

Not covered by a test: clearing the partition count when a segment's parameters are derived from its index rather than its stored details (the test index stores details that leave the count unset already).

@github-actions github-actions Bot added A-index Vector index, linalg, tokenizer enhancement New feature or request A-python Python bindings A-java Java bindings + JNI A-deps Dependency updates labels Nov 3, 2026
@xuanyu-z
xuanyu-z marked this pull request as ready for review November 4, 2026 15:04

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

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-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 4, 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.

consolidate_selected makes the caller responsible for total rewrite work: the full selection is processed without an aggregate row cap.

The documented recall risk remains when older distributions are encoded with the newest model. Prefer sources sharing that model, or check representative ANN recall before consolidating across models.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Nov 4, 2026
@xuanyu-z xuanyu-z changed the title feat: consolidate an explicit set of vector index segments feat: consolidate selected vector index segments Nov 4, 2026
@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 Nov 4, 2026
@LuQQiu

LuQQiu commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

@claude review

@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 Nov 4, 2026

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

@LuQQiu LuQQiu 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.

nice change

@lance-gatekeeper lance-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 6, 2026
`OptimizeOptions::consolidate_segments` names the segments of one vector
index to consolidate, instead of the newest ones that fit the row budget.
Any segments may be chosen, so older segments that have lost most of their
rows to deletes can be rewritten. Each selected segment is re-encoded from
its current fragments with the model of the index's newest segment, which
drops deleted rows. Selected segments are packed whole, in index order, into
outputs of at most `max_rows_per_segment` live rows.

A selection must name one index and carry a row budget; reading more than
the budget is an error rather than a shorter selection. Selected segments
the index no longer has are skipped with a warning. Commit validation is the
same per-source check bounded consolidation already uses.
Drop the separate planner and multi-output packing: a selection is the
same consolidation with explicit sources, re-encoded into one segment.
Also note the recall trade-off of re-encoding sources under another model.
… segment size

A selection whose segments share one model keeps it. Otherwise a model is
trained on the selected rows, since encoding data under a model trained on
other data loses recall. max_rows_per_segment now packs a selection's output
into several segments, so a single large segment can be split.
A segment's output holds only live rows, so packing by physical rows left
outputs far below the limit when their fragments carry deletions, and a
second pass packed them the same way again. Tasks still bound physical
rows. The limits are now checked before a model is trained, so a request
that can't be satisfied trains and writes nothing.
@xuanyu-z
xuanyu-z force-pushed the feat/consolidate-selected-vector-segments branch from 9e05200 to bd72d08 Compare November 9, 2026 18:46
A selection rejected any fragment larger than its task or segment limit, so
a caller whose segment size was below a fragment's rows failed on every
attempt. Such a fragment now gets a task and an output of its own; bounded
consolidation still rejects a fragment larger than its task limit. Also
names consolidate::is_consolidation and train_model_segment's segment.
@Xuanwo
Xuanwo merged commit f113852 into lance-format:main Nov 10, 2026
41 checks passed
Xuanwo added a commit to kamronis/lance that referenced this pull request Nov 14, 2026
Brings the result-cache branch onto main e602c45, preserving the merged
cross-column refinement (lance-format#9127), query-listing generation token (lance-format#10619),
segment-contract rejection (lance-format#10761) and manifest/index replacement
semantics (lance-format#10698).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-deps Dependency updates A-index Vector index, linalg, tokenizer A-java Java bindings + JNI A-python Python bindings enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants