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

Xurenhe commented on CALCITE-4800:
----------------------------------

Handle for Sort's mv-match
 * Equal collation's info, we just check offset and fetch. Query could be 
rewritten by mv, when mv's data-range must be larger than query's, such as:

{code:java}
--query: 
Sort(collation: [1], offset: 3)
  Calc(program: (expr#0..4=[{inputs}], name=[$t2], deptno=[$t1]))
    Scan(table: [hr, emps])
--mv:
Sort(collation: [1], offset: 2)
  Calc(program: (expr#0..4=[{inputs}], name=[$t2], deptno=[$t1]))
    Scan(table: [hr, emps])
--rewrite:
Sort(collation: [], offset: 1)
  Sort(collation: [1], offset: 2)
    Calc(program: (expr#0..4=[{inputs}], name=[$t2], deptno=[$t1]))
      Scan(table: [hr, emps])
--replace:
Sort(collation: [], offset: 1)
  Scan(table: [mv]) {code}
 * No-equal collation's info, we could build a new sort above mv's, which 
collation must be query', such as:

{code:java}
--query: 
Sort(collation: [1 DESC])
  Calc(program: (expr#0..4=[{inputs}], name=[$t2], deptno=[$t1]))
    Scan(table: [hr, emps])
--mv:
Sort(collation: [1])
  Calc(program: (expr#0..4=[{inputs}], name=[$t2], deptno=[$t1]))
    Scan(table: [hr, emps])
--rewrite:
Sort(collation: [1 DESC])
  Sort(collation: [1])
    Calc(program: (expr#0..4=[{inputs}], name=[$t2], deptno=[$t1]))
      Scan(table: [hr, emps])
--replace:
Sort(collation: [1 DESC])
  Scan(table: [mv]) {code}
 

PR: [https://github.com/apache/calcite/pull/2702]

[~xzh_dz] Hi please review this pr, if you have time.
Thanks

> Add SortToSortUnifyRule in SubstitutionVisitor
> ----------------------------------------------
>
>                 Key: CALCITE-4800
>                 URL: https://issues.apache.org/jira/browse/CALCITE-4800
>             Project: Calcite
>          Issue Type: Improvement
>            Reporter: xzh_dz
>            Assignee: Xurenhe
>            Priority: Major
>              Labels: pull-request-available
>          Time Spent: 40m
>  Remaining Estimate: 0h
>
> In my project, there are more usage scenarios for materialized view 
> recognition, and the `Sort Operator` can help sort data sets.But it has not 
> been supported yet.A `Sort Operator` failed example can be reproduced as 
> follows.
> {code:java}
> // code placeholder
> org.apache.calcite.test.MaterializedViewSubstitutionVisitorTest
> @Test void testSortToSort() {
>   final String mv = ""
>       + "select \"name\", \"deptno\"\n"
>       + "from \"emps\"\n"
>       + "order by \"deptno\"";
>   final String query = ""
>       + "select \"name\", \"deptno\"\n"
>       + "from \"emps\"\n"
>       + "order by \"deptno\" desc";
>   sql(mv, query).withChecker(
>       resultContains(""
>           + "EnumerableTableScan(table=[[hr, MV0]])")).ok();
> }
> {code}
> Exception:
> {code:java}
> // code placeholder
> Materialized view failed to be matched by optimized results:Materialized view 
> failed to be matched by optimized results:
> java.lang.AssertionError: Materialized view failed to be matched by optimized 
> results:
>  at 
> org.apache.calcite.test.AbstractMaterializedViewTest.checkMaterialize(AbstractMaterializedViewTest.java:116)
>  at 
> org.apache.calcite.test.AbstractMaterializedViewTest.access$000(AbstractMaterializedViewTest.java:67)
>  at 
> org.apache.calcite.test.AbstractMaterializedViewTest$Sql.ok(AbstractMaterializedViewTest.java:229)
>  at java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:627) 
> at 
> org.apache.calcite.util.ImmutableBeans.lambda$makeDef$3(ImmutableBeans.java:299)
> {code}
>  
>  



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to