Skip to content

Commit 11ada88

Browse files
authored
Stop compiling Python UDFs like they're Python Models (#12154)
* Only do special python model compilation for python models (not other python nodes) * Test jinja compilation of python udfs * Add changie doc
1 parent b9e5c14 commit 11ada88

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Fixes
2+
body: Stop compiling python udfs like python models
3+
time: 2025-11-06T16:35:14.735442-06:00
4+
custom:
5+
Author: QMalcolm
6+
Issue: "12153"

core/dbt/compilation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ def _compile_code(
599599
if extra_context is None:
600600
extra_context = {}
601601

602-
if node.language == ModelLanguage.python:
602+
if node.language == ModelLanguage.python and node.resource_type == NodeType.Model:
603603
context = self._create_node_context(node, manifest, extra_context)
604604

605605
postfix = jinja.get_rendered(

tests/functional/functions/test_udfs.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ def entry(value):
1717
return value * 2
1818
"""
1919

20+
double_it_py_with_jinja = """
21+
def entry(value):
22+
{% if 1 == 1 %}
23+
return value * 2
24+
{% else %}
25+
{# this should never happen #}
26+
return value * 3
27+
{% endif %}
28+
"""
29+
2030
double_it_deterministic_sql = """
2131
{{ config(volatility='deterministic') }}
2232
SELECT value * 2
@@ -133,6 +143,13 @@ def functions(self) -> Dict[str, str]:
133143
}
134144

135145

146+
scalar_function_python_macro = """
147+
{% macro postgres__scalar_function_python(target_relation) %}
148+
SELECT 1;
149+
{% endmacro %}
150+
"""
151+
152+
136153
class TestBasicSQLUDF(BasicUDFSetup):
137154
def test_basic_parsing(self, project):
138155
# Simple parsing
@@ -387,3 +404,48 @@ def test_can_config_functions_from_project_config(self, project):
387404
function_node = manifest.functions["function.test.double_it"]
388405
# Volatility from sql should take precedence over the project config
389406
assert function_node.config.volatility == FunctionVolatility.Deterministic
407+
408+
409+
class TestPythonFunctionWithoutJinjaHasEquivalentRawCodeAndCompiledCode:
410+
@pytest.fixture(scope="class")
411+
def functions(self) -> Dict[str, str]:
412+
return {
413+
"double_it.py": double_it_py,
414+
"double_it.yml": double_it_python_yml,
415+
}
416+
417+
@pytest.fixture(scope="class")
418+
def macros(self) -> Dict[str, str]:
419+
return {
420+
"postgres__scalar_function_python.sql": scalar_function_python_macro,
421+
}
422+
423+
def test_udfs(self, project):
424+
run_dbt(["build"])
425+
result = run_dbt(["compile"])
426+
assert len(result.results) == 1
427+
node = result.results[0].node
428+
assert isinstance(node, FunctionNode)
429+
assert node.raw_code == node.compiled_code
430+
431+
432+
class TestPythonFunctionWithJinjaHasCorrectCompiledCode:
433+
@pytest.fixture(scope="class")
434+
def functions(self) -> Dict[str, str]:
435+
return {
436+
"double_it.py": double_it_py_with_jinja,
437+
"double_it.yml": double_it_python_yml,
438+
}
439+
440+
@pytest.fixture(scope="class")
441+
def macros(self) -> Dict[str, str]:
442+
return {
443+
"postgres__scalar_function_python.sql": scalar_function_python_macro,
444+
}
445+
446+
def test_udfs(self, project):
447+
result = run_dbt(["compile"])
448+
assert len(result.results) == 1
449+
node = result.results[0].node
450+
assert isinstance(node, FunctionNode)
451+
assert node.compiled_code == "def entry(value):\n \n return value * 2\n "

0 commit comments

Comments
 (0)