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

Thomas Rebele commented on CALCITE-5473:
----------------------------------------

Potentially this could avoid the sort in the following example query (in 
combination with CALCITE-5472):
{noformat}
0: jdbc:calcite:example/csv/src/test/resource> explain plan for select a,b+10 
from (values (1,2,3), (2, 3, 4), (3,10,10) ) T(a,b,c) ORDER BY a,b+10;
+--------------------------------------------------------------------------------------------------+
|                                               PLAN                            
                   |
+--------------------------------------------------------------------------------------------------+
| EnumerableSort(sort0=[$0], sort1=[$1], dir0=[ASC], dir1=[ASC])
  EnumerableCalc(expr#0..2=[{inputs}], expr#3=[10], expr#4=[+($t1, $t3)], 
A=[$t0], EXPR$1=[$t4])
    EnumerableValues(tuples=[[{ 1, 2, 3 }, { 2, 3, 4 }, { 3, 10, 10 }]])
 |
+--------------------------------------------------------------------------------------------------+
1 row selected (0.015 seconds)
0: jdbc:calcite:example/csv/src/test/resource> explain plan for select a,b+10 
from (values (1,2,3), (2, 3, 4), (3,10,10) ) T(a,b,c) ORDER BY a,b;
+---------------------------------------------------------------------------------------------------------+
|                                                  PLAN                         
                          |
+---------------------------------------------------------------------------------------------------------+
| EnumerableCalc(expr#0..2=[{inputs}], expr#3=[10], expr#4=[+($t1, $t3)], 
A=[$t0], EXPR$1=[$t4], B=[$t1])
  EnumerableValues(tuples=[[{ 1, 2, 3 }, { 2, 3, 4 }, { 3, 10, 10 }]])
 |
+---------------------------------------------------------------------------------------------------------+
1 row selected (0.017 seconds) {noformat}

> Keep more collations in RelMdCollation#project for monotonic RexCalls
> ---------------------------------------------------------------------
>
>                 Key: CALCITE-5473
>                 URL: https://issues.apache.org/jira/browse/CALCITE-5473
>             Project: Calcite
>          Issue Type: Improvement
>            Reporter: Thomas Rebele
>            Priority: Major
>
> Some RexCalls do not affect the collation, i.e., $0+10 has the same collation 
> as $0 (if there are now integer overflows). This is already implemented in 
> {{{}SqlMonotonicBinaryOperator#getMonotonicity{}}}.
> If we have an input with collation [0,1], and a {{{}Calc(a=$0+10, b=$1){}}}, 
> then the output collation would be [0, 1] as well. This is the case for 
> STRICTLY_INCREASING or STRICTLY_DECREASING calls (though the direction of the 
> field collation might need to be adapted).
> However, if the Calc was {{{}Calc(a=FLOOR($0), b=$1){}}}, then the output 
> collation would be just [0], as the sortedness of b is not guaranteed. This 
> happens for example with rows (0.2, 50) and (0.3, 20) and (0.4, 30), in that 
> order. So for INCREASING, DECREASING, or CONSTANT the collation would be 
> shortened.
> The right place to implement this would probably be 
> {{{}RelMdCollation#project{}}}. The {{fieldCollationsForRexCalls}} loop would 
> need to stay, because some calls may introduce a collation, regardless of the 
> input. E.g., {{SELECT CURRENT_DATE FROM some_random_table}} is always sorted.



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

Reply via email to