nullccxsy commented on code in PR #180:
URL: https://github.com/apache/iceberg-cpp/pull/180#discussion_r2291645770
##########
src/iceberg/schema.cc:
##########
@@ -44,4 +80,172 @@ bool Schema::Equals(const Schema& other) const {
return schema_id_ == other.schema_id_ && fields_ == other.fields_;
}
+Result<std::optional<std::reference_wrapper<const SchemaField>>>
Schema::FindFieldByName(
+ std::string_view name, bool case_sensitive) const {
+ if (case_sensitive) {
+ ICEBERG_RETURN_UNEXPECTED(InitNameToIndexMap());
+ auto it = name_to_index_.find(std::string(name));
+ if (it == name_to_index_.end()) return std::nullopt;
+ return full_schemafield_[it->second];
+ }
+ ICEBERG_RETURN_UNEXPECTED(InitLowerCaseNameToIndexMap());
+ std::string lower_name(name);
+ std::ranges::transform(lower_name, lower_name.begin(), ::tolower);
+ auto it = lowercase_name_to_index_.find(lower_name);
+ if (it == lowercase_name_to_index_.end()) return std::nullopt;
+ return full_schemafield_[it->second];
+}
+
+Result<std::optional<std::reference_wrapper<const SchemaField>>>
Schema::FindFieldByName(
+ std::string_view name) const {
+ return FindFieldByName(name, /*case_sensitive*/ true);
+}
+
+Result<Status> Schema::InitIdToIndexMap() const {
+ if (!id_to_index_.empty()) {
+ return {};
+ }
+ bool has_init = !full_schemafield_.empty();
+ IdVisitor visitor(has_init);
+ ICEBERG_RETURN_UNEXPECTED(VisitTypeInline(*this, &visitor));
+ id_to_index_ = std::move(visitor.id_to_index);
+ if (!has_init) {
+ full_schemafield_ = std::move(visitor.full_schemafield);
+ }
+ return {};
+}
+
+Result<Status> Schema::InitNameToIndexMap() const {
+ if (!name_to_index_.empty()) {
+ return {};
+ }
+ bool has_init = !full_schemafield_.empty();
+ std::string path, short_path;
+ NameVisitor visitor(true, has_init);
+ ICEBERG_RETURN_UNEXPECTED(VisitTypeInline(*this, &visitor, path,
short_path));
+ name_to_index_ = std::move(visitor.name_to_index);
+ if (!has_init) {
+ full_schemafield_ = std::move(visitor.full_schemafield);
+ }
+ return {};
+}
+
+Result<Status> Schema::InitLowerCaseNameToIndexMap() const {
+ if (!lowercase_name_to_index_.empty()) {
+ return {};
+ }
+ bool has_init = !full_schemafield_.empty();
+ std::string path, short_path;
+ NameVisitor visitor(false, has_init);
+ ICEBERG_RETURN_UNEXPECTED(VisitTypeInline(*this, &visitor, path,
short_path));
+ lowercase_name_to_index_ = std::move(visitor.name_to_index);
+ if (!has_init) {
+ full_schemafield_ = std::move(visitor.full_schemafield);
+ }
+ return {};
+}
+
+Result<std::optional<std::reference_wrapper<const SchemaField>>>
Schema::FindFieldById(
+ int32_t field_id) const {
+ ICEBERG_RETURN_UNEXPECTED(InitIdToIndexMap());
+ auto it = id_to_index_.find(field_id);
+ if (it == id_to_index_.end()) {
+ return std::nullopt;
+ }
+ return full_schemafield_[it->second];
+}
+
+IdVisitor::IdVisitor(bool has_init_) : has_init(has_init_) {}
+
+Status IdVisitor::Visit(const Type& type) {
+ const auto& nested = iceberg::internal::checked_cast<const
NestedType&>(type);
Review Comment:
the first call of this function must be a NestedType, and recursive calls
will only occur if the child is a nested type.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]