[ 
https://issues.apache.org/jira/browse/CALCITE-7104?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18009684#comment-18009684
 ] 

Julian Hyde commented on CALCITE-7104:
--------------------------------------

I suggest that you use metadata (i.e. some information available via 
{{RelMetadataQuery}}).

We already use metadata a lot. For example, suppose you want to know, in a 
{{Join}}, whether an integer column is always positive. Do you write a rule 
that matches a Join on top of a {{Filter}}? No, you ask for a 
{{RelMdPredicate}} on that column. That predicate would be created by a 
{{Filter}} directly under the 
{{Join}}, but it might be created by a {{Filter}} seven layers down, or if the 
column is created from a literal 100 in a {{Project}}.

A basic approach is to look for constraints "x = y" or "x is not distinct from 
y". I don't recall whether there is any library code that makes this easier. 
I'd look through {{RelOptRulesTest}} for other rules that look for equivalent 
columns and see what they do. 

Maybe functional dependencies are of use. If y is functionally dependent on x 
then "order by x, y" can be simplified to "order by x". Of course that applies 
if x is always equal to y, but it also applies to

{code}select *
from emp as e
  join dept as d on e.deptno = d.deptno
order by e.deptno, d.dname
{code}

because {{d.dname}} is functionally dependent on both {{e.deptno}} and 
{{d.deptno}}.

> Remove duplicate sort keys
> --------------------------
>
>                 Key: CALCITE-7104
>                 URL: https://issues.apache.org/jira/browse/CALCITE-7104
>             Project: Calcite
>          Issue Type: Improvement
>            Reporter: weihua zhang
>            Priority: Major
>              Labels: pull-request-available
>
> {code:sql}
> select d1 from  (select deptno as d1, deptno as d2 from dept ) as tmp order 
> by d1, d2;
> {code}
> {noformat}
> LogicalProject(D1=[$0]), id = 2284
>   LogicalSort(sort0=[$0], sort1=[$1], dir0=[ASC], dir1=[ASC]), id = 2283
>     LogicalProject(D1=[$0], D2=[$0]), id = 2282
>       LogicalTableScan(table=[[default, dept]]), id = 2278
> {noformat}
> LogicalSort can change to:
> {noformat}
> LogicalProject(D1=[$0]), id = 2284
>   LogicalSort(sort0=[$0], dir0=[ASC]), id = 2283
>     LogicalProject(D1=[$0], D2=[$0]), id = 2282
>       LogicalTableScan(table=[[default, dept]]), id = 2278
> {noformat}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to