-
Notifications
You must be signed in to change notification settings - Fork 42
/
test_public_interface.py
1170 lines (947 loc) · 34.2 KB
/
test_public_interface.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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pytest
import chispa
import pyspark
from delta import DeltaTable, configure_spark_with_delta_pip
from datetime import datetime as dt
from pyspark.sql.types import (
StructType,
StructField,
StringType,
IntegerType,
BooleanType,
DateType,
TimestampType,
)
import mack
builder = (
pyspark.sql.SparkSession.builder.appName("MyApp")
.config("spark.sql.extensions", "io.delta.sql.DeltaSparkSessionExtension")
.config(
"spark.sql.catalog.spark_catalog",
"org.apache.spark.sql.delta.catalog.DeltaCatalog",
)
.config("spark.sql.shuffle.partitions", "2")
)
spark = configure_spark_with_delta_pip(builder).getOrCreate()
# upsert
def test_upserts_with_single_attribute(tmp_path):
path = f"{tmp_path}/tmp/delta-upsert-single-attr"
data2 = [
(1, "A", True, dt(2019, 1, 1), None),
(2, "B", True, dt(2019, 1, 1), None),
(4, "D", True, dt(2019, 1, 1), None),
]
schema = StructType(
[
StructField("pkey", IntegerType(), True),
StructField("attr", StringType(), True),
StructField("is_current", BooleanType(), True),
StructField("effective_time", TimestampType(), True),
StructField("end_time", TimestampType(), True),
]
)
df = spark.createDataFrame(data=data2, schema=schema)
df.write.format("delta").save(path)
updates_data = [
(2, "Z", dt(2020, 1, 1)), # value to upsert
(3, "C", dt(2020, 9, 15)), # new value
]
updates_schema = StructType(
[
StructField("pkey", IntegerType(), True),
StructField("attr", StringType(), True),
StructField("effective_time", TimestampType(), True),
]
)
updates_df = spark.createDataFrame(data=updates_data, schema=updates_schema)
delta_table = DeltaTable.forPath(spark, path)
mack.type_2_scd_upsert(delta_table, updates_df, "pkey", ["attr"])
actual_df = spark.read.format("delta").load(path)
expected_df = spark.createDataFrame(
[
(2, "B", False, dt(2019, 1, 1), dt(2020, 1, 1)),
(3, "C", True, dt(2020, 9, 15), None),
(2, "Z", True, dt(2020, 1, 1), None),
(4, "D", True, dt(2019, 1, 1), None),
(1, "A", True, dt(2019, 1, 1), None),
],
schema,
)
chispa.assert_df_equality(actual_df, expected_df, ignore_row_order=True)
def test_errors_out_if_base_df_does_not_have_all_required_columns(tmp_path):
path = f"{tmp_path}/tmp/delta-incomplete"
data2 = [
("A", True, dt(2019, 1, 1), None),
("B", True, dt(2019, 1, 1), None),
("D", True, dt(2019, 1, 1), None),
]
schema = StructType(
[
# pkey is missing from base!
StructField("attr", StringType(), True),
StructField("is_current", BooleanType(), True),
StructField("effective_time", TimestampType(), True),
StructField("end_time", TimestampType(), True),
]
)
df = spark.createDataFrame(data=data2, schema=schema)
df.write.format("delta").save(path)
updates_data = [
(2, "Z", dt(2020, 1, 1)), # value to upsert
(3, "C", dt(2020, 9, 15)), # new value
]
updates_schema = StructType(
[
StructField("pkey", IntegerType(), True),
StructField("attr", StringType(), True),
StructField("effective_time", TimestampType(), True),
]
)
updates_df = spark.createDataFrame(data=updates_data, schema=updates_schema)
delta_table = DeltaTable.forPath(spark, path)
with pytest.raises(TypeError):
mack.type_2_scd_upsert(delta_table, updates_df, "pkey", ["attr"])
def test_errors_out_if_updates_table_does_not_contain_all_required_columns(tmp_path):
path = f"{tmp_path}/tmp/delta-error-udpate-missing-col"
data2 = [
(1, "A", True, dt(2019, 1, 1), None),
(2, "B", True, dt(2019, 1, 1), None),
(4, "D", True, dt(2019, 1, 1), None),
]
schema = StructType(
[
StructField("pkey", IntegerType(), True),
StructField("attr", StringType(), True),
StructField("is_current", BooleanType(), True),
StructField("effective_time", TimestampType(), True),
StructField("end_time", TimestampType(), True),
]
)
df = spark.createDataFrame(data=data2, schema=schema)
df.write.format("delta").save(path)
updates_data = [
("Z", dt(2020, 1, 1)), # value to upsert
("C", dt(2020, 9, 15)), # new value
]
updates_schema = StructType(
[
# pkey is missing from updates DataFrame
StructField("attr", StringType(), True),
StructField("effective_time", TimestampType(), True),
]
)
updates_df = spark.createDataFrame(data=updates_data, schema=updates_schema)
delta_table = DeltaTable.forPath(spark, path)
with pytest.raises(TypeError):
mack.type_2_scd_upsert(delta_table, updates_df, "pkey", ["attr"])
def test_upserts_based_on_multiple_attributes(tmp_path):
path = f"{tmp_path}/tmp/delta-upsert-multiple-attr"
data2 = [
(1, "A", "A", True, dt(2019, 1, 1), None),
(2, "B", "B", True, dt(2019, 1, 1), None),
(4, "D", "D", True, dt(2019, 1, 1), None),
]
schema = StructType(
[
StructField("pkey", IntegerType(), True),
StructField("attr1", StringType(), True),
StructField("attr2", StringType(), True),
StructField("is_current", BooleanType(), True),
StructField("effective_time", TimestampType(), True),
StructField("end_time", TimestampType(), True),
]
)
df = spark.createDataFrame(data=data2, schema=schema)
df.write.format("delta").save(path)
updates_data = [
(2, "Z", None, dt(2020, 1, 1)), # value to upsert
(3, "C", "C", dt(2020, 9, 15)), # new value
]
updates_schema = StructType(
[
StructField("pkey", IntegerType(), True),
StructField("attr1", StringType(), True),
StructField("attr2", StringType(), True),
StructField("effective_time", TimestampType(), True),
]
)
updates_df = spark.createDataFrame(data=updates_data, schema=updates_schema)
delta_table = DeltaTable.forPath(spark, path)
mack.type_2_scd_upsert(delta_table, updates_df, "pkey", ["attr1", "attr2"])
actual_df = spark.read.format("delta").load(path)
expected_df = spark.createDataFrame(
[
(2, "B", "B", False, dt(2019, 1, 1), dt(2020, 1, 1)),
(3, "C", "C", True, dt(2020, 9, 15), None),
(2, "Z", None, True, dt(2020, 1, 1), None),
(4, "D", "D", True, dt(2019, 1, 1), None),
(1, "A", "A", True, dt(2019, 1, 1), None),
],
schema,
)
chispa.assert_df_equality(actual_df, expected_df, ignore_row_order=True)
# def describe_type_2_scd_generic_upsert():
# type_2_scd_generic_upsert
def test_upserts_based_on_date_columns(tmp_path):
path = f"{tmp_path}/tmp/delta-upsert-date"
# // create Delta Lake
data2 = [
(1, "A", True, dt(2019, 1, 1), None),
(2, "B", True, dt(2019, 1, 1), None),
(4, "D", True, dt(2019, 1, 1), None),
]
schema = StructType(
[
StructField("pkey", IntegerType(), True),
StructField("attr", StringType(), True),
StructField("cur", BooleanType(), True),
StructField("effective_date", DateType(), True),
StructField("end_date", DateType(), True),
]
)
df = spark.createDataFrame(data=data2, schema=schema)
df.write.format("delta").save(path)
# create updates DF
updates_df = spark.createDataFrame(
[
(3, "C", dt(2020, 9, 15)), # new value
(2, "Z", dt(2020, 1, 1)), # value to upsert
]
).toDF("pkey", "attr", "effective_date")
# perform upsert
delta_table = DeltaTable.forPath(spark, path)
mack.type_2_scd_generic_upsert(
delta_table, updates_df, "pkey", ["attr"], "cur", "effective_date", "end_date"
)
actual_df = spark.read.format("delta").load(path)
expected_df = spark.createDataFrame(
[
(2, "B", False, dt(2019, 1, 1), dt(2020, 1, 1)),
(3, "C", True, dt(2020, 9, 15), None),
(2, "Z", True, dt(2020, 1, 1), None),
(4, "D", True, dt(2019, 1, 1), None),
(1, "A", True, dt(2019, 1, 1), None),
],
schema,
)
chispa.assert_df_equality(actual_df, expected_df, ignore_row_order=True)
def test_upserts_based_on_version_number(tmp_path):
path = f"{tmp_path}/tmp/delta-upsert-version"
# create Delta Lake
data2 = [
(1, "A", True, 1, None),
(2, "B", True, 1, None),
(4, "D", True, 1, None),
]
schema = StructType(
[
StructField("pkey", IntegerType(), True),
StructField("attr", StringType(), True),
StructField("is_current", BooleanType(), True),
StructField("effective_ver", IntegerType(), True),
StructField("end_ver", IntegerType(), True),
]
)
df = spark.createDataFrame(data=data2, schema=schema)
df.write.format("delta").save(path)
# create updates DF
updates_df = spark.createDataFrame(
[
(2, "Z", 2), # value to upsert
(3, "C", 3), # new value
]
).toDF("pkey", "attr", "effective_ver")
# perform upsert
delta_table = DeltaTable.forPath(spark, path)
mack.type_2_scd_generic_upsert(
delta_table,
updates_df,
"pkey",
["attr"],
"is_current",
"effective_ver",
"end_ver",
)
# show result
res = spark.read.format("delta").load(path)
expected_data = [
(2, "B", False, 1, 2),
(3, "C", True, 3, None),
(2, "Z", True, 2, None),
(4, "D", True, 1, None),
(1, "A", True, 1, None),
]
expected = spark.createDataFrame(expected_data, schema)
chispa.assert_df_equality(res, expected, ignore_row_order=True)
def test_upserts_does_not_insert_duplicate(tmp_path):
path = f"{tmp_path}/tmp/delta-no-duplicate"
# create Delta Lake
data2 = [
(1, "A", True, dt(2019, 1, 1), None),
(2, "B", True, dt(2019, 1, 1), None),
(4, "D", True, dt(2019, 1, 1), None),
]
schema = StructType(
[
StructField("pkey", IntegerType(), True),
StructField("attr", StringType(), True),
StructField("cur", BooleanType(), True),
StructField("effective_date", DateType(), True),
StructField("end_date", DateType(), True),
]
)
df = spark.createDataFrame(data=data2, schema=schema)
df.write.format("delta").save(path)
# create updates DF
updates_df = spark.createDataFrame(
[
(1, "A", dt(2019, 1, 1)), # duplicate row
]
).toDF("pkey", "attr", "effective_date")
# perform upsert
delta_table = DeltaTable.forPath(spark, path)
mack.type_2_scd_generic_upsert(
delta_table, updates_df, "pkey", ["attr"], "cur", "effective_date", "end_date"
)
actual_df = spark.read.format("delta").load(path)
expected_df = spark.createDataFrame(
[
(1, "A", True, dt(2019, 1, 1), None),
(2, "B", True, dt(2019, 1, 1), None),
(4, "D", True, dt(2019, 1, 1), None),
],
schema,
)
chispa.assert_df_equality(actual_df, expected_df, ignore_row_order=True)
# def describe_kill_duplicates():
def test_kills_duplicates_in_a_delta_table(tmp_path):
path = f"{tmp_path}/deduplicate1"
data = [
(1, "A", "A"), # duplicate
(2, "A", "B"),
(3, "A", "A"), # duplicate
(4, "A", "A"), # duplicate
(5, "B", "B"), # duplicate
(6, "D", "D"),
(9, "B", "B"), # duplicate
]
df = spark.createDataFrame(data, ["col1", "col2", "col3"])
df.write.format("delta").save(path)
delta_table = DeltaTable.forPath(spark, path)
mack.kill_duplicates(delta_table, ["col3", "col2"])
res = spark.read.format("delta").load(path)
expected_data = [
(2, "A", "B"),
(6, "D", "D"),
]
expected = spark.createDataFrame(expected_data, ["col1", "col2", "col3"])
chispa.assert_df_equality(res, expected, ignore_row_order=True)
def test_drop_duplicates_pkey_in_a_delta_table(tmp_path):
path = f"{tmp_path}/drop_duplicates_pkey"
data = [
(1, "A", "A", "C"), # duplicate
(2, "A", "B", "C"),
(3, "A", "A", "D"), # duplicate
(4, "A", "A", "E"), # duplicate
(5, "B", "B", "C"), # duplicate
(6, "D", "D", "C"),
(9, "B", "B", "E"), # duplicate
]
df = spark.createDataFrame(data, ["col1", "col2", "col3", "col4"])
df.write.format("delta").save(path)
delta_table = DeltaTable.forPath(spark, path)
mack.drop_duplicates_pkey(delta_table, "col1", ["col2", "col3"])
res = spark.read.format("delta").load(path)
expected_data = [
(1, "A", "A", "C"),
(2, "A", "B", "C"),
(5, "B", "B", "C"),
(6, "D", "D", "C"),
]
expected = spark.createDataFrame(expected_data, ["col1", "col2", "col3", "col4"])
chispa.assert_df_equality(res, expected, ignore_row_order=True)
def test_drop_duplicates_pkey_in_a_delta_table_no_duplication_cols(tmp_path):
path = f"{tmp_path}/drop_duplicates_pkey_no_duplication_cols"
data = [
(1, "A", "A", "C"), # duplicate
(1, "A", "A", "C"), # duplicate
(1, "A", "A", "C"), # duplicate
(1, "A", "A", "C"), # duplicate
]
df = spark.createDataFrame(data, ["col1", "col2", "col3", "col4"])
df.write.format("delta").save(path)
delta_table = DeltaTable.forPath(spark, path)
with pytest.raises(TypeError):
mack.drop_duplicates_pkey(delta_table, "col1", [])
def test_drop_duplicates_in_a_delta_table(tmp_path):
path = f"{tmp_path}/drop_duplicates"
data = [
(1, "A", "A", "C"), # duplicate
(1, "A", "A", "C"), # duplicate
(1, "A", "A", "C"), # duplicate
(1, "A", "A", "C"), # duplicate
]
df = spark.createDataFrame(data, ["col1", "col2", "col3", "col4"])
df.write.format("delta").save(path)
delta_table = DeltaTable.forPath(spark, path)
mack.drop_duplicates(delta_table, ["col1"]),
res = spark.read.format("delta").load(path)
expected_data = [
(1, "A", "A", "C"),
]
expected = spark.createDataFrame(expected_data, ["col1", "col2", "col3", "col4"])
chispa.assert_df_equality(res, expected, ignore_row_order=True)
def test_copy_delta_table(tmp_path):
path = f"{tmp_path}/copy_test_1"
data = [
(1, "A", "A"),
(2, "A", "B"),
]
df = spark.createDataFrame(data, ["col1", "col2", "col3"])
(
df.write.format("delta")
.partitionBy(["col1"])
.option("delta.logRetentionDuration", "interval 30 days")
.save(path)
)
origin_table = DeltaTable.forPath(spark, path)
origin_details = origin_table.detail().select("partitionColumns", "properties")
mack.copy_table(origin_table, f"{tmp_path}/copy_test_2")
copied_table = DeltaTable.forPath(spark, f"{tmp_path}/copy_test_2")
copied_details = copied_table.detail().select("partitionColumns", "properties")
chispa.assert_df_equality(origin_details, copied_details)
chispa.assert_df_equality(
origin_table.toDF(), copied_table.toDF(), ignore_row_order=True
)
# append without duplicates
def test_append_without_duplicates_single_column(tmp_path):
path = f"{tmp_path}/append_without_duplicates"
data = [
(1, "A", "B"),
(2, "C", "D"),
(3, "E", "F"),
]
df = spark.createDataFrame(data, ["col1", "col2", "col3"])
df.write.format("delta").save(path)
delta_table = DeltaTable.forPath(spark, path)
append_df = spark.createDataFrame(
[
(2, "R", "T"), # duplicate
(8, "A", "B"),
(8, "B", "C"), # duplicate
(10, "X", "Y"),
],
["col1", "col2", "col3"],
)
mack.append_without_duplicates(delta_table, append_df, ["col1"])
appended_data = spark.read.format("delta").load(path)
expected_data = [
(1, "A", "B"),
(2, "C", "D"),
(3, "E", "F"),
(8, "A", "B"),
(10, "X", "Y"),
]
expected = spark.createDataFrame(expected_data, ["col1", "col2", "col3"])
chispa.assert_df_equality(appended_data, expected, ignore_row_order=True)
def test_validate_append(tmp_path):
path = f"{tmp_path}/validate_append"
def append_fun(delta_table, append_df):
mack.validate_append(
delta_table,
append_df,
required_cols=["col1", "col2"],
optional_cols=["col4"],
)
# Create Delta table
data = [
(1, "a", "A"),
(2, "b", "B"),
]
df = spark.createDataFrame(data, ["col1", "col2", "col3"])
df.write.format("delta").save(path)
# Demonstrate that certain DataFrames with optional columns can be appended
delta_table = DeltaTable.forPath(spark, path)
append_df = spark.createDataFrame(
[
(3, "c", "cat"),
(4, "d", "dog"),
],
["col1", "col2", "col4"],
)
append_fun(delta_table, append_df)
expected_data = [
(1, "a", "A", None),
(2, "b", "B", None),
(3, "c", None, "cat"),
(4, "d", None, "dog"),
]
expected = spark.createDataFrame(expected_data, ["col1", "col2", "col3", "col4"])
chispa.assert_df_equality(
spark.read.format("delta").load(path), expected, ignore_row_order=True
)
# demonstrate that DataFrames with columns that are not on the accept list cannot be appended
append_df = spark.createDataFrame(
[
(4, "b", "A"),
(5, "y", "C"),
(6, "z", "D"),
],
["col1", "col2", "col5"],
)
with pytest.raises(TypeError):
mack.validate_append(
delta_table,
append_df,
required_cols=["col1", "col2"],
optional_cols=["col4"],
)
# demonstrate that DataFrames with missing required columns cannot be appended
append_df = spark.createDataFrame(
[
(4, "A"),
(5, "C"),
(6, "D"),
],
["col1", "col4"],
)
with pytest.raises(TypeError):
mack.validate_append(
delta_table,
append_df,
required_cols=["col1", "col2"],
optional_cols=["col4"],
)
def test_append_without_duplicates_multi_column(tmp_path):
path = f"{tmp_path}/append_without_duplicates"
data = [
(1, "a", "A"),
(2, "b", "R"),
(3, "c", "X"),
]
df = spark.createDataFrame(data, ["col1", "col2", "col3"])
df.write.format("delta").save(path)
delta_table = DeltaTable.forPath(spark, path)
append_data = spark.createDataFrame(
[
(2, "b", "R"), # duplicate col1, col2
(2, "x", "R"), # NOT duplicate col1, col2
(8, "y", "F"),
(10, "z", "U"),
],
["col1", "col2", "col3"],
)
mack.append_without_duplicates(delta_table, append_data, ["col1", "col2"])
appended_data = spark.read.format("delta").load(path)
expected_data = [
(1, "a", "A"),
(2, "b", "R"),
(2, "x", "R"),
(3, "c", "X"),
(8, "y", "F"),
(10, "z", "U"),
]
expected = spark.createDataFrame(expected_data, ["col1", "col2", "col3"])
chispa.assert_df_equality(appended_data, expected, ignore_row_order=True)
def test_is_composite_key_candidate(tmp_path):
path = f"{tmp_path}/is_composite_key_candidate"
data = [
(1, "a", "A"),
(2, "b", "R"),
(2, "c", "D"),
(3, "e", "F"),
]
df = spark.createDataFrame(data, ["col1", "col2", "col3"])
df.write.format("delta").save(path)
delta_table = DeltaTable.forPath(spark, path)
assert not mack.is_composite_key_candidate(delta_table, ["col1"])
assert mack.is_composite_key_candidate(delta_table, ["col1", "col2"])
def test_delta_file_sizes(tmp_path):
path = f"{tmp_path}/delta_file_sizes"
data = [
(1, "A", "A"),
(2, "A", "B"),
]
df = spark.createDataFrame(data, ["col1", "col2", "col3"])
(
df.write.format("delta")
.partitionBy(["col1"])
.option("delta.logRetentionDuration", "interval 30 days")
.save(path)
)
delta_table = DeltaTable.forPath(spark, path)
result = mack.delta_file_sizes(delta_table)
expected_result = {
"size_in_bytes": 1320,
"number_of_files": 2,
"average_file_size_in_bytes": 660,
}
assert result == expected_result
def test_show_delta_file_sizes(capfd, tmp_path):
path = f"{tmp_path}/show_delta_file_sizes"
data = [
(1, "A", "A"),
(2, "A", "B"),
]
df = spark.createDataFrame(data, ["col1", "col2", "col3"])
(df.write.format("delta").partitionBy(["col1"]).save(path))
delta_table = DeltaTable.forPath(spark, path)
mack.show_delta_file_sizes(delta_table)
out, _ = capfd.readouterr()
assert (
out
== "The delta table contains 2 files with a size of 1.32 kB. The average file size is 660.0 B\n"
)
def test_humanize_bytes_formats_nicely():
assert mack.humanize_bytes(12345678) == "12.35 MB"
assert mack.humanize_bytes(1234567890) == "1.23 GB"
assert mack.humanize_bytes(1234567890000) == "1.23 TB"
assert mack.humanize_bytes(1234567890000000) == "1.23 PB"
def test_humanize_bytes_binary_formats_nicely():
assert mack.humanize_bytes_binary(12345678) == "11.77 MB"
assert mack.humanize_bytes_binary(1234567890) == "1.15 GB"
assert mack.humanize_bytes_binary(1234567890000) == "1.12 TB"
assert mack.humanize_bytes_binary(1234567890000000) == "1.10 PB"
def test_find_composite_key(tmp_path):
path = f"{tmp_path}/find_composite_key"
data = [
(1, "a", "z"),
(1, "a", "b"),
(3, "c", "b"),
]
df = spark.createDataFrame(
data,
[
"col1",
"col2",
"col3",
],
)
df.write.format("delta").save(path)
delta_table = DeltaTable.forPath(spark, path)
composite_keys = mack.find_composite_key_candidates(delta_table)
expected_keys = ["col1", "col3"]
assert composite_keys == expected_keys
def test_find_composite_key_with_value_error(tmp_path):
path = f"{tmp_path}/find_composite_key"
data = [
(1, "a", "A"),
(2, "b", "R"),
(2, "c", "D"),
(3, "e", "F"),
]
df = spark.createDataFrame(data, ["col1", "col2", "col3"])
df.write.format("delta").save(path)
delta_table = DeltaTable.forPath(spark, path)
with pytest.raises(
ValueError, match="No composite key candidates could be identified."
):
mack.find_composite_key_candidates(delta_table, ["col2", "col3"])
def test_with_md5_cols(tmp_path):
path = f"{tmp_path}/find_composite_key"
data = [
(1, "a", None),
(2, "b", "b"),
(3, "c", "c"),
]
df = spark.createDataFrame(
data,
[
"col1",
"col2",
"col3",
],
)
df.write.format("delta").save(path)
delta_table = DeltaTable.forPath(spark, path)
with_md5 = mack.with_md5_cols(delta_table, ["col2", "col3"])
expected_data = [
(1, "a", None, "0cc175b9c0f1b6a831c399e269772661"),
(2, "b", "b", "1eeaac3814eb80cc40efb005cf0b9141"),
(3, "c", "c", "4e202f8309e7b00349c70845ab02fce9"),
]
expected_df = spark.createDataFrame(
expected_data,
["col1", "col2", "col3", "md5_col2_col3"],
)
chispa.assert_df_equality(
with_md5, expected_df, ignore_row_order=True, ignore_nullable=True
)
def test_lastest_version(tmp_path):
path = f"{tmp_path}/latestversion"
data = [
(1, "a", None),
(2, "b", "b"),
(3, "c", "c"),
]
df = spark.createDataFrame(
data,
[
"col1",
"col2",
"col3",
],
)
df.write.format("delta").save(path)
# write the same dataframe twice
df.write.format("delta").mode("append").save(path)
df.write.format("delta").mode("append").save(path)
delta_table = DeltaTable.forPath(spark, path)
latest_version = mack.latest_version(delta_table)
assert latest_version == 2
def test_constraint_append_no_constraint(tmp_path):
target_path = f"{tmp_path}/constraint_append/target_table"
quarantine_path = f"{tmp_path}/constraint_append/quarantine_table"
data = [
(1, "A", "B"),
(2, "C", "D"),
(3, "E", "F"),
]
df = spark.createDataFrame(data, ["col1", "col2", "col3"])
df.write.format("delta").save(target_path)
df2 = spark.createDataFrame([], df.schema)
df2.write.format("delta").save(quarantine_path)
target_table = DeltaTable.forPath(spark, target_path)
append_df = spark.createDataFrame([], df.schema)
quarantine_table = DeltaTable.forPath(spark, quarantine_path)
# demonstrate that the function cannot be run with target table not having constraints
with pytest.raises(
TypeError, match="There are no constraints present in the target delta table"
):
mack.constraint_append(target_table, append_df, quarantine_table)
def test_constraint_append_multi_constraint(tmp_path):
target_path = f"{tmp_path}/constraint_append/target_table"
quarantine_path = f"{tmp_path}/constraint_append/quarantine_table"
data = [
(1, "A", "B"),
(2, "C", "D"),
(3, "E", "F"),
]
df = spark.createDataFrame(data, ["col1", "col2", "col3"])
df.write.format("delta").save(target_path)
df2 = spark.createDataFrame([], df.schema)
df2.write.format("delta").save(quarantine_path)
target_table = DeltaTable.forPath(spark, target_path)
# adding two constraints
spark.sql(
f"ALTER TABLE delta.`{target_path}` ADD CONSTRAINT col1_constraint CHECK (col1 > 0) "
)
spark.sql(
f"ALTER TABLE delta.`{target_path}` ADD CONSTRAINT col2_constraint CHECK (col2 != 'Z') "
)
# adding other table properties
spark.sql(
f"ALTER TABLE delta.`{target_path}` SET TBLPROPERTIES('this.is.my.key' = 12, this.is.my.key2 = true)"
)
append_data = [
(0, "Z", "Z"),
(4, "A", "B"),
(5, "C", "D"),
(6, "E", "F"),
(9, "G", "G"),
(11, "Z", "Z"),
]
append_df = spark.createDataFrame(append_data, ["col1", "col2", "col3"])
# testing with two constraints
target_table = DeltaTable.forPath(spark, target_path)
quarantine_table = DeltaTable.forPath(spark, quarantine_path)
mack.constraint_append(target_table, append_df, quarantine_table)
expected_data = [
(1, "A", "B"),
(2, "C", "D"),
(3, "E", "F"),
(4, "A", "B"),
(5, "C", "D"),
(6, "E", "F"),
(9, "G", "G"),
]
expected_df = spark.createDataFrame(expected_data, ["col1", "col2", "col3"])
appended_data = spark.read.format("delta").load(target_path)
chispa.assert_df_equality(appended_data, expected_df, ignore_row_order=True)
expected_quarantined_data = [(0, "Z", "Z"), (11, "Z", "Z")]
expected_quarantined_df = spark.createDataFrame(
expected_quarantined_data, ["col1", "col2", "col3"]
)
quarantined_data = spark.read.format("delta").load(quarantine_path)
chispa.assert_df_equality(
quarantined_data, expected_quarantined_df, ignore_row_order=True
)
def test_constraint_append_single_constraint(tmp_path):
target_path = f"{tmp_path}/constraint_append/target_table"
quarantine_path = f"{tmp_path}/constraint_append/quarantine_table"
data = [
(1, "A", "B"),
(2, "C", "D"),
(3, "E", "F"),
]
df = spark.createDataFrame(data, ["col1", "col2", "col3"])
df.write.format("delta").save(target_path)
df2 = spark.createDataFrame([], df.schema)
df2.write.format("delta").save(quarantine_path)
target_table = DeltaTable.forPath(spark, target_path)
# adding two constraints
spark.sql(
f"ALTER TABLE delta.`{target_path}` ADD CONSTRAINT col1_constraint CHECK (col1 > 0) "
)
append_data = [
(0, "Z", "Z"),
(4, "A", "B"),
(5, "C", "D"),
(6, "E", "F"),
(9, "G", "G"),
(11, "Z", "Z"),
]
append_df = spark.createDataFrame(append_data, ["col1", "col2", "col3"])
# testing with two constraints
target_table = DeltaTable.forPath(spark, target_path)
quarantine_table = DeltaTable.forPath(spark, quarantine_path)
mack.constraint_append(target_table, append_df, quarantine_table)
expected_data = [
(1, "A", "B"),
(2, "C", "D"),
(3, "E", "F"),
(4, "A", "B"),
(5, "C", "D"),
(6, "E", "F"),
(9, "G", "G"),
(11, "Z", "Z"),
]
expected_df = spark.createDataFrame(expected_data, ["col1", "col2", "col3"])
appended_data = spark.read.format("delta").load(target_path)
chispa.assert_df_equality(appended_data, expected_df, ignore_row_order=True)
expected_quarantined_data = [(0, "Z", "Z")]
expected_quarantined_df = spark.createDataFrame(
expected_quarantined_data, ["col1", "col2", "col3"]
)