wesm commented on a change in pull request #7410:
URL: https://github.com/apache/arrow/pull/7410#discussion_r440952996



##########
File path: cpp/src/arrow/testing/random.h
##########
@@ -250,6 +250,21 @@ class ARROW_EXPORT RandomArrayGenerator {
                                            int32_t min_length, int32_t 
max_length,
                                            double null_probability = 0);
 
+  /// \brief Randomly generate an Array of the specified type, size, and 
null_probability.
+  ///
+  /// Generation parameters other than size and null_probability are 
determined based on
+  /// the type of Array to be generated.
+  /// If boolean the probabilities of true,false values are 0.25,0.75 
respectively.
+  /// If numeric min,max will be the least and greatest representable values.
+  /// If string min_length,max_length will be 0,sqrt(size) respectively.
+  ///
+  /// \param[in] type the type of Array to generate
+  /// \param[in] size the size of the Array to generate
+  /// \param[in] null_probability the probability of a slot being null
+  /// \return a generated Array
+  std::shared_ptr<Array> ArrayOf(std::shared_ptr<DataType> type, int64_t size,
+                                 double null_probability);

Review comment:
       Perhaps as a follow up JIRA, can you introduce an `ArrayOfOptions` 
struct that allows some of the type-specific configurables to be set up 
en-masse and then passed to this function, such as
   
   * The boolean true probability
   * string min/max lengths

##########
File path: cpp/src/arrow/compute/kernels/scalar_validity.cc
##########
@@ -0,0 +1,110 @@
+// 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/kernels/common.h"
+
+#include "arrow/util/bit_util.h"
+#include "arrow/util/bitmap_ops.h"
+
+namespace arrow {
+
+using internal::CopyBitmap;
+using internal::InvertBitmap;
+
+namespace compute {
+namespace {
+
+struct IsValidOperator {
+  static void Call(KernelContext* ctx, const Scalar& in, Scalar* out) {
+    checked_cast<BooleanScalar*>(out)->value = in.is_valid;
+  }
+
+  static void Call(KernelContext* ctx, const ArrayData& arr, ArrayData* out) {
+    if (arr.buffers[0] != nullptr && out->offset == arr.offset &&
+        out->length == arr.length) {
+      out->buffers[1] = arr.buffers[0];
+      return;
+    }
+
+    KERNEL_RETURN_IF_ERROR(ctx, 
ctx->AllocateBitmap(out->length).Value(&out->buffers[1]));
+
+    if (arr.null_count == 0 || arr.buffers[0] == nullptr) {
+      BitUtil::SetBitsTo(out->buffers[1]->mutable_data(), out->offset, 
out->length, true);
+      return;
+    }
+
+    CopyBitmap(arr.buffers[0]->data(), arr.offset, arr.length,
+               out->buffers[1]->mutable_data(), out->offset);
+  }
+};
+
+struct IsNullOperator {
+  static void Call(KernelContext* ctx, const Scalar& in, Scalar* out) {
+    checked_cast<BooleanScalar*>(out)->value = !in.is_valid;
+  }
+
+  static void Call(KernelContext* ctx, const ArrayData& arr, ArrayData* out) {
+    if (arr.null_count == 0 || arr.buffers[0] == nullptr) {
+      BitUtil::SetBitsTo(out->buffers[1]->mutable_data(), out->offset, 
out->length,
+                         false);
+      return;
+    }
+
+    InvertBitmap(arr.buffers[0]->data(), arr.offset, arr.length,
+                 out->buffers[1]->mutable_data(), out->offset);
+  }
+};
+}  // namespace
+
+namespace codegen {
+namespace {
+
+using arrow::compute::codegen::SimpleUnary;

Review comment:
       This using not needed

##########
File path: cpp/src/arrow/compute/kernels/scalar_validity.cc
##########
@@ -0,0 +1,110 @@
+// 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/kernels/common.h"
+
+#include "arrow/util/bit_util.h"
+#include "arrow/util/bitmap_ops.h"
+
+namespace arrow {
+
+using internal::CopyBitmap;
+using internal::InvertBitmap;
+
+namespace compute {
+namespace {
+
+struct IsValidOperator {
+  static void Call(KernelContext* ctx, const Scalar& in, Scalar* out) {
+    checked_cast<BooleanScalar*>(out)->value = in.is_valid;
+  }
+
+  static void Call(KernelContext* ctx, const ArrayData& arr, ArrayData* out) {
+    if (arr.buffers[0] != nullptr && out->offset == arr.offset &&
+        out->length == arr.length) {

Review comment:
       I don't think that the offset and length checks are needed anymore, you 
can either remove them or make them DCHECK

##########
File path: cpp/src/arrow/compute/kernels/scalar_validity_test.cc
##########
@@ -0,0 +1,78 @@
+// 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 <gtest/gtest.h>
+
+#include "arrow/array.h"
+#include "arrow/compute/api.h"
+#include "arrow/compute/kernels/test_util.h"
+#include "arrow/testing/gtest_common.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/random.h"
+#include "arrow/type.h"
+#include "arrow/type_traits.h"
+#include "arrow/util/bitmap_reader.h"
+#include "arrow/util/checked_cast.h"
+
+namespace arrow {
+namespace compute {
+
+class TestValidityKernels : public ::testing::Test {
+ protected:
+  // XXX Since IsValid and IsNull don't touch any buffers but the null bitmap
+  // testing multiple types seems redundant.
+  using ArrowType = BooleanType;
+
+  static std::shared_ptr<DataType> type_singleton() {
+    return TypeTraits<ArrowType>::type_singleton();
+  }
+};
+
+TEST_F(TestValidityKernels, ArrayIsValid) {
+  CheckScalarUnary("is_valid", type_singleton(), "[]", type_singleton(), "[]");
+  CheckScalarUnary("is_valid", type_singleton(), "[null]", type_singleton(), 
"[false]");
+  CheckScalarUnary("is_valid", type_singleton(), "[1]", type_singleton(), 
"[true]");
+  CheckScalarUnary("is_valid", type_singleton(), "[null, 1, 0, null]", 
type_singleton(),
+                   "[false, true, true, false]");
+}
+
+TEST_F(TestValidityKernels, ArrayIsValidBufferPassthruOptimization) {
+  Datum arg = ArrayFromJSON(boolean(), "[null, 1, 0, null]");
+  ASSERT_OK_AND_ASSIGN(auto validity, arrow::compute::IsValid(arg));
+  ASSERT_EQ(validity.array()->buffers[1], arg.array()->buffers[0]);
+}
+
+TEST_F(TestValidityKernels, ScalarIsValid) {
+  CheckScalarUnary("is_valid", MakeScalar(19.7), MakeScalar(true));
+  CheckScalarUnary("is_valid", MakeNullScalar(float64()), MakeScalar(false));
+}
+
+TEST_F(TestValidityKernels, ArrayIsNull) {
+  CheckScalarUnary("is_null", type_singleton(), "[]", type_singleton(), "[]");
+  CheckScalarUnary("is_null", type_singleton(), "[null]", type_singleton(), 
"[true]");
+  CheckScalarUnary("is_null", type_singleton(), "[1]", type_singleton(), 
"[false]");
+  CheckScalarUnary("is_null", type_singleton(), "[null, 1, 0, null]", 
type_singleton(),
+                   "[true, false, false, true]");
+}
+
+TEST_F(TestValidityKernels, ScalarIsNull) {
+  CheckScalarUnary("is_null", MakeScalar(19.7), MakeScalar(false));
+  CheckScalarUnary("is_null", MakeNullScalar(float64()), MakeScalar(true));
+}

Review comment:
       Can you add a sliced-array check to CheckScalarUnary to sure the offset 
!= 0 case is handled?

##########
File path: cpp/src/arrow/testing/random.cc
##########
@@ -262,5 +262,62 @@ std::shared_ptr<Array> 
RandomArrayGenerator::Offsets(int64_t size, int32_t first
   return std::make_shared<Int32Array>(array_data);
 }
 
+struct RandomArrayGeneratorOfImpl {
+  Status Visit(const NullType&) {
+    DCHECK_NE(null_probability_, 0.0);

Review comment:
       This check can perhaps be dropped, I could see this being an issue if 
you were running an array of null probabilities across some type parameters

##########
File path: cpp/src/arrow/compute/kernels/scalar_validity.cc
##########
@@ -0,0 +1,110 @@
+// 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/kernels/common.h"
+
+#include "arrow/util/bit_util.h"
+#include "arrow/util/bitmap_ops.h"
+
+namespace arrow {
+
+using internal::CopyBitmap;
+using internal::InvertBitmap;
+
+namespace compute {
+namespace {
+
+struct IsValidOperator {
+  static void Call(KernelContext* ctx, const Scalar& in, Scalar* out) {
+    checked_cast<BooleanScalar*>(out)->value = in.is_valid;
+  }
+
+  static void Call(KernelContext* ctx, const ArrayData& arr, ArrayData* out) {
+    if (arr.buffers[0] != nullptr && out->offset == arr.offset &&
+        out->length == arr.length) {
+      out->buffers[1] = arr.buffers[0];
+      return;
+    }
+
+    KERNEL_RETURN_IF_ERROR(ctx, 
ctx->AllocateBitmap(out->length).Value(&out->buffers[1]));
+
+    if (arr.null_count == 0 || arr.buffers[0] == nullptr) {
+      BitUtil::SetBitsTo(out->buffers[1]->mutable_data(), out->offset, 
out->length, true);
+      return;
+    }
+
+    CopyBitmap(arr.buffers[0]->data(), arr.offset, arr.length,
+               out->buffers[1]->mutable_data(), out->offset);
+  }
+};
+
+struct IsNullOperator {
+  static void Call(KernelContext* ctx, const Scalar& in, Scalar* out) {
+    checked_cast<BooleanScalar*>(out)->value = !in.is_valid;
+  }
+
+  static void Call(KernelContext* ctx, const ArrayData& arr, ArrayData* out) {
+    if (arr.null_count == 0 || arr.buffers[0] == nullptr) {
+      BitUtil::SetBitsTo(out->buffers[1]->mutable_data(), out->offset, 
out->length,
+                         false);
+      return;
+    }
+
+    InvertBitmap(arr.buffers[0]->data(), arr.offset, arr.length,
+                 out->buffers[1]->mutable_data(), out->offset);
+  }
+};
+}  // namespace
+
+namespace codegen {

Review comment:
       Remove this unneeded namespace and just have the anonymous namespace 
enclosing all of the implementation




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to