File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree 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 , Dialect
1010from sqlglot .errors import SqlglotError
1111from sqlglot .helper import ensure_list
1212from sqlglot .optimizer .annotate_types import annotate_types
@@ -249,15 +249,24 @@ 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 dialect .parser ().parse (tokens , rendered_expression ) if e
260+ ]
261+
262+ if not expressions :
263+ raise ConfigError (
264+ f"Failed to parse an expression:\n { rendered_expression } "
265+ )
266+ except Exception as ex :
267+ raise ConfigError (
268+ f"Could not parse the rendered jinja at '{ self ._path } '.\n { ex } "
269+ ) from ex
261270
262271 if this_model :
263272 render_kwargs ["this_model" ] = this_model
Original file line number Diff line number Diff line change @@ -873,3 +873,24 @@ 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+ @pytest .mark .slow
879+ def test_jinja_config_no_query (tmp_path , create_empty_project ):
880+ project_dir , model_dir = create_empty_project ()
881+
882+ # model definition contains only a comment and non-SQL jinja
883+ model_contents = "/* comment */ {{ config(materialized='table') }}"
884+ model_file = model_dir / "comment_config_model.sql"
885+ with open (model_file , "w" , encoding = "utf-8" ) as f :
886+ f .write (model_contents )
887+
888+ schema_yaml = {"version" : 2 , "models" : [{"name" : "comment_config_model" }]}
889+ schema_file = model_dir / "schema.yml"
890+ with open (schema_file , "w" , encoding = "utf-8" ) as f :
891+ YAML ().dump (schema_yaml , f )
892+
893+ context = Context (paths = project_dir )
894+
895+ # loads without error and contains empty query (which will error at runtime)
896+ assert not context .snapshots ['"local"."main"."comment_config_model"' ].model .render_query ()
You can’t perform that action at this time.
0 commit comments