Skip to content

feat: add UDF function resource support and adapter tests for dbt-core v1.11#763

Open
axellpadilla wants to merge 2 commits into
dbt-msft:masterfrom
axellpadilla:feat/add-function-resource-tests
Open

feat: add UDF function resource support and adapter tests for dbt-core v1.11#763
axellpadilla wants to merge 2 commits into
dbt-msft:masterfrom
axellpadilla:feat/add-function-resource-tests

Conversation

@axellpadilla

Copy link
Copy Markdown
Collaborator

Summary

Adds UDF (user-defined function) resource support and adapter tests for dbt-core v1.11's new function resource type (issue #761, part of #719).

Changes

  • Added Function = "function" to SQLServerRelationType enum

New: dbt/include/sqlserver/macros/materializations/functions/scalar.sql

SQL Server-specific scalar UDF macros:

  • sqlserver__scalar_function_sql — main entry point
  • sqlserver__scalar_function_create_replace_signature_sql — uses CREATE OR ALTER FUNCTION with 2-part names (SQL Server forbids database prefix)
  • sqlserver__formatted_scalar_function_args_sql@param type syntax with default value support
  • sqlserver__scalar_function_body_sqlBEGIN RETURN ... END
  • sqlserver__scalar_function_volatility_sql — no-op (SQL Server has no volatility keywords)
  • sqlserver__unsupported_volatility_warning — warns when volatility is set

New: dbt/include/sqlserver/macros/materializations/functions/helpers.sql

  • sqlserver__function_execute_build_sql — runs USE [db] in a separate batch before CREATE FUNCTION (required because CREATE FUNCTION must be the first statement in a batch)

New: tests/functional/adapter/dbt/test_functions.py

15 test classes (13 active, 2 skipped for UDAFs):

  • TestUDFsBasic — scalar UDF build + execute
  • TestDeterministicUDF, TestStableUDF, TestNonDeterministicUDF — volatility warnings
  • TestErrorForUnsupportedTypetype: table fails gracefully
  • TestPythonUDFNotSupported — Python UDFs fail gracefully
  • TestSqlUDFDefaultArgSupport — default parameter values
  • TestUDFParsedbt parse with functions/
  • TestUDFListdbt list --select resource_type:function
  • TestUDFBuilddbt build --select resource_type:function
  • TestUDFModelRef — model using {{ function('name') }}
  • TestUDFSchemaCreation — schema created before function
  • TestUDFRebuild — function rebuild via CREATE OR ALTER
  • TestBasicSQLUDAF, TestBasicPythonUDAF — skipped (SQL Server doesn't support aggregate functions)

SQL Server-Specific Adaptations

Issue Solution
3-part names forbidden in CREATE FUNCTION USE [db] in separate batch, then 2-part name
CREATE FUNCTION must be first statement Separate USE batch via function_execute_build_sql override
No IMMUTABLE/STABLE/VOLATILE Emit nothing + warning
Parameters require @ prefix Override formatted_scalar_function_args_sql
Default params use = value syntax Override formatted_scalar_function_args_sql
Function body uses BEGIN...END Override scalar_function_body_sql
Calling with defaults requires DEFAULT keyword Test uses func(DEFAULT) not func()

Testing

All 13 active tests pass against a live SQL Server instance. 2 UDAF tests skipped with explicit reason.

@axellpadilla
axellpadilla force-pushed the feat/add-function-resource-tests branch from 52fa00e to 727ec57 Compare July 24, 2026 23:15
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.

SQL Server function resources are either supported or fail with a clear unsupported error. Function resource tests are added.

1 participant