| 1556 | // given helper functions to copy data from a source array to a target array |
| 1557 | template <typename Type> |
| 1558 | Status ExecArrayCaseWhen(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) { |
| 1559 | const ArraySpan& conds_array = batch[0].array; |
| 1560 | if (conds_array.GetNullCount() > 0) { |
| 1561 | return Status::Invalid( |
| 1562 | "cond struct must not be a null scalar or " |
| 1563 | "have top-level nulls"); |
| 1564 | } |
| 1565 | ArraySpan* output = out->array_span_mutable(); |
| 1566 | const int64_t out_offset = output->offset; |
| 1567 | const auto num_value_args = batch.values.size() - 1; |
| 1568 | const bool have_else_arg = |
| 1569 | static_cast<size_t>(conds_array.type->num_fields()) < num_value_args; |
| 1570 | uint8_t* out_valid = output->buffers[0].data; |
| 1571 | uint8_t* out_values = output->buffers[1].data; |
| 1572 | |
| 1573 | if (have_else_arg) { |
| 1574 | // Copy 'else' value into output |
| 1575 | CopyValues<Type>(batch.values.back(), /*in_offset=*/0, batch.length, out_valid, |
| 1576 | out_values, out_offset); |
| 1577 | } else { |
| 1578 | // There's no 'else' argument, so we should have an all-null validity bitmap |
| 1579 | bit_util::SetBitsTo(out_valid, out_offset, batch.length, false); |
| 1580 | } |
| 1581 | |
| 1582 | // Allocate a temporary bitmap to determine which elements still need setting. |
| 1583 | ARROW_ASSIGN_OR_RAISE(auto mask_buffer, ctx->AllocateBitmap(batch.length)); |
| 1584 | uint8_t* mask = mask_buffer->mutable_data(); |
| 1585 | std::memset(mask, 0xFF, mask_buffer->size()); |
| 1586 | |
| 1587 | // Then iterate through each argument in turn and set elements. |
| 1588 | for (int i = 0; i < batch.num_values() - (have_else_arg ? 2 : 1); i++) { |
| 1589 | const ArraySpan& cond_array = conds_array.child_data[i]; |
| 1590 | const int64_t cond_offset = conds_array.offset + cond_array.offset; |
| 1591 | const uint8_t* cond_values = cond_array.buffers[1].data; |
| 1592 | const ExecValue& value = batch[i + 1]; |
| 1593 | int64_t offset = 0; |
| 1594 | |
| 1595 | if (cond_array.GetNullCount() == 0) { |
| 1596 | // If no valid buffer, visit mask & cond bitmap simultaneously |
| 1597 | auto visit_run = [&](int64_t position, int64_t run_length) { |
| 1598 | CopyValues<Type>(value, position, run_length, out_valid, out_values, |
| 1599 | out_offset + position); |
| 1600 | bit_util::SetBitsTo(mask, position, run_length, false); |
| 1601 | return Status::OK(); |
| 1602 | }; |
| 1603 | RETURN_NOT_OK(::arrow::internal::VisitTwoSetBitRuns( |
| 1604 | mask, /*start_offset=*/0, cond_values, cond_offset, batch.length, visit_run)); |
| 1605 | } else { |
| 1606 | // Visit mask & cond bitmap & cond validity |
| 1607 | const uint8_t* cond_valid = cond_array.buffers[0].data; |
| 1608 | Bitmap bitmaps[3] = {{mask, /*offset=*/0, batch.length}, |
| 1609 | {cond_values, cond_offset, batch.length}, |
| 1610 | {cond_valid, cond_offset, batch.length}}; |
| 1611 | Bitmap::VisitWords(bitmaps, [&](std::array<uint64_t, 3> words) { |
| 1612 | const uint64_t word = words[0] & words[1] & words[2]; |
| 1613 | const int64_t block_length = std::min<int64_t>(64, batch.length - offset); |
| 1614 | if (word == std::numeric_limits<uint64_t>::max()) { |
| 1615 | CopyValues<Type>(value, offset, block_length, out_valid, out_values, |
nothing calls this directly
no test coverage detected