steFaiz commented on code in PR #8344: URL: https://github.com/apache/paimon/pull/8344#discussion_r3472713837
########## paimon-core/src/main/java/org/apache/paimon/deletionvectors/DataEvolutionApplyDvReader.java: ########## @@ -0,0 +1,212 @@ +/* + * 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.deletionvectors; + +import org.apache.paimon.data.InternalRow; +import org.apache.paimon.fs.FileIO; +import org.apache.paimon.reader.RecordReader; +import org.apache.paimon.table.SpecialFields; +import org.apache.paimon.table.source.DeletionFile; +import org.apache.paimon.types.RowType; +import org.apache.paimon.utils.ProjectedRow; +import org.apache.paimon.utils.Range; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; + +/** + * The RecordReader to apply deletion vectors for data evolution tables. At first, readType will be + * enriched by `_ROW_ID`, then the returned id will be filtered by DVs. + */ +public class DataEvolutionApplyDvReader implements RecordReader<InternalRow> { + + private final RecordReader<InternalRow> reader; + private final List<RowRangeDeletionVector> deletionVectors; + @Nullable private final ProjectedRow projectedRow; + private final int rowIdIndex; + + private int nextDvIndex; + private RowRangeDeletionVector currentDv; + + public DataEvolutionApplyDvReader(RecordReader<InternalRow> reader, Info info) { + this.reader = reader; + this.deletionVectors = new ArrayList<>(info.deletionVectors); + this.deletionVectors.sort(Comparator.comparingLong(dv -> dv.range.from)); + this.rowIdIndex = info.rowIdIndex; + this.projectedRow = info.projectedRow; + + this.nextDvIndex = 1; + this.currentDv = deletionVectors.get(0); + } + + @Nullable + @Override + public RecordIterator<InternalRow> readBatch() throws IOException { + RecordIterator<InternalRow> iterator = reader.readBatch(); + if (iterator == null) { + return null; + } + + return new RecordIterator<InternalRow>() { + + @Nullable + @Override + public InternalRow next() throws IOException { + while (true) { + InternalRow row = iterator.next(); + if (row == null) { + return null; + } + + if (!isDeleted(row)) { + if (projectedRow != null) { + return projectedRow.replaceRow(row); + } + return row; + } + } + } + + @Override + public void releaseBatch() { + iterator.releaseBatch(); + } + }; + } + + private boolean isDeleted(InternalRow row) { + long rowId = row.getLong(rowIdIndex); + + moveToPossibleDv(rowId); + + if (currentDv == null || !currentDv.mayContains(rowId)) { + return false; + } + + return currentDv.isDeleted(rowId); Review Comment: Thanks for your remind! I've added the check in `GlobalCombiner` and added a test -- 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]
