@@ -348,6 +348,8 @@ pub enum Expr {
348348 ListAgg ( ListAgg ) ,
349349 /// The `ARRAY_AGG` function `SELECT ARRAY_AGG(... ORDER BY ...)`
350350 ArrayAgg ( ArrayAgg ) ,
351+ /// The `WITHIN GROUP` expr `... WITHIN GROUP (ORDER BY ...)`
352+ WithinGroup ( WithinGroup ) ,
351353 /// The `GROUPING SETS` expr.
352354 GroupingSets ( Vec < Vec < Expr > > ) ,
353355 /// The `CUBE` expr.
@@ -549,6 +551,7 @@ impl fmt::Display for Expr {
549551 Expr :: ArraySubquery ( s) => write ! ( f, "ARRAY({})" , s) ,
550552 Expr :: ListAgg ( listagg) => write ! ( f, "{}" , listagg) ,
551553 Expr :: ArrayAgg ( arrayagg) => write ! ( f, "{}" , arrayagg) ,
554+ Expr :: WithinGroup ( withingroup) => write ! ( f, "{}" , withingroup) ,
552555 Expr :: GroupingSets ( sets) => {
553556 write ! ( f, "GROUPING SETS (" ) ?;
554557 let mut sep = "" ;
@@ -2420,7 +2423,6 @@ pub struct ListAgg {
24202423 pub expr : Box < Expr > ,
24212424 pub separator : Option < Box < Expr > > ,
24222425 pub on_overflow : Option < ListAggOnOverflow > ,
2423- pub within_group : Vec < OrderByExpr > ,
24242426}
24252427
24262428impl fmt:: Display for ListAgg {
@@ -2438,13 +2440,6 @@ impl fmt::Display for ListAgg {
24382440 write ! ( f, "{}" , on_overflow) ?;
24392441 }
24402442 write ! ( f, ")" ) ?;
2441- if !self . within_group . is_empty ( ) {
2442- write ! (
2443- f,
2444- " WITHIN GROUP (ORDER BY {})" ,
2445- display_comma_separated( & self . within_group)
2446- ) ?;
2447- }
24482443 Ok ( ( ) )
24492444 }
24502445}
@@ -2494,7 +2489,6 @@ pub struct ArrayAgg {
24942489 pub expr : Box < Expr > ,
24952490 pub order_by : Option < Box < OrderByExpr > > ,
24962491 pub limit : Option < Box < Expr > > ,
2497- pub within_group : bool , // order by is used inside a within group or not
24982492}
24992493
25002494impl fmt:: Display for ArrayAgg {
@@ -2505,20 +2499,33 @@ impl fmt::Display for ArrayAgg {
25052499 if self . distinct { "DISTINCT " } else { "" } ,
25062500 self . expr
25072501 ) ?;
2508- if !self . within_group {
2509- if let Some ( order_by) = & self . order_by {
2510- write ! ( f, " ORDER BY {}" , order_by) ?;
2511- }
2512- if let Some ( limit) = & self . limit {
2513- write ! ( f, " LIMIT {}" , limit) ?;
2514- }
2502+ if let Some ( order_by) = & self . order_by {
2503+ write ! ( f, " ORDER BY {}" , order_by) ?;
25152504 }
2516- write ! ( f, ")" ) ?;
2517- if self . within_group {
2518- if let Some ( order_by) = & self . order_by {
2519- write ! ( f, " WITHIN GROUP (ORDER BY {})" , order_by) ?;
2520- }
2505+ if let Some ( limit) = & self . limit {
2506+ write ! ( f, " LIMIT {}" , limit) ?;
25212507 }
2508+ write ! ( f, ")" ) ?;
2509+ Ok ( ( ) )
2510+ }
2511+ }
2512+
2513+ /// A `WITHIN GROUP` invocation `<expr> WITHIN GROUP (ORDER BY <sort_expr> )`
2514+ #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
2515+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
2516+ pub struct WithinGroup {
2517+ pub expr : Box < Expr > ,
2518+ pub order_by : Vec < OrderByExpr > ,
2519+ }
2520+
2521+ impl fmt:: Display for WithinGroup {
2522+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
2523+ write ! (
2524+ f,
2525+ "{} WITHIN GROUP (ORDER BY {})" ,
2526+ self . expr,
2527+ display_comma_separated( & self . order_by) ,
2528+ ) ?;
25222529 Ok ( ( ) )
25232530 }
25242531}
0 commit comments