LukaZdravic commented on code in PR #60227:
URL: https://github.com/apache/spark/pull/60227#discussion_r4514607635


##########
sql/core/src/test/scala/org/apache/spark/sql/SortMergeAsOfJoinSuite.scala:
##########
@@ -67,38 +71,85 @@ class SortMergeAsOfJoinSuite extends QueryTest
     (df1, df2)
   }
 
-  // Like checkAnswer, but also asserts the right-side buffer spilled 
(spillSize > 0).
-  private def checkAnswerAndSpill(df: classic.DataFrame, expectedAnswer: 
Seq[Row]): Unit = {
-    checkAnswer(df, expectedAnswer)
-    val op = collectFirst(df.queryExecution.executedPlan) {
-      case s: SortMergeAsOfJoinExec => s
+  // Like checkAnswer, but also asserts the right-side buffer spilled 
(spillSize > 0). Runs with
+  // the ASOF join codegen off and on, so both paths must handle the spilled 
buffer.
+  private def checkAnswerAndSpill(df: => classic.DataFrame, expectedAnswer: 
Seq[Row]): Unit = {
+    Seq(false, true).foreach { codegen =>
+      withClue(s"ASOF join codegen $codegen: ") {
+        withSQLConf(SQLConf.ENABLE_SORT_MERGE_AS_OF_JOIN_CODEGEN.key -> 
codegen.toString) {
+          val query = df
+          checkAnswer(query, expectedAnswer)
+          val op = collectFirst(query.queryExecution.executedPlan) {
+            case s: SortMergeAsOfJoinExec => s
+          }
+          assert(op.isDefined,
+            s"Expected SortMergeAsOfJoinExec in 
plan:\n${query.queryExecution.executedPlan}")
+          assert(op.get.metrics("spillSize").value > 0,
+            s"Expected the right-side buffer to spill (spillSize > 0), " +
+              s"got ${op.get.metrics("spillSize").value}")
+          assert(op.get.metrics("numAsOfCursorTasks").value == 0,
+            "Expected the buffered scan, but a task used the cursor scan")
+          assert(isGenerated(query.queryExecution.executedPlan, op.get) ==
+            (codegen && op.get.direction != Nearest),
+            s"Unexpected ASOF join codegen in 
plan:\n${query.queryExecution.executedPlan}")
+        }
+      }
+    }
+  }
+
+  // Whether `join` runs in a codegen stage, and not only in a stage below it.
+  private def isGenerated(plan: SparkPlan, join: SortMergeAsOfJoinExec): 
Boolean = {
+    def inStage(node: SparkPlan): Boolean = node match {
+      case j: SortMergeAsOfJoinExec => j eq join
+      case _: InputAdapter => false
+      case other => other.children.exists(inStage)
     }
-    assert(op.isDefined,
-      s"Expected SortMergeAsOfJoinExec in 
plan:\n${df.queryExecution.executedPlan}")
-    assert(op.get.metrics("spillSize").value > 0,
-      s"Expected the right-side buffer to spill (spillSize > 0), " +
-        s"got ${op.get.metrics("spillSize").value}")
-    assert(op.get.metrics("numAsOfCursorTasks").value == 0,
-      "Expected the buffered scan, but a task used the cursor scan")
+    collect(plan) { case stage: WholeStageCodegenExec if inStage(stage.child) 
=> stage }.nonEmpty
   }
 
   /**
-   * Like checkAnswer, but also asserts that the join used the cursor scan. A 
right group spills
-   * from its second row, so a cursor scan that buffered rows would show a 
spill.
+   * Like checkAnswer, but also asserts that the join used the cursor scan, 
with the ASOF join
+   * codegen off and on. A right group spills from its second row, so a cursor 
scan that
+   * buffered rows would show a spill.
    */
-  private def checkCursorScan(df: classic.DataFrame, expectedAnswer: 
Seq[Row]): Unit = {
-    withSQLConf(
-        SQLConf.SORT_MERGE_JOIN_EXEC_BUFFER_IN_MEMORY_THRESHOLD.key -> "1",
-        SQLConf.SORT_MERGE_JOIN_EXEC_BUFFER_SPILL_THRESHOLD.key -> "1") {
-      checkAnswer(df, expectedAnswer)
+  private def checkCursorScan(df: => classic.DataFrame, expectedAnswer: 
Seq[Row]): Unit = {
+    Seq(false, true).foreach { codegen =>
+      withClue(s"ASOF join codegen $codegen: ") {
+        withSQLConf(
+            SQLConf.ENABLE_SORT_MERGE_AS_OF_JOIN_CODEGEN.key -> 
codegen.toString,
+            SQLConf.SORT_MERGE_JOIN_EXEC_BUFFER_IN_MEMORY_THRESHOLD.key -> "1",
+            SQLConf.SORT_MERGE_JOIN_EXEC_BUFFER_SPILL_THRESHOLD.key -> "1") {
+          val query = df
+          checkAnswer(query, expectedAnswer)
+          val plan = query.queryExecution.executedPlan
+          val op = collectFirst(plan) { case s: SortMergeAsOfJoinExec => s }
+          assert(op.isDefined, s"Expected SortMergeAsOfJoinExec in 
plan:\n$plan")
+          assert(op.get.metrics("numAsOfCursorTasks").value > 0, "Expected the 
cursor scan")
+          assert(op.get.metrics("spillSize").value == 0, "The cursor scan 
buffered right rows")
+          assert(isGenerated(plan, op.get) == codegen, s"Unexpected ASOF join 
codegen in:\n$plan")
+        }
+      }
     }
-    val op = collectFirst(df.queryExecution.executedPlan) {
-      case s: SortMergeAsOfJoinExec => s
+  }
+
+  /**
+   * Like checkAnswer, with the ASOF join codegen off and on. Also asserts 
that the plan has the
+   * join, and that it ran generated exactly when the codegen is on and the 
join is not nearest.
+   */
+  private def checkAnswerCodegenOffAndOn(df: => DataFrame, expectedAnswer: 
Seq[Row]): Unit = {

Review Comment:
   Added a general way to run both ways these tests. It takes the checkToRDD 
parameter.



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

To unsubscribe, e-mail: [email protected]

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