Stephen0421 commented on code in PR #10569:
URL: https://github.com/apache/paimon/pull/10569#discussion_r4307184960


##########
paimon-spark/paimon-spark-common/src/main/java/org/apache/paimon/spark/read/SparkDataEvolutionBatchVectorRead.java:
##########
@@ -0,0 +1,297 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.spark.read;
+
+import org.apache.paimon.globalindex.GlobalIndexReadThreadPool;
+import org.apache.paimon.globalindex.GlobalIndexResult;
+import org.apache.paimon.globalindex.GlobalIndexResultSerializer;
+import org.apache.paimon.globalindex.GlobalIndexer;
+import org.apache.paimon.globalindex.GlobalIndexerFactoryUtils;
+import org.apache.paimon.globalindex.ScoredGlobalIndexResult;
+import org.apache.paimon.index.IndexPathFactory;
+import org.apache.paimon.partition.PartitionPredicate;
+import org.apache.paimon.predicate.Predicate;
+import org.apache.paimon.table.FileStoreTable;
+import org.apache.paimon.table.source.DataEvolutionBatchVectorRead;
+import org.apache.paimon.table.source.IndexVectorSearchSplit;
+import org.apache.paimon.table.source.RawVectorSearchSplit;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.utils.InstantiationUtil;
+import org.apache.paimon.utils.Range;
+import org.apache.paimon.utils.RoaringNavigableMap64;
+import org.apache.paimon.utils.SerializableFunction;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+
+/**
+ * Spark-aware {@link DataEvolutionBatchVectorRead}. Each Spark task evaluates 
every query vector
+ * against its split group, and the driver merges one result per query in 
input order.
+ */
+public class SparkDataEvolutionBatchVectorRead extends 
DataEvolutionBatchVectorRead {
+
+    private static final long serialVersionUID = 1L;
+
+    public SparkDataEvolutionBatchVectorRead(
+            FileStoreTable table,
+            @Nullable PartitionPredicate partitionFilter,
+            @Nullable Predicate filter,
+            int limit,
+            DataField vectorColumn,
+            float[][] vectors,
+            @Nullable Map<String, String> options) {
+        super(table, partitionFilter, filter, limit, vectorColumn, vectors, 
options);
+    }
+
+    @Override
+    protected ScoredGlobalIndexResult[] readIndexedBatch(
+            List<IndexVectorSearchSplit> splits, GlobalIndexer globalIndexer) {
+        int parallelism = sparkParallelism();
+        if (splits.size() < parallelism * 2) {
+            return super.readIndexedBatch(splits, globalIndexer);
+        }
+
+        List<RoaringNavigableMap64> preFilters = preFilters(splits);
+        String indexType = vectorIndexType(splits);
+        int searchLimit = indexedSearchLimit(indexType);
+        int queryCount = vectors.length;
+        List<SparkVectorSearchSupport.SerializedSplit> serializedSplits =
+                new ArrayList<>(splits.size());
+        for (int i = 0; i < splits.size(); i++) {
+            try {
+                IndexVectorSearchSplit split = splits.get(i);
+                RoaringNavigableMap64 preFilter = preFilters.isEmpty() ? null 
: preFilters.get(i);
+                serializedSplits.add(
+                        new SparkVectorSearchSupport.SerializedSplit(
+                                InstantiationUtil.serializeObject(split),
+                                preFilter == null
+                                        ? null
+                                        : 
InstantiationUtil.serializeObject(preFilter)));
+            } catch (IOException e) {
+                throw new RuntimeException("Failed to serialize 
VectorSearchSplit", e);
+            }
+        }
+        List<List<SparkVectorSearchSupport.SerializedSplit>> splitGroups =
+                SparkVectorSearchSupport.splitGroups(serializedSplits, 
parallelism);
+        SerializableFunction<List<SparkVectorSearchSupport.SerializedSplit>, 
byte[][]> task =
+                group -> evaluateIndexGroup(group, indexType, searchLimit, 
queryCount, parallelism);
+
+        List<byte[][]> remoteResults = mapInSpark(splitGroups, task, 
splitGroups.size());
+        ScoredGlobalIndexResult[] merged =
+                mergeRemoteResults(remoteResults, queryCount, searchLimit);
+        // Avoid loading the indexer when refine is off; 
maybeRerankIndexedBatchResults repeats this
+        // check.
+        if (configuredRefineFactor(indexType) == 0) {
+            return merged;
+        }
+        GlobalIndexer rerankGlobalIndexer =
+                globalIndexer == null ? createGlobalIndexer(splits) : 
globalIndexer;
+        return maybeRerankIndexedBatchResults(merged, indexType, 
rerankGlobalIndexer);

Review Comment:
   Fixed. Each executor returns the metric learned after its shard reader is 
opened. The driver normalizes those values, rejects a batch when tasks 
disagree, and uses the resolved metric for both refine rerank and the raw merge 
of uncovered rows. The driver indexer still does not open the shard readers, so 
it no longer reranks with the table configuration.
   
   
`SparkDataEvolutionBatchVectorReadTest#testDistributedBatchReranksWithPersistedMetric`
 sets the table metric to cosine and the persisted metric to l2, with refine 
factor 2. For query `[1, 0]` and candidates `[2, 0]` and `[1, 0.1]`, top-1 is 
the L2 neighbor, and the following raw merge also scores with l2.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to