Description
The following SQL performs sort twice, however inner sort can be eliminated
select * from ( select * from "emps" order by "emps"."deptno" ) order by 1 desc
The same goes for (window calculation will sort on its own)
select row_number() over (order by "emps"."deptno") from ( select * from "emps" order by "emps"."deptno" desc )
The same goes for SetOp (union, minus):
select * from ( select * from "emps" order by "emps"."deptno" ) union select * from ( select * from "emps" order by "emps"."deptno" desc )
There might be other cases like that (e.g. Aggregate, Join, Exchange, SortExchange)
Attachments
Issue Links
- causes
-
CALCITE-2978 sorting not applied in subqueries
- Closed
-
CALCITE-4160 Add configuration to retain ORDER BY in sub-query
- Closed
- is related to
-
CALCITE-4886 When converting SQL to RelNode, SqlOrderBy is missing in sub-query that contains SqlSetOperator
- Closed
-
HIVE-6348 Order by/Sort by in subquery
- Closed
- relates to
-
CALCITE-3664 Sort in subquery is lost when converting SqlNode to Relnode
- Closed