-
Notifications
You must be signed in to change notification settings - Fork 28.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BinarydHashJoinNode and BroadcastHashJoinNode.
- Loading branch information
Showing
4 changed files
with
237 additions
and
15 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
sql/core/src/main/scala/org/apache/spark/sql/execution/local/BinarydHashJoinNode.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,103 @@ | ||
/* | ||
* 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.local | ||
|
||
import org.apache.spark.sql.SQLConf | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.execution.joins.{HashedRelation, BuildLeft, BuildRight, BuildSide} | ||
|
||
/** | ||
* A wrapper of [[HashJoinNode]]. It will build the [[HashedRelation]] according to the value of | ||
* `buildSide`. The actual work of this node will be delegated to the [[HashJoinNode]] | ||
* that is created in `open`. | ||
*/ | ||
case class BinarydHashJoinNode( | ||
conf: SQLConf, | ||
leftKeys: Seq[Expression], | ||
rightKeys: Seq[Expression], | ||
buildSide: BuildSide, | ||
left: LocalNode, | ||
right: LocalNode) extends BinaryLocalNode(conf) { | ||
|
||
private[this] lazy val (buildNode, buildKeys, streamedNode, streamedKeys) = buildSide match { | ||
case BuildLeft => (left, leftKeys, right, rightKeys) | ||
case BuildRight => (right, rightKeys, left, leftKeys) | ||
} | ||
|
||
private[this] var hashJoinNode: HashJoinNode = _ | ||
|
||
override def output: Seq[Attribute] = left.output ++ right.output | ||
|
||
private[this] def isUnsafeMode: Boolean = { | ||
(codegenEnabled && unsafeEnabled && UnsafeProjection.canSupport(buildKeys)) | ||
} | ||
|
||
private[this] def buildSideKeyGenerator: Projection = { | ||
if (isUnsafeMode) { | ||
UnsafeProjection.create(buildKeys, buildNode.output) | ||
} else { | ||
newMutableProjection(buildKeys, buildNode.output)() | ||
} | ||
} | ||
|
||
override def open(): Unit = { | ||
// buildNode's prepare has been called in this.prepare. | ||
buildNode.open() | ||
val hashedRelation = HashedRelation(buildNode, buildSideKeyGenerator) | ||
// We have built the HashedRelation. So, close buildNode. | ||
buildNode.close() | ||
|
||
// Call the open of streamedNode. | ||
streamedNode.open() | ||
// Create the HashJoinNode based on the streamedNode and HashedRelation. | ||
hashJoinNode = | ||
HashJoinNode( | ||
conf = conf, | ||
streamedKeys = streamedKeys, | ||
streamedNode = streamedNode, | ||
buildSide = buildSide, | ||
buildOutput = buildNode.output, | ||
hashedRelation = hashedRelation, | ||
isWrapped = true) | ||
// Setup this HashJoinNode. We still call these in case there is any setup work | ||
// that needs to be done in this HashJoinNode. Because isWrapped is true, | ||
// prepare and open will not propagate to the child of streamedNode. | ||
hashJoinNode.prepare() | ||
hashJoinNode.open() | ||
} | ||
|
||
override def next(): Boolean = { | ||
hashJoinNode.next() | ||
} | ||
|
||
override def fetch(): InternalRow = { | ||
hashJoinNode.fetch() | ||
} | ||
|
||
override def close(): Unit = { | ||
// Close the internal HashJoinNode. We still call this in case there is any teardown work | ||
// that needs to be done in this HashJoinNode. Because isWrapped is true, | ||
// prepare and open will not propagate to the child of streamedNode. | ||
hashJoinNode.close() | ||
// Now, close the streamedNode. | ||
streamedNode.close() | ||
// Please note that we do not need to call the close method of our buildNode because | ||
// it has been called in this.open. | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
sql/core/src/main/scala/org/apache/spark/sql/execution/local/BroadcastHashJoinNode.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,87 @@ | ||
/* | ||
* 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.local | ||
|
||
import org.apache.spark.broadcast.Broadcast | ||
import org.apache.spark.sql.SQLConf | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.execution.joins.{BuildLeft, BuildRight, BuildSide, HashedRelation} | ||
|
||
/** | ||
* A wrapper of [[HashJoinNode]] for broadcast join. The actual work of this node will be | ||
* delegated to the [[HashJoinNode]] that is created in `open`. | ||
*/ | ||
case class BroadcastHashJoinNode( | ||
conf: SQLConf, | ||
streamedKeys: Seq[Expression], | ||
streamedNode: LocalNode, | ||
buildSide: BuildSide, | ||
buildOutput: Seq[Attribute], | ||
hashedRelation: Broadcast[HashedRelation]) extends UnaryLocalNode(conf) { | ||
|
||
override val child = streamedNode | ||
|
||
private[this] var hashJoinNode: HashJoinNode = _ | ||
|
||
// Because we do not pass in the buildNode, we take the output of buildNode to | ||
// create the inputSet properly. | ||
override def inputSet: AttributeSet = AttributeSet(child.output ++ buildOutput) | ||
|
||
override def output: Seq[Attribute] = buildSide match { | ||
case BuildRight => streamedNode.output ++ buildOutput | ||
case BuildLeft => buildOutput ++ streamedNode.output | ||
} | ||
|
||
override def open(): Unit = { | ||
// Call the open of streamedNode. | ||
streamedNode.open() | ||
// Create the HashJoinNode based on the streamedNode and HashedRelation. | ||
hashJoinNode = | ||
HashJoinNode( | ||
conf = conf, | ||
streamedKeys = streamedKeys, | ||
streamedNode = streamedNode, | ||
buildSide = buildSide, | ||
buildOutput = buildOutput, | ||
hashedRelation = hashedRelation.value, | ||
isWrapped = true) | ||
// Setup this HashJoinNode. We still call these in case there is any setup work | ||
// that needs to be done in this HashJoinNode. Because isWrapped is true, | ||
// prepare and open will not propagate to the child of streamedNode. | ||
hashJoinNode.prepare() | ||
hashJoinNode.open() | ||
} | ||
|
||
override def next(): Boolean = { | ||
hashJoinNode.next() | ||
} | ||
|
||
override def fetch(): InternalRow = { | ||
hashJoinNode.fetch() | ||
} | ||
|
||
override def close(): Unit = { | ||
// Close the internal HashJoinNode. We still call this in case there is any teardown work | ||
// that needs to be done in this HashJoinNode. Because isWrapped is true, | ||
// prepare and open will not propagate to the child of streamedNode. | ||
hashJoinNode.close() | ||
// Now, close the streamedNode. | ||
streamedNode.close() | ||
} | ||
} |
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