forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-13977] [SQL] Brings back Shuffled hash join
## What changes were proposed in this pull request? ShuffledHashJoin (also outer join) is removed in 1.6, in favor of SortMergeJoin, which is more robust and also fast. ShuffledHashJoin is still useful in this case: 1) one table is much smaller than the other one, then cost to build a hash table on smaller table is smaller than sorting the larger table 2) any partition of the small table could fit in memory. This PR brings back ShuffledHashJoin, basically revert apache#9645, and fix the conflict. Also merging outer join and left-semi join into the same class. This PR does not implement full outer join, because it's not implemented efficiently (requiring build hash table on both side). A simple benchmark (one table is 5x smaller than other one) show that ShuffledHashJoin could be 2X faster than SortMergeJoin. ## How was this patch tested? Added new unit tests for ShuffledHashJoin. Author: Davies Liu <davies@databricks.com> Closes apache#11788 from davies/shuffle_join.
- Loading branch information
Showing
13 changed files
with
277 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 0 additions & 58 deletions
58
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/LeftSemiJoinHash.scala
This file was deleted.
Oops, something went wrong.
95 changes: 95 additions & 0 deletions
95
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/ShuffledHashJoin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* 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.execution.joins | ||
|
||
import org.apache.spark.rdd.RDD | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions.{Expression, JoinedRow} | ||
import org.apache.spark.sql.catalyst.plans._ | ||
import org.apache.spark.sql.catalyst.plans.physical._ | ||
import org.apache.spark.sql.execution.{BinaryNode, SparkPlan} | ||
import org.apache.spark.sql.execution.metric.SQLMetrics | ||
|
||
/** | ||
* Performs an inner hash join of two child relations by first shuffling the data using the join | ||
* keys. | ||
*/ | ||
case class ShuffledHashJoin( | ||
leftKeys: Seq[Expression], | ||
rightKeys: Seq[Expression], | ||
joinType: JoinType, | ||
buildSide: BuildSide, | ||
condition: Option[Expression], | ||
left: SparkPlan, | ||
right: SparkPlan) | ||
extends BinaryNode with HashJoin { | ||
|
||
override private[sql] lazy val metrics = Map( | ||
"numOutputRows" -> SQLMetrics.createLongMetric(sparkContext, "number of output rows")) | ||
|
||
override def outputPartitioning: Partitioning = joinType match { | ||
case Inner => PartitioningCollection(Seq(left.outputPartitioning, right.outputPartitioning)) | ||
case LeftSemi => left.outputPartitioning | ||
case LeftOuter => left.outputPartitioning | ||
case RightOuter => right.outputPartitioning | ||
case FullOuter => UnknownPartitioning(left.outputPartitioning.numPartitions) | ||
case x => | ||
throw new IllegalArgumentException(s"ShuffledHashJoin should not take $x as the JoinType") | ||
} | ||
|
||
override def requiredChildDistribution: Seq[Distribution] = | ||
ClusteredDistribution(leftKeys) :: ClusteredDistribution(rightKeys) :: Nil | ||
|
||
protected override def doExecute(): RDD[InternalRow] = { | ||
val numOutputRows = longMetric("numOutputRows") | ||
|
||
streamedPlan.execute().zipPartitions(buildPlan.execute()) { (streamIter, buildIter) => | ||
val hashed = HashedRelation(buildIter.map(_.copy()), buildSideKeyGenerator) | ||
val joinedRow = new JoinedRow | ||
joinType match { | ||
case Inner => | ||
hashJoin(streamIter, hashed, numOutputRows) | ||
|
||
case LeftSemi => | ||
hashSemiJoin(streamIter, hashed, numOutputRows) | ||
|
||
case LeftOuter => | ||
val keyGenerator = streamSideKeyGenerator | ||
val resultProj = createResultProjection | ||
streamIter.flatMap(currentRow => { | ||
val rowKey = keyGenerator(currentRow) | ||
joinedRow.withLeft(currentRow) | ||
leftOuterIterator(rowKey, joinedRow, hashed.get(rowKey), resultProj, numOutputRows) | ||
}) | ||
|
||
case RightOuter => | ||
val keyGenerator = streamSideKeyGenerator | ||
val resultProj = createResultProjection | ||
streamIter.flatMap(currentRow => { | ||
val rowKey = keyGenerator(currentRow) | ||
joinedRow.withRight(currentRow) | ||
rightOuterIterator(rowKey, hashed.get(rowKey), joinedRow, resultProj, numOutputRows) | ||
}) | ||
|
||
case x => | ||
throw new IllegalArgumentException( | ||
s"ShuffledHashJoin should not take $x as the JoinType") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.