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



##########
File path: 
sql/core/src/test/scala/org/apache/spark/sql/execution/exchange/ValidateRequirementsSuite.scala
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.exchange
+
+import org.apache.spark.sql.catalyst.expressions.{Ascending, SortOrder}
+import org.apache.spark.sql.catalyst.plans.{Inner, PlanTest}
+import org.apache.spark.sql.catalyst.plans.physical.HashPartitioning
+import org.apache.spark.sql.execution.SortExec
+import org.apache.spark.sql.execution.joins.SortMergeJoinExec
+import org.apache.spark.sql.test.SharedSparkSession
+
+class ValidateRequirementsSuite extends PlanTest with SharedSparkSession {
+
+  import testImplicits._
+
+  private def testValidate(
+      joinKeyIndices: Seq[Int],
+      leftPartitionKeyIndices: Seq[Int],
+      rightPartitionKeyIndices: Seq[Int],
+      leftPartitionNum: Int,
+      rightPartitionNum: Int,
+      success: Boolean): Unit = {
+    val table1 =
+      spark.range(10).select('id + 1 as 'a1, 'id + 2 as 'b1, 'id + 3 as 'c1)
+        .queryExecution.executedPlan
+    val table2 =
+      spark.range(10).select('id + 1 as 'a2, 'id + 2 as 'b2, 'id + 3 as 'c2)
+        .queryExecution.executedPlan
+
+    val leftKeys = joinKeyIndices.map(table1.output)
+    val rightKeys = joinKeyIndices.map(table2.output)
+    val leftPartitioning =
+      HashPartitioning(leftPartitionKeyIndices.map(table1.output), 
leftPartitionNum)
+    val rightPartitioning =
+      HashPartitioning(rightPartitionKeyIndices.map(table2.output), 
rightPartitionNum)
+    val left =
+      SortExec(leftKeys.map(SortOrder(_, Ascending)), false,
+        ShuffleExchangeExec(leftPartitioning, table1))
+    val right =
+      SortExec(rightKeys.map(SortOrder(_, Ascending)), false,
+        ShuffleExchangeExec(rightPartitioning, table2))
+
+    val plan = SortMergeJoinExec(leftKeys, rightKeys, Inner, None, left, right)
+    assert(ValidateRequirements.validate(plan) == success, plan)
+  }
+
+  test("SMJ requirements satisfied with partial partition key") {
+    testValidate(Seq(0, 1, 2), Seq(1), Seq(1), 5, 5, true)
+  }
+
+  test("SMJ requirements satisfied with different partition key order") {
+    testValidate(Seq(0, 1, 2), Seq(2, 0, 1), Seq(2, 0, 1), 5, 5, true)
+  }
+
+  test("SMJ requirements not satisfied with unequal partition key order") {
+    testValidate(Seq(0, 1, 2), Seq(1, 0), Seq(0, 1), 5, 5, false)
+  }
+
+  test("SMJ requirements not satisfied with unequal partition key length") {
+    testValidate(Seq(0, 1, 2), Seq(1), Seq(1, 2), 5, 5, false)
+  }
+
+  test("SMJ requirements not satisfied with partition key missing from join 
key") {
+    testValidate(Seq(1, 2), Seq(1, 0), Seq(1, 0), 5, 5, false)
+  }
+
+  test("SMJ requirements not satisfied with unequal partition number") {

Review comment:
       It would be nice to test for `HashPartitioning(1)` and `AllTuples`. I 
remember we had quite some bugs with `AllTuples` before. But this comment is 
non-blocking.




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