44
55from dataclasses import replace
66
7- from sqlbuild .compiler .compile .models import CompiledProject
8- from sqlbuild .compiler .planner ._helpers .changes .detect import detect_changes
7+ from sqlbuild .compiler .compile .models import CompiledObjectKey , CompiledProject
8+ from sqlbuild .compiler .compile .types import CompiledResourceType
9+ from sqlbuild .compiler .planner ._helpers .changes .detect import detect_model_changes_in_scope
910from sqlbuild .compiler .planner ._helpers .identity .direct import (
1011 build_direct_model_version_identities ,
1112)
1213from sqlbuild .compiler .planner .models import (
14+ ChangeDetectionResult ,
15+ DirectModelVersionIdentities ,
1316 PlannerChangeResults ,
1417 PlannerIdentityContext ,
1518 PlannerScopeResolution ,
@@ -21,21 +24,27 @@ def build_planner_identity_context(
2124 * ,
2225 project : CompiledProject ,
2326 scopes : PlannerScopeResolution ,
27+ include_stale_warning_identities : bool = True ,
2428) -> PlannerIdentityContext :
2529 """Build expected version identities for the inspection and stale-warning scopes."""
2630
31+ version_identities : DirectModelVersionIdentities = build_direct_model_version_identities (
32+ functions = project .functions ,
33+ seeds = project .seeds ,
34+ scope = scopes .inspection_scope ,
35+ hook_functions = project .hook_functions ,
36+ )
2737 return PlannerIdentityContext (
28- version_identities = build_direct_model_version_identities (
29- functions = project .functions ,
30- seeds = project .seeds ,
31- scope = scopes .inspection_scope ,
32- hook_functions = project .hook_functions ,
33- ),
34- stale_warning_identities = build_direct_model_version_identities (
35- functions = project .functions ,
36- seeds = project .seeds ,
37- scope = scopes .stale_warning_scope ,
38- hook_functions = project .hook_functions ,
38+ version_identities = version_identities ,
39+ stale_warning_identities = (
40+ build_direct_model_version_identities (
41+ functions = project .functions ,
42+ seeds = project .seeds ,
43+ scope = scopes .stale_warning_scope ,
44+ hook_functions = project .hook_functions ,
45+ )
46+ if include_stale_warning_identities
47+ else version_identities
3948 ),
4049 )
4150
@@ -46,17 +55,64 @@ def detect_stale_warning_changes(
4655 scopes : PlannerScopeResolution ,
4756 snapshot : WarehouseSnapshot ,
4857 identities : PlannerIdentityContext ,
58+ execution_changes : PlannerChangeResults ,
4959) -> PlannerChangeResults :
50- """Detect project-wide changes used for stale-out-of-selection warnings ."""
60+ """Detect only stale-warning model changes not already known from execution planning ."""
5161
52- return detect_changes (
62+ reusable_model_names : frozenset [str ] = _reusable_execution_model_names (
63+ identities = identities ,
64+ execution_changes = execution_changes ,
65+ query_change_tracking = project .settings .query_change_tracking ,
66+ )
67+ stale_keys : frozenset [CompiledObjectKey ] = frozenset (
68+ scopes .stale_warning_scope .all_keys .values ()
69+ )
70+ remaining_model_keys : frozenset [CompiledObjectKey ] = frozenset (
71+ key
72+ for key in stale_keys
73+ if key .resource_type == CompiledResourceType .MODEL and key .name not in reusable_model_names
74+ )
75+ remaining_model_changes : dict [str , ChangeDetectionResult ] = detect_model_changes_in_scope (
5376 project = project ,
5477 scope = replace (
5578 scopes .stale_warning_scope ,
56- selected_keys = frozenset ( scopes . stale_warning_scope . all_keys . values ()) ,
79+ selected_keys = remaining_model_keys ,
5780 ),
5881 snapshot = snapshot ,
59- full_refresh = False ,
82+ query_change_tracking = True ,
6083 expected_version_hashes = identities .stale_warning_identities .model_version_hashes ,
6184 expected_metadata_jsons = identities .stale_warning_identities .model_metadata_jsons ,
6285 )
86+ return PlannerChangeResults (
87+ models = {
88+ ** {
89+ name : change
90+ for name , change in execution_changes .models .items ()
91+ if name in reusable_model_names
92+ },
93+ ** remaining_model_changes ,
94+ },
95+ functions = {},
96+ )
97+
98+
99+ def _reusable_execution_model_names (
100+ * ,
101+ identities : PlannerIdentityContext ,
102+ execution_changes : PlannerChangeResults ,
103+ query_change_tracking : bool ,
104+ ) -> frozenset [str ]:
105+ if not query_change_tracking :
106+ return frozenset ()
107+ execution_identities : DirectModelVersionIdentities = identities .version_identities
108+ stale_identities : DirectModelVersionIdentities = identities .stale_warning_identities
109+ return frozenset (
110+ model_name
111+ for model_name in execution_changes .models
112+ if (
113+ execution_identities .model_version_hashes .get (model_name )
114+ == stale_identities .model_version_hashes .get (model_name )
115+ and execution_identities .model_metadata_jsons .get (model_name )
116+ == stale_identities .model_metadata_jsons .get (model_name )
117+ )
118+ )
0 commit comments