Stephen0421 commented on code in PR #10569: URL: https://github.com/apache/paimon/pull/10569#discussion_r4307184208
########## 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); + } + + @Override + protected List<GlobalIndexResult> combineWithRaw( + ScoredGlobalIndexResult[] indexedResults, + List<RawVectorSearchSplit> rawSplits, + @Nullable GlobalIndexer globalIndexer, + @Nullable RoaringNavigableMap64 rawPreFilter) { + List<Range> rawRowRanges = rawRowRanges(rawSplits); + int parallelism = sparkParallelism(); + if (rawRowRanges.isEmpty() + || SparkVectorSearchSupport.rawRowCount(rawRowRanges) < parallelism * 2L) { + return super.combineWithRaw(indexedResults, rawSplits, globalIndexer, rawPreFilter); + } + + String metric = rawSearchMetric(rawSearchIndexer(rawSplits, globalIndexer)); + int queryCount = vectors.length; + List<List<Range>> rangeGroups = + SparkVectorSearchSupport.rangeGroups(rawRowRanges, parallelism); + List<SparkVectorSearchSupport.SerializedSplit> serializedSplits = + new ArrayList<>(rangeGroups.size()); + for (List<Range> rangeGroup : rangeGroups) { + try { + serializedSplits.add( + new SparkVectorSearchSupport.SerializedSplit( + InstantiationUtil.serializeObject(rangeGroup), + rawPreFilter == null + ? null + : InstantiationUtil.serializeObject(rawPreFilter))); + } catch (IOException e) { + throw new RuntimeException("Failed to serialize raw vector row ranges", e); + } + } + List<List<SparkVectorSearchSupport.SerializedSplit>> splitGroups = + SparkVectorSearchSupport.splitGroups(serializedSplits, parallelism); + SerializableFunction<List<SparkVectorSearchSupport.SerializedSplit>, byte[][]> task = + group -> evaluateRawGroup(group, metric, queryCount); + + List<byte[][]> remoteResults = mapInSpark(splitGroups, task, splitGroups.size()); + ScoredGlobalIndexResult[] rawResults = mergeRemoteResults(remoteResults, queryCount, limit); + List<GlobalIndexResult> results = new ArrayList<>(queryCount); + for (int i = 0; i < queryCount; i++) { + results.add(indexedResults[i].or(rawResults[i]).topK(limit)); + } + return results; + } + + private byte[][] evaluateIndexGroup( + List<SparkVectorSearchSupport.SerializedSplit> group, + String indexType, + int searchLimit, + int queryCount, + int parallelism) { + GlobalIndexer taskGlobalIndexer = + GlobalIndexerFactoryUtils.load(indexType) + .create( + Collections.singletonList(vectorColumn), Review Comment: Fixed. Distributed data-evolution tasks now call `createGlobalIndexer(splits)`, so the executor factory receives the same indexed field list as the local path, including scalar or text columns. A vector-only field list was selecting the single-file reader and rejecting multi-field shards. `SparkDataEvolutionBatchVectorReadTest#testDistributedBatchKeepsEveryIndexedField` covers the distributed batch reader used by LATERAL: two committed files per shard, and the task builds `TestMultiFieldVectorGlobalIndexer`. The single-vector reader has the same regression. -- 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]
