forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-7026] [SQL] fix left semi join with equi key and non-equi cond…
…ition When the `condition` extracted by `ExtractEquiJoinKeys` contain join Predicate for left semi join, we can not plan it as semiJoin. Such as SELECT * FROM testData2 x LEFT SEMI JOIN testData2 y ON x.b = y.b AND x.a >= y.a + 2 Condition `x.a >= y.a + 2` can not evaluate on table `x`, so it throw errors Author: Daoyuan Wang <[email protected]> Closes apache#5643 from adrian-wang/spark7026 and squashes the following commits: cc09809 [Daoyuan Wang] refactor semijoin and add plan test 575a7c8 [Daoyuan Wang] fix notserializable 27841de [Daoyuan Wang] fix rebase 10bf124 [Daoyuan Wang] fix style 72baa02 [Daoyuan Wang] fix style 8e0afca [Daoyuan Wang] merge commits for rebase
- Loading branch information
1 parent
b13ef77
commit 1707238
Showing
7 changed files
with
208 additions
and
59 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
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
91 changes: 91 additions & 0 deletions
91
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashSemiJoin.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,91 @@ | ||
/* | ||
* 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.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.execution.SparkPlan | ||
|
||
|
||
trait HashSemiJoin { | ||
self: SparkPlan => | ||
val leftKeys: Seq[Expression] | ||
val rightKeys: Seq[Expression] | ||
val left: SparkPlan | ||
val right: SparkPlan | ||
val condition: Option[Expression] | ||
|
||
override def output: Seq[Attribute] = left.output | ||
|
||
@transient protected lazy val rightKeyGenerator: Projection = | ||
newProjection(rightKeys, right.output) | ||
|
||
@transient protected lazy val leftKeyGenerator: () => MutableProjection = | ||
newMutableProjection(leftKeys, left.output) | ||
|
||
@transient private lazy val boundCondition = | ||
newPredicate(condition.getOrElse(Literal(true)), left.output ++ right.output) | ||
|
||
protected def buildKeyHashSet( | ||
buildIter: Iterator[InternalRow], | ||
copy: Boolean): java.util.Set[InternalRow] = { | ||
val hashSet = new java.util.HashSet[InternalRow]() | ||
var currentRow: InternalRow = null | ||
|
||
// Create a Hash set of buildKeys | ||
while (buildIter.hasNext) { | ||
currentRow = buildIter.next() | ||
val rowKey = rightKeyGenerator(currentRow) | ||
if (!rowKey.anyNull) { | ||
val keyExists = hashSet.contains(rowKey) | ||
if (!keyExists) { | ||
if (copy) { | ||
hashSet.add(rowKey.copy()) | ||
} else { | ||
// rowKey may be not serializable (from codegen) | ||
hashSet.add(rowKey) | ||
} | ||
} | ||
} | ||
} | ||
hashSet | ||
} | ||
|
||
protected def hashSemiJoin( | ||
streamIter: Iterator[InternalRow], | ||
hashedRelation: HashedRelation): Iterator[InternalRow] = { | ||
val joinKeys = leftKeyGenerator() | ||
val joinedRow = new JoinedRow | ||
streamIter.filter(current => { | ||
lazy val rowBuffer = hashedRelation.get(joinKeys.currentValue) | ||
!joinKeys(current).anyNull && rowBuffer != null && rowBuffer.exists { | ||
(build: InternalRow) => boundCondition(joinedRow(current, build)) | ||
} | ||
}) | ||
} | ||
|
||
protected def hashSemiJoin( | ||
streamIter: Iterator[InternalRow], | ||
hashSet: java.util.Set[InternalRow]): Iterator[InternalRow] = { | ||
val joinKeys = leftKeyGenerator() | ||
val joinedRow = new JoinedRow | ||
streamIter.filter(current => { | ||
!joinKeys(current.copy()).anyNull && hashSet.contains(joinKeys.currentValue) | ||
}) | ||
} | ||
} |
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
74 changes: 74 additions & 0 deletions
74
sql/core/src/test/scala/org/apache/spark/sql/execution/joins/SemiJoinSuite.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,74 @@ | ||
/* | ||
* 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.sql.Row | ||
import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
import org.apache.spark.sql.catalyst.expressions.{LessThan, Expression} | ||
import org.apache.spark.sql.execution.{SparkPlan, SparkPlanTest} | ||
|
||
|
||
class SemiJoinSuite extends SparkPlanTest{ | ||
val left = Seq( | ||
(1, 2.0), | ||
(1, 2.0), | ||
(2, 1.0), | ||
(2, 1.0), | ||
(3, 3.0) | ||
).toDF("a", "b") | ||
|
||
val right = Seq( | ||
(2, 3.0), | ||
(2, 3.0), | ||
(3, 2.0), | ||
(4, 1.0) | ||
).toDF("c", "d") | ||
|
||
val leftKeys: List[Expression] = 'a :: Nil | ||
val rightKeys: List[Expression] = 'c :: Nil | ||
val condition = Some(LessThan('b, 'd)) | ||
|
||
test("left semi join hash") { | ||
checkAnswer2(left, right, (left: SparkPlan, right: SparkPlan) => | ||
LeftSemiJoinHash(leftKeys, rightKeys, left, right, condition), | ||
Seq( | ||
(2, 1.0), | ||
(2, 1.0) | ||
).map(Row.fromTuple)) | ||
} | ||
|
||
test("left semi join BNL") { | ||
checkAnswer2(left, right, (left: SparkPlan, right: SparkPlan) => | ||
LeftSemiJoinBNL(left, right, condition), | ||
Seq( | ||
(1, 2.0), | ||
(1, 2.0), | ||
(2, 1.0), | ||
(2, 1.0) | ||
).map(Row.fromTuple)) | ||
} | ||
|
||
test("broadcast left semi join hash") { | ||
checkAnswer2(left, right, (left: SparkPlan, right: SparkPlan) => | ||
BroadcastLeftSemiJoinHash(leftKeys, rightKeys, left, right, condition), | ||
Seq( | ||
(2, 1.0), | ||
(2, 1.0) | ||
).map(Row.fromTuple)) | ||
} | ||
} |