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


##########
sql/core/src/test/scala/org/apache/spark/sql/AsOfJoinSortMergeSQLSuite.scala:
##########
@@ -160,16 +162,67 @@ class AsOfJoinSortMergeSQLSuite extends QueryTest
     }
   }
 
-  /** Names of the columns that the scan of `table` filters on. */
-  private def scanFilterColumns(df: DataFrame, table: String): Set[String] = {
+  /** The data filters that the scan of `table` runs. */
+  private def scanDataFilters(df: DataFrame, table: String): Seq[Expression] = 
{
     val plan = df.queryExecution.executedPlan
     val scans = collect(plan) {
       case scan: FileSourceScanExec if scan.tableIdentifier.exists(_.table == 
table) => scan
     }
     assert(scans.length == 1, s"Expected one scan of $table in plan:\n$plan")
-    scans.head.dataFilters.flatMap(_.references.map(_.name)).toSet
+    scans.head.dataFilters
+  }
+
+  /** Names of the columns that the scan of `table` filters on. */
+  private def scanFilterColumns(df: DataFrame, table: String): Set[String] =
+    scanDataFilters(df, table).flatMap(_.references.map(_.name)).toSet
+
+  /** The joins below the left input and below the right input of the as-of 
join of `df`. */
+  private def joinsBelowAsOfInputs(df: DataFrame): (Seq[BaseJoinExec], 
Seq[BaseJoinExec]) = {
+    val plan = df.queryExecution.executedPlan
+    val asOfJoins = collect(plan) { case j: SortMergeAsOfJoinExec => j }
+    assert(asOfJoins.length == 1, s"Expected one SortMergeAsOfJoinExec in 
plan:\n$plan")
+    def joinsBelow(input: SparkPlan): Seq[BaseJoinExec] =
+      collect(input) { case j: BaseJoinExec => j }
+    (joinsBelow(asOfJoins.head.left), joinsBelow(asOfJoins.head.right))
   }
 
+  /**
+   * Checks the rows of `query` with InferFiltersFromConstraints excluded and 
with it on. With it
+   * on, asserts one join below the as-of input with the subquery and none 
below the other input.
+   * Returns the query with the rule on.
+   */
+  private def checkOneSubqueryJoin(
+      query: String, onLeftInput: Boolean, expected: Seq[Row]): DataFrame = {
+    withSQLConf(SQLConf.OPTIMIZER_EXCLUDED_RULES.key -> 
InferFiltersFromConstraints.ruleName) {
+      checkSortMergeAsOf(sql(query), expected)
+    }
+    val df = sql(query)
+    checkSortMergeAsOf(df, expected)
+    val (leftJoins, rightJoins) = joinsBelowAsOfInputs(df)
+    val (subqueryJoins, otherJoins) =
+      if (onLeftInput) (leftJoins, rightJoins) else (rightJoins, leftJoins)
+    assert(subqueryJoins.size == 1 && otherJoins.isEmpty,
+      s"Expected one join below only the input with the subquery in plan:\n" +
+        df.queryExecution.executedPlan)
+    df
+  }
+
+  /** Whether the scan of `table` keeps only the rows where string `column` 
equals `value`. */
+  private def scanFiltersOnEquality(
+      df: DataFrame, table: String, column: String, value: String): Boolean =
+    scanDataFilters(df, table).exists {
+      case EqualTo(a: Attribute, Literal(v, _: StringType)) =>
+        a.name == column && v.toString == value
+      case _ => false
+    }
+
+  /**
+   * Runs `f` without InferFiltersFromConstraints. It would drop the right 
rows with a NULL ON
+   * key before the join, so a test of how the join itself treats NULL keys 
would not see them.
+   */
+  private def withNullOnKeysKept(f: => Unit): Unit =

Review Comment:
   Done, the NULL-key SQL tests and the spill test now check that every input 
row reaches the as-of join.



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