|
| 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 <memory> |
| 21 | + |
| 22 | +#include <gmock/gmock.h> |
| 23 | +#include <gtest/gtest.h> |
| 24 | + |
| 25 | +#include "iceberg/schema.h" |
| 26 | +#include "iceberg/schema_field.h" |
| 27 | +#include "iceberg/test/matchers.h" |
| 28 | +#include "iceberg/type.h" |
| 29 | +#include "iceberg/util/type_util.h" |
| 30 | + |
| 31 | +namespace iceberg { |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +Schema CreateFlatSchema() { |
| 36 | + return Schema({ |
| 37 | + SchemaField::MakeRequired(/*field_id=*/10, "id", iceberg::int64()), |
| 38 | + SchemaField::MakeOptional(/*field_id=*/20, "name", iceberg::string()), |
| 39 | + SchemaField::MakeOptional(/*field_id=*/30, "age", iceberg::int32()), |
| 40 | + SchemaField::MakeRequired(/*field_id=*/40, "data", iceberg::float64()), |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +std::shared_ptr<Type> CreateListOfStruct() { |
| 45 | + return std::make_shared<ListType>(SchemaField::MakeOptional( |
| 46 | + /*field_id=*/101, "element", |
| 47 | + std::make_shared<StructType>(std::vector<SchemaField>{ |
| 48 | + SchemaField::MakeOptional(/*field_id=*/102, "x", iceberg::int32()), |
| 49 | + SchemaField::MakeRequired(/*field_id=*/103, "y", iceberg::string()), |
| 50 | + }))); |
| 51 | +} |
| 52 | + |
| 53 | +std::shared_ptr<Type> CreateMapWithStructValue() { |
| 54 | + return std::make_shared<MapType>( |
| 55 | + SchemaField::MakeRequired(/*field_id=*/201, "key", iceberg::string()), |
| 56 | + SchemaField::MakeRequired( |
| 57 | + /*field_id=*/202, "value", |
| 58 | + std::make_shared<StructType>(std::vector<SchemaField>{ |
| 59 | + SchemaField::MakeRequired(/*field_id=*/203, "id", iceberg::int64()), |
| 60 | + SchemaField::MakeOptional(/*field_id=*/204, "name", iceberg::string()), |
| 61 | + }))); |
| 62 | +} |
| 63 | + |
| 64 | +std::shared_ptr<Type> CreateNestedStruct() { |
| 65 | + return std::make_shared<StructType>(std::vector<SchemaField>{ |
| 66 | + SchemaField::MakeRequired(/*field_id=*/301, "outer_id", iceberg::int64()), |
| 67 | + SchemaField::MakeRequired( |
| 68 | + /*field_id=*/302, "nested", |
| 69 | + std::make_shared<StructType>(std::vector<SchemaField>{ |
| 70 | + SchemaField::MakeOptional(/*field_id=*/303, "inner_id", iceberg::int32()), |
| 71 | + SchemaField::MakeRequired(/*field_id=*/304, "inner_name", |
| 72 | + iceberg::string()), |
| 73 | + })), |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +Schema CreateNestedSchema(std::vector<int32_t> identifier_field_ids = {}) { |
| 78 | + return Schema( |
| 79 | + { |
| 80 | + SchemaField::MakeRequired(/*field_id=*/10, "id", iceberg::int64()), |
| 81 | + SchemaField::MakeOptional(/*field_id=*/20, "list", CreateListOfStruct()), |
| 82 | + SchemaField::MakeOptional(/*field_id=*/30, "map", CreateMapWithStructValue()), |
| 83 | + SchemaField::MakeRequired(/*field_id=*/40, "struct", CreateNestedStruct()), |
| 84 | + }, |
| 85 | + Schema::kInitialSchemaId, std::move(identifier_field_ids)); |
| 86 | +} |
| 87 | + |
| 88 | +} // namespace |
| 89 | + |
| 90 | +TEST(AssignFreshIdVisitorTest, FlatSchema) { |
| 91 | + Schema schema = CreateFlatSchema(); |
| 92 | + |
| 93 | + std::atomic<int32_t> id = 0; |
| 94 | + auto next_id = [&id]() { return ++id; }; |
| 95 | + ICEBERG_UNWRAP_OR_FAIL(auto fresh_schema, |
| 96 | + AssignFreshIds(Schema::kInitialSchemaId, schema, next_id)); |
| 97 | + |
| 98 | + ASSERT_EQ(fresh_schema->fields().size(), schema.fields().size()); |
| 99 | + EXPECT_EQ(Schema( |
| 100 | + { |
| 101 | + SchemaField::MakeRequired(/*field_id=*/1, "id", iceberg::int64()), |
| 102 | + SchemaField::MakeOptional(/*field_id=*/2, "name", iceberg::string()), |
| 103 | + SchemaField::MakeOptional(/*field_id=*/3, "age", iceberg::int32()), |
| 104 | + SchemaField::MakeRequired(/*field_id=*/4, "data", iceberg::float64()), |
| 105 | + }, |
| 106 | + Schema::kInitialSchemaId), |
| 107 | + *fresh_schema); |
| 108 | +} |
| 109 | + |
| 110 | +TEST(AssignFreshIdVisitorTest, NestedSchema) { |
| 111 | + Schema schema = CreateNestedSchema(); |
| 112 | + std::atomic<int32_t> id = 0; |
| 113 | + auto next_id = [&id]() { return ++id; }; |
| 114 | + ICEBERG_UNWRAP_OR_FAIL(auto fresh_schema, |
| 115 | + AssignFreshIds(Schema::kInitialSchemaId, schema, next_id)); |
| 116 | + |
| 117 | + ASSERT_EQ(4, fresh_schema->fields().size()); |
| 118 | + for (int32_t i = 0; i < fresh_schema->fields().size(); ++i) { |
| 119 | + EXPECT_EQ(i + 1, fresh_schema->fields()[i].field_id()); |
| 120 | + } |
| 121 | + |
| 122 | + auto list_field = fresh_schema->fields()[1]; |
| 123 | + auto list_type = std::dynamic_pointer_cast<ListType>(list_field.type()); |
| 124 | + ASSERT_TRUE(list_type); |
| 125 | + auto list_element_field = list_type->fields()[0]; |
| 126 | + EXPECT_EQ(5, list_element_field.field_id()); |
| 127 | + auto list_element_type = |
| 128 | + std::dynamic_pointer_cast<StructType>(list_element_field.type()); |
| 129 | + ASSERT_TRUE(list_element_type); |
| 130 | + EXPECT_EQ(StructType(std::vector<SchemaField>{ |
| 131 | + SchemaField::MakeOptional(/*field_id=*/6, "x", iceberg::int32()), |
| 132 | + SchemaField::MakeRequired(/*field_id=*/7, "y", iceberg::string()), |
| 133 | + }), |
| 134 | + *list_element_type); |
| 135 | + |
| 136 | + auto map_field = fresh_schema->fields()[2]; |
| 137 | + auto map_type = std::dynamic_pointer_cast<MapType>(map_field.type()); |
| 138 | + ASSERT_TRUE(map_type); |
| 139 | + EXPECT_EQ(8, map_type->fields()[0].field_id()); |
| 140 | + auto map_value_field = map_type->fields()[1]; |
| 141 | + EXPECT_EQ(9, map_value_field.field_id()); |
| 142 | + auto map_value_type = std::dynamic_pointer_cast<StructType>(map_value_field.type()); |
| 143 | + ASSERT_TRUE(map_value_type); |
| 144 | + EXPECT_EQ(StructType(std::vector<SchemaField>{ |
| 145 | + SchemaField::MakeRequired(/*field_id=*/10, "id", iceberg::int64()), |
| 146 | + SchemaField::MakeOptional(/*field_id=*/11, "name", iceberg::string()), |
| 147 | + }), |
| 148 | + *map_value_type); |
| 149 | + |
| 150 | + auto struct_field = fresh_schema->fields()[3]; |
| 151 | + auto struct_type = std::dynamic_pointer_cast<StructType>(struct_field.type()); |
| 152 | + ASSERT_TRUE(struct_type); |
| 153 | + |
| 154 | + auto expect_nested_struct_type = std::make_shared<StructType>(std::vector<SchemaField>{ |
| 155 | + SchemaField::MakeOptional(/*field_id=*/14, "inner_id", iceberg::int32()), |
| 156 | + SchemaField::MakeRequired(/*field_id=*/15, "inner_name", iceberg::string()), |
| 157 | + }); |
| 158 | + EXPECT_EQ(StructType(std::vector<SchemaField>{ |
| 159 | + SchemaField::MakeRequired(/*field_id=*/12, "outer_id", iceberg::int64()), |
| 160 | + SchemaField::MakeRequired( |
| 161 | + /*field_id=*/13, "nested", expect_nested_struct_type)}), |
| 162 | + *struct_type); |
| 163 | + |
| 164 | + auto nested_struct_field = struct_type->fields()[1]; |
| 165 | + auto nested_struct_type = |
| 166 | + std::dynamic_pointer_cast<StructType>(nested_struct_field.type()); |
| 167 | + ASSERT_TRUE(nested_struct_type); |
| 168 | + EXPECT_EQ(*expect_nested_struct_type, *nested_struct_type); |
| 169 | +} |
| 170 | + |
| 171 | +TEST(AssignFreshIdVisitorTest, RefreshIdentifierId) { |
| 172 | + std::atomic<int32_t> id = 0; |
| 173 | + auto next_id = [&id]() { return ++id; }; |
| 174 | + |
| 175 | + Schema invalid_schema = CreateNestedSchema({10, 400}); |
| 176 | + // Invalid identified field id |
| 177 | + auto result = AssignFreshIds(Schema::kInitialSchemaId, invalid_schema, next_id); |
| 178 | + EXPECT_THAT(result, IsError(ErrorKind::kInvalidSchema)); |
| 179 | + EXPECT_THAT(result, HasErrorMessage("Cannot find")); |
| 180 | + |
| 181 | + id = 0; |
| 182 | + Schema schema = CreateNestedSchema({10, 301}); |
| 183 | + ICEBERG_UNWRAP_OR_FAIL(auto fresh_schema, |
| 184 | + AssignFreshIds(Schema::kInitialSchemaId, schema, next_id)); |
| 185 | + EXPECT_THAT(fresh_schema->IdentifierFieldIds(), testing::ElementsAre(1, 12)); |
| 186 | +} |
| 187 | + |
| 188 | +} // namespace iceberg |
0 commit comments