@@ -64,7 +64,7 @@ abstract class RemoveRedundantSortsSuiteBase
6464 withTempView(" t1" , " t2" ) {
6565 spark.range(1000 ).select(' id as " key" ).createOrReplaceTempView(" t1" )
6666 spark.range(1000 ).select(' id as " key" ).createOrReplaceTempView(" t2" )
67-
67+
6868 val queryTemplate = """
6969 |SELECT /*+ BROADCAST(%s) */ t1.key FROM
7070 | (SELECT key FROM t1 WHERE key > 10 ORDER BY key DESC LIMIT 10) t1
@@ -74,17 +74,25 @@ abstract class RemoveRedundantSortsSuiteBase
7474 |ORDER BY %s
7575 """ .stripMargin
7676
77- val innerJoinAsc = queryTemplate.format(" t1" , " t2.key ASC" )
78- checkSorts(innerJoinAsc, 1 , 1 )
79-
80- val innerJoinDesc = queryTemplate.format(" t1" , " t2.key DESC" )
81- checkSorts(innerJoinDesc, 0 , 1 )
82-
83- val innerJoinDesc1 = queryTemplate.format(" t1" , " t1.key DESC" )
84- checkSorts(innerJoinDesc1, 1 , 1 )
85-
86- val leftOuterJoinDesc = queryTemplate.format(" t2" , " t1.key DESC" )
87- checkSorts(leftOuterJoinDesc, 0 , 1 )
77+ // No sort should be removed since the stream side (t2) order DESC
78+ // does not satisfy the required sort order ASC.
79+ val buildLeftOrderByRightAsc = queryTemplate.format(" t1" , " t2.key ASC" )
80+ checkSorts(buildLeftOrderByRightAsc, 1 , 1 )
81+
82+ // The top sort node should be removed since the stream side (t2) order DESC already
83+ // satisfies the required sort order DESC.
84+ val buildLeftOrderByRightDesc = queryTemplate.format(" t1" , " t2.key DESC" )
85+ checkSorts(buildLeftOrderByRightDesc, 0 , 1 )
86+
87+ // No sort should be removed since the sort ordering from broadcast-hash join is based
88+ // on the stream side (t2) and the required sort order is from t1.
89+ val buildLeftOrderByLeftDesc = queryTemplate.format(" t1" , " t1.key DESC" )
90+ checkSorts(buildLeftOrderByLeftDesc, 1 , 1 )
91+
92+ // The top sort node should be removed since the stream side (t1) order DESC already
93+ // satisfies the required sort order DESC.
94+ val buildRightOrderByLeftDesc = queryTemplate.format(" t2" , " t1.key DESC" )
95+ checkSorts(buildRightOrderByLeftDesc, 0 , 1 )
8896 }
8997 }
9098
@@ -104,7 +112,8 @@ abstract class RemoveRedundantSortsSuiteBase
104112 val queryAsc = query + " ASC"
105113 checkSorts(queryAsc, 2 , 3 )
106114
107- // Top level sort should only be eliminated if it's order is descending with SMJ.
115+ // The top level sort should not be removed since the child output ordering is ASC and
116+ // the required ordering is DESC.
108117 val queryDesc = query + " DESC"
109118 checkSorts(queryDesc, 3 , 3 )
110119 }
0 commit comments