2010YOUY01 commented on code in PR #22729:
URL: https://github.com/apache/datafusion/pull/22729#discussion_r3356023077


##########
datafusion/physical-plan/src/aggregates/hash_aggregate.rs:
##########
@@ -0,0 +1,349 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Grouped hash aggregation for simple multi-stage aggregation paths.
+//!
+//! This module handles the basic grouped two-stage paths:
+//!
+//! ```text
+//! input rows -> GROUP BY hash table -> accumulator state rows
+//! state rows -> GROUP BY hash table -> final aggregate rows
+//! ```
+//!
+//! `AggregateExec` keeps finite-memory, ordered, limit, grouping-set,
+//! `partial state -> partial state`, and single-stage aggregation on
+//! `GroupedHashAggregateStream` for now.
+
+use std::sync::Arc;
+use std::task::{Context, Poll};
+
+use arrow::datatypes::SchemaRef;
+use arrow::record_batch::RecordBatch;
+use datafusion_common::Result;
+use datafusion_execution::TaskContext;
+use datafusion_execution::memory_pool::{MemoryConsumer, MemoryReservation};
+use futures::stream::{Stream, StreamExt};
+
+use super::AggregateExec;
+use super::hash_table::{AggregateHashTable, InitialPartial, PartialFinal};
+use crate::metrics::{BaselineMetrics, MetricBuilder, RecordOutput, 
SpillMetrics};
+use crate::stream::EmptyRecordBatchStream;
+use crate::{InputOrderMode, RecordBatchStream, SendableRecordBatchStream, 
metrics};
+
+/// Hash aggregation uses a 2-stage (partial and final) hash aggregation, this 
stream
+/// is for the partial stage.
+///
+/// # Example
+///
+/// select k, avg(v) from t group by k;
+///
+/// ## Plan
+/// AggregateExec(stage=final)
+/// -- RepartitionExec(hash(k))
+/// ---- AggregateExec(stage=partial)
+///
+/// ## Partial Stage Behavior
+/// Input: raw rows
+/// Output: partial states for all groups (e.g. for avg(x), it's sum(x), 
count(x))
+///
+/// ## Final Stage Behavior
+/// Input: partial states
+/// Output: results for all groups (e.g. for avg(x), it's avg(x) calculated 
from the state)
+pub(crate) struct InitialPartialHashAggregateStream {
+    /// Output schema: group columns followed by partial aggregate state 
columns.
+    schema: SchemaRef,
+
+    /// Input batches containing raw rows, not partial aggregate state.
+    input: SendableRecordBatchStream,
+
+    /// Hash table state for this aggregate stream.
+    hash_table: AggregateHashTable<InitialPartial>,
+
+    /// Memory reservation for group keys and accumulators.
+    reservation: MemoryReservation,
+
+    /// Execution metrics shared with the aggregate plan node.
+    baseline_metrics: BaselineMetrics,
+
+    /// Tracks partial aggregation row reduction, matching 
`GroupedHashAggregateStream`.
+    reduction_factor: metrics::RatioMetrics,
+}
+
+/// Hash aggregation uses a 2-stage (partial and final) hash aggregation, this 
stream
+/// is for the final stage.
+///
+/// See [`InitialPartialHashAggregateStream`] for details.
+pub(crate) struct PartialFinalHashAggregateStream {

Review Comment:
   I think it might be clearer to call them `PartialHashAggregateStream` and 
`FinalHashAggregateStream` 🤔. I'll update the names.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to