|
| 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 | +#include "iceberg/util/data_file_set.h" |
| 21 | + |
| 22 | +#include <gtest/gtest.h> |
| 23 | + |
| 24 | +#include "iceberg/file_format.h" |
| 25 | +#include "iceberg/manifest/manifest_entry.h" |
| 26 | +#include "iceberg/row/partition_values.h" |
| 27 | + |
| 28 | +namespace iceberg { |
| 29 | + |
| 30 | +class DataFileSetTest : public ::testing::Test { |
| 31 | + protected: |
| 32 | + std::shared_ptr<DataFile> CreateDataFile(const std::string& path, int64_t size = 100) { |
| 33 | + auto file = std::make_shared<DataFile>(); |
| 34 | + file->file_path = path; |
| 35 | + file->file_format = FileFormatType::kParquet; |
| 36 | + file->file_size_in_bytes = size; |
| 37 | + file->record_count = 10; |
| 38 | + file->content = DataFile::Content::kData; |
| 39 | + return file; |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +TEST_F(DataFileSetTest, EmptySet) { |
| 44 | + DataFileSet set; |
| 45 | + EXPECT_TRUE(set.empty()); |
| 46 | + EXPECT_EQ(set.size(), 0); |
| 47 | + EXPECT_EQ(set.begin(), set.end()); |
| 48 | +} |
| 49 | + |
| 50 | +TEST_F(DataFileSetTest, InsertSingleFile) { |
| 51 | + DataFileSet set; |
| 52 | + auto file = CreateDataFile("/path/to/file.parquet"); |
| 53 | + |
| 54 | + auto [iter, inserted] = set.insert(file); |
| 55 | + EXPECT_TRUE(inserted); |
| 56 | + EXPECT_EQ(*iter, file); |
| 57 | + EXPECT_FALSE(set.empty()); |
| 58 | + EXPECT_EQ(set.size(), 1); |
| 59 | +} |
| 60 | + |
| 61 | +TEST_F(DataFileSetTest, InsertDuplicateFile) { |
| 62 | + DataFileSet set; |
| 63 | + auto file1 = CreateDataFile("/path/to/file.parquet"); |
| 64 | + auto file2 = CreateDataFile("/path/to/file.parquet"); // Same path |
| 65 | + |
| 66 | + auto [iter1, inserted1] = set.insert(file1); |
| 67 | + EXPECT_TRUE(inserted1); |
| 68 | + |
| 69 | + auto [iter2, inserted2] = set.insert(file2); |
| 70 | + EXPECT_FALSE(inserted2); |
| 71 | + EXPECT_EQ(iter1, iter2); // Should point to the same element |
| 72 | + EXPECT_EQ(set.size(), 1); // Should still be size 1 |
| 73 | +} |
| 74 | + |
| 75 | +TEST_F(DataFileSetTest, InsertDifferentFiles) { |
| 76 | + DataFileSet set; |
| 77 | + auto file1 = CreateDataFile("/path/to/file1.parquet"); |
| 78 | + auto file2 = CreateDataFile("/path/to/file2.parquet"); |
| 79 | + auto file3 = CreateDataFile("/path/to/file3.parquet"); |
| 80 | + |
| 81 | + set.insert(file1); |
| 82 | + set.insert(file2); |
| 83 | + set.insert(file3); |
| 84 | + |
| 85 | + EXPECT_EQ(set.size(), 3); |
| 86 | + EXPECT_FALSE(set.empty()); |
| 87 | +} |
| 88 | + |
| 89 | +TEST_F(DataFileSetTest, InsertionOrderPreserved) { |
| 90 | + DataFileSet set; |
| 91 | + auto file1 = CreateDataFile("/path/to/file1.parquet"); |
| 92 | + auto file2 = CreateDataFile("/path/to/file2.parquet"); |
| 93 | + auto file3 = CreateDataFile("/path/to/file3.parquet"); |
| 94 | + |
| 95 | + set.insert(file1); |
| 96 | + set.insert(file2); |
| 97 | + set.insert(file3); |
| 98 | + |
| 99 | + // Iterate and verify order |
| 100 | + std::vector<std::string> paths; |
| 101 | + for (const auto& file : set) { |
| 102 | + paths.push_back(file->file_path); |
| 103 | + } |
| 104 | + |
| 105 | + EXPECT_EQ(paths.size(), 3); |
| 106 | + EXPECT_EQ(paths[0], "/path/to/file1.parquet"); |
| 107 | + EXPECT_EQ(paths[1], "/path/to/file2.parquet"); |
| 108 | + EXPECT_EQ(paths[2], "/path/to/file3.parquet"); |
| 109 | +} |
| 110 | + |
| 111 | +TEST_F(DataFileSetTest, InsertDuplicatePreservesOrder) { |
| 112 | + DataFileSet set; |
| 113 | + auto file1 = CreateDataFile("/path/to/file1.parquet"); |
| 114 | + auto file2 = CreateDataFile("/path/to/file2.parquet"); |
| 115 | + auto file3 = CreateDataFile("/path/to/file1.parquet"); // Duplicate of file1 |
| 116 | + |
| 117 | + set.insert(file1); |
| 118 | + set.insert(file2); |
| 119 | + set.insert(file3); // Should not insert, but order should be preserved |
| 120 | + |
| 121 | + EXPECT_EQ(set.size(), 2); |
| 122 | + |
| 123 | + std::vector<std::string> paths; |
| 124 | + for (const auto& file : set) { |
| 125 | + paths.push_back(file->file_path); |
| 126 | + } |
| 127 | + |
| 128 | + EXPECT_EQ(paths[0], "/path/to/file1.parquet"); |
| 129 | + EXPECT_EQ(paths[1], "/path/to/file2.parquet"); |
| 130 | +} |
| 131 | + |
| 132 | +TEST_F(DataFileSetTest, InsertNullFile) { |
| 133 | + DataFileSet set; |
| 134 | + std::shared_ptr<DataFile> null_file = nullptr; |
| 135 | + |
| 136 | + auto [iter, inserted] = set.insert(null_file); |
| 137 | + EXPECT_FALSE(inserted); |
| 138 | + EXPECT_EQ(iter, set.end()); |
| 139 | + EXPECT_TRUE(set.empty()); |
| 140 | + EXPECT_EQ(set.size(), 0); |
| 141 | +} |
| 142 | + |
| 143 | +TEST_F(DataFileSetTest, InsertMoveSemantics) { |
| 144 | + DataFileSet set; |
| 145 | + auto file1 = CreateDataFile("/path/to/file1.parquet"); |
| 146 | + auto file2 = CreateDataFile("/path/to/file2.parquet"); |
| 147 | + |
| 148 | + // Insert using move |
| 149 | + auto [iter1, inserted1] = set.insert(std::move(file1)); |
| 150 | + EXPECT_TRUE(inserted1); |
| 151 | + EXPECT_EQ(file1, nullptr); // Should be moved |
| 152 | + |
| 153 | + // Insert using copy |
| 154 | + auto [iter2, inserted2] = set.insert(file2); |
| 155 | + EXPECT_TRUE(inserted2); |
| 156 | + EXPECT_NE(file2, nullptr); // Should still be valid |
| 157 | + |
| 158 | + EXPECT_EQ(set.size(), 2); |
| 159 | +} |
| 160 | + |
| 161 | +TEST_F(DataFileSetTest, Clear) { |
| 162 | + DataFileSet set; |
| 163 | + set.insert(CreateDataFile("/path/to/file1.parquet")); |
| 164 | + set.insert(CreateDataFile("/path/to/file2.parquet")); |
| 165 | + |
| 166 | + EXPECT_EQ(set.size(), 2); |
| 167 | + set.clear(); |
| 168 | + EXPECT_TRUE(set.empty()); |
| 169 | + EXPECT_EQ(set.size(), 0); |
| 170 | + EXPECT_EQ(set.begin(), set.end()); |
| 171 | +} |
| 172 | + |
| 173 | +TEST_F(DataFileSetTest, IteratorOperations) { |
| 174 | + DataFileSet set; |
| 175 | + auto file1 = CreateDataFile("/path/to/file1.parquet"); |
| 176 | + auto file2 = CreateDataFile("/path/to/file2.parquet"); |
| 177 | + auto file3 = CreateDataFile("/path/to/file3.parquet"); |
| 178 | + |
| 179 | + set.insert(file1); |
| 180 | + set.insert(file2); |
| 181 | + set.insert(file3); |
| 182 | + |
| 183 | + // Test const iterators |
| 184 | + const auto& const_set = set; |
| 185 | + EXPECT_NE(const_set.begin(), const_set.end()); |
| 186 | + EXPECT_NE(const_set.cbegin(), const_set.cend()); |
| 187 | + |
| 188 | + // Test iterator increment |
| 189 | + auto it = set.begin(); |
| 190 | + EXPECT_EQ((*it)->file_path, "/path/to/file1.parquet"); |
| 191 | + ++it; |
| 192 | + EXPECT_EQ((*it)->file_path, "/path/to/file2.parquet"); |
| 193 | + ++it; |
| 194 | + EXPECT_EQ((*it)->file_path, "/path/to/file3.parquet"); |
| 195 | + ++it; |
| 196 | + EXPECT_EQ(it, set.end()); |
| 197 | +} |
| 198 | + |
| 199 | +TEST_F(DataFileSetTest, RangeBasedForLoop) { |
| 200 | + DataFileSet set; |
| 201 | + set.insert(CreateDataFile("/path/to/file1.parquet")); |
| 202 | + set.insert(CreateDataFile("/path/to/file2.parquet")); |
| 203 | + set.insert(CreateDataFile("/path/to/file3.parquet")); |
| 204 | + |
| 205 | + int count = 0; |
| 206 | + for (const auto& file : set) { |
| 207 | + EXPECT_NE(file, nullptr); |
| 208 | + ++count; |
| 209 | + } |
| 210 | + EXPECT_EQ(count, 3); |
| 211 | +} |
| 212 | + |
| 213 | +TEST_F(DataFileSetTest, CaseSensitivePaths) { |
| 214 | + DataFileSet set; |
| 215 | + auto file1 = CreateDataFile("/path/to/file.parquet"); |
| 216 | + auto file2 = CreateDataFile("/path/to/FILE.parquet"); // Different case |
| 217 | + |
| 218 | + set.insert(file1); |
| 219 | + set.insert(file2); |
| 220 | + |
| 221 | + // Should be treated as different files |
| 222 | + EXPECT_EQ(set.size(), 2); |
| 223 | +} |
| 224 | + |
| 225 | +TEST_F(DataFileSetTest, MultipleInsertsSameFile) { |
| 226 | + DataFileSet set; |
| 227 | + auto file = CreateDataFile("/path/to/file.parquet"); |
| 228 | + |
| 229 | + // Insert the same file multiple times |
| 230 | + set.insert(file); |
| 231 | + set.insert(file); |
| 232 | + set.insert(file); |
| 233 | + |
| 234 | + EXPECT_EQ(set.size(), 1); |
| 235 | +} |
| 236 | + |
| 237 | +} // namespace iceberg |
0 commit comments