Skip to content

Commit

Permalink
Fix: Support comments in Jinja queries coming from dbt models (#3152)
Browse files Browse the repository at this point in the history
  • Loading branch information
izeigerman authored Sep 17, 2024
1 parent 925b046 commit c742bcb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sqlmesh/core/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ def render(
except ParsetimeAdapterCallError:
return None

expressions = [e for e in expressions if not isinstance(e, exp.Semicolon)]

if not expressions:
raise ConfigError(f"Failed to render query at '{self._path}':\n{self._expression}")

Expand Down
36 changes: 36 additions & 0 deletions tests/core/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5858,6 +5858,42 @@ def test_trailing_comments():
assert not model.render_post_statements()


def test_comments_in_jinja_query():
expressions = d.parse(
"""
MODEL (name db.table);
JINJA_QUERY_BEGIN;
/* some comment A */
SELECT 1;
/* some comment B */
JINJA_END;
"""
)
model = load_sql_based_model(expressions)
assert model.render_query().sql() == '/* some comment A */ SELECT 1 AS "1"'

expressions = d.parse(
"""
MODEL (name db.table);
JINJA_QUERY_BEGIN;
/* some comment A */
SELECT 1;
SELECT 2;
/* some comment B */
JINJA_END;
"""
)
model = load_sql_based_model(expressions)
with pytest.raises(ConfigError, match=r"Too many statements in query.*"):
model.render_query()


def test_staged_file_path():
expressions = d.parse(
"""
Expand Down

0 comments on commit c742bcb

Please sign in to comment.