tohuya6 commented on code in PR #23682:
URL: https://github.com/apache/datafusion/pull/23682#discussion_r3695526386


##########
datafusion/optimizer/src/coalesce_first_last.rs:
##########
@@ -0,0 +1,658 @@
+// 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.
+
+//! [`CoalesceFirstLast`] coalesces peer `first_value` / `last_value` aggregate
+//! expressions that share the same `ORDER BY` key into a single struct-valued
+//! aggregate.
+
+use std::collections::HashMap;
+use std::sync::Arc;
+
+use crate::optimizer::ApplyOrder;
+use crate::{OptimizerConfig, OptimizerRule};
+
+use datafusion_common::Result;
+use datafusion_common::tree_node::Transformed;
+use datafusion_expr::expr::{
+    AggregateFunction, AggregateFunctionParams, NullTreatment, Sort,
+};
+use datafusion_expr::{
+    Aggregate, AggregateUDF, Expr, LogicalPlan, LogicalPlanBuilder, col, lit,
+};
+
+use indexmap::IndexMap;
+
+const FIRST_VALUE: &str = "first_value";
+const LAST_VALUE: &str = "last_value";
+const NAMED_STRUCT: &str = "named_struct";
+const GET_FIELD: &str = "get_field";
+
+/// Coalesces peer `first_value` / `last_value` aggregates that share one
+/// `ORDER BY` key into a single struct-valued aggregate, with a projection on
+/// top to unpack the struct back into the original columns:
+///
+/// ```text
+/// Aggregate: groupBy=[[p]], aggr=[[first_value(a ORDER BY o DESC),
+///                                  first_value(b ORDER BY o DESC)]]
+/// ```
+///
+/// becomes
+///
+/// ```text
+/// Projection: p, get_field(wrapped, 'c0'), get_field(wrapped, 'c1')
+///   Aggregate: groupBy=[[p]],
+///              aggr=[[first_value(named_struct('c0', a, 'c1', b) ORDER BY o 
DESC) AS wrapped]]
+/// ```
+///
+/// The input is scanned once, not once per expression, and holds one per-group
+/// state slot instead of N.

Review Comment:
   added doc note, thanks



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