pvary commented on code in PR #12493: URL: https://github.com/apache/iceberg/pull/12493#discussion_r2016142918
########## core/src/test/java/org/apache/iceberg/actions/TestBinPackRewriteFileGroupPlanner.java: ########## @@ -0,0 +1,468 @@ +/* + * 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 static org.apache.iceberg.actions.BinPackRewriteFileGroupPlanner.MAX_FILE_SIZE_DEFAULT_RATIO; +import static org.apache.iceberg.actions.RewriteDataFiles.REWRITE_JOB_ORDER; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.File; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.DataFiles; +import org.apache.iceberg.FileScanTask; +import org.apache.iceberg.MockFileScanTask; +import org.apache.iceberg.RewriteJobOrder; +import org.apache.iceberg.StructLike; +import org.apache.iceberg.TestBase; +import org.apache.iceberg.TestTables; +import org.apache.iceberg.actions.RewriteDataFiles.FileGroupInfo; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +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.relocated.com.google.common.collect.Maps; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.junit.jupiter.params.provider.ValueSource; + +class TestBinPackRewriteFileGroupPlanner { + private static final Map<String, String> REWRITE_ALL = + ImmutableMap.of(BinPackRewriteFileGroupPlanner.REWRITE_ALL, "true"); + + private static final DataFile FILE_1 = newDataFile("data_bucket=0", 10); + private static final DataFile FILE_2 = newDataFile("data_bucket=0", 10); + private static final DataFile FILE_3 = newDataFile("data_bucket=0", 10); + private static final DataFile FILE_4 = newDataFile("data_bucket=1", 11); + private static final DataFile FILE_5 = newDataFile("data_bucket=1", 11); + private static final DataFile FILE_6 = newDataFile("data_bucket=2", 50); + + private static final Map<RewriteJobOrder, List<StructLike>> EXPECTED = + ImmutableMap.of( + RewriteJobOrder.FILES_DESC, + ImmutableList.of(FILE_1.partition(), FILE_4.partition(), FILE_6.partition()), + RewriteJobOrder.FILES_ASC, + ImmutableList.of(FILE_6.partition(), FILE_4.partition(), FILE_1.partition()), + RewriteJobOrder.BYTES_DESC, + ImmutableList.of(FILE_6.partition(), FILE_1.partition(), FILE_4.partition()), + RewriteJobOrder.BYTES_ASC, + ImmutableList.of(FILE_4.partition(), FILE_1.partition(), FILE_6.partition())); + + @TempDir private File tableDir = null; + private TestTables.TestTable table = null; + + @BeforeEach + public void setupTable() throws Exception { + this.table = TestTables.create(tableDir, "test", TestBase.SCHEMA, TestBase.SPEC, 3); + } + + @AfterEach + public void cleanupTables() { + TestTables.clearTables(); + } + + @Test + void testPartitionedTable() { + addFiles(); + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init(ImmutableMap.of(BinPackRewriteFileGroupPlanner.REWRITE_ALL, "true")); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + List<RewriteFileGroup> groups = Lists.newArrayList(plan.groups().iterator()); + assertThat(groups).hasSize(3); + assertThat(groups.stream().mapToInt(RewriteGroupBase::inputFileNum).sum()).isEqualTo(6); + } + + @Test + void testUnpartitionedTable() { + table.updateSpec().removeField("data_bucket").commit(); + table.refresh(); + table + .newAppend() + .appendFile(newDataFile("", 10)) + .appendFile(newDataFile("", 20)) + .appendFile(newDataFile("", 30)) + .commit(); + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init( + ImmutableMap.of( + BinPackRewriteFileGroupPlanner.MIN_INPUT_FILES, + "1", + BinPackRewriteFileGroupPlanner.MIN_FILE_SIZE_BYTES, + "30")); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + assertThat(plan.totalGroupCount()).isEqualTo(1); + assertThat(plan.groups().iterator().next().inputFileNum()).isEqualTo(2); + } + + @Test + void testEmptyTable() { + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init(REWRITE_ALL); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + assertThat(table.currentSnapshot()).as("Table must be empty").isNull(); + assertThat(plan.totalGroupCount()).isZero(); + } + + @ParameterizedTest + @EnumSource( + value = RewriteJobOrder.class, + names = {"FILES_DESC", "FILES_ASC", "BYTES_DESC", "BYTES_ASC"}) + void testJobOrder(RewriteJobOrder order) { + addFiles(); + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init( + ImmutableMap.of( + BinPackRewriteFileGroupPlanner.REWRITE_ALL, "true", REWRITE_JOB_ORDER, order.name())); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + List<RewriteFileGroup> groups = Lists.newArrayList(plan.groups().iterator()); + assertThat(groups.stream().map(group -> group.info().partition()).collect(Collectors.toList())) + .isEqualTo(EXPECTED.get(order)); + assertThat(plan.totalGroupCount()).isEqualTo(3); + EXPECTED.get(order).forEach(s -> assertThat(plan.groupsInPartition(s)).isEqualTo(1)); + } + + @Test + void testMaxGroupSize() { + addFiles(); + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init( + ImmutableMap.of( + BinPackRewriteFileGroupPlanner.REWRITE_ALL, + "true", + BinPackRewriteFileGroupPlanner.MAX_FILE_GROUP_SIZE_BYTES, + "10")); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + assertThat(plan.totalGroupCount()).isEqualTo(6); + assertThat(plan.groupsInPartition(FILE_1.partition())).isEqualTo(3); + assertThat(plan.groupsInPartition(FILE_4.partition())).isEqualTo(2); + assertThat(plan.groupsInPartition(FILE_6.partition())).isEqualTo(1); + } + + @Test + void testFilter() { + addFiles(); + BinPackRewriteFileGroupPlanner planner = + new BinPackRewriteFileGroupPlanner( + table, + Expressions.or( + Expressions.equal(Expressions.bucket("data", 16), 0), + Expressions.equal(Expressions.bucket("data", 16), 2))); + planner.init(REWRITE_ALL); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + List<RewriteFileGroup> groups = Lists.newArrayList(plan.groups().iterator()); + assertThat(plan.totalGroupCount()).isEqualTo(2); + assertThat(groups).hasSize(2); + assertThat(groups.stream().mapToLong(RewriteFileGroup::inputFileNum).sum()).isEqualTo(4); + } + + @Test + void testMaxOutputFileSize() { + addFiles(); + int targetFileSize = 10; + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init( + ImmutableMap.of( + BinPackRewriteFileGroupPlanner.REWRITE_ALL, + "true", + BinPackRewriteFileGroupPlanner.TARGET_FILE_SIZE_BYTES, + String.valueOf(targetFileSize))); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + assertThat(plan.groups().iterator().next().maxOutputFileSize()) + .isGreaterThan(targetFileSize) + .isLessThan((long) (targetFileSize * MAX_FILE_SIZE_DEFAULT_RATIO)); + } + + @Test + void testExpectedOutputFiles() { + addFiles(); + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init( + ImmutableMap.of( + BinPackRewriteFileGroupPlanner.REWRITE_ALL, + "true", + BinPackRewriteFileGroupPlanner.TARGET_FILE_SIZE_BYTES, + "21")); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + List<RewriteFileGroup> groups = Lists.newArrayList(plan.groups().iterator()); + assertThat(groups).hasSize(3); + for (RewriteFileGroup group : groups) { + if (FILE_1.partition().equals(group.info().partition())) { + assertThat(group.expectedOutputFiles()).isEqualTo(2); + } else if (FILE_4.partition().equals(group.info().partition())) { + assertThat(group.expectedOutputFiles()).isEqualTo(1); Review Comment: This one is a bit tricky. We allow file size 10% bigger than the target. See: `SizeBasedFileRewritePlanner.expectedOutputFiles` method ``` * [..] If the new average file size is no more than 10% greater than * the target file size, then this method will round down when determining the number of output * files. Otherwise, the remainder will be written into a separate file. ``` Added some comment to describe the tests case ########## core/src/test/java/org/apache/iceberg/actions/TestBinPackRewriteFileGroupPlanner.java: ########## @@ -0,0 +1,468 @@ +/* + * 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 static org.apache.iceberg.actions.BinPackRewriteFileGroupPlanner.MAX_FILE_SIZE_DEFAULT_RATIO; +import static org.apache.iceberg.actions.RewriteDataFiles.REWRITE_JOB_ORDER; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.File; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.DataFiles; +import org.apache.iceberg.FileScanTask; +import org.apache.iceberg.MockFileScanTask; +import org.apache.iceberg.RewriteJobOrder; +import org.apache.iceberg.StructLike; +import org.apache.iceberg.TestBase; +import org.apache.iceberg.TestTables; +import org.apache.iceberg.actions.RewriteDataFiles.FileGroupInfo; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +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.relocated.com.google.common.collect.Maps; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.junit.jupiter.params.provider.ValueSource; + +class TestBinPackRewriteFileGroupPlanner { + private static final Map<String, String> REWRITE_ALL = + ImmutableMap.of(BinPackRewriteFileGroupPlanner.REWRITE_ALL, "true"); + + private static final DataFile FILE_1 = newDataFile("data_bucket=0", 10); + private static final DataFile FILE_2 = newDataFile("data_bucket=0", 10); + private static final DataFile FILE_3 = newDataFile("data_bucket=0", 10); + private static final DataFile FILE_4 = newDataFile("data_bucket=1", 11); + private static final DataFile FILE_5 = newDataFile("data_bucket=1", 11); + private static final DataFile FILE_6 = newDataFile("data_bucket=2", 50); + + private static final Map<RewriteJobOrder, List<StructLike>> EXPECTED = + ImmutableMap.of( + RewriteJobOrder.FILES_DESC, + ImmutableList.of(FILE_1.partition(), FILE_4.partition(), FILE_6.partition()), + RewriteJobOrder.FILES_ASC, + ImmutableList.of(FILE_6.partition(), FILE_4.partition(), FILE_1.partition()), + RewriteJobOrder.BYTES_DESC, + ImmutableList.of(FILE_6.partition(), FILE_1.partition(), FILE_4.partition()), + RewriteJobOrder.BYTES_ASC, + ImmutableList.of(FILE_4.partition(), FILE_1.partition(), FILE_6.partition())); + + @TempDir private File tableDir = null; + private TestTables.TestTable table = null; + + @BeforeEach + public void setupTable() throws Exception { + this.table = TestTables.create(tableDir, "test", TestBase.SCHEMA, TestBase.SPEC, 3); + } + + @AfterEach + public void cleanupTables() { + TestTables.clearTables(); + } + + @Test + void testPartitionedTable() { + addFiles(); + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init(ImmutableMap.of(BinPackRewriteFileGroupPlanner.REWRITE_ALL, "true")); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + List<RewriteFileGroup> groups = Lists.newArrayList(plan.groups().iterator()); + assertThat(groups).hasSize(3); + assertThat(groups.stream().mapToInt(RewriteGroupBase::inputFileNum).sum()).isEqualTo(6); + } + + @Test + void testUnpartitionedTable() { + table.updateSpec().removeField("data_bucket").commit(); + table.refresh(); + table + .newAppend() + .appendFile(newDataFile("", 10)) + .appendFile(newDataFile("", 20)) + .appendFile(newDataFile("", 30)) + .commit(); + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init( + ImmutableMap.of( + BinPackRewriteFileGroupPlanner.MIN_INPUT_FILES, + "1", + BinPackRewriteFileGroupPlanner.MIN_FILE_SIZE_BYTES, + "30")); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + assertThat(plan.totalGroupCount()).isEqualTo(1); + assertThat(plan.groups().iterator().next().inputFileNum()).isEqualTo(2); + } + + @Test + void testEmptyTable() { + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init(REWRITE_ALL); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + assertThat(table.currentSnapshot()).as("Table must be empty").isNull(); + assertThat(plan.totalGroupCount()).isZero(); + } + + @ParameterizedTest + @EnumSource( + value = RewriteJobOrder.class, + names = {"FILES_DESC", "FILES_ASC", "BYTES_DESC", "BYTES_ASC"}) + void testJobOrder(RewriteJobOrder order) { + addFiles(); + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init( + ImmutableMap.of( + BinPackRewriteFileGroupPlanner.REWRITE_ALL, "true", REWRITE_JOB_ORDER, order.name())); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + List<RewriteFileGroup> groups = Lists.newArrayList(plan.groups().iterator()); + assertThat(groups.stream().map(group -> group.info().partition()).collect(Collectors.toList())) + .isEqualTo(EXPECTED.get(order)); + assertThat(plan.totalGroupCount()).isEqualTo(3); + EXPECTED.get(order).forEach(s -> assertThat(plan.groupsInPartition(s)).isEqualTo(1)); + } + + @Test + void testMaxGroupSize() { + addFiles(); + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init( + ImmutableMap.of( + BinPackRewriteFileGroupPlanner.REWRITE_ALL, + "true", + BinPackRewriteFileGroupPlanner.MAX_FILE_GROUP_SIZE_BYTES, + "10")); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + assertThat(plan.totalGroupCount()).isEqualTo(6); + assertThat(plan.groupsInPartition(FILE_1.partition())).isEqualTo(3); + assertThat(plan.groupsInPartition(FILE_4.partition())).isEqualTo(2); + assertThat(plan.groupsInPartition(FILE_6.partition())).isEqualTo(1); + } + + @Test + void testFilter() { + addFiles(); + BinPackRewriteFileGroupPlanner planner = + new BinPackRewriteFileGroupPlanner( + table, + Expressions.or( + Expressions.equal(Expressions.bucket("data", 16), 0), + Expressions.equal(Expressions.bucket("data", 16), 2))); + planner.init(REWRITE_ALL); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + List<RewriteFileGroup> groups = Lists.newArrayList(plan.groups().iterator()); + assertThat(plan.totalGroupCount()).isEqualTo(2); + assertThat(groups).hasSize(2); + assertThat(groups.stream().mapToLong(RewriteFileGroup::inputFileNum).sum()).isEqualTo(4); + } + + @Test + void testMaxOutputFileSize() { + addFiles(); + int targetFileSize = 10; + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init( + ImmutableMap.of( + BinPackRewriteFileGroupPlanner.REWRITE_ALL, + "true", + BinPackRewriteFileGroupPlanner.TARGET_FILE_SIZE_BYTES, + String.valueOf(targetFileSize))); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + assertThat(plan.groups().iterator().next().maxOutputFileSize()) + .isGreaterThan(targetFileSize) + .isLessThan((long) (targetFileSize * MAX_FILE_SIZE_DEFAULT_RATIO)); + } + + @Test + void testExpectedOutputFiles() { + addFiles(); + BinPackRewriteFileGroupPlanner planner = new BinPackRewriteFileGroupPlanner(table); + planner.init( + ImmutableMap.of( + BinPackRewriteFileGroupPlanner.REWRITE_ALL, + "true", + BinPackRewriteFileGroupPlanner.TARGET_FILE_SIZE_BYTES, + "21")); + + FileRewritePlan<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup> plan = planner.plan(); + + List<RewriteFileGroup> groups = Lists.newArrayList(plan.groups().iterator()); + assertThat(groups).hasSize(3); + for (RewriteFileGroup group : groups) { + if (FILE_1.partition().equals(group.info().partition())) { + assertThat(group.expectedOutputFiles()).isEqualTo(2); + } else if (FILE_4.partition().equals(group.info().partition())) { + assertThat(group.expectedOutputFiles()).isEqualTo(1); + } else if (FILE_6.partition().equals(group.info().partition())) { + assertThat(group.expectedOutputFiles()).isEqualTo(3); + } else { + throw new IllegalStateException("Unexpected partition: " + group.info().partition()); + } + } + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void testOutputSpec(boolean specific) { Review Comment: Done -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org