@@ -348,6 +348,8 @@ pub enum Expr {
348
348
ListAgg ( ListAgg ) ,
349
349
/// The `ARRAY_AGG` function `SELECT ARRAY_AGG(... ORDER BY ...)`
350
350
ArrayAgg ( ArrayAgg ) ,
351
+ /// The `WITHIN GROUP` expr `... WITHIN GROUP (ORDER BY ...)`
352
+ WithinGroup ( WithinGroup ) ,
351
353
/// The `GROUPING SETS` expr.
352
354
GroupingSets ( Vec < Vec < Expr > > ) ,
353
355
/// The `CUBE` expr.
@@ -549,6 +551,7 @@ impl fmt::Display for Expr {
549
551
Expr :: ArraySubquery ( s) => write ! ( f, "ARRAY({})" , s) ,
550
552
Expr :: ListAgg ( listagg) => write ! ( f, "{}" , listagg) ,
551
553
Expr :: ArrayAgg ( arrayagg) => write ! ( f, "{}" , arrayagg) ,
554
+ Expr :: WithinGroup ( withingroup) => write ! ( f, "{}" , withingroup) ,
552
555
Expr :: GroupingSets ( sets) => {
553
556
write ! ( f, "GROUPING SETS (" ) ?;
554
557
let mut sep = "" ;
@@ -2420,7 +2423,6 @@ pub struct ListAgg {
2420
2423
pub expr : Box < Expr > ,
2421
2424
pub separator : Option < Box < Expr > > ,
2422
2425
pub on_overflow : Option < ListAggOnOverflow > ,
2423
- pub within_group : Vec < OrderByExpr > ,
2424
2426
}
2425
2427
2426
2428
impl fmt:: Display for ListAgg {
@@ -2438,13 +2440,6 @@ impl fmt::Display for ListAgg {
2438
2440
write ! ( f, "{}" , on_overflow) ?;
2439
2441
}
2440
2442
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
- }
2448
2443
Ok ( ( ) )
2449
2444
}
2450
2445
}
@@ -2494,7 +2489,6 @@ pub struct ArrayAgg {
2494
2489
pub expr : Box < Expr > ,
2495
2490
pub order_by : Option < Box < OrderByExpr > > ,
2496
2491
pub limit : Option < Box < Expr > > ,
2497
- pub within_group : bool , // order by is used inside a within group or not
2498
2492
}
2499
2493
2500
2494
impl fmt:: Display for ArrayAgg {
@@ -2505,20 +2499,33 @@ impl fmt::Display for ArrayAgg {
2505
2499
if self . distinct { "DISTINCT " } else { "" } ,
2506
2500
self . expr
2507
2501
) ?;
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) ?;
2515
2504
}
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) ?;
2521
2507
}
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
+ ) ?;
2522
2529
Ok ( ( ) )
2523
2530
}
2524
2531
}
0 commit comments