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

Luis Fernando Kauer commented on CALCITE-1906:
----------------------------------------------

Debugging some more about this problem, I found out that what makes the sort be 
pushed is replacing the trait with the collations with a Collations.EMPTY trait:
{code}
convert(sort.getInput(), traitSet.replace(RelCollations.EMPTY))
{code}
The problem is that SortRemoveRule matches and it is applied when there is an 
active RelCollationTraitDef, removing Sort and setting the collation for the 
sort input.  Maybe JdbcTableScan was supposed to use the collation information, 
but it doesn't.
When using the code above to remove collations, SortRemoveRule is not applied 
and that's why the sort gets pushed to the database.
And the EnumerableLimitRule extracts the fetch and limit from Sort and it also 
is not applied when there is no collation field in the Sort. So the code above 
also prevents this rule from being applied.
If it can't assure that the table is sorted the best to do is to remove the 
collations when converting to JdbcSort.


> JdbcSortRule has a bug and it is never chosen
> ---------------------------------------------
>
>                 Key: CALCITE-1906
>                 URL: https://issues.apache.org/jira/browse/CALCITE-1906
>             Project: Calcite
>          Issue Type: Bug
>          Components: jdbc-adapter
>            Reporter: Luis Fernando Kauer
>            Assignee: Julian Hyde
>
> JdbcSortRule tries to push sort and limit operations to the database.
> Currently offset and limit operations are explicitly not pushed to the 
> database (prevented by the rule) but even sort operations end up not being 
> pushed.
> Checking how other adapters deal with this, like Mongo and Cassandra 
> adapters, I realized that the convert function from JdbcSortRule is different 
> from the others.
> Jdbc-adapter:
> {code}
>      if (sort.offset != null || sort.fetch != null) {
>         // Cannot implement "OFFSET n FETCH n" currently.
>         return null;
>       }
>       final RelTraitSet traitSet = sort.getTraitSet().replace(out);
>       return new JdbcSort(rel.getCluster(), traitSet,
>           convert(sort.getInput(), traitSet), sort.getCollation());
> {code}
> mongodb-adapter:
> {code}
>       final RelTraitSet traitSet =
>           sort.getTraitSet().replace(out)
>               .replace(sort.getCollation());
>       return new MongoSort(rel.getCluster(), traitSet,
>           convert(sort.getInput(), traitSet.replace(RelCollations.EMPTY)),
>           sort.getCollation(), sort.offset, sort.fetch);
> {code}
> By fixing JdbcSortRule so that it is just like those others and by removing 
> the code that prevented the rule to match when limit or offset are used seems 
> to solve the problem and JdbcSortRule now is being applied and both sort and 
> limit are being pushed to the database.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to