-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[core] introducing row-range-keyed DeletionVectors for DataEvolution tables #8344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
steFaiz
wants to merge
6
commits into
apache:master
Choose a base branch
from
steFaiz:dv_for_de_core_support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,802
−412
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
94a4727
[core] introducing row-range-keyed DeletionVectors for DataEvolution …
steFaiz c04f1a2
fix tests
steFaiz 8f28553
fix tests
steFaiz e381df9
fix tests
steFaiz 9cebbd7
add test
steFaiz b98f581
fix comments: add some hard check
steFaiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
228 changes: 228 additions & 0 deletions
228
paimon-core/src/main/java/org/apache/paimon/deletionvectors/DataEvolutionApplyDvReader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| /* | ||
| * 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.Preconditions; | ||
| 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. | ||
| * | ||
| * <p>This reader assumes that the underlying reader will return monotonically incrementing | ||
| * _ROW_IDs, which is guaranteed by the current implementation. | ||
| */ | ||
| 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 long lastRowId = -1; | ||
| 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); | ||
| checkRowIdMonotonicity(rowId); | ||
|
|
||
| moveToPossibleDv(rowId); | ||
|
|
||
| if (currentDv == null || !currentDv.mayContains(rowId)) { | ||
| return false; | ||
| } | ||
|
|
||
| return currentDv.isDeleted(rowId); | ||
| } | ||
|
|
||
| private void checkRowIdMonotonicity(long rowId) { | ||
| if (lastRowId >= 0) { | ||
| Preconditions.checkState( | ||
| rowId > lastRowId, | ||
| "This reader works only if underlying reader produces incremental _ROW_IDs."); | ||
| } | ||
|
|
||
| lastRowId = rowId; | ||
| } | ||
|
|
||
| private void moveToPossibleDv(long rowId) { | ||
| if (currentDv == null) { | ||
| return; | ||
| } | ||
|
|
||
| while (rowId > currentDv.range.to) { | ||
| if (nextDvIndex >= deletionVectors.size()) { | ||
| currentDv = null; | ||
| return; | ||
| } | ||
| currentDv = deletionVectors.get(nextDvIndex); | ||
| nextDvIndex++; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| reader.close(); | ||
| } | ||
|
|
||
| public static Info readInfo( | ||
| FileIO fileIO, RowType readRowType, Map<Range, DeletionFile> deletionFiles) | ||
| throws IOException { | ||
| if (deletionFiles == null || deletionFiles.isEmpty()) { | ||
| return Info.noDeletionVectors(readRowType); | ||
| } | ||
|
|
||
| List<RowRangeDeletionVector> deletionVectors = new ArrayList<>(deletionFiles.size()); | ||
| for (Map.Entry<Range, DeletionFile> entry : deletionFiles.entrySet()) { | ||
| DeletionVector deletionVector = DeletionVector.read(fileIO, entry.getValue()); | ||
| if (!deletionVector.isEmpty()) { | ||
| deletionVectors.add(new RowRangeDeletionVector(entry.getKey(), deletionVector)); | ||
| } | ||
| } | ||
| if (deletionVectors.isEmpty()) { | ||
| return Info.noDeletionVectors(readRowType); | ||
| } | ||
|
|
||
| int rowIdIndex = readRowType.getFieldIndex(SpecialFields.ROW_ID.name()); | ||
| RowType actualReadType = readRowType; | ||
| ProjectedRow projectedRow = null; | ||
| if (rowIdIndex == -1) { | ||
| actualReadType = SpecialFields.rowTypeWithRowId(readRowType); | ||
| rowIdIndex = actualReadType.getFieldCount() - 1; | ||
| int[] mappings = new int[readRowType.getFieldCount()]; | ||
| for (int i = 0; i < readRowType.getFieldCount(); i++) { | ||
| mappings[i] = i; | ||
| } | ||
| projectedRow = ProjectedRow.from(mappings); | ||
| } | ||
|
|
||
| return new Info(deletionVectors, rowIdIndex, actualReadType, projectedRow); | ||
| } | ||
|
|
||
| /** Information for data evolution deletion vector applying. */ | ||
| public static class Info { | ||
|
|
||
| private final List<RowRangeDeletionVector> deletionVectors; | ||
| private final int rowIdIndex; | ||
| public final RowType actualReadType; | ||
| @Nullable private final ProjectedRow projectedRow; | ||
|
|
||
| private Info( | ||
| List<RowRangeDeletionVector> deletionVectors, | ||
| int rowIdIndex, | ||
| RowType actualReadType, | ||
| @Nullable ProjectedRow projectedRow) { | ||
| this.deletionVectors = deletionVectors; | ||
| this.rowIdIndex = rowIdIndex; | ||
| this.actualReadType = actualReadType; | ||
| this.projectedRow = projectedRow; | ||
| } | ||
|
|
||
| private static Info noDeletionVectors(RowType readRowType) { | ||
| return new Info(Collections.emptyList(), -1, readRowType, null); | ||
| } | ||
|
|
||
| public boolean hasDeletionVectors() { | ||
| return !deletionVectors.isEmpty(); | ||
| } | ||
| } | ||
|
|
||
| /** Deletion Vector and range pair. */ | ||
| private static class RowRangeDeletionVector { | ||
|
|
||
| private final Range range; | ||
| private final DeletionVector deletionVector; | ||
|
|
||
| private RowRangeDeletionVector(Range range, DeletionVector deletionVector) { | ||
| this.range = range; | ||
| this.deletionVector = deletionVector; | ||
| } | ||
|
|
||
| boolean mayContains(long rowId) { | ||
| return rowId <= range.to && rowId >= range.from; | ||
| } | ||
|
|
||
| boolean isDeleted(long rowId) { | ||
| return deletionVector.isDeleted(rowId - range.from); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reader only evaluates the first row-range DV whose end has not passed the current row id. If two row-range keyed DVs overlap, for example [0, 10] and [5, 15], rows 5-10 are never checked against the second DV, so deletions recorded only there will be returned. I don't see the writer/manifest combiner rejecting overlapping RowIdRangeKey values; they only deduplicate exact keys. Please either enforce non-overlapping row-range DVs when writing/combining metadata, or make this reader evaluate all DVs that may contain the row.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your remind! I've added the check in
GlobalCombinerand added a test