@@ -446,12 +446,14 @@ def engine_adapter(self) -> EngineAdapter:
446446 self ._engine_adapter = self .connection_config .create_engine_adapter ()
447447 return self ._engine_adapter
448448
449- @property
450- def snapshot_evaluator (self ) -> SnapshotEvaluator :
451- if not self ._snapshot_evaluator :
449+ def snapshot_evaluator (self , job_id : t .Optional [str ] = None ) -> SnapshotEvaluator :
450+ # Cache snapshot evaluators by job_id to avoid old job_ids being attached to future Context operations
451+ if not self ._snapshot_evaluator or any (
452+ adapter ._job_id != job_id for adapter in self ._snapshot_evaluator .adapters .values ()
453+ ):
452454 self ._snapshot_evaluator = SnapshotEvaluator (
453455 {
454- gateway : adapter .with_log_level (logging .INFO )
456+ gateway : adapter .with_log_level (logging .INFO , job_id )
455457 for gateway , adapter in self .engine_adapters .items ()
456458 },
457459 ddl_concurrent_tasks = self .concurrent_tasks ,
@@ -538,7 +540,9 @@ def scheduler(self, environment: t.Optional[str] = None) -> Scheduler:
538540
539541 return self .create_scheduler (snapshots )
540542
541- def create_scheduler (self , snapshots : t .Iterable [Snapshot ]) -> Scheduler :
543+ def create_scheduler (
544+ self , snapshots : t .Iterable [Snapshot ], job_id : t .Optional [str ] = None
545+ ) -> Scheduler :
542546 """Creates the built-in scheduler.
543547
544548 Args:
@@ -549,7 +553,7 @@ def create_scheduler(self, snapshots: t.Iterable[Snapshot]) -> Scheduler:
549553 """
550554 return Scheduler (
551555 snapshots ,
552- self .snapshot_evaluator ,
556+ self .snapshot_evaluator ( job_id ) ,
553557 self .state_sync ,
554558 default_catalog = self .default_catalog ,
555559 max_workers = self .concurrent_tasks ,
@@ -714,7 +718,7 @@ def run(
714718 NotificationEvent .RUN_START , environment = environment
715719 )
716720 analytics_run_id = analytics .collector .on_run_start (
717- engine_type = self .snapshot_evaluator .adapter .dialect ,
721+ engine_type = self .snapshot_evaluator () .adapter .dialect ,
718722 state_sync_type = self .state_sync .state_type (),
719723 )
720724 self ._load_materializations ()
@@ -1076,7 +1080,7 @@ def evaluate(
10761080 and not parent_snapshot .categorized
10771081 ]
10781082
1079- df = self .snapshot_evaluator .evaluate_and_fetch (
1083+ df = self .snapshot_evaluator () .evaluate_and_fetch (
10801084 snapshot ,
10811085 start = start ,
10821086 end = end ,
@@ -2110,7 +2114,7 @@ def audit(
21102114 errors = []
21112115 skipped_count = 0
21122116 for snapshot in snapshots :
2113- for audit_result in self .snapshot_evaluator .audit (
2117+ for audit_result in self .snapshot_evaluator () .audit (
21142118 snapshot = snapshot ,
21152119 start = start ,
21162120 end = end ,
@@ -2142,7 +2146,7 @@ def audit(
21422146 self .console .log_status_update (f"Got { error .count } results, expected 0." )
21432147 if error .query :
21442148 self .console .show_sql (
2145- f"{ error .query .sql (dialect = self .snapshot_evaluator .adapter .dialect )} "
2149+ f"{ error .query .sql (dialect = self .snapshot_evaluator () .adapter .dialect )} "
21462150 )
21472151
21482152 self .console .log_status_update ("Done." )
@@ -2388,7 +2392,7 @@ def _run(
23882392 return completion_status
23892393
23902394 def _apply (self , plan : Plan , circuit_breaker : t .Optional [t .Callable [[], bool ]]) -> None :
2391- self ._scheduler .create_plan_evaluator (self ).evaluate (
2395+ self ._scheduler .create_plan_evaluator (self , job_id = plan . plan_id ).evaluate (
23922396 plan .to_evaluatable (), circuit_breaker = circuit_breaker
23932397 )
23942398
@@ -2682,7 +2686,7 @@ def _run_janitor(self, ignore_ttl: bool = False) -> None:
26822686 )
26832687
26842688 # Remove the expired snapshots tables
2685- self .snapshot_evaluator .cleanup (
2689+ self .snapshot_evaluator () .cleanup (
26862690 target_snapshots = cleanup_targets ,
26872691 on_complete = self .console .update_cleanup_progress ,
26882692 )
0 commit comments