| 253 | } |
| 254 | |
| 255 | Status GroupByNode::Merge() { |
| 256 | arrow::util::tracing::Span span; |
| 257 | START_COMPUTE_SPAN(span, "Merge", |
| 258 | {{"group_by", ToStringExtra(0)}, {"node.label", label()}}); |
| 259 | ThreadLocalState* state0 = &local_states_[0]; |
| 260 | for (size_t i = 1; i < local_states_.size(); ++i) { |
| 261 | ThreadLocalState* state = &local_states_[i]; |
| 262 | if (!state->grouper) { |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | ARROW_ASSIGN_OR_RAISE(ExecBatch other_keys, state->grouper->GetUniques()); |
| 267 | ARROW_ASSIGN_OR_RAISE(Datum transposition, |
| 268 | state0->grouper->Consume(ExecSpan(other_keys))); |
| 269 | state->grouper.reset(); |
| 270 | |
| 271 | for (size_t span_i = 0; span_i < agg_kernels_.size(); ++span_i) { |
| 272 | arrow::util::tracing::Span span_item; |
| 273 | START_COMPUTE_SPAN( |
| 274 | span_item, aggs_[span_i].function, |
| 275 | {{"function.name", aggs_[span_i].function}, |
| 276 | {"function.options", |
| 277 | aggs_[span_i].options ? aggs_[span_i].options->ToString() : "<NULLPTR>"}, |
| 278 | {"function.kind", std::string(kind_name()) + "::Merge"}}); |
| 279 | |
| 280 | auto ctx = plan_->query_context()->exec_context(); |
| 281 | KernelContext batch_ctx{ctx}; |
| 282 | DCHECK(state0->agg_states[span_i]); |
| 283 | batch_ctx.SetState(state0->agg_states[span_i].get()); |
| 284 | |
| 285 | // XXX this resizes each KernelState (state0->agg_states[span_i]) multiple times. |
| 286 | // An alternative would be a two-pass algorithm: |
| 287 | // 1. Compute all transpositions (one per local state) and the final number of |
| 288 | // groups. |
| 289 | // 2. Process all agg kernels, resizing each KernelState only once. |
| 290 | RETURN_NOT_OK( |
| 291 | agg_kernels_[span_i]->resize(&batch_ctx, state0->grouper->num_groups())); |
| 292 | RETURN_NOT_OK(agg_kernels_[span_i]->merge( |
| 293 | &batch_ctx, std::move(*state->agg_states[span_i]), *transposition.array())); |
| 294 | state->agg_states[span_i].reset(); |
| 295 | } |
| 296 | } |
| 297 | return Status::OK(); |
| 298 | } |
| 299 | |
| 300 | Result<ExecBatch> GroupByNode::Finalize() { |
| 301 | arrow::util::tracing::Span span; |
nothing calls this directly
no test coverage detected