nirandaperera commented on a change in pull request #10538:
URL: https://github.com/apache/arrow/pull/10538#discussion_r663116352



##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc
##########
@@ -455,6 +602,302 @@ struct IfElseFunctor<Type, enable_if_number<Type>> {
   }
 };
 
+template <typename Type>
+struct IfElseFunctor<Type, enable_if_base_binary<Type>> {
+  using OffsetType = typename TypeTraits<Type>::OffsetType::c_type;
+  using ArrayType = typename TypeTraits<Type>::ArrayType;
+
+  // A - Array, S - Scalar, X = Array/Scalar
+
+  // SXX
+  static Status Call(KernelContext* ctx, const BooleanScalar& cond, const 
Datum& left,
+                     const Datum& right, Datum* out) {
+    if (left.is_scalar() && right.is_scalar()) {
+      if (cond.is_valid) {
+        *out = cond.value ? left.scalar() : right.scalar();
+      } else {
+        *out = MakeNullScalar(left.type());
+      }
+      return Status::OK();
+    }
+    // either left or right is an array. Output is always an array
+    int64_t out_arr_len = std::max(left.length(), right.length());
+    if (!cond.is_valid) {
+      // cond is null; just create a null array
+      ARROW_ASSIGN_OR_RAISE(*out,
+                            MakeArrayOfNull(left.type(), out_arr_len, 
ctx->memory_pool()))
+      return Status::OK();
+    }
+
+    const auto& valid_data = cond.value ? left : right;
+    if (valid_data.is_array()) {
+      *out = valid_data;
+    } else {
+      // valid data is a scalar that needs to be broadcasted
+      ARROW_ASSIGN_OR_RAISE(*out, MakeArrayFromScalar(*valid_data.scalar(), 
out_arr_len,
+                                                      ctx->memory_pool()));
+    }
+    return Status::OK();
+  }
+
+  //  AAA
+  static Status Call(KernelContext* ctx, const ArrayData& cond, const 
ArrayData& left,
+                     const ArrayData& right, ArrayData* out) {
+    const uint8_t* cond_data = cond.buffers[1]->data();
+    BitBlockCounter bit_counter(cond_data, cond.offset, cond.length);
+
+    const auto* left_offsets = left.GetValues<OffsetType>(1);
+    const uint8_t* left_data = left.buffers[2]->data();
+    const auto* right_offsets = right.GetValues<OffsetType>(1);
+    const uint8_t* right_data = right.buffers[2]->data();
+
+    // reserve an additional space
+    ARROW_ASSIGN_OR_RAISE(auto out_offset_buf,
+                          ctx->Allocate((cond.length + 1) * 
sizeof(OffsetType)));
+    auto* out_offsets = 
reinterpret_cast<OffsetType*>(out_offset_buf->mutable_data());
+    out_offsets[0] = 0;
+
+    // allocate data buffer conservatively

Review comment:
       @lidavidm I tried with the builders. There's some perf penalty but the 
code is much more readable and manageable. So, I'll change binary ones to that. 
I simplified the code a lot TBH. Let me push that changes 




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


Reply via email to