-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility_functions_non_stat.py
174 lines (150 loc) · 5.25 KB
/
utility_functions_non_stat.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
from snowflake.snowpark import functions as F
from snowflake.snowpark import DataFrame as SnowparkDataFrame
# ********************************
# NON-STATISTICAL TEST FUNCTIONS
# ********************************
# def compare_column_aggregates(
# check_type_id: int,
# tbla: SnowparkDataFrame,
# tblb: SnowparkDataFrame,
# a_columns_to_check: list,
# b_columns_to_check: list,
# a_agg_list: list,
# b_agg_list: list,
# a_output_key: str,
# b_output_key: str,
# udf_nm: str,
# ) -> SnowparkDataFrame:
# # Obtain aggregate value (ex: sums, null counts, distinct value counts, etc) of relevant columns and place all aggregated values into an ARRAY column
# a_values_df = (
# tbla.group_by("PARTITION_VALUES")
# .agg(*a_agg_list)
# .with_column("TABLE_A_AGG_VALUES", F.array_construct(*a_columns_to_check))
# .select("PARTITION_VALUES", "TABLE_A_AGG_VALUES")
# )
# b_values_df = (
# tblb.group_by("PARTITION_VALUES")
# .agg(*b_agg_list)
# .with_column("TABLE_B_AGG_VALUES", F.array_construct(*b_columns_to_check))
# .select("PARTITION_VALUES", "TABLE_B_AGG_VALUES")
# )
# joined_df = a_values_df.join(b_values_df, on=["PARTITION_VALUES"])
# # Subtract each value in the two arrays to get a third array containing the changes
# delta_df = joined_df.withColumn(
# "DELTA",
# F.call_udf(udf_nm, F.col("TABLE_A_AGG_VALUES"), F.col("TABLE_B_AGG_VALUES")),
# )
# result_df = delta_df.select(
# F.lit(check_type_id).alias("CHECK_TYPE_ID"),
# "PARTITION_VALUES",
# # "TABLE_A_AGG_VALUES",
# # "TABLE_B_AGG_VALUES",
# # "DELTA",
# F.object_construct_keep_null(
# F.lit("TABLE_A_COLUMNS"),
# F.lit(a_columns_to_check),
# F.lit("TABLE_B_COLUMNS"),
# F.lit(b_columns_to_check),
# F.lit(a_output_key),
# "TABLE_A_AGG_VALUES",
# F.lit(b_output_key),
# "TABLE_B_AGG_VALUES",
# F.lit("DELTA"),
# "DELTA",
# ).alias("RESULTS"),
# )
# return result_df
def get_group_aggregates(
tbla: SnowparkDataFrame,
tblb: SnowparkDataFrame,
a_columns_to_check: list,
b_columns_to_check: list,
a_agg_list: list,
b_agg_list: list
) -> SnowparkDataFrame:
"""
Groups by the PARTITION_VALUES column and calculates the specified aggregate function over the specified columns to check.
The results of the for the Table A and Table B are joined together and returned as a Snowpark DataFrame
"""
# Obtain aggregate value (ex: sums, null counts, distinct value counts, etc) of relevant columns and place all aggregated values into an ARRAY column
a_values_df = (
tbla.group_by("PARTITION_VALUES")
.agg(*a_agg_list)
.with_column("TABLE_A_AGG_VALUES", F.array_construct(*a_columns_to_check))
.select("PARTITION_VALUES", "TABLE_A_AGG_VALUES")
)
b_values_df = (
tblb.group_by("PARTITION_VALUES")
.agg(*b_agg_list)
.with_column("TABLE_B_AGG_VALUES", F.array_construct(*b_columns_to_check))
.select("PARTITION_VALUES", "TABLE_B_AGG_VALUES")
)
joined_df = a_values_df.join(b_values_df, on=["PARTITION_VALUES"])
return joined_df
def create_result_df_for_simple_summary_check(
joined_df: SnowparkDataFrame,
check_type_id: int,
a_columns_to_check: list,
b_columns_to_check: list,
a_output_key: str,
b_output_key: str,
udf_nm: str,
) -> SnowparkDataFrame:
"""
Calculates the delta between the aggregated values of two tables and returns the results as a dictionary in Snowpark DataFrame.
"""
# Subtract each value in the two arrays to get a third array containing the changes
delta_df = joined_df.withColumn(
"DELTA",
F.call_udf(udf_nm, F.col("TABLE_A_AGG_VALUES"), F.col("TABLE_B_AGG_VALUES")),
)
result_df = delta_df.select(
F.lit(check_type_id).alias("CHECK_TYPE_ID"),
"PARTITION_VALUES",
# "TABLE_A_AGG_VALUES",
# "TABLE_B_AGG_VALUES",
# "DELTA",
F.object_construct_keep_null(
F.lit("TABLE_A_COLUMNS"),
F.lit(a_columns_to_check),
F.lit("TABLE_B_COLUMNS"),
F.lit(b_columns_to_check),
F.lit(a_output_key),
F.col("TABLE_A_AGG_VALUES"),
F.lit(b_output_key),
F.col("TABLE_B_AGG_VALUES"),
F.lit("DELTA"),
F.col("DELTA"),
).alias("RESULTS"),
)
return result_df
def compare_column_aggregates(
check_type_id: int,
tbla: SnowparkDataFrame,
tblb: SnowparkDataFrame,
a_columns_to_check: list,
b_columns_to_check: list,
a_agg_list: list,
b_agg_list: list,
a_output_key: str,
b_output_key: str,
udf_nm: str,
) -> SnowparkDataFrame:
joined_df = get_group_aggregates(
tbla,
tblb,
a_columns_to_check,
b_columns_to_check,
a_agg_list,
b_agg_list
)
result_df = create_result_df_for_simple_summary_check(
joined_df,
check_type_id,
a_columns_to_check,
b_columns_to_check,
a_output_key,
b_output_key,
udf_nm
)
return result_df