Skip to content

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

aharpervc
Copy link
Contributor

Note: temporarily rebased on #1834, prior to that being merged; I will rebase once it's on main.


This PR adds support for table valued functions for SQL Server, both inline & multi statement functions. For reference, that's the B & C documentation here: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql?view=sql-server-ver16#b-create-an-inline-table-valued-function

Inline TVF's are defined with AS RETURN, so we have a new CreateFunctionBody::AsReturn variant accordingly. Functions using "AS RETURN" don't have BEGIN/END, so that part of the parsing logic is now conditional. Additionally, the data type parser now supports "RETURNS TABLE" without a table definition.

Multi statement TVF's use named table expressions, so a new NamedTable data type variant was added. I didn't see a great way to integrate this into the existing data type parser (especially without rewinding), so creating this data type happens inside the parse create function logic first by parsing the identifier, then parsing the table definition, then using those elements to produce a NamedTable.

I also added a new test example for each of these scenarios.

aharpervc added 2 commits May 5, 2025 11:36
- formerly, a semicolon after the last statement in a procedure was non-canonical (because they were added via `join`); a `BeginEndStatements` statements list will always write them out
- `BeginEndStatements` begin/end tokens won't be written when empty
- EOF now concludes parsing a statement list
…tokens

- this further consolidates with existing patterns
@aharpervc aharpervc marked this pull request as ready for review May 6, 2025 19:24
@aharpervc aharpervc changed the title Add support for table valued functions for SQL Serve Add support for table valued functions for SQL Server May 6, 2025
@aharpervc aharpervc force-pushed the mssql-create-tvf branch from 2b2a68a to c04172c Compare May 6, 2025 20:19
@aharpervc aharpervc force-pushed the mssql-create-tvf branch from c04172c to 10e3231 Compare May 6, 2025 20:24
@aharpervc aharpervc force-pushed the mssql-create-tvf branch from 3686b1b to db1c9b2 Compare May 6, 2025 23:37
CREATE FUNCTION some_inline_tvf(@foo INT, @bar VARCHAR(256)) \
RETURNS TABLE \
AS \
RETURN (SELECT 1 AS col_1)\
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parentheses are optional for inline tvf return queries, although I think the subquery expr expects/requires them currently.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added support for that syntax & added a new test case example

Copy link
Contributor Author

@aharpervc aharpervc May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UNION is also supported in this syntax but not in this current approach due to using parse_select, which is restricted. I'm comfortable leaving that for later

- rename `AsReturn` to `AsReturnSubquery` for clarity between these two variants
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant