-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
default_expectation_configuration_builder.py
384 lines (329 loc) · 14.6 KB
/
default_expectation_configuration_builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
from typing import Any, Dict, List, Optional, Set, Union
from pyparsing import Combine
from pyparsing import Optional as ppOptional
from pyparsing import (
ParseException,
ParseResults,
Suppress,
Word,
alphanums,
alphas,
infixNotation,
nums,
oneOf,
opAssoc,
)
import great_expectations.exceptions as ge_exceptions
from great_expectations.core.expectation_configuration import ExpectationConfiguration
from great_expectations.rule_based_profiler.config import ParameterBuilderConfig
from great_expectations.rule_based_profiler.expectation_configuration_builder import (
ExpectationConfigurationBuilder,
)
from great_expectations.rule_based_profiler.helpers.util import (
get_parameter_value_and_validate_return_type,
)
from great_expectations.rule_based_profiler.types import Domain, ParameterContainer
text = Suppress("'") + Word(alphas, alphanums) + Suppress("'")
integer = Word(nums).setParseAction(lambda t: int(t[0]))
var = Combine(Word("$" + alphas, alphanums + "_.") + ppOptional("[" + integer + "]"))
comparison_operator = oneOf(">= <= != > < ==")
binary_operator = oneOf("~ & |")
operand = text | integer | var
expr = infixNotation(
operand,
[
(comparison_operator, 2, opAssoc.LEFT),
(binary_operator, 2, opAssoc.LEFT),
],
)
class ExpectationConfigurationConditionParserError(
ge_exceptions.GreatExpectationsError
):
pass
class DefaultExpectationConfigurationBuilder(ExpectationConfigurationBuilder):
"""
Class which creates ExpectationConfiguration out of a given Expectation type and
parameter_name-to-parameter_fully_qualified_parameter_name map (name-value pairs supplied in the kwargs dictionary).
ExpectationConfigurations can be optionally filtered if a supplied condition is met.
"""
exclude_field_names: Set[
str
] = ExpectationConfigurationBuilder.exclude_field_names | {
"kwargs",
}
def __init__(
self,
expectation_type: str,
meta: Optional[Dict[str, Any]] = None,
condition: Optional[str] = None,
validation_parameter_builder_configs: Optional[
List[ParameterBuilderConfig]
] = None,
data_context: Optional["BaseDataContext"] = None, # noqa: F821
**kwargs,
):
"""
Args:
expectation_type: the "expectation_type" argument of "ExpectationConfiguration" object to be emitted.
meta: the "meta" argument of "ExpectationConfiguration" object to be emitted
condition: Boolean statement (expressed as string and following specified grammar), which controls whether
or not underlying logic should be executed and thus resulting "ExpectationConfiguration" emitted
validation_parameter_builder_configs: ParameterBuilder configurations, having whose outputs available (as
fully-qualified parameter names) is pre-requisite for present ExpectationConfigurationBuilder instance
These "ParameterBuilder" configurations help build kwargs needed for this "ExpectationConfigurationBuilder"
data_context: BaseDataContext associated with this ExpectationConfigurationBuilder
kwargs: additional arguments
"""
super().__init__(
expectation_type=expectation_type,
validation_parameter_builder_configs=validation_parameter_builder_configs,
data_context=data_context,
**kwargs,
)
if meta is None:
meta = {}
self._meta = meta
if not isinstance(meta, dict):
raise ge_exceptions.ProfilerExecutionError(
message=f"""Argument "{meta}" in "{self.__class__.__name__}" must be of type "dictionary" \
(value of type "{str(type(meta))}" was encountered).
"""
)
if condition and (not isinstance(condition, str)):
raise ge_exceptions.ProfilerExecutionError(
message=f"""Argument "{condition}" in "{self.__class__.__name__}" must be of type "string" \
(value of type "{str(type(condition))}" was encountered).
"""
)
self._condition = condition
self._validation_parameter_builder_configs = (
validation_parameter_builder_configs
)
self._kwargs = kwargs
@property
def expectation_type(self) -> str:
return self._expectation_type
@property
def condition(self) -> Optional[str]:
return self._condition
@property
def validation_parameter_builder_configs(
self,
) -> Optional[List[ParameterBuilderConfig]]:
return self._validation_parameter_builder_configs
@property
def kwargs(self) -> dict:
return self._kwargs
@property
def meta(self) -> dict:
return self._meta
def _parse_condition(self) -> ParseResults:
"""
Using the grammer defined by expr, provides the condition to the parser.
Applicability: To be used as part of configuration (e.g., YAML-based files or text strings).
Extendability: Readily extensible to include "slice" and other standard accessors (as long as no dynamic elements).
"""
try:
return expr.parseString(self._condition)
except ParseException:
raise ExpectationConfigurationConditionParserError(
f'Unable to parse Expectation Configuration Condition: "{self._condition}".'
)
def _substitute_parameters_and_variables(
self,
term_list: Union[str, ParseResults],
domain: Domain,
variables: Optional[ParameterContainer] = None,
parameters: Optional[Dict[str, ParameterContainer]] = None,
) -> ParseResults:
"""Recursively substitute all parameters and variables in term list
Given a list of terms created by parsing a provided condition, recursively substitute all parameters and
variables in the term list, regardless of depth of groupings.
Example:
condition: "($variables.max_user_id>0 & $variables.answer==42) | $parameter.my_min_user_id.value[0]<0" will
return the following term list from self._parse_condition:
parsed_condition = [[
[
['$variables.max_user_id', '>', 0],
'&',
['$variables.answer', '==', 42]
],
'|',
['$parameter.my_min_user_id.value[0]', '<', 0]
]]
This method will then take that term list and recursively search for parameters and variables that need to
be substituted and return this ParseResults object:
return [[
[
[999999999999, '>', 0],
'&',
[42, '==', 42]
],
'|',
[397433, '<', 0]
]]
Args:
term_list (Union[str, ParseResults): the ParseResults object returned from self._parse_condition
domain (Domain): The domain of the ExpectationConfiguration
variables (Optional[ParameterContainer]): The variables set for this ExpectationConfiguration
parameters (Optional[Dict[str, ParameterContainer]]): The parameters set for this ExpectationConfiguration
Returns:
ParseResults: a ParseResults object identical to the one returned by self._parse_condition except with
substituted parameters and variables.
"""
idx: int
token: Union[str, ParseResults]
for idx, token in enumerate(term_list):
if isinstance(token, str) and token.startswith("$"):
term_list[idx]: Dict[
str, Any
] = get_parameter_value_and_validate_return_type(
domain=domain,
parameter_reference=token,
expected_return_type=None,
variables=variables,
parameters=parameters,
)
elif isinstance(token, ParseResults):
self._substitute_parameters_and_variables(
term_list=token,
domain=domain,
variables=variables,
parameters=parameters,
)
return term_list
def _build_binary_list(
self,
substituted_term_list: Union[str, ParseResults],
) -> ParseResults:
"""Recursively build binary list from substituted terms
Given a list of substituted terms created by parsing a provided condition and substituting parameters and
variables, recursively build binary condition ParseResults object, regardless of depth of groupings.
Example:
substituted_term_list = [[
[
[999999999999, '>', 0],
'&',
[42, '==', 42]
],
'|',
[397433, '<', 0]
]]
This method will then take that term list and recursively evaluate the terms between the top-level binary
conditions and return this ParseResults object:
return [True, '&' True]
Args:
substituted_term_list (Union[str, ParseResults]): the ParseResults object returned from
self._substitute_parameters_and_variables
Returns:
ParseResults: a ParseResults object with all terms evaluated except for binary operations.
"""
idx: int
token: Union[str, list]
for idx, token in enumerate(substituted_term_list):
if (not any([isinstance(t, ParseResults) for t in token])) and len(
token
) > 1:
substituted_term_list[idx] = eval("".join([str(t) for t in token]))
elif isinstance(token, ParseResults):
self._build_binary_list(substituted_term_list=token)
return substituted_term_list
def _build_boolean_result(
self,
binary_list: Union[ParseResults, List[Union[bool, str]]],
) -> bool:
"""Recursively build boolean result from binary list
Given a list of binary terms created by parsing a provided condition, substituting parameters and
variables, and building binary condition ParseResults object, recursively evaluate remaining conditions and
return boolean result of condition.
Example:
binary_list = [True, '&' True]
This method will then take that term list and recursively evaluate the remaining and return a boolean result
for the provided condition:
return True
Args:
binary_list (List[Union[bool, str]]): the ParseResults object returned from
self._substitute_parameters_and_variables
Returns:
bool: a boolean representing the evaluation of the entire provided condition.
"""
idx: int
token: Union[str, list]
for idx, token in enumerate(binary_list):
if (
(not isinstance(token, bool))
and (not any([isinstance(t, ParseResults) for t in token]))
and (len(token) > 1)
):
binary_list[idx] = eval("".join([str(t) for t in token]))
return self._build_boolean_result(binary_list=binary_list)
elif isinstance(token, ParseResults):
return self._build_boolean_result(binary_list=token)
return eval("".join([str(t) for t in binary_list]))
def _evaluate_condition(
self,
parsed_condition: ParseResults,
domain: Domain,
variables: Optional[ParameterContainer] = None,
parameters: Optional[Dict[str, ParameterContainer]] = None,
) -> bool:
"""Evaluates the parsed condition to True/False and returns the boolean result"""
substituted_term_list: ParseResults = self._substitute_parameters_and_variables(
term_list=parsed_condition,
domain=domain,
variables=variables,
parameters=parameters,
)
binary_list: ParseResults = self._build_binary_list(
substituted_term_list=substituted_term_list
)
boolean_result: bool = self._build_boolean_result(binary_list=binary_list)
return boolean_result
def _build_expectation_configuration(
self,
domain: Domain,
variables: Optional[ParameterContainer] = None,
parameters: Optional[Dict[str, ParameterContainer]] = None,
) -> Optional[ExpectationConfiguration]:
"""Returns either and ExpectationConfiguration object or None depending on evaluation of condition"""
parameter_name: str
fully_qualified_parameter_name: str
expectation_kwargs: Dict[str, Any] = {
parameter_name: get_parameter_value_and_validate_return_type(
domain=domain,
parameter_reference=fully_qualified_parameter_name,
expected_return_type=None,
variables=variables,
parameters=parameters,
)
for parameter_name, fully_qualified_parameter_name in self.kwargs.items()
}
meta: Dict[str, Any] = get_parameter_value_and_validate_return_type(
domain=domain,
parameter_reference=self.meta,
expected_return_type=dict,
variables=variables,
parameters=parameters,
)
if self._condition:
parsed_condition: ParseResults = self._parse_condition()
condition: bool = self._evaluate_condition(
parsed_condition=parsed_condition,
domain=domain,
variables=variables,
parameters=parameters,
)
if condition:
return ExpectationConfiguration(
expectation_type=self._expectation_type,
kwargs=expectation_kwargs,
meta=meta,
)
else:
return None
else:
return ExpectationConfiguration(
expectation_type=self._expectation_type,
kwargs=expectation_kwargs,
meta=meta,
)