Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20260217-125507.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: Strip ref() from SQL comments so Jinja never executes them, preventing false
dependencies when ref() appears in -- or /* */ comments
time: 2026-02-17T12:55:07.125871+05:30
custom:
Author: anjutiwari
Issue: "12499"
35 changes: 35 additions & 0 deletions core/dbt/clients/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,36 @@ def __call__(self, *args, **kwargs):
# is small enough that I've just chosen the more readable option.
_HAS_RENDER_CHARS_PAT = re.compile(r"({[{%#]|[#}%]})")

# Pattern to match {{ ref('...') }} in SQL - used to neutralize ref() in comments
_REF_IN_JINJA_PAT = re.compile(r"\{\{\s*ref\s*\(\s*['\"]([^'\"]+)['\"]\s*\)\s*\}\}")

_render_cache: Dict[str, Any] = dict()


# copyright (c) 2026 Atlassian Pty Ltd.
# copyright (c) 2026 Atlassian US, Inc.
def _neutralize_ref_in_sql_comments(string: str) -> str:
"""Replace {{ ref('...') }} in SQL comments with {{ '' }} so Jinja doesn't
execute them and create false dependencies.
"""
lines = []
in_block_comment = False

for line in string.split("\n"):
stripped = line.lstrip()
if stripped.startswith("--"):
line = _REF_IN_JINJA_PAT.sub("{{ '' }}", line)
elif in_block_comment or "/*" in line:
line = _REF_IN_JINJA_PAT.sub("{{ '' }}", line)
if "*/" in line:
in_block_comment = False
elif "/*" in line:
in_block_comment = True
lines.append(line)

return "\n".join(lines)


def get_rendered(
string: str,
ctx: Dict[str, Any],
Expand All @@ -133,6 +160,14 @@ def get_rendered(
elif string in _render_cache:
return _render_cache[string]

if (
isinstance(string, str)
and ("--" in string or "/*" in string)
and "ref" in string
and "{{" in string
):
string = _neutralize_ref_in_sql_comments(string)

template = get_template(
string,
ctx,
Expand Down
24 changes: 23 additions & 1 deletion tests/unit/clients/test_jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import pytest
import yaml

from dbt.clients.jinja import get_rendered, get_template
from dbt.clients.jinja import (
_neutralize_ref_in_sql_comments,
get_rendered,
get_template,
)
from dbt_common.exceptions import JinjaRenderingError


Expand Down Expand Up @@ -414,3 +418,21 @@ def test_native_render():
s = "{{ 1991 | as_text }}"
value = get_rendered(s, {}, native=True)
assert value == "1991"


# copyright (c) 2026 Atlassian Pty Ltd.
# copyright (c) 2026 Atlassian US, Inc.
def test_neutralize_ref_in_sql_comments_removes_ref_from_line_comment():
sql = "-- {{ ref('nonexistent_model') }}\nSELECT 1 as id"
result = _neutralize_ref_in_sql_comments(sql)
assert "ref('nonexistent_model')" not in result
assert "{{ '' }}" in result
assert "SELECT 1 as id" in result

# copyright (c) 2026 Atlassian Pty Ltd.
# copyright (c) 2026 Atlassian US, Inc.
def test_neutralize_ref_in_sql_comments_preserves_ref_in_executable_sql():
sql = "SELECT * FROM {{ ref('my_model') }}"
result = _neutralize_ref_in_sql_comments(sql)
assert "ref('my_model')" in result