You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@calcite.apache.org by "Julian Hyde (JIRA)" <ji...@apache.org> on 2019/01/31 02:51:00 UTC
[jira] [Commented] (CALCITE-2798) Optimizer should remove ORDER BY
in sub-query, provided it has no LIMIT or OFFSET
[ https://issues.apache.org/jira/browse/CALCITE-2798?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16756806#comment-16756806 ]
Julian Hyde commented on CALCITE-2798:
--------------------------------------
Reviewing now.
Normally I'd prefer to do these kinds of transformations using planner rules, but in this case it is hard for a planner rule to know whether the sorted output of a RelNode is needed by its consumer. So SqlToRelConverter is indeed the best place to do it.
I believe that if you define a view with an ORDER BY clause and use that view in a query, the ORDER BY clause will not be removed. For example, in the following, the first SELECT query will return sorted output but the second will not. This behavior is OK.
{code}
CREATE VIEW EmpsByAge AS SELECT * FROM Emps ORDER BY sal DESC;
SELECT * FROM EmpsByAge; -- output is sorted
SELECT * FROM (SELECT * FROM Emps ORDER BY sal DESC); -- output is not sorted
{code}
> Optimizer should remove ORDER BY in sub-query, provided it has no LIMIT or OFFSET
> ---------------------------------------------------------------------------------
>
> Key: CALCITE-2798
> URL: https://issues.apache.org/jira/browse/CALCITE-2798
> Project: Calcite
> Issue Type: Improvement
> Components: core
> Affects Versions: 1.18.0
> Reporter: Vladimir Sitnikov
> Assignee: Julian Hyde
> Priority: Major
>
> The following SQL performs sort twice, however inner sort can be eliminated
> {code}select * from (
> select * from "emps"
> order by "emps"."deptno"
> ) order by 1 desc{code}
> The same goes for (window calculation will sort on its own)
> {code}select row_number() over (order by "emps"."deptno") from (
> select * from "emps"
> order by "emps"."deptno" desc
> ){code}
> The same goes for SetOp (union, minus):
> {code}select * from (
> select * from "emps"
> order by "emps"."deptno"
> ) union select * from (
> select * from "emps"
> order by "emps"."deptno" desc
> ){code}
> There might be other cases like that (e.g. Aggregate, Join, Exchange, SortExchange)
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)