|
| 1 | +/* |
| 2 | + * Copyright 2026-present Alibaba Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "paimon/core/table/source/snapshot/snapshot_reader.h" |
| 18 | + |
| 19 | +#include <memory> |
| 20 | +#include <optional> |
| 21 | +#include <string> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "arrow/api.h" |
| 25 | +#include "gtest/gtest.h" |
| 26 | +#include "paimon/core/deletionvectors/deletion_vectors_index_file.h" |
| 27 | +#include "paimon/core/index/index_file_handler.h" |
| 28 | +#include "paimon/core/index/index_file_meta.h" |
| 29 | +#include "paimon/core/io/data_file_meta.h" |
| 30 | +#include "paimon/core/utils/file_store_path_factory.h" |
| 31 | +#include "paimon/core/utils/index_file_path_factories.h" |
| 32 | +#include "paimon/fs/local/local_file_system.h" |
| 33 | +#include "paimon/memory/memory_pool.h" |
| 34 | +#include "paimon/testing/utils/testharness.h" |
| 35 | + |
| 36 | +namespace paimon::test { |
| 37 | +class SnapshotReaderTest : public testing::Test { |
| 38 | + protected: |
| 39 | + void SetUp() override { |
| 40 | + pool_ = GetDefaultPool(); |
| 41 | + dir_ = UniqueTestDirectory::Create(); |
| 42 | + ASSERT_TRUE(dir_ != nullptr); |
| 43 | + } |
| 44 | + |
| 45 | + std::shared_ptr<DataFileMeta> CreateDataFileMeta(const std::string& file_name) const { |
| 46 | + return std::make_shared<DataFileMeta>( |
| 47 | + file_name, /*file_size=*/100, /*row_count=*/10, DataFileMeta::EmptyMinKey(), |
| 48 | + DataFileMeta::EmptyMaxKey(), SimpleStats::EmptyStats(), SimpleStats::EmptyStats(), |
| 49 | + /*min_sequence_number=*/0, /*max_sequence_number=*/0, /*schema_id=*/0, |
| 50 | + DataFileMeta::DUMMY_LEVEL, std::vector<std::optional<std::string>>{}, Timestamp(0, 0), |
| 51 | + std::nullopt, nullptr, FileSource::Append(), std::nullopt, std::nullopt, std::nullopt, |
| 52 | + std::nullopt); |
| 53 | + } |
| 54 | + |
| 55 | + std::shared_ptr<IndexFileMeta> CreateIndexFileMeta(const std::string& index_file_name, |
| 56 | + const std::string& data_file_name, |
| 57 | + int64_t offset, int64_t length, |
| 58 | + std::optional<int64_t> cardinality) const { |
| 59 | + LinkedHashMap<std::string, DeletionVectorMeta> dv_ranges; |
| 60 | + dv_ranges.insert(data_file_name, |
| 61 | + DeletionVectorMeta(data_file_name, offset, length, cardinality)); |
| 62 | + return std::make_shared<IndexFileMeta>(DeletionVectorsIndexFile::DELETION_VECTORS_INDEX, |
| 63 | + index_file_name, /*file_size=*/100, /*row_count=*/10, |
| 64 | + dv_ranges, /*external_path=*/std::nullopt); |
| 65 | + } |
| 66 | + |
| 67 | + Result<std::unique_ptr<IndexFileHandler>> CreateIndexFileHandler() const { |
| 68 | + auto schema = arrow::schema({arrow::field("f0", arrow::int32())}); |
| 69 | + PAIMON_ASSIGN_OR_RAISE( |
| 70 | + std::shared_ptr<FileStorePathFactory> path_factory, |
| 71 | + FileStorePathFactory::Create( |
| 72 | + dir_->Str(), schema, /*partition_keys=*/{}, /*default_part_value=*/"", "orc", |
| 73 | + /*data_file_prefix=*/"data-", /*legacy_partition_name_enabled=*/true, |
| 74 | + /*external_paths=*/{}, /*global_index_external_path=*/std::nullopt, |
| 75 | + /*index_file_in_data_file_dir=*/false, pool_)); |
| 76 | + auto path_factories = std::make_shared<IndexFilePathFactories>(path_factory); |
| 77 | + return std::make_unique<IndexFileHandler>(std::make_shared<LocalFileSystem>(), |
| 78 | + std::unique_ptr<IndexManifestFile>(), |
| 79 | + path_factories, /*dv_bitmap64=*/false, pool_); |
| 80 | + } |
| 81 | + |
| 82 | + std::shared_ptr<MemoryPool> pool_; |
| 83 | + std::unique_ptr<UniqueTestDirectory> dir_; |
| 84 | +}; |
| 85 | + |
| 86 | +TEST_F(SnapshotReaderTest, GetDeletionFilesOverwritesDuplicateDataFileName) { |
| 87 | + ASSERT_OK_AND_ASSIGN(std::unique_ptr<IndexFileHandler> index_file_handler, |
| 88 | + CreateIndexFileHandler()); |
| 89 | + SnapshotReader snapshot_reader(/*scan=*/nullptr, /*path_factory=*/nullptr, |
| 90 | + /*split_generator=*/nullptr, std::move(index_file_handler)); |
| 91 | + |
| 92 | + const std::string data_file_name = "data-0.orc"; |
| 93 | + std::vector<std::shared_ptr<DataFileMeta>> data_files = {CreateDataFileMeta(data_file_name)}; |
| 94 | + std::vector<std::shared_ptr<IndexFileMeta>> index_file_metas = { |
| 95 | + CreateIndexFileMeta("index-first", data_file_name, /*offset=*/1, /*length=*/11, |
| 96 | + /*cardinality=*/3), |
| 97 | + CreateIndexFileMeta("index-second", data_file_name, /*offset=*/2, /*length=*/22, |
| 98 | + /*cardinality=*/4)}; |
| 99 | + |
| 100 | + ASSERT_OK_AND_ASSIGN(std::vector<std::optional<DeletionFile>> deletion_files, |
| 101 | + snapshot_reader.GetDeletionFiles(BinaryRow::EmptyRow(), /*bucket=*/0, |
| 102 | + data_files, index_file_metas)); |
| 103 | + |
| 104 | + ASSERT_EQ(deletion_files.size(), 1); |
| 105 | + ASSERT_TRUE(deletion_files[0].has_value()); |
| 106 | + EXPECT_EQ(deletion_files[0]->path, dir_->Str() + "/index/index-second"); |
| 107 | + EXPECT_EQ(deletion_files[0]->offset, 2); |
| 108 | + EXPECT_EQ(deletion_files[0]->length, 22); |
| 109 | + EXPECT_EQ(deletion_files[0]->cardinality, std::optional<int64_t>(4)); |
| 110 | +} |
| 111 | + |
| 112 | +} // namespace paimon::test |
0 commit comments