| 2073 | // Implement 'coalesce' for any mix of scalar/array arguments for any fixed-width type |
| 2074 | template <typename Type> |
| 2075 | Status ExecArrayCoalesce(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) { |
| 2076 | ArraySpan* output = out->array_span_mutable(); |
| 2077 | const int64_t out_offset = output->offset; |
| 2078 | // Use output validity buffer as mask to decide what values to copy |
| 2079 | uint8_t* out_valid = output->buffers[0].data; |
| 2080 | |
| 2081 | // Clear output validity buffer - no values are set initially |
| 2082 | bit_util::SetBitsTo(out_valid, out_offset, batch.length, false); |
| 2083 | uint8_t* out_values = output->buffers[1].data; |
| 2084 | |
| 2085 | for (const ExecValue& value : batch.values) { |
| 2086 | if (value.null_count() == 0) { |
| 2087 | // Valid scalar, or all-valid array |
| 2088 | CopyValuesAllValid<Type>(value, out_valid, out_values, out_offset, batch.length); |
| 2089 | break; |
| 2090 | } else if (value.is_array()) { |
| 2091 | // Array with nulls |
| 2092 | const ArraySpan& arr = value.array; |
| 2093 | const int64_t in_offset = arr.offset; |
| 2094 | const int64_t in_null_count = arr.GetNullCount(); |
| 2095 | DCHECK_GT(in_null_count, 0); |
| 2096 | const DataType& type = *arr.type; |
| 2097 | const uint8_t* in_valid = arr.buffers[0].data; |
| 2098 | const uint8_t* in_values = arr.buffers[1].data; |
| 2099 | |
| 2100 | if (in_null_count < 0.8 * batch.length) { |
| 2101 | // The input is not mostly null, we deem it more efficient to |
| 2102 | // copy values even underlying null slots instead of the more |
| 2103 | // expensive bitmasking using BinaryBitBlockCounter. |
| 2104 | BitRunReader bit_reader(out_valid, out_offset, batch.length); |
| 2105 | int64_t offset = 0; |
| 2106 | while (true) { |
| 2107 | const auto run = bit_reader.NextRun(); |
| 2108 | if (run.length == 0) { |
| 2109 | break; |
| 2110 | } |
| 2111 | if (!run.set) { |
| 2112 | // Copy from input |
| 2113 | CopyDataUtils<Type>::CopyData(type, in_values, in_offset + offset, out_values, |
| 2114 | out_offset + offset, run.length); |
| 2115 | } |
| 2116 | offset += run.length; |
| 2117 | } |
| 2118 | arrow::internal::BitmapOr(out_valid, out_offset, in_valid, in_offset, |
| 2119 | batch.length, out_offset, out_valid); |
| 2120 | } else { |
| 2121 | BinaryBitBlockCounter counter(in_valid, in_offset, out_valid, out_offset, |
| 2122 | batch.length); |
| 2123 | int64_t offset = 0; |
| 2124 | while (offset < batch.length) { |
| 2125 | const auto block = counter.NextAndNotWord(); |
| 2126 | if (block.AllSet()) { |
| 2127 | CopyValues<Type>(value, offset, block.length, out_valid, out_values, |
| 2128 | out_offset + offset); |
| 2129 | } else if (block.popcount) { |
| 2130 | for (int64_t j = 0; j < block.length; ++j) { |
| 2131 | if (!bit_util::GetBit(out_valid, out_offset + offset + j) && |
| 2132 | bit_util::GetBit(in_valid, in_offset + offset + j)) { |
nothing calls this directly
no test coverage detected