Skip to content

[spark] Add distance range search for Data Evolution - #11657

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

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

Conversation

@shyjsarah

@shyjsarah shyjsarah commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Add Spark SQL vector_range_search for single-vector columns in Data Evolution append tables. Spark plans snapshot-scoped reference tasks on the driver. Executors evaluate the range and stream matching rows and scores in bounded chunks.

This draft depends on #11655. See the Spark-only diff.

Usage

vector_range_search(table, column, query_vector, distance_bounds [, search_options])

The fourth argument is a constant map containing numeric min_distance, max_distance, or both. Both bounds are inclusive. The optional fifth argument is a constant map of string-valued execution options, such as vector-index.search-mode and ivf.nprobe.

Run the examples with a Paimon catalog selected. The sample table uses two-dimensional vectors and squared L2 distance.

CREATE TABLE range_items (id INT, embedding ARRAY<FLOAT>)
TBLPROPERTIES (
    'bucket' = '-1',
    'row-tracking.enabled' = 'true',
    'data-evolution.enabled' = 'true'
);

INSERT INTO range_items VALUES
    (1, array(0.0F, 0.0F)),
    (2, array(1.0F, 0.0F)),
    (3, array(2.0F, 0.0F));

Single query

SELECT id, __paimon_search_score
FROM vector_range_search(
    'range_items', 'embedding', array(0.0F, 0.0F),
    map('max_distance', 1.0D)
)
ORDER BY id;

The query returns IDs 1 and 2. The distance for ID 2 equals the upper bound. Use map('min_distance', 1.0D) to apply only the lower bound.

Batch queries

SELECT q.query_id, r.id, r.__paimon_search_score
FROM VALUES
    (10, array(0.0F, 0.0F)),
    (20, array(1.0F, 0.0F))
AS q(query_id, query_vector),
LATERAL (
    SELECT id, __paimon_search_score
    FROM vector_range_search(
        'range_items', 'embedding', q.query_vector,
        map('min_distance', 0.25D, 'max_distance', 1.0D),
        map('vector-index.search-mode', 'detail')
    )
) r
ORDER BY q.query_id, r.id;

The batch returns (query_id, id) pairs (10, 2), (20, 1), and (20, 3). The lower bound excludes each query's exact match. With an index, the index metric determines the distance units. Quantization and probe options can affect matches. Range results are uncapped and have no implicit order.

Changes

  • Resolve both SQL forms to the same Core VectorDistanceRange contract and validate bounds separately from execution options.
  • Distribute standalone queries across reference tasks and process lateral queries in batches per input partition. Reuse predicate and projection handling, including metadata-only and count queries.
  • Wake the bounded result stream when a batch finishes, fails, or its Spark task closes.

Tests

  • Passed 47 tests on Spark 3.5 and 47 tests on Spark 4.1, with no failures or skips. The suites cover both SQL signatures, inclusive endpoints, invalid maps, indexed and exact reads, projection and predicate pushdown, and early termination with LIMIT.
  • Ran the Usage SQL on Spark 3.5 and verified the single and batch results shown above. The documentation production build passed.
mvn -B -Pspark3 -pl paimon-spark/paimon-spark-ut -am \
  -DfailIfNoTests=false -Dtest=none \
  -DwildcardSuites=org.apache.paimon.spark.sql.VectorRangeSearchTest,org.apache.paimon.spark.sql.VectorSearchOptionsTest,org.apache.paimon.spark.sql.TableValuedFunctionsTest,org.apache.paimon.spark.sql.PrimaryKeyVectorSearchTest \
  clean package

The Spark 4 run used the same suites with -Pfast-build -Pspark4 on JDK 17.

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.

1 participant