-
Notifications
You must be signed in to change notification settings - Fork 668
Add support for table valued functions for SQL Server #1839
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
Changes from 14 commits
e2c8b8b
0288177
25a4416
b84ec50
c2fd171
9e45f27
10919b5
6798bcc
846e5c9
e36933c
0e11006
4dd05d1
486f85a
8bc8d38
9b6d6b1
0add6e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8778,6 +8778,30 @@ pub enum CreateFunctionBody { | |
/// | ||
/// [PostgreSQL]: https://www.postgresql.org/docs/current/sql-createfunction.html | ||
Return(Expr), | ||
|
||
/// Function body expression using the 'AS RETURN' keywords | ||
/// | ||
/// Example: | ||
/// ```sql | ||
/// CREATE FUNCTION myfunc(a INT, b INT) | ||
/// RETURNS TABLE | ||
/// AS RETURN (SELECT a + b AS sum); | ||
/// ``` | ||
/// | ||
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql | ||
AsReturnExpr(Expr), | ||
|
||
/// Function body expression using the 'AS RETURN' keywords, with an un-parenthesized SELECT query | ||
/// | ||
/// Example: | ||
/// ```sql | ||
/// CREATE FUNCTION myfunc(a INT, b INT) | ||
/// RETURNS TABLE | ||
/// AS RETURN SELECT a + b AS sum; | ||
/// ``` | ||
/// | ||
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql?view=sql-server-ver16#select_stmt | ||
AsReturnSelect(Select), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we can also include a reference doc link here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done 👍 |
||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5204,19 +5204,79 @@ impl<'a> Parser<'a> { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
let (name, args) = self.parse_create_function_name_and_params()?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.expect_keyword(Keyword::RETURNS)?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let return_type = Some(self.parse_data_type()?); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.expect_keyword_is(Keyword::AS)?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let return_table = self.maybe_parse(|p| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let return_table_name = p.parse_identifier()?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if !p.peek_keyword(Keyword::TABLE) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
parser_err!( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"Expected TABLE keyword after return type", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
p.peek_token().span.start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
)? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
if !p.peek_keyword(Keyword::TABLE) { | |
parser_err!( | |
"Expected TABLE keyword after return type", | |
p.peek_token().span.start | |
)? | |
} | |
p.expect_keyword(Keyword::TABLE)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I didn't do it this way is because expect_keyword (and expect_keyword_is) consume the token. That causes parse_data_type to break, because it uses the TABLE keyword to understand it should parse the table data type.
However, I suppose that I could just call prev_token()
after expect to undo consuming the token. I will make this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DataType::Table(maybe_table_column_defs) => match maybe_table_column_defs { | |
Some(table_column_defs) => { | |
if table_column_defs.is_empty() { | |
parser_err!( | |
"Expected table column definitions after TABLE keyword", | |
p.peek_token().span.start | |
)? | |
} | |
table_column_defs | |
} | |
None => parser_err!( | |
"Expected table column definitions after TABLE keyword", | |
p.peek_token().span.start | |
)?, | |
}, | |
_ => parser_err!( | |
"Expected table data type after TABLE keyword", | |
p.peek_token().span.start | |
)?, | |
}; | |
DataType::Table(Some(table_column_defs)) !if table_column_defs.is_empty() => table_column_defs, | |
_ => parser_err!( | |
"Expected table data type after TABLE keyword", | |
p.peek_token().span.start | |
)?, | |
}; |
looks like this condition can be simplified?
Also could we add a negative test to assert the behavior on invalid data_types, noticed it seems to be lacking coverage in the PR tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I consolidated this per your suggestion, and I added a test to assert a parser error for an incorrect table definition 👍
iffyio marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a test for this behavior? (e.g. one that passes a regular expression and the verifies that the parser rejects it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -254,6 +254,12 @@ fn parse_create_function() { | |
"; | ||
let _ = ms().verified_stmt(multi_statement_function); | ||
|
||
let multi_statement_function_without_as = multi_statement_function.replace(" AS", ""); | ||
let _ = ms().one_statement_parses_to( | ||
&multi_statement_function_without_as, | ||
multi_statement_function, | ||
); | ||
|
||
let create_function_with_conditional = "\ | ||
CREATE FUNCTION some_scalar_udf() \ | ||
RETURNS INT \ | ||
|
@@ -288,6 +294,58 @@ fn parse_create_function() { | |
END\ | ||
"; | ||
let _ = ms().verified_stmt(create_function_with_return_expression); | ||
|
||
let create_inline_table_value_function = "\ | ||
CREATE FUNCTION some_inline_tvf(@foo INT, @bar VARCHAR(256)) \ | ||
RETURNS TABLE \ | ||
AS \ | ||
RETURN (SELECT 1 AS col_1)\ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parentheses are optional for inline tvf There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added support for that syntax & added a new test case example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"; | ||
let _ = ms().verified_stmt(create_inline_table_value_function); | ||
|
||
let create_inline_table_value_function_without_parentheses = "\ | ||
CREATE FUNCTION some_inline_tvf(@foo INT, @bar VARCHAR(256)) \ | ||
RETURNS TABLE \ | ||
AS \ | ||
RETURN SELECT 1 AS col_1\ | ||
"; | ||
let _ = ms().verified_stmt(create_inline_table_value_function_without_parentheses); | ||
|
||
let create_inline_table_value_function_without_as = | ||
create_inline_table_value_function.replace(" AS", ""); | ||
let _ = ms().one_statement_parses_to( | ||
&create_inline_table_value_function_without_as, | ||
create_inline_table_value_function, | ||
); | ||
|
||
let create_multi_statement_table_value_function = "\ | ||
CREATE FUNCTION some_multi_statement_tvf(@foo INT, @bar VARCHAR(256)) \ | ||
RETURNS @t TABLE (col_1 INT) \ | ||
AS \ | ||
BEGIN \ | ||
INSERT INTO @t SELECT 1; \ | ||
RETURN; \ | ||
END\ | ||
"; | ||
let _ = ms().verified_stmt(create_multi_statement_table_value_function); | ||
|
||
let create_multi_statement_table_value_function_without_as = | ||
create_multi_statement_table_value_function.replace(" AS", ""); | ||
let _ = ms().one_statement_parses_to( | ||
&create_multi_statement_table_value_function_without_as, | ||
create_multi_statement_table_value_function, | ||
); | ||
|
||
let create_multi_statement_table_value_function_with_constraints = "\ | ||
CREATE FUNCTION some_multi_statement_tvf(@foo INT, @bar VARCHAR(256)) \ | ||
RETURNS @t TABLE (col_1 INT NOT NULL) \ | ||
AS \ | ||
BEGIN \ | ||
INSERT INTO @t SELECT 1; \ | ||
RETURN @t; \ | ||
END\ | ||
"; | ||
let _ = ms().verified_stmt(create_multi_statement_table_value_function_with_constraints); | ||
} | ||
|
||
#[test] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add a link to the docs that support
NamedTable
variant?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍