-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgreat_expectations.py
319 lines (271 loc) · 10.2 KB
/
great_expectations.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
import logging
import json
import ast
import traceback
import great_expectations as ge
import pandas as pd
import yaml
from utils.utils import get_test_id
from utils.connection_utils import (
update_db_config_from_os,
get_db_params_from_config,
remove_ge_schema_parameters,
)
from utils.configuration_utils import (
DbParamsConfigFile,
DbParamsConnection,
GE_GREAT_EXPECTATIONS_FINAL_FILENAME,
GE_BATCH_CONFIG_FINAL_FILENAME,
)
from psycopg2 import sql
from pandas import json_normalize
class GEException(Exception):
"""Exception in great expectations"""
def run_ge_tests(project_id: str, logger: logging.Logger):
"""Runs Great Expectations tests
Parameters
----------
logger : logger
Logger
project_id : str
Project ID, eg 'Muso'. Must align with project_id in dot.projects
"""
logger.info("======== Running Great Expectations tests ========")
with open(GE_BATCH_CONFIG_FINAL_FILENAME, "r") as opened:
config = json.load(opened)
with open(GE_GREAT_EXPECTATIONS_FINAL_FILENAME, "r") as opened:
ge_config = yaml.safe_load(opened)
db_config_path = "great_expectations/" + ge_config["config_variables_file_path"]
with open(
db_config_path, "r"
) as opened: # Assumes the credentials are stored in a local file
db_config = yaml.safe_load(opened)[config["datasource_name"]]
db_config = update_db_config_from_os(db_config.copy())
# The batch_config json file specifies three things:
# 1. The datasource, as configured in great_expectations.yml
# 2. The name of the expectation suite that holds the expectations
# 3. A table you want as an entry point for generating a dataset,
# or a query for ganerating that same dataset
# Note: Ideally, our expectations would take in a table name as an argument
# and would not rely on the table/query chosen here. Great Expectations,
# however,always genarates a dataset, since the class we are using for our
# custom validations extends from `SqlAlchemyDataset`
# Uses configuration to create a validation batch and runs an expectation
# suite on it
context = ge.data_context.DataContext()
batch_kwargs = {
"query": config["query"],
"datasource": config["datasource_name"],
}
batch = context.get_batch(batch_kwargs, config["expectation_suite_name"])
# TODO use checkpoint instead of validation_operator to adapt to GE 0.13
# result: CheckpointResult = context.run_checkpoint(
# checkpoint_name="dot_checkpoint",
# batch_request=None,
# run_name=None,
# )
results = context.run_validation_operator(
"action_list_operator",
assets_to_validate=[batch],
)
results = parse_results(results)
view_sql = create_views(project_id, results, db_config)
if view_sql:
results["failed_tests_view_sql"] = view_sql
results.to_csv(f"generated_files/{project_id}/ge_clean_results.csv", index=False)
def parse_unexpected_list(unexpected):
"""Converts a pandas Series or a string representation into a list, or pass on the
list if it already was one. This is to make sure we can read the results from both
a file or in-memory directly from Great Expectations.
Parameters
----------
unexpected: Pandas series or string
Input series or string to convert
Return
------
values: list
List of values
"""
if isinstance(unexpected, str):
values = ast.literal_eval(unexpected)
elif isinstance(unexpected, list):
values = unexpected
elif isinstance(unexpected, pd.Series):
values = unexpected.to_list()
else:
values = []
return values
def create_views(project_id, results, db_config):
"""Creates a failed tests view for Great Expectations tests
Parameters
----------
project_id: str
Current run project ID
results: Pandas series or string
Great expectations results dataframe
db_config: dict
enum for dbt_project.yml config file
Return
------
view_sql: str
SQL for the view
Will also create a DB view
"""
schema_test, _, connection = get_db_params_from_config(
DbParamsConfigFile["dot_config.yml"],
DbParamsConnection["project_test"],
project_id,
)
schema_core, _, _ = get_db_params_from_config(
DbParamsConfigFile["dot_config.yml"],
DbParamsConnection["project_core"],
project_id,
)
cursor = connection.cursor()
view_sql = []
for _, result in results.iterrows():
try:
if result["error"]:
print(
f"Failed to execute {result['expectation_config.expectation_type']}"
)
print(result["exception_info.exception_message"])
continue
if result["fail"]:
source_table = result["result.table"]
source_column = result["result.id_column"]
if result["result.short_name"].startswith("chv_"):
name = f"chv_tr_{result['result.short_name'][4:]}"
else:
name = f"tr_{result['result.short_name']}"
raw_values = result["result.unexpected_list"]
values = parse_unexpected_list(raw_values)
# Weird super slowdown if using "create or replace view" as opposing
# to dropping view first
query_drop = sql.SQL("drop view if exists {name}").format(
name=sql.Identifier(schema_test, name)
)
cursor.execute(query_drop)
connection.commit()
query_create = sql.SQL(
"create view {name} as select * from {source_table} "
"inner join unnest(%s) as failed "
"on failed={source_table}.{source_column}"
).format(
name=sql.Identifier(schema_test, name),
source_table=sql.Identifier(schema_core, source_table),
source_column=sql.Identifier(source_column),
)
cursor.execute(query_create, (values,))
connection.commit()
print(f"Created view at {name} with {len(values)} rows")
view_sql.append(query_create)
except GEException:
traceback.print_exc()
continue
connection.close()
return view_sql
def parse_results(results):
"""Creates a csv file parsed from great expectations results json object
Parameters
----------
results: Pandas dataframe
Returns
-------
results: Pandas dataframe
"""
for key in results["run_results"]:
actual_results = results["run_results"][key]["validation_result"]["results"]
results = [result.to_json_dict() for result in actual_results]
test_params = []
for r in results:
t = r["expectation_config"]["kwargs"]
del t["result_format"]
test_params.append(t)
results = json_normalize(results)
results["fail"] = ~results["success"]
results["error"] = results["exception_info.raised_exception"]
results["status"] = (
results["result.unexpected_list"].apply(lambda x: len(parse_unexpected_list(x)))
if "result.unexpected_list" in results.columns
else "pass"
)
results["warn"] = False # because we don't have that concept yet
results["skip"] = False # we also don't ever skip expectations yet
results["test_parameters"] = [
str(item) for item in remove_ge_schema_parameters(test_params)
]
return results
def extract_df_from_ge_test_results_csv(run_id, project_id, logger):
"""Function to parse GE csv results file (that was created by parse_results) to
form a test_summaries standard dataframe.
Parameters
----------
run_id: str
Current run ID as found in dot.run_log
project_id: str
Current run project ID
logger: Logging object
Custom logging object
Returns
-------
ge_tests_summary: Pandas dataframe
Standard tests summary dataframe, same columns as returned by
extract_df_from_dbt_test_results_json
"""
logger.info("Extracting GE test summary dataframe ...")
ge_tests_summary = pd.read_csv(f"generated_files/{project_id}/ge_clean_results.csv")
ge_tests_summary["run_id"] = run_id
ge_tests_summary.rename(
columns={"expectation_config.kwargs.quantity": "column_name"}, inplace=True
)
ge_tests_summary.rename(
columns={"expectation_config.expectation_type": "test_type"}, inplace=True
)
ge_tests_summary.rename(
columns={"expectation_config.kwargs.data_table": "entity_id"}, inplace=True
)
ge_tests_summary.rename(
columns={"exception_info.exception_message": "test_status_message"},
inplace=True,
)
ge_tests_summary["test_id"] = ge_tests_summary.apply(
lambda x: get_test_id(
x["test_type"],
x["entity_id"],
x["column_name"],
project_id,
x["test_parameters"],
),
axis=1,
)
ge_tests_summary["test_status"] = ge_tests_summary["fail"].apply(
lambda x: "fail" if x is True else "pass"
)
ge_tests_summary["test_status"] = ge_tests_summary.apply(
lambda x: "error" if x["error"] is True else x["test_status"], axis=1
)
ge_tests_summary.rename(
columns={"result.short_name": "failed_tests_view"}, inplace=True
)
if "failed_tests_view" in ge_tests_summary.columns:
ge_tests_summary["failed_tests_view"] = ge_tests_summary[
"failed_tests_view"
].apply(lambda x: f"chv_tr_{x[4:]}" if x.startswith("chv_") else x)
else:
ge_tests_summary["failed_tests_view"] = None
ge_tests_summary = ge_tests_summary[
[
"run_id",
"test_id",
"entity_id",
"test_type",
"column_name",
"test_status",
"test_status_message",
"test_parameters",
"failed_tests_view",
]
]
ge_tests_summary.set_index("test_id")
return ge_tests_summary