Skip to content
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 @@ -27,6 +27,7 @@ import org.apache.spark.sql.catalyst.rules._
import org.apache.spark.sql.catalyst.trees.TreePattern._
import org.apache.spark.sql.errors.QueryCompilationErrors
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.util.Utils

/**
* Reorder the joins and push all the conditions into join, so that the bottom ones have at least
Expand Down Expand Up @@ -274,14 +275,16 @@ trait JoinSelectionHelper {
} else {
hintToPreferShuffleHashJoinLeft(hint) ||
(!conf.preferSortMergeJoin && canBuildLocalHashMapBySize(left, conf) &&
muchSmaller(left, right))
muchSmaller(left, right)) ||
forceApplyShuffledHashJoin(conf)
}
val buildRight = if (hintOnly) {
hintToShuffleHashJoinRight(hint)
} else {
hintToPreferShuffleHashJoinRight(hint) ||
(!conf.preferSortMergeJoin && canBuildLocalHashMapBySize(right, conf) &&
muchSmaller(right, left))
muchSmaller(right, left)) ||
forceApplyShuffledHashJoin(conf)
}
getBuildSide(
canBuildShuffledHashJoinLeft(joinType) && buildLeft,
Expand Down Expand Up @@ -424,5 +427,14 @@ trait JoinSelectionHelper {
private def muchSmaller(a: LogicalPlan, b: LogicalPlan): Boolean = {
a.stats.sizeInBytes * 3 <= b.stats.sizeInBytes
}

/**
* Returns whether a shuffled hash join should be force applied.
* The config key is hard-coded because it's testing only and should not be exposed.
*/
private def forceApplyShuffledHashJoin(conf: SQLConf): Boolean = {
Utils.isTesting &&
conf.getConfString("spark.sql.join.forceApplyShuffledHashJoin", "false") == "true"
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

--CONFIG_DIM1 spark.sql.autoBroadcastJoinThreshold=10485760
--CONFIG_DIM1 spark.sql.autoBroadcastJoinThreshold=-1,spark.sql.join.preferSortMergeJoin=true
--CONFIG_DIM1 spark.sql.autoBroadcastJoinThreshold=-1,spark.sql.join.preferSortMergeJoin=false
--CONFIG_DIM1 spark.sql.autoBroadcastJoinThreshold=-1,spark.sql.join.forceApplyShuffledHashJoin=true

--CONFIG_DIM2 spark.sql.codegen.wholeStage=true
--CONFIG_DIM2 spark.sql.codegen.wholeStage=false,spark.sql.codegen.factoryMode=CODEGEN_ONLY
Expand Down
8 changes: 8 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/JoinSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1394,4 +1394,12 @@ class JoinSuite extends QueryTest with SharedSparkSession with AdaptiveSparkPlan
checkAnswer(fullJoinDF, Row(100))
}
}

test("SPARK-35984: Config to force applying shuffled hash join") {
val sql = "SELECT * FROM testData JOIN testData2 ON key = a"
assertJoin(sql, classOf[SortMergeJoinExec])
withSQLConf("spark.sql.join.forceApplyShuffledHashJoin" -> "true") {
assertJoin(sql, classOf[ShuffledHashJoinExec])
}
}
}