]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/cpp/src/arrow/compute/kernels/scalar_validity_test.cc
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / src / arrow / compute / kernels / scalar_validity_test.cc
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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
16 // under the License.
17
18 #include <gtest/gtest.h>
19
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"
30
31 namespace arrow {
32 namespace compute {
33
34 template <typename ArrowType>
35 class TestValidityKernels : public ::testing::Test {
36 protected:
37 static std::shared_ptr<DataType> type_singleton() {
38 return TypeTraits<ArrowType>::type_singleton();
39 }
40 };
41
42 using TestBooleanValidityKernels = TestValidityKernels<BooleanType>;
43
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]");
50 }
51
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]);
56 }
57
58 TEST_F(TestBooleanValidityKernels, IsNull) {
59 auto ty = type_singleton();
60 NullOptions default_options;
61 NullOptions nan_is_null_options(/*nan_is_null=*/true);
62
63 CheckScalarUnary("is_null", ty, "[]", boolean(), "[]");
64 CheckScalarUnary("is_null", ty, "[]", boolean(), "[]", &default_options);
65 CheckScalarUnary("is_null", ty, "[]", boolean(), "[]", &nan_is_null_options);
66
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);
70
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);
74
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);
81 }
82
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]"));
88 }
89
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);
94 }
95
96 template <typename ArrowType>
97 class TestFloatingPointValidityKernels : public TestValidityKernels<ArrowType> {
98 public:
99 void TestIsNull() {
100 NullOptions default_options;
101 NullOptions nan_is_null_options(/*nan_is_null=*/true);
102
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);
109
110 // Without nulls
111 arr = ArrayFromJSON(ty, "[1.5, 0.0, -0.0, Inf, -Inf, NaN]");
112 CheckScalarUnary(
113 "is_null", arr,
114 ArrayFromJSON(boolean(), "[false, false, false, false, false, false]"));
115 CheckScalarUnary(
116 "is_null", arr,
117 ArrayFromJSON(boolean(), "[false, false, false, false, false, false]"),
118 &default_options);
119 CheckScalarUnary(
120 "is_null", arr,
121 ArrayFromJSON(boolean(), "[false, false, false, false, false, true]"),
122 &nan_is_null_options);
123
124 // With nulls
125 arr = ArrayFromJSON(ty, "[1.5, -0.0, null, Inf, -Inf, NaN]");
126 CheckScalarUnary(
127 "is_null", arr,
128 ArrayFromJSON(boolean(), "[false, false, true, false, false, false]"));
129 CheckScalarUnary(
130 "is_null", arr,
131 ArrayFromJSON(boolean(), "[false, false, true, false, false, false]"),
132 &default_options);
133 CheckScalarUnary("is_null", arr,
134 ArrayFromJSON(boolean(), "[false, false, true, false, false, true]"),
135 &nan_is_null_options);
136
137 // Only nulls
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]"),
141 &default_options);
142 CheckScalarUnary("is_null", arr, ArrayFromJSON(boolean(), "[true, true, true]"),
143 &nan_is_null_options);
144 }
145
146 void TestIsFinite() {
147 auto ty = this->type_singleton();
148 CheckScalarUnary("is_finite", ArrayFromJSON(ty, "[]"),
149 ArrayFromJSON(boolean(), "[]"));
150
151 // All Inf
152 CheckScalarUnary("is_finite", ArrayFromJSON(ty, "[Inf, -Inf, Inf, -Inf, Inf]"),
153 ArrayFromJSON(boolean(), "[false, false, false, false, false]"));
154 // No Inf
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]"));
157 // Some Inf
158 CheckScalarUnary("is_finite", ArrayFromJSON(ty, "[0.0, Inf, 2.0, -Inf, NaN, null]"),
159 ArrayFromJSON(boolean(), "[true, false, true, false, false, null]"));
160 }
161
162 void TestIsInf() {
163 auto ty = this->type_singleton();
164 CheckScalarUnary("is_inf", ArrayFromJSON(ty, "[]"), ArrayFromJSON(boolean(), "[]"));
165
166 // All Inf
167 CheckScalarUnary("is_inf", ArrayFromJSON(ty, "[Inf, -Inf, Inf, -Inf, Inf]"),
168 ArrayFromJSON(boolean(), "[true, true, true, true, true]"));
169 // No Inf
170 CheckScalarUnary(
171 "is_inf", ArrayFromJSON(ty, "[0.0, 1.0, 2.0, 3.0, NaN, null]"),
172 ArrayFromJSON(boolean(), "[false, false, false, false, false, null]"));
173 // Some Inf
174 CheckScalarUnary("is_inf", ArrayFromJSON(ty, "[0.0, Inf, 2.0, -Inf, NaN, null]"),
175 ArrayFromJSON(boolean(), "[false, true, false, true, false, null]"));
176 }
177
178 void TestIsNan() {
179 auto ty = this->type_singleton();
180 CheckScalarUnary("is_nan", ArrayFromJSON(ty, "[]"), ArrayFromJSON(boolean(), "[]"));
181
182 // All NaN
183 CheckScalarUnary("is_nan", ArrayFromJSON(ty, "[NaN, NaN, NaN, NaN, NaN]"),
184 ArrayFromJSON(boolean(), "[true, true, true, true, true]"));
185 // No NaN
186 CheckScalarUnary(
187 "is_nan", ArrayFromJSON(ty, "[0.0, 1.0, 2.0, 3.0, Inf, null]"),
188 ArrayFromJSON(boolean(), "[false, false, false, false, false, null]"));
189 // Some NaNs
190 CheckScalarUnary("is_nan", ArrayFromJSON(ty, "[0.0, NaN, 2.0, NaN, Inf, null]"),
191 ArrayFromJSON(boolean(), "[false, true, false, true, false, null]"));
192 }
193 };
194
195 TYPED_TEST_SUITE(TestFloatingPointValidityKernels, RealArrowTypes);
196
197 TYPED_TEST(TestFloatingPointValidityKernels, IsNull) { this->TestIsNull(); }
198
199 TYPED_TEST(TestFloatingPointValidityKernels, IsFinite) { this->TestIsFinite(); }
200
201 TYPED_TEST(TestFloatingPointValidityKernels, IsInf) { this->TestIsInf(); }
202
203 TYPED_TEST(TestFloatingPointValidityKernels, IsNan) { this->TestIsNan(); }
204
205 } // namespace compute
206 } // namespace arrow