Skip to content

[SPARK-52873][SQL] Further restrict when SHJ semi/anti join can ignore duplicate keys on the build side #52067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,35 @@ case class ShuffledHashJoinExec(
case _ => super.outputOrdering
}

private def validCondForIgnoreDupKey(cond: Expression): Boolean = {
// to ignore duplicate keys on the build side, the join condition must
// have the following properties:
// 1) a subtree that is a semantic match to a build-side key, and/or
// 2) outside any subtree that is a semantic match to a build-side key,
// all attributes should be from the stream-side.
val buildKeysSet = ExpressionSet(buildKeys)
val streamedOutputAttrs = AttributeSet(streamedOutput)

def validCond(cond: Expression): Boolean = {
cond match {
// don't bother traversing any subtree that has a semantic match to a build key
case e: Expression if buildKeysSet.contains(e) => true
// all attributes (outside any subtree that matches a build key) should be
// from the stream side
case a: Attribute if !streamedOutputAttrs.contains(a) => false
case e: Expression =>
e.children.forall(validCond(_))
case _ => true
}
}

validCond(cond)
}

// 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, or
// 2. Join condition only references streamed attributes and build join keys.
val streamedOutputAndBuildKeys = AttributeSet(streamedOutput ++ buildKeys)
condition.forall(_.references.subsetOf(streamedOutputAndBuildKeys))
condition.forall(validCondForIgnoreDupKey(_))
case _ => false
}

Expand Down
37 changes: 31 additions & 6 deletions sql/core/src/test/scala/org/apache/spark/sql/JoinSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1571,30 +1571,55 @@ class JoinSuite extends QueryTest with SharedSparkSession with AdaptiveSparkPlan
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")

val semiExpected1 = Seq(Row("0"), Row("1"), Row("2"), Row("3"), Row("4"))
val antiExpected1 = Seq(Row("5"), Row("6"), Row("7"), Row("8"), Row("9"))
val semiExpected2 = Seq(Row("0"))
val antiExpected2 = Seq.tabulate(9) { x => Row((x + 1).toString) }

val semiJoinQueries = Seq(
// No join condition, ignore duplicated key.
(s"SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1 FROM t1 LEFT SEMI JOIN t2 ON t1.c1 = t2.c1",
true),
true, semiExpected1, antiExpected1),
// Have join condition on build join key only, ignore duplicated key.
(s"""
|SELECT /*+ SHUFFLE_HASH(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),
true, semiExpected1, antiExpected1),
// Have join condition on other build attribute beside join key, do not ignore
// duplicated key.
(s"""
|SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1 FROM t1 LEFT SEMI JOIN t2
|ON t1.c1 = t2.c1 AND t1.c2 * 100 != t2.c2
""".stripMargin,
false)
false, semiExpected1, antiExpected1),
// SPARK-52873: Have a join condition that references attributes from the build-side
// join key, but those attributes are contained by a different expression than that
// used as the build-side join key (that is, CAST((t2.c2+10000)/1000 AS INT) is not
// the same as t2.c2). In this case, ignoreDuplicatedKey should be false
(
s"""
|SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1 FROM t1 LEFT SEMI JOIN t2
|ON CAST((t1.c2+10000)/1000 AS INT) = CAST((t2.c2+10000)/1000 AS INT)
|AND t2.c2 >= t1.c2 + 1
|""".stripMargin,
false, semiExpected2, antiExpected2),
// SPARK-52873: Have a join condition that contains the same expression as the
// build-side join key,and does not violate any other rules for the join condition.
// In this case, ignoreDuplicatedKey should be true
(
s"""
|SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1 FROM t1 LEFT SEMI JOIN t2
|ON t1.c1 * 10000 = t2.c1 * 1000 AND t2.c1 * 1000 >= t1.c1
|""".stripMargin,
true, semiExpected2, antiExpected2)
)
semiJoinQueries.foreach {
case (query, ignoreDuplicatedKey) =>
case (query, ignoreDuplicatedKey, semiExpected, antiExpected) =>
val semiJoinDF = sql(query)
val antiJoinDF = sql(query.replaceAll("SEMI", "ANTI"))
checkAnswer(semiJoinDF, Seq(Row("0"), Row("1"), Row("2"), Row("3"), Row("4")))
checkAnswer(antiJoinDF, Seq(Row("5"), Row("6"), Row("7"), Row("8"), Row("9")))
checkAnswer(semiJoinDF, semiExpected)
checkAnswer(antiJoinDF, antiExpected)
Seq(semiJoinDF, antiJoinDF).foreach { df =>
assert(collect(df.queryExecution.executedPlan) {
case j: ShuffledHashJoinExec if j.ignoreDuplicatedKey == ignoreDuplicatedKey => true
Expand Down