lidavidm commented on a change in pull request #10538:
URL: https://github.com/apache/arrow/pull/10538#discussion_r662573252
##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc
##########
@@ -455,6 +602,302 @@ struct IfElseFunctor<Type, enable_if_number<Type>> {
}
};
+template <typename Type>
+struct IfElseFunctor<Type, enable_if_base_binary<Type>> {
+ using OffsetType = typename TypeTraits<Type>::OffsetType::c_type;
+ using ArrayType = typename TypeTraits<Type>::ArrayType;
+
+ // A - Array, S - Scalar, X = Array/Scalar
+
+ // SXX
+ static Status Call(KernelContext* ctx, const BooleanScalar& cond, const
Datum& left,
+ const Datum& right, Datum* out) {
+ if (left.is_scalar() && right.is_scalar()) {
+ if (cond.is_valid) {
+ *out = cond.value ? left.scalar() : right.scalar();
+ } else {
+ *out = MakeNullScalar(left.type());
+ }
+ return Status::OK();
+ }
+ // either left or right is an array. Output is always an array
+ int64_t out_arr_len = std::max(left.length(), right.length());
+ if (!cond.is_valid) {
+ // cond is null; just create a null array
+ ARROW_ASSIGN_OR_RAISE(*out,
+ MakeArrayOfNull(left.type(), out_arr_len,
ctx->memory_pool()))
+ return Status::OK();
+ }
+
+ const auto& valid_data = cond.value ? left : right;
+ if (valid_data.is_array()) {
+ *out = valid_data;
+ } else {
+ // valid data is a scalar that needs to be broadcasted
+ ARROW_ASSIGN_OR_RAISE(*out, MakeArrayFromScalar(*valid_data.scalar(),
out_arr_len,
+ ctx->memory_pool()));
+ }
+ return Status::OK();
+ }
+
+ // AAA
+ static Status Call(KernelContext* ctx, const ArrayData& cond, const
ArrayData& left,
+ const ArrayData& right, ArrayData* out) {
+ const uint8_t* cond_data = cond.buffers[1]->data();
+ BitBlockCounter bit_counter(cond_data, cond.offset, cond.length);
+
+ const auto* left_offsets = left.GetValues<OffsetType>(1);
+ const uint8_t* left_data = left.buffers[2]->data();
+ const auto* right_offsets = right.GetValues<OffsetType>(1);
+ const uint8_t* right_data = right.buffers[2]->data();
+
+ // reserve an additional space
+ ARROW_ASSIGN_OR_RAISE(auto out_offset_buf,
+ ctx->Allocate((cond.length + 1) *
sizeof(OffsetType)));
+ auto* out_offsets =
reinterpret_cast<OffsetType*>(out_offset_buf->mutable_data());
+ out_offsets[0] = 0;
+
+ // allocate data buffer conservatively
+ int64_t data_buff_alloc = std::max(left_offsets[left.length] -
left_offsets[0],
+ right_offsets[right.length] -
right_offsets[0]);
+ ARROW_ASSIGN_OR_RAISE(std::shared_ptr<ResizableBuffer> out_data_buf,
+ ctx->Allocate(data_buff_alloc));
+ uint8_t* out_data = out_data_buf->mutable_data();
+
+ RunIfElseLoop(
+ cond,
+ [&](int64_t offset, int64_t length) { // from left bulk
+ auto bytes_written = left_offsets[offset + length] -
left_offsets[offset];
+ std::memcpy(out_data + out_offsets[offset], left_data +
left_offsets[offset],
+ bytes_written);
+ // normalize the out_offsets by reducing input start offset, and
adding the
+ // offset upto the word
+ std::transform(left_offsets + offset + 1, left_offsets + offset +
length + 1,
+ out_offsets + offset + 1, [&](const OffsetType&
src_offset) {
+ return src_offset - left_offsets[offset] +
out_offsets[offset];
+ });
+ },
+ [&](int64_t offset, int64_t length) { // from right bulk
+ auto bytes_written = right_offsets[offset + length] -
right_offsets[offset];
+ std::memcpy(out_data + out_offsets[offset], right_data +
right_offsets[offset],
+ bytes_written);
+ // normalize the out_offsets by reducing input start offset, and
adding the
+ // offset upto the word
+ std::transform(right_offsets + offset + 1, right_offsets + offset +
length + 1,
+ out_offsets + offset + 1, [&](const OffsetType&
src_offset) {
+ return src_offset - right_offsets[offset] +
+ out_offsets[offset];
+ });
+ },
+ [&](int64_t offset) { // left each
+ auto bytes_written = left_offsets[offset + 1] - left_offsets[offset];
+ std::memcpy(out_data + out_offsets[offset], left_data +
left_offsets[offset],
+ bytes_written);
+ out_offsets[offset + 1] = out_offsets[offset] + bytes_written;
+ },
+ [&](int64_t offset) { // right each
+ auto bytes_written = right_offsets[offset + 1] -
right_offsets[offset];
+ std::memcpy(out_data + out_offsets[offset], right_data +
right_offsets[offset],
+ bytes_written);
+ out_offsets[offset + 1] = out_offsets[offset] + bytes_written;
+ });
+ // resize the data buffer
+ ARROW_RETURN_NOT_OK(out_data_buf->Resize(out_offsets[cond.length]));
+
+ out->buffers[1] = std::move(out_offset_buf);
+ out->buffers[2] = std::move(out_data_buf);
+ return Status::OK();
+ }
+
+ // ASA
+ static Status Call(KernelContext* ctx, const ArrayData& cond, const Scalar&
left,
+ const ArrayData& right, ArrayData* out) {
+ const uint8_t* cond_data = cond.buffers[1]->data();
+ BitBlockCounter bit_counter(cond_data, cond.offset, cond.length);
+
+ util::string_view left_data = internal::UnboxScalar<Type>::Unbox(left);
+ size_t left_size = left_data.size();
+
+ const auto* right_offsets = right.GetValues<OffsetType>(1);
+ const uint8_t* right_data = right.buffers[2]->data();
+
+ // reserve an additional space
+ ARROW_ASSIGN_OR_RAISE(auto out_offset_buf,
+ ctx->Allocate((cond.length + 1) *
sizeof(OffsetType)));
+ auto* out_offsets =
reinterpret_cast<OffsetType*>(out_offset_buf->mutable_data());
+ out_offsets[0] = 0;
+
+ // allocate data buffer conservatively
+ auto data_buff_alloc =
+ std::max(left_size * cond.length,
Review comment:
It looks like clang wants you to pass the type explicitly here
##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc
##########
@@ -607,15 +1050,16 @@ struct ResolveIfElseExec {
}
};
-template <>
-struct ResolveIfElseExec<NullType> {
+template <typename AllocateMem>
+struct ResolveIfElseExec<NullType, AllocateMem> {
static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
- if (batch[0].is_scalar()) {
+ // if all are scalars, return a null scalar
+ if (batch[0].is_scalar() && batch[1].is_scalar() && batch[2].is_scalar()) {
*out = MakeNullScalar(null());
} else {
- const std::shared_ptr<ArrayData>& cond_array = batch[0].array();
- ARROW_ASSIGN_OR_RAISE(
- *out, MakeArrayOfNull(null(), cond_array->length,
ctx->memory_pool()));
+ int64_t len =
+ std::max(batch[0].length(), std::max(batch[1].length(),
batch[2].length()));
Review comment:
In this case it should just be `batch.length`
##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc
##########
@@ -455,6 +602,302 @@ struct IfElseFunctor<Type, enable_if_number<Type>> {
}
};
+template <typename Type>
+struct IfElseFunctor<Type, enable_if_base_binary<Type>> {
+ using OffsetType = typename TypeTraits<Type>::OffsetType::c_type;
+ using ArrayType = typename TypeTraits<Type>::ArrayType;
+
+ // A - Array, S - Scalar, X = Array/Scalar
+
+ // SXX
+ static Status Call(KernelContext* ctx, const BooleanScalar& cond, const
Datum& left,
+ const Datum& right, Datum* out) {
+ if (left.is_scalar() && right.is_scalar()) {
+ if (cond.is_valid) {
+ *out = cond.value ? left.scalar() : right.scalar();
+ } else {
+ *out = MakeNullScalar(left.type());
+ }
+ return Status::OK();
+ }
+ // either left or right is an array. Output is always an array
+ int64_t out_arr_len = std::max(left.length(), right.length());
+ if (!cond.is_valid) {
+ // cond is null; just create a null array
+ ARROW_ASSIGN_OR_RAISE(*out,
+ MakeArrayOfNull(left.type(), out_arr_len,
ctx->memory_pool()))
+ return Status::OK();
+ }
+
+ const auto& valid_data = cond.value ? left : right;
+ if (valid_data.is_array()) {
+ *out = valid_data;
+ } else {
+ // valid data is a scalar that needs to be broadcasted
+ ARROW_ASSIGN_OR_RAISE(*out, MakeArrayFromScalar(*valid_data.scalar(),
out_arr_len,
+ ctx->memory_pool()));
+ }
+ return Status::OK();
+ }
+
+ // AAA
+ static Status Call(KernelContext* ctx, const ArrayData& cond, const
ArrayData& left,
+ const ArrayData& right, ArrayData* out) {
+ const uint8_t* cond_data = cond.buffers[1]->data();
+ BitBlockCounter bit_counter(cond_data, cond.offset, cond.length);
+
+ const auto* left_offsets = left.GetValues<OffsetType>(1);
+ const uint8_t* left_data = left.buffers[2]->data();
+ const auto* right_offsets = right.GetValues<OffsetType>(1);
+ const uint8_t* right_data = right.buffers[2]->data();
+
+ // reserve an additional space
+ ARROW_ASSIGN_OR_RAISE(auto out_offset_buf,
+ ctx->Allocate((cond.length + 1) *
sizeof(OffsetType)));
+ auto* out_offsets =
reinterpret_cast<OffsetType*>(out_offset_buf->mutable_data());
+ out_offsets[0] = 0;
+
+ // allocate data buffer conservatively
Review comment:
Note that for 'coalesce' and 'choose', what I've gone with is using a
builder (with ReserveData) for binary types, and using something akin to your
'handlebulk' only for fixed-width types.
##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc
##########
@@ -455,6 +602,302 @@ struct IfElseFunctor<Type, enable_if_number<Type>> {
}
};
+template <typename Type>
+struct IfElseFunctor<Type, enable_if_base_binary<Type>> {
+ using OffsetType = typename TypeTraits<Type>::OffsetType::c_type;
+ using ArrayType = typename TypeTraits<Type>::ArrayType;
+
+ // A - Array, S - Scalar, X = Array/Scalar
+
+ // SXX
+ static Status Call(KernelContext* ctx, const BooleanScalar& cond, const
Datum& left,
+ const Datum& right, Datum* out) {
+ if (left.is_scalar() && right.is_scalar()) {
+ if (cond.is_valid) {
+ *out = cond.value ? left.scalar() : right.scalar();
+ } else {
+ *out = MakeNullScalar(left.type());
+ }
+ return Status::OK();
+ }
+ // either left or right is an array. Output is always an array
+ int64_t out_arr_len = std::max(left.length(), right.length());
Review comment:
Just use `batch.length`
##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc
##########
@@ -58,8 +58,10 @@ inline Bitmap GetBitmap(const Datum& datum, int 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)
-Status PromoteNullsVisitor(KernelContext* ctx, const Datum& cond_d, const
Datum& left_d,
- const Datum& right_d, ArrayData* output) {
+template <typename AllocateMem>
Review comment:
nit: maybe AllocateNullBitmap?
##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc
##########
@@ -36,7 +36,7 @@ namespace {
constexpr uint64_t kAllNull = 0;
constexpr uint64_t kAllValid = ~kAllNull;
-util::optional<uint64_t> GetConstantValidityWord(const Datum& data) {
Review comment:
AIUI, they don't need to be declared static if inside an anonymous
namespace?
##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc
##########
@@ -455,6 +602,302 @@ struct IfElseFunctor<Type, enable_if_number<Type>> {
}
};
+template <typename Type>
+struct IfElseFunctor<Type, enable_if_base_binary<Type>> {
+ using OffsetType = typename TypeTraits<Type>::OffsetType::c_type;
+ using ArrayType = typename TypeTraits<Type>::ArrayType;
+
+ // A - Array, S - Scalar, X = Array/Scalar
+
+ // SXX
+ static Status Call(KernelContext* ctx, const BooleanScalar& cond, const
Datum& left,
+ const Datum& right, Datum* out) {
+ if (left.is_scalar() && right.is_scalar()) {
+ if (cond.is_valid) {
+ *out = cond.value ? left.scalar() : right.scalar();
+ } else {
+ *out = MakeNullScalar(left.type());
+ }
+ return Status::OK();
+ }
+ // either left or right is an array. Output is always an array
+ int64_t out_arr_len = std::max(left.length(), right.length());
+ if (!cond.is_valid) {
+ // cond is null; just create a null array
+ ARROW_ASSIGN_OR_RAISE(*out,
+ MakeArrayOfNull(left.type(), out_arr_len,
ctx->memory_pool()))
+ return Status::OK();
+ }
+
+ const auto& valid_data = cond.value ? left : right;
+ if (valid_data.is_array()) {
+ *out = valid_data;
+ } else {
+ // valid data is a scalar that needs to be broadcasted
+ ARROW_ASSIGN_OR_RAISE(*out, MakeArrayFromScalar(*valid_data.scalar(),
out_arr_len,
+ ctx->memory_pool()));
+ }
+ return Status::OK();
+ }
+
+ // AAA
+ static Status Call(KernelContext* ctx, const ArrayData& cond, const
ArrayData& left,
+ const ArrayData& right, ArrayData* out) {
+ const uint8_t* cond_data = cond.buffers[1]->data();
+ BitBlockCounter bit_counter(cond_data, cond.offset, cond.length);
+
+ const auto* left_offsets = left.GetValues<OffsetType>(1);
+ const uint8_t* left_data = left.buffers[2]->data();
+ const auto* right_offsets = right.GetValues<OffsetType>(1);
+ const uint8_t* right_data = right.buffers[2]->data();
+
+ // reserve an additional space
+ ARROW_ASSIGN_OR_RAISE(auto out_offset_buf,
+ ctx->Allocate((cond.length + 1) *
sizeof(OffsetType)));
+ auto* out_offsets =
reinterpret_cast<OffsetType*>(out_offset_buf->mutable_data());
+ out_offsets[0] = 0;
+
+ // allocate data buffer conservatively
Review comment:
Is this sufficient? For `IfElse([true, false], ['asdf', ''], ['',
'asdf'])` this will allocate only a length 4 buffer but a length 8 buffer is
needed.
##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc
##########
@@ -78,19 +80,37 @@ Status PromoteNullsVisitor(KernelContext* ctx, const Datum&
cond_d, const Datum&
// cond.valid & (cond.data & left.valid | ~cond.data & right.valid)
// In the following cases, we dont need to allocate out_valid bitmap
- // if cond & left & right all ones, then output is all valid. output
validity buffer
- // is already allocated, hence set all bits
+ // if cond & left & right all ones, then output is all valid.
+ // if output validity buffer is already allocated (NullHandling::
+ // COMPUTED_PREALLOCATE) -> set all bits
+ // else, return nullptr
if (cond_const == kAllValid && left_const == kAllValid && right_const ==
kAllValid) {
- BitUtil::SetBitmap(output->buffers[0]->mutable_data(), output->offset,
- output->length);
+ if (AllocateMem::value) {
+ output->buffers[0] = nullptr;
+ } else { // NullHandling::COMPUTED_NO_PREALLOCATE
Review comment:
These comments contradict each other?
##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc
##########
@@ -455,6 +602,302 @@ struct IfElseFunctor<Type, enable_if_number<Type>> {
}
};
+template <typename Type>
+struct IfElseFunctor<Type, enable_if_base_binary<Type>> {
+ using OffsetType = typename TypeTraits<Type>::OffsetType::c_type;
+ using ArrayType = typename TypeTraits<Type>::ArrayType;
+
+ // A - Array, S - Scalar, X = Array/Scalar
+
+ // SXX
+ static Status Call(KernelContext* ctx, const BooleanScalar& cond, const
Datum& left,
+ const Datum& right, Datum* out) {
+ if (left.is_scalar() && right.is_scalar()) {
+ if (cond.is_valid) {
+ *out = cond.value ? left.scalar() : right.scalar();
+ } else {
+ *out = MakeNullScalar(left.type());
+ }
+ return Status::OK();
+ }
+ // either left or right is an array. Output is always an array
+ int64_t out_arr_len = std::max(left.length(), right.length());
+ if (!cond.is_valid) {
+ // cond is null; just create a null array
+ ARROW_ASSIGN_OR_RAISE(*out,
+ MakeArrayOfNull(left.type(), out_arr_len,
ctx->memory_pool()))
+ return Status::OK();
+ }
+
+ const auto& valid_data = cond.value ? left : right;
+ if (valid_data.is_array()) {
+ *out = valid_data;
+ } else {
+ // valid data is a scalar that needs to be broadcasted
+ ARROW_ASSIGN_OR_RAISE(*out, MakeArrayFromScalar(*valid_data.scalar(),
out_arr_len,
+ ctx->memory_pool()));
+ }
+ return Status::OK();
+ }
+
+ // AAA
+ static Status Call(KernelContext* ctx, const ArrayData& cond, const
ArrayData& left,
+ const ArrayData& right, ArrayData* out) {
+ const uint8_t* cond_data = cond.buffers[1]->data();
+ BitBlockCounter bit_counter(cond_data, cond.offset, cond.length);
+
+ const auto* left_offsets = left.GetValues<OffsetType>(1);
+ const uint8_t* left_data = left.buffers[2]->data();
+ const auto* right_offsets = right.GetValues<OffsetType>(1);
+ const uint8_t* right_data = right.buffers[2]->data();
+
+ // reserve an additional space
+ ARROW_ASSIGN_OR_RAISE(auto out_offset_buf,
+ ctx->Allocate((cond.length + 1) *
sizeof(OffsetType)));
+ auto* out_offsets =
reinterpret_cast<OffsetType*>(out_offset_buf->mutable_data());
+ out_offsets[0] = 0;
+
+ // allocate data buffer conservatively
+ int64_t data_buff_alloc = std::max(left_offsets[left.length] -
left_offsets[0],
+ right_offsets[right.length] -
right_offsets[0]);
+ ARROW_ASSIGN_OR_RAISE(std::shared_ptr<ResizableBuffer> out_data_buf,
+ ctx->Allocate(data_buff_alloc));
+ uint8_t* out_data = out_data_buf->mutable_data();
+
+ RunIfElseLoop(
+ cond,
+ [&](int64_t offset, int64_t length) { // from left bulk
Review comment:
minor nit but I wonder if the compiler could generate similar (enough)
code from a
```
struct CopyBinaryBulk {
const offset_type* offsets;
const uint8_t* values;
offset_type* out_offsets;
uint8_t* out_values;
operator()(...) { ... }
};
```
so you don't have to copy-paste these everywhere
##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc
##########
@@ -274,18 +310,129 @@ static void RunIfElseLoop(const ArrayData& cond,
HandleBulk handle_bulk,
}
template <typename HandleBulk, typename HandleEach>
-static void RunIfElseLoopInverted(const ArrayData& cond, HandleBulk
handle_bulk,
- HandleEach handle_each) {
- return RunIfElseLoop<HandleBulk, HandleEach, true>(cond, handle_bulk,
handle_each);
+static void RunIfElseLoopInverted(const ArrayData& cond, const HandleBulk&
handle_bulk,
+ const HandleEach& handle_each) {
+ RunIfElseLoop<HandleBulk, HandleEach, true>(cond, handle_bulk, handle_each);
+}
+
+/// Runs the main if_else loop.
+///
+/// `HandleBulk` has the signature:
+/// [](int64_t offset, int64_t length){...}
+/// It should copy `length` number of elements from source array to output
array with
+/// `offset` offset in both arrays
+///
+/// `HandleEach` has the signature:
+/// [](int64_t offset){...}
+/// It should copy single element from source array to output array with
`offset`
+/// offset in both arrays
+template <typename HandleBulkLeft, typename HandleBulkRight, typename
HandleEachLeft,
+ typename HandleEachRight, bool invert = false>
+static void RunIfElseLoop(const ArrayData& cond, const HandleBulkLeft&
handle_bulk_left,
+ const HandleBulkRight& handle_bulk_right,
+ const HandleEachLeft& handle_each_left,
+ const HandleEachRight& handle_each_right) {
+ int64_t offset = 0;
+ const auto* cond_data = cond.buffers[1]->data(); // this is a BoolArray
+
+ // There are multiple options for this one. Ex: BitBlockCounter,
BitmapWordReader,
+ // BitRunReader, etc. BitRunReader would be efficient for longer contiguous
values in
+ // the cond data buffer.
+ // BitmapWordReader was slightly better performant that BitBlockCounter.
+ BitmapWordReader<Word> cond_reader(cond_data, cond.offset, cond.length);
+
+ int64_t cnt = cond_reader.words();
+ while (cnt--) {
+ Word word = cond_reader.NextWord();
+ if (invert) {
+ if (word == UINT64_MAX) {
+ handle_bulk_right(offset, word_len);
+ } else if (word == 0) {
+ handle_bulk_left(offset, word_len);
+ } else {
+ for (int64_t i = 0; i < word_len; ++i) {
+ if (!BitUtil::GetBit(cond_data, cond.offset + offset + i)) {
+ handle_each_right(offset + i);
+ } else {
+ handle_each_left(offset + i);
+ }
+ }
+ }
+ } else {
+ if (word == UINT64_MAX) {
+ handle_bulk_left(offset, word_len);
+ } else if (word == 0) {
+ handle_bulk_right(offset, word_len);
+ } else {
+ for (int64_t i = 0; i < word_len; ++i) {
+ if (BitUtil::GetBit(cond_data, cond.offset + offset + i)) {
+ handle_each_left(offset + i);
+ } else {
+ handle_each_right(offset + i);
+ }
+ }
+ }
+ }
+ offset += word_len;
+ }
+
+ cnt = cond_reader.trailing_bytes();
+ while (cnt--) {
+ int valid_bits;
+ uint8_t byte = cond_reader.NextTrailingByte(valid_bits);
+ if (invert) {
+ if (byte == UINT8_MAX && valid_bits == 8) {
+ handle_bulk_right(offset, 8);
+ } else if (byte == 0 && valid_bits == 8) {
+ handle_bulk_left(offset, 8);
+ } else {
+ for (int i = 0; i < valid_bits; ++i) {
+ if (!BitUtil::GetBit(cond_data, cond.offset + offset + i)) {
+ handle_each_right(offset + i);
+ } else {
+ handle_each_left(offset + i);
+ }
+ }
+ }
+ } else {
+ if (byte == UINT8_MAX && valid_bits == 8) {
+ handle_bulk_left(offset, 8);
+ } else if (byte == 0 && valid_bits == 8) {
+ handle_bulk_right(offset, 8);
+ } else {
+ for (int i = 0; i < valid_bits; ++i) {
+ if (BitUtil::GetBit(cond_data, cond.offset + offset + i)) {
+ handle_each_left(offset + i);
+ } else {
+ handle_each_right(offset + i);
+ }
+ }
+ }
+ }
+ offset += 8; // doesn't necessarily have to be valid_bits here. Because it
+ // valid_bits < 8, then the loop will exit
Review comment:
this is a little confusing but the intent is, valid_bits < 8 => cnt == 0?
##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else.cc
##########
@@ -455,6 +602,302 @@ struct IfElseFunctor<Type, enable_if_number<Type>> {
}
};
+template <typename Type>
+struct IfElseFunctor<Type, enable_if_base_binary<Type>> {
+ using OffsetType = typename TypeTraits<Type>::OffsetType::c_type;
+ using ArrayType = typename TypeTraits<Type>::ArrayType;
+
+ // A - Array, S - Scalar, X = Array/Scalar
+
+ // SXX
+ static Status Call(KernelContext* ctx, const BooleanScalar& cond, const
Datum& left,
+ const Datum& right, Datum* out) {
+ if (left.is_scalar() && right.is_scalar()) {
+ if (cond.is_valid) {
+ *out = cond.value ? left.scalar() : right.scalar();
+ } else {
+ *out = MakeNullScalar(left.type());
+ }
+ return Status::OK();
+ }
+ // either left or right is an array. Output is always an array
+ int64_t out_arr_len = std::max(left.length(), right.length());
+ if (!cond.is_valid) {
+ // cond is null; just create a null array
+ ARROW_ASSIGN_OR_RAISE(*out,
+ MakeArrayOfNull(left.type(), out_arr_len,
ctx->memory_pool()))
+ return Status::OK();
+ }
+
+ const auto& valid_data = cond.value ? left : right;
+ if (valid_data.is_array()) {
+ *out = valid_data;
+ } else {
+ // valid data is a scalar that needs to be broadcasted
+ ARROW_ASSIGN_OR_RAISE(*out, MakeArrayFromScalar(*valid_data.scalar(),
out_arr_len,
+ ctx->memory_pool()));
+ }
+ return Status::OK();
+ }
+
+ // AAA
+ static Status Call(KernelContext* ctx, const ArrayData& cond, const
ArrayData& left,
+ const ArrayData& right, ArrayData* out) {
+ const uint8_t* cond_data = cond.buffers[1]->data();
+ BitBlockCounter bit_counter(cond_data, cond.offset, cond.length);
+
+ const auto* left_offsets = left.GetValues<OffsetType>(1);
+ const uint8_t* left_data = left.buffers[2]->data();
+ const auto* right_offsets = right.GetValues<OffsetType>(1);
+ const uint8_t* right_data = right.buffers[2]->data();
+
+ // reserve an additional space
+ ARROW_ASSIGN_OR_RAISE(auto out_offset_buf,
+ ctx->Allocate((cond.length + 1) *
sizeof(OffsetType)));
+ auto* out_offsets =
reinterpret_cast<OffsetType*>(out_offset_buf->mutable_data());
+ out_offsets[0] = 0;
+
+ // allocate data buffer conservatively
Review comment:
The 'most conservative' would be to allocate the sum of the value
lengths.
##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else_test.cc
##########
@@ -316,5 +313,98 @@ TEST_F(TestIfElseKernel, IfElseDispatchBest) {
CheckDispatchBest(name, {null(), uint8(), int8()}, {boolean(), int16(),
int16()});
}
+template <typename Type>
+class TestIfElseBaseBinary : public ::testing::Test {};
+
+TYPED_TEST_SUITE(TestIfElseBaseBinary, BinaryTypes);
+
+TYPED_TEST(TestIfElseBaseBinary, IfElseBaseBinary) {
Review comment:
IMO these cases could be consolidated into one or two checks since there
are only 12 cases here (or 5 if you want to prune: null cond, true cond with
null/non-null left array, false cond with null/non-null right array).
##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else_benchmark.cc
##########
@@ -48,13 +48,13 @@ static void IfElseBench(benchmark::State& state) {
ABORT_NOT_OK(IfElse(cond->Slice(offset), left->Slice(offset),
right->Slice(offset)));
}
- state.SetBytesProcessed(state.iterations() *
Review comment:
You could use BaseBinaryArray::values_length() if you want to write a
specialized 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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]