Github user marmbrus commented on a diff in the pull request:

    https://github.com/apache/spark/pull/395#discussion_r11650765
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/joins.scala ---
    @@ -165,36 +165,64 @@ 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]
    +            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 += buildRow(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
    +
    +            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)
    --- End diff --
    
    Existing: (I realize this was already here.)
    
    I think it would be cheaper to use a new `JoinedRow` here instead of 
`buildRow`.  `buildRow` is going to allocate a new Array and copy all the 
values instead of just the values for `streamedRow`.  You will also need to 
call `copy()` on `streamedRow`.
    
    A similar trick could be done below to avoid creating a new Array of nulls 
for each output row.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to