Skip to content

Commit 9e45f27

Browse files
committed
Add support for un-parenthesized "RETURN SELECT" syntax
- rename `AsReturn` to `AsReturnSubquery` for clarity between these two variants
1 parent c2fd171 commit 9e45f27

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

src/ast/ddl.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2346,7 +2346,10 @@ impl fmt::Display for CreateFunction {
23462346
if let Some(CreateFunctionBody::Return(function_body)) = &self.function_body {
23472347
write!(f, " RETURN {function_body}")?;
23482348
}
2349-
if let Some(CreateFunctionBody::AsReturn(function_body)) = &self.function_body {
2349+
if let Some(CreateFunctionBody::AsReturnSubquery(function_body)) = &self.function_body {
2350+
write!(f, " AS RETURN {function_body}")?;
2351+
}
2352+
if let Some(CreateFunctionBody::AsReturnSelect(function_body)) = &self.function_body {
23502353
write!(f, " AS RETURN {function_body}")?;
23512354
}
23522355
if let Some(using) = &self.using {

src/ast/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8789,7 +8789,17 @@ pub enum CreateFunctionBody {
87898789
/// ```
87908790
///
87918791
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql
8792-
AsReturn(Expr),
8792+
AsReturnSubquery(Expr),
8793+
8794+
/// Function body expression using the 'AS RETURN' keywords, with an un-parenthesized SELECT query
8795+
///
8796+
/// Example:
8797+
/// ```sql
8798+
/// CREATE FUNCTION myfunc(a INT, b INT)
8799+
/// RETURNS TABLE
8800+
/// AS RETURN SELECT a + b AS sum;
8801+
/// ```
8802+
AsReturnSelect(Select),
87938803
}
87948804

87958805
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]

src/parser/mod.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5249,11 +5249,25 @@ impl<'a> Parser<'a> {
52495249
}))
52505250
} else if self.peek_keyword(Keyword::RETURN) {
52515251
self.expect_keyword(Keyword::RETURN)?;
5252-
let expr = self.parse_expr()?;
5253-
if !matches!(expr, Expr::Subquery(_)) {
5254-
parser_err!("Expected a subquery after RETURN", expr.span().start)?;
5252+
5253+
if self.peek_token() == Token::LParen {
5254+
let expr = self.parse_expr()?;
5255+
if !matches!(expr, Expr::Subquery(_)) {
5256+
parser_err!(
5257+
"Expected a subquery after RETURN",
5258+
self.peek_token().span.start
5259+
)?
5260+
}
5261+
Some(CreateFunctionBody::AsReturnSubquery(expr))
5262+
} else if self.peek_keyword(Keyword::SELECT) {
5263+
let select = self.parse_select()?;
5264+
Some(CreateFunctionBody::AsReturnSelect(select))
5265+
} else {
5266+
parser_err!(
5267+
"Expected a subquery (or bare SELECT statement) after RETURN",
5268+
self.peek_token().span.start
5269+
)?
52555270
}
5256-
Some(CreateFunctionBody::AsReturn(expr))
52575271
} else {
52585272
parser_err!("Unparsable function body", self.peek_token().span.start)?
52595273
};

tests/sqlparser_mssql.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@ fn parse_create_function() {
303303
";
304304
let _ = ms().verified_stmt(create_inline_table_value_function);
305305

306+
let create_inline_table_value_function_without_parentheses = "\
307+
CREATE FUNCTION some_inline_tvf(@foo INT, @bar VARCHAR(256)) \
308+
RETURNS TABLE \
309+
AS \
310+
RETURN SELECT 1 AS col_1\
311+
";
312+
let _ = ms().verified_stmt(create_inline_table_value_function_without_parentheses);
313+
306314
let create_inline_table_value_function_without_as =
307315
create_inline_table_value_function.replace(" AS", "");
308316
let _ = ms().one_statement_parses_to(

0 commit comments

Comments
 (0)