c21 commented on a change in pull request #34034:
URL: https://github.com/apache/spark/pull/34034#discussion_r717205037
##########
File path: sql/core/src/test/scala/org/apache/spark/sql/JoinSuite.scala
##########
@@ -1402,4 +1402,73 @@ class JoinSuite extends QueryTest with
SharedSparkSession with AdaptiveSparkPlan
assertJoin(sql, classOf[ShuffledHashJoinExec])
}
}
+
+ test("SPARK-36794: Ignore duplicated key when building relation for
semi/anti hash join") {
+ withTable("t1", "t2") {
+ spark.range(10).map(i => (i.toString, i + 1)).toDF("c1",
"c2").write.saveAsTable("t1")
+ spark.range(10).map(i => ((i % 5).toString, i % 3)).toDF("c1",
"c2").write.saveAsTable("t2")
+
+ Seq("BROADCAST", "SHUFFLE_HASH").foreach {
+ joinHint =>
+ val semiJoinDFs = Seq(
+ // No join condition, ignore duplicated key.
+ (sql(
+ s"SELECT /*+ $joinHint(t2) */ t1.c1 FROM t1 LEFT SEMI JOIN t2 ON
t1.c1 = t2.c1"),
+ true),
+ // Have join condition on build join key only, ignore duplicated
key.
+ (sql(
+ s"""
+ |SELECT /*+ $joinHint(t2) */ t1.c1 FROM t1 LEFT SEMI JOIN t2
+ |ON t1.c1 = t2.c1 AND CAST(t1.c2 * 2 AS STRING) != t2.c1
+ """.stripMargin),
+ true),
+ // Have join condition on other build attribute beside join key,
do not ignore
+ // duplicated key.
+ (sql(
+ s"""
+ |SELECT /*+ $joinHint(t2) */ t1.c1 FROM t1 LEFT SEMI JOIN t2
+ |ON t1.c1 = t2.c1 AND t1.c2 * 100 != t2.c2
+ """.stripMargin),
+ false)
+ )
+ val antiJoinDFs = Seq(
Review comment:
@cloud-fan - sure, updated.
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashJoin.scala
##########
@@ -158,6 +158,17 @@ trait HashJoin extends JoinCodegenSupport {
output, (streamedPlan.output ++
buildPlan.output).map(_.withNullability(true)))
}
+ // Exposed for testing
+ @transient lazy val ignoreDuplicatedKey = joinType match {
+ case LeftExistence(_) =>
+ // For building hash relation, ignore duplicated rows with same join
keys if:
+ // 1. Join condition is empty.
+ // 2. Join condition only references streamed attributes and build join
keys.
Review comment:
@viirya - yes, updated.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]