wgtmac commented on code in PR #166:
URL: https://github.com/apache/iceberg-cpp/pull/166#discussion_r2275884437


##########
src/iceberg/avro/avro_data_util.cc:
##########
@@ -451,4 +453,217 @@ Status AppendDatumToBuilder(const ::avro::NodePtr& 
avro_node,
                                     projected_schema, array_builder);
 }
 
+namespace {
+
+// ToAvroNodeVisitor uses 0 for null branch and 1 for value branch.
+constexpr int64_t kNullBranch = 0;
+constexpr int64_t kValueBranch = 1;
+
+}  // namespace
+
+Status ExtractDatumFromArray(const ::arrow::Array& array, int64_t index,
+                             ::avro::GenericDatum* datum) {
+  if (index < 0 || index >= array.length()) {
+    return InvalidArgument("Cannot extract datum from array at index {} of 
length {}",
+                           index, array.length());
+  }
+
+  if (array.IsNull(index)) {
+    if (!datum->isUnion()) [[unlikely]] {
+      return InvalidSchema("Cannot extract null to non-union type: {}",
+                           ::avro::toString(datum->type()));
+    }
+    datum->selectBranch(kNullBranch);
+    return {};
+  }
+
+  if (datum->isUnion()) {
+    datum->selectBranch(kValueBranch);
+  }
+
+  switch (array.type()->id()) {
+    case ::arrow::Type::BOOL: {
+      const auto& bool_array =
+          internal::checked_cast<const ::arrow::BooleanArray&>(array);
+      datum->value<bool>() = bool_array.Value(index);

Review Comment:
   No, this is an internal API used by the Avro writer. Schemas of 
GenericDatum, ArrowArray and Iceberg should be consistent. The GenericDatum is 
reused so we don't want to check it repeatedly.



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

Reply via email to