[ 
https://issues.apache.org/jira/browse/CALCITE-3865?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Haisheng Yuan updated CALCITE-3865:
-----------------------------------
    Description: 
CALCITE-1148 introduced the following change to RelCollationTraitDef to fix 
RelTrait conversion bug, but it is just hiding the underlying issue and adding 
redundant and unnecessary check to planner.

{code:java}
@Override public boolean canConvert(RelOptPlanner planner,
      RelCollation fromTrait, RelCollation toTrait, RelNode fromRel) {
    // Returns true only if we can convert.  In this case, we can only convert
    // if the fromTrait (the input) has fields that the toTrait wants to sort.
    for (RelFieldCollation field : toTrait.getFieldCollations()) {
      int index = field.getFieldIndex();
      if (index >= fromRel.getRowType().getFieldCount()) {
        return false;
      }
    }
    return true;
  }
{code}


The root cause is that logical operators, especially LogicalSort can have 
traits, which is a bad design decision, and {{AggregateReduceFunctionsRule}} 
fails to adjust the column mapping in RelTraitSet. The newly created 
{{LogicalProject}} has collation on column 5 (it just copy its input's 
RelTraitSet blindly), but it only has 2 columns.
 !image-2020-03-19-16-44-25-621.png! 


  was:
CALCITE-1148 introduced the following change to RelCollationTraitDef to fix 
RelTrait conversion bug, but it is just hiding the underlying issue and adding 
redundant and unnecessary check to planner.
 !image-2020-03-19-16-47-21-805.png! 

The root cause is that logical operators, especially LogicalSort can have 
traits, which is a bad design decision, and {{AggregateReduceFunctionsRule}} 
fails to adjust the column mapping in RelTraitSet. The newly created 
{{LogicalProject}} has collation on column 5 (it just copy its input's 
RelTraitSet blindly), but it only has 2 columns.
 !image-2020-03-19-16-44-25-621.png! 



> RelCollationTraitDef.canConvert should always return true
> ---------------------------------------------------------
>
>                 Key: CALCITE-3865
>                 URL: https://issues.apache.org/jira/browse/CALCITE-3865
>             Project: Calcite
>          Issue Type: Improvement
>          Components: core
>            Reporter: Haisheng Yuan
>            Priority: Major
>         Attachments: image-2020-03-19-16-44-25-621.png, 
> image-2020-03-19-16-47-21-805.png
>
>
> CALCITE-1148 introduced the following change to RelCollationTraitDef to fix 
> RelTrait conversion bug, but it is just hiding the underlying issue and 
> adding redundant and unnecessary check to planner.
> {code:java}
> @Override public boolean canConvert(RelOptPlanner planner,
>       RelCollation fromTrait, RelCollation toTrait, RelNode fromRel) {
>     // Returns true only if we can convert.  In this case, we can only convert
>     // if the fromTrait (the input) has fields that the toTrait wants to sort.
>     for (RelFieldCollation field : toTrait.getFieldCollations()) {
>       int index = field.getFieldIndex();
>       if (index >= fromRel.getRowType().getFieldCount()) {
>         return false;
>       }
>     }
>     return true;
>   }
> {code}
> The root cause is that logical operators, especially LogicalSort can have 
> traits, which is a bad design decision, and {{AggregateReduceFunctionsRule}} 
> fails to adjust the column mapping in RelTraitSet. The newly created 
> {{LogicalProject}} has collation on column 5 (it just copy its input's 
> RelTraitSet blindly), but it only has 2 columns.
>  !image-2020-03-19-16-44-25-621.png! 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to