@@ -1748,6 +1748,7 @@ def render(
17481748
17491749 variables = env .get (c .SQLMESH_VARS , {})
17501750 variables .update (kwargs .pop ("variables" , {}))
1751+ variables .update (env .get (c .SQLMESH_BLUEPRINT_VARS , {}))
17511752
17521753 try :
17531754 kwargs = {
@@ -1855,18 +1856,14 @@ def _extract_blueprints(blueprints: t.Any, path: Path) -> t.List[t.Any]:
18551856 return [] # This is unreachable, but is done to satisfy mypy
18561857
18571858
1858- def _extract_blueprint_variables (
1859- blueprint : t .Any ,
1860- dialect : DialectType ,
1861- path : Path ,
1862- ) -> t .Dict [str , str ]:
1859+ def _extract_blueprint_variables (blueprint : t .Any , path : Path ) -> t .Dict [str , t .Any ]:
18631860 if not blueprint :
18641861 return {}
18651862 if isinstance (blueprint , exp .Paren ):
18661863 blueprint = blueprint .unnest ()
1867- return {blueprint .left .name : blueprint .right . sql ( dialect = dialect ) }
1864+ return {blueprint .left .name : blueprint .right }
18681865 if isinstance (blueprint , (exp .Tuple , exp .Array )):
1869- return {e .left .name : e .right . sql ( dialect = dialect ) for e in blueprint .expressions }
1866+ return {e .left .name : e .right for e in blueprint .expressions }
18701867 if isinstance (blueprint , dict ):
18711868 return blueprint
18721869
@@ -1889,18 +1886,18 @@ def create_models_from_blueprints(
18891886) -> t .List [Model ]:
18901887 model_blueprints : t .List [Model ] = []
18911888 for blueprint in _extract_blueprints (blueprints , path ):
1892- variables = _extract_blueprint_variables (blueprint , dialect , path )
1889+ blueprint_variables = _extract_blueprint_variables (blueprint , path )
18931890
18941891 if gateway :
18951892 rendered_gateway = render_expression (
18961893 expression = exp .maybe_parse (gateway , dialect = dialect ),
18971894 module_path = module_path ,
18981895 macros = loader_kwargs .get ("macros" ),
18991896 jinja_macros = loader_kwargs .get ("jinja_macros" ),
1900- variables = variables ,
19011897 path = path ,
19021898 dialect = dialect ,
19031899 default_catalog = loader_kwargs .get ("default_catalog" ),
1900+ blueprint_variables = blueprint_variables ,
19041901 )
19051902 gateway_name = rendered_gateway [0 ].name if rendered_gateway else None
19061903 else :
@@ -1911,7 +1908,8 @@ def create_models_from_blueprints(
19111908 path = path ,
19121909 module_path = module_path ,
19131910 dialect = dialect ,
1914- variables = {** get_variables (gateway_name ), ** variables },
1911+ variables = get_variables (gateway_name ),
1912+ blueprint_variables = blueprint_variables ,
19151913 ** loader_kwargs ,
19161914 )
19171915 )
@@ -1983,6 +1981,7 @@ def load_sql_based_model(
19831981 default_catalog : t .Optional [str ] = None ,
19841982 variables : t .Optional [t .Dict [str , t .Any ]] = None ,
19851983 infer_names : t .Optional [bool ] = False ,
1984+ blueprint_variables : t .Optional [t .Dict [str , t .Any ]] = None ,
19861985 ** kwargs : t .Any ,
19871986) -> Model :
19881987 """Load a model from a parsed SQLMesh model SQL file.
@@ -2059,6 +2058,7 @@ def load_sql_based_model(
20592058 path = path ,
20602059 dialect = dialect ,
20612060 default_catalog = default_catalog ,
2061+ blueprint_variables = blueprint_variables ,
20622062 )
20632063
20642064 if rendered_meta_exprs is None or len (rendered_meta_exprs ) != 1 :
@@ -2143,6 +2143,7 @@ def load_sql_based_model(
21432143 variables = variables ,
21442144 default_audits = default_audits ,
21452145 inline_audits = inline_audits ,
2146+ blueprint_variables = blueprint_variables ,
21462147 ** meta_fields ,
21472148 )
21482149
@@ -2247,6 +2248,7 @@ def create_python_model(
22472248 module_path : Path = Path (),
22482249 depends_on : t .Optional [t .Set [str ]] = None ,
22492250 variables : t .Optional [t .Dict [str , t .Any ]] = None ,
2251+ blueprint_variables : t .Optional [t .Dict [str , t .Any ]] = None ,
22502252 ** kwargs : t .Any ,
22512253) -> Model :
22522254 """Creates a Python model.
@@ -2259,6 +2261,7 @@ def create_python_model(
22592261 path: An optional path to the model definition file.
22602262 depends_on: The custom set of model's upstream dependencies.
22612263 variables: The variables to pass to the model.
2264+ blueprint_variables: The blueprint's variables to pass to the model.
22622265 """
22632266 # Find dependencies for python models by parsing code if they are not explicitly defined
22642267 # Also remove self-references that are found
@@ -2307,6 +2310,7 @@ def create_python_model(
23072310 jinja_macros = jinja_macros ,
23082311 module_path = module_path ,
23092312 variables = variables ,
2313+ blueprint_variables = blueprint_variables ,
23102314 ** kwargs ,
23112315 )
23122316
@@ -2361,6 +2365,7 @@ def _create_model(
23612365 macros : t .Optional [MacroRegistry ] = None ,
23622366 signal_definitions : t .Optional [SignalRegistry ] = None ,
23632367 variables : t .Optional [t .Dict [str , t .Any ]] = None ,
2368+ blueprint_variables : t .Optional [t .Dict [str , t .Any ]] = None ,
23642369 ** kwargs : t .Any ,
23652370) -> Model :
23662371 _validate_model_fields (klass , {"name" , * kwargs } - {"grain" , "table_properties" }, path )
@@ -2469,6 +2474,8 @@ def _create_model(
24692474 path = path ,
24702475 python_env = python_env ,
24712476 strict_resolution = depends_on is None ,
2477+ blueprint_variables = blueprint_variables ,
2478+ dialect = dialect ,
24722479 )
24732480
24742481 env : t .Dict [str , t .Any ] = {}
@@ -2632,6 +2639,7 @@ def render_meta_fields(
26322639 dialect : DialectType ,
26332640 variables : t .Optional [t .Dict [str , t .Any ]],
26342641 default_catalog : t .Optional [str ],
2642+ blueprint_variables : t .Optional [t .Dict [str , t .Any ]] = None ,
26352643) -> t .Dict [str , t .Any ]:
26362644 def render_field_value (value : t .Any ) -> t .Any :
26372645 if isinstance (value , exp .Expression ) or (isinstance (value , str ) and "@" in value ):
@@ -2645,6 +2653,7 @@ def render_field_value(value: t.Any) -> t.Any:
26452653 path = path ,
26462654 dialect = dialect ,
26472655 default_catalog = default_catalog ,
2656+ blueprint_variables = blueprint_variables ,
26482657 )
26492658 if not rendered_expr :
26502659 raise SQLMeshError (
@@ -2752,6 +2761,7 @@ def render_expression(
27522761 dialect : DialectType = None ,
27532762 variables : t .Optional [t .Dict [str , t .Any ]] = None ,
27542763 default_catalog : t .Optional [str ] = None ,
2764+ blueprint_variables : t .Optional [t .Dict [str , t .Any ]] = None ,
27552765) -> t .Optional [t .List [exp .Expression ]]:
27562766 meta_python_env = make_python_env (
27572767 expressions = expression ,
@@ -2760,6 +2770,7 @@ def render_expression(
27602770 macros = macros or macro .get_registry (),
27612771 variables = variables ,
27622772 path = path ,
2773+ blueprint_variables = blueprint_variables ,
27632774 )
27642775 return ExpressionRenderer (
27652776 expression ,
0 commit comments