Skip to content

Commit 482a3ad

Browse files
authored
Add support for multiple expressions, order by in aggregations (apache#879)
* Add support for multiple expressions, order by in aggregations * Fix formatting errors * Resolve linter errors
1 parent ae3b584 commit 482a3ad

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/ast/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3518,7 +3518,7 @@ impl fmt::Display for ListAggOnOverflow {
35183518
pub struct ArrayAgg {
35193519
pub distinct: bool,
35203520
pub expr: Box<Expr>,
3521-
pub order_by: Option<Box<OrderByExpr>>,
3521+
pub order_by: Option<Vec<OrderByExpr>>,
35223522
pub limit: Option<Box<Expr>>,
35233523
pub within_group: bool, // order by is used inside a within group or not
35243524
}
@@ -3533,7 +3533,7 @@ impl fmt::Display for ArrayAgg {
35333533
)?;
35343534
if !self.within_group {
35353535
if let Some(order_by) = &self.order_by {
3536-
write!(f, " ORDER BY {order_by}")?;
3536+
write!(f, " ORDER BY {}", display_comma_separated(order_by))?;
35373537
}
35383538
if let Some(limit) = &self.limit {
35393539
write!(f, " LIMIT {limit}")?;
@@ -3542,7 +3542,11 @@ impl fmt::Display for ArrayAgg {
35423542
write!(f, ")")?;
35433543
if self.within_group {
35443544
if let Some(order_by) = &self.order_by {
3545-
write!(f, " WITHIN GROUP (ORDER BY {order_by})")?;
3545+
write!(
3546+
f,
3547+
" WITHIN GROUP (ORDER BY {})",
3548+
display_comma_separated(order_by)
3549+
)?;
35463550
}
35473551
}
35483552
Ok(())

src/parser.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,8 +1369,7 @@ impl<'a> Parser<'a> {
13691369
// ANSI SQL and BigQuery define ORDER BY inside function.
13701370
if !self.dialect.supports_within_after_array_aggregation() {
13711371
let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
1372-
let order_by_expr = self.parse_order_by_expr()?;
1373-
Some(Box::new(order_by_expr))
1372+
Some(self.parse_comma_separated(Parser::parse_order_by_expr)?)
13741373
} else {
13751374
None
13761375
};
@@ -1393,10 +1392,13 @@ impl<'a> Parser<'a> {
13931392
self.expect_token(&Token::RParen)?;
13941393
let within_group = if self.parse_keywords(&[Keyword::WITHIN, Keyword::GROUP]) {
13951394
self.expect_token(&Token::LParen)?;
1396-
self.expect_keywords(&[Keyword::ORDER, Keyword::BY])?;
1397-
let order_by_expr = self.parse_order_by_expr()?;
1395+
let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
1396+
Some(self.parse_comma_separated(Parser::parse_order_by_expr)?)
1397+
} else {
1398+
None
1399+
};
13981400
self.expect_token(&Token::RParen)?;
1399-
Some(Box::new(order_by_expr))
1401+
order_by
14001402
} else {
14011403
None
14021404
};

tests/sqlparser_common.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,6 +2065,8 @@ fn parse_array_agg_func() {
20652065
"SELECT ARRAY_AGG(x ORDER BY x) AS a FROM T",
20662066
"SELECT ARRAY_AGG(x ORDER BY x LIMIT 2) FROM tbl",
20672067
"SELECT ARRAY_AGG(DISTINCT x ORDER BY x LIMIT 2) FROM tbl",
2068+
"SELECT ARRAY_AGG(x ORDER BY x, y) AS a FROM T",
2069+
"SELECT ARRAY_AGG(x ORDER BY x ASC, y DESC) AS a FROM T",
20682070
] {
20692071
supported_dialects.verified_stmt(sql);
20702072
}

0 commit comments

Comments
 (0)