wgtmac commented on code in PR #106: URL: https://github.com/apache/iceberg-cpp/pull/106#discussion_r2100379480
########## 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: This visitor mixes the feature of `HasIds` and `MissingIds` from the Java implementation so I can check them in a single pass. I think an Avro schema should be considered valid when all or none of fields have field-ids, but not in between. -- 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