/
JoinHintSuite.scala
573 lines (518 loc) · 21.7 KB
/
JoinHintSuite.scala
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.spark.sql
import org.apache.log4j.Level
import org.apache.spark.sql.catalyst.optimizer.{BuildLeft, BuildRight, BuildSide, EliminateResolvedHint}
import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.rules.RuleExecutor
import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper
import org.apache.spark.sql.execution.joins._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.test.SharedSparkSession
class JoinHintSuite extends PlanTest with SharedSparkSession with AdaptiveSparkPlanHelper {
import testImplicits._
lazy val df = spark.range(10)
lazy val df1 = df.selectExpr("id as a1", "id as a2")
lazy val df2 = df.selectExpr("id as b1", "id as b2")
lazy val df3 = df.selectExpr("id as c1", "id as c2")
def msgNoHintRelationFound(relation: String, hint: String): String =
s"Count not find relation '$relation' specified in hint '$hint'."
def msgNoJoinForJoinHint(strategy: String): String =
s"A join hint (strategy=$strategy) is specified but it is not part of a join relation."
def msgJoinHintOverridden(strategy: String): String =
s"Hint (strategy=$strategy) is overridden by another hint and will not take effect."
def verifyJoinHintWithWarnings(
df: => DataFrame,
expectedHints: Seq[JoinHint],
warnings: Seq[String]): Unit = {
val logAppender = new LogAppender("join hints")
withLogAppender(logAppender) {
verifyJoinHint(df, expectedHints)
}
val warningMessages = logAppender.loggingEvents
.filter(_.getLevel == Level.WARN)
.map(_.getRenderedMessage)
.filter(_.contains("hint"))
assert(warningMessages.size == warnings.size)
warnings.foreach { w =>
assert(warningMessages.contains(w))
}
}
def verifyJoinHint(df: DataFrame, expectedHints: Seq[JoinHint]): Unit = {
val optimized = df.queryExecution.optimizedPlan
val joinHints = optimized collect {
case Join(_, _, _, _, hint) => hint
case _: ResolvedHint => fail("ResolvedHint should not appear after optimize.")
}
assert(joinHints == expectedHints)
}
test("single join") {
verifyJoinHint(
df.hint("broadcast").join(df, "id"),
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
None) :: Nil
)
verifyJoinHint(
df.join(df.hint("broadcast"), "id"),
JoinHint(
None,
Some(HintInfo(strategy = Some(BROADCAST)))) :: Nil
)
}
test("multiple joins") {
verifyJoinHint(
df1.join(df2.hint("broadcast").join(df3, $"b1" === $"c1").hint("broadcast"), $"a1" === $"c1"),
JoinHint(
None,
Some(HintInfo(strategy = Some(BROADCAST)))) ::
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
None) :: Nil
)
verifyJoinHint(
df1.hint("broadcast").join(df2, $"a1" === $"b1").hint("broadcast").join(df3, $"a1" === $"c1"),
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
None) ::
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
None) :: Nil
)
}
test("hint scope") {
withTempView("a", "b", "c") {
df1.createOrReplaceTempView("a")
df2.createOrReplaceTempView("b")
verifyJoinHint(
sql(
"""
|select /*+ broadcast(a, b)*/ * from (
| select /*+ broadcast(b)*/ * from a join b on a.a1 = b.b1
|) a join (
| select /*+ broadcast(a)*/ * from a join b on a.a1 = b.b1
|) b on a.a1 = b.b1
""".stripMargin),
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
Some(HintInfo(strategy = Some(BROADCAST)))) ::
JoinHint(
None,
Some(HintInfo(strategy = Some(BROADCAST)))) ::
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
None) :: Nil
)
}
}
test("hints prevent join reorder") {
withSQLConf(SQLConf.CBO_ENABLED.key -> "true", SQLConf.JOIN_REORDER_ENABLED.key -> "true") {
withTempView("a", "b", "c") {
df1.createOrReplaceTempView("a")
df2.createOrReplaceTempView("b")
df3.createOrReplaceTempView("c")
verifyJoinHint(
sql("select /*+ broadcast(a, c)*/ * from a, b, c " +
"where a.a1 = b.b1 and b.b1 = c.c1"),
JoinHint(
None,
Some(HintInfo(strategy = Some(BROADCAST)))) ::
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
None) :: Nil
)
verifyJoinHint(
sql("select /*+ broadcast(a, c)*/ * from a, c, b " +
"where a.a1 = b.b1 and b.b1 = c.c1"),
JoinHint.NONE ::
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
Some(HintInfo(strategy = Some(BROADCAST)))) :: Nil
)
verifyJoinHint(
sql("select /*+ broadcast(b, c)*/ * from a, c, b " +
"where a.a1 = b.b1 and b.b1 = c.c1"),
JoinHint(
None,
Some(HintInfo(strategy = Some(BROADCAST)))) ::
JoinHint(
None,
Some(HintInfo(strategy = Some(BROADCAST)))) :: Nil
)
verifyJoinHint(
df1.join(df2, $"a1" === $"b1" && $"a1" > 5).hint("broadcast")
.join(df3, $"b1" === $"c1" && $"a1" < 10),
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
None) ::
JoinHint.NONE :: Nil
)
verifyJoinHint(
df1.join(df2, $"a1" === $"b1" && $"a1" > 5).hint("broadcast")
.join(df3, $"b1" === $"c1" && $"a1" < 10)
.join(df, $"b1" === $"id"),
JoinHint.NONE ::
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
None) ::
JoinHint.NONE :: Nil
)
}
}
}
test("intersect/except") {
val dfSub = spark.range(2)
verifyJoinHint(
df.hint("broadcast").except(dfSub).join(df, "id"),
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
None) ::
JoinHint.NONE :: Nil
)
verifyJoinHint(
df.join(df.hint("broadcast").intersect(dfSub), "id"),
JoinHint(
None,
Some(HintInfo(strategy = Some(BROADCAST)))) ::
JoinHint.NONE :: Nil
)
}
test("hint merge") {
verifyJoinHintWithWarnings(
df.hint("broadcast").filter($"id" > 2).hint("broadcast").join(df, "id"),
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
None) :: Nil,
Nil
)
verifyJoinHintWithWarnings(
df.join(df.hint("broadcast").limit(2).hint("broadcast"), "id"),
JoinHint(
None,
Some(HintInfo(strategy = Some(BROADCAST)))) :: Nil,
Nil
)
verifyJoinHintWithWarnings(
df.hint("merge").filter($"id" > 2).hint("shuffle_hash").join(df, "id").hint("broadcast"),
JoinHint(
Some(HintInfo(strategy = Some(SHUFFLE_HASH))),
None) :: Nil,
msgJoinHintOverridden("merge") ::
msgNoJoinForJoinHint("broadcast") :: Nil
)
verifyJoinHintWithWarnings(
df.join(df.hint("broadcast").limit(2).hint("merge"), "id")
.hint("shuffle_hash")
.hint("shuffle_replicate_nl")
.join(df, "id"),
JoinHint(
Some(HintInfo(strategy = Some(SHUFFLE_REPLICATE_NL))),
None) ::
JoinHint(
None,
Some(HintInfo(strategy = Some(SHUFFLE_MERGE)))) :: Nil,
msgJoinHintOverridden("broadcast") ::
msgJoinHintOverridden("shuffle_hash") :: Nil
)
}
test("hint merge - SQL") {
withTempView("a", "b", "c") {
df1.createOrReplaceTempView("a")
df2.createOrReplaceTempView("b")
df3.createOrReplaceTempView("c")
verifyJoinHintWithWarnings(
sql("select /*+ shuffle_hash merge(a, c) broadcast(a, b)*/ * from a, b, c " +
"where a.a1 = b.b1 and b.b1 = c.c1"),
JoinHint(
None,
Some(HintInfo(strategy = Some(SHUFFLE_MERGE)))) ::
JoinHint(
Some(HintInfo(strategy = Some(SHUFFLE_MERGE))),
Some(HintInfo(strategy = Some(BROADCAST)))) :: Nil,
msgNoJoinForJoinHint("shuffle_hash") ::
msgJoinHintOverridden("broadcast") :: Nil
)
verifyJoinHintWithWarnings(
sql("select /*+ shuffle_hash(a, b) merge(b, d) broadcast(b)*/ * from a, b, c " +
"where a.a1 = b.b1 and b.b1 = c.c1"),
JoinHint.NONE ::
JoinHint(
Some(HintInfo(strategy = Some(SHUFFLE_HASH))),
Some(HintInfo(strategy = Some(SHUFFLE_HASH)))) :: Nil,
msgNoHintRelationFound("d", "merge(b, d)") ::
msgJoinHintOverridden("broadcast") ::
msgJoinHintOverridden("merge") :: Nil
)
verifyJoinHintWithWarnings(
sql(
"""
|select /*+ broadcast(a, c) merge(a, d)*/ * from a
|join (
| select /*+ shuffle_hash(c) shuffle_replicate_nl(b, c)*/ * from b
| join c on b.b1 = c.c1
|) as d
|on a.a2 = d.b2
""".stripMargin),
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
Some(HintInfo(strategy = Some(SHUFFLE_MERGE)))) ::
JoinHint(
Some(HintInfo(strategy = Some(SHUFFLE_REPLICATE_NL))),
Some(HintInfo(strategy = Some(SHUFFLE_HASH)))) :: Nil,
msgNoHintRelationFound("c", "broadcast(a, c)") ::
msgJoinHintOverridden("merge") ::
msgJoinHintOverridden("shuffle_replicate_nl") :: Nil
)
}
}
test("nested hint") {
verifyJoinHint(
df.hint("broadcast").hint("broadcast").filter($"id" > 2).join(df, "id"),
JoinHint(
Some(HintInfo(strategy = Some(BROADCAST))),
None) :: Nil
)
verifyJoinHint(
df.hint("shuffle_hash").hint("broadcast").hint("merge").filter($"id" > 2).join(df, "id"),
JoinHint(
Some(HintInfo(strategy = Some(SHUFFLE_MERGE))),
None) :: Nil
)
}
test("hints prevent cost-based join reorder") {
withSQLConf(SQLConf.CBO_ENABLED.key -> "true", SQLConf.JOIN_REORDER_ENABLED.key -> "true") {
val join = df.join(df, "id")
val broadcasted = join.hint("broadcast")
verifyJoinHint(
join.join(broadcasted, "id").join(broadcasted, "id"),
JoinHint(
None,
Some(HintInfo(strategy = Some(BROADCAST)))) ::
JoinHint(
None,
Some(HintInfo(strategy = Some(BROADCAST)))) ::
JoinHint.NONE :: JoinHint.NONE :: JoinHint.NONE :: Nil
)
}
}
def equiJoinQueryWithHint(hints: Seq[String], joinType: String = "INNER"): String =
hints.map("/*+ " + _ + " */").mkString(
"SELECT ", " ", s" * FROM t1 $joinType JOIN t2 ON t1.key = t2.key")
def nonEquiJoinQueryWithHint(hints: Seq[String], joinType: String = "INNER"): String =
hints.map("/*+ " + _ + " */").mkString(
"SELECT ", " ", s" * FROM t1 $joinType JOIN t2 ON t1.key > t2.key")
private def assertBroadcastHashJoin(df: DataFrame, buildSide: BuildSide): Unit = {
val executedPlan = df.queryExecution.executedPlan
val broadcastHashJoins = collect(executedPlan) {
case b: BroadcastHashJoinExec => b
}
assert(broadcastHashJoins.size == 1)
assert(broadcastHashJoins.head.buildSide == buildSide)
}
private def assertBroadcastNLJoin(df: DataFrame, buildSide: BuildSide): Unit = {
val executedPlan = df.queryExecution.executedPlan
val broadcastNLJoins = collect(executedPlan) {
case b: BroadcastNestedLoopJoinExec => b
}
assert(broadcastNLJoins.size == 1)
assert(broadcastNLJoins.head.buildSide == buildSide)
}
private def assertShuffleHashJoin(df: DataFrame, buildSide: BuildSide): Unit = {
val executedPlan = df.queryExecution.executedPlan
val shuffleHashJoins = collect(executedPlan) {
case s: ShuffledHashJoinExec => s
}
assert(shuffleHashJoins.size == 1)
assert(shuffleHashJoins.head.buildSide == buildSide)
}
private def assertShuffleMergeJoin(df: DataFrame): Unit = {
val executedPlan = df.queryExecution.executedPlan
val shuffleMergeJoins = collect(executedPlan) {
case s: SortMergeJoinExec => s
}
assert(shuffleMergeJoins.size == 1)
}
private def assertShuffleReplicateNLJoin(df: DataFrame): Unit = {
val executedPlan = df.queryExecution.executedPlan
val shuffleReplicateNLJoins = collect(executedPlan) {
case c: CartesianProductExec => c
}
assert(shuffleReplicateNLJoins.size == 1)
}
test("join strategy hint - broadcast") {
withTempView("t1", "t2") {
Seq((1, "4"), (2, "2")).toDF("key", "value").createTempView("t1")
Seq((1, "1"), (2, "12.3"), (2, "123")).toDF("key", "value").createTempView("t2")
val t1Size = spark.table("t1").queryExecution.analyzed.children.head.stats.sizeInBytes
val t2Size = spark.table("t2").queryExecution.analyzed.children.head.stats.sizeInBytes
assert(t1Size < t2Size)
withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
// Broadcast hint specified on one side
assertBroadcastHashJoin(
sql(equiJoinQueryWithHint("BROADCAST(t1)" :: Nil)), BuildLeft)
assertBroadcastNLJoin(
sql(nonEquiJoinQueryWithHint("BROADCAST(t2)" :: Nil)), BuildRight)
// Determine build side based on the join type and child relation sizes
assertBroadcastHashJoin(
sql(equiJoinQueryWithHint("BROADCAST(t1, t2)" :: Nil)), BuildLeft)
assertBroadcastNLJoin(
sql(nonEquiJoinQueryWithHint("BROADCAST(t1, t2)" :: Nil, "left")), BuildRight)
assertBroadcastNLJoin(
sql(nonEquiJoinQueryWithHint("BROADCAST(t1, t2)" :: Nil, "right")), BuildLeft)
// Use broadcast-hash join if hinted "broadcast" and equi-join
assertBroadcastHashJoin(
sql(equiJoinQueryWithHint("BROADCAST(t2)" :: "SHUFFLE_HASH(t1)" :: Nil)), BuildRight)
assertBroadcastHashJoin(
sql(equiJoinQueryWithHint("BROADCAST(t1)" :: "MERGE(t1, t2)" :: Nil)), BuildLeft)
assertBroadcastHashJoin(
sql(equiJoinQueryWithHint("BROADCAST(t1)" :: "SHUFFLE_REPLICATE_NL(t2)" :: Nil)),
BuildLeft)
// Use broadcast-nl join if hinted "broadcast" and non-equi-join
assertBroadcastNLJoin(
sql(nonEquiJoinQueryWithHint("SHUFFLE_HASH(t2)" :: "BROADCAST(t1)" :: Nil)), BuildLeft)
assertBroadcastNLJoin(
sql(nonEquiJoinQueryWithHint("MERGE(t1)" :: "BROADCAST(t2)" :: Nil)), BuildRight)
assertBroadcastNLJoin(
sql(nonEquiJoinQueryWithHint("SHUFFLE_REPLICATE_NL(t1)" :: "BROADCAST(t2)" :: Nil)),
BuildRight)
// Broadcast hint specified but not doable
assertShuffleMergeJoin(
sql(equiJoinQueryWithHint("BROADCAST(t1)" :: Nil, "left")))
assertShuffleMergeJoin(
sql(equiJoinQueryWithHint("BROADCAST(t2)" :: Nil, "right")))
}
}
}
test("join strategy hint - shuffle-merge") {
withTempView("t1", "t2") {
Seq((1, "4"), (2, "2")).toDF("key", "value").createTempView("t1")
Seq((1, "1"), (2, "12.3"), (2, "123")).toDF("key", "value").createTempView("t2")
withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> Int.MaxValue.toString) {
// Shuffle-merge hint specified on one side
assertShuffleMergeJoin(
sql(equiJoinQueryWithHint("SHUFFLE_MERGE(t1)" :: Nil)))
assertShuffleMergeJoin(
sql(equiJoinQueryWithHint("MERGEJOIN(t2)" :: Nil)))
// Shuffle-merge hint specified on both sides
assertShuffleMergeJoin(
sql(equiJoinQueryWithHint("MERGE(t1, t2)" :: Nil)))
// Shuffle-merge hint prioritized over shuffle-hash hint and shuffle-replicate-nl hint
assertShuffleMergeJoin(
sql(equiJoinQueryWithHint("SHUFFLE_REPLICATE_NL(t2)" :: "MERGE(t1)" :: Nil, "left")))
assertShuffleMergeJoin(
sql(equiJoinQueryWithHint("MERGE(t2)" :: "SHUFFLE_HASH(t1)" :: Nil, "right")))
// Broadcast hint prioritized over shuffle-merge hint, but broadcast hint is not applicable
assertShuffleMergeJoin(
sql(equiJoinQueryWithHint("BROADCAST(t1)" :: "MERGE(t2)" :: Nil, "left")))
assertShuffleMergeJoin(
sql(equiJoinQueryWithHint("BROADCAST(t2)" :: "MERGE(t1)" :: Nil, "right")))
// Shuffle-merge hint specified but not doable
assertBroadcastNLJoin(
sql(nonEquiJoinQueryWithHint("MERGE(t1, t2)" :: Nil, "left")), BuildRight)
}
}
}
test("join strategy hint - shuffle-hash") {
withTempView("t1", "t2") {
Seq((1, "4"), (2, "2")).toDF("key", "value").createTempView("t1")
Seq((1, "1"), (2, "12.3"), (2, "123")).toDF("key", "value").createTempView("t2")
val t1Size = spark.table("t1").queryExecution.analyzed.children.head.stats.sizeInBytes
val t2Size = spark.table("t2").queryExecution.analyzed.children.head.stats.sizeInBytes
assert(t1Size < t2Size)
withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> Int.MaxValue.toString) {
// Shuffle-hash hint specified on one side
assertShuffleHashJoin(
sql(equiJoinQueryWithHint("SHUFFLE_HASH(t1)" :: Nil)), BuildLeft)
assertShuffleHashJoin(
sql(equiJoinQueryWithHint("SHUFFLE_HASH(t2)" :: Nil)), BuildRight)
// Determine build side based on the join type and child relation sizes
assertShuffleHashJoin(
sql(equiJoinQueryWithHint("SHUFFLE_HASH(t1, t2)" :: Nil)), BuildLeft)
assertShuffleHashJoin(
sql(equiJoinQueryWithHint("SHUFFLE_HASH(t1, t2)" :: Nil, "left")), BuildRight)
assertShuffleHashJoin(
sql(equiJoinQueryWithHint("SHUFFLE_HASH(t1, t2)" :: Nil, "right")), BuildLeft)
// Shuffle-hash hint prioritized over shuffle-replicate-nl hint
assertShuffleHashJoin(
sql(equiJoinQueryWithHint("SHUFFLE_REPLICATE_NL(t2)" :: "SHUFFLE_HASH(t1)" :: Nil)),
BuildLeft)
// Broadcast hint prioritized over shuffle-hash hint, but broadcast hint is not applicable
assertShuffleHashJoin(
sql(equiJoinQueryWithHint("BROADCAST(t1)" :: "SHUFFLE_HASH(t2)" :: Nil, "left")),
BuildRight)
assertShuffleHashJoin(
sql(equiJoinQueryWithHint("BROADCAST(t2)" :: "SHUFFLE_HASH(t1)" :: Nil, "right")),
BuildLeft)
// Shuffle-hash hint specified but not doable
assertBroadcastHashJoin(
sql(equiJoinQueryWithHint("SHUFFLE_HASH(t1)" :: Nil, "left")), BuildRight)
assertBroadcastNLJoin(
sql(nonEquiJoinQueryWithHint("SHUFFLE_HASH(t1)" :: Nil)), BuildLeft)
}
}
}
test("join strategy hint - shuffle-replicate-nl") {
withTempView("t1", "t2") {
Seq((1, "4"), (2, "2")).toDF("key", "value").createTempView("t1")
Seq((1, "1"), (2, "12.3"), (2, "123")).toDF("key", "value").createTempView("t2")
withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> Int.MaxValue.toString) {
// Shuffle-replicate-nl hint specified on one side
assertShuffleReplicateNLJoin(
sql(equiJoinQueryWithHint("SHUFFLE_REPLICATE_NL(t1)" :: Nil)))
assertShuffleReplicateNLJoin(
sql(equiJoinQueryWithHint("SHUFFLE_REPLICATE_NL(t2)" :: Nil)))
// Shuffle-replicate-nl hint specified on both sides
assertShuffleReplicateNLJoin(
sql(equiJoinQueryWithHint("SHUFFLE_REPLICATE_NL(t1, t2)" :: Nil)))
// Shuffle-merge hint prioritized over shuffle-replicate-nl hint, but shuffle-merge hint
// is not applicable
assertShuffleReplicateNLJoin(
sql(nonEquiJoinQueryWithHint("MERGE(t1)" :: "SHUFFLE_REPLICATE_NL(t2)" :: Nil)))
// Shuffle-hash hint prioritized over shuffle-replicate-nl hint, but shuffle-hash hint is
// not applicable
assertShuffleReplicateNLJoin(
sql(nonEquiJoinQueryWithHint("SHUFFLE_HASH(t2)" :: "SHUFFLE_REPLICATE_NL(t1)" :: Nil)))
// Shuffle-replicate-nl hint specified but not doable
assertBroadcastHashJoin(
sql(equiJoinQueryWithHint("SHUFFLE_REPLICATE_NL(t1, t2)" :: Nil, "left")), BuildRight)
assertBroadcastNLJoin(
sql(nonEquiJoinQueryWithHint("SHUFFLE_REPLICATE_NL(t1, t2)" :: Nil, "right")), BuildLeft)
}
}
}
test("Verify that the EliminatedResolvedHint rule is idempotent") {
withTempView("t1", "t2") {
Seq((1, "4"), (2, "2")).toDF("key", "value").createTempView("t1")
Seq((1, "1"), (2, "12.3"), (2, "123")).toDF("key", "value").createTempView("t2")
val df = sql("SELECT /*+ broadcast(t2) */ * from t1 join t2 ON t1.key = t2.key")
val optimize = new RuleExecutor[LogicalPlan] {
val batches = Batch("EliminateResolvedHint", FixedPoint(10), EliminateResolvedHint) :: Nil
}
val optimized = optimize.execute(df.logicalPlan)
val expectedHints =
JoinHint(
None,
Some(HintInfo(strategy = Some(BROADCAST)))) :: Nil
val joinHints = optimized collect {
case Join(_, _, _, _, hint) => hint
case _: ResolvedHint => fail("ResolvedHint should not appear after optimize.")
}
assert(joinHints == expectedHints)
}
}
}