Add CREATE FUNCTION support for SQL Server#1808
Merged
iffyio merged 16 commits intoapache:mainfrom Apr 23, 2025
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,
BeginEndStatementwas extracted from the conditional logic to be reused here, along with aRETURNstatement typeDetails
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/1791Then 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 toCREATE PROCEDURElogic).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
;...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 🤔