Skip to content

[core][vector] Add distance range search for Data Evolution - #11655

Open
shyjsarah wants to merge 6 commits into
apache:masterfrom
shyjsarah:feat/vector-distance-range-search
Open

shyjsarah wants to merge 6 commits into
apache:masterfrom
shyjsarah:feat/vector-distance-range-search

Conversation

@shyjsarah

@shyjsarah shyjsarah commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Add uncapped distance range reads for single-vector columns on Data Evolution append tables. Engines can plan snapshot-scoped tasks and distribute them before searching. Each task streams matching row IDs and scores in bounded chunks and can read projected table rows from the same snapshot.

Usage

The single-vector and batch APIs use the same inclusive bounds.

Java factory Matching distance
VectorDistanceRange.atMost(max) distance <= max
VectorDistanceRange.atLeast(min) distance >= min
VectorDistanceRange.closed(min, max) min <= distance <= max

Bounds are finite. A closed range may have equal bounds. Result order is unspecified. Both range methods reject an explicit withLimit(k) and branch fallback reads.

Single query

import org.apache.paimon.data.InternalRow;
import org.apache.paimon.predicate.VectorDistanceRange;
import org.apache.paimon.reader.RecordReader;
import org.apache.paimon.table.Table;
import org.apache.paimon.table.source.VectorRangeRead;
import org.apache.paimon.types.RowType;

import java.util.Collections;

// Use a Data Evolution table with a three-dimensional embedding column and an INT id.
Table table = catalog.getTable(identifier);
RowType projection = table.rowType().project(Collections.singletonList("id"));
VectorRangeRead read = table.newVectorSearchBuilder()
        .withVectorColumn("embedding")
        .withVector(new float[] {1.0f, 2.0f, 3.0f})
        .newVectorRangeRead(VectorDistanceRange.closed(0.05, 0.20));

for (VectorRangeRead.Task task : read.plan()) {
    try (RecordReader<InternalRow> rows = read.createReader(task, projection)) {
        rows.forEachRemaining(row -> System.out.println(row.getInt(0)));
    }
}

Batch queries

Plan once and run searchBatch for each task. The callback produces the zero-based input query index and aligned row IDs and scores, with up to 4,096 hits per chunk. Use the chunk directly for metadata-only consumers or call createBatchReader to read table columns.

float[][] queries = {{1.0f, 2.0f, 3.0f}, {3.0f, 2.0f, 1.0f}};
VectorRangeRead batch = table.newVectorSearchBuilder()
        .withVectorColumn("embedding")
        .newBatchVectorRangeRead(VectorDistanceRange.atMost(0.20));

for (VectorRangeRead.Task task : batch.plan()) {
    batch.searchBatch(task, queries, (queryIndex, rowIds, scores) -> {
        try (RecordReader<InternalRow> rows =
                batch.createBatchReader(task, rowIds, scores, projection)) {
            rows.forEachRemaining(row -> System.out.println(queryIndex + ": " + row.getInt(0)));
        }
    });
}

Distance means squared L2, 1 - cosine similarity, or the negative dot product according to the index metric. Set search options with withOption or withOptions. The detail mode also evaluates uncovered rows from their original vectors. The fast mode restricts the search to index coverage. Probe options can narrow candidate coverage, and quantized distances can move results across a boundary.

Changes

  • Convert inclusive Java bounds to the Native SDK's half-open float interval and apply the range predicate to both indexed and exact distances. This uses the chunked SDK API already available through [build] Upgrade Java and Python vector index dependencies to 0.6.0 #11358.
  • Resolve the persisted metric from the selected snapshot and use it for exact fallback. Apply live-row and predicate filters at that snapshot and reuse file-group planning across result chunks.
  • Bind Lumina's persisted metric during planning. Prescale cosine queries only when their FP32 squared norm risks overflow or underflow. Native search and centroid routing use the same prescaling rule and let the SDK normalize the query.
  • Keep the snapshot handling local to the Range path and read the existing index format. The Spark SQL integration is tracked in [spark] Add distance range search for Data Evolution #11657.

Tests

The selected suites cover inclusive bounds, time travel through a tag, explicit limit and branch fallback rejection, persisted Lumina metrics, zero-distance cosine matches for identical non-axis-aligned vectors, and extreme-scale cosine queries with and without centroid routing.

  • Clean Maven build passed 178 tests. Two existing Lumina scan tests require the Lumina JNI library and were skipped on this machine. The new Lumina range fallback regression passed using persisted manifest metadata.
  • The documentation production build passed, and both Java examples compile against the rebuilt API.
mvn -B -pl paimon-vector,paimon-lumina -am -DfailIfNoTests=false -DwildcardSuites=none \
  -Dtest=VectorSearchBuilderTest,VectorSearchMetricTest,RangeChunkReaderTest,NativeVectorGlobalIndexTest,CentroidVectorGlobalIndexTest,CentroidTableInteropTest,LuminaVectorRangeReadTest,LuminaVectorGlobalIndexerFactoryTest,LuminaVectorGlobalIndexScanTest \
  clean package

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

Please separate any changes unrelated to the functionality into a distinct PR.

@JingsongLi

Copy link
Copy Markdown
Contributor

Could we support both distance bounds in this PR? A lower bound lets callers exclude near-duplicates while keeping other similar results. Lance exposes this through its lower bound, and Milvus covers the same use case with range_filter. Lance Java API · Milvus Range Search

I’d use a VectorDistanceRange for both newVectorRangeRead and newBatchVectorRangeRead, with explicit factory methods:

  • atMost(max) matches distance <= max and preserves the current inclusive threshold.
  • atLeast(min) matches distance >= min.
  • closed(min, max) matches min <= distance <= max.

The indexed path and the exact fallback should apply the same boundary rules. Tests should cover both endpoints and adjacent floating-point values.

I’d keep range reads uncapped in this PR. If a caller sets withLimit(k) before creating a range read, the builder should reject that combination explicitly. A future bounded search can pair the same range with the existing Top-K API, filter by distance first, and apply k per query across all tasks.

@shyjsarah

Copy link
Copy Markdown
Contributor Author

Thanks @JingsongLi. I updated the PR in 0ca4fb4.

  • Both the single-vector and batch Java APIs now accept VectorDistanceRange.atMost(max), atLeast(min), and closed(min, max). Both endpoints are inclusive for indexed and exact reads. The builder rejects an explicit withLimit(k), including zero. The PR description now has single-query and batch Java examples. Distance bounds and limit feedback
  • The snapshot changes are scoped to range reads, and I dropped the index writer metadata change. Range reads can resolve the metric from existing index metadata or the native index header. The existing Top-K tag-snapshot case can follow separately once reproduced through the Top-K API. PR scope feedback

The clean Maven build passed all 162 selected Core and Native tests, including boundary and adjacent floating-point cases. Both Java usage examples compiled against the rebuilt API.

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