Fokko commented on code in PR #106: URL: https://github.com/apache/iceberg-cpp/pull/106#discussion_r2100138742
########## src/iceberg/avro/avro_schema_util.cc: ########## @@ -263,4 +265,116 @@ Status ToAvroNodeVisitor::Visit(const SchemaField& field, ::avro::NodePtr* node) return {}; } +namespace { + +bool HasId(const ::avro::NodePtr& parent_node, size_t field_idx, + const std::string& attr_name) { + if (field_idx >= parent_node->customAttributes()) { + return false; + } + return parent_node->customAttributesAt(field_idx).getAttribute(attr_name).has_value(); +} + +} // namespace + +Status HasIdVisitor::Visit(const ::avro::NodePtr& node) { + if (!node) [[unlikely]] { + return InvalidSchema("Avro node is null"); + } + + switch (node->type()) { + case ::avro::AVRO_RECORD: + return VisitRecord(node); + case ::avro::AVRO_ARRAY: + return VisitArray(node); + case ::avro::AVRO_MAP: + return VisitMap(node); + case ::avro::AVRO_UNION: + return VisitUnion(node); + case ::avro::AVRO_BOOL: + case ::avro::AVRO_INT: + case ::avro::AVRO_LONG: + case ::avro::AVRO_FLOAT: + case ::avro::AVRO_DOUBLE: + case ::avro::AVRO_STRING: + case ::avro::AVRO_BYTES: + case ::avro::AVRO_FIXED: + return {}; + case ::avro::AVRO_NULL: + case ::avro::AVRO_ENUM: + default: + return InvalidSchema("Unsupported Avro type: {}", static_cast<int>(node->type())); + } +} + +Status HasIdVisitor::VisitRecord(const ::avro::NodePtr& node) { + static const std::string kFieldIdKey{kFieldIdProp}; + total_fields_ += node->leaves(); + for (size_t i = 0; i < node->leaves(); ++i) { + if (HasId(node, i, kFieldIdKey)) { + fields_with_id_++; Review Comment: Why keep track of the number of fields? Returning a boolean feels much less error-prone to me. On top of that, you can also very nicely `&&` the results: https://github.com/apache/iceberg-python/blob/f47513b8fc972dd26b99b1a9fbb102c712dd07fe/pyiceberg/io/pyarrow.py#L1132-L1155 -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org