@@ -41,7 +41,7 @@ def make_python_env(
41
41
module_path : Path ,
42
42
macros : MacroRegistry ,
43
43
variables : t .Optional [t .Dict [str , t .Any ]] = None ,
44
- used_variables : t .Optional [t .Set [str ]] = None ,
44
+ referenced_variables : t .Optional [t .Set [str ]] = None ,
45
45
path : t .Optional [Path ] = None ,
46
46
python_env : t .Optional [t .Dict [str , Executable ]] = None ,
47
47
strict_resolution : bool = True ,
@@ -55,7 +55,7 @@ def make_python_env(
55
55
blueprint_variables = blueprint_variables or {}
56
56
57
57
used_macros : t .Dict [str , t .Tuple [MacroCallable , bool ]] = {}
58
- used_variable_referenced_in_metadata_expression = dict .fromkeys (used_variables or set (), False )
58
+ used_variables = dict .fromkeys (referenced_variables or set (), False ) # var -> is_metadata
59
59
60
60
# For an expression like @foo(@v1, @bar(@v1, @v2), @v3), the following mapping would be:
61
61
# v1 -> {"foo", "bar"}, v2 -> {"bar"}, v3 -> "foo"
@@ -96,10 +96,7 @@ def make_python_env(
96
96
)
97
97
98
98
var_name = args [0 ].this .lower ()
99
- used_variable_referenced_in_metadata_expression [var_name ] = (
100
- used_variable_referenced_in_metadata_expression .get (var_name , True )
101
- and is_metadata
102
- )
99
+ used_variables [var_name ] = used_variables .get (var_name , True ) and is_metadata
103
100
else :
104
101
for var_ref in _extract_macro_func_variable_references (macro_func_or_var ):
105
102
macro_funcs_by_used_var [var_ref ].add (name )
@@ -112,10 +109,7 @@ def make_python_env(
112
109
used_macros .get (name , (None , is_metadata ))[1 ] and is_metadata ,
113
110
)
114
111
elif name in variables or name in blueprint_variables :
115
- used_variable_referenced_in_metadata_expression [name ] = (
116
- used_variable_referenced_in_metadata_expression .get (name , True )
117
- and is_metadata
118
- )
112
+ used_variables [name ] = used_variables .get (name , True ) and is_metadata
119
113
elif (
120
114
isinstance (macro_func_or_var , (exp .Identifier , d .MacroStrReplace , d .MacroSQL ))
121
115
) and "@" in macro_func_or_var .name :
@@ -124,9 +118,8 @@ def make_python_env(
124
118
):
125
119
var_name = braced_identifier or identifier
126
120
if var_name in variables or var_name in blueprint_variables :
127
- used_variable_referenced_in_metadata_expression [var_name ] = (
128
- used_variable_referenced_in_metadata_expression .get (var_name , True )
129
- and is_metadata
121
+ used_variables [var_name ] = (
122
+ used_variables .get (var_name , True ) and is_metadata
130
123
)
131
124
132
125
for macro_ref in jinja_macro_references or set ():
@@ -148,7 +141,7 @@ def make_python_env(
148
141
python_env .update (serialize_env (env , path = module_path ))
149
142
return _add_variables_to_python_env (
150
143
python_env ,
151
- used_variable_referenced_in_metadata_expression ,
144
+ used_variables ,
152
145
variables ,
153
146
blueprint_variables = blueprint_variables ,
154
147
dialect = dialect ,
@@ -189,37 +182,34 @@ def _prune_nested_macro_func(expression: exp.Expression) -> bool:
189
182
190
183
def _add_variables_to_python_env (
191
184
python_env : t .Dict [str , Executable ],
192
- used_variable_referenced_in_metadata_expression : t .Dict [str , bool ],
185
+ used_variables : t .Dict [str , bool ],
193
186
variables : t .Optional [t .Dict [str , t .Any ]],
194
187
strict_resolution : bool = True ,
195
188
blueprint_variables : t .Optional [t .Dict [str , t .Any ]] = None ,
196
189
dialect : DialectType = None ,
197
190
macro_funcs_by_used_var : t .Optional [t .DefaultDict [str , t .Set [str ]]] = None ,
198
191
) -> t .Dict [str , Executable ]:
199
- _ , python_used_variable_referenced_in_metadata_expression = parse_dependencies (
192
+ _ , python_used_variables = parse_dependencies (
200
193
python_env ,
201
194
None ,
202
195
strict_resolution = strict_resolution ,
203
196
variables = variables ,
204
197
blueprint_variables = blueprint_variables ,
205
198
)
206
- for var_name , is_metadata in python_used_variable_referenced_in_metadata_expression .items ():
207
- used_variable_referenced_in_metadata_expression [var_name ] = (
208
- used_variable_referenced_in_metadata_expression .get (var_name , True ) and is_metadata
209
- )
199
+ for var_name , is_metadata in python_used_variables .items ():
200
+ used_variables [var_name ] = used_variables .get (var_name , True ) and is_metadata
210
201
211
202
# Variables are treated as metadata when:
212
203
# - They are only referenced in metadata-only contexts, such as `audits (...)`, virtual statements, etc
213
- # - They are only referenced in metadata-only macros, either as their arguments of within their definitions
204
+ # - They are only referenced in metadata-only macros, either as their arguments or within their definitions
214
205
metadata_used_variables = set ()
215
206
for used_var , macro_names in (macro_funcs_by_used_var or {}).items ():
216
- if used_variable_referenced_in_metadata_expression .get (used_var ) or all (
207
+ if used_variables .get (used_var ) or all (
217
208
name in python_env and python_env [name ].is_metadata for name in macro_names
218
209
):
219
210
metadata_used_variables .add (used_var )
220
211
221
- used_variables = set (used_variable_referenced_in_metadata_expression )
222
- non_metadata_used_variables = used_variables - metadata_used_variables
212
+ non_metadata_used_variables = set (used_variables ) - metadata_used_variables
223
213
224
214
metadata_variables = {
225
215
k : v for k , v in (variables or {}).items () if k in metadata_used_variables
@@ -295,7 +285,7 @@ def blueprint_var(var_name: str, default: t.Optional[t.Any] = None) -> t.Optiona
295
285
local_env = dict .fromkeys (("context" , "evaluator" ), VariableResolutionContext )
296
286
297
287
depends_on = set ()
298
- used_variable_referenced_in_metadata_expression : t .Dict [str , bool ] = {}
288
+ used_variables : t .Dict [str , bool ] = {}
299
289
300
290
for executable in python_env .values ():
301
291
if not executable .is_definition :
@@ -358,12 +348,9 @@ def get_first_arg(keyword_arg_name: str) -> t.Any:
358
348
)
359
349
360
350
for var_name in next_variables :
361
- used_variable_referenced_in_metadata_expression [var_name ] = (
362
- used_variable_referenced_in_metadata_expression .get (var_name , True )
363
- and bool (is_metadata )
364
- )
351
+ used_variables [var_name ] = used_variables .get (var_name , True ) and bool (is_metadata )
365
352
366
- return depends_on , used_variable_referenced_in_metadata_expression
353
+ return depends_on , used_variables
367
354
368
355
369
356
def validate_extra_and_required_fields (
0 commit comments