@@ -339,8 +339,14 @@ pub fn expr_sub_expressions(expr: &Expr) -> Result<Vec<Expr>> {
339339 Expr :: ScalarFunction { args, .. }
340340 | Expr :: ScalarUDF { args, .. }
341341 | Expr :: TableUDF { args, .. }
342- | Expr :: AggregateFunction { args, .. }
343342 | Expr :: AggregateUDF { args, .. } => Ok ( args. clone ( ) ) ,
343+ Expr :: AggregateFunction {
344+ args, within_group, ..
345+ } => Ok ( args
346+ . iter ( )
347+ . chain ( within_group. as_ref ( ) . unwrap_or ( & vec ! [ ] ) )
348+ . cloned ( )
349+ . collect ( ) ) ,
344350 Expr :: GroupingSet ( grouping_set) => match grouping_set {
345351 GroupingSet :: Rollup ( exprs) => Ok ( exprs. clone ( ) ) ,
346352 GroupingSet :: Cube ( exprs) => Ok ( exprs. clone ( ) ) ,
@@ -517,11 +523,25 @@ pub fn rewrite_expression(expr: &Expr, expressions: &[Expr]) -> Result<Expr> {
517523 } )
518524 }
519525 }
520- Expr :: AggregateFunction { fun, distinct, .. } => Ok ( Expr :: AggregateFunction {
521- fun : fun. clone ( ) ,
522- args : expressions. to_vec ( ) ,
523- distinct : * distinct,
524- } ) ,
526+ Expr :: AggregateFunction {
527+ fun,
528+ distinct,
529+ args,
530+ ..
531+ } => {
532+ let args_limit = args. len ( ) ;
533+ let within_group = if expressions. len ( ) > args_limit {
534+ Some ( expressions[ args_limit..] . to_vec ( ) )
535+ } else {
536+ None
537+ } ;
538+ Ok ( Expr :: AggregateFunction {
539+ fun : fun. clone ( ) ,
540+ args : expressions[ ..args_limit] . to_vec ( ) ,
541+ distinct : * distinct,
542+ within_group,
543+ } )
544+ }
525545 Expr :: AggregateUDF { fun, .. } => Ok ( Expr :: AggregateUDF {
526546 fun : fun. clone ( ) ,
527547 args : expressions. to_vec ( ) ,
0 commit comments