2626#include " iceberg/util/formatter.h" // IWYU pragma: keep
2727#include " iceberg/util/macros.h"
2828#include " iceberg/util/visit_type.h"
29+
2930namespace iceberg {
30- class IdVisitor {
31+ class IdToFieldVisitor {
3132 public:
32- explicit IdVisitor (
33+ explicit IdToFieldVisitor (
3334 std::unordered_map<int32_t , std::reference_wrapper<const SchemaField>>&
3435 id_to_field);
3536 Status Visit (const Type& type);
@@ -38,9 +39,10 @@ class IdVisitor {
3839 private:
3940 std::unordered_map<int32_t , std::reference_wrapper<const SchemaField>>& id_to_field_;
4041};
42+
4143class NametoIdVisitor {
4244 public:
43- explicit NametoIdVisitor (std::unordered_map<std::string, size_t >& name_to_id,
45+ explicit NametoIdVisitor (std::unordered_map<std::string, int32_t >& name_to_id,
4446 bool case_sensitive_ = true );
4547 Status Visit (const ListType& type, const std::string& path,
4648 const std::string& short_path);
@@ -50,13 +52,14 @@ class NametoIdVisitor {
5052 const std::string& short_path);
5153 Status Visit (const PrimitiveType& type, const std::string& path,
5254 const std::string& short_path);
53- static std::string GetPath ( const std::string& last_path, const std::string& field_name,
54- bool case_sensitive = true );
55+ static std::string BuildPath ( std::string_view prefix, std::string_view field_name,
56+ bool case_sensitive );
5557
5658 private:
5759 bool case_sensitive_;
58- std::unordered_map<std::string, size_t >& name_to_id_;
60+ std::unordered_map<std::string, int32_t >& name_to_id_;
5961};
62+
6063Schema::Schema (std::vector<SchemaField> fields, std::optional<int32_t > schema_id)
6164 : StructType(std::move(fields)), schema_id_(schema_id) {}
6265
@@ -91,16 +94,11 @@ Result<std::optional<std::reference_wrapper<const SchemaField>>> Schema::FindFie
9194 return FindFieldById (it->second );
9295}
9396
94- Result<std::optional<std::reference_wrapper<const SchemaField>>> Schema::FindFieldByName (
95- std::string_view name) const {
96- return FindFieldByName (name, /* case_sensitive*/ true );
97- }
98-
9997Result<Status> Schema::InitIdToIndexMap () const {
10098 if (!id_to_field_.empty ()) {
10199 return {};
102100 }
103- IdVisitor visitor (id_to_field_);
101+ IdToFieldVisitor visitor (id_to_field_);
104102 ICEBERG_RETURN_UNEXPECTED (VisitTypeInline (*this , &visitor));
105103 return {};
106104}
@@ -135,18 +133,18 @@ Result<std::optional<std::reference_wrapper<const SchemaField>>> Schema::FindFie
135133 return it->second ;
136134}
137135
138- IdVisitor::IdVisitor (
136+ IdToFieldVisitor::IdToFieldVisitor (
139137 std::unordered_map<int32_t , std::reference_wrapper<const SchemaField>>& id_to_field)
140138 : id_to_field_(id_to_field) {}
141139
142- Status IdVisitor ::Visit (const Type& type) {
140+ Status IdToFieldVisitor ::Visit (const Type& type) {
143141 if (type.is_nested ()) {
144142 ICEBERG_RETURN_UNEXPECTED (VisitNestedType (type));
145143 }
146144 return {};
147145}
148146
149- Status IdVisitor ::VisitNestedType (const Type& type) {
147+ Status IdToFieldVisitor ::VisitNestedType (const Type& type) {
150148 const auto & nested = iceberg::internal::checked_cast<const NestedType&>(type);
151149 const auto & fields = nested.fields ();
152150 for (const auto & field : fields) {
@@ -156,59 +154,58 @@ Status IdVisitor::VisitNestedType(const Type& type) {
156154 return {};
157155}
158156
159- NametoIdVisitor::NametoIdVisitor (std::unordered_map<std::string, size_t >& name_to_id,
157+ NametoIdVisitor::NametoIdVisitor (std::unordered_map<std::string, int32_t >& name_to_id,
160158 bool case_sensitive)
161159 : name_to_id_(name_to_id), case_sensitive_(case_sensitive) {}
162160
163161Status NametoIdVisitor::Visit (const ListType& type, const std::string& path,
164162 const std::string& short_path) {
165163 const auto & field = type.fields ()[0 ];
166- std::string full_path = GetPath (path, std::string ( field.name () ), case_sensitive_);
167- std::string short_full_path ;
164+ std::string new_path = BuildPath (path, field.name (), case_sensitive_);
165+ std::string new_short_path ;
168166 if (field.type ()->type_id () == TypeId::kStruct ) {
169- short_full_path = short_path;
167+ new_short_path = short_path;
170168 } else {
171- short_full_path = GetPath (short_path, std::string ( field.name () ), case_sensitive_);
169+ new_short_path = BuildPath (short_path, field.name (), case_sensitive_);
172170 }
173- name_to_id_[full_path ] = field.field_id ();
174- name_to_id_.emplace (short_full_path , field.field_id ());
171+ name_to_id_[new_path ] = field.field_id ();
172+ name_to_id_.emplace (new_short_path , field.field_id ());
175173 ICEBERG_RETURN_UNEXPECTED (
176- VisitTypeInline (*field.type (), this , full_path, short_full_path ));
174+ VisitTypeInline (*field.type (), this , new_path, new_short_path ));
177175 return {};
178176}
179177
180178Status NametoIdVisitor::Visit (const MapType& type, const std::string& path,
181179 const std::string& short_path) {
182- std::string full_path, short_full_path ;
180+ std::string new_path, new_short_path ;
183181 const auto & fields = type.fields ();
184182 for (const auto & field : fields) {
185- full_path = GetPath (path, std::string ( field.name () ), case_sensitive_);
183+ new_path = BuildPath (path, field.name (), case_sensitive_);
186184 if (field.name () == MapType::kValueName &&
187185 field.type ()->type_id () == TypeId::kStruct ) {
188- short_full_path = short_path;
186+ new_short_path = short_path;
189187 } else {
190- short_full_path = GetPath (path, std::string ( field.name () ), case_sensitive_);
188+ new_short_path = BuildPath (short_path, field.name (), case_sensitive_);
191189 }
192- name_to_id_[full_path ] = field.field_id ();
193- name_to_id_.emplace (short_full_path , field.field_id ());
190+ name_to_id_[new_path ] = field.field_id ();
191+ name_to_id_.emplace (new_short_path , field.field_id ());
194192 ICEBERG_RETURN_UNEXPECTED (
195- VisitTypeInline (*field.type (), this , full_path, short_full_path ));
193+ VisitTypeInline (*field.type (), this , new_path, new_short_path ));
196194 }
197195 return {};
198196}
199197
200198Status NametoIdVisitor::Visit (const StructType& type, const std::string& path,
201199 const std::string& short_path) {
202200 const auto & fields = type.fields ();
203- std::string full_path, short_full_path ;
201+ std::string new_path, new_short_path ;
204202 for (const auto & field : fields) {
205- full_path =
206- NametoIdVisitor::GetPath (path, std::string (field.name ()), case_sensitive_);
207- short_full_path = GetPath (short_path, std::string (field.name ()), case_sensitive_);
208- name_to_id_[full_path] = field.field_id ();
209- name_to_id_.emplace (short_full_path, field.field_id ());
203+ new_path = BuildPath (path, field.name (), case_sensitive_);
204+ new_short_path = BuildPath (short_path, field.name (), case_sensitive_);
205+ name_to_id_[new_path] = field.field_id ();
206+ name_to_id_.emplace (new_short_path, field.field_id ());
210207 ICEBERG_RETURN_UNEXPECTED (
211- VisitTypeInline (*field.type (), this , full_path, short_full_path ));
208+ VisitTypeInline (*field.type (), this , new_path, new_short_path ));
212209 }
213210 return {};
214211}
@@ -218,13 +215,14 @@ Status NametoIdVisitor::Visit(const PrimitiveType& type, const std::string& path
218215 return {};
219216}
220217
221- std::string NametoIdVisitor::GetPath ( const std::string& last_path ,
222- const std::string& field_name, bool case_sensitive) {
218+ std::string NametoIdVisitor::BuildPath ( std::string_view prefix ,
219+ std::string_view field_name, bool case_sensitive) {
223220 if (case_sensitive) {
224- return last_path.empty () ? field_name : last_path + " ." + field_name;
221+ return prefix.empty () ? std::string (field_name)
222+ : std::string (prefix) + " ." + std::string (field_name);
225223 }
226224 std::string lower_name (field_name);
227225 std::ranges::transform (lower_name, lower_name.begin (), ::tolower);
228- return last_path .empty () ? lower_name : last_path + " ." + lower_name;
226+ return prefix .empty () ? lower_name : std::string (prefix) + " ." + lower_name;
229227}
230228} // namespace iceberg
0 commit comments