@@ -64,7 +64,7 @@ abstract class RemoveRedundantSortsSuiteBase
64
64
withTempView(" t1" , " t2" ) {
65
65
spark.range(1000 ).select(' id as " key" ).createOrReplaceTempView(" t1" )
66
66
spark.range(1000 ).select(' id as " key" ).createOrReplaceTempView(" t2" )
67
-
67
+
68
68
val queryTemplate = """
69
69
|SELECT /*+ BROADCAST(%s) */ t1.key FROM
70
70
| (SELECT key FROM t1 WHERE key > 10 ORDER BY key DESC LIMIT 10) t1
@@ -74,17 +74,25 @@ abstract class RemoveRedundantSortsSuiteBase
74
74
|ORDER BY %s
75
75
""" .stripMargin
76
76
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 )
88
96
}
89
97
}
90
98
@@ -104,7 +112,8 @@ abstract class RemoveRedundantSortsSuiteBase
104
112
val queryAsc = query + " ASC"
105
113
checkSorts(queryAsc, 2 , 3 )
106
114
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.
108
117
val queryDesc = query + " DESC"
109
118
checkSorts(queryDesc, 3 , 3 )
110
119
}
0 commit comments