|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iceberg.actions; |
| 21 | + |
| 22 | + |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import java.util.stream.IntStream; |
| 26 | +import org.apache.iceberg.AssertHelpers; |
| 27 | +import org.apache.iceberg.DataFile; |
| 28 | +import org.apache.iceberg.FileScanTask; |
| 29 | +import org.apache.iceberg.MockFileScanTask; |
| 30 | +import org.apache.iceberg.Schema; |
| 31 | +import org.apache.iceberg.SortOrder; |
| 32 | +import org.apache.iceberg.Table; |
| 33 | +import org.apache.iceberg.TableTestBase; |
| 34 | +import org.apache.iceberg.exceptions.ValidationException; |
| 35 | +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; |
| 36 | +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; |
| 37 | +import org.apache.iceberg.types.Types; |
| 38 | +import org.junit.Assert; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.runner.RunWith; |
| 41 | +import org.junit.runners.Parameterized; |
| 42 | + |
| 43 | +@RunWith(Parameterized.class) |
| 44 | +public class TestSortStrategy extends TableTestBase { |
| 45 | + |
| 46 | + @Parameterized.Parameters(name = "formatVersion = {0}") |
| 47 | + public static Object[] parameters() { |
| 48 | + return new Object[] {2}; // We don't actually use the format version since everything is mock |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void setupTable() throws Exception { |
| 53 | + super.setupTable(); |
| 54 | + table.replaceSortOrder().asc("data").commit(); |
| 55 | + } |
| 56 | + |
| 57 | + private static final long MB = 1024 * 1024; |
| 58 | + |
| 59 | + public TestSortStrategy(int formatVersion) { |
| 60 | + super(formatVersion); |
| 61 | + } |
| 62 | + |
| 63 | + class TestSortStrategyImpl extends SortStrategy { |
| 64 | + |
| 65 | + @Override |
| 66 | + public Table table() { |
| 67 | + return table; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public List<DataFile> rewriteFiles(List<FileScanTask> filesToRewrite) { |
| 72 | + throw new UnsupportedOperationException(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private SortStrategy defaultSort() { |
| 77 | + return (SortStrategy) new TestSortStrategyImpl().options(Collections.emptyMap()); |
| 78 | + } |
| 79 | + |
| 80 | + private List<FileScanTask> tasksForSortOrder(int sortOrderId, int... fileSizesMB) { |
| 81 | + ImmutableList.Builder<FileScanTask> files = ImmutableList.builder(); |
| 82 | + IntStream.of(fileSizesMB).forEach(length -> files.add(MockFileScanTask.mockTask(length * MB, sortOrderId))); |
| 83 | + return files.build(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testInvalidSortOrder() { |
| 88 | + AssertHelpers.assertThrows("Should not allow an unsorted Sort order", IllegalArgumentException.class, |
| 89 | + () -> defaultSort().sortOrder(SortOrder.unsorted()).options(Collections.emptyMap())); |
| 90 | + |
| 91 | + AssertHelpers.assertThrows("Should not allow a Sort order with bad columns", ValidationException.class, |
| 92 | + () -> { |
| 93 | + Schema badSchema = new Schema( |
| 94 | + ImmutableList.of(Types.NestedField.required(0, "nonexistant", Types.IntegerType.get()))); |
| 95 | + |
| 96 | + defaultSort() |
| 97 | + .sortOrder(SortOrder.builderFor(badSchema).asc("nonexistant").build()) |
| 98 | + .options(Collections.emptyMap()); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testSelectWrongSortOrder() { |
| 104 | + List<FileScanTask> expected = tasksForSortOrder(-1, 500, 500, 500, 500); |
| 105 | + RewriteStrategy strategy = defaultSort().options(Collections.emptyMap()); |
| 106 | + List<FileScanTask> actual = ImmutableList.copyOf(strategy.selectFilesToRewrite(expected)); |
| 107 | + |
| 108 | + Assert.assertEquals("Should mark all files for rewrite if they have the wrong sort order", |
| 109 | + expected, actual); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testSelectCorrectSortOrder() { |
| 114 | + List<FileScanTask> fileScanTasks = tasksForSortOrder(table.sortOrder().orderId(), 500, 500, 500, 500); |
| 115 | + RewriteStrategy strategy = defaultSort().options(Collections.emptyMap()); |
| 116 | + List<FileScanTask> actual = ImmutableList.copyOf(strategy.selectFilesToRewrite(fileScanTasks)); |
| 117 | + |
| 118 | + Assert.assertEquals("Should mark no files for rewrite if they have a good size and the right sort order", |
| 119 | + Collections.emptyList(), actual); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testSelectMixedOrderMixedSize() { |
| 124 | + List<FileScanTask> expected = ImmutableList.<FileScanTask>builder() |
| 125 | + .addAll(tasksForSortOrder(-1, 500, 500, 500, 500)) |
| 126 | + .addAll(tasksForSortOrder(table.sortOrder().orderId(), 10, 10, 2000, 10)) |
| 127 | + .build(); |
| 128 | + |
| 129 | + List<FileScanTask> fileScanTasks = ImmutableList.<FileScanTask>builder() |
| 130 | + .addAll(expected) |
| 131 | + .addAll(tasksForSortOrder(table.sortOrder().orderId(), 500, 490, 520)) |
| 132 | + .build(); |
| 133 | + |
| 134 | + RewriteStrategy strategy = defaultSort().options(Collections.emptyMap()); |
| 135 | + List<FileScanTask> actual = ImmutableList.copyOf(strategy.selectFilesToRewrite(fileScanTasks)); |
| 136 | + |
| 137 | + Assert.assertEquals("Should mark files for rewrite with invalid size and bad sort order", |
| 138 | + expected, actual); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testSelectAll() { |
| 143 | + List<FileScanTask> invalid = ImmutableList.<FileScanTask>builder() |
| 144 | + .addAll(tasksForSortOrder(-1, 500, 500, 500, 500)) |
| 145 | + .addAll(tasksForSortOrder(table.sortOrder().orderId(), 10, 10, 2000, 10)) |
| 146 | + .build(); |
| 147 | + |
| 148 | + List<FileScanTask> expected = ImmutableList.<FileScanTask>builder() |
| 149 | + .addAll(invalid) |
| 150 | + .addAll(tasksForSortOrder(table.sortOrder().orderId(), 500, 490, 520)) |
| 151 | + .build(); |
| 152 | + |
| 153 | + RewriteStrategy strategy = defaultSort().options(ImmutableMap.of(SortStrategy.REWRITE_ALL, "true")); |
| 154 | + List<FileScanTask> actual = ImmutableList.copyOf(strategy.selectFilesToRewrite(expected)); |
| 155 | + |
| 156 | + Assert.assertEquals("Should mark all files for rewrite", |
| 157 | + expected, actual); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testUseSizeOptions() { |
| 162 | + List<FileScanTask> expected = ImmutableList.<FileScanTask>builder() |
| 163 | + .addAll(tasksForSortOrder(table.sortOrder().orderId(), 498, 551)) |
| 164 | + .build(); |
| 165 | + |
| 166 | + List<FileScanTask> fileScanTasks = ImmutableList.<FileScanTask>builder() |
| 167 | + .addAll(expected) |
| 168 | + .addAll(tasksForSortOrder(table.sortOrder().orderId(), 500, 500)) |
| 169 | + .build(); |
| 170 | + |
| 171 | + RewriteStrategy strategy = defaultSort().options(ImmutableMap.of( |
| 172 | + SortStrategy.MAX_FILE_SIZE_BYTES, Long.toString(550 * MB), |
| 173 | + SortStrategy.MIN_FILE_SIZE_BYTES, Long.toString(499 * MB))); |
| 174 | + |
| 175 | + List<FileScanTask> actual = ImmutableList.copyOf(strategy.selectFilesToRewrite(fileScanTasks)); |
| 176 | + |
| 177 | + Assert.assertEquals("Should mark files for rewrite with adjusted min and max size", |
| 178 | + expected, actual); |
| 179 | + } |
| 180 | + |
| 181 | + |
| 182 | +} |
| 183 | + |
0 commit comments