-
Notifications
You must be signed in to change notification settings - Fork 3k
[Core][Flink][Spark]: Refactor TaskWriter implementations
#4132
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
10 commits
Select commit
Hold shift + click to select a range
dcdaccf
Core: add `FanoutEqualityDeleteWriter` and `FanoutPositionDeleteWriter`
dungdm93 6e6b519
Core: make `StructCopy` public
dungdm93 6747c61
Core: make `FileWriter.write` return `PathOffset`
dungdm93 3432723
Core: make `PartitioningWriter.write` return `PathOffset`
dungdm93 78ff4a0
Core: implement `PartitioningWriterFactory`
dungdm93 f2b60cf
Core: implement `DirectTaskWriter`
dungdm93 76fd2e9
Flink: implement `FlinkTaskWriter`
dungdm93 da59042
Flink: re-implement `RowDataTaskWriterFactory`
dungdm93 732ef14
Flink: remove legacy `DeltaTaskWriter`s
dungdm93 40354bb
Spark: refactor to use `DirectTaskWriter`
dungdm93 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
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
69 changes: 69 additions & 0 deletions
69
core/src/main/java/org/apache/iceberg/io/DefaultPartitioningWriterFactory.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,69 @@ | ||
| /* | ||
| * 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 org.apache.iceberg.FileFormat; | ||
| import org.apache.iceberg.deletes.PositionDelete; | ||
|
|
||
| class DefaultPartitioningWriterFactory<T> implements PartitioningWriterFactory<T> { | ||
| private final FileWriterFactory<T> writerFactory; | ||
| private final OutputFileFactory fileFactory; | ||
| private final FileIO io; | ||
| private final FileFormat fileFormat; | ||
| private final long targetFileSizeInBytes; | ||
| private final Type type; | ||
|
|
||
| DefaultPartitioningWriterFactory( | ||
| FileWriterFactory<T> writerFactory, OutputFileFactory fileFactory, | ||
| FileIO io, FileFormat fileFormat, long targetFileSizeInBytes, Type type) { | ||
| this.writerFactory = writerFactory; | ||
| this.fileFactory = fileFactory; | ||
| this.io = io; | ||
| this.fileFormat = fileFormat; | ||
| this.targetFileSizeInBytes = targetFileSizeInBytes; | ||
| this.type = type; | ||
| } | ||
|
|
||
| @Override | ||
| public PartitioningWriter<T, DataWriteResult> newDataWriter() { | ||
| return type == Type.CLUSTERED ? | ||
| new ClusteredDataWriter<>(writerFactory, fileFactory, io, fileFormat, targetFileSizeInBytes) : | ||
| new FanoutDataWriter<>(writerFactory, fileFactory, io, fileFormat, targetFileSizeInBytes); | ||
| } | ||
|
|
||
| @Override | ||
| public PartitioningWriter<T, DeleteWriteResult> newEqualityDeleteWriter() { | ||
| return type == Type.CLUSTERED ? | ||
| new ClusteredEqualityDeleteWriter<>(writerFactory, fileFactory, io, fileFormat, targetFileSizeInBytes) : | ||
| new FanoutEqualityDeleteWriter<>(writerFactory, fileFactory, io, fileFormat, targetFileSizeInBytes); | ||
| } | ||
|
|
||
| @Override | ||
| public PartitioningWriter<PositionDelete<T>, DeleteWriteResult> newPositionDeleteWriter() { | ||
| return type == Type.CLUSTERED ? | ||
| new ClusteredPositionDeleteWriter<>(writerFactory, fileFactory, io, fileFormat, targetFileSizeInBytes) : | ||
| new FanoutPositionDeleteWriter<>(writerFactory, fileFactory, io, fileFormat, targetFileSizeInBytes); | ||
| } | ||
|
|
||
| enum Type { | ||
| CLUSTERED, | ||
| FANOUT, | ||
| } | ||
| } |
88 changes: 88 additions & 0 deletions
88
core/src/main/java/org/apache/iceberg/io/DirectTaskWriter.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,88 @@ | ||
| /* | ||
| * 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.function.Function; | ||
| import org.apache.iceberg.PartitionSpec; | ||
| import org.apache.iceberg.StructLike; | ||
| import org.apache.iceberg.util.Tasks; | ||
|
|
||
| public class DirectTaskWriter<T> implements TaskWriter<T> { | ||
| @SuppressWarnings("rawtypes") | ||
| private static final Function UNPARTITION = s -> null; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public static <T> Function<T, StructLike> unpartition() { | ||
| return UNPARTITION; | ||
| } | ||
|
|
||
| private final PartitioningWriter<T, DataWriteResult> writer; | ||
| private final Function<T, StructLike> partitioner; | ||
| private final PartitionSpec spec; | ||
| private final FileIO io; | ||
|
|
||
| public DirectTaskWriter( | ||
| PartitioningWriterFactory<T> partitioningWriterFactory, | ||
| Function<T, StructLike> partitioner, PartitionSpec spec, | ||
| FileIO io) { | ||
| this.writer = partitioningWriterFactory.newDataWriter(); | ||
| this.partitioner = partitioner; | ||
| this.spec = spec; | ||
| this.io = io; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(T row) throws IOException { | ||
| StructLike partition = partitioner.apply(row); | ||
| writer.write(row, spec, partition); | ||
| } | ||
|
|
||
| @Override | ||
| public void abort() throws IOException { | ||
| close(); | ||
|
|
||
| // clean up files created by this writer | ||
| WriteResult result = writeResult(); | ||
| Tasks.foreach(result.dataFiles()) | ||
| .throwFailureWhenFinished() | ||
| .noRetry() | ||
| .run(file -> io.deleteFile(file.path().toString())); | ||
| } | ||
|
|
||
| @Override | ||
| public WriteResult complete() throws IOException { | ||
| close(); | ||
| return writeResult(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| writer.close(); | ||
| } | ||
|
|
||
| private WriteResult writeResult() { | ||
| DataWriteResult result = writer.result(); | ||
|
|
||
| return WriteResult.builder() | ||
| .addDataFiles(result.dataFiles()) | ||
| .build(); | ||
| } | ||
| } | ||
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
| /* | ||
| * 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.FileFormat; | ||
| import org.apache.iceberg.PartitionSpec; | ||
| import org.apache.iceberg.StructLike; | ||
| import org.apache.iceberg.encryption.EncryptedOutputFile; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
|
|
||
| /** | ||
| * An equality delete writer capable of writing to multiple specs and partitions that keeps | ||
| * delete writers for each seen spec/partition pair open until this writer is closed. | ||
| */ | ||
| public class FanoutEqualityDeleteWriter<T> extends FanoutWriter<T, DeleteWriteResult> { | ||
|
|
||
| private final FileWriterFactory<T> writerFactory; | ||
| private final OutputFileFactory fileFactory; | ||
| private final FileIO io; | ||
| private final FileFormat fileFormat; | ||
| private final long targetFileSizeInBytes; | ||
| private final List<DeleteFile> deleteFiles; | ||
|
|
||
| public FanoutEqualityDeleteWriter( | ||
| FileWriterFactory<T> writerFactory, OutputFileFactory fileFactory, | ||
| FileIO io, FileFormat fileFormat, long targetFileSizeInBytes) { | ||
| this.writerFactory = writerFactory; | ||
| this.fileFactory = fileFactory; | ||
| this.io = io; | ||
| this.fileFormat = fileFormat; | ||
| this.targetFileSizeInBytes = targetFileSizeInBytes; | ||
| this.deleteFiles = Lists.newArrayList(); | ||
| } | ||
|
|
||
| @Override | ||
| protected FileWriter<T, DeleteWriteResult> newWriter(PartitionSpec spec, StructLike partition) { | ||
| // TODO: support ORC rolling writers | ||
| if (fileFormat == FileFormat.ORC) { | ||
| EncryptedOutputFile outputFile = newOutputFile(fileFactory, spec, partition); | ||
| return writerFactory.newEqualityDeleteWriter(outputFile, spec, partition); | ||
| } else { | ||
| return new RollingEqualityDeleteWriter<>(writerFactory, fileFactory, io, targetFileSizeInBytes, spec, partition); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void addResult(DeleteWriteResult result) { | ||
| Preconditions.checkArgument(!result.referencesDataFiles(), "Equality deletes cannot reference data files"); | ||
| deleteFiles.addAll(result.deleteFiles()); | ||
| } | ||
|
|
||
| @Override | ||
| protected DeleteWriteResult aggregatedResult() { | ||
| return new DeleteWriteResult(deleteFiles); | ||
| } | ||
| } |
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.
@openinx are you have any naming suggestion for this class,
DirectTaskWriter,AppendTaskWriter,...?