bkietz commented on a change in pull request #8728:
URL: https://github.com/apache/arrow/pull/8728#discussion_r528793473
##########
File path: cpp/src/arrow/compute/kernels/scalar_boolean_test.cc
##########
@@ -27,185 +27,114 @@
#include "arrow/compute/kernels/test_util.h"
#include "arrow/testing/gtest_common.h"
#include "arrow/testing/gtest_util.h"
+#include "arrow/util/checked_cast.h"
namespace arrow {
+
+using internal::checked_pointer_cast;
+
namespace compute {
-using BinaryKernelFunc =
- std::function<Result<Datum>(const Datum&, const Datum&, ExecContext*)>;
-
-class TestBooleanKernel : public TestBase {
- public:
- void TestArrayBinary(const BinaryKernelFunc& kernel, const
std::shared_ptr<Array>& left,
- const std::shared_ptr<Array>& right,
- const std::shared_ptr<Array>& expected) {
- ASSERT_OK_AND_ASSIGN(Datum result, kernel(left, right, &ctx_));
- ASSERT_EQ(Datum::ARRAY, result.kind());
- std::shared_ptr<Array> result_array = result.make_array();
- ASSERT_OK(result_array->ValidateFull());
- AssertArraysEqual(*expected, *result_array, /*verbose=*/true);
-
- ASSERT_OK_AND_ASSIGN(result, kernel(right, left, &ctx_));
- ASSERT_EQ(Datum::ARRAY, result.kind());
- result_array = result.make_array();
- ASSERT_OK(result_array->ValidateFull());
- AssertArraysEqual(*expected, *result_array, /*verbose=*/true);
- }
+void CheckBooleanScalarArrayBinary(std::string func_name, Datum array) {
+ for (std::shared_ptr<Scalar> scalar :
+ {std::make_shared<BooleanScalar>(),
std::make_shared<BooleanScalar>(true),
+ std::make_shared<BooleanScalar>(false)}) {
+ ASSERT_OK_AND_ASSIGN(Datum actual, CallFunction(func_name, {Datum(scalar),
array}));
- void TestChunkedArrayBinary(const BinaryKernelFunc& kernel,
- const std::shared_ptr<ChunkedArray>& left,
- const std::shared_ptr<ChunkedArray>& right,
- const std::shared_ptr<ChunkedArray>& expected) {
- ASSERT_OK_AND_ASSIGN(Datum result, kernel(left, right, &ctx_));
- ASSERT_EQ(Datum::CHUNKED_ARRAY, result.kind());
- std::shared_ptr<ChunkedArray> result_ca = result.chunked_array();
- AssertChunkedEquivalent(*expected, *result_ca);
-
- ASSERT_OK_AND_ASSIGN(result, kernel(right, left, &ctx_));
- ASSERT_EQ(Datum::CHUNKED_ARRAY, result.kind());
- result_ca = result.chunked_array();
- AssertChunkedEquivalent(*expected, *result_ca);
- }
+ ASSERT_OK_AND_ASSIGN(auto constant_array,
+ MakeArrayFromScalar(*scalar, array.length()));
- void TestBinaryKernel(const BinaryKernelFunc& kernel,
- const std::shared_ptr<Array>& left,
- const std::shared_ptr<Array>& right,
- const std::shared_ptr<Array>& expected) {
- TestArrayBinary(kernel, left, right, expected);
- TestArrayBinary(kernel, left->Slice(1), right->Slice(1),
expected->Slice(1));
-
- // ChunkedArray
- auto cleft = std::make_shared<ChunkedArray>(ArrayVector{left,
left->Slice(1)});
- auto cright = std::make_shared<ChunkedArray>(ArrayVector{right,
right->Slice(1)});
- auto cexpected =
- std::make_shared<ChunkedArray>(ArrayVector{expected,
expected->Slice(1)});
- TestChunkedArrayBinary(kernel, cleft, cright, cexpected);
-
- // ChunkedArray with different chunks
- cleft = std::make_shared<ChunkedArray>(ArrayVector{
- left->Slice(0, 1), left->Slice(1), left->Slice(1, 1), left->Slice(2)});
- TestChunkedArrayBinary(kernel, cleft, cright, cexpected);
- }
+ ASSERT_OK_AND_ASSIGN(Datum expected,
+ CallFunction(func_name, {Datum(constant_array),
array}));
+ AssertDatumsEqual(expected, actual);
- void TestBinaryKernelPropagate(const BinaryKernelFunc& kernel,
- const std::vector<bool>& left,
- const std::vector<bool>& right,
- const std::vector<bool>& expected,
- const std::vector<bool>& expected_nulls) {
- auto type = boolean();
- TestBinaryKernel(kernel, _MakeArray<BooleanType, bool>(type, left, {}),
- _MakeArray<BooleanType, bool>(type, right, {}),
- _MakeArray<BooleanType, bool>(type, expected, {}));
-
- TestBinaryKernel(kernel, _MakeArray<BooleanType, bool>(type, left, left),
- _MakeArray<BooleanType, bool>(type, right, right),
- _MakeArray<BooleanType, bool>(type, expected,
expected_nulls));
+ ASSERT_OK_AND_ASSIGN(actual, CallFunction(func_name, {array,
Datum(scalar)}));
+ ASSERT_OK_AND_ASSIGN(expected,
+ CallFunction(func_name, {array,
Datum(constant_array)}));
+ AssertDatumsEqual(expected, actual);
}
-
- protected:
- ExecContext ctx_;
-};
-
-TEST_F(TestBooleanKernel, Invert) {
- std::vector<bool> values1 = {true, false, true, false};
- std::vector<bool> values2 = {false, true, false, true};
-
- auto type = boolean();
- auto a1 = _MakeArray<BooleanType, bool>(type, values1, {true, true, true,
false});
- auto a2 = _MakeArray<BooleanType, bool>(type, values2, {true, true, true,
false});
-
- // Plain array
- ASSERT_OK_AND_ASSIGN(Datum result, Invert(a1));
- ASSERT_EQ(Datum::ARRAY, result.kind());
- ASSERT_ARRAYS_EQUAL(*a2, *result.make_array());
-
- // Array with offset
- ASSERT_OK_AND_ASSIGN(result, Invert(a1->Slice(1)));
- ASSERT_EQ(Datum::ARRAY, result.kind());
- ASSERT_ARRAYS_EQUAL(*a2->Slice(1), *result.make_array());
-
- // ChunkedArray
- std::vector<std::shared_ptr<Array>> ca1_arrs = {a1, a1->Slice(1)};
- auto ca1 = std::make_shared<ChunkedArray>(ca1_arrs);
- std::vector<std::shared_ptr<Array>> ca2_arrs = {a2, a2->Slice(1)};
- auto ca2 = std::make_shared<ChunkedArray>(ca2_arrs);
- ASSERT_OK_AND_ASSIGN(result, Invert(ca1));
- ASSERT_EQ(Datum::CHUNKED_ARRAY, result.kind());
- std::shared_ptr<ChunkedArray> result_ca = result.chunked_array();
-
- // Contiguous preallocation, so a single output chunk even though there were
- // two input chunks
- ASSERT_EQ(1, result_ca->num_chunks());
- AssertChunkedEquivalent(*ca2, *result_ca);
}
-TEST_F(TestBooleanKernel, InvertEmptyArray) {
- std::vector<std::shared_ptr<Buffer>> data_buffers(2);
- Datum input;
- input.value = ArrayData::Make(boolean(), 0 /* length */,
std::move(data_buffers),
- 0 /* null_count */);
-
- ASSERT_OK_AND_ASSIGN(Datum result, Invert(input));
- ASSERT_ARRAYS_EQUAL(*input.make_array(), *result.make_array());
+TEST(TestBooleanKernel, Invert) {
+ auto arr =
+ ArrayFromJSON(boolean(), "[true, false, true, null, false, true, false,
null]");
+ auto expected =
+ ArrayFromJSON(boolean(), "[false, true, false, null, true, false, true,
null]");
+ CheckScalarUnary("invert", arr, expected);
}
-TEST_F(TestBooleanKernel, BinaryOpOnEmptyArray) {
- auto type = boolean();
- std::vector<std::shared_ptr<Buffer>> data_buffers(2);
- Datum input;
- input.value = ArrayData::Make(boolean(), 0 /* length */,
std::move(data_buffers),
- 0 /* null_count */);
-
- ASSERT_OK_AND_ASSIGN(Datum result, And(input, input));
- // Result should be empty as well.
- ASSERT_ARRAYS_EQUAL(*input.make_array(), *result.make_array());
+TEST(TestBooleanKernel, And) {
+ auto left = ArrayFromJSON(boolean(), " [true, true, true, false, false,
null]");
+ auto right = ArrayFromJSON(boolean(), " [true, false, null, false, null,
null]");
+ auto expected = ArrayFromJSON(boolean(), "[true, false, null, false, null,
null]");
+ // CheckScalarBinary("and", left, right, expected);
+ CheckBooleanScalarArrayBinary("and", left);
}
-TEST_F(TestBooleanKernel, And) {
- std::vector<bool> values1 = {true, false, true, false, true, true};
- std::vector<bool> values2 = {true, true, false, false, true, false};
- std::vector<bool> values3 = {true, false, false, false, true, false};
- TestBinaryKernelPropagate(And, values1, values2, values3, values3);
+TEST(TestBooleanKernel, Or) {
+ auto left = ArrayFromJSON(boolean(), " [true, true, true, false, false,
null]");
+ auto right = ArrayFromJSON(boolean(), " [true, false, null, false, null,
null]");
+ auto expected = ArrayFromJSON(boolean(), "[true, true, null, false, null,
null]");
+ CheckScalarBinary("or", left, right, expected);
+ CheckBooleanScalarArrayBinary("or", left);
}
-TEST_F(TestBooleanKernel, Or) {
- std::vector<bool> values1 = {true, false, true, false, true, true};
- std::vector<bool> values2 = {true, true, false, false, true, false};
- std::vector<bool> values3 = {true, true, true, false, true, true};
- std::vector<bool> values3_nulls = {true, false, false, false, true, false};
- TestBinaryKernelPropagate(Or, values1, values2, values3, values3_nulls);
+TEST(TestBooleanKernel, Xor) {
+ auto left = ArrayFromJSON(boolean(), " [true, true, true, false, false,
null]");
+ auto right = ArrayFromJSON(boolean(), " [true, false, null, false, null,
null]");
+ auto expected = ArrayFromJSON(boolean(), "[false, true, null, false, null,
null]");
+ CheckScalarBinary("xor", left, right, expected);
+ CheckBooleanScalarArrayBinary("xor", left);
}
-TEST_F(TestBooleanKernel, Xor) {
- std::vector<bool> values1 = {true, false, true, false, true, true};
- std::vector<bool> values2 = {true, true, false, false, true, false};
- std::vector<bool> values3 = {false, true, true, false, false, true};
- std::vector<bool> values3_nulls = {true, false, false, false, true, false};
- TestBinaryKernelPropagate(Xor, values1, values2, values3, values3_nulls);
+TEST(TestBooleanKernel, AndNot) {
+ auto left = ArrayFromJSON(
+ boolean(), "[true, true, true, false, false, false, null, null,
null]");
+ auto right = ArrayFromJSON(
+ boolean(), "[true, false, null, true, false, null, true, false,
null]");
+ auto expected = ArrayFromJSON(
+ boolean(), "[false, true, null, false, false, null, null, null,
null]");
+ CheckScalarBinary("and_not", left, right, expected);
+ CheckBooleanScalarArrayBinary("and_not", left);
}
-TEST_F(TestBooleanKernel, KleeneAnd) {
+TEST(TestBooleanKernel, KleeneAnd) {
auto left = ArrayFromJSON(boolean(), " [true, true, true, false, false,
null]");
auto right = ArrayFromJSON(boolean(), " [true, false, null, false, null,
null]");
auto expected = ArrayFromJSON(boolean(), "[true, false, null, false, false,
null]");
- TestBinaryKernel(KleeneAnd, left, right, expected);
+ CheckScalarBinary("and_kleene", left, right, expected);
+ CheckBooleanScalarArrayBinary("and_kleene", left);
left = ArrayFromJSON(boolean(), " [true, true, false, null, null]");
right = ArrayFromJSON(boolean(), " [true, false, false, true, false]");
expected = ArrayFromJSON(boolean(), "[true, false, false, null, false]");
- TestBinaryKernel(KleeneAnd, left, right, expected);
+ CheckScalarBinary("and_kleene", left, right, expected);
+ CheckBooleanScalarArrayBinary("and_kleene", left);
Review comment:
Ah, I see. Yes they're the same input values (the full truth table) just
with a the different call signatures
----------------------------------------------------------------
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]