|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.paimon.operation; |
| 20 | + |
| 21 | +import org.apache.paimon.CoreOptions; |
| 22 | +import org.apache.paimon.catalog.FileSystemCatalog; |
| 23 | +import org.apache.paimon.catalog.Identifier; |
| 24 | +import org.apache.paimon.data.BinaryRow; |
| 25 | +import org.apache.paimon.data.BinaryRowWriter; |
| 26 | +import org.apache.paimon.data.GenericRow; |
| 27 | +import org.apache.paimon.fs.FileStatus; |
| 28 | +import org.apache.paimon.fs.Path; |
| 29 | +import org.apache.paimon.fs.local.LocalFileIO; |
| 30 | +import org.apache.paimon.io.DataFileMeta; |
| 31 | +import org.apache.paimon.schema.Schema; |
| 32 | +import org.apache.paimon.table.FileStoreTable; |
| 33 | +import org.apache.paimon.table.sink.TableCommitImpl; |
| 34 | +import org.apache.paimon.table.sink.TableWriteImpl; |
| 35 | +import org.apache.paimon.types.DataType; |
| 36 | +import org.apache.paimon.types.DataTypes; |
| 37 | +import org.apache.paimon.types.RowType; |
| 38 | + |
| 39 | +import org.junit.jupiter.api.io.TempDir; |
| 40 | +import org.junit.jupiter.params.ParameterizedTest; |
| 41 | +import org.junit.jupiter.params.provider.ValueSource; |
| 42 | + |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.Arrays; |
| 45 | +import java.util.Collections; |
| 46 | +import java.util.HashMap; |
| 47 | +import java.util.List; |
| 48 | +import java.util.Map; |
| 49 | +import java.util.UUID; |
| 50 | +import java.util.concurrent.ThreadLocalRandom; |
| 51 | +import java.util.function.Function; |
| 52 | +import java.util.stream.Collectors; |
| 53 | + |
| 54 | +import static org.assertj.core.api.Assertions.assertThat; |
| 55 | + |
| 56 | +/** Tests for {@link ListUnexistingFiles}. */ |
| 57 | +public class ListUnexistingFilesTest { |
| 58 | + |
| 59 | + @TempDir java.nio.file.Path tempDir; |
| 60 | + |
| 61 | + @ParameterizedTest |
| 62 | + @ValueSource(ints = {-1, 3}) |
| 63 | + public void testListFiles(int bucket) throws Exception { |
| 64 | + int numPartitions = 2; |
| 65 | + int numFiles = 10; |
| 66 | + int[] numDeletes = new int[numPartitions]; |
| 67 | + FileStoreTable table = |
| 68 | + prepareRandomlyDeletedTable( |
| 69 | + tempDir.toString(), "mydb", "t", bucket, numFiles, numDeletes); |
| 70 | + |
| 71 | + Function<Integer, BinaryRow> binaryRow = |
| 72 | + i -> { |
| 73 | + BinaryRow b = new BinaryRow(1); |
| 74 | + BinaryRowWriter writer = new BinaryRowWriter(b); |
| 75 | + writer.writeInt(0, i); |
| 76 | + writer.complete(); |
| 77 | + return b; |
| 78 | + }; |
| 79 | + |
| 80 | + ListUnexistingFiles operation = new ListUnexistingFiles(table); |
| 81 | + for (int i = 0; i < numPartitions; i++) { |
| 82 | + Map<Integer, Map<String, DataFileMeta>> result = operation.list(binaryRow.apply(i)); |
| 83 | + assertThat(result.values().stream().mapToInt(Map::size).sum()).isEqualTo(numDeletes[i]); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public static FileStoreTable prepareRandomlyDeletedTable( |
| 88 | + String warehouse, |
| 89 | + String databaseName, |
| 90 | + String tableName, |
| 91 | + int bucket, |
| 92 | + int numFiles, |
| 93 | + int[] numDeletes) |
| 94 | + throws Exception { |
| 95 | + RowType rowType = |
| 96 | + RowType.of( |
| 97 | + new DataType[] {DataTypes.INT(), DataTypes.INT(), DataTypes.BIGINT()}, |
| 98 | + new String[] {"pt", "id", "v"}); |
| 99 | + Map<String, String> options = new HashMap<>(); |
| 100 | + options.put(CoreOptions.BUCKET.key(), String.valueOf(bucket)); |
| 101 | + options.put(CoreOptions.WRITE_ONLY.key(), "true"); |
| 102 | + if (bucket > 0) { |
| 103 | + options.put(CoreOptions.BUCKET_KEY.key(), "id"); |
| 104 | + } |
| 105 | + FileStoreTable table = |
| 106 | + createPaimonTable( |
| 107 | + warehouse, |
| 108 | + databaseName, |
| 109 | + tableName, |
| 110 | + rowType, |
| 111 | + Collections.singletonList("pt"), |
| 112 | + options); |
| 113 | + |
| 114 | + String commitUser = UUID.randomUUID().toString(); |
| 115 | + TableWriteImpl<?> write = table.newWrite(commitUser); |
| 116 | + TableCommitImpl commit = table.newCommit(commitUser); |
| 117 | + |
| 118 | + ThreadLocalRandom random = ThreadLocalRandom.current(); |
| 119 | + int numPartitions = numDeletes.length; |
| 120 | + for (int i = 0; i < numPartitions; i++) { |
| 121 | + numDeletes[i] = random.nextInt(0, numFiles + 1); |
| 122 | + } |
| 123 | + |
| 124 | + int identifier = 0; |
| 125 | + for (int i = 0; i < numPartitions; i++) { |
| 126 | + for (int j = 0; j < numFiles; j++) { |
| 127 | + write.write(GenericRow.of(i, random.nextInt(), random.nextLong())); |
| 128 | + identifier++; |
| 129 | + commit.commit(identifier, write.prepareCommit(false, identifier)); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + write.close(); |
| 134 | + commit.close(); |
| 135 | + |
| 136 | + for (int i = 0; i < numPartitions; i++) { |
| 137 | + LocalFileIO fileIO = LocalFileIO.create(); |
| 138 | + List<Path> paths = new ArrayList<>(); |
| 139 | + for (int j = 0; j < Math.max(1, bucket); j++) { |
| 140 | + Path path = new Path(table.location(), "pt=" + i + "/bucket-" + j); |
| 141 | + paths.addAll( |
| 142 | + Arrays.stream(fileIO.listStatus(path)) |
| 143 | + .map(FileStatus::getPath) |
| 144 | + .collect(Collectors.toList())); |
| 145 | + } |
| 146 | + Collections.shuffle(paths); |
| 147 | + for (int j = 0; j < numDeletes[i]; j++) { |
| 148 | + fileIO.deleteQuietly(paths.get(j)); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return table; |
| 153 | + } |
| 154 | + |
| 155 | + private static FileStoreTable createPaimonTable( |
| 156 | + String warehouse, |
| 157 | + String databaseName, |
| 158 | + String tableName, |
| 159 | + RowType rowType, |
| 160 | + List<String> partitionKeys, |
| 161 | + Map<String, String> customOptions) |
| 162 | + throws Exception { |
| 163 | + LocalFileIO fileIO = LocalFileIO.create(); |
| 164 | + Path path = new Path(warehouse); |
| 165 | + |
| 166 | + Schema schema = |
| 167 | + new Schema( |
| 168 | + rowType.getFields(), |
| 169 | + partitionKeys, |
| 170 | + Collections.emptyList(), |
| 171 | + customOptions, |
| 172 | + ""); |
| 173 | + |
| 174 | + try (FileSystemCatalog paimonCatalog = new FileSystemCatalog(fileIO, path)) { |
| 175 | + paimonCatalog.createDatabase(databaseName, true); |
| 176 | + Identifier paimonIdentifier = Identifier.create(databaseName, tableName); |
| 177 | + paimonCatalog.createTable(paimonIdentifier, schema, false); |
| 178 | + return (FileStoreTable) paimonCatalog.getTable(paimonIdentifier); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments