@@ -386,6 +386,8 @@ pub enum Expr {
386386 Subquery ( Box < Query > ) ,
387387 /// The `LISTAGG` function `SELECT LISTAGG(...) WITHIN GROUP (ORDER BY ...)`
388388 ListAgg ( ListAgg ) ,
389+ /// The `ARRAY_AGG` function `SELECT ARRAY_AGG(... ORDER BY ...)`
390+ ArrayAgg ( ArrayAgg ) ,
389391 /// The `GROUPING SETS` expr.
390392 GroupingSets ( Vec < Vec < Expr > > ) ,
391393 /// The `CUBE` expr.
@@ -580,6 +582,7 @@ impl fmt::Display for Expr {
580582 Expr :: Subquery ( s) => write ! ( f, "({})" , s) ,
581583 Expr :: ArraySubquery ( s) => write ! ( f, "ARRAY({})" , s) ,
582584 Expr :: ListAgg ( listagg) => write ! ( f, "{}" , listagg) ,
585+ Expr :: ArrayAgg ( arrayagg) => write ! ( f, "{}" , arrayagg) ,
583586 Expr :: GroupingSets ( sets) => {
584587 write ! ( f, "GROUPING SETS (" ) ?;
585588 let mut sep = "" ;
@@ -2491,6 +2494,45 @@ impl fmt::Display for ListAggOnOverflow {
24912494 }
24922495}
24932496
2497+ /// An `ARRAY_AGG` invocation `ARRAY_AGG( [ DISTINCT ] <expr> [ORDER BY <expr>] [LIMIT <n>] )`
2498+ /// Or `ARRAY_AGG( [ DISTINCT ] <expr> ) [ WITHIN GROUP ( ORDER BY <expr> ) ]`
2499+ /// ORDER BY position is defined differently for BigQuery, Postgres and Snowflake.
2500+ #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
2501+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
2502+ pub struct ArrayAgg {
2503+ pub distinct : bool ,
2504+ pub expr : Box < Expr > ,
2505+ pub order_by : Option < Box < OrderByExpr > > ,
2506+ pub limit : Option < Box < Expr > > ,
2507+ pub within_group : bool , // order by is used inside a within group or not
2508+ }
2509+
2510+ impl fmt:: Display for ArrayAgg {
2511+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
2512+ write ! (
2513+ f,
2514+ "ARRAY_AGG({}{}" ,
2515+ if self . distinct { "DISTINCT " } else { "" } ,
2516+ self . expr
2517+ ) ?;
2518+ if !self . within_group {
2519+ if let Some ( order_by) = & self . order_by {
2520+ write ! ( f, " ORDER BY {}" , order_by) ?;
2521+ }
2522+ if let Some ( limit) = & self . limit {
2523+ write ! ( f, " LIMIT {}" , limit) ?;
2524+ }
2525+ }
2526+ write ! ( f, ")" ) ?;
2527+ if self . within_group {
2528+ if let Some ( order_by) = & self . order_by {
2529+ write ! ( f, " WITHIN GROUP (ORDER BY {})" , order_by) ?;
2530+ }
2531+ }
2532+ Ok ( ( ) )
2533+ }
2534+ }
2535+
24942536#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
24952537#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
24962538pub enum ObjectType {
0 commit comments