-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Core, Spark: Remove dangling deletes as part of RewriteDataFilesAction #9724
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4a093d2
Core, Spark: Remove dangling delete files
dramaticlly 82b6889
Fix assertJ for static import
dramaticlly f6b0be3
Address szehon feedback
dramaticlly 18e2f79
Address review feedback and improve docs
dramaticlly ae887af
Merge branch 'main' into danglingDeletes
dramaticlly a84cf88
spotlessApply to reformat spaces
dramaticlly 253228a
Merge remote-tracking branch 'upstream/main' into danglingDeletes
dramaticlly 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
35 changes: 35 additions & 0 deletions
35
api/src/main/java/org/apache/iceberg/actions/RemoveDanglingDeleteFiles.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,35 @@ | ||
| /* | ||
| * 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.actions; | ||
|
|
||
| import org.apache.iceberg.DeleteFile; | ||
|
|
||
| /** | ||
| * An action that removes dangling delete files from the current snapshot. A delete file is dangling | ||
| * if its deletes no longer applies to any live data files. | ||
| */ | ||
| public interface RemoveDanglingDeleteFiles | ||
| extends Action<RemoveDanglingDeleteFiles, RemoveDanglingDeleteFiles.Result> { | ||
|
|
||
| /** An action that remove dangling deletes. */ | ||
| interface Result { | ||
| /** Return removed deletes. */ | ||
| Iterable<DeleteFile> removedDeleteFiles(); | ||
| } | ||
| } |
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
33 changes: 33 additions & 0 deletions
33
core/src/main/java/org/apache/iceberg/actions/BaseRemoveDanglingDeleteFiles.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,33 @@ | ||
| /* | ||
| * 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.actions; | ||
|
|
||
| import org.immutables.value.Value; | ||
|
|
||
| @Value.Enclosing | ||
| @SuppressWarnings("ImmutablesStyle") | ||
| @Value.Style( | ||
| typeImmutableEnclosing = "ImmutableRemoveDanglingDeleteFiles", | ||
| visibilityString = "PUBLIC", | ||
| builderVisibilityString = "PUBLIC") | ||
| interface BaseRemoveDanglingDeleteFiles extends RemoveDanglingDeleteFiles { | ||
|
|
||
| @Value.Immutable | ||
| interface Result extends RemoveDanglingDeleteFiles.Result {} | ||
| } |
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
179 changes: 179 additions & 0 deletions
179
...park/src/main/java/org/apache/iceberg/spark/actions/RemoveDanglingDeletesSparkAction.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,179 @@ | ||
| /* | ||
| * 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.spark.actions; | ||
|
|
||
| import static org.apache.spark.sql.functions.col; | ||
| import static org.apache.spark.sql.functions.min; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.iceberg.DataFile; | ||
| import org.apache.iceberg.DeleteFile; | ||
| import org.apache.iceberg.MetadataTableType; | ||
| import org.apache.iceberg.Partitioning; | ||
| import org.apache.iceberg.RewriteFiles; | ||
| import org.apache.iceberg.Table; | ||
| import org.apache.iceberg.actions.ImmutableRemoveDanglingDeleteFiles; | ||
| import org.apache.iceberg.actions.RemoveDanglingDeleteFiles; | ||
| import org.apache.iceberg.spark.JobGroupInfo; | ||
| import org.apache.iceberg.spark.SparkDeleteFile; | ||
| import org.apache.iceberg.types.Types; | ||
| import org.apache.spark.sql.Column; | ||
| import org.apache.spark.sql.Dataset; | ||
| import org.apache.spark.sql.Row; | ||
| import org.apache.spark.sql.SparkSession; | ||
| import org.apache.spark.sql.types.StructType; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * An action that removes dangling delete files from the current snapshot. A delete file is dangling | ||
| * if its deletes no longer applies to any live data files. | ||
| * | ||
| * <p>The following dangling delete files are removed: | ||
| * | ||
| * <ul> | ||
| * <li>Position delete files with a data sequence number less than that of any data file in the | ||
| * same partition | ||
| * <li>Equality delete files with a data sequence number less than or equal to that of any data | ||
| * file in the same partition | ||
| * </ul> | ||
| */ | ||
| class RemoveDanglingDeletesSparkAction | ||
| extends BaseSnapshotUpdateSparkAction<RemoveDanglingDeletesSparkAction> | ||
| implements RemoveDanglingDeleteFiles { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(RemoveDanglingDeletesSparkAction.class); | ||
| private final Table table; | ||
|
|
||
| protected RemoveDanglingDeletesSparkAction(SparkSession spark, Table table) { | ||
| super(spark); | ||
| this.table = table; | ||
| } | ||
|
|
||
| @Override | ||
| protected RemoveDanglingDeletesSparkAction self() { | ||
| return this; | ||
| } | ||
|
|
||
| public Result execute() { | ||
| if (table.specs().size() == 1 && table.spec().isUnpartitioned()) { | ||
| // ManifestFilterManager already performs this table-wide delete on each commit | ||
| return ImmutableRemoveDanglingDeleteFiles.Result.builder() | ||
| .removedDeleteFiles(Collections.emptyList()) | ||
| .build(); | ||
| } | ||
|
|
||
| String desc = String.format("Removing dangling delete files in %s", table.name()); | ||
| JobGroupInfo info = newJobGroupInfo("REMOVE-DELETES", desc); | ||
| return withJobGroupInfo(info, this::doExecute); | ||
| } | ||
|
|
||
| Result doExecute() { | ||
| RewriteFiles rewriteFiles = table.newRewrite(); | ||
| List<DeleteFile> danglingDeletes = findDanglingDeletes(); | ||
| for (DeleteFile deleteFile : danglingDeletes) { | ||
| LOG.debug("Removing dangling delete file {}", deleteFile.path()); | ||
| rewriteFiles.deleteFile(deleteFile); | ||
| } | ||
|
|
||
| if (!danglingDeletes.isEmpty()) { | ||
| commit(rewriteFiles); | ||
| } | ||
|
|
||
| return ImmutableRemoveDanglingDeleteFiles.Result.builder() | ||
| .removedDeleteFiles(danglingDeletes) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Dangling delete files can be identified with following steps | ||
| * | ||
| * <ol> | ||
| * <li>Group data files by partition keys and find the minimum data sequence number in each | ||
| * group. | ||
| * <li>Left outer join delete files with partition-grouped data files on partition keys. | ||
| * <li>Find dangling deletes by comparing each delete file's sequence number to its partition's | ||
| * minimum data sequence number. | ||
| * <li>Collect results row to driver and use {@link SparkDeleteFile SparkDeleteFile} to wrap | ||
| * rows to valid delete files | ||
| * </ol> | ||
| */ | ||
| private List<DeleteFile> findDanglingDeletes() { | ||
| Dataset<Row> minSequenceNumberByPartition = | ||
| loadMetadataTable(table, MetadataTableType.ENTRIES) | ||
| // find live data files | ||
| .filter("data_file.content == 0 AND status < 2") | ||
| .selectExpr( | ||
| "data_file.partition as partition", | ||
| "data_file.spec_id as spec_id", | ||
| "sequence_number") | ||
| .groupBy("partition", "spec_id") | ||
| .agg(min("sequence_number")) | ||
| .toDF("grouped_partition", "grouped_spec_id", "min_data_sequence_number"); | ||
|
|
||
| Dataset<Row> deleteEntries = | ||
| loadMetadataTable(table, MetadataTableType.ENTRIES) | ||
| // find live delete files | ||
| .filter("data_file.content != 0 AND status < 2"); | ||
|
|
||
| Column joinOnPartition = | ||
| deleteEntries | ||
| .col("data_file.spec_id") | ||
| .equalTo(minSequenceNumberByPartition.col("grouped_spec_id")) | ||
| .and( | ||
| deleteEntries | ||
| .col("data_file.partition") | ||
| .equalTo(minSequenceNumberByPartition.col("grouped_partition"))); | ||
|
|
||
| Column filterOnDanglingDeletes = | ||
| col("min_data_sequence_number") | ||
| // delete fies without any data files in partition | ||
| .isNull() | ||
| // position delete files without any applicable data files in partition | ||
| .or( | ||
| col("data_file.content") | ||
| .equalTo("1") | ||
| .and(col("sequence_number").$less(col("min_data_sequence_number")))) | ||
| // equality delete files without any applicable data files in the partition | ||
| .or( | ||
| col("data_file.content") | ||
| .equalTo("2") | ||
| .and(col("sequence_number").$less$eq(col("min_data_sequence_number")))); | ||
|
dramaticlly marked this conversation as resolved.
|
||
|
|
||
| Dataset<Row> danglingDeletes = | ||
| deleteEntries | ||
| .join(minSequenceNumberByPartition, joinOnPartition, "left") | ||
| .filter(filterOnDanglingDeletes) | ||
| .select("data_file.*"); | ||
| return danglingDeletes.collectAsList().stream() | ||
|
dramaticlly marked this conversation as resolved.
|
||
| // map on driver because SparkDeleteFile is not serializable | ||
| .map(row -> deleteFileWrapper(danglingDeletes.schema(), row)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private DeleteFile deleteFileWrapper(StructType sparkFileType, Row row) { | ||
| int specId = row.getInt(row.fieldIndex("spec_id")); | ||
| Types.StructType combinedFileType = DataFile.getType(Partitioning.partitionType(table)); | ||
|
dramaticlly marked this conversation as resolved.
|
||
| // Set correct spec id | ||
| Types.StructType projection = DataFile.getType(table.specs().get(specId).partitionType()); | ||
| return new SparkDeleteFile(combinedFileType, projection, sparkFileType).wrap(row); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.