aokolnychyi commented on code in PR #7389: URL: https://github.com/apache/iceberg/pull/7389#discussion_r1179811288
########## spark/v3.4/spark/src/main/java/org/apache/iceberg/spark/actions/RewritePositionDeletesSparkAction.java: ########## @@ -0,0 +1,511 @@ +/* + * 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 java.io.IOException; +import java.math.RoundingMode; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.iceberg.DeleteFile; +import org.apache.iceberg.MetadataTableType; +import org.apache.iceberg.MetadataTableUtils; +import org.apache.iceberg.PositionDeletesScanTask; +import org.apache.iceberg.RewriteJobOrder; +import org.apache.iceberg.StructLike; +import org.apache.iceberg.Table; +import org.apache.iceberg.actions.FileRewriter; +import org.apache.iceberg.actions.ImmutablePositionDeleteGroupInfo; +import org.apache.iceberg.actions.ImmutableResult; +import org.apache.iceberg.actions.RewritePositionDeleteFiles; +import org.apache.iceberg.actions.RewritePositionDeleteGroup; +import org.apache.iceberg.actions.RewritePositionDeletesCommitManager; +import org.apache.iceberg.data.GenericRecord; +import org.apache.iceberg.exceptions.CommitFailedException; +import org.apache.iceberg.exceptions.ValidationException; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.relocated.com.google.common.collect.Queues; +import org.apache.iceberg.relocated.com.google.common.collect.Sets; +import org.apache.iceberg.relocated.com.google.common.math.IntMath; +import org.apache.iceberg.relocated.com.google.common.util.concurrent.MoreExecutors; +import org.apache.iceberg.relocated.com.google.common.util.concurrent.ThreadFactoryBuilder; +import org.apache.iceberg.types.Types.StructType; +import org.apache.iceberg.util.PropertyUtil; +import org.apache.iceberg.util.StructLikeMap; +import org.apache.iceberg.util.Tasks; +import org.apache.spark.sql.SparkSession; +import org.apache.spark.sql.internal.SQLConf; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Spark implementation of {@link org.apache.iceberg.actions.RewritePositionDeleteFiles}. */ +public class RewritePositionDeletesSparkAction + extends BaseSnapshotUpdateSparkAction<RewritePositionDeletesSparkAction> + implements RewritePositionDeleteFiles { + + private static final Logger LOG = + LoggerFactory.getLogger(RewritePositionDeletesSparkAction.class); + private static final Set<String> VALID_OPTIONS = + ImmutableSet.of( + MAX_CONCURRENT_FILE_GROUP_REWRITES, + MAX_FILE_GROUP_SIZE_BYTES, + PARTIAL_PROGRESS_ENABLED, + PARTIAL_PROGRESS_MAX_COMMITS, + TARGET_FILE_SIZE_BYTES, + REWRITE_JOB_ORDER); + + private final Table table; + private final FileRewriter<PositionDeletesScanTask, DeleteFile> rewriter; + + private int maxConcurrentFileGroupRewrites; + private int maxCommits; + private boolean partialProgressEnabled; + private RewriteJobOrder rewriteJobOrder; + + RewritePositionDeletesSparkAction(SparkSession spark, Table table) { + super(spark.cloneSession()); + + // Disable Adaptive Query Execution as this may change the output partitioning of our write + spark().conf().set(SQLConf.ADAPTIVE_EXECUTION_ENABLED().key(), false); + this.table = table; + this.rewriter = new SparkPositionDeletesRewriter(spark, table); + } + + @Override + protected RewritePositionDeletesSparkAction self() { + return this; + } + + @Override + public RewritePositionDeletesSparkAction filter(Expression expression) { + throw new UnsupportedOperationException("Regular filters not supported yet."); + } + + @Override + public Result execute() { + if (table.currentSnapshot() == null) { + LOG.info("Nothing found to rewrite in empty table {}", table.name()); + return ImmutableResult.builder() + .rewrittenDeleteFilesCount(0) + .addedDeleteFilesCount(0) + .rewrittenBytesCount(0) + .addedBytesCount(0) + .build(); + } + + validateAndInitOptions(); + + Map<StructLike, List<List<PositionDeletesScanTask>>> fileGroupsByPartition = planFileGroups(); + RewriteExecutionContext ctx = new RewriteExecutionContext(fileGroupsByPartition); + + if (ctx.totalGroupCount() == 0) { + LOG.info("Nothing found to rewrite in {}", table.name()); + return ImmutableResult.builder() + .rewrittenDeleteFilesCount(0) + .addedDeleteFilesCount(0) + .rewrittenBytesCount(0) + .addedBytesCount(0) + .build(); + } + + Stream<RewritePositionDeleteGroup> groupStream = toGroupStream(ctx, fileGroupsByPartition); + + RewritePositionDeletesCommitManager commitManager = commitManager(); + if (partialProgressEnabled) { + return doExecuteWithPartialProgress(ctx, groupStream, commitManager); + } else { + return doExecute(ctx, groupStream, commitManager); + } + } + + Map<StructLike, List<List<PositionDeletesScanTask>>> planFileGroups() { + Table deletesTable = + MetadataTableUtils.createMetadataTableInstance(table, MetadataTableType.POSITION_DELETES); + CloseableIterable<PositionDeletesScanTask> scanTasks = + CloseableIterable.transform( + deletesTable.newBatchScan().ignoreResiduals().planFiles(), + t -> (PositionDeletesScanTask) t); + + try { + StructType partitionType = table.spec().partitionType(); + StructLikeMap<List<PositionDeletesScanTask>> filesByPartition = + StructLikeMap.create(partitionType); + StructLike emptyStruct = GenericRecord.create(partitionType); + + scanTasks.forEach( + task -> { + // If a task uses an incompatible partition spec the data inside could contain values + // which + // belong to multiple partitions in the current spec. Treating all such files as + // un-partitioned and + // grouping them together helps to minimize new files made. + StructLike taskPartition = Review Comment: I don't think this would apply in this case as the in and out spec will always be the same. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
