mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-09-01 03:37:21 +00:00
Support subquery as function arg w/o parens in Snowflake dialect (#996)
This commit is contained in:
parent
5c10668dbb
commit
56f24ce236
2 changed files with 39 additions and 1 deletions
|
@ -1507,7 +1507,7 @@ impl<'a> Parser<'a> {
|
||||||
within_group: false,
|
within_group: false,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
// Snowflake defines ORDERY BY in within group instead of inside the function like
|
// Snowflake defines ORDER BY in within group instead of inside the function like
|
||||||
// ANSI SQL.
|
// ANSI SQL.
|
||||||
self.expect_token(&Token::RParen)?;
|
self.expect_token(&Token::RParen)?;
|
||||||
let within_group = if self.parse_keywords(&[Keyword::WITHIN, Keyword::GROUP]) {
|
let within_group = if self.parse_keywords(&[Keyword::WITHIN, Keyword::GROUP]) {
|
||||||
|
@ -6914,6 +6914,24 @@ impl<'a> Parser<'a> {
|
||||||
if self.consume_token(&Token::RParen) {
|
if self.consume_token(&Token::RParen) {
|
||||||
Ok((vec![], vec![]))
|
Ok((vec![], vec![]))
|
||||||
} else {
|
} else {
|
||||||
|
// Snowflake permits a subquery to be passed as an argument without
|
||||||
|
// an enclosing set of parens if it's the only argument.
|
||||||
|
if dialect_of!(self is SnowflakeDialect)
|
||||||
|
&& self
|
||||||
|
.parse_one_of_keywords(&[Keyword::WITH, Keyword::SELECT])
|
||||||
|
.is_some()
|
||||||
|
{
|
||||||
|
self.prev_token();
|
||||||
|
let subquery = self.parse_query()?;
|
||||||
|
self.expect_token(&Token::RParen)?;
|
||||||
|
return Ok((
|
||||||
|
vec![FunctionArg::Unnamed(FunctionArgExpr::from(
|
||||||
|
WildcardExpr::Expr(Expr::Subquery(Box::new(subquery))),
|
||||||
|
))],
|
||||||
|
vec![],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
let args = self.parse_comma_separated(Parser::parse_function_args)?;
|
let args = self.parse_comma_separated(Parser::parse_function_args)?;
|
||||||
let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
|
let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
|
||||||
self.parse_comma_separated(Parser::parse_order_by_expr)?
|
self.parse_comma_separated(Parser::parse_order_by_expr)?
|
||||||
|
|
|
@ -1065,3 +1065,23 @@ fn test_snowflake_trim() {
|
||||||
snowflake().parse_sql_statements(error_sql).unwrap_err()
|
snowflake().parse_sql_statements(error_sql).unwrap_err()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_subquery_function_argument() {
|
||||||
|
// Snowflake allows passing an unparenthesized subquery as the single
|
||||||
|
// argument to a function.
|
||||||
|
snowflake().one_statement_parses_to(
|
||||||
|
"SELECT parse_json(SELECT '{}')",
|
||||||
|
"SELECT parse_json((SELECT '{}'))",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Subqueries that begin with WITH work too.
|
||||||
|
snowflake().one_statement_parses_to(
|
||||||
|
"SELECT parse_json(WITH q AS (SELECT '{}' AS foo) SELECT foo FROM q)",
|
||||||
|
"SELECT parse_json((WITH q AS (SELECT '{}' AS foo) SELECT foo FROM q))",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Commas are parsed as part of the subquery, not additional arguments to
|
||||||
|
// the function.
|
||||||
|
snowflake().one_statement_parses_to("SELECT func(SELECT 1, 2)", "SELECT func((SELECT 1, 2))");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue