Skip to content

Add CREATE FUNCTION support for SQL Server#1808

Merged
iffyio merged 16 commits intoapache:mainfrom
aharpervc:mssql-create-function
Apr 23, 2025
Merged

Add CREATE FUNCTION support for SQL Server#1808
iffyio merged 16 commits intoapache:mainfrom
aharpervc:mssql-create-function

Conversation

@aharpervc
Copy link
Copy Markdown
Contributor

@aharpervc aharpervc commented Apr 10, 2025

Partially related: #1800


For reference: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql?view=sql-server-ver16

In SQL Server, functions look a lot like procedures, with begin/end, multiple statements, and a return. To accommodate that here in the parser, BeginEndStatement was extracted from the conditional logic to be reused here, along with a RETURN statement type

Details Formerly, this was a new struct, but was later consolidated in with BeginEndStatement. I'm open to suggestions about how this could be improve (now or later). For example, since it's a BEGIN (statements) END construct, should this (and procedures) be using the statement block logic introduced here? https://github.com//pull/1791

Then also while I was working on this, I noticed that only SQL Server (so far...?) supports OR ALTER, so I introduced a new struct field to track that. (Again, very similar to CREATE PROCEDURE logic).

I'm not aiming to enable parsing every function type here in this PR -- just trying to get something operational, and the additional capabilities like table expressions, etc, can be added in later.


To continue the recent discussion, I noticed that both these new multi statement functions & the existing procedure parsing fails unless the statements are semi-colon delimited. Semi-colons are optional in SQL Server, so for example, this code executes fine for real but fails to parse here in this library, unless that "SET" ends with a ;...

CREATE OR ALTER PROCEDURE test (@foo INT, @bar VARCHAR(256))
AS
BEGIN
    SET @foo = @foo + 1
    SELECT @foo
END

I think this is because the procedure body parsing logic is using parse_statements(), which requires it? I'm game to tackle the problem with some guidance on what's a good approach to make it parse 🤔

Loading
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.

3 participants