File tree Expand file tree Collapse file tree 2 files changed +32
-10
lines changed
Expand file tree Collapse file tree 2 files changed +32
-10
lines changed Original file line number Diff line number Diff line change 66from functools import partial
77from pathlib import Path
88
9- from sqlglot import exp , parse
9+ from sqlglot import exp , parse , Dialect
1010from sqlglot .errors import SqlglotError
1111from sqlglot .helper import ensure_list
1212from sqlglot .optimizer .annotate_types import annotate_types
@@ -249,15 +249,22 @@ def _resolve_table(table: str | exp.Table) -> str:
249249 ) from ex
250250
251251 if rendered_expression .strip ():
252- try :
253- expressions = [e for e in parse (rendered_expression , read = self ._dialect ) if e ]
254-
255- if not expressions :
256- raise ConfigError (f"Failed to parse an expression:\n { self ._expression } " )
257- except Exception as ex :
258- raise ConfigError (
259- f"Could not parse the rendered jinja at '{ self ._path } '.\n { ex } "
260- ) from ex
252+ # ensure there is actual SQL and not just comments and non-SQL jinja
253+ dialect = Dialect .get_or_raise (self ._dialect )
254+ tokens = dialect .tokenize (rendered_expression )
255+
256+ if tokens :
257+ try :
258+ expressions = [
259+ e for e in parse (rendered_expression , read = self ._dialect ) if e
260+ ]
261+
262+ if not expressions :
263+ raise ConfigError (f"Failed to parse an expression:\n { self ._expression } " )
264+ except Exception as ex :
265+ raise ConfigError (
266+ f"Could not parse the rendered jinja at '{ self ._path } '.\n { ex } "
267+ ) from ex
261268
262269 if this_model :
263270 render_kwargs ["this_model" ] = this_model
Original file line number Diff line number Diff line change @@ -873,3 +873,18 @@ def test_load_model_dbt_node_name(tmp_path: Path) -> None:
873873 # Verify that node_name is the equivalent dbt one
874874 model = context .snapshots [model_fqn ].model
875875 assert model .dbt_name == "model.test_project.simple_model"
876+
877+
878+ def test_jinja_config_no_query (tmp_path , create_empty_project ):
879+ project_dir , model_dir = create_empty_project ()
880+
881+ # model definition contains only a comment and non-SQL jinja
882+ model_contents = "/* comment */ {{ config(materialized='table') }}"
883+ model_file = model_dir / "comment_config_model.sql"
884+ with open (model_file , "w" , encoding = "utf-8" ) as f :
885+ f .write (model_contents )
886+
887+ context = Context (paths = project_dir )
888+
889+ # loads without error and contains empty query (which will error at runtime)
890+ assert not context .snapshots ['"local"."main"."comment_config_model"' ].model .render_query ()
You can’t perform that action at this time.
0 commit comments