@@ -343,6 +343,8 @@ pub enum Expr {
343343 Subquery ( Box < Query > ) ,
344344 /// The `LISTAGG` function `SELECT LISTAGG(...) WITHIN GROUP (ORDER BY ...)`
345345 ListAgg ( ListAgg ) ,
346+ /// The `ARRAY_AGG` function `SELECT ARRAY_AGG(... ORDER BY ...)`
347+ ArrayAgg ( ArrayAgg ) ,
346348 /// The `GROUPING SETS` expr.
347349 GroupingSets ( Vec < Vec < Expr > > ) ,
348350 /// The `CUBE` expr.
@@ -542,6 +544,7 @@ impl fmt::Display for Expr {
542544 Expr :: Subquery ( s) => write ! ( f, "({})" , s) ,
543545 Expr :: ArraySubquery ( s) => write ! ( f, "ARRAY({})" , s) ,
544546 Expr :: ListAgg ( listagg) => write ! ( f, "{}" , listagg) ,
547+ Expr :: ArrayAgg ( arrayagg) => write ! ( f, "{}" , arrayagg) ,
545548 Expr :: GroupingSets ( sets) => {
546549 write ! ( f, "GROUPING SETS (" ) ?;
547550 let mut sep = "" ;
@@ -2448,6 +2451,45 @@ impl fmt::Display for ListAggOnOverflow {
24482451 }
24492452}
24502453
2454+ /// An `ARRAY_AGG` invocation `ARRAY_AGG( [ DISTINCT ] <expr> [ORDER BY <expr>] [LIMIT <n>] )`
2455+ /// Or `ARRAY_AGG( [ DISTINCT ] <expr> ) [ WITHIN GROUP ( ORDER BY <expr> ) ]`
2456+ /// ORDER BY position is defined differently for BigQuery, Postgres and Snowflake.
2457+ #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
2458+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
2459+ pub struct ArrayAgg {
2460+ pub distinct : bool ,
2461+ pub expr : Box < Expr > ,
2462+ pub order_by : Option < Box < OrderByExpr > > ,
2463+ pub limit : Option < Box < Expr > > ,
2464+ pub within_group : bool , // order by is used inside a within group or not
2465+ }
2466+
2467+ impl fmt:: Display for ArrayAgg {
2468+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
2469+ write ! (
2470+ f,
2471+ "ARRAY_AGG({}{}" ,
2472+ if self . distinct { "DISTINCT " } else { "" } ,
2473+ self . expr
2474+ ) ?;
2475+ if !self . within_group {
2476+ if let Some ( order_by) = & self . order_by {
2477+ write ! ( f, " ORDER BY {}" , order_by) ?;
2478+ }
2479+ if let Some ( limit) = & self . limit {
2480+ write ! ( f, " LIMIT {}" , limit) ?;
2481+ }
2482+ }
2483+ write ! ( f, ")" ) ?;
2484+ if self . within_group {
2485+ if let Some ( order_by) = & self . order_by {
2486+ write ! ( f, " WITHIN GROUP (ORDER BY {})" , order_by) ?;
2487+ }
2488+ }
2489+ Ok ( ( ) )
2490+ }
2491+ }
2492+
24512493#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
24522494#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
24532495pub enum ObjectType {
0 commit comments