-
Notifications
You must be signed in to change notification settings - Fork 28.4k
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
[SPARK-7026] [SQL] fix left semi join with equi key and non-equi condition #5643
Changes from all commits
8e0afca
72baa02
10bf124
27841de
575a7c8
cc09809
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -395,6 +395,18 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll with SQLTestUtils { | |
) | ||
} | ||
|
||
test("left semi greater than predicate and equal operator") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adrian-wang i suggest you add the case chenghao described in my PR to the unit test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create a pr for your branch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. closed since you have added the test |
||
checkAnswer( | ||
sql("SELECT * FROM testData2 x LEFT SEMI JOIN testData2 y ON x.b = y.b and x.a >= y.a + 2"), | ||
Seq(Row(3, 1), Row(3, 2)) | ||
) | ||
|
||
checkAnswer( | ||
sql("SELECT * FROM testData2 x LEFT SEMI JOIN testData2 y ON x.b = y.a and x.a >= y.b + 1"), | ||
Seq(Row(2, 1), Row(2, 2), Row(3, 1), Row(3, 2)) | ||
) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be great to also add some tests using the new |
||
|
||
test("index into array of arrays") { | ||
checkAnswer( | ||
sql( | ||
|
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)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Long term I wonder if its actually a win for us to build just a set instead of using hashed relation everywhere. We have done a bunch optimization on HashedRelation to make it serialize faster.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we need to implement a version of HashedRelation which only stores the keys.