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



##########
File path: cpp/src/arrow/testing/random.h
##########
@@ -250,6 +250,9 @@ class ARROW_EXPORT RandomArrayGenerator {
                                            int32_t min_length, int32_t 
max_length,
                                            double null_probability = 0);
 
+  std::shared_ptr<Array> Of(std::shared_ptr<DataType> type, int64_t size,

Review comment:
       docstring

##########
File path: cpp/src/arrow/array/builder_base.cc
##########
@@ -111,4 +115,43 @@ void ArrayBuilder::UnsafeSetNull(int64_t length) {
   null_bitmap_builder_.UnsafeAppend(length, false);
 }
 
+struct ArrayBuilderAppendScalarImpl {
+  template <typename S,
+            typename B = typename TypeTraits<typename 
S::TypeClass>::BuilderType>
+  B& builder() {
+    return *internal::checked_cast<B*>(builder_);
+  }
+
+  template <typename S>
+  auto Visit(const S& scalar) -> decltype(builder<S>().Append(scalar.value)) {
+    return builder<S>().Append(scalar.value);
+  }
+
+  template <typename S>
+  enable_if_base_binary<typename S::TypeClass, Status> Visit(const S& scalar) {
+    return builder<S>().Append(scalar.value->data(), scalar.value->size());
+  }
+
+  Status Visit(const Scalar& scalar) {
+    return Status::NotImplemented("Appending scalars to builders of type ", 
*scalar.type);
+  }
+
+  Status Finish() && { return VisitScalarInline(scalar_, this); }
+  ArrayBuilder* builder_;
+  const Scalar& scalar_;
+};

Review comment:
       If this is just to enable the test harness created below I'm not sure 
it's worth it

##########
File path: cpp/src/arrow/compute/kernels/scalar_validity_test.cc
##########
@@ -0,0 +1,151 @@
+// 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;
+
+  using CType = typename ArrowType::c_type;
+
+  static std::shared_ptr<DataType> type_singleton() {
+    return TypeTraits<ArrowType>::type_singleton();
+  }
+
+  void AssertUnary(Datum arg, Datum expected) {
+    ASSERT_OK_AND_ASSIGN(auto actual, func_(arg, nullptr));
+    ASSERT_EQ(actual.kind(), expected.kind());
+    if (actual.kind() == Datum::ARRAY) {
+      ASSERT_OK(actual.make_array()->ValidateFull());
+      AssertArraysApproxEqual(*expected.make_array(), *actual.make_array());
+    } else {
+      AssertScalarsEqual(*expected.scalar(), *actual.scalar());
+    }
+  }
+
+  void AssertUnary(const std::string& arg_json, const std::string& 
expected_json) {
+    AssertUnary(ArrayFromJSON(type_singleton(), arg_json),
+                ArrayFromJSON(type_singleton(), expected_json));
+  }
+
+  using UnaryFunction = std::function<Result<Datum>(const Datum&, 
ExecContext*)>;
+  UnaryFunction func_;

Review comment:
       Is this meaningfully different from
   
   
https://github.com/apache/arrow/blob/master/cpp/src/arrow/compute/kernels/test_util.cc#L33?

##########
File path: cpp/src/arrow/compute/kernels/scalar_validity_test.cc
##########
@@ -0,0 +1,151 @@
+// 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;
+
+  using CType = typename ArrowType::c_type;
+
+  static std::shared_ptr<DataType> type_singleton() {
+    return TypeTraits<ArrowType>::type_singleton();
+  }
+
+  void AssertUnary(Datum arg, Datum expected) {
+    ASSERT_OK_AND_ASSIGN(auto actual, func_(arg, nullptr));
+    ASSERT_EQ(actual.kind(), expected.kind());
+    if (actual.kind() == Datum::ARRAY) {
+      ASSERT_OK(actual.make_array()->ValidateFull());
+      AssertArraysApproxEqual(*expected.make_array(), *actual.make_array());
+    } else {
+      AssertScalarsEqual(*expected.scalar(), *actual.scalar());
+    }
+  }
+
+  void AssertUnary(const std::string& arg_json, const std::string& 
expected_json) {
+    AssertUnary(ArrayFromJSON(type_singleton(), arg_json),
+                ArrayFromJSON(type_singleton(), expected_json));
+  }
+
+  using UnaryFunction = std::function<Result<Datum>(const Datum&, 
ExecContext*)>;
+  UnaryFunction func_;
+};
+
+TEST_F(TestValidityKernels, ArrayIsValid) {
+  func_ = arrow::compute::IsValid;
+
+  this->AssertUnary("[]", "[]");
+  this->AssertUnary("[null]", "[false]");
+  this->AssertUnary("[1]", "[true]");
+  this->AssertUnary("[null, 1, 0, null]", "[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) {
+  func_ = arrow::compute::IsValid;
+
+  AssertUnary(Datum(19.7), Datum(true));
+  AssertUnary(MakeNullScalar(float64()), Datum(false));
+}
+
+TEST_F(TestValidityKernels, ArrayIsNull) {
+  func_ = arrow::compute::IsNull;
+
+  this->AssertUnary("[]", "[]");
+  this->AssertUnary("[null]", "[true]");
+  this->AssertUnary("[1]", "[false]");
+  this->AssertUnary("[null, 1, 0, null]", "[true, false, false, true]");
+}
+
+TEST_F(TestValidityKernels, DISABLED_ScalarIsNull) {
+  func_ = arrow::compute::IsNull;
+
+  AssertUnary(Datum(19.7), Datum(false));
+  AssertUnary(MakeNullScalar(float64()), Datum(true));
+}
+
+class IsValidProperty : public ScalarFunctionPropertyMixin {
+ public:
+  std::shared_ptr<ScalarFunction> GetFunction() override {
+    return internal::checked_pointer_cast<ScalarFunction>(
+        *GetFunctionRegistry()->GetFunction("is_valid"));
+  }
+
+  Result<std::shared_ptr<Scalar>> Contract(const ScalarVector& args,
+                                           const FunctionOptions*) override {
+    return std::make_shared<BooleanScalar>(args[0]->is_valid);
+  }
+};
+
+TEST_P(IsValidProperty, TestIsValidProperty) { Validate(); }
+
+INSTANTIATE_TEST_SUITE_P(IsValidPropertyTest, IsValidProperty,
+                         ScalarFunctionPropertyTestParam::Values({
+                             {0, 0.0},
+                             {1, 0.0},
+                             {2, 0.0},
+                             {1024, 0.25},
+                         }));
+
+class IsNullProperty : public ScalarFunctionPropertyMixin {
+ public:
+  std::shared_ptr<ScalarFunction> GetFunction() override {
+    return internal::checked_pointer_cast<ScalarFunction>(
+        *GetFunctionRegistry()->GetFunction("is_null"));
+  }
+
+  Result<std::shared_ptr<Scalar>> Contract(const ScalarVector& args,
+                                           const FunctionOptions*) override {
+    return std::make_shared<BooleanScalar>(!args[0]->is_valid);
+  }
+};
+
+TEST_P(IsNullProperty, TestIsNullProperty) { Validate(); }
+
+INSTANTIATE_TEST_SUITE_P(IsNullPropertyTest, IsNullProperty,
+                         ScalarFunctionPropertyTestParam::Values({
+                             {0, 0.0},
+                             {1, 0.0},
+                             {2, 0.0},
+                             {1024, 0.25},
+                         }));

Review comment:
       Honestly this seems quite complex / opaque to me -- I am not going to be 
eager to write unit tests in this fashion. I would personally rather keep 
things as simple as possible.




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