mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-09-10 07:56:20 +00:00
Parse ARRAY_AGG for Bigquery and Snowflake (#662)
This commit is contained in:
parent
0428ac742b
commit
87b4a168cb
8 changed files with 156 additions and 4 deletions
|
@ -473,6 +473,7 @@ impl<'a> Parser<'a> {
|
|||
self.expect_token(&Token::LParen)?;
|
||||
self.parse_array_subquery()
|
||||
}
|
||||
Keyword::ARRAY_AGG => self.parse_array_agg_expr(),
|
||||
Keyword::NOT => self.parse_not(),
|
||||
// Here `w` is a word, check if it's a part of a multi-part
|
||||
// identifier, a function call, or a simple identifier:
|
||||
|
@ -1071,6 +1072,54 @@ impl<'a> Parser<'a> {
|
|||
}))
|
||||
}
|
||||
|
||||
pub fn parse_array_agg_expr(&mut self) -> Result<Expr, ParserError> {
|
||||
self.expect_token(&Token::LParen)?;
|
||||
let distinct = self.parse_keyword(Keyword::DISTINCT);
|
||||
let expr = Box::new(self.parse_expr()?);
|
||||
// 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))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let limit = if self.parse_keyword(Keyword::LIMIT) {
|
||||
self.parse_limit()?.map(Box::new)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
self.expect_token(&Token::RParen)?;
|
||||
return Ok(Expr::ArrayAgg(ArrayAgg {
|
||||
distinct,
|
||||
expr,
|
||||
order_by,
|
||||
limit,
|
||||
within_group: false,
|
||||
}));
|
||||
}
|
||||
// Snowflake defines ORDERY BY in within group instead of inside the function like
|
||||
// ANSI SQL.
|
||||
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()?;
|
||||
self.expect_token(&Token::RParen)?;
|
||||
Some(Box::new(order_by_expr))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(Expr::ArrayAgg(ArrayAgg {
|
||||
distinct,
|
||||
expr,
|
||||
order_by: within_group,
|
||||
limit: None,
|
||||
within_group: true,
|
||||
}))
|
||||
}
|
||||
|
||||
// This function parses date/time fields for the EXTRACT function-like
|
||||
// operator, interval qualifiers, and the ceil/floor operations.
|
||||
// EXTRACT supports a wider set of date/time fields than interval qualifiers,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue