1818
1919from sqlmesh .core import constants as c
2020from sqlmesh .core import dialect as d
21- from sqlmesh .core .macros import MacroEvaluator
21+ from sqlmesh .core .macros import MacroEvaluator , RuntimeStage
2222from sqlmesh .utils .date import TimeLike , date_dict , make_inclusive , to_datetime
2323from sqlmesh .utils .errors import (
2424 ConfigError ,
3434
3535 from sqlmesh .core .snapshot import Snapshot
3636
37+ CacheKey = t .Tuple [datetime , datetime , datetime , RuntimeStage ]
38+
3739
3840logger = logging .getLogger (__name__ )
3941
@@ -59,7 +61,7 @@ def __init__(
5961 self ._only_execution_time = only_execution_time
6062 self .schema = {} if schema is None else schema
6163
62- self ._cache : t .Dict [t . Tuple [ datetime , datetime , datetime ] , t .List [exp .Expression ]] = {}
64+ self ._cache : t .Dict [CacheKey , t .List [exp .Expression ]] = {}
6365
6466 def _render (
6567 self ,
@@ -69,6 +71,7 @@ def _render(
6971 snapshots : t .Optional [t .Dict [str , Snapshot ]] = None ,
7072 table_mapping : t .Optional [t .Dict [str , str ]] = None ,
7173 is_dev : bool = False ,
74+ runtime_stage : t .Optional [RuntimeStage ] = None ,
7275 ** kwargs : t .Any ,
7376 ) -> t .List [exp .Expression ]:
7477 """Renders a expression, expanding macros with provided kwargs
@@ -77,18 +80,19 @@ def _render(
7780 start: The start datetime to render. Defaults to epoch start.
7881 end: The end datetime to render. Defaults to epoch start.
7982 execution_time: The date/time time reference to use for execution time.
80- kwargs: Additional kwargs to pass to the renderer.
8183 snapshots: All upstream snapshots (by model name) to use for expansion and mapping of physical locations.
8284 table_mapping: Table mapping of physical locations. Takes precedence over snapshot mappings.
8385 is_dev: Indicates whether the rendering happens in the development mode and temporary
8486 tables / table clones should be used where applicable.
87+ runtime_stage: Indicates the current runtime stage, for example if we're still loading the project, etc.
88+ kwargs: Additional kwargs to pass to the renderer.
8589
8690 Returns:
8791 The rendered expressions.
8892 """
8993
90- cache_key = self ._cache_key (start , end , execution_time )
91- start_dt , end_dt , execution_dt = cache_key
94+ cache_key = self ._cache_key (start , end , execution_time , runtime_stage )
95+ start_dt , end_dt , execution_dt , runtime_stage = cache_key
9296 if cache_key not in self ._cache :
9397 expressions = [self ._expression ]
9498
@@ -132,6 +136,7 @@ def _render(
132136 python_env = self ._python_env ,
133137 jinja_env = jinja_env ,
134138 schema = self .schema ,
139+ runtime_stage = runtime_stage ,
135140 )
136141
137142 for definition in self ._macro_definitions :
@@ -203,10 +208,12 @@ def _cache_key(
203208 start : t .Optional [TimeLike ] = None ,
204209 end : t .Optional [TimeLike ] = None ,
205210 execution_time : t .Optional [TimeLike ] = None ,
206- ) -> t .Tuple [datetime , datetime , datetime ]:
211+ runtime_stage : t .Optional [RuntimeStage ] = None ,
212+ ) -> CacheKey :
207213 return (
208214 * make_inclusive (start or c .EPOCH , end or c .EPOCH ),
209215 to_datetime (execution_time or c .EPOCH ),
216+ runtime_stage or RuntimeStage .LOADING ,
210217 )
211218
212219
@@ -273,7 +280,7 @@ def __init__(
273280
274281 self ._model_name = model_name
275282
276- self ._optimized_cache : t .Dict [t . Tuple [ datetime , datetime , datetime ] , exp .Expression ] = {}
283+ self ._optimized_cache : t .Dict [CacheKey , exp .Expression ] = {}
277284
278285 def render (
279286 self ,
@@ -285,6 +292,7 @@ def render(
285292 is_dev : bool = False ,
286293 expand : t .Iterable [str ] = tuple (),
287294 optimize : bool = True ,
295+ runtime_stage : t .Optional [RuntimeStage ] = None ,
288296 ** kwargs : t .Any ,
289297 ) -> t .Optional [exp .Subqueryable ]:
290298 """Renders a query, expanding macros with provided kwargs, and optionally expanding referenced models.
@@ -302,12 +310,13 @@ def render(
302310 that depend on materialized tables. Model definitions are inlined and can thus be run end to
303311 end on the fly.
304312 optimize: Whether to optimize the query.
313+ runtime_stage: Indicates the current runtime stage, for example if we're still loading the project, etc.
305314 kwargs: Additional kwargs to pass to the renderer.
306315
307316 Returns:
308317 The rendered expression.
309318 """
310- cache_key = self ._cache_key (start , end , execution_time )
319+ cache_key = self ._cache_key (start , end , execution_time , runtime_stage )
311320
312321 if not optimize or cache_key not in self ._optimized_cache :
313322 try :
0 commit comments