Skip to content

Add support for multiple expressions, order by in aggregations #879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3505,7 +3505,7 @@ impl fmt::Display for ListAggOnOverflow {
pub struct ArrayAgg {
pub distinct: bool,
pub expr: Box<Expr>,
pub order_by: Option<Box<OrderByExpr>>,
pub order_by: Option<Vec<OrderByExpr>>,
pub limit: Option<Box<Expr>>,
pub within_group: bool, // order by is used inside a within group or not
}
Expand All @@ -3520,7 +3520,7 @@ impl fmt::Display for ArrayAgg {
)?;
if !self.within_group {
if let Some(order_by) = &self.order_by {
write!(f, " ORDER BY {order_by}")?;
write!(f, " ORDER BY {}", display_comma_separated(order_by))?;
}
if let Some(limit) = &self.limit {
write!(f, " LIMIT {limit}")?;
Expand All @@ -3529,7 +3529,11 @@ impl fmt::Display for ArrayAgg {
write!(f, ")")?;
if self.within_group {
if let Some(order_by) = &self.order_by {
write!(f, " WITHIN GROUP (ORDER BY {order_by})")?;
write!(
f,
" WITHIN GROUP (ORDER BY {})",
display_comma_separated(order_by)
)?;
}
}
Ok(())
Expand Down
12 changes: 7 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1369,8 +1369,7 @@ impl<'a> Parser<'a> {
// ANSI SQL and BigQuery define ORDER BY inside function.
if !self.dialect.supports_within_after_array_aggregation() {
let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
let order_by_expr = self.parse_order_by_expr()?;
Some(Box::new(order_by_expr))
Some(self.parse_comma_separated(Parser::parse_order_by_expr)?)
} else {
None
};
Expand All @@ -1393,10 +1392,13 @@ impl<'a> Parser<'a> {
self.expect_token(&Token::RParen)?;
let within_group = if self.parse_keywords(&[Keyword::WITHIN, Keyword::GROUP]) {
self.expect_token(&Token::LParen)?;
self.expect_keywords(&[Keyword::ORDER, Keyword::BY])?;
let order_by_expr = self.parse_order_by_expr()?;
let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
Some(self.parse_comma_separated(Parser::parse_order_by_expr)?)
} else {
None
};
self.expect_token(&Token::RParen)?;
Some(Box::new(order_by_expr))
order_by
} else {
None
};
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,8 @@ fn parse_array_agg_func() {
"SELECT ARRAY_AGG(x ORDER BY x) AS a FROM T",
"SELECT ARRAY_AGG(x ORDER BY x LIMIT 2) FROM tbl",
"SELECT ARRAY_AGG(DISTINCT x ORDER BY x LIMIT 2) FROM tbl",
"SELECT ARRAY_AGG(x ORDER BY x, y) AS a FROM T",
"SELECT ARRAY_AGG(x ORDER BY x ASC, y DESC) AS a FROM T",
] {
supported_dialects.verified_stmt(sql);
}
Expand Down