anuragmantri commented on code in PR #16305: URL: https://github.com/apache/iceberg/pull/16305#discussion_r3346018538
########## core/src/main/java/org/apache/iceberg/actions/KWayMergeRewriteFilePlanner.java: ########## @@ -0,0 +1,266 @@ +/* + * 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 java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.FileScanTask; +import org.apache.iceberg.RewriteJobOrder; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.StructLike; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableProperties; +import org.apache.iceberg.TableScan; +import org.apache.iceberg.actions.RewriteDataFiles.FileGroupInfo; +import org.apache.iceberg.data.GenericRecord; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.io.CloseableIterable; +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.Iterables; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.types.Comparators; +import org.apache.iceberg.types.Conversions; +import org.apache.iceberg.types.Type; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.util.PropertyUtil; +import org.apache.iceberg.util.StructLikeMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A planner for K-way merge rewriting that selects files based on size thresholds and orders them + * by sort-key lower bounds before bin-packing into groups. + * + * <p>File selection uses the same size-based criteria as other strategies: files outside the + * desired size range (too small or too large) are candidates for rewriting. This ensures K-way + * merge compacts fragmented files within a partition without shuffling, preserving the existing + * sort order. + * + * <p>Files are sorted by their lower bounds on the first sort field before bin-packing so that + * adjacent files in key space are packed into the same group, minimizing cross-group overlap. + */ +public class KWayMergeRewriteFilePlanner + extends SizeBasedFileRewritePlanner<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> { + + private static final Logger LOG = LoggerFactory.getLogger(KWayMergeRewriteFilePlanner.class); + + private final Expression filter; + private final Long snapshotId; + private final boolean caseSensitive; + + private RewriteJobOrder rewriteJobOrder; + + public KWayMergeRewriteFilePlanner(Table table) { + this(table, Expressions.alwaysTrue(), null, false); + } + + public KWayMergeRewriteFilePlanner(Table table, Expression filter) { + this( + table, + filter, + table.currentSnapshot() != null ? table.currentSnapshot().snapshotId() : null, + false); + } + + public KWayMergeRewriteFilePlanner( + Table table, Expression filter, Long snapshotId, boolean caseSensitive) { + super(table); + this.filter = filter; + this.snapshotId = snapshotId; + this.caseSensitive = caseSensitive; + } + + @Override + public Set<String> validOptions() { + return ImmutableSet.<String>builder() + .addAll(super.validOptions()) + .add(RewriteDataFiles.REWRITE_JOB_ORDER) + .build(); + } + + @Override + public void init(Map<String, String> options) { + super.init(options); + this.rewriteJobOrder = + RewriteJobOrder.fromName( + PropertyUtil.propertyAsString( + options, + RewriteDataFiles.REWRITE_JOB_ORDER, + RewriteDataFiles.REWRITE_JOB_ORDER_DEFAULT)); + } + + @Override + protected long defaultTargetFileSize() { + return PropertyUtil.propertyAsLong( + table().properties(), + TableProperties.WRITE_TARGET_FILE_SIZE_BYTES, + TableProperties.WRITE_TARGET_FILE_SIZE_BYTES_DEFAULT); + } + + @Override + protected Iterable<FileScanTask> filterFiles(Iterable<FileScanTask> tasks) { + return Iterables.filter(tasks, this::outsideDesiredFileSizeRange); + } + + @Override + protected Iterable<List<FileScanTask>> filterFileGroups(List<List<FileScanTask>> groups) { + return Iterables.filter( + groups, group -> enoughInputFiles(group) || enoughContent(group) || tooMuchContent(group)); + } + + @Override + protected Iterable<List<FileScanTask>> planFileGroups(Iterable<FileScanTask> tasks) { Review Comment: I now inherited from the BinPackRewriteFilePlanner. That reduced a lot of duplication but I had to override `planFileGroups()` because we sort by lower bounds to reduce the overlap. -- 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]
