Skip to content
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

support leftsemijoin for sparkSQL #395

Closed
wants to merge 2 commits into from
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 @@ -22,3 +22,4 @@ case object Inner extends JoinType
case object LeftOuter extends JoinType
case object RightOuter extends JoinType
case object FullOuter extends JoinType
case object LeftSemi extends JoinType
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.catalyst.plans.logical

import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.JoinType
import org.apache.spark.sql.catalyst.plans.{LeftSemi, JoinType}
import org.apache.spark.sql.catalyst.types._

case class Project(projectList: Seq[NamedExpression], child: LogicalPlan) extends UnaryNode {
Expand Down Expand Up @@ -81,7 +81,12 @@ case class Join(
condition: Option[Expression]) extends BinaryNode {

def references = condition.map(_.references).getOrElse(Set.empty)
def output = left.output ++ right.output
def output = joinType match {
case LeftSemi =>
left.output
case _ =>
left.output ++ right.output
}
}

case class InsertIntoTable(
Expand Down
82 changes: 56 additions & 26 deletions sql/core/src/main/scala/org/apache/spark/sql/execution/joins.scala
Original file line number Diff line number Diff line change
Expand Up @@ -165,36 +165,66 @@ case class BroadcastNestedLoopJoin(
def execute() = {
val broadcastedRelation = sc.broadcast(broadcast.execute().map(_.copy()).collect().toIndexedSeq)

val streamedPlusMatches = streamed.execute().mapPartitions { streamedIter =>
val matchedRows = new ArrayBuffer[Row]
// TODO: Use Spark's BitSet.
val includedBroadcastTuples = new BitSet(broadcastedRelation.value.size)
val joinedRow = new JoinedRow

streamedIter.foreach { streamedRow =>
var i = 0
var matched = false

while (i < broadcastedRelation.value.size) {
// TODO: One bitset per partition instead of per row.
val broadcastedRow = broadcastedRelation.value(i)
if (boundCondition(joinedRow(streamedRow, broadcastedRow))) {
matchedRows += buildRow(streamedRow ++ broadcastedRow)
matched = true
includedBroadcastTuples += i
}
i += 1
val streamedPlusMatches = joinType match {
case LeftSemi =>
streamed.execute().mapPartitions {
streamedIter =>
val matchedRows = new ArrayBuffer[Row]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the logic is pretty simple here, it might be better to stream the results though a custom iterator instead of buffering them all in memory. It would also be good to do this match at a higher level, or even break this out into its own operator so that we don't need to build tuple objects for no reason.

val joinedRow = new JoinedRow

streamedIter.foreach {
streamedRow =>
var i = 0
var matched = false

while (i < broadcastedRelation.value.size && !matched) {
// TODO: One bitset per partition instead of per row.
val broadcastedRow = broadcastedRelation.value(i)
if (boundCondition(joinedRow(streamedRow, broadcastedRow))) {
matchedRows += streamedRow
matched = true
}
i += 1
}
}
Iterator((matchedRows, null))
}

if (!matched && (joinType == LeftOuter || joinType == FullOuter)) {
matchedRows += buildRow(streamedRow ++ Array.fill(right.output.size)(null))
case _ =>
streamed.execute().mapPartitions {
streamedIter =>
val matchedRows = new ArrayBuffer[Row]
// TODO: Use Spark's BitSet.
val includedBroadcastTuples = new BitSet(broadcastedRelation.value.size)
val joinedRow = new JoinedRow

val rightNull = Array.fill(right.output.size)(null)

streamedIter.foreach {
streamedRow =>
var i = 0
var matched = false

while (i < broadcastedRelation.value.size) {
// TODO: One bitset per partition instead of per row.
val broadcastedRow = broadcastedRelation.value(i)
if (boundCondition(joinedRow(streamedRow, broadcastedRow))) {
matchedRows += joinedRow(streamedRow.copy, broadcastedRow)
matched = true
includedBroadcastTuples += i
}
i += 1
}

if (!matched && (joinType == LeftOuter || joinType == FullOuter)) {
matchedRows += joinedRow(streamedRow.copy, rightNull)
}
}
Iterator((matchedRows, includedBroadcastTuples))
}
}
Iterator((matchedRows, includedBroadcastTuples))
}

val includedBroadcastTuples = streamedPlusMatches.map(_._2)
val allIncludedBroadcastTuples =
lazy val includedBroadcastTuples = streamedPlusMatches.map(_._2)
lazy val allIncludedBroadcastTuples =
if (includedBroadcastTuples.count == 0) {
new scala.collection.mutable.BitSet(broadcastedRelation.value.size)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ object HiveQl {
case "TOK_RIGHTOUTERJOIN" => RightOuter
case "TOK_LEFTOUTERJOIN" => LeftOuter
case "TOK_FULLOUTERJOIN" => FullOuter
case "TOK_LEFTSEMIJOIN" => LeftSemi
}
assert(other.size <= 1, "Unhandled join clauses.")
Join(nodeToRelation(relation1),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Hank 2
Hank 2
Joe 2
Joe 2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Hank 2
Hank 2
Joe 2
Joe 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2 Tie
2 Tie
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
1
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ class HiveCompatibilitySuite extends HiveQueryFileTest {
"join_view",
"lateral_view_cp",
"lateral_view_ppd",
"leftsemijoin",
"leftsemijoin_mr",
"lineage1",
"literal_double",
"literal_ints",
Expand Down