|
| 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 | +/// \file metadata_table_test_base.h |
| 21 | +/// Shared test base for all metadata table tests. |
| 22 | +/// |
| 23 | +/// Provides common helpers (FinishAndImport, MakeTestSnapshots, |
| 24 | +/// MakeTableWithSnapshots) and the MockFileIO + MockCatalog fixture that |
| 25 | +/// every metadata table test needs. |
| 26 | + |
| 27 | +#pragma once |
| 28 | + |
| 29 | +#include <memory> |
| 30 | +#include <unordered_map> |
| 31 | +#include <utility> |
| 32 | +#include <vector> |
| 33 | + |
| 34 | +#include <arrow/array.h> |
| 35 | +#include <arrow/c/bridge.h> |
| 36 | +#include <arrow/record_batch.h> |
| 37 | +#include <gmock/gmock.h> |
| 38 | +#include <gtest/gtest.h> |
| 39 | + |
| 40 | +#include "iceberg/schema.h" |
| 41 | +#include "iceberg/schema_field.h" |
| 42 | +#include "iceberg/schema_internal.h" |
| 43 | +#include "iceberg/snapshot.h" |
| 44 | +#include "iceberg/table.h" |
| 45 | +#include "iceberg/table_identifier.h" |
| 46 | +#include "iceberg/table_metadata.h" |
| 47 | +#include "iceberg/test/matchers.h" |
| 48 | +#include "iceberg/test/mock_catalog.h" |
| 49 | +#include "iceberg/test/mock_io.h" |
| 50 | +#include "iceberg/type.h" |
| 51 | +#include "iceberg/util/timepoint.h" |
| 52 | + |
| 53 | +namespace iceberg { |
| 54 | + |
| 55 | +/// \brief Base class for all metadata table tests. |
| 56 | +/// |
| 57 | +/// Provides MockFileIO and MockCatalog instances plus helpers shared across |
| 58 | +/// metadata table tests (SnapshotsTable, HistoryTable, RefsTable, ...). |
| 59 | +class MetadataTableTestBase : public ::testing::Test { |
| 60 | + protected: |
| 61 | + void SetUp() override { |
| 62 | + io_ = std::make_shared<MockFileIO>(); |
| 63 | + catalog_ = std::make_shared<MockCatalog>(); |
| 64 | + } |
| 65 | + |
| 66 | + /// \brief Import a Scan()-produced ArrowArray into an Arrow RecordBatch. |
| 67 | + static std::shared_ptr<::arrow::RecordBatch> FinishAndImport(ArrowArray array, |
| 68 | + const Schema& schema) { |
| 69 | + ArrowSchema c_schema; |
| 70 | + EXPECT_THAT(ToArrowSchema(schema, &c_schema), IsOk()); |
| 71 | + auto arrow_schema = ::arrow::ImportSchema(&c_schema).ValueOrDie(); |
| 72 | + |
| 73 | + // ImportRecordBatch takes ownership of the array and releases it. |
| 74 | + return ::arrow::ImportRecordBatch(&array, arrow_schema).ValueOrDie(); |
| 75 | + } |
| 76 | + |
| 77 | + /// \brief Create two snapshots matching the Java TestDataTaskParser test data. |
| 78 | + /// |
| 79 | + /// Snapshot 1: id=1, no parent, timestamp=1234567890000, operation="append" |
| 80 | + /// Snapshot 2: id=2, parent=1, timestamp=9876543210000, operation="append" |
| 81 | + static std::pair<std::shared_ptr<Snapshot>, std::shared_ptr<Snapshot>> |
| 82 | + MakeTestSnapshots() { |
| 83 | + std::unordered_map<std::string, std::string> summary1{ |
| 84 | + {"added-data-files", "1"}, {"added-records", "1"}, |
| 85 | + {"added-files-size", "10"}, {"changed-partition-count", "1"}, |
| 86 | + {"total-records", "1"}, {"total-files-size", "10"}, |
| 87 | + {"total-data-files", "1"}, {"total-delete-files", "0"}, |
| 88 | + {"total-position-deletes", "0"}, {"total-equality-deletes", "0"}, |
| 89 | + {"operation", "append"}, |
| 90 | + }; |
| 91 | + |
| 92 | + std::unordered_map<std::string, std::string> summary2{ |
| 93 | + {"added-data-files", "1"}, {"added-records", "1"}, |
| 94 | + {"added-files-size", "10"}, {"changed-partition-count", "1"}, |
| 95 | + {"total-records", "2"}, {"total-files-size", "20"}, |
| 96 | + {"total-data-files", "2"}, {"total-delete-files", "0"}, |
| 97 | + {"total-position-deletes", "0"}, {"total-equality-deletes", "0"}, |
| 98 | + {"operation", "append"}, |
| 99 | + }; |
| 100 | + |
| 101 | + auto snap1 = std::make_shared<Snapshot>(Snapshot{ |
| 102 | + .snapshot_id = 1, |
| 103 | + .parent_snapshot_id = std::nullopt, |
| 104 | + .sequence_number = 1, |
| 105 | + .timestamp_ms = TimePointMsFromUnixMs(1234567890000), |
| 106 | + .manifest_list = "file:/tmp/manifest1.avro", |
| 107 | + .summary = std::move(summary1), |
| 108 | + .schema_id = 1, |
| 109 | + }); |
| 110 | + |
| 111 | + auto snap2 = std::make_shared<Snapshot>(Snapshot{ |
| 112 | + .snapshot_id = 2, |
| 113 | + .parent_snapshot_id = 1, |
| 114 | + .sequence_number = 2, |
| 115 | + .timestamp_ms = TimePointMsFromUnixMs(9876543210000), |
| 116 | + .manifest_list = "file:/tmp/manifest2.avro", |
| 117 | + .summary = std::move(summary2), |
| 118 | + .schema_id = 1, |
| 119 | + }); |
| 120 | + |
| 121 | + return {snap1, snap2}; |
| 122 | + } |
| 123 | + |
| 124 | + /// \brief Create a Table with the given snapshots. |
| 125 | + Result<std::shared_ptr<Table>> MakeTableWithSnapshots( |
| 126 | + std::vector<std::shared_ptr<Snapshot>> snapshots, int64_t current_snapshot_id) { |
| 127 | + auto schema = std::make_shared<Schema>( |
| 128 | + std::vector<SchemaField>{SchemaField::MakeRequired(1, "id", int64()), |
| 129 | + SchemaField::MakeOptional(2, "name", string())}, |
| 130 | + 1); |
| 131 | + auto metadata = std::make_shared<TableMetadata>(TableMetadata{ |
| 132 | + .format_version = 2, |
| 133 | + .schemas = {schema}, |
| 134 | + .current_schema_id = 1, |
| 135 | + .current_snapshot_id = current_snapshot_id, |
| 136 | + .snapshots = std::move(snapshots), |
| 137 | + }); |
| 138 | + |
| 139 | + TableIdentifier ident{.ns = Namespace{.levels = {"db"}}, .name = "test_table"}; |
| 140 | + return Table::Make(ident, metadata, "s3://bucket/meta.json", io_, catalog_); |
| 141 | + } |
| 142 | + |
| 143 | + std::shared_ptr<MockFileIO> io_; |
| 144 | + std::shared_ptr<MockCatalog> catalog_; |
| 145 | +}; |
| 146 | + |
| 147 | +} // namespace iceberg |
0 commit comments