| 298 | } |
| 299 | |
| 300 | Result<ExecBatch> GroupByNode::Finalize() { |
| 301 | arrow::util::tracing::Span span; |
| 302 | START_COMPUTE_SPAN(span, "Finalize", |
| 303 | {{"group_by", ToStringExtra(0)}, {"node.label", label()}}); |
| 304 | |
| 305 | ThreadLocalState* state = &local_states_[0]; |
| 306 | // If we never got any batches, then state won't have been initialized |
| 307 | RETURN_NOT_OK(InitLocalStateIfNeeded(state)); |
| 308 | |
| 309 | // Allocate a batch for output |
| 310 | ExecBatch out_data{{}, state->grouper->num_groups()}; |
| 311 | out_data.values.resize(agg_kernels_.size() + key_field_ids_.size() + |
| 312 | segment_key_field_ids_.size()); |
| 313 | |
| 314 | // Segment keys come first |
| 315 | PlaceFields(out_data, 0, segmenter_values_); |
| 316 | // Followed by keys |
| 317 | ARROW_ASSIGN_OR_RAISE(ExecBatch out_keys, state->grouper->GetUniques()); |
| 318 | std::move(out_keys.values.begin(), out_keys.values.end(), |
| 319 | out_data.values.begin() + segment_key_field_ids_.size()); |
| 320 | // And finally, the aggregates themselves |
| 321 | std::size_t base = segment_key_field_ids_.size() + key_field_ids_.size(); |
| 322 | for (size_t i = 0; i < agg_kernels_.size(); ++i) { |
| 323 | arrow::util::tracing::Span span_item; |
| 324 | START_COMPUTE_SPAN(span_item, aggs_[i].function, |
| 325 | {{"function.name", aggs_[i].function}, |
| 326 | {"function.options", |
| 327 | aggs_[i].options ? aggs_[i].options->ToString() : "<NULLPTR>"}, |
| 328 | {"function.kind", std::string(kind_name()) + "::Finalize"}}); |
| 329 | KernelContext batch_ctx{plan_->query_context()->exec_context()}; |
| 330 | batch_ctx.SetState(state->agg_states[i].get()); |
| 331 | RETURN_NOT_OK(agg_kernels_[i]->finalize(&batch_ctx, &out_data.values[i + base])); |
| 332 | state->agg_states[i].reset(); |
| 333 | } |
| 334 | state->grouper.reset(); |
| 335 | |
| 336 | return out_data; |
| 337 | } |
| 338 | |
| 339 | Status GroupByNode::OutputNthBatch(int64_t n) { |
| 340 | int64_t batch_size = output_batch_size(); |
no test coverage detected