liangjie3138 commented on code in PR #8834: URL: https://github.com/apache/paimon/pull/8834#discussion_r3643735573
########## paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/vectorsearch/FlinkDataEvolutionVectorRead.java: ########## @@ -0,0 +1,596 @@ +/* + * 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.flink.vectorsearch; + +import org.apache.paimon.flink.utils.StreamExecutionEnvironmentUtils; +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.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.DataEvolutionVectorRead; +import org.apache.paimon.table.source.IndexVectorSearchSplit; +import org.apache.paimon.table.source.RawVectorSearchSplit; +import org.apache.paimon.table.source.VectorScan; +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.flink.api.common.functions.MapFunction; +import org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.util.CloseableIterator; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; + +import static org.apache.paimon.CoreOptions.GLOBAL_INDEX_THREAD_NUM; +import static org.apache.paimon.utils.Preconditions.checkNotNull; + +/** Flink-aware {@link DataEvolutionVectorRead}. */ +public class FlinkDataEvolutionVectorRead extends DataEvolutionVectorRead { + + private static final long serialVersionUID = 1L; + private static final byte INDEX_RESULT = 0; + private static final byte RAW_RESULT = 1; + + private final transient StreamExecutionEnvironment env; + + public FlinkDataEvolutionVectorRead( + FileStoreTable table, + @Nullable PartitionPredicate partitionFilter, + @Nullable Predicate filter, + int limit, + DataField vectorColumn, + float[] vector, + @Nullable Map<String, String> options, + StreamExecutionEnvironment env) { + super(table, partitionFilter, filter, limit, vectorColumn, vector, options); + this.env = checkNotNull(env); + } + + @Override + public GlobalIndexResult read(VectorScan.Plan plan) { + List<IndexVectorSearchSplit> indexSplits = new ArrayList<>(); + List<RawVectorSearchSplit> rawSplits = new ArrayList<>(); + splitSearchSplits(plan.splits(), indexSplits, rawSplits); + if (indexSplits.isEmpty() && rawSplits.isEmpty()) { + return GlobalIndexResult.createEmpty(); + } + + GlobalIndexer globalIndexer = + !indexSplits.isEmpty() && !rawSplits.isEmpty() + ? createGlobalIndexer(indexSplits) + : null; + if (!indexSplits.isEmpty() && !rawSplits.isEmpty()) { + int parallelism = flinkParallelism(); + List<Range> rawRowRanges = rawRowRanges(rawSplits); + if (indexSplits.size() >= parallelism * 2L Review Comment: Thanks. The factor 2 follows Spark’s heuristic to amortize distributed execution overhead. Small index splits are already grouped into at most parallelism tasks, while Raw ranges are balanced by row count. Keeping it fixed avoids another configuration option. -- 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]
