You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/02/01 07:13:16 UTC

[GitHub] [arrow] WillAyd commented on a change in pull request #12248: ARROW-1888: [C++] Implement Struct Casts

WillAyd commented on a change in pull request #12248:
URL: https://github.com/apache/arrow/pull/12248#discussion_r796311267



##########
File path: cpp/src/arrow/compute/kernels/scalar_cast_nested.cc
##########
@@ -150,6 +150,73 @@ void AddListCast(CastFunction* func) {
   DCHECK_OK(func->AddKernel(SrcType::type_id, std::move(kernel)));
 }
 
+struct CastStruct {
+  static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
+    const CastOptions& options = CastState::Get(ctx);
+    const auto in_field_count =
+        checked_cast<const StructType&>(*batch[0].type()).num_fields();
+    const auto out_field_count =
+        checked_cast<const StructType&>(*out->type()).num_fields();
+
+    if (in_field_count != out_field_count) {
+      ARROW_RETURN_NOT_OK(
+          Status(StatusCode::TypeError, "struct field sizes do not match"));
+    }
+
+    for (int64_t i = 0; i < in_field_count; ++i) {
+      const auto in_field_name =
+          checked_cast<const StructType&>(*batch[0].type()).field(i)->name();
+      const auto out_field_name =
+          checked_cast<const StructType&>(*out->type()).field(i)->name();
+      if (in_field_name != out_field_name) {
+        ARROW_RETURN_NOT_OK(
+            Status(StatusCode::TypeError, "struct field names do not match"));
+      }
+    }
+
+    if (out->kind() == Datum::SCALAR) {
+      const auto& in_scalar = checked_cast<const StructScalar&>(*batch[0].scalar());
+      auto out_scalar = checked_cast<StructScalar*>(out->scalar().get());
+
+      for (int64_t i = 0; i < in_field_count; i++) {
+        auto values = in_scalar.value[i];
+        auto target_type = out->type()->field(i)->type();
+        ARROW_ASSIGN_OR_RAISE(Datum cast_values,
+                              Cast(values, target_type, options, ctx->exec_context()));
+        DCHECK_EQ(Datum::SCALAR, cast_values.kind());
+        out_scalar->value.push_back(cast_values.scalar());
+      }
+
+      out_scalar->is_valid = true;
+      return Status::OK();
+    }
+
+    const ArrayData& in_array = *batch[0].array();
+    ArrayData* out_array = out->mutable_array();
+
+    for (int64_t i = 0; i < in_field_count; ++i) {
+      auto values = in_array.child_data[0];

Review comment:
       I think I set up a minimal case for this in the tests but it did not fail - let me know if I am overlooking something




-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org