1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
18 #include <gtest/gtest.h>
20 #include "arrow/array.h"
21 #include "arrow/compute/api.h"
22 #include "arrow/compute/kernels/test_util.h"
23 #include "arrow/testing/gtest_common.h"
24 #include "arrow/testing/gtest_util.h"
25 #include "arrow/testing/random.h"
26 #include "arrow/type.h"
27 #include "arrow/type_traits.h"
28 #include "arrow/util/bitmap_reader.h"
29 #include "arrow/util/checked_cast.h"
34 template <typename ArrowType
>
35 class TestValidityKernels
: public ::testing::Test
{
37 static std::shared_ptr
<DataType
> type_singleton() {
38 return TypeTraits
<ArrowType
>::type_singleton();
42 using TestBooleanValidityKernels
= TestValidityKernels
<BooleanType
>;
44 TEST_F(TestBooleanValidityKernels
, ArrayIsValid
) {
45 CheckScalarUnary("is_valid", type_singleton(), "[]", type_singleton(), "[]");
46 CheckScalarUnary("is_valid", type_singleton(), "[null]", type_singleton(), "[false]");
47 CheckScalarUnary("is_valid", type_singleton(), "[1]", type_singleton(), "[true]");
48 CheckScalarUnary("is_valid", type_singleton(), "[null, 1, 0, null]", type_singleton(),
49 "[false, true, true, false]");
52 TEST_F(TestBooleanValidityKernels
, ArrayIsValidBufferPassthruOptimization
) {
53 Datum arg
= ArrayFromJSON(boolean(), "[null, 1, 0, null]");
54 ASSERT_OK_AND_ASSIGN(auto validity
, arrow::compute::IsValid(arg
));
55 ASSERT_EQ(validity
.array()->buffers
[1], arg
.array()->buffers
[0]);
58 TEST_F(TestBooleanValidityKernels
, IsNull
) {
59 auto ty
= type_singleton();
60 NullOptions default_options
;
61 NullOptions
nan_is_null_options(/*nan_is_null=*/true);
63 CheckScalarUnary("is_null", ty
, "[]", boolean(), "[]");
64 CheckScalarUnary("is_null", ty
, "[]", boolean(), "[]", &default_options
);
65 CheckScalarUnary("is_null", ty
, "[]", boolean(), "[]", &nan_is_null_options
);
67 CheckScalarUnary("is_null", ty
, "[null]", boolean(), "[true]");
68 CheckScalarUnary("is_null", ty
, "[null]", boolean(), "[true]", &default_options
);
69 CheckScalarUnary("is_null", ty
, "[null]", boolean(), "[true]", &nan_is_null_options
);
71 CheckScalarUnary("is_null", ty
, "[1]", boolean(), "[false]");
72 CheckScalarUnary("is_null", ty
, "[1]", boolean(), "[false]", &default_options
);
73 CheckScalarUnary("is_null", ty
, "[1]", boolean(), "[false]", &nan_is_null_options
);
75 CheckScalarUnary("is_null", ty
, "[null, 1, 0, null]", boolean(),
76 "[true, false, false, true]");
77 CheckScalarUnary("is_null", ty
, "[null, 1, 0, null]", boolean(),
78 "[true, false, false, true]", &default_options
);
79 CheckScalarUnary("is_null", ty
, "[null, 1, 0, null]", boolean(),
80 "[true, false, false, true]", &nan_is_null_options
);
83 TEST(TestValidityKernels
, IsValidIsNullNullType
) {
84 CheckScalarUnary("is_null", std::make_shared
<NullArray
>(5),
85 ArrayFromJSON(boolean(), "[true, true, true, true, true]"));
86 CheckScalarUnary("is_valid", std::make_shared
<NullArray
>(5),
87 ArrayFromJSON(boolean(), "[false, false, false, false, false]"));
90 TEST(TestValidityKernels
, IsNullSetsZeroNullCount
) {
91 auto arr
= ArrayFromJSON(int32(), "[1, 2, 3, 4, null]");
92 ASSERT_OK_AND_ASSIGN(Datum out
, IsNull(arr
));
93 ASSERT_EQ(out
.array()->null_count
, 0);
96 template <typename ArrowType
>
97 class TestFloatingPointValidityKernels
: public TestValidityKernels
<ArrowType
> {
100 NullOptions default_options
;
101 NullOptions
nan_is_null_options(/*nan_is_null=*/true);
103 auto ty
= this->type_singleton();
104 auto arr
= ArrayFromJSON(ty
, "[]");
105 CheckScalarUnary("is_null", arr
, ArrayFromJSON(boolean(), "[]"));
106 CheckScalarUnary("is_null", arr
, ArrayFromJSON(boolean(), "[]"), &default_options
);
107 CheckScalarUnary("is_null", arr
, ArrayFromJSON(boolean(), "[]"),
108 &nan_is_null_options
);
111 arr
= ArrayFromJSON(ty
, "[1.5, 0.0, -0.0, Inf, -Inf, NaN]");
114 ArrayFromJSON(boolean(), "[false, false, false, false, false, false]"));
117 ArrayFromJSON(boolean(), "[false, false, false, false, false, false]"),
121 ArrayFromJSON(boolean(), "[false, false, false, false, false, true]"),
122 &nan_is_null_options
);
125 arr
= ArrayFromJSON(ty
, "[1.5, -0.0, null, Inf, -Inf, NaN]");
128 ArrayFromJSON(boolean(), "[false, false, true, false, false, false]"));
131 ArrayFromJSON(boolean(), "[false, false, true, false, false, false]"),
133 CheckScalarUnary("is_null", arr
,
134 ArrayFromJSON(boolean(), "[false, false, true, false, false, true]"),
135 &nan_is_null_options
);
138 arr
= ArrayFromJSON(ty
, "[null, null, null]");
139 CheckScalarUnary("is_null", arr
, ArrayFromJSON(boolean(), "[true, true, true]"));
140 CheckScalarUnary("is_null", arr
, ArrayFromJSON(boolean(), "[true, true, true]"),
142 CheckScalarUnary("is_null", arr
, ArrayFromJSON(boolean(), "[true, true, true]"),
143 &nan_is_null_options
);
146 void TestIsFinite() {
147 auto ty
= this->type_singleton();
148 CheckScalarUnary("is_finite", ArrayFromJSON(ty
, "[]"),
149 ArrayFromJSON(boolean(), "[]"));
152 CheckScalarUnary("is_finite", ArrayFromJSON(ty
, "[Inf, -Inf, Inf, -Inf, Inf]"),
153 ArrayFromJSON(boolean(), "[false, false, false, false, false]"));
155 CheckScalarUnary("is_finite", ArrayFromJSON(ty
, "[0.0, 1.0, 2.0, 3.0, NaN, null]"),
156 ArrayFromJSON(boolean(), "[true, true, true, true, false, null]"));
158 CheckScalarUnary("is_finite", ArrayFromJSON(ty
, "[0.0, Inf, 2.0, -Inf, NaN, null]"),
159 ArrayFromJSON(boolean(), "[true, false, true, false, false, null]"));
163 auto ty
= this->type_singleton();
164 CheckScalarUnary("is_inf", ArrayFromJSON(ty
, "[]"), ArrayFromJSON(boolean(), "[]"));
167 CheckScalarUnary("is_inf", ArrayFromJSON(ty
, "[Inf, -Inf, Inf, -Inf, Inf]"),
168 ArrayFromJSON(boolean(), "[true, true, true, true, true]"));
171 "is_inf", ArrayFromJSON(ty
, "[0.0, 1.0, 2.0, 3.0, NaN, null]"),
172 ArrayFromJSON(boolean(), "[false, false, false, false, false, null]"));
174 CheckScalarUnary("is_inf", ArrayFromJSON(ty
, "[0.0, Inf, 2.0, -Inf, NaN, null]"),
175 ArrayFromJSON(boolean(), "[false, true, false, true, false, null]"));
179 auto ty
= this->type_singleton();
180 CheckScalarUnary("is_nan", ArrayFromJSON(ty
, "[]"), ArrayFromJSON(boolean(), "[]"));
183 CheckScalarUnary("is_nan", ArrayFromJSON(ty
, "[NaN, NaN, NaN, NaN, NaN]"),
184 ArrayFromJSON(boolean(), "[true, true, true, true, true]"));
187 "is_nan", ArrayFromJSON(ty
, "[0.0, 1.0, 2.0, 3.0, Inf, null]"),
188 ArrayFromJSON(boolean(), "[false, false, false, false, false, null]"));
190 CheckScalarUnary("is_nan", ArrayFromJSON(ty
, "[0.0, NaN, 2.0, NaN, Inf, null]"),
191 ArrayFromJSON(boolean(), "[false, true, false, true, false, null]"));
195 TYPED_TEST_SUITE(TestFloatingPointValidityKernels
, RealArrowTypes
);
197 TYPED_TEST(TestFloatingPointValidityKernels
, IsNull
) { this->TestIsNull(); }
199 TYPED_TEST(TestFloatingPointValidityKernels
, IsFinite
) { this->TestIsFinite(); }
201 TYPED_TEST(TestFloatingPointValidityKernels
, IsInf
) { this->TestIsInf(); }
203 TYPED_TEST(TestFloatingPointValidityKernels
, IsNan
) { this->TestIsNan(); }
205 } // namespace compute