cloud-fan commented on a change in pull request #32528:
URL: https://github.com/apache/spark/pull/32528#discussion_r631716305



##########
File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/SortMergeJoinExec.scala
##########
@@ -679,60 +704,124 @@ case class SortMergeJoinExec(
       (evaluateVariables(streamedVars), "")
     }
 
-    val thisPlan = ctx.addReferenceObj("plan", this)
-    val eagerCleanup = s"$thisPlan.cleanupResources();"
-
-    lazy val innerJoin =
+    val beforeLoop =
       s"""
-         |while (findNextJoinRows($streamedInput, $bufferedInput)) {
-         |  ${streamedVarDecl.mkString("\n")}
-         |  ${beforeLoop.trim}
-         |  scala.collection.Iterator<UnsafeRow> $iterator = 
$matches.generateIterator();
-         |  while ($iterator.hasNext()) {
-         |    InternalRow $bufferedRow = (InternalRow) $iterator.next();
-         |    ${condCheck.trim}
-         |    $numOutput.add(1);
-         |    ${consume(ctx, resultVars)}
-         |  }
-         |  if (shouldStop()) return;
-         |}
-         |$eagerCleanup
-     """.stripMargin
-
-    lazy val outerJoin = {
-      val hasOutputRow = ctx.freshName("hasOutputRow")
+         |${streamedVarDecl.mkString("\n")}
+         |${streamedBeforeLoop.trim}
+         |scala.collection.Iterator<UnsafeRow> $iterator = 
$matches.generateIterator();
+       """.stripMargin
+    val outputRow =
       s"""
-         |while ($streamedInput.hasNext()) {
-         |  findNextJoinRows($streamedInput, $bufferedInput);
-         |  ${streamedVarDecl.mkString("\n")}
-         |  ${beforeLoop.trim}
-         |  scala.collection.Iterator<UnsafeRow> $iterator = 
$matches.generateIterator();
-         |  boolean $hasOutputRow = false;
-         |
-         |  // the last iteration of this loop is to emit an empty row if 
there is no matched rows.
-         |  while ($iterator.hasNext() || !$hasOutputRow) {
-         |    InternalRow $bufferedRow = $iterator.hasNext() ?
-         |      (InternalRow) $iterator.next() : null;
-         |    ${condCheck.trim}
-         |    $hasOutputRow = true;
-         |    $numOutput.add(1);
-         |    ${consume(ctx, resultVars)}
-         |  }
-         |  if (shouldStop()) return;
-         |}
-         |$eagerCleanup
+         |$numOutput.add(1);
+         |${consume(ctx, resultVars)}
        """.stripMargin
-    }
+    val findNextJoinRows = s"findNextJoinRows($streamedInput, $bufferedInput)"
+    val thisPlan = ctx.addReferenceObj("plan", this)
+    val eagerCleanup = s"$thisPlan.cleanupResources();"
 
     joinType match {
-      case _: InnerLike => innerJoin
-      case LeftOuter | RightOuter => outerJoin
+      case _: InnerLike =>
+        codegenInner(findNextJoinRows, beforeLoop, iterator, bufferedRow, 
condCheck, outputRow,
+          eagerCleanup)
+      case LeftOuter | RightOuter =>
+        codegenOuter(streamedInput, findNextJoinRows, beforeLoop, iterator, 
bufferedRow, condCheck,
+          ctx.freshName("hasOutputRow"), outputRow, eagerCleanup)
+      case LeftSemi =>
+        codegenSemi(findNextJoinRows, beforeLoop, iterator, bufferedRow, 
condCheck,
+          ctx.freshName("hasOutputRow"), outputRow, eagerCleanup)
       case x =>
         throw new IllegalArgumentException(
           s"SortMergeJoin.doProduce should not take $x as the JoinType")
     }
   }
 
+  /**
+   * Generates the code for Inner join.
+   */
+  private def codegenInner(
+      findNextJoinRows: String,
+      beforeLoop: String,
+      matchIterator: String,
+      bufferedRow: String,
+      conditionCheck: String,
+      outputRow: String,
+      eagerCleanup: String): String = {
+    s"""
+       |while ($findNextJoinRows) {
+       |  ${beforeLoop.trim}
+       |  while ($matchIterator.hasNext()) {
+       |    InternalRow $bufferedRow = (InternalRow) $matchIterator.next();
+       |    ${conditionCheck.trim}
+       |    $outputRow
+       |  }
+       |  if (shouldStop()) return;
+       |}
+       |$eagerCleanup
+     """.stripMargin
+  }
+
+  /**
+   * Generates the code for Left or Right Outer join.
+   */
+  private def codegenOuter(
+      streamedInput: String,
+      findNextJoinRows: String,
+      beforeLoop: String,
+      matchIterator: String,
+      bufferedRow: String,
+      conditionCheck: String,
+      hasOutputRow: String,
+      outputRow: String,
+      eagerCleanup: String): String = {
+    s"""
+       |while ($streamedInput.hasNext()) {
+       |  $findNextJoinRows;
+       |  ${beforeLoop.trim}
+       |  boolean $hasOutputRow = false;
+       |
+       |  // the last iteration of this loop is to emit an empty row if there 
is no matched rows.
+       |  while ($matchIterator.hasNext() || !$hasOutputRow) {
+       |    InternalRow $bufferedRow = $matchIterator.hasNext() ?
+       |      (InternalRow) $matchIterator.next() : null;
+       |    ${conditionCheck.trim}
+       |    $hasOutputRow = true;
+       |    $outputRow
+       |  }
+       |  if (shouldStop()) return;
+       |}
+       |$eagerCleanup
+     """.stripMargin
+  }
+
+  /**
+   * Generates the code for Left Semi join.
+   */
+  private def codegenSemi(
+      findNextJoinRows: String,
+      beforeLoop: String,
+      matchIterator: String,
+      bufferedRow: String,
+      conditionCheck: String,
+      hasOutputRow: String,
+      outputRow: String,
+      eagerCleanup: String): String = {
+    s"""
+       |while ($findNextJoinRows) {
+       |  ${beforeLoop.trim}
+       |  boolean $hasOutputRow = false;

Review comment:
       I see, let's keep it




-- 
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:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to