|
| 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/update/update_properties.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <string> |
| 24 | +#include <unordered_map> |
| 25 | + |
| 26 | +#include <gmock/gmock.h> |
| 27 | +#include <gtest/gtest.h> |
| 28 | + |
| 29 | +#include "iceberg/file_format.h" |
| 30 | +#include "iceberg/partition_spec.h" |
| 31 | +#include "iceberg/snapshot.h" |
| 32 | +#include "iceberg/sort_order.h" |
| 33 | +#include "iceberg/table.h" |
| 34 | +#include "iceberg/table_metadata.h" |
| 35 | +#include "iceberg/table_properties.h" |
| 36 | +#include "iceberg/test/matchers.h" |
| 37 | +#include "iceberg/test/mock_catalog.h" |
| 38 | + |
| 39 | +namespace iceberg { |
| 40 | + |
| 41 | +namespace { |
| 42 | + |
| 43 | +std::shared_ptr<TableMetadata> MakeBaseMetadata( |
| 44 | + std::unordered_map<std::string, std::string> properties) { |
| 45 | + auto metadata = std::make_shared<TableMetadata>(); |
| 46 | + metadata->format_version = 2; |
| 47 | + metadata->table_uuid = "test-uuid"; |
| 48 | + metadata->location = "s3://bucket/table"; |
| 49 | + metadata->last_sequence_number = TableMetadata::kInitialSequenceNumber; |
| 50 | + metadata->last_updated_ms = TimePointMs{}; |
| 51 | + metadata->last_column_id = 0; |
| 52 | + metadata->default_spec_id = PartitionSpec::kInitialSpecId; |
| 53 | + metadata->last_partition_id = 0; |
| 54 | + metadata->default_sort_order_id = SortOrder::kInitialSortOrderId; |
| 55 | + metadata->current_snapshot_id = Snapshot::kInvalidSnapshotId; |
| 56 | + metadata->next_row_id = TableMetadata::kInitialRowId; |
| 57 | + metadata->properties = std::move(properties); |
| 58 | + return metadata; |
| 59 | +} |
| 60 | + |
| 61 | +TableIdentifier MakeIdentifier() { return TableIdentifier{Namespace{{"ns"}}, "tbl"}; } |
| 62 | + |
| 63 | +} // namespace |
| 64 | + |
| 65 | +using ::testing::_; |
| 66 | +using ::testing::ByMove; |
| 67 | +using ::testing::Return; |
| 68 | + |
| 69 | +TEST(UpdatePropertiesTest, ApplyMergesUpdatesAndRemovals) { |
| 70 | + auto metadata = |
| 71 | + MakeBaseMetadata({{"foo", "bar"}, {"keep", "yes"}, {"format-version", "2"}}); |
| 72 | + Table table(MakeIdentifier(), metadata, "loc", /*io=*/nullptr, /*catalog=*/nullptr); |
| 73 | + |
| 74 | + auto updater = table.UpdateProperties(); |
| 75 | + updater->Set("foo", "baz").Remove("keep").DefaultFormat(FileFormatType::kOrc); |
| 76 | + |
| 77 | + auto applied = updater->Apply(); |
| 78 | + ASSERT_THAT(applied, IsOk()); |
| 79 | + |
| 80 | + const auto& props = *applied; |
| 81 | + EXPECT_EQ(props.at("foo"), "baz"); |
| 82 | + EXPECT_FALSE(props.contains("keep")); |
| 83 | + EXPECT_EQ(props.at(TableProperties::kDefaultFileFormat.key()), "orc"); |
| 84 | +} |
| 85 | + |
| 86 | +TEST(UpdatePropertiesTest, CommitUsesCatalogAndRefreshesTable) { |
| 87 | + auto catalog = std::make_shared<MockCatalog>(); |
| 88 | + auto base_metadata = MakeBaseMetadata({{"foo", "bar"}}); |
| 89 | + Table table(MakeIdentifier(), base_metadata, "loc", /*io=*/nullptr, catalog); |
| 90 | + |
| 91 | + auto updated_metadata = MakeBaseMetadata({{"foo", "new"}}); // response metadata |
| 92 | + |
| 93 | + EXPECT_CALL(*catalog, UpdateTable(table.name(), _, _)) |
| 94 | + .WillOnce(Return(ByMove(Result<std::unique_ptr<Table>>{ |
| 95 | + std::make_unique<Table>(table.name(), updated_metadata, "loc2", nullptr, |
| 96 | + catalog)}))); |
| 97 | + |
| 98 | + auto updater = table.UpdateProperties(); |
| 99 | + updater->Set("foo", "new"); |
| 100 | + |
| 101 | + EXPECT_THAT(updater->Commit(), IsOk()); |
| 102 | + EXPECT_EQ(table.properties().configs().at("foo"), "new"); |
| 103 | + EXPECT_EQ(table.location(), "s3://bucket/table"); |
| 104 | +} |
| 105 | + |
| 106 | +} // namespace iceberg |
0 commit comments