MisterRaindrop commented on code in PR #127:
URL: https://github.com/apache/iceberg-cpp/pull/127#discussion_r2177752721
##########
src/iceberg/avro/avro_reader.cc:
##########
@@ -195,6 +204,156 @@ class AvroBatchReader::Impl {
return arrow_array;
}
+ // Apply field IDs to Avro schema nodes based on name mapping
+ Status ApplyFieldIdsFromNameMapping(const NameMapping& name_mapping,
+ ::avro::Node* node) {
+ switch (node->type()) {
+ case ::avro::AVRO_RECORD:
+ return ApplyFieldIdsToRecord(node, name_mapping);
+ case ::avro::AVRO_ARRAY:
+ return ApplyFieldIdsToArray(node, name_mapping);
+ case ::avro::AVRO_MAP:
+ return ApplyFieldIdsToMap(node, name_mapping);
+ case ::avro::AVRO_UNION:
+ return ApplyFieldIdsToUnion(node, name_mapping);
+ 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 for field ID application:
{}",
+ static_cast<int>(node->type()));
+ }
+ }
+
+ Status ApplyFieldIdsToRecord(::avro::Node* node, const NameMapping&
name_mapping) {
+ for (size_t i = 0; i < node->leaves(); ++i) {
+ const std::string& field_name = node->nameAt(i);
+ ::avro::Node* field_node = node->leafAt(i).get();
+
+ // Try to find field ID by name in the name mapping
+ if (auto field_ref = name_mapping.Find(field_name)) {
+ if (field_ref->get().field_id.has_value()) {
+ // Add field ID attribute to the node
+ ::avro::CustomAttributes attributes;
+ attributes.addAttribute(std::string(kFieldId),
+
std::to_string(field_ref->get().field_id.value()),
+ false);
+ node->addCustomAttributesForField(attributes);
Review Comment:
I understand.
if name mapping available I will call the `CreateAvroNodeWithFieldIds`, will
create new schema and update avro file_schema
like this
```
ICEBERG_ASSIGN_OR_RAISE(auto new_root_node,
CreateAvroNodeWithFieldIds(file_schema.root(), *options.name_mapping));
// Create a new schema with the updated root node
auto new_schema = ::avro::ValidSchema(new_root_node);
// Update the file schema to use the new schema with field IDs
file_schema = new_schema;
```
--
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]