|
35 | 35 |
|
36 | 36 | namespace iceberg { |
37 | 37 |
|
| 38 | +Schema::Schema(std::vector<SchemaField> fields, int32_t schema_id) |
| 39 | + : StructType(std::move(fields)), schema_id_(schema_id) {} |
| 40 | + |
| 41 | +Result<std::unique_ptr<Schema>> Schema::Make(std::vector<SchemaField> fields, |
| 42 | + int32_t schema_id, |
| 43 | + std::vector<int32_t> identifier_field_ids) { |
| 44 | + auto schema = std::make_unique<Schema>(std::move(fields), schema_id); |
38 | 45 |
|
| 46 | + if (!identifier_field_ids.empty()) { |
| 47 | + auto id_to_parent = IndexParents(*schema); |
| 48 | + for (auto field_id : identifier_field_ids) { |
| 49 | + ICEBERG_RETURN_UNEXPECTED( |
| 50 | + ValidateIdentifierFields(field_id, *schema, id_to_parent)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + schema->identifier_field_ids_ = std::move(identifier_field_ids); |
| 55 | + return schema; |
| 56 | +} |
| 57 | + |
| 58 | +Result<std::unique_ptr<Schema>> Schema::Make( |
| 59 | + std::vector<SchemaField> fields, int32_t schema_id, |
| 60 | + const std::vector<std::string>& identifier_field_names) { |
| 61 | + auto schema = std::make_unique<Schema>(std::move(fields), schema_id); |
39 | 62 |
|
40 | | -namespace { |
41 | | -Status ValidateIdentifierFields( |
| 63 | + std::vector<int32_t> fresh_identifier_ids; |
| 64 | + for (const auto& name : identifier_field_names) { |
| 65 | + ICEBERG_ASSIGN_OR_RAISE(auto field, schema->FindFieldByName(name)); |
| 66 | + if (!field) { |
| 67 | + return InvalidSchema("Cannot find identifier field: {}", name); |
| 68 | + } |
| 69 | + fresh_identifier_ids.push_back(field.value().get().field_id()); |
| 70 | + } |
| 71 | + |
| 72 | + if (!fresh_identifier_ids.empty()) { |
| 73 | + auto id_to_parent = IndexParents(*schema); |
| 74 | + for (auto field_id : fresh_identifier_ids) { |
| 75 | + ICEBERG_RETURN_UNEXPECTED( |
| 76 | + ValidateIdentifierFields(field_id, *schema, id_to_parent)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + schema->identifier_field_ids_ = std::move(fresh_identifier_ids); |
| 81 | + return schema; |
| 82 | +} |
| 83 | + |
| 84 | +Status Schema::ValidateIdentifierFields( |
42 | 85 | int32_t field_id, const Schema& schema, |
43 | 86 | const std::unordered_map<int32_t, int32_t>& id_to_parent) { |
44 | 87 | ICEBERG_ASSIGN_OR_RAISE(auto field_opt, schema.FindFieldById(field_id)); |
@@ -87,53 +130,6 @@ Status ValidateIdentifierFields( |
87 | 130 | } |
88 | 131 | return {}; |
89 | 132 | } |
90 | | -} // namespace |
91 | | - |
92 | | -Schema::Schema(std::vector<SchemaField> fields, int32_t schema_id) |
93 | | - : StructType(std::move(fields)), schema_id_(schema_id) {} |
94 | | - |
95 | | -Result<std::unique_ptr<Schema>> Schema::Make(std::vector<SchemaField> fields, |
96 | | - int32_t schema_id, |
97 | | - std::vector<int32_t> identifier_field_ids) { |
98 | | - auto schema = std::make_unique<Schema>(std::move(fields), schema_id); |
99 | | - |
100 | | - if (!identifier_field_ids.empty()) { |
101 | | - auto id_to_parent = IndexParents(*schema); |
102 | | - for (auto field_id : identifier_field_ids) { |
103 | | - ICEBERG_RETURN_UNEXPECTED( |
104 | | - ValidateIdentifierFields(field_id, *schema, id_to_parent)); |
105 | | - } |
106 | | - } |
107 | | - |
108 | | - schema->identifier_field_ids_ = std::move(identifier_field_ids); |
109 | | - return schema; |
110 | | -} |
111 | | - |
112 | | -Result<std::unique_ptr<Schema>> Schema::Make( |
113 | | - std::vector<SchemaField> fields, int32_t schema_id, |
114 | | - const std::vector<std::string>& identifier_field_names) { |
115 | | - auto schema = std::make_unique<Schema>(std::move(fields), schema_id); |
116 | | - |
117 | | - std::vector<int32_t> fresh_identifier_ids; |
118 | | - for (const auto& name : identifier_field_names) { |
119 | | - ICEBERG_ASSIGN_OR_RAISE(auto field, schema->FindFieldByName(name)); |
120 | | - if (!field) { |
121 | | - return InvalidSchema("Cannot find identifier field: {}", name); |
122 | | - } |
123 | | - fresh_identifier_ids.push_back(field.value().get().field_id()); |
124 | | - } |
125 | | - |
126 | | - if (!fresh_identifier_ids.empty()) { |
127 | | - auto id_to_parent = IndexParents(*schema); |
128 | | - for (auto field_id : fresh_identifier_ids) { |
129 | | - ICEBERG_RETURN_UNEXPECTED( |
130 | | - ValidateIdentifierFields(field_id, *schema, id_to_parent)); |
131 | | - } |
132 | | - } |
133 | | - |
134 | | - schema->identifier_field_ids_ = std::move(fresh_identifier_ids); |
135 | | - return schema; |
136 | | -} |
137 | 133 |
|
138 | 134 | const std::shared_ptr<Schema>& Schema::EmptySchema() { |
139 | 135 | static const auto empty_schema = |
|
0 commit comments