/
case_statements.py
564 lines (449 loc) · 18 KB
/
case_statements.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
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
import warnings
def _check_jaro_registered(spark):
if spark is None:
return False
if (
spark == "supress_warnings"
): # Allows us to surpress the warning in the test suite
return False
for fn in spark.catalog.listFunctions():
if fn.name == "jaro_winkler_sim":
return True
warnings.warn(
f"The jaro_winkler_sim user definined function is not available in Spark "
"Or you did not pass 'spark' (the SparkSession) into 'Params' "
"Falling back to using levenshtein in the default string comparison functions "
"You can import these functions using the scala-udf-similarity-0.0.7.jar provided with Splink"
)
return False
def _add_as_gamma_to_case_statement(case_statement: str, gamma_col_name):
"""As the correct column alias to the case statement if it does not exist
Args:
case_statement (str): Original case statement
Returns:
str: case_statement with correct alias
"""
sl = case_statement.lower()
# What we're trying to do is distinguish between 'case when end as gamma_blah' from case when end
sl = sl.replace("\n", " ").replace("\r", "")
sl = sl.strip()
# Only need to do anything if the last non-blank string is not ' end'
if sl[-4:] != " end":
# The case statement has a 'end as gamma blah'
last_end_index = sl.rfind(" end ")
sl = sl[: last_end_index + 5]
return f"{sl} as gamma_{gamma_col_name}"
def _check_no_obvious_problem_with_case_statement(case_statement):
seems_valid = True
cs_l = case_statement.lower()
if "case" not in cs_l:
seems_valid = False
if "end" not in cs_l:
seems_valid = False
if "when" not in cs_l:
seems_valid = False
if "then" not in cs_l:
seems_valid = False
if not seems_valid:
s1 = "The case expression you provided does not seem to be valid SQL."
s2 = f"Expression provided is: '{case_statement}'"
raise ValueError(f"{s1} {s2}")
def sql_gen_case_smnt_strict_equality_2(col_name, gamma_col_name=None):
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
when {col_name}_l = {col_name}_r then 1
else 0 end as gamma_{gamma_col_name}"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
# String comparison select statements
# jaro winkler statements, using param values suggested on page 355 of https://imai.fas.harvard.edu/research/files/linkage.pdf
# American Political Science Review (2019) 113, 2, 353–371, Using a Probabilistic Model to Assist Merging of Large-Scale
# Administrative Records
def sql_gen_gammas_case_stmt_jaro_2(col_name, gamma_col_name=None, threshold=0.94):
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
when jaro_winkler_sim({col_name}_l, {col_name}_r) > {threshold} then 1
else 0 end"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
def sql_gen_gammas_case_stmt_jaro_3(
col_name, gamma_col_name=None, threshold1=0.94, threshold2=0.88
):
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
when jaro_winkler_sim({col_name}_l, {col_name}_r) > {threshold1} then 2
when jaro_winkler_sim({col_name}_l, {col_name}_r) > {threshold2} then 1
else 0 end"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
def sql_gen_gammas_case_stmt_jaro_4(
col_name, gamma_col_name=None, threshold1=0.94, threshold2=0.88, threshold3=0.7
):
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
when jaro_winkler_sim({col_name}_l, {col_name}_r) > {threshold1} then 3
when jaro_winkler_sim({col_name}_l, {col_name}_r) > {threshold2} then 2
when jaro_winkler_sim({col_name}_l, {col_name}_r) > {threshold3} then 1
else 0 end"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
# levenshtein fallbacks for if string similarity jar isn't available
def sql_gen_case_stmt_levenshtein_3(col_name, gamma_col_name=None, threshold=0.3):
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
when {col_name}_l = {col_name}_r then 2
when levenshtein({col_name}_l, {col_name}_r)/((length({col_name}_l) + length({col_name}_r))/2) <= {threshold}
then 1
else 0 end"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
def sql_gen_case_stmt_levenshtein_4(
col_name, gamma_col_name=None, threshold1=0.2, threshold2=0.4
):
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
when {col_name}_l = {col_name}_r then 3
when levenshtein({col_name}_l, {col_name}_r)/((length({col_name}_l) + length({col_name}_r))/2) <= {threshold1}
then 2
when levenshtein({col_name}_l, {col_name}_r)/((length({col_name}_l) + length({col_name}_r))/2) <= {threshold2}
then 1
else 0 end"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
# Numeric value statements
def _sql_gen_max_of_two_cols(col1, col2):
return f"""
case
when {col1} > {col2} then {col1}
else {col2}
end
"""
def _sql_gen_abs_diff(col1, col2):
return f"(abs({col1} - {col2}))"
def sql_gen_case_stmt_numeric_2(col_name, gamma_col_name=None):
col1 = f"{col_name}_l"
col2 = f"{col_name}_r"
abs_difference = _sql_gen_abs_diff(col1, col2)
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
when {abs_difference} < 0.00001 then 1
else 0 end"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
def sql_gen_case_stmt_numeric_abs_3(
col_name, gamma_col_name=None, abs_amount=1, equality_threshold=0.0001
):
col1 = f"{col_name}_l"
col2 = f"{col_name}_r"
abs_difference = _sql_gen_abs_diff(col1, col2)
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
when {abs_difference} < {equality_threshold} THEN 2
when {abs_difference} < {abs_amount} THEN 1
else 0 end"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
def sql_gen_case_stmt_numeric_abs_4(
col_name,
gamma_col_name=None,
abs_amount_low=1,
abs_amount_high=10,
equality_threshold=0.0001,
):
col1 = f"{col_name}_l"
col2 = f"{col_name}_r"
abs_difference = _sql_gen_abs_diff(col1, col2)
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
when {abs_difference} < {equality_threshold} THEN 3
when {abs_difference} < {abs_amount_low} THEN 2
when {abs_difference} < {abs_amount_high} THEN 1
else 0 end"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
def sql_gen_case_stmt_numeric_perc_3(
col_name, gamma_col_name=None, per_diff=0.05, equality_threshold=0.0001
):
col1 = f"{col_name}_l"
col2 = f"{col_name}_r"
max_of_cols = _sql_gen_max_of_two_cols(col1, col2)
abs_difference = _sql_gen_abs_diff(col1, col2)
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
when {abs_difference}/abs({max_of_cols}) < {equality_threshold} then 2
when {abs_difference}/abs({max_of_cols}) < {per_diff} then 1
else 0 end"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
def sql_gen_case_stmt_numeric_perc_4(
col_name,
gamma_col_name=None,
per_diff_low=0.05,
per_diff_high=0.10,
equality_threshold=0.0001,
):
col1 = f"{col_name}_l"
col2 = f"{col_name}_r"
max_of_cols = _sql_gen_max_of_two_cols(col1, col2)
abs_difference = _sql_gen_abs_diff(col1, col2)
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
when {abs_difference}/abs({max_of_cols}) < {equality_threshold} then 3
when {abs_difference}/abs({max_of_cols}) < {per_diff_low} then 2
when {abs_difference}/abs({max_of_cols}) < {per_diff_high} then 1
else 0 end"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
def _sql_gen_get_or_list_jaro(col_name, other_name_cols, threshold=0.94):
# Note the ifnull 1234 just ensures that if one of the other columns is null, the jaro score is lower than the threshold
ors = [
f"jaro_winkler_sim(ifnull({col_name}_l, '1234abcd5678'), ifnull({n}_r, '987pqrxyz654')) > {threshold}"
for n in other_name_cols
]
ors_string = " OR ".join(ors)
return f"({ors_string})"
def sql_gen_gammas_name_inversion_4(
col_name: str,
other_name_cols: list,
gamma_col_name=None,
threshold1=0.94,
threshold2=0.88,
include_dmeta=False,
):
"""Generate a case expression which can handle name inversions where e.g. surname and forename are inverted
Args:
col_name (str): The name of the column we want to generate a custom case expression for e.g. surname
other_name_cols (list): The name of the other columns that contain names e.g. forename1, forename2
gamma_col_name (str, optional): . The name of the column, for the alias e.g. surname
threshold1 (float, optional): Jaro threshold for almost exact match. Defaults to 0.94.
threshold2 (float, optional): Jaro threshold for close match Defaults to 0.88.
include_dmeta (bool, optional): Also allow a dmetaphone match at threshold2
Returns:
str: A sql string
"""
dmeta_statment = ""
if include_dmeta:
dmeta_statment = f"""
when Dmetaphone({col_name}_l) = Dmetaphone({col_name}_r) then 1
when DmetaphoneAlt({col_name}_l) = DmetaphoneAlt({col_name}_r) then 1
"""
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
when jaro_winkler_sim({col_name}_l, {col_name}_r) > {threshold1} then 3
when {_sql_gen_get_or_list_jaro(col_name, other_name_cols, threshold1)} then 2
{dmeta_statment}
when jaro_winkler_sim({col_name}_l, {col_name}_r) > {threshold2} then 1
else 0 end"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
# Array comparison functions
def sql_gen_case_stmt_array_intersect_2(
col_name: str, gamma_col_name=None, zero_length_is_null=True
):
"""Generate a case comparison which is 1 if the size of the intersection of the arrays is
one or more. Otherwise zero.
Args:
col_name (str): The name of the column we want to generate a custom case expression for e.g. phone_number
gamma_col_name (str, optional): . The name of the column, for the alias e.g. surname
zero_length_is_null (bool, optional): Whether to treat a zero length array as a null. Defaults to True.
"""
zero_length_expr = ""
if zero_length_is_null:
zero_length_expr = (
f"when size({col_name}_l) = 0 or size({col_name}_r) = 0 then -1"
)
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
{zero_length_expr}
when size(array_intersect({col_name}_l, {col_name}_r)) >= 1 then 1
else 0
end
"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
def sql_gen_case_stmt_array_intersect_3(
col_name: str, gamma_col_name=None, zero_length_is_null=True
):
"""Generate a three level case comparison based on the size of the intersection of the arrays
Args:
col_name (str): The name of the column we want to generate a custom case expression for e.g. phone_number
gamma_col_name (str, optional): . The name of the column, for the alias e.g. surname
zero_length_is_null (bool, optional): Whether to treat a zero length array as a null. Defaults to True.
"""
zero_length_expr = ""
if zero_length_is_null:
zero_length_expr = (
f"when size({col_name}_l) = 0 or size({col_name}_r) = 0 then -1"
)
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
{zero_length_expr}
when size(array_intersect({col_name}_l, {col_name}_r)) > 1 then 2
when size(array_intersect({col_name}_l, {col_name}_r)) = 1 then 1
else 0
end
"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
def _daexplode(col_name):
return f"DualArrayExplode({col_name}_l, {col_name}_r)"
def _score_all_pairwise_combinations_distance(col_name, fn_name):
return f"""
transform({_daexplode(col_name)},
x -> {fn_name}(x['_1'], x['_2'] )
)
"""
def _compare_pairwise_transformed_combinations(col_name, fn_name):
return f"""
transform(
{_daexplode(col_name)},
x -> {fn_name}(x['_1']) = {fn_name}(x['_2'])
)
"""
def _jaro_winkler_array(col_name):
return _score_all_pairwise_combinations_distance(col_name, "jaro_winkler_sim")
def _leven_array(col_name):
return _score_all_pairwise_combinations_distance(col_name, "levenshtein")
def _dmeta_array(col_name):
# Is at least one true
return f"""exists(
{_compare_pairwise_transformed_combinations(col_name, "Dmetaphone")},
x -> x
)
"""
def _size_intersect(col_name):
return f"size(array_intersect({col_name}_l, {col_name}_r))"
def sql_gen_case_stmt_array_combinations_leven_3(
col_name: str,
threshold_1: int = 1,
threshold_2: int = 2,
gamma_col_name=None,
zero_length_is_null=True,
):
"""Compare all combinations of values in input arrays. Gamma level 2 if minimum levenshtein score is <=
threshold_1. Gamma level 1 if min score is <= threshold_2. Otherwise level 0
Args:
col_name (str): The name of the column we want to generate a custom case expression for e.g. surname
threshold_1 (int, optional): Defaults to 1.
threshold_2 (int, optional): Defaults to 2.
gamma_col_name (str, optional): . The name of the column, for the alias e.g. surname
zero_length_is_null (bool, optional): Whether to treat a zero length array as a null. Defaults to True.
"""
zero_length_expr = ""
if zero_length_is_null:
zero_length_expr = (
f"when size({col_name}_l) = 0 or size({col_name}_r) = 0 then -1"
)
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
{zero_length_expr}
when {_size_intersect(col_name)} >= 1 then 2
when array_min({_leven_array(col_name)}) <= {threshold_1} then 2
when array_min({_leven_array(col_name)}) <= {threshold_2} then 1
else 0
end
"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
def sql_gen_case_stmt_array_combinations_jaro_3(
col_name: str,
threshold_1=0.94,
threshold_2=0.88,
gamma_col_name=None,
zero_length_is_null=True,
):
"""Compare all combinations of values in input arrays. Gamma level 2 if max jaro_winkler score is >=
threshold_1. Gamma level 1 if max score is >= threshold_2. Otherwise level 0
Args:
col_name (str): The name of the column we want to generate a custom case expression for e.g. surname
threshold_1 (int, optional): Defaults to 0.94.
threshold_2 (int, optional): Defaults to 0.88.
gamma_col_name (str, optional): . The name of the column, for the alias e.g. surname
zero_length_is_null (bool, optional): Whether to treat a zero length array as a null. Defaults to True.
"""
zero_length_expr = ""
if zero_length_is_null:
zero_length_expr = (
f"when size({col_name}_l) = 0 or size({col_name}_r) = 0 then -1"
)
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
{zero_length_expr}
when {_size_intersect(col_name)} >= 1 then 2
when array_max({_jaro_winkler_array(col_name)}) > {threshold_1} then 2
when array_max({_jaro_winkler_array(col_name)}) > {threshold_2} then 1
else 0
end
"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c
def sql_gen_case_stmt_array_combinations_jaro_dmeta_4(
col_name: str,
threshold_1=0.94,
threshold_2=0.88,
gamma_col_name=None,
zero_length_is_null=True,
):
"""Compare all combinations of values in input arrays.
Gamma level 3 if max jaro_winkler score is >= threshold_1
Gamma level 2 if there's at least one match on dmetaphone
Gamma level 1 if max jaro_winkler score is >= threshold_2
else Gamma level 0
Args:
col_name (str): The name of the column we want to generate a custom case expression for e.g. surname
threshold_1 (int, optional): Defaults to 0.94.
threshold_2 (int, optional): Defaults to 0.88.
gamma_col_name (str, optional): . The name of the column, for the alias e.g. surname
zero_length_is_null (bool, optional): Whether to treat a zero length array as a null. Defaults to True.
"""
zero_length_expr = ""
if zero_length_is_null:
zero_length_expr = (
f"when size({col_name}_l) = 0 or size({col_name}_r) = 0 then -1"
)
c = f"""case
when {col_name}_l is null or {col_name}_r is null then -1
{zero_length_expr}
when {_size_intersect(col_name)} >= 1 then 3
when array_max({_jaro_winkler_array(col_name)}) > {threshold_1} then 3
when {_dmeta_array(col_name)} then 2
when array_max({_jaro_winkler_array(col_name)}) > {threshold_2} then 1
else 0
end
"""
if gamma_col_name is not None:
return _add_as_gamma_to_case_statement(c, gamma_col_name)
else:
return c