Add CREATE FUNCTION support for SQL Server (#1808)

This commit is contained in:
Andrew Harper 2025-04-23 12:10:57 -04:00 committed by GitHub
parent 945f8e0534
commit 2eb1e7bdd4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 313 additions and 50 deletions

View file

@ -187,6 +187,92 @@ fn parse_mssql_create_procedure() {
let _ = ms().verified_stmt("CREATE PROCEDURE [foo] AS BEGIN UPDATE bar SET col = 'test'; SELECT [foo] FROM BAR WHERE [FOO] > 10 END");
}
#[test]
fn parse_create_function() {
let return_expression_function = "CREATE FUNCTION some_scalar_udf(@foo INT, @bar VARCHAR(256)) RETURNS INT AS BEGIN RETURN 1; END";
assert_eq!(
ms().verified_stmt(return_expression_function),
sqlparser::ast::Statement::CreateFunction(CreateFunction {
or_alter: false,
or_replace: false,
temporary: false,
if_not_exists: false,
name: ObjectName::from(vec![Ident::new("some_scalar_udf")]),
args: Some(vec![
OperateFunctionArg {
mode: None,
name: Some(Ident::new("@foo")),
data_type: DataType::Int(None),
default_expr: None,
},
OperateFunctionArg {
mode: None,
name: Some(Ident::new("@bar")),
data_type: DataType::Varchar(Some(CharacterLength::IntegerLength {
length: 256,
unit: None
})),
default_expr: None,
},
]),
return_type: Some(DataType::Int(None)),
function_body: Some(CreateFunctionBody::AsBeginEnd(BeginEndStatements {
begin_token: AttachedToken::empty(),
statements: vec![Statement::Return(ReturnStatement {
value: Some(ReturnStatementValue::Expr(Expr::Value(
(number("1")).with_empty_span()
))),
}),],
end_token: AttachedToken::empty(),
})),
behavior: None,
called_on_null: None,
parallel: None,
using: None,
language: None,
determinism_specifier: None,
options: None,
remote_connection: None,
}),
);
let multi_statement_function = "\
CREATE FUNCTION some_scalar_udf(@foo INT, @bar VARCHAR(256)) \
RETURNS INT \
AS \
BEGIN \
SET @foo = @foo + 1; \
RETURN @foo; \
END\
";
let _ = ms().verified_stmt(multi_statement_function);
let create_function_with_conditional = "\
CREATE FUNCTION some_scalar_udf() \
RETURNS INT \
AS \
BEGIN \
IF 1 = 2 \
BEGIN \
RETURN 1; \
END; \
RETURN 0; \
END\
";
let _ = ms().verified_stmt(create_function_with_conditional);
let create_or_alter_function = "\
CREATE OR ALTER FUNCTION some_scalar_udf(@foo INT, @bar VARCHAR(256)) \
RETURNS INT \
AS \
BEGIN \
SET @foo = @foo + 1; \
RETURN @foo; \
END\
";
let _ = ms().verified_stmt(create_or_alter_function);
}
#[test]
fn parse_mssql_apply_join() {
let _ = ms_and_generic().verified_only_select(