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

suibianwanwan commented on CALCITE-6652:
----------------------------------------

Sorry, there seems to be something wrong with the above example SQL.  

I prefer relational algebra. and the following plan is a deterministic 
optimization
{code:java}
LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{0}])
    LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
     LogicalSort(sort0=[$0], dir0=[ASC], fetch=[1])
        LogicalProject(SAL=[$5])
            LogicalFilter(condition=[=($cor0.DEPTNO, $7)])
                LogicalTableScan(table=[[CATALOG, SALES, EMP]])


LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{0}])
    LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
     LogicalAggregate(SAL=[max(0)])
        LogicalProject(SAL=[$5])
            LogicalFilter(condition=[=($cor0.DEPTNO, $7)])
                LogicalTableScan(table=[[CATALOG, SALES, EMP]])
{code}
But not if there is no correlate. Unless there are some constraints. II think 
the optimization above is also due to the constraints imposed by 
LogicalCorrelate itself.
{code:java}
LogicalSort(sort0=[$0], dir0=[ASC], fetch=[1])
   LogicalProject(SAL=[$5])
          LogicalTableScan(table=[[CATALOG, SALES, EMP]])


LogicalAggregate(SAL=[max(0)])
   LogicalProject(SAL=[$5])
         LogicalTableScan(table=[[CATALOG, SALES, EMP]])
{code}

> RelDecorrelator can't decorrelate query with limit 1
> ----------------------------------------------------
>
>                 Key: CALCITE-6652
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6652
>             Project: Calcite
>          Issue Type: Bug
>            Reporter: suibianwanwan
>            Assignee: suibianwanwan
>            Priority: Major
>
> The following query can't decorrelate by RelDecorrelate:
> {code:java}
> @Test void testDecorrelateFetchOne1() {
>   final String query = "SELECT deptno FROM dept WHERE\n" +
>       "1000 > (SELECT sal FROM emp WHERE dept.deptno = emp.deptno order by 
> emp.sal limit 1)";
>   sql(query).withRule(
>           CoreRules.FILTER_SUB_QUERY_TO_CORRELATE)
>       .withLateDecorrelate(true)
>       .check();
> } 
> @Test void testDecorrelateFetchOne2() {
>   final String query = "SELECT deptno, (SELECT sal FROM emp where dept.deptno 
> = emp.deptno order by emp.sal limit 1) FROM dept";
>   sql(query).withRule(
>           CoreRules.PROJECT_SUB_QUERY_TO_CORRELATE)
>       .withLateDecorrelate(true)
>       .check();
> }
> @Test void testDecorrelateFetchOne3() {
>   final String query = "SELECT deptno, (SELECT sal FROM emp where dept.deptno 
> = emp.deptno order by emp.sal desc limit 1) FROM dept";
>   sql(query).withRule(
>           CoreRules.PROJECT_SUB_QUERY_TO_CORRELATE)
>       .withLateDecorrelate(true)
>       .check();
> }{code}
> is equivalent to the following query, and the following query can be 
> decorrelated into a join, so we can do some appropriate conversions in 
> RelDecorrelate:
> {code:java}
> @Test void testDecorrelateFetchOne1() {
>   final String query = "SELECT deptno FROM dept WHERE\n" +
>       "1000 > (SELECT min(sal) FROM emp WHERE dept.deptno = emp.deptno)";
>   sql(query).withRule(
>           CoreRules.FILTER_SUB_QUERY_TO_CORRELATE)
>       .withLateDecorrelate(true)
>       .check();
> } 
> @Test void testDecorrelateFetchOne2() {
>   final String query = "SELECT deptno, (SELECT SINGLE_VALUE(sal) FROM emp 
> where dept.deptno = emp.deptno) FROM dept";
>   sql(query).withRule(
>           CoreRules.PROJECT_SUB_QUERY_TO_CORRELATE)
>       .withLateDecorrelate(true)
>       .check();
> }
> @Test void testDecorrelateFetchOne3() {
>   final String query = "SELECT deptno, (SELECT max(sal) FROM emp where 
> dept.deptno = emp.deptno) FROM dept";
>   sql(query).withRule(
>           CoreRules.PROJECT_SUB_QUERY_TO_CORRELATE)
>       .withLateDecorrelate(true)
>       .check();
> }{code}



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

Reply via email to