c21 commented on a change in pull request #31736:
URL: https://github.com/apache/spark/pull/31736#discussion_r588148408



##########
File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/BroadcastNestedLoopJoinExec.scala
##########
@@ -393,4 +394,65 @@ case class BroadcastNestedLoopJoinExec(
       }
     }
   }
+
+  override def supportCodegen: Boolean = {
+    joinType.isInstanceOf[InnerLike]
+  }
+
+  override def inputRDDs(): Seq[RDD[InternalRow]] = {
+    streamed.asInstanceOf[CodegenSupport].inputRDDs()
+  }
+
+  override def needCopyResult: Boolean = true
+
+  override def doProduce(ctx: CodegenContext): String = {
+    streamed.asInstanceOf[CodegenSupport].produce(ctx, this)
+  }
+
+  override def doConsume(ctx: CodegenContext, input: Seq[ExprCode], row: 
ExprCode): String = {
+    joinType match {
+      case _: InnerLike => codegenInner(ctx, input)
+      case _ =>
+        throw new IllegalArgumentException(
+          s"BroadcastNestedLoopJoin code-gen should not take $joinType as the 
JoinType")
+    }
+  }
+
+  /**
+   * Returns the variable name for [[Broadcast]] side.
+   */
+  private def prepareBroadcast(ctx: CodegenContext): String = {
+    // Create a name for broadcast side
+    val broadcastArray = broadcast.executeBroadcast[Array[InternalRow]]()
+    val broadcastTerm = ctx.addReferenceObj("broadcastTerm", broadcastArray)
+
+    // Inline mutable state since not many join operations in a task
+    ctx.addMutableState("InternalRow[]", "buildRowArray",
+      v => s"$v = (InternalRow[]) $broadcastTerm.value();", forceInline = true)
+  }
+
+  private def codegenInner(ctx: CodegenContext, input: Seq[ExprCode]): String 
= {
+    val buildRowArrayTerm = prepareBroadcast(ctx)
+    val (buildRow, checkCondition, buildVars) = getJoinCondition(ctx, input, 
streamed, broadcast)
+
+    val resultVars = buildSide match {
+      case BuildLeft => buildVars ++ input
+      case BuildRight => input ++ buildVars
+    }
+    val arrayIndex = ctx.freshName("arrayIndex")
+    val numOutput = metricTerm(ctx, "numOutputRows")
+
+    s"""
+       |int $arrayIndex = 0;
+       |UnsafeRow $buildRow;
+       |while ($arrayIndex < $buildRowArrayTerm.length) {

Review comment:
       I vaguely remembered some places in one PR that I saw `while` is 
preferred over `for` (or my memory can be wrong and it's opposite). @cloud-fan 
- do you remember related discussion?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to