-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprofiling_queries.py
More file actions
596 lines (549 loc) · 20.1 KB
/
Copy pathprofiling_queries.py
File metadata and controls
596 lines (549 loc) · 20.1 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
from typing import Literal
import pandas as pd
import streamlit as st
from testgen.ui.services.database_service import fetch_all_from_db, fetch_df_from_db
from testgen.utils import is_uuid4
TAG_FIELDS = [
"data_source",
"source_system",
"source_process",
"business_domain",
"stakeholder_group",
"transform_level",
"aggregation_level",
"data_product",
]
COLUMN_PROFILING_FIELDS = """
-- Value Counts
profile_results.record_ct,
value_ct,
distinct_value_ct,
null_value_ct,
zero_value_ct,
-- Alpha
zero_length_ct,
filled_value_ct,
mixed_case_ct,
lower_case_ct,
upper_case_ct,
non_alpha_ct,
includes_digit_ct,
numeric_ct,
date_ct,
quoted_value_ct,
lead_space_ct,
embedded_space_ct,
avg_embedded_spaces,
min_length,
max_length,
avg_length,
min_text,
max_text,
distinct_std_value_ct,
distinct_pattern_ct,
std_pattern_match,
top_freq_values,
top_patterns,
-- Numeric
min_value,
min_value_over_0,
max_value,
avg_value,
stdev_value,
percentile_25,
percentile_50,
percentile_75,
-- Date
min_date,
max_date,
before_1yr_date_ct,
before_5yr_date_ct,
before_20yr_date_ct,
within_1yr_date_ct,
within_1mo_date_ct,
future_date_ct,
-- Boolean
boolean_true_ct
"""
@st.cache_data(show_spinner=False)
def get_profiling_results(profiling_run_id: str, table_name: str | None = None, column_name: str | None = None, sorting_columns = None) -> pd.DataFrame:
order_by = ""
if sorting_columns is None:
order_by = "ORDER BY LOWER(schema_name), LOWER(table_name), position"
elif len(sorting_columns):
order_by = "ORDER BY " + ", ".join(" ".join(col) for col in sorting_columns)
query = f"""
SELECT
profile_results.id::VARCHAR,
'column' AS type,
schema_name,
table_name,
column_name,
table_groups_id::VARCHAR AS table_group_id,
-- Characteristics
general_type,
db_data_type,
functional_data_type,
datatype_suggestion,
-- Profile Run
profile_run_id::VARCHAR,
run_date AS profile_run_date,
query_error AS profiling_error,
{COLUMN_PROFILING_FIELDS},
-- Extra fields for sorting and exporting
position,
functional_data_type AS semantic_data_type,
functional_table_type AS semantic_table_type,
CASE WHEN EXISTS(
SELECT 1
FROM profile_anomaly_results
WHERE profile_run_id = profile_results.profile_run_id
AND table_name = profile_results.table_name
AND column_name = profile_results.column_name
) THEN 'Yes' END AS hygiene_issues,
CASE WHEN query_error IS NOT NULL THEN 'Error: ' || query_error ELSE NULL END AS result_details,
tg.project_code,
tg.connection_id::VARCHAR AS connection_id
FROM profile_results
LEFT JOIN table_groups tg ON (profile_results.table_groups_id = tg.id)
WHERE profile_run_id = :profiling_run_id
AND table_name ILIKE :table_name
AND column_name ILIKE :column_name
{order_by};
"""
params = {
"profiling_run_id": profiling_run_id,
"table_name": table_name or "%%",
"column_name": column_name or "%%",
}
return fetch_df_from_db(query, params)
@st.cache_data(show_spinner=False)
def get_table_by_id(
table_id: str,
include_tags: bool = False,
include_has_test_runs: bool = False,
include_active_tests: bool = False,
include_scores: bool = False,
) -> dict | None:
if not is_uuid4(table_id):
return None
condition = "WHERE table_id = :table_id"
params = {"table_id": table_id}
return get_tables_by_condition(condition, params, include_tags, include_has_test_runs, include_active_tests, include_scores)[0]
def get_tables_by_id(
table_ids: list[str],
include_tags: bool = False,
include_has_test_runs: bool = False,
include_active_tests: bool = False,
include_scores: bool = False,
) -> list[dict]:
condition = """
INNER JOIN (
SELECT UNNEST(ARRAY [:table_ids]) AS id
) selected ON (table_chars.table_id = selected.id::UUID)"""
params = {"table_ids": table_ids}
return get_tables_by_condition(condition, params, include_tags, include_has_test_runs, include_active_tests, include_scores)
def get_tables_by_table_group(
table_group_id: str,
include_tags: bool = False,
include_has_test_runs: bool = False,
include_active_tests: bool = False,
include_scores: bool = False,
) -> list[dict]:
if not is_uuid4(table_group_id):
return None
condition = "WHERE table_chars.table_groups_id = :table_group_id"
params = {"table_group_id": table_group_id}
return get_tables_by_condition(condition, params, include_tags, include_has_test_runs, include_active_tests, include_scores)
def get_tables_by_condition(
filter_condition: str,
filter_params: dict,
include_tags: bool = False,
include_has_test_runs: bool = False,
include_active_tests: bool = False,
include_scores: bool = False,
) -> list[dict]:
active_tests_cte = """
WITH active_test_definitions AS (
SELECT
test_defs.table_groups_id,
test_defs.schema_name,
test_defs.table_name,
COUNT(*) AS count
FROM test_definitions test_defs
LEFT JOIN data_column_chars ON (
test_defs.table_groups_id = data_column_chars.table_groups_id
AND test_defs.schema_name = data_column_chars.schema_name
AND test_defs.table_name = data_column_chars.table_name
AND test_defs.column_name = data_column_chars.column_name
)
WHERE test_active = 'Y'
AND column_id IS NULL
GROUP BY test_defs.table_groups_id,
test_defs.schema_name,
test_defs.table_name
)
""" if include_active_tests else ""
table_tags_select = f"""
-- Table Tags
table_chars.description,
table_chars.critical_data_element,
{", ".join([ f"table_chars.{tag}" for tag in TAG_FIELDS ])},
-- Table Groups Tags
{", ".join([ f"table_groups.{tag} AS table_group_{tag}" for tag in TAG_FIELDS if tag != "aggregation_level" ])},
""" if include_tags else ""
has_test_runs_select = """
-- Has Test Runs
EXISTS(
SELECT 1
FROM test_results
WHERE table_groups_id = table_chars.table_groups_id
AND table_name = table_chars.table_name
) AS has_test_runs,
""" if include_has_test_runs else ""
active_tests_select = """
-- Test Definition Count
active_tests.count AS active_test_count,
""" if include_active_tests else ""
scores_select = """
-- Scores
table_chars.dq_score_profiling,
table_chars.dq_score_testing,
""" if include_scores else ""
active_tests_join = """
LEFT JOIN active_test_definitions active_tests ON (
table_chars.table_groups_id = active_tests.table_groups_id
AND table_chars.schema_name = active_tests.schema_name
AND table_chars.table_name = active_tests.table_name
)
""" if include_active_tests else ""
query = f"""
{active_tests_cte}
SELECT
table_chars.table_id::VARCHAR AS id,
'table' AS type,
table_chars.table_name,
table_chars.schema_name,
table_chars.table_groups_id::VARCHAR AS table_group_id,
-- Characteristics
functional_table_type,
approx_record_ct,
table_chars.record_ct,
table_chars.column_ct,
add_date,
last_refresh_date,
drop_date,
{table_tags_select}
{has_test_runs_select}
{active_tests_select}
{scores_select}
-- Profile Run
table_chars.last_complete_profile_run_id::VARCHAR AS profile_run_id,
profiling_starttime AS profile_run_date,
TRUE AS is_latest_profile,
table_groups.project_code,
table_groups.connection_id::VARCHAR AS connection_id
FROM data_table_chars table_chars
LEFT JOIN profiling_runs ON (
table_chars.last_complete_profile_run_id = profiling_runs.id
)
LEFT JOIN table_groups ON (
table_chars.table_groups_id = table_groups.id
)
{active_tests_join}
{filter_condition}
ORDER BY LOWER(table_chars.table_name);
"""
results = fetch_all_from_db(query, filter_params)
return [ dict(row) for row in results ]
@st.cache_data(show_spinner=False)
def get_column_by_id(
column_id: str,
include_tags: bool = False,
include_has_test_runs: bool = False,
include_active_tests: bool = False,
include_scores: bool = False,
) -> dict | None:
if not is_uuid4(column_id):
return None
condition = "WHERE column_chars.column_id = :column_id"
params = {"column_id": column_id}
return get_columns_by_condition(condition, params, include_tags, include_has_test_runs, include_active_tests, include_scores)[0]
@st.cache_data(show_spinner="Loading data ...")
def get_column_by_name(
column_name: str,
table_name: str,
table_group_id: str,
include_tags: bool = False,
include_has_test_runs: bool = False,
include_active_tests: bool = False,
include_scores: bool = False,
) -> dict | None:
condition = """
WHERE column_chars.column_name = :column_name
AND column_chars.table_name = :table_name
AND column_chars.table_groups_id = :table_group_id
"""
params = {
"column_name": column_name,
"table_name": table_name,
"table_group_id": table_group_id,
}
return get_columns_by_condition(condition, params, include_tags, include_has_test_runs, include_active_tests, include_scores)[0]
def get_columns_by_id(
column_ids: list[str],
include_tags: bool = False,
include_has_test_runs: bool = False,
include_active_tests: bool = False,
include_scores: bool = False,
) -> list[dict]:
condition = """
INNER JOIN (
SELECT UNNEST(ARRAY [:column_ids]) AS id
) selected ON (column_chars.column_id = selected.id::UUID)"""
params = {"column_ids": [ col for col in column_ids if is_uuid4(col) ]}
return get_columns_by_condition(condition, params, include_tags, include_has_test_runs, include_active_tests, include_scores)
def get_columns_by_table_group(
table_group_id: str,
include_tags: bool = False,
include_has_test_runs: bool = False,
include_active_tests: bool = False,
include_scores: bool = False,
) -> list[dict]:
if not is_uuid4(table_group_id):
return None
condition = "WHERE column_chars.table_groups_id = :table_group_id"
params = {"table_group_id": table_group_id}
return get_columns_by_condition(condition, params, include_tags, include_has_test_runs, include_active_tests, include_scores)
def get_columns_by_condition(
filter_condition: str,
filter_params: dict,
include_tags: bool = False,
include_has_test_runs: bool = False,
include_active_tests: bool = False,
include_scores: bool = False,
) -> list[dict]:
column_tags_select = f"""
-- Column Tags
column_chars.description,
column_chars.critical_data_element,
column_chars.excluded_data_element,
column_chars.pii_flag,
{", ".join([ f"column_chars.{tag}" for tag in TAG_FIELDS ])},
-- Table Tags
table_chars.critical_data_element AS table_critical_data_element,
{", ".join([ f"table_chars.{tag} AS table_{tag}" for tag in TAG_FIELDS ])},
-- Table Groups Tags
{", ".join([ f"table_groups.{tag} AS table_group_{tag}" for tag in TAG_FIELDS if tag != "aggregation_level" ])},
""" if include_tags else ""
has_test_runs_select = """
-- Has Test Runs
EXISTS(
SELECT 1
FROM test_results
WHERE table_groups_id = column_chars.table_groups_id
AND table_name = column_chars.table_name
AND column_names = column_chars.column_name
) AS has_test_runs,
""" if include_has_test_runs else ""
active_tests_select = """
-- Test Definition Count
(
SELECT COUNT(*)
FROM test_definitions
WHERE table_groups_id = column_chars.table_groups_id
AND table_name = column_chars.table_name
AND column_name = column_chars.column_name
AND test_active = 'Y'
) AS active_test_count,
""" if include_active_tests else ""
scores_select = """
-- Scores
column_chars.dq_score_profiling,
column_chars.dq_score_testing,
""" if include_scores else ""
query = f"""
SELECT
column_chars.column_id::VARCHAR AS id,
'column' AS type,
column_chars.column_name,
column_chars.table_name,
column_chars.schema_name,
column_chars.table_groups_id::VARCHAR AS table_group_id,
column_chars.ordinal_position,
-- Characteristics
column_chars.general_type,
column_chars.db_data_type,
column_chars.functional_data_type,
datatype_suggestion,
column_chars.add_date,
column_chars.last_mod_date,
column_chars.drop_date,
{column_tags_select}
-- Profile Run
column_chars.last_complete_profile_run_id::VARCHAR AS profile_run_id,
run_date AS profile_run_date,
TRUE AS is_latest_profile,
query_error AS profiling_error,
{has_test_runs_select}
{active_tests_select}
{scores_select}
table_chars.approx_record_ct,
table_groups.project_code,
table_groups.connection_id::VARCHAR AS connection_id,
{COLUMN_PROFILING_FIELDS}
FROM data_column_chars column_chars
LEFT JOIN data_table_chars table_chars ON (
column_chars.table_id = table_chars.table_id
)
LEFT JOIN table_groups ON (
column_chars.table_groups_id = table_groups.id
)
LEFT JOIN profile_results ON (
column_chars.last_complete_profile_run_id = profile_results.profile_run_id
AND column_chars.schema_name = profile_results.schema_name
AND column_chars.table_name = profile_results.table_name
AND column_chars.column_name = profile_results.column_name
)
{filter_condition}
ORDER BY LOWER(column_chars.table_name), ordinal_position;
"""
results = fetch_all_from_db(query, filter_params)
return [ dict(row) for row in results ]
@st.cache_data(show_spinner=False)
def get_hygiene_issues(profile_run_id: str, table_name: str, column_name: str | None = None) -> list[dict]:
if not profile_run_id:
return []
query = f"""
WITH pii_results AS (
SELECT id,
CASE
WHEN detail LIKE 'Risk: HIGH%%' THEN 'High'
WHEN detail LIKE 'Risk: MODERATE%%' THEN 'Moderate'
ELSE null
END AS pii_risk
FROM profile_anomaly_results
)
SELECT column_name,
anomaly_name,
issue_likelihood,
detail,
detail_redactable,
pii_risk
FROM profile_anomaly_results anomaly_results
LEFT JOIN profile_anomaly_types anomaly_types ON (
anomaly_types.id = anomaly_results.anomaly_id
)
LEFT JOIN pii_results ON (
anomaly_results.id = pii_results.id
)
WHERE profile_run_id = :profile_run_id
AND table_name = :table_name
{"AND column_name = :column_name" if column_name else ""}
AND COALESCE(disposition, 'Confirmed') = 'Confirmed'
ORDER BY
CASE issue_likelihood
WHEN 'Definite' THEN 1
WHEN 'Likely' THEN 2
WHEN 'Possible' THEN 3
ELSE 4
END,
CASE pii_risk
WHEN 'High' THEN 1
WHEN 'Moderate' THEN 2
ELSE 3
END,
LOWER(column_name);
"""
params = {
"profile_run_id": profile_run_id,
"table_name": table_name,
"column_name": column_name,
}
results = fetch_all_from_db(query, params)
return [ dict(row) for row in results ]
@st.cache_data(show_spinner=False)
def get_profiling_anomalies(
profile_run_id: str,
likelihood: str | None = None,
issue_type_id: str | None = None,
table_name: str | None = None,
column_name: str | None = None,
action: Literal["Confirmed", "Dismissed", "Muted", "No Action"] | None = None,
sorting_columns: list[str] | None = None,
) -> pd.DataFrame:
query = f"""
SELECT
r.table_name,
r.column_name,
r.schema_name,
r.db_data_type,
t.anomaly_name,
t.issue_likelihood,
r.disposition,
null as action,
CASE
WHEN t.issue_likelihood = 'Possible' THEN 'Possible: speculative test that often identifies problems'
WHEN t.issue_likelihood = 'Likely' THEN 'Likely: typically indicates a data problem'
WHEN t.issue_likelihood = 'Definite' THEN 'Definite: indicates a highly-likely data problem'
WHEN t.issue_likelihood = 'Potential PII'
THEN 'Potential PII: may require privacy policies, standards and procedures for access, storage and transmission.'
END AS likelihood_explanation,
CASE
WHEN t.issue_likelihood = 'Potential PII' THEN 4
WHEN t.issue_likelihood = 'Possible' THEN 3
WHEN t.issue_likelihood = 'Likely' THEN 2
WHEN t.issue_likelihood = 'Definite' THEN 1
END AS likelihood_order,
t.anomaly_description, r.detail, t.detail_redactable, t.suggested_action,
r.anomaly_id, r.table_groups_id::VARCHAR, r.id::VARCHAR, p.profiling_starttime, r.profile_run_id::VARCHAR,
tg.table_groups_name, tg.project_code,
-- These are used in the PDF report
dcc.functional_data_type,
dcc.description as column_description,
COALESCE(dcc.critical_data_element, dtc.critical_data_element) as critical_data_element,
dcc.pii_flag,
COALESCE(dcc.data_source, dtc.data_source, tg.data_source) as data_source,
COALESCE(dcc.source_system, dtc.source_system, tg.source_system) as source_system,
COALESCE(dcc.source_process, dtc.source_process, tg.source_process) as source_process,
COALESCE(dcc.business_domain, dtc.business_domain, tg.business_domain) as business_domain,
COALESCE(dcc.stakeholder_group, dtc.stakeholder_group, tg.stakeholder_group) as stakeholder_group,
COALESCE(dcc.transform_level, dtc.transform_level, tg.transform_level) as transform_level,
COALESCE(dcc.aggregation_level, dtc.aggregation_level) as aggregation_level,
COALESCE(dcc.data_product, dtc.data_product, tg.data_product) as data_product
FROM profile_anomaly_results r
INNER JOIN profile_anomaly_types t
ON r.anomaly_id = t.id
INNER JOIN profiling_runs p
ON r.profile_run_id = p.id
INNER JOIN table_groups tg
ON r.table_groups_id = tg.id
LEFT JOIN data_column_chars dcc
ON (tg.id = dcc.table_groups_id
AND r.schema_name = dcc.schema_name
AND r.table_name = dcc.table_name
AND r.column_name = dcc.column_name)
LEFT JOIN data_table_chars dtc
ON dcc.table_id = dtc.table_id
WHERE r.profile_run_id = :profile_run_id
{"AND t.issue_likelihood = :likelihood" if likelihood else ""}
{"AND t.id = :issue_type_id" if issue_type_id else ""}
{"AND r.table_name = :table_name" if table_name else ""}
{"AND r.column_name ILIKE :column_name" if column_name else ""}
{"AND r.disposition IS NULL" if action == "No Action" else "AND r.disposition = :disposition" if action else ""}
{f"ORDER BY {', '.join(' '.join(col) for col in sorting_columns)}" if sorting_columns else ""}
"""
params = {
"profile_run_id": profile_run_id,
"likelihood": likelihood,
"issue_type_id": issue_type_id,
"table_name": table_name,
"column_name": column_name,
"disposition": {
"Muted": "Inactive",
}.get(action, action),
}
df = fetch_df_from_db(query, params)
dct_replace = {"Confirmed": "✓", "Dismissed": "✘", "Inactive": "🔇"}
df["action"] = df["disposition"].replace(dct_replace)
return df