-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathjoin_test.py
More file actions
1957 lines (1799 loc) · 105 KB
/
join_test.py
File metadata and controls
1957 lines (1799 loc) · 105 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
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
# Copyright (c) 2020-2026, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from _pytest.mark.structures import ParameterSet
from pyspark.sql.functions import array_contains, broadcast, col, lit
from pyspark.sql.types import *
from asserts import (assert_gpu_and_cpu_are_equal_collect, assert_gpu_and_cpu_row_counts_equal,
assert_gpu_fallback_collect, assert_cpu_and_gpu_are_equal_collect_with_capture,
assert_cpu_and_gpu_are_equal_sql_with_capture, assert_gpu_and_cpu_are_equal_sql)
from conftest import is_emr_runtime
from data_gen import *
from marks import ignore_order, allow_non_gpu, incompat, validate_execs_in_gpu_plan, disable_ansi_mode
from spark_session import with_cpu_session, is_before_spark_330, is_databricks_runtime, is_spark_400_or_later, is_spark_411_or_later
from src.main.python.spark_session import with_gpu_session
# mark this test as ci_1 for mvn verify sanity check in pre-merge CI
pytestmark = [pytest.mark.nightly_resource_consuming_test, pytest.mark.premerge_ci_1]
all_non_sized_join_types = ['LeftSemi', 'LeftAnti', 'Cross']
all_symmetric_sized_join_types = ['Inner', 'FullOuter']
all_asymmetric_sized_join_types = ['LeftOuter', 'RightOuter']
all_sized_join_types = all_symmetric_sized_join_types + all_asymmetric_sized_join_types
all_join_types = all_non_sized_join_types + all_sized_join_types
all_gen = [StringGen(), ByteGen(), ShortGen(), IntegerGen(), LongGen(),
BooleanGen(), DateGen(), TimestampGen(), null_gen,
pytest.param(FloatGen(), marks=[incompat]),
pytest.param(DoubleGen(), marks=[incompat])] + orderable_decimal_gens
all_gen_no_nulls = [StringGen(nullable=False), ByteGen(nullable=False),
ShortGen(nullable=False), IntegerGen(nullable=False), LongGen(nullable=False),
BooleanGen(nullable=False), DateGen(nullable=False), TimestampGen(nullable=False),
pytest.param(FloatGen(nullable=False), marks=[incompat]),
pytest.param(DoubleGen(nullable=False), marks=[incompat])]
basic_struct_gen = StructGen([
['child' + str(ind), sub_gen]
for ind, sub_gen in enumerate([StringGen(), ByteGen(), ShortGen(), IntegerGen(), LongGen(),
BooleanGen(), DateGen(), TimestampGen(), null_gen, decimal_gen_64bit])],
nullable=True)
basic_struct_gen_with_no_null_child = StructGen([
['child' + str(ind), sub_gen]
for ind, sub_gen in enumerate([StringGen(nullable=False), ByteGen(nullable=False),
ShortGen(nullable=False), IntegerGen(nullable=False), LongGen(nullable=False),
BooleanGen(nullable=False), DateGen(nullable=False), TimestampGen(nullable=False)])],
nullable=True)
basic_struct_gen_with_floats = StructGen([['child0', FloatGen()], ['child1', DoubleGen()]], nullable=False)
nested_2d_struct_gens = StructGen([['child0', basic_struct_gen]], nullable=False)
nested_3d_struct_gens = StructGen([['child0', nested_2d_struct_gens]], nullable=False)
struct_gens = [basic_struct_gen, basic_struct_gen_with_no_null_child, nested_2d_struct_gens, nested_3d_struct_gens]
basic_nested_gens = single_level_array_gens + map_string_string_gen + [all_basic_struct_gen, binary_gen]
# data types supported by AST expressions in joins
join_ast_gen = [
boolean_gen, byte_gen, short_gen, int_gen, long_gen, date_gen, timestamp_gen, string_gen
]
# data types not supported by AST expressions in joins
join_no_ast_gen = [
pytest.param(FloatGen(), marks=[incompat]), pytest.param(DoubleGen(), marks=[incompat]),
null_gen, decimal_gen_64bit
]
# Types to use when running joins on small batches. Small batch joins can take a long time
# to run and are mostly redundant with the normal batch size test, so we only run these on a
# set of representative types rather than all types.
join_small_batch_gens = [ StringGen(), IntegerGen(), orderable_decimal_gen_128bit ]
cartesian_join_small_batch_gens = join_small_batch_gens + [basic_struct_gen, ArrayGen(string_gen)]
_sortmerge_join_conf = {'spark.sql.autoBroadcastJoinThreshold': '-1',
'spark.sql.join.preferSortMergeJoin': 'True',
'spark.sql.shuffle.partitions': '2',
}
# For spark to insert a shuffled hash join it has to be enabled with
# "spark.sql.join.preferSortMergeJoin" = "false" and both sides have to
# be larger than a broadcast hash join would want
# "spark.sql.autoBroadcastJoinThreshold", but one side has to be smaller
# than the number of splits * broadcast threshold and also be at least
# 3 times smaller than the other side. So it is not likely to happen
# unless we can give it some help.
_hash_join_conf = {'spark.sql.autoBroadcastJoinThreshold': '160',
'spark.sql.join.preferSortMergeJoin': 'false',
'spark.sql.shuffle.partitions': '2',
}
kudo_enabled_conf_key = "spark.rapids.shuffle.kudo.serializer.enabled"
def create_df(spark, data_gen, left_length, right_length, num_slices=None):
left = binary_op_df(spark, data_gen, length=left_length, num_slices=num_slices)
right = binary_op_df(spark, data_gen, length=right_length, num_slices=num_slices).withColumnRenamed("a", "r_a")\
.withColumnRenamed("b", "r_b")
return left, right
# create a dataframe with 2 columns where one is a nested type to be passed
# along but not used as key and the other can be used as join key
def create_ridealong_df(spark, key_data_gen, data_gen, left_length, right_length):
left = two_col_df(spark, key_data_gen, data_gen, length=left_length).withColumnRenamed("a", "key")
right = two_col_df(spark, key_data_gen, data_gen, length=right_length).withColumnRenamed("a", "r_key")\
.withColumnRenamed("b", "r_b")
return left, right
# Takes a sequence of list-of-generator and batch size string pairs and returns the
# test parameters, using the batch size setting for each corresponding data generator.
def join_batch_size_test_params(*args):
params = []
for (data_gens, batch_size) in args:
for obj in data_gens:
if isinstance(obj, ParameterSet):
params += [ pytest.param(v, batch_size, marks=obj.marks) for v in obj.values ]
else:
params += [ pytest.param(obj, batch_size) ]
return params
@ignore_order(local=True)
@pytest.mark.parametrize('join_type', ['Left', 'Inner', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("aqe_enabled", ["true", "false"], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
# https://github.com/NVIDIA/spark-rapids/issues/11100
@allow_non_gpu('EmptyRelationExec')
def test_right_broadcast_nested_loop_join_without_condition_empty(join_type, aqe_enabled, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, long_gen, 50, 0)
return left.join(broadcast(right), how=join_type)
assert_gpu_and_cpu_are_equal_collect(do_join, conf={
"spark.sql.adaptive.enabled": aqe_enabled,
kudo_enabled_conf_key: kudo_enabled
})
@ignore_order(local=True)
@pytest.mark.parametrize('join_type', ['Left', 'Inner', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("aqe_enabled", ["true", "false"], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
# https://github.com/NVIDIA/spark-rapids/issues/11100
@allow_non_gpu('EmptyRelationExec')
def test_left_broadcast_nested_loop_join_without_condition_empty(join_type, aqe_enabled, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, long_gen, 0, 50)
return left.join(broadcast(right), how=join_type)
assert_gpu_and_cpu_are_equal_collect(do_join, conf={
"spark.sql.adaptive.enabled": aqe_enabled,
kudo_enabled_conf_key: kudo_enabled
})
@ignore_order(local=True)
@pytest.mark.parametrize('join_type', ['Left', 'Inner', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("aqe_enabled", ["true", "false"], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
# https://github.com/NVIDIA/spark-rapids/issues/11100
@allow_non_gpu('EmptyRelationExec')
def test_broadcast_nested_loop_join_without_condition_empty(join_type, aqe_enabled, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, long_gen, 0, 0)
return left.join(broadcast(right), how=join_type)
assert_gpu_and_cpu_are_equal_collect(do_join, conf={
"spark.sql.adaptive.enabled": aqe_enabled,
kudo_enabled_conf_key: kudo_enabled
})
@ignore_order(local=True)
@pytest.mark.parametrize('join_type', ['Left', 'Inner', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
# https://github.com/NVIDIA/spark-rapids/issues/11100
@allow_non_gpu('EmptyRelationExec')
def test_right_broadcast_nested_loop_join_without_condition_empty_small_batch(join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, long_gen, 50, 0)
return left.join(broadcast(right), how=join_type)
assert_gpu_and_cpu_are_equal_collect(do_join, conf={
'spark.sql.adaptive.enabled': 'true',
kudo_enabled_conf_key: kudo_enabled
})
@ignore_order(local=True)
@pytest.mark.parametrize('join_type', ['Left', 'Right', 'Inner', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
# https://github.com/NVIDIA/spark-rapids/issues/11100
@allow_non_gpu('EmptyRelationExec')
def test_empty_broadcast_hash_join(join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, long_gen, 50, 0)
return left.join(right.hint("broadcast"), left.a == right.r_a, join_type)
assert_gpu_and_cpu_are_equal_collect(do_join, conf={
'spark.sql.adaptive.enabled': 'true',
kudo_enabled_conf_key: kudo_enabled
})
@pytest.mark.parametrize('join_type', ['Left', 'Inner', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_broadcast_hash_join_constant_keys(join_type, kudo_enabled):
def do_join(spark):
left = spark.range(10).withColumn("s", lit(1))
right = spark.range(10000).withColumn("r_s", lit(1))
return left.join(right.hint("broadcast"), left.s == right.r_s, join_type)
assert_gpu_and_cpu_row_counts_equal(do_join, conf={
'spark.sql.adaptive.enabled': 'true',
kudo_enabled_conf_key: kudo_enabled
})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen,batch_size', join_batch_size_test_params(
(all_gen, '1g'),
(join_small_batch_gens, '1000')), ids=idfn)
@pytest.mark.parametrize('join_type', all_join_types, ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_sortmerge_join(data_gen, join_type, batch_size, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 500, 500)
return left.join(right, left.a == right.r_a, join_type)
conf = copy_and_update(_sortmerge_join_conf, {
'spark.rapids.sql.batchSizeBytes': batch_size,
kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false' # disable AQE as it can change the join type
})
assert_gpu_and_cpu_are_equal_collect(do_join, conf=conf)
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', basic_nested_gens + [decimal_gen_128bit], ids=idfn)
@pytest.mark.parametrize('join_type', all_join_types, ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_sortmerge_join_ridealong(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_ridealong_df(spark, short_gen, data_gen, 500, 500)
return left.join(right, left.key == right.r_key, join_type)
conf = copy_and_update(_sortmerge_join_conf, {
kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false' # disable AQE as it can change the join type
})
assert_gpu_and_cpu_are_equal_collect(do_join, conf=conf)
# For floating point values the normalization is done using a higher order function. We could probably work around this
# for now it falls back to the CPU
@allow_non_gpu('SortMergeJoinExec', 'SortExec', 'ArrayTransform', 'LambdaFunction',
'NamedLambdaVariable', 'NormalizeNaNAndZero', 'ShuffleExchangeExec', 'HashPartitioning',
*non_utc_allow)
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', single_level_array_gens + [binary_gen], ids=idfn)
@pytest.mark.parametrize('join_type', all_join_types, ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_sortmerge_join_wrong_key_fallback(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 500, 500)
return left.join(right, left.a == right.r_a, join_type)
conf = copy_and_update(_sortmerge_join_conf, {
kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false' # disable AQE as it can change the join type
})
assert_gpu_fallback_collect(do_join, 'SortMergeJoinExec', conf=conf)
# For spark to insert a shuffled hash join it has to be enabled with
# "spark.sql.join.preferSortMergeJoin" = "false" and both sides have to
# be larger than a broadcast hash join would want
# "spark.sql.autoBroadcastJoinThreshold", but one side has to be smaller
# than the number of splits * broadcast threshold and also be at least
# 3 times smaller than the other side. So it is not likely to happen
# unless we can give it some help. Parameters are setup to try to make
# this happen, if test fails something might have changed related to that.
def hash_join_ridealong(data_gen, join_type, confs):
def do_join(spark):
left, right = create_ridealong_df(spark, short_gen, data_gen, 50, 500)
return left.join(right, left.key == right.r_key, join_type)
_all_conf = copy_and_update(_hash_join_conf, confs)
assert_gpu_and_cpu_are_equal_collect(do_join, conf=_all_conf)
@validate_execs_in_gpu_plan('GpuShuffledHashJoinExec')
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', basic_nested_gens + [decimal_gen_128bit], ids=idfn)
@pytest.mark.parametrize('join_type', all_non_sized_join_types, ids=idfn)
@pytest.mark.parametrize('sub_part_enabled', ['false', 'true'], ids=['SubPartition_OFF', 'SubPartition_ON'])
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_hash_join_ridealong_non_sized(data_gen, join_type, sub_part_enabled, kudo_enabled):
confs = {
"spark.rapids.sql.test.subPartitioning.enabled": sub_part_enabled,
kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false' # Disable AQE as it can change the query plan
}
hash_join_ridealong(data_gen, join_type, confs)
@validate_execs_in_gpu_plan('GpuShuffledSymmetricHashJoinExec')
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', basic_nested_gens + [decimal_gen_128bit], ids=idfn)
@pytest.mark.parametrize('join_type', all_symmetric_sized_join_types, ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_hash_join_ridealong_symmetric(data_gen, join_type, kudo_enabled):
confs = {
"spark.rapids.sql.join.useShuffledSymmetricHashJoin": "true",
kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false' # Disable AQE as it can change the query plan
}
hash_join_ridealong(data_gen, join_type, confs)
@validate_execs_in_gpu_plan('GpuShuffledAsymmetricHashJoinExec')
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', basic_nested_gens + [decimal_gen_128bit], ids=idfn)
@pytest.mark.parametrize('join_type', all_asymmetric_sized_join_types, ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_hash_join_ridealong_asymmetric(data_gen, join_type, kudo_enabled):
confs = {
"spark.rapids.sql.join.useShuffledAsymmetricHashJoin": "true",
kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false' # Disable AQE as it can change the query plan
}
hash_join_ridealong(data_gen, join_type, confs)
# test join side is build side for left/right join
# using SHUFFLE_HASH hint to specify the build side
def hash_join_side_is_build_side(data_gen, join_type, confs):
def do_join(spark):
left, right = create_ridealong_df(spark, short_gen, data_gen, 50, 500)
if (join_type == "LeftOuter"):
return left.hint("SHUFFLE_HASH").join(right, left.key == right.r_key, join_type)
elif (join_type == "RightOuter"):
return left.join(right.hint("SHUFFLE_HASH"), left.key == right.r_key, join_type)
else:
raise RuntimeError("Only supports left join and right join")
_all_conf = copy_and_update(_hash_join_conf, confs)
assert_gpu_and_cpu_are_equal_collect(do_join, conf=_all_conf)
# test left outer join with left side is build side
# test right outer join with right side is build side
@validate_execs_in_gpu_plan('GpuShuffledAsymmetricHashJoinExec')
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', basic_nested_gens + [decimal_gen_128bit], ids=idfn)
@pytest.mark.parametrize('join_type', all_asymmetric_sized_join_types, ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_hash_join_side_is_build_side_asymmetric(data_gen, join_type, kudo_enabled):
# Disable AQE temporarily until https://github.com/NVIDIA/spark-rapids/issues/14319 is resolved.
confs = {
"spark.rapids.sql.join.useShuffledAsymmetricHashJoin": "true",
kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false'
}
hash_join_side_is_build_side(data_gen, join_type, confs)
@ignore_order(local=True)
@pytest.mark.parametrize('join_type', all_asymmetric_sized_join_types, ids=idfn)
def test_hash_join_side_is_build_side_basic(join_type):
def _do_join(spark):
left = [
(1, ("Alice",)),
(2, ("Bob",)),
(3, None),
(4, (None,)),
]
right = [
(11, ("Alice",)),
(33, None),
(333, None),
(44, (None,)),
]
schema = StructType([
StructField("id", IntegerType()),
StructField("name", StructType([
StructField("value", StringType())]))])
left = spark.createDataFrame(left, schema)
right = spark.createDataFrame(right, schema)
if (join_type == "LeftOuter"):
return left.hint("SHUFFLE_HASH").join(right, "name", join_type).select(left.id, left.name, right.id, right.name)
elif (join_type == "RightOuter"):
return left.join(right.hint("SHUFFLE_HASH"), "name", join_type).select(left.id, left.name, right.id, right.name)
else:
raise RuntimeError("Only supports left join and right join")
# Disable AQE temporarily until https://github.com/NVIDIA/spark-rapids/issues/14319 is resolved.
assert_gpu_and_cpu_are_equal_collect(_do_join, conf={'spark.sql.adaptive.enabled': 'false'})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', all_gen, ids=idfn)
# Not all join types can be translated to a broadcast join, but this tests them to be sure we
# can handle what spark is doing
@pytest.mark.parametrize('join_type', all_join_types, ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_broadcast_join_right_table(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 500, 250)
return left.join(broadcast(right), left.a == right.r_a, join_type)
# Disable AQE temporarily until https://github.com/NVIDIA/spark-rapids/issues/14319 is resolved.
conf = {kudo_enabled_conf_key: kudo_enabled, 'spark.sql.adaptive.enabled': 'false'}
assert_gpu_and_cpu_are_equal_collect(do_join, conf = conf)
@ignore_order(local=True)
@pytest.mark.parametrize('rows', ['(1)', '(1), (null)', '()'], ids=['no_nulls', 'has_nulls', 'empty'])
def test_broadcast_join_null_aware_anti(rows):
sub_condition = ''
if rows == '()':
# 'VALUES()' is not supported by SQL, so leverage 'WHERE false' to produce
# an empty right table.
sub_condition = ' WHERE false'
rows = '(1)'
# Disable AQE temporarily until https://github.com/NVIDIA/spark-rapids/issues/14319 is resolved.
conf = {'spark.sql.adaptive.enabled': 'false',
'spark.sql.optimizeNullAwareAntiJoin': 'true'}
assert_cpu_and_gpu_are_equal_sql_with_capture(
lambda spark: two_col_df(spark, string_gen, int_gen, length=100),
sql="SELECT * FROM null_aware_anti_table WHERE b NOT IN ("
f"SELECT b FROM VALUES {rows} AS sub_right(b){sub_condition})",
table_name='null_aware_anti_table',
exist_classes='GpuBroadcastHashJoinExec',
conf=conf)
@ignore_order(local=True)
def test_broadcast_nested_loop_join_degen_left_outer_build_no_columns():
def gen_df_func(spark):
spark.sql("create or replace temp view right_tbl(r1) as values (22),(33);")
return unary_op_df(spark, int_gen, length=300)
# The sql is from https://github.com/NVIDIA/spark-rapids/issues/13731.
# The degenerate left-outer join (no columns in the build side) only appears
# from Spark 4.0.0, but ok to test against all the Spark versions.
assert_gpu_and_cpu_are_equal_sql(gen_df_func,
sql="SELECT * FROM left_tbl WHERE EXISTS "
"(SELECT COUNT(*) FROM right_tbl WHERE left_tbl.a = 1);",
table_name='left_tbl',
# Disable AQE temporarily until https://github.com/NVIDIA/spark-rapids/issues/14319 is resolved.
conf={'spark.sql.adaptive.enabled': 'false'})
@ignore_order(local=True)
@pytest.mark.skipif(is_before_spark_330() or is_databricks_runtime(),
reason="GPU does not support InSubqueryExec before 330 and on DBs")
@pytest.mark.parametrize('a_val', ['1', '10'], ids=idfn) # 1: in t1, 10: not in t1
def test_broadcast_nested_loop_join_degen_left_outer_stream_no_columns(a_val):
def degen_join_func(spark):
# This repro case is from https://github.com/NVIDIA/spark-rapids/issues/13708.
# And here does some change to cover more cases.
spark.sql(f"create or replace temp view t0 as select {a_val} as a;")
spark.sql("create or replace temp view t1(b) as values (1),(2);")
spark.sql("create or replace temp view t2(c) as values (22),(33),(44);")
return spark.sql("select a, cast(c as string) from t0 left join t2 on (a in (select b from t1));")
assert_gpu_and_cpu_are_equal_collect(degen_join_func)
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', basic_nested_gens + [decimal_gen_128bit], ids=idfn)
# Not all join types can be translated to a broadcast join, but this tests them to be sure we
# can handle what spark is doing
@pytest.mark.parametrize('join_type', all_join_types, ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_broadcast_join_right_table_ridealong(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_ridealong_df(spark, short_gen, data_gen, 500, 500)
return left.join(broadcast(right), left.key == right.r_key, join_type)
conf = {kudo_enabled_conf_key: kudo_enabled}
assert_gpu_and_cpu_are_equal_collect(do_join, conf = conf)
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', all_gen, ids=idfn)
# Not all join types can be translated to a broadcast join, but this tests them to be sure we
# can handle what spark is doing
@pytest.mark.parametrize('join_type', all_join_types, ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_broadcast_join_right_table_with_job_group(data_gen, join_type, kudo_enabled):
with_cpu_session(lambda spark : spark.sparkContext.setJobGroup("testjob1", "test", False))
def do_join(spark):
left, right = create_df(spark, data_gen, 500, 250)
return left.join(broadcast(right), left.a == right.r_a, join_type)
conf = {kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false' # disable AQE as it can change the join type
}
assert_gpu_and_cpu_are_equal_collect(do_join, conf = conf)
# because this infers the schema for CSV we need to allow some ops to be on the CPU
@allow_non_gpu("CollectLimitExec", "FileSourceScanExec", "DeserializeToObjectExec")
def test_empty_cross_side_with_limit(std_input_path):
def do_join(spark):
t0 = spark.read.csv(std_input_path + '/t0.csv', header=True, inferSchema=True)
t1 = spark.read.csv(std_input_path + '/t1.csv', header=True, inferSchema=True)
return t0.crossJoin(t1).limit(21)
assert_gpu_and_cpu_are_equal_collect(
do_join,
# Disable AQE temporarily until https://github.com/NVIDIA/spark-rapids/issues/14319 is resolved.
conf={'spark.sql.adaptive.enabled': 'false'})
@allow_non_gpu('CollectLimitExec')
def test_empty_right_outer_side_with_limit(std_input_path):
built_csv_path = std_input_path + '/t1.csv'
stream_csv_path = std_input_path + '/t0.csv'
def create_views(spark):
spark.read.csv(built_csv_path, header=True, inferSchema=True).createOrReplaceTempView("built_table")
spark.read.csv(stream_csv_path, header=True, inferSchema=True).createOrReplaceTempView("stream_table")
# create views first on CPU
with_cpu_session(lambda spark: create_views(spark))
# limit to 10 rows to produce `LocalLimitExec` node
def do_join(spark):
return spark.sql("""
SELECT '1', CAST(CAST(stream_table.c0 AS int) as string)
FROM built_table
RIGHT OUTER JOIN stream_table
ON TRUE limit 10
""")
assert_gpu_and_cpu_are_equal_collect(do_join)
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.order(1) # at the head of xdist worker queue if pytest-order is installed
@pytest.mark.parametrize('data_gen,batch_size', join_batch_size_test_params(
(all_gen + basic_nested_gens, '1g'),
(join_small_batch_gens + [basic_struct_gen, ArrayGen(string_gen)], '100')), ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_cartesian_join(data_gen, batch_size, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
return left.crossJoin(right)
assert_gpu_and_cpu_are_equal_collect(do_join, conf={
'spark.rapids.sql.batchSizeBytes': batch_size,
kudo_enabled_conf_key: kudo_enabled
})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.order(1) # at the head of xdist worker queue if pytest-order is installed
@pytest.mark.xfail(condition=is_databricks_runtime(),
reason='https://github.com/NVIDIA/spark-rapids/issues/334')
@pytest.mark.parametrize('batch_size', ['100', '1g'], ids=idfn) # set the batch size so we can test multiple stream batches
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_cartesian_join_special_case_count(batch_size, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, int_gen, 50, 25)
return left.crossJoin(right).selectExpr('COUNT(*)')
assert_gpu_and_cpu_are_equal_collect(do_join, conf={
'spark.rapids.sql.batchSizeBytes': batch_size,
kudo_enabled_conf_key: kudo_enabled
})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.order(1) # at the head of xdist worker queue if pytest-order is installed
@pytest.mark.xfail(condition=is_databricks_runtime(),
reason='https://github.com/NVIDIA/spark-rapids/issues/334')
@pytest.mark.parametrize('batch_size', ['1000', '1g'], ids=idfn) # set the batch size so we can test multiple stream batches
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_cartesian_join_special_case_group_by_count(batch_size, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, int_gen, 50, 25)
return left.crossJoin(right).groupBy('a').count()
assert_gpu_and_cpu_are_equal_collect(do_join, conf={
'spark.rapids.sql.batchSizeBytes': batch_size,
kudo_enabled_conf_key: kudo_enabled
})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.order(1) # at the head of xdist worker queue if pytest-order is installed
@pytest.mark.parametrize('data_gen,batch_size', join_batch_size_test_params(
(all_gen, '1g'),
(join_small_batch_gens, '100')), ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_cartesian_join_with_condition(data_gen, batch_size, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
# This test is impacted by https://github.com/NVIDIA/spark-rapids/issues/294
# if the sizes are large enough to have both 0.0 and -0.0 show up 500 and 250
# but these take a long time to verify so we run with smaller numbers by default
# that do not expose the error
return left.join(right, left.b >= right.r_b, "cross")
conf = copy_and_update(_sortmerge_join_conf, {
'spark.rapids.sql.batchSizeBytes': batch_size,
kudo_enabled_conf_key: kudo_enabled
})
assert_gpu_and_cpu_are_equal_collect(do_join, conf=conf)
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen,batch_size', join_batch_size_test_params(
(all_gen + basic_nested_gens, '1g'),
(join_small_batch_gens, '100')), ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_broadcast_nested_loop_join(data_gen, batch_size, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
return left.crossJoin(broadcast(right))
assert_gpu_and_cpu_are_equal_collect(do_join, conf={
'spark.rapids.sql.batchSizeBytes': batch_size,
kudo_enabled_conf_key: kudo_enabled
})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.parametrize('batch_size', ['100', '1g'], ids=idfn) # set the batch size so we can test multiple stream batches
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_broadcast_nested_loop_join_special_case_count(batch_size, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, int_gen, 50, 25)
return left.crossJoin(broadcast(right)).selectExpr('COUNT(*)')
assert_gpu_and_cpu_are_equal_collect(do_join, conf={
'spark.rapids.sql.batchSizeBytes': batch_size,
kudo_enabled_conf_key: kudo_enabled
})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.xfail(condition=is_databricks_runtime(),
reason='https://github.com/NVIDIA/spark-rapids/issues/334')
@pytest.mark.parametrize('batch_size', ['1000', '1g'], ids=idfn) # set the batch size so we can test multiple stream batches
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_broadcast_nested_loop_join_special_case_group_by_count(batch_size, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, int_gen, 50, 25)
return left.crossJoin(broadcast(right)).groupBy('a').count()
assert_gpu_and_cpu_are_equal_collect(do_join, conf={
'spark.rapids.sql.batchSizeBytes': batch_size,
kudo_enabled_conf_key: kudo_enabled
})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen,batch_size', join_batch_size_test_params(
(join_ast_gen, '1g'),
([int_gen], 100)), ids=idfn)
@pytest.mark.parametrize('join_type', ['Left', 'Inner', 'LeftSemi', 'LeftAnti', 'Cross'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_right_broadcast_nested_loop_join_with_ast_condition(data_gen, join_type, batch_size, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
# This test is impacted by https://github.com/NVIDIA/spark-rapids/issues/294
# if the sizes are large enough to have both 0.0 and -0.0 show up 500 and 250
# but these take a long time to verify so we run with smaller numbers by default
# that do not expose the error
return left.join(broadcast(right), (left.b >= right.r_b), join_type)
assert_gpu_and_cpu_are_equal_collect(do_join, conf={
'spark.rapids.sql.batchSizeBytes': batch_size,
kudo_enabled_conf_key: kudo_enabled
})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', join_ast_gen, ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_left_broadcast_nested_loop_join_with_ast_condition(data_gen, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
# This test is impacted by https://github.com/NVIDIA/spark-rapids/issues/294
# if the sizes are large enough to have both 0.0 and -0.0 show up 500 and 250
# but these take a long time to verify so we run with smaller numbers by default
# that do not expose the error
return broadcast(left).join(right, (left.b >= right.r_b), 'Right')
assert_gpu_and_cpu_are_equal_collect(do_join, conf = {kudo_enabled_conf_key: kudo_enabled})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', [IntegerGen(), LongGen(), pytest.param(FloatGen(), marks=[incompat]), pytest.param(DoubleGen(), marks=[incompat])], ids=idfn)
@pytest.mark.parametrize('join_type', ['Inner', 'Cross'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_broadcast_nested_loop_join_with_condition_post_filter(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
# This test is impacted by https://github.com/NVIDIA/spark-rapids/issues/294
# if the sizes are large enough to have both 0.0 and -0.0 show up 500 and 250
# but these take a long time to verify so we run with smaller numbers by default
# that do not expose the error
# AST does not support cast or logarithm yet, so this must be implemented as a post-filter
return left.join(broadcast(right), left.a > f.log(right.r_a), join_type)
assert_gpu_and_cpu_are_equal_collect(do_join, conf = {kudo_enabled_conf_key: kudo_enabled})
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', [IntegerGen(), LongGen(), pytest.param(FloatGen(), marks=[incompat]), pytest.param(DoubleGen(), marks=[incompat])], ids=idfn)
@pytest.mark.parametrize('join_type', ['Cross', 'Left', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
# https://github.com/NVIDIA/spark-rapids/issues/12700
@disable_ansi_mode
def test_broadcast_nested_loop_join_with_condition(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
# AST does not support cast or logarithm yet which is supposed to be extracted into child
# nodes. And this test doesn't cover other join types due to:
# (1) build right are not supported for Right
# (2) FullOuter: currently is not supported
# Those fallback reasons are not due to AST. Additionally, this test case changes test_broadcast_nested_loop_join_with_condition_fallback:
# (1) adapt double to integer since AST current doesn't support it.
# (2) switch to right side build to pass checks of 'Left', 'LeftSemi', 'LeftAnti' join types
return left.join(broadcast(right), f.round(left.a).cast('integer') > f.round(f.log(right.r_a).cast('integer')), join_type)
assert_gpu_and_cpu_are_equal_collect(do_join, conf={
"spark.rapids.sql.castFloatToIntegralTypes.enabled": True,
kudo_enabled_conf_key: kudo_enabled
})
@allow_non_gpu('BroadcastExchangeExec', 'BroadcastNestedLoopJoinExec', 'Cast', 'GreaterThan', 'Log')
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', [IntegerGen(), LongGen(), pytest.param(FloatGen(), marks=[incompat]), pytest.param(DoubleGen(), marks=[incompat])], ids=idfn)
@pytest.mark.parametrize('join_type', ['Left', 'Right', 'FullOuter', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_broadcast_nested_loop_join_with_condition_fallback(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
# AST does not support double type which is not split-able into child nodes.
return broadcast(left).join(right, left.a > f.log(right.r_a), join_type)
assert_gpu_fallback_collect(do_join, 'BroadcastNestedLoopJoinExec',
conf = {kudo_enabled_conf_key: kudo_enabled,
# disable AQE as it can change the join type
"spark.sql.adaptive.enabled": "false"})
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', [byte_gen, short_gen, int_gen, long_gen,
float_gen, double_gen,
string_gen, boolean_gen, date_gen, timestamp_gen], ids=idfn)
@pytest.mark.parametrize('join_type', ['Left', 'Right', 'FullOuter', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_broadcast_nested_loop_join_with_array_contains(data_gen, join_type, kudo_enabled):
arr_gen = ArrayGen(data_gen)
literal = with_cpu_session(lambda spark: gen_scalar(data_gen))
def do_join(spark):
left, right = create_df(spark, arr_gen, 50, 25)
# Array_contains will be pushed down into project child nodes
return broadcast(left).join(right, array_contains(left.a, literal.cast(data_gen.data_type)) < array_contains(right.r_a, literal.cast(data_gen.data_type)))
assert_gpu_and_cpu_are_equal_collect(do_join, conf = {kudo_enabled_conf_key: kudo_enabled})
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', all_gen, ids=idfn)
@pytest.mark.parametrize('join_type', ['Left', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_right_broadcast_nested_loop_join_condition_missing(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
# This test is impacted by https://github.com/NVIDIA/spark-rapids/issues/294
# if the sizes are large enough to have both 0.0 and -0.0 show up 500 and 250
# but these take a long time to verify so we run with smaller numbers by default
# that do not expose the error
# Compute the distinct of the join result to verify the join produces a proper dataframe
# for downstream processing.
return left.join(broadcast(right), how=join_type).distinct()
assert_gpu_and_cpu_are_equal_collect(
do_join,
conf = {kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false' # disable AQE as it can change the join type
})
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', all_gen, ids=idfn)
@pytest.mark.parametrize('join_type', ['Right'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_left_broadcast_nested_loop_join_condition_missing(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
# This test is impacted by https://github.com/NVIDIA/spark-rapids/issues/294
# if the sizes are large enough to have both 0.0 and -0.0 show up 500 and 250
# but these take a long time to verify so we run with smaller numbers by default
# that do not expose the error
# Compute the distinct of the join result to verify the join produces a proper dataframe
# for downstream processing.
return broadcast(left).join(right, how=join_type).distinct()
assert_gpu_and_cpu_are_equal_collect(do_join, conf = {kudo_enabled_conf_key: kudo_enabled})
@pytest.mark.parametrize('data_gen', all_gen + single_level_array_gens + [binary_gen], ids=idfn)
@pytest.mark.parametrize('join_type', ['Left', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_right_broadcast_nested_loop_join_condition_missing_count(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
return left.join(broadcast(right), how=join_type).selectExpr('COUNT(*)')
assert_gpu_and_cpu_are_equal_collect(do_join, conf = {kudo_enabled_conf_key: kudo_enabled})
@pytest.mark.parametrize('data_gen', all_gen + single_level_array_gens + [binary_gen], ids=idfn)
@pytest.mark.parametrize('join_type', ['Right'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_left_broadcast_nested_loop_join_condition_missing_count(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
return broadcast(left).join(right, how=join_type).selectExpr('COUNT(*)')
assert_gpu_and_cpu_are_equal_collect(do_join, conf = {kudo_enabled_conf_key: kudo_enabled})
@allow_non_gpu('BroadcastExchangeExec', 'BroadcastNestedLoopJoinExec', 'GreaterThanOrEqual', *non_utc_allow)
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', all_gen, ids=idfn)
@pytest.mark.parametrize('join_type', ['LeftOuter', 'LeftSemi', 'LeftAnti', 'FullOuter'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_broadcast_nested_loop_join_with_conditionals_build_left_fallback(data_gen, join_type,
kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
return broadcast(left).join(right, (left.b >= right.r_b), join_type)
assert_gpu_fallback_collect(do_join, 'BroadcastNestedLoopJoinExec',
conf = {kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false' # disable AQE as it can change the join type
})
@allow_non_gpu('BroadcastExchangeExec', 'BroadcastNestedLoopJoinExec', 'GreaterThanOrEqual', *non_utc_allow)
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', all_gen, ids=idfn)
@pytest.mark.parametrize('join_type', ['RightOuter', 'FullOuter'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_broadcast_nested_loop_with_conditionals_build_right_fallback(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
return left.join(broadcast(right), (left.b >= right.r_b), join_type)
assert_gpu_fallback_collect(do_join, 'BroadcastNestedLoopJoinExec',
conf = {kudo_enabled_conf_key: kudo_enabled})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', all_gen, ids=idfn)
# Not all join types can be translated to a broadcast join, but this tests them to be sure we
# can handle what spark is doing
@pytest.mark.parametrize('join_type', all_join_types, ids=idfn)
# Specify 200 shuffle partitions to test cases where streaming side is empty
# as in https://github.com/NVIDIA/spark-rapids/issues/7516
@pytest.mark.parametrize('shuffle_conf', [{}, {'spark.sql.shuffle.partitions': 200}], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
@allow_non_gpu(*non_utc_allow)
def test_broadcast_join_left_table(data_gen, join_type, shuffle_conf, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 250, 500)
return broadcast(left).join(right, left.a == right.r_a, join_type)
# Disable AQE temporarily until https://github.com/NVIDIA/spark-rapids/issues/14319 is resolved.
conf = copy_and_update(shuffle_conf, {kudo_enabled_conf_key: kudo_enabled, 'spark.sql.adaptive.enabled': 'false'})
assert_gpu_and_cpu_are_equal_collect(do_join, conf=conf)
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', join_ast_gen, ids=idfn)
@pytest.mark.parametrize('join_type', all_join_types, ids=idfn)
@pytest.mark.parametrize("kudo_enabled", [True, False], ids=["KUDO_ON", "KUDO_OFF"])
@allow_non_gpu(*non_utc_allow)
def test_broadcast_join_with_conditionals(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 500, 250)
return left.join(broadcast(right),
(left.a == right.r_a) & (left.b >= right.r_b), join_type)
# Disable AQE temporarily until https://github.com/NVIDIA/spark-rapids/issues/14319 is resolved.
assert_gpu_and_cpu_are_equal_collect(do_join, conf = {kudo_enabled_conf_key: kudo_enabled, 'spark.sql.adaptive.enabled': 'false'})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@allow_non_gpu('BroadcastExchangeExec', 'BroadcastHashJoinExec', 'Cast', 'GreaterThan', 'Log', 'SortMergeJoinExec')
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', [long_gen], ids=idfn)
@pytest.mark.parametrize('join_type', ['Left', 'Right', 'FullOuter', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_broadcast_join_with_condition_ast_op_fallback(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
# AST does not support cast or logarithm yet
return left.join(broadcast(right),
(left.a == right.r_a) & (left.b > f.log(right.r_b)), join_type)
exec = 'SortMergeJoinExec' if join_type in ['Right', 'FullOuter'] else 'BroadcastHashJoinExec'
# Disable AQE as it changes the query plan
assert_gpu_fallback_collect(do_join, exec, conf = {kudo_enabled_conf_key: kudo_enabled, 'spark.sql.adaptive.enabled': 'false'})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@allow_non_gpu('BroadcastExchangeExec', 'BroadcastHashJoinExec', 'Cast', 'GreaterThan', 'SortMergeJoinExec')
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', join_no_ast_gen, ids=idfn)
@pytest.mark.parametrize('join_type', ['Left', 'Right', 'FullOuter', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_broadcast_join_with_condition_ast_type_fallback(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
# AST does not support cast or logarithm yet
return left.join(broadcast(right),
(left.a == right.r_a) & (left.b > right.r_b), join_type)
exec = 'SortMergeJoinExec' if join_type in ['Right', 'FullOuter'] else 'BroadcastHashJoinExec'
# Disable AQE as it changes the query plan
assert_gpu_fallback_collect(do_join, exec, conf = {kudo_enabled_conf_key: kudo_enabled, 'spark.sql.adaptive.enabled': 'false'})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', join_no_ast_gen, ids=idfn)
@pytest.mark.parametrize('join_type', ['Inner', 'Cross'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_broadcast_join_with_condition_post_filter(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 500, 250)
return left.join(broadcast(right),
(left.a == right.r_a) & (left.b > right.r_b), join_type)
assert_gpu_and_cpu_are_equal_collect(
do_join,
conf = {kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false' # disable AQE as it can change the join type
})
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', join_ast_gen, ids=idfn)
@pytest.mark.parametrize('join_type', ['Left', 'Right', 'Inner', 'FullOuter', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", [True, False], ids=["KUDO_ON", "KUDO_OFF"])
@allow_non_gpu(*non_utc_allow)
def test_sortmerge_join_with_condition_ast(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 500, 250)
return left.join(right, (left.a == right.r_a) & (left.b >= right.r_b), join_type)
conf = copy_and_update(_sortmerge_join_conf, {
kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false' # disable AQE as it can change the join type
})
assert_gpu_and_cpu_are_equal_collect(do_join, conf=conf)
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@allow_non_gpu('GreaterThan', 'Log', 'ShuffleExchangeExec', 'SortMergeJoinExec')
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', [long_gen], ids=idfn)
@pytest.mark.parametrize('join_type', ['Left', 'Right', 'FullOuter', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_sortmerge_join_with_condition_ast_op_fallback(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 500, 250)
# AST does not support cast or logarithm yet
return left.join(right, (left.a == right.r_a) & (left.b > f.log(right.r_b)), join_type)
conf = copy_and_update(_sortmerge_join_conf, {
kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false' # disable AQE as it can change the join type
})
assert_gpu_fallback_collect(do_join, 'SortMergeJoinExec', conf=conf)
# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@allow_non_gpu('GreaterThan', 'ShuffleExchangeExec', 'SortMergeJoinExec')
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', join_no_ast_gen, ids=idfn)
@pytest.mark.parametrize('join_type', ['Left', 'Right', 'FullOuter', 'LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_sortmerge_join_with_condition_ast_type_fallback(data_gen, join_type, kudo_enabled):
def do_join(spark):
left, right = create_df(spark, data_gen, 500, 250)
return left.join(right, (left.a == right.r_a) & (left.b > right.r_b), join_type)
conf = copy_and_update(_sortmerge_join_conf, {
kudo_enabled_conf_key: kudo_enabled,
'spark.sql.adaptive.enabled': 'false' # disable AQE as it can change the join type
})
assert_gpu_fallback_collect(do_join, 'SortMergeJoinExec', conf=conf)
_mixed_df1_with_nulls = [('a', RepeatSeqGen(LongGen(nullable=(True, 20.0)), length= 10)),
('b', IntegerGen()), ('c', LongGen())]
_mixed_df2_with_nulls = [('a', RepeatSeqGen(LongGen(nullable=(True, 20.0)), length= 10)),
('b', StringGen()), ('c', BooleanGen())]
@ignore_order
@pytest.mark.parametrize('join_type', ['Left', 'Right', 'Inner', 'LeftSemi', 'LeftAnti', 'FullOuter', 'Cross'], ids=idfn)
@pytest.mark.parametrize("kudo_enabled", ["true", "false"], ids=idfn)
def test_broadcast_join_mixed(join_type, kudo_enabled):
def do_join(spark):
left = gen_df(spark, _mixed_df1_with_nulls, length=500)
right = gen_df(spark, _mixed_df2_with_nulls, length=500).withColumnRenamed("a", "r_a")\
.withColumnRenamed("b", "r_b").withColumnRenamed("c", "r_c")
return left.join(broadcast(right), left.a.eqNullSafe(right.r_a), join_type)
# Disable AQE temporarily until https://github.com/NVIDIA/spark-rapids/issues/14319 is resolved.
assert_gpu_and_cpu_are_equal_collect(do_join, conf={kudo_enabled_conf_key: kudo_enabled, 'spark.sql.adaptive.enabled': 'false'})