lidavidm commented on a change in pull request #10410: URL: https://github.com/apache/arrow/pull/10410#discussion_r641643738
########## File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc ########## @@ -0,0 +1,572 @@ +// 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 <arrow/compute/api.h> +#include <arrow/compute/kernels/codegen_internal.h> +#include <arrow/compute/util_internal.h> +#include <arrow/util/bit_block_counter.h> +#include <arrow/util/bitmap.h> +#include <arrow/util/bitmap_ops.h> + +namespace arrow { +using internal::BitBlockCount; +using internal::BitBlockCounter; +using internal::Bitmap; + +namespace compute { + +namespace { + +util::optional<uint64_t> GetConstantValidityWord(const Datum& data) { + if (data.is_scalar()) { + return data.scalar()->is_valid ? ~uint64_t(0) : uint64_t(0); + } + + if (data.array()->null_count == data.array()->length) return uint64_t(0); + + if (!data.array()->MayHaveNulls()) return ~uint64_t(0); + + // no constant validity word available + return {}; +} + +inline Bitmap GetBitmap(const Datum& datum, int i) { + if (datum.is_scalar()) return {}; + const ArrayData& a = *datum.array(); + return Bitmap{a.buffers[i], a.offset, a.length}; +} + +// if the condition is null then output is null otherwise we take validity from the +// selected argument +// ie. cond.valid & (cond.data & left.valid | ~cond.data & right.valid) +Status PromoteNullsVisitor(KernelContext* ctx, const Datum& cond_d, const Datum& left_d, + const Datum& right_d, ArrayData* output) { + auto cond_const = GetConstantValidityWord(cond_d); + auto left_const = GetConstantValidityWord(left_d); + auto right_const = GetConstantValidityWord(right_d); + + enum { COND_CONST = 1, LEFT_CONST = 2, RIGHT_CONST = 4 }; + auto flag = COND_CONST * cond_const.has_value() | LEFT_CONST * left_const.has_value() | + RIGHT_CONST * right_const.has_value(); + + const ArrayData& cond = *cond_d.array(); + // cond.data will always be available + Bitmap cond_data{cond.buffers[1], cond.offset, cond.length}; + Bitmap cond_valid{cond.buffers[0], cond.offset, cond.length}; + Bitmap left_valid = GetBitmap(left_d, 0); + Bitmap right_valid = GetBitmap(right_d, 0); + // sometimes Bitmaps will be ignored, in which case we replace access to them with + // duplicated (probably elided) access to cond_data + const Bitmap& _ = cond_data; + + // lambda function that will be used inside the visitor + uint64_t* out_validity = nullptr; + int64_t i = 0; + auto apply = [&](uint64_t c_valid, uint64_t c_data, uint64_t l_valid, + uint64_t r_valid) { + out_validity[i] = c_valid & ((c_data & l_valid) | (~c_data & r_valid)); + i++; + }; + + // if the condition is null then output is null otherwise we take validity from the + // selected argument + // ie. cond.valid & (cond.data & left.valid | ~cond.data & right.valid) + + // In the following cases, we dont need to allocate out_valid bitmap + switch (flag) { + case COND_CONST | LEFT_CONST | RIGHT_CONST: + // if cond & left & right all ones, then output is all valid --> out_valid = nullptr Review comment: Sorry about the confusion - can we explicitly put `// fallthrough` at the end of this case? -- 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]
