]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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 "arrow/compute/kernels/util_internal.h" | |
19 | ||
20 | #include <cstdint> | |
21 | ||
22 | #include "arrow/array/data.h" | |
23 | #include "arrow/type.h" | |
24 | #include "arrow/util/checked_cast.h" | |
25 | ||
26 | namespace arrow { | |
27 | ||
28 | using internal::checked_cast; | |
29 | ||
30 | namespace compute { | |
31 | namespace internal { | |
32 | ||
33 | const uint8_t* GetValidityBitmap(const ArrayData& data) { | |
34 | const uint8_t* bitmap = nullptr; | |
35 | if (data.buffers[0]) { | |
36 | bitmap = data.buffers[0]->data(); | |
37 | } | |
38 | return bitmap; | |
39 | } | |
40 | ||
41 | int GetBitWidth(const DataType& type) { | |
42 | return checked_cast<const FixedWidthType&>(type).bit_width(); | |
43 | } | |
44 | ||
45 | PrimitiveArg GetPrimitiveArg(const ArrayData& arr) { | |
46 | PrimitiveArg arg; | |
47 | arg.is_valid = GetValidityBitmap(arr); | |
48 | arg.data = arr.buffers[1]->data(); | |
49 | arg.bit_width = GetBitWidth(*arr.type); | |
50 | arg.offset = arr.offset; | |
51 | arg.length = arr.length; | |
52 | if (arg.bit_width > 1) { | |
53 | arg.data += arr.offset * arg.bit_width / 8; | |
54 | } | |
55 | // This may be kUnknownNullCount | |
56 | arg.null_count = (arg.is_valid != nullptr) ? arr.null_count.load() : 0; | |
57 | return arg; | |
58 | } | |
59 | ||
60 | ArrayKernelExec TrivialScalarUnaryAsArraysExec(ArrayKernelExec exec, | |
61 | NullHandling::type null_handling) { | |
62 | return [=](KernelContext* ctx, const ExecBatch& batch, Datum* out) -> Status { | |
63 | if (out->is_array()) { | |
64 | return exec(ctx, batch, out); | |
65 | } | |
66 | ||
67 | if (null_handling == NullHandling::INTERSECTION && !batch[0].scalar()->is_valid) { | |
68 | out->scalar()->is_valid = false; | |
69 | return Status::OK(); | |
70 | } | |
71 | ||
72 | ARROW_ASSIGN_OR_RAISE(Datum array_in, MakeArrayFromScalar(*batch[0].scalar(), 1)); | |
73 | ARROW_ASSIGN_OR_RAISE(Datum array_out, MakeArrayFromScalar(*out->scalar(), 1)); | |
74 | RETURN_NOT_OK(exec(ctx, ExecBatch{{std::move(array_in)}, 1}, &array_out)); | |
75 | ARROW_ASSIGN_OR_RAISE(*out, array_out.make_array()->GetScalar(0)); | |
76 | return Status::OK(); | |
77 | }; | |
78 | } | |
79 | ||
80 | } // namespace internal | |
81 | } // namespace compute | |
82 | } // namespace arrow |