1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
// 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 <cmath>
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/compute/api_scalar.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/compute/kernels/common_internal.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/bit_util.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/bitmap_ops.h"
namespace arrow20 {
using internal::CopyBitmap;
using internal::InvertBitmap;
namespace compute {
namespace internal {
namespace {
Status IsValidExec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
const ArraySpan& arr = batch[0].array;
ArraySpan* out_span = out->array_span_mutable();
if (arr.type->id() == Type::NA) {
// Input is all nulls => output is entirely false.
bit_util::SetBitsTo(out_span->buffers[1].data, out_span->offset, out_span->length,
false);
return Status::OK();
}
DCHECK_EQ(out_span->offset, 0);
DCHECK_LE(out_span->length, arr.length);
if (arr.MayHaveNulls()) {
// We could do a zero-copy optimization, but it isn't worth the added complexity
::arrow20::internal::CopyBitmap(arr.buffers[0].data, arr.offset, arr.length,
out_span->buffers[1].data, out_span->offset);
} else {
// Input has no nulls => output is entirely true.
bit_util::SetBitsTo(out_span->buffers[1].data, out_span->offset, out_span->length,
true);
}
return Status::OK();
}
struct IsFiniteOperator {
template <typename OutType, typename InType>
static constexpr OutType Call(KernelContext*, const InType& value, Status*) {
return std::isfinite(value);
}
};
struct IsInfOperator {
template <typename OutType, typename InType>
static constexpr OutType Call(KernelContext*, const InType& value, Status*) {
return std::isinf(value);
}
};
using NanOptionsState = OptionsWrapper<NullOptions>;
template <typename T>
static void SetNanBits(const ArraySpan& arr, uint8_t* out_bitmap, int64_t out_offset) {
const T* data = arr.GetValues<T>(1);
for (int64_t i = 0; i < arr.length; ++i) {
if (std::isnan(data[i])) {
bit_util::SetBit(out_bitmap, i + out_offset);
}
}
}
Status IsNullExec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
const ArraySpan& arr = batch[0].array;
ArraySpan* out_span = out->array_span_mutable();
if (arr.type->id() == Type::NA) {
bit_util::SetBitsTo(out_span->buffers[1].data, out_span->offset, out_span->length,
true);
return Status::OK();
}
const auto& options = NanOptionsState::Get(ctx);
uint8_t* out_bitmap = out_span->buffers[1].data;
if (arr.GetNullCount() > 0) {
// Input has nulls => output is the inverted null (validity) bitmap.
InvertBitmap(arr.buffers[0].data, arr.offset, arr.length, out_bitmap,
out_span->offset);
} else {
// Input has no nulls => output is entirely false.
bit_util::SetBitsTo(out_bitmap, out_span->offset, out_span->length, false);
}
if (is_floating(arr.type->id()) && options.nan_is_null) {
switch (arr.type->id()) {
case Type::FLOAT:
SetNanBits<float>(arr, out_bitmap, out_span->offset);
break;
case Type::DOUBLE:
SetNanBits<double>(arr, out_bitmap, out_span->offset);
break;
default:
return Status::NotImplemented("NaN detection not implemented for type ",
arr.type->ToString());
}
}
return Status::OK();
}
struct IsNanOperator {
template <typename OutType, typename InType>
static constexpr OutType Call(KernelContext*, const InType& value, Status*) {
return std::isnan(value);
}
};
void MakeFunction(std::string name, FunctionDoc doc, std::vector<InputType> in_types,
OutputType out_type, ArrayKernelExec exec, FunctionRegistry* registry,
NullHandling::type null_handling, bool can_write_into_slices,
const FunctionOptions* default_options = NULLPTR,
KernelInit init = NULLPTR) {
Arity arity{static_cast<int>(in_types.size())};
auto func =
std::make_shared<ScalarFunction>(name, arity, std::move(doc), default_options);
ScalarKernel kernel(std::move(in_types), out_type, exec, init);
kernel.null_handling = null_handling;
kernel.can_write_into_slices = can_write_into_slices;
DCHECK_OK(func->AddKernel(std::move(kernel)));
DCHECK_OK(registry->AddFunction(std::move(func)));
}
template <typename InType, typename Op>
void AddFloatValidityKernel(const std::shared_ptr<DataType>& ty, ScalarFunction* func) {
DCHECK_OK(func->AddKernel({ty}, boolean(),
applicator::ScalarUnary<BooleanType, InType, Op>::Exec));
}
template <bool kConstant>
Status ConstBoolExec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
ArraySpan* array = out->array_span_mutable();
bit_util::SetBitsTo(array->buffers[1].data, array->offset, array->length, kConstant);
return Status::OK();
}
std::shared_ptr<ScalarFunction> MakeIsFiniteFunction(std::string name, FunctionDoc doc) {
auto func = std::make_shared<ScalarFunction>(name, Arity::Unary(), std::move(doc));
AddFloatValidityKernel<FloatType, IsFiniteOperator>(float32(), func.get());
AddFloatValidityKernel<DoubleType, IsFiniteOperator>(float64(), func.get());
for (const auto& ty : IntTypes()) {
DCHECK_OK(func->AddKernel({InputType(ty->id())}, boolean(), ConstBoolExec<true>));
}
DCHECK_OK(func->AddKernel({InputType(Type::NA)}, boolean(), ConstBoolExec<true>));
DCHECK_OK(
func->AddKernel({InputType(Type::DECIMAL128)}, boolean(), ConstBoolExec<true>));
DCHECK_OK(
func->AddKernel({InputType(Type::DECIMAL256)}, boolean(), ConstBoolExec<true>));
DCHECK_OK(func->AddKernel({InputType(Type::DURATION)}, boolean(), ConstBoolExec<true>));
return func;
}
std::shared_ptr<ScalarFunction> MakeIsInfFunction(std::string name, FunctionDoc doc) {
auto func = std::make_shared<ScalarFunction>(name, Arity::Unary(), std::move(doc));
AddFloatValidityKernel<FloatType, IsInfOperator>(float32(), func.get());
AddFloatValidityKernel<DoubleType, IsInfOperator>(float64(), func.get());
for (const auto& ty : IntTypes()) {
DCHECK_OK(func->AddKernel({InputType(ty->id())}, boolean(), ConstBoolExec<false>));
}
DCHECK_OK(func->AddKernel({InputType(Type::NA)}, boolean(), ConstBoolExec<false>));
DCHECK_OK(
func->AddKernel({InputType(Type::DECIMAL128)}, boolean(), ConstBoolExec<false>));
DCHECK_OK(
func->AddKernel({InputType(Type::DECIMAL256)}, boolean(), ConstBoolExec<false>));
DCHECK_OK(
func->AddKernel({InputType(Type::DURATION)}, boolean(), ConstBoolExec<false>));
return func;
}
std::shared_ptr<ScalarFunction> MakeIsNanFunction(std::string name, FunctionDoc doc) {
auto func = std::make_shared<ScalarFunction>(name, Arity::Unary(), std::move(doc));
AddFloatValidityKernel<FloatType, IsNanOperator>(float32(), func.get());
AddFloatValidityKernel<DoubleType, IsNanOperator>(float64(), func.get());
for (const auto& ty : IntTypes()) {
DCHECK_OK(func->AddKernel({InputType(ty->id())}, boolean(), ConstBoolExec<false>));
}
DCHECK_OK(func->AddKernel({InputType(Type::NA)}, boolean(), ConstBoolExec<false>));
DCHECK_OK(
func->AddKernel({InputType(Type::DECIMAL128)}, boolean(), ConstBoolExec<false>));
DCHECK_OK(
func->AddKernel({InputType(Type::DECIMAL256)}, boolean(), ConstBoolExec<false>));
DCHECK_OK(
func->AddKernel({InputType(Type::DURATION)}, boolean(), ConstBoolExec<false>));
return func;
}
Status TrueUnlessNullExec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
ArraySpan* out_span = out->array_span_mutable();
if (out_span->buffers[0].data) {
// If there is a validity bitmap computed above the kernel
// invocation, we copy it to the output buffers
::arrow20::internal::CopyBitmap(out_span->buffers[0].data, out_span->offset,
out_span->length, out_span->buffers[1].data,
out_span->offset);
} else {
// But for all-valid inputs, the engine will skip allocating a
// validity bitmap, so we set everything to true
bit_util::SetBitsTo(out_span->buffers[1].data, out_span->offset, out_span->length,
true);
}
return Status::OK();
}
const FunctionDoc is_valid_doc(
"Return true if non-null",
("For each input value, emit true iff the value is valid (i.e. non-null)."),
{"values"});
const FunctionDoc is_finite_doc(
"Return true if value is finite",
("For each input value, emit true iff the value is finite\n"
"(i.e. neither NaN, inf, nor -inf)."),
{"values"});
const FunctionDoc is_inf_doc(
"Return true if infinity",
("For each input value, emit true iff the value is infinite (inf or -inf)."),
{"values"});
const FunctionDoc is_null_doc(
"Return true if null (and optionally NaN)",
("For each input value, emit true iff the value is null.\n"
"True may also be emitted for NaN values by setting the `nan_is_null` flag."),
{"values"}, "NullOptions");
const FunctionDoc true_unless_null_doc("Return true if non-null, else return null",
("For each input value, emit true iff the value\n"
"is valid (non-null), otherwise emit null."),
{"values"});
const FunctionDoc is_nan_doc("Return true if NaN",
("For each input value, emit true iff the value is NaN."),
{"values"});
} // namespace
void RegisterScalarValidity(FunctionRegistry* registry) {
static auto kNullOptions = NullOptions::Defaults();
MakeFunction("is_valid", is_valid_doc, {InputType::Any()}, boolean(), IsValidExec,
registry, NullHandling::OUTPUT_NOT_NULL,
/*can_write_into_slices=*/false);
MakeFunction("is_null", is_null_doc, {InputType::Any()}, boolean(), IsNullExec,
registry, NullHandling::OUTPUT_NOT_NULL,
/*can_write_into_slices=*/true, &kNullOptions, NanOptionsState::Init);
MakeFunction("true_unless_null", true_unless_null_doc, {InputType::Any()}, boolean(),
TrueUnlessNullExec, registry, NullHandling::INTERSECTION,
/*can_write_into_slices=*/false);
DCHECK_OK(registry->AddFunction(MakeIsFiniteFunction("is_finite", is_finite_doc)));
DCHECK_OK(registry->AddFunction(MakeIsInfFunction("is_inf", is_inf_doc)));
DCHECK_OK(registry->AddFunction(MakeIsNanFunction("is_nan", is_nan_doc)));
}
} // namespace internal
} // namespace compute
} // namespace arrow20
|