|
| 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/manifest_reader.h" |
| 21 | + |
| 22 | +#include <arrow/filesystem/localfs.h> |
| 23 | +#include <gtest/gtest.h> |
| 24 | + |
| 25 | +#include "iceberg/arrow/arrow_fs_file_io.h" |
| 26 | +#include "iceberg/avro/avro_reader.h" |
| 27 | +#include "iceberg/avro/avro_schema_util_internal.h" |
| 28 | +#include "iceberg/manifest_entry.h" |
| 29 | +#include "iceberg/schema.h" |
| 30 | +#include "temp_file_test_base.h" |
| 31 | +#include "test_common.h" |
| 32 | + |
| 33 | +namespace iceberg { |
| 34 | + |
| 35 | +class ManifestReaderTest : public TempFileTestBase { |
| 36 | + protected: |
| 37 | + static void SetUpTestSuite() { avro::AvroReader::Register(); } |
| 38 | + |
| 39 | + void SetUp() override { |
| 40 | + TempFileTestBase::SetUp(); |
| 41 | + local_fs_ = std::make_shared<::arrow::fs::LocalFileSystem>(); |
| 42 | + file_io_ = std::make_shared<iceberg::arrow::ArrowFileSystemFileIO>(local_fs_); |
| 43 | + |
| 44 | + avro::RegisterLogicalTypes(); |
| 45 | + } |
| 46 | + |
| 47 | + std::vector<ManifestEntry> prepare_manifest_entries() { |
| 48 | + std::vector<ManifestEntry> manifest_entries; |
| 49 | + std::string test_dir_prefix = "/tmp/db/db/iceberg_test/data/"; |
| 50 | + std::vector<std::string> paths = { |
| 51 | + "order_ts_hour=2021-01-27-00/" |
| 52 | + "00000-2-d5ae78b7-4449-45ec-adb7-c0e9c0bdb714-0-00001.parquet", |
| 53 | + "order_ts_hour=2024-01-27-00/" |
| 54 | + "00000-2-d5ae78b7-4449-45ec-adb7-c0e9c0bdb714-0-00002.parquet", |
| 55 | + "order_ts_hour=2023-01-26-00/" |
| 56 | + "00000-2-d5ae78b7-4449-45ec-adb7-c0e9c0bdb714-0-00003.parquet", |
| 57 | + "order_ts_hour=2021-01-26-00/" |
| 58 | + "00000-2-d5ae78b7-4449-45ec-adb7-c0e9c0bdb714-0-00004.parquet"}; |
| 59 | + std::vector<int64_t> partitions = {447696, 473976, 465192, 447672}; |
| 60 | + std::vector<std::map<int32_t, std::vector<uint8_t>>> bounds = { |
| 61 | + {{1, {0xd2, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, |
| 62 | + {2, {'.', 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, |
| 63 | + {3, {0x12, 0xe2}}, |
| 64 | + {4, {0xc0, 'y', 0xe7, 0x98, 0xd6, 0xb9, 0x05, 0x00}}}, |
| 65 | + {{1, {0xd2, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, |
| 66 | + {2, {'.', 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, |
| 67 | + {3, {0x12, 0xe3}}, |
| 68 | + {4, {0xc0, 0x19, '#', '=', 0xe2, 0x0f, 0x06, 0x00}}}, |
| 69 | + {{1, {'{', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, |
| 70 | + {2, {0xc8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, |
| 71 | + {3, {0x0e, '"'}}, |
| 72 | + {4, {0xc0, 0xd9, '7', 0x93, 0x1f, 0xf3, 0x05, 0x00}}}, |
| 73 | + {{1, {'{', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, |
| 74 | + {2, {0xc8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, |
| 75 | + {3, {0x0e, '!'}}, |
| 76 | + {4, {0xc0, 0x19, 0x10, '{', 0xc2, 0xb9, 0x05, 0x00}}}, |
| 77 | + }; |
| 78 | + for (int i = 0; i < 4; ++i) { |
| 79 | + ManifestEntry entry; |
| 80 | + entry.status = ManifestStatus::kAdded; |
| 81 | + entry.snapshot_id = 6387266376565973956; |
| 82 | + entry.data_file = std::make_shared<DataFile>(); |
| 83 | + entry.data_file->file_path = test_dir_prefix + paths[i]; |
| 84 | + entry.data_file->file_format = FileFormatType::kParquet; |
| 85 | + entry.data_file->partition.emplace_back(Literal::Int(partitions[i])); |
| 86 | + entry.data_file->record_count = 1; |
| 87 | + entry.data_file->file_size_in_bytes = 1375; |
| 88 | + entry.data_file->column_sizes = {{1, 49}, {2, 49}, {3, 49}, {4, 49}}; |
| 89 | + entry.data_file->value_counts = {{1, 1}, {2, 1}, {3, 1}, {4, 1}}; |
| 90 | + entry.data_file->null_value_counts = {{1, 0}, {2, 0}, {3, 0}, {4, 0}}; |
| 91 | + entry.data_file->split_offsets = {4}; |
| 92 | + entry.data_file->sort_order_id = 0; |
| 93 | + entry.data_file->upper_bounds = bounds[i]; |
| 94 | + entry.data_file->lower_bounds = bounds[i]; |
| 95 | + manifest_entries.emplace_back(entry); |
| 96 | + } |
| 97 | + return manifest_entries; |
| 98 | + } |
| 99 | + |
| 100 | + std::shared_ptr<::arrow::fs::LocalFileSystem> local_fs_; |
| 101 | + std::shared_ptr<FileIO> file_io_; |
| 102 | +}; |
| 103 | + |
| 104 | +TEST_F(ManifestReaderTest, BasicTest) { |
| 105 | + iceberg::SchemaField partition_field(1000, "order_ts_hour", iceberg::int32(), true); |
| 106 | + auto partition_schema = |
| 107 | + std::make_shared<Schema>(std::vector<SchemaField>({partition_field})); |
| 108 | + std::string path = GetResourcePath("56357cd7-391f-4df8-aa24-e7e667da8870-m4.avro"); |
| 109 | + auto manifest_reader_result = |
| 110 | + ManifestReader::MakeReader(path, file_io_, partition_schema); |
| 111 | + ASSERT_EQ(manifest_reader_result.has_value(), true) |
| 112 | + << manifest_reader_result.error().message; |
| 113 | + auto manifest_reader = std::move(manifest_reader_result.value()); |
| 114 | + auto read_result = manifest_reader->Entries(); |
| 115 | + ASSERT_EQ(read_result.has_value(), true) << read_result.error().message; |
| 116 | + |
| 117 | + auto expected_entries = prepare_manifest_entries(); |
| 118 | + ASSERT_EQ(read_result.value(), expected_entries); |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace iceberg |
0 commit comments