spark-test commented on code in PR #60675:
URL: https://github.com/apache/spark/pull/60675#discussion_r4646529940


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/joins/FullOuterJoinOrderingSuite.scala:
##########
@@ -0,0 +1,424 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.joins
+
+import org.apache.spark.sql.{DataFrame, QueryTest, Row}
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.plans.FullOuter
+import org.apache.spark.sql.execution.{LocalTableScanExec, SortExec, 
SparkPlan, WholeStageCodegenExec}
+import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanHelper, 
AQEShuffleReadExec}
+import org.apache.spark.sql.execution.aggregate.SortAggregateExec
+import org.apache.spark.sql.execution.exchange.ShuffleExchangeExec
+import org.apache.spark.sql.execution.window.WindowExec
+import org.apache.spark.sql.functions.{coalesce, col}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.test.SharedSparkSession
+import org.apache.spark.sql.types.{IntegerType, StructField, StructType}
+
+class FullOuterJoinOrderingSuite
+  extends QueryTest with SharedSparkSession with AdaptiveSparkPlanHelper {
+
+  override protected def sparkConf = super.sparkConf
+    .set(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key, "-1")
+    .set(SQLConf.ADAPTIVE_EXECUTION_ENABLED.key, "false")
+    .set(SQLConf.PREFER_SORTMERGEJOIN.key, "true")
+    .set(SQLConf.SHUFFLE_PARTITIONS.key, "5")
+    .set(SQLConf.SHUFFLE_SPREAD_NULL_JOIN_KEYS_ENABLED.key, "false")
+    .set(SQLConf.ENABLE_FULL_OUTER_SORT_MERGE_JOIN_CODEGEN.key, "true")
+    .set(SQLConf.ADAPTIVE_MAX_SHUFFLE_HASH_JOIN_LOCAL_MAP_THRESHOLD.key, "0")
+
+  testWithWholeStageCodegenOnAndOff("single nullable keys retain merged 
ordering") { codegen =>
+    withSingleKeys(
+        Seq(Row(null), Row(1), Row(3), Row(3)),
+        Seq(Row(null), Row(2), Row(3), Row(4))) {
+      val df = sql("SELECT /*+ MERGE(a, b) */ k FROM a FULL OUTER JOIN b USING 
(k)")
+      checkOrdered(df, Seq(Row(null), Row(null), Row(1), Row(2), Row(3), 
Row(3), Row(4)), "k")
+      val plan = df.queryExecution.executedPlan
+      val generatedJoins = collect(plan) {
+        case w: WholeStageCodegenExec if 
w.exists(_.isInstanceOf[SortMergeJoinExec]) => w

Review Comment:
   **Nit (P3):** `w.exists(_.isInstanceOf[SortMergeJoinExec])` searches the 
whole stage, and `TreeNode.exists` also descends into `InputAdapter`. If the 
FULL OUTER join is not code-generated, for example with 
`spark.sql.codegen.join.fullOuterSortMergeJoin.enabled=false`, 
`CollapseCodegenStages` wraps it as `InputAdapter(SortMergeJoin(...))` under 
the generated `Project` of this USING query. The outer `WholeStageCodegenExec` 
then still matches, so `generatedJoins.nonEmpty == codegen.toBoolean` holds 
while the "(whole-stage-codegen on)" variant runs the interpreted 
`SortMergeFullOuterJoinScanner`. The suite pins that conf to true, so the 
generated merge does run today, but this is the suite's only check of which 
merge ran, and it cannot catch that fallback. Matching the stage shape closes 
the gap. One option is `case w @ WholeStageCodegenExec(ProjectExec(_, _: 
SortMergeJoinExec)) => w` with `ProjectExec` imported, the pattern 
WholeStageCodegenSuite uses for left semi and left anti sort
  merge joins. Another is a search that stops at `InputAdapter`.
   
   **Verification:**
   - **Regression:** With whole-stage codegen on and 
spark.sql.codegen.join.fullOuterSortMergeJoin.enabled set to false, the 
tightened mode check reports no generated FULL OUTER join for the 
single-nullable-key query, showing that it now fails for the interpreted 
fallback it is meant to catch.
   - **Behavior:** With the suite's own settings, both whole-stage codegen 
variants of the single-nullable-key ordering test pass, and the mode check 
finds the FULL OUTER join compiled into a generated stage exactly when 
whole-stage codegen is on.



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