zhjwpku commented on code in PR #124:
URL: https://github.com/apache/iceberg-cpp/pull/124#discussion_r2159975860


##########
src/iceberg/avro/avro_data_util.cc:
##########
@@ -17,16 +17,383 @@
  * under the License.
  */
 
+#include <arrow/array/builder_binary.h>
+#include <arrow/array/builder_decimal.h>
+#include <arrow/array/builder_nested.h>
+#include <arrow/array/builder_primitive.h>
+#include <arrow/json/from_string.h>
+#include <arrow/type.h>
+#include <arrow/util/decimal.h>
+#include <avro/Generic.hh>
+#include <avro/Node.hh>
+#include <avro/Types.hh>
+
+#include "iceberg/arrow/arrow_error_transform_internal.h"
 #include "iceberg/avro/avro_data_util_internal.h"
+#include "iceberg/avro/avro_schema_util_internal.h"
+#include "iceberg/schema.h"
+#include "iceberg/schema_util.h"
+#include "iceberg/util/checked_cast.h"
+#include "iceberg/util/macros.h"
 
 namespace iceberg::avro {
 
+using ::iceberg::arrow::ToErrorKind;
+
+namespace {
+
+/// \brief Forward declaration for mutual recursion.
+Status AppendFieldToBuilder(const ::avro::NodePtr& avro_node,
+                            const ::avro::GenericDatum& avro_datum,
+                            const FieldProjection& projection,
+                            const SchemaField& projected_field,
+                            ::arrow::ArrayBuilder* array_builder);
+
+/// \brief Append Avro record data to Arrow struct builder.
+Status AppendStructToBuilder(const ::avro::NodePtr& avro_node,
+                             const ::avro::GenericDatum& avro_datum,
+                             const std::span<const FieldProjection>& 
projections,
+                             const StructType& struct_type,
+                             ::arrow::ArrayBuilder* array_builder) {
+  if (avro_node->type() != ::avro::AVRO_RECORD) {
+    return InvalidArgument("Expected Avro record, got type: {}", 
ToString(avro_node));
+  }
+  const auto& avro_record = avro_datum.value<::avro::GenericRecord>();
+
+  auto* struct_builder = 
internal::checked_cast<::arrow::StructBuilder*>(array_builder);
+  ICEBERG_ARROW_RETURN_NOT_OK(struct_builder->Append());
+
+  for (size_t i = 0; i < projections.size(); ++i) {
+    const auto& field_projection = projections[i];
+    const auto& expected_field = struct_type.fields()[i];
+    auto* field_builder = struct_builder->field_builder(static_cast<int>(i));
+
+    if (field_projection.kind == FieldProjection::Kind::kProjected) {
+      size_t avro_field_index =
+          std::get<FieldProjection::SourceFieldIndex>(field_projection.from);
+      if (avro_field_index >= avro_record.fieldCount()) {
+        return InvalidArgument("Avro field index {} out of bound {}", 
avro_field_index,
+                               avro_record.fieldCount());
+      }
+
+      const auto& avro_field_node = avro_node->leafAt(avro_field_index);
+      const auto& avro_field_datum = avro_record.fieldAt(avro_field_index);
+      ICEBERG_RETURN_UNEXPECTED(AppendFieldToBuilder(avro_field_node, 
avro_field_datum,
+                                                     field_projection, 
expected_field,
+                                                     field_builder));
+    } else if (field_projection.kind == FieldProjection::Kind::kNull) {
+      ICEBERG_ARROW_RETURN_NOT_OK(field_builder->AppendNull());
+    } else {
+      return NotImplemented("Unsupported field projection kind: {}",
+                            ToString(field_projection.kind));
+    }
+  }
+  return {};
+}
+
+/// \brief Append Avro array data to Arrow list builder.
+Status AppendListToBuilder(const ::avro::NodePtr& avro_node,
+                           const ::avro::GenericDatum& avro_datum,
+                           const FieldProjection& projection, const ListType& 
list_type,
+                           ::arrow::ArrayBuilder* array_builder) {
+  if (avro_node->type() != ::avro::AVRO_ARRAY) {
+    return InvalidArgument("Expected Avro array, got type: {}", 
ToString(avro_node));
+  }
+  const auto& avro_array = avro_datum.value<::avro::GenericArray>();
+
+  auto* list_builder = 
internal::checked_cast<::arrow::ListBuilder*>(array_builder);

Review Comment:
   Is there any chance `list_builder` could be a nullptr, should we add a check 
for that?
   If the user provides the same schema for ToArrowSchema and Project, it 
shouldn't be an issue. However, should we return an error if the user make 
mistakes? Not so sure.



##########
test/avro_data_test.cc:
##########
@@ -0,0 +1,365 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <ranges>
+
+#include <arrow/c/bridge.h>
+#include <arrow/json/from_string.h>
+#include <avro/Compiler.hh>
+#include <avro/Generic.hh>
+#include <avro/Node.hh>
+#include <avro/Types.hh>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <iceberg/arrow/arrow_error_transform_internal.h>

Review Comment:
   nit: use "" for iceberg headers?



-- 
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

Reply via email to