leaves12138 commented on code in PR #8549: URL: https://github.com/apache/paimon/pull/8549#discussion_r3564294354
########## paimon-core/src/main/java/org/apache/paimon/index/pkvector/PkVectorAnnSegmentFile.java: ########## @@ -0,0 +1,268 @@ +/* + * 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.index.pkvector; + +import org.apache.paimon.fs.FileIO; +import org.apache.paimon.fs.Path; +import org.apache.paimon.fs.PositionOutputStream; +import org.apache.paimon.globalindex.GlobalIndexSingleColumnWriter; +import org.apache.paimon.globalindex.GlobalIndexWriter; +import org.apache.paimon.globalindex.GlobalIndexer; +import org.apache.paimon.globalindex.ResultEntry; +import org.apache.paimon.globalindex.VectorGlobalIndexer; +import org.apache.paimon.globalindex.io.GlobalIndexFileWriter; +import org.apache.paimon.index.GlobalIndexMeta; +import org.apache.paimon.index.IndexFile; +import org.apache.paimon.index.IndexFileMeta; +import org.apache.paimon.index.IndexPathFactory; +import org.apache.paimon.io.DataFileMeta; +import org.apache.paimon.options.Options; +import org.apache.paimon.types.DataField; +import org.apache.paimon.types.VectorType; +import org.apache.paimon.utils.IOUtils; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.function.LongPredicate; + +import static org.apache.paimon.utils.Preconditions.checkArgument; + +/** Builds immutable ANN payloads whose index ids are source data-file row positions. */ +public class PkVectorAnnSegmentFile extends IndexFile { + + public PkVectorAnnSegmentFile(FileIO fileIO, IndexPathFactory pathFactory) { + super(fileIO, pathFactory); + } + + public IndexFileMeta build( + List<Source> sources, + DataField vectorField, + Options indexOptions, + String metric, + String indexType) + throws IOException { + checkArgument(!sources.isEmpty(), "An ANN segment must reference source files."); + long totalRowCount = 0; + List<PkVectorSourceFile> sourceFiles = new ArrayList<>(sources.size()); + for (Source source : sources) { + totalRowCount = Math.addExact(totalRowCount, source.sourceFile.rowCount()); + sourceFiles.add(source.sourceFile); + } + + GlobalIndexer indexer = GlobalIndexer.create(indexType, vectorField, indexOptions); + checkArgument( + indexer instanceof VectorGlobalIndexer, + "Index algorithm %s does not implement VectorGlobalIndexer.", + indexType); + String indexerMetric = normalizeMetric(((VectorGlobalIndexer) indexer).metric()); + checkArgument( + normalizeMetric(metric).equals(indexerMetric), + "Configured metric %s does not match index algorithm metric %s.", + metric, + indexerMetric); + + SegmentFileWriter fileWriter = new SegmentFileWriter(); + GlobalIndexWriter writer = null; + boolean success = false; + try { + writer = indexer.createWriter(fileWriter); + checkArgument( + writer instanceof GlobalIndexSingleColumnWriter, + "Index algorithm %s does not create a single-column writer.", + indexType); + GlobalIndexSingleColumnWriter singleColumnWriter = + (GlobalIndexSingleColumnWriter) writer; + long liveRowCount = 0; + long fileOffset = 0; + int dimension = -1; + for (Source source : sources) { + PkVectorReader vectors = source.openReader(); + try { + checkArgument( + vectors.rowCount() == source.sourceFile.rowCount(), + "Vector row count %s does not match source file %s row count %s.", + vectors.rowCount(), + source.sourceFile.fileName(), + source.sourceFile.rowCount()); + if (dimension < 0) { + dimension = vectors.dimension(); + } + checkArgument( + vectors.dimension() == dimension, + "Vector source %s dimension %s does not match dimension %s.", + source.sourceFile.fileName(), + vectors.dimension(), + dimension); + if (vectorField.type() instanceof VectorType) { + checkArgument( + ((VectorType) vectorField.type()).getLength() == dimension, + "Vector field dimension %s does not match source vector dimension %s.", + ((VectorType) vectorField.type()).getLength(), + dimension); + } + + float[] vector = new float[dimension]; + for (long rowPosition = 0; rowPosition < vectors.rowCount(); rowPosition++) { + boolean present = vectors.readNextVector(vector); + if (!present || source.excludedPosition.test(rowPosition)) { + continue; + } + singleColumnWriter.write(vector, fileOffset + rowPosition); + liveRowCount++; + } + } finally { + source.closeReader(vectors); + } + fileOffset += source.sourceFile.rowCount(); + } + + List<ResultEntry> results = writer.finish(); + checkArgument( + results.size() == 1, + "ANN segment build must produce exactly one payload file, but produced %s.", + results.size()); + ResultEntry result = results.get(0); + Path payloadPath = fileWriter.path(result.fileName()); + byte[] payloadMetadata = result.meta() == null ? new byte[0] : result.meta(); + IndexFileMeta segment = + new IndexFileMeta( + indexType, + result.fileName(), + fileIO.getFileSize(payloadPath), + liveRowCount, + new GlobalIndexMeta( + 0, + totalRowCount, Review Comment: `GlobalIndexMeta` row ranges are inclusive, but the physical ANN ordinals written above are `0..totalRowCount - 1`. Storing `[0, totalRowCount]` therefore advertises one nonexistent row and can distort global-index coverage/conflict checks. A two-file test with four total rows currently records `rowRangeEnd() == 4`; it should be `3`. Please use `totalRowCount - 1` here and add a regression assertion for the persisted range (and reject an all-empty source set if that can reach this builder). -- 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]
