lidavidm commented on a change in pull request #10410:
URL: https://github.com/apache/arrow/pull/10410#discussion_r640711551



##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc
##########
@@ -0,0 +1,838 @@
+// 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 <arrow/compute/api.h>
+#include <arrow/util/bit_block_counter.h>
+#include <arrow/util/bitmap.h>
+#include <arrow/util/bitmap_ops.h>
+
+#include "codegen_internal.h"
+
+namespace arrow {
+using internal::BitBlockCount;
+using internal::BitBlockCounter;
+using internal::Bitmap;
+
+namespace compute {
+
+namespace {
+
+enum { COND_ALL_VALID = 1, LEFT_ALL_VALID = 2, RIGHT_ALL_VALID = 4 };
+
+// if the condition is null then output is null otherwise we take validity 
from the
+// selected argument
+// ie. cond.valid & (cond.data & left.valid | ~cond.data & right.valid)
+Status PromoteNullsVisitor(KernelContext* ctx, const ArrayData& cond, const 
Scalar& left,
+                           const Scalar& right, ArrayData* output) {
+  uint8_t flag = right.is_valid * 4 + left.is_valid * 2 + !cond.MayHaveNulls();
+
+  if (flag < 6 && flag != 3) {
+    // there will be a validity buffer in the output
+    ARROW_ASSIGN_OR_RAISE(output->buffers[0], 
ctx->AllocateBitmap(cond.length));
+  }
+
+  // if the condition is null then output is null otherwise we take validity 
from the
+  // selected argument
+  // ie. cond.valid & (cond.data & left.valid | ~cond.data & right.valid)
+  switch (flag) {
+    case COND_ALL_VALID | LEFT_ALL_VALID | RIGHT_ALL_VALID:  // = 7
+      break;
+    case LEFT_ALL_VALID | RIGHT_ALL_VALID:  // = 6
+      // out_valid = c_valid
+      output->buffers[0] = SliceBuffer(cond.buffers[0], cond.offset, 
cond.length);
+      break;
+    case COND_ALL_VALID | RIGHT_ALL_VALID:  // = 5
+      // out_valid = ~cond.data
+      arrow::internal::InvertBitmap(cond.buffers[1]->data(), cond.offset, 
cond.length,
+                                    output->buffers[0]->mutable_data(), 0);
+      break;
+    case RIGHT_ALL_VALID:  // = 4
+      // out_valid = c_valid & ~cond.data
+      arrow::internal::BitmapAndNot(cond.buffers[0]->data(), cond.offset,
+                                    cond.buffers[1]->data(), cond.offset, 
cond.length, 0,
+                                    output->buffers[0]->mutable_data());
+      break;
+    case COND_ALL_VALID | LEFT_ALL_VALID:  // = 3
+      // out_valid = cond.data
+      output->buffers[0] = SliceBuffer(cond.buffers[1], cond.offset, 
cond.length);
+      break;
+    case LEFT_ALL_VALID:  // = 2
+      // out_valid = cond.valid & cond.data
+      arrow::internal::BitmapAnd(cond.buffers[0]->data(), cond.offset,
+                                 cond.buffers[1]->data(), cond.offset, 
cond.length, 0,
+                                 output->buffers[0]->mutable_data());
+      break;
+    case COND_ALL_VALID:  // = 1
+      // out_valid = 0 --> nothing to do; but requires out_valid to be a 
all-zero buffer

Review comment:
       Hmm, we should probably update comment then. CC @bkietz given 
KernelContext::AllocateBitmap has to zero the allocation to satisfy Valgrind et 
al, should we just go ahead and guarantee that it returns a pre-cleared bitmap?




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to