Github user chenghao-intel commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1448#discussion_r15267409
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/joins.scala ---
    @@ -332,71 +342,88 @@ case class BroadcastNestedLoopJoin(
         }
       }
     
    -  /** The Streamed Relation */
    -  def left = streamed
    -  /** The Broadcast relation */
    -  def right = broadcast
    -
       @transient lazy val boundCondition =
         InterpretedPredicate(
           condition
             .map(c => BindReferences.bindReference(c, left.output ++ 
right.output))
             .getOrElse(Literal(true)))
     
    +  // TODO: low-hanging fruits for performance? (Reduce branching / pattern 
matches?)
       def execute() = {
         val broadcastedRelation =
           
sqlContext.sparkContext.broadcast(broadcast.execute().map(_.copy()).collect().toIndexedSeq)
     
    -    val streamedPlusMatches = streamed.execute().mapPartitions { 
streamedIter =>
    +    /** All rows that either match both-way, or rows from streamed joined 
with nulls. */
    +    val matchesOrStreamedRowsWithNulls = streamed.execute().mapPartitions 
{ streamedIter =>
           val matchedRows = new ArrayBuffer[Row]
           // TODO: Use Spark's BitSet.
    -      val includedBroadcastTuples = new 
BitSet(broadcastedRelation.value.size)
    +      val includedBroadcastTuples =
    +        new scala.collection.mutable.BitSet(broadcastedRelation.value.size)
           val joinedRow = new JoinedRow
     
           streamedIter.foreach { streamedRow =>
             var i = 0
    -        var matched = false
    +        var streamRowMatched = 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
    +          val jr = buildSide match {
    +            case BuildRight => joinedRow(streamedRow, broadcastedRow)
    +            case BuildLeft => joinedRow(broadcastedRow, streamedRow)
    +          }
    +          if (boundCondition(jr)) {
    +            // Putting this branching inside this conditional: assume ++ 
has a
    +            // much higher cost than another branch & pattern matching.
    +            val br = buildSide match {
    +              case BuildRight => streamedRow ++ broadcastedRow
    +              case BuildLeft => broadcastedRow ++ streamedRow
    +            }
    +            matchedRows += buildRow(br)
    +            streamRowMatched = true
                 includedBroadcastTuples += i
               }
               i += 1
             }
     
    -        if (!matched && (joinType == LeftOuter || joinType == FullOuter)) {
    -          matchedRows += buildRow(streamedRow ++ 
Array.fill(right.output.size)(null))
    +        (streamRowMatched, joinType, buildSide) match {
    +          case (false, LeftOuter | FullOuter, BuildRight) =>
    +            matchedRows += buildRow(streamedRow ++ 
Array.fill(right.output.size)(null))
    --- End diff --
    
    Move out `Array.fill(right.output.size)(null)` to avoid object creation in 
every iteration.


---
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