-
Notifications
You must be signed in to change notification settings - Fork 3k
Flink: Refactor to use the BaseEqualityDeltaWriter. #4264
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
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
68fd511
Flink: Refactor to use the BaseEqualityDeltaWriter.
openinx 18eca26
tmp
openinx 10daaa5
Replace the TaskWriter.
openinx 87eaaaf
Temporary fix.
openinx c98365c
Remove the old delta writer.
openinx 76979b8
Fix the broken unit tests.
openinx 4335865
Fix the broken unit tests.
openinx 1ecc3f0
Address the unit tests.
openinx d9dc0a9
Address the unit tests.
openinx 521e72d
Refactor the rowOffset to recordCount.
openinx 25071c4
Address comments.
openinx 232c4fb
Add unit tests.
openinx 3b71206
Remove the orc special cases.
openinx 8aef88d
Minor fixes.
openinx ed45f5d
Fix the checkstyles.
openinx e9657fc
Improve unit tests and address comments.
openinx 5793eeb
Fix the broken TestFlinkUpsert.
openinx 86f83fa
Rebase to the latest master & Address the unit tests.
openinx 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
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
138 changes: 138 additions & 0 deletions
138
core/src/main/java/org/apache/iceberg/io/BaseEqualityDeltaWriter.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,138 @@ | ||
| /* | ||
| * 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.iceberg.io; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import org.apache.iceberg.PartitionSpec; | ||
| import org.apache.iceberg.Schema; | ||
| import org.apache.iceberg.StructLike; | ||
| import org.apache.iceberg.deletes.PositionDelete; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.util.StructLikeMap; | ||
| import org.apache.iceberg.util.StructProjection; | ||
|
|
||
| public class BaseEqualityDeltaWriter<T> implements EqualityDeltaWriter<T> { | ||
|
|
||
| private final PositionDelete<T> posDelete = PositionDelete.create(); | ||
|
|
||
| private final PartitioningWriter<T, DataWriteResult> dataWriter; | ||
| private final PartitioningWriter<T, DeleteWriteResult> equalityWriter; | ||
| private final PartitioningWriter<PositionDelete<T>, DeleteWriteResult> positionWriter; | ||
|
|
||
| private final Map<StructLike, PathOffset> insertedRowMap; | ||
|
|
||
| private final Function<T, StructLike> asStructLikeKey; | ||
| private final Function<T, StructLike> keyRefFunc; | ||
| private final Function<T, StructLike> keyCopyFunc; | ||
|
|
||
| private boolean closed = false; | ||
|
|
||
| public BaseEqualityDeltaWriter( | ||
| PartitioningWriter<T, DataWriteResult> dataWriter, | ||
| PartitioningWriter<T, DeleteWriteResult> equalityWriter, | ||
| PartitioningWriter<PositionDelete<T>, DeleteWriteResult> positionWriter, | ||
| Schema schema, | ||
| Schema deleteSchema, | ||
| Function<T, StructLike> asStructLike, | ||
| Function<T, StructLike> asStructLikeKey) { | ||
| this.dataWriter = dataWriter; | ||
| this.equalityWriter = equalityWriter; | ||
| this.positionWriter = positionWriter; | ||
|
|
||
| this.insertedRowMap = StructLikeMap.create(deleteSchema.asStruct()); | ||
| this.asStructLikeKey = asStructLikeKey; | ||
|
|
||
| StructProjection projection = StructProjection.create(schema, deleteSchema); | ||
| this.keyRefFunc = row -> projection.wrap(asStructLike.apply(row)); | ||
| this.keyCopyFunc = row -> StructCopy.copy(keyRefFunc.apply(row)); | ||
| } | ||
|
|
||
| @Override | ||
| public void insert(T row, PartitionSpec spec, StructLike partition) { | ||
| PathOffset pathOffset = dataWriter.write(row, spec, partition); | ||
|
|
||
| // Create a copied key from this row. | ||
| StructLike copiedKey = keyCopyFunc.apply(row); | ||
|
|
||
| // Adding a pos-delete to replace the old path-offset. | ||
| PathOffset previous = insertedRowMap.put(copiedKey, pathOffset); | ||
| if (previous != null) { | ||
| posDelete.set(previous.path(), previous.rowOffset(), null); | ||
| positionWriter.write(posDelete, spec, partition); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Retire the old key & position from insertedRowMap cache to position delete file. | ||
| */ | ||
| private boolean retireOldKey(StructLike key, PartitionSpec spec, StructLike partition) { | ||
| PathOffset previous = insertedRowMap.remove(key); | ||
| if (previous != null) { | ||
| posDelete.set(previous.path(), previous.rowOffset(), null); | ||
| positionWriter.write(posDelete, spec, partition); | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void delete(T row, PartitionSpec spec, StructLike partition) { | ||
| if (!retireOldKey(keyRefFunc.apply(row), spec, partition)) { | ||
| equalityWriter.write(row, spec, partition); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteKey(T key, PartitionSpec spec, StructLike partition) { | ||
| if (!retireOldKey(asStructLikeKey.apply(key), spec, partition)) { | ||
| equalityWriter.write(key, spec, partition); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public WriteResult result() { | ||
| Preconditions.checkState(closed, "Cannot get result from unclosed writer."); | ||
|
|
||
| DataWriteResult dataWriteResult = dataWriter.result(); | ||
| DeleteWriteResult positionWriteResult = positionWriter.result(); | ||
| DeleteWriteResult equalityWriteResult = equalityWriter.result(); | ||
|
|
||
| return WriteResult.builder() | ||
| .addDataFiles(dataWriteResult.dataFiles()) | ||
| .addDeleteFiles(positionWriteResult.deleteFiles()) | ||
| .addDeleteFiles(equalityWriteResult.deleteFiles()) | ||
| .addReferencedDataFiles(positionWriteResult.referencedDataFiles()) | ||
| .build(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| if (!closed) { | ||
| dataWriter.close(); | ||
| positionWriter.close(); | ||
| equalityWriter.close(); | ||
|
|
||
| this.closed = true; | ||
| } | ||
| } | ||
| } | ||
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
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
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
62 changes: 62 additions & 0 deletions
62
core/src/main/java/org/apache/iceberg/io/FanoutEqualityDeleteWriter.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,62 @@ | ||
| /* | ||
| * 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.iceberg.io; | ||
|
|
||
| import java.util.List; | ||
| import org.apache.iceberg.DeleteFile; | ||
| import org.apache.iceberg.PartitionSpec; | ||
| import org.apache.iceberg.StructLike; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.util.CharSequenceSet; | ||
|
|
||
| public class FanoutEqualityDeleteWriter<T> extends FanoutWriter<T, DeleteWriteResult> { | ||
|
|
||
| private final FileWriterFactory<T> writerFactory; | ||
| private final OutputFileFactory fileFactory; | ||
| private final FileIO io; | ||
| private final long targetFileSizeInBytes; | ||
|
|
||
| private final List<DeleteFile> deleteFiles; | ||
|
|
||
| public FanoutEqualityDeleteWriter( | ||
| FileWriterFactory<T> writerFactory, OutputFileFactory fileFactory, | ||
| FileIO io, long targetFileSizeInBytes) { | ||
| this.writerFactory = writerFactory; | ||
| this.fileFactory = fileFactory; | ||
| this.io = io; | ||
| this.targetFileSizeInBytes = targetFileSizeInBytes; | ||
| this.deleteFiles = Lists.newArrayList(); | ||
| } | ||
|
|
||
| @Override | ||
| protected FileWriter<T, DeleteWriteResult> newWriter(PartitionSpec spec, StructLike partition) { | ||
| return new RollingEqualityDeleteWriter<>(writerFactory, fileFactory, io, targetFileSizeInBytes, spec, partition); | ||
| } | ||
|
|
||
| @Override | ||
| protected void addResult(DeleteWriteResult result) { | ||
| deleteFiles.addAll(result.deleteFiles()); | ||
| } | ||
|
|
||
| @Override | ||
| protected DeleteWriteResult aggregatedResult() { | ||
| return new DeleteWriteResult(deleteFiles, CharSequenceSet.empty()); | ||
| } | ||
| } |
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.
Are there any style guidelines for documentation? Typically its nice to have javadoc on al public classes/members that aren't overwritten.