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

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

Also, for the case where the order key has more than one value, I inferred a 
conversion.

For case:
{code:java}
SELECT dname FROM dept WHERE 2000 > (SELECT sal FROM "scott".emp where 
dept.deptno = emp.deptno ORDER BY year(hiredate), sal limit 1); {code}
We optimize for

 
{code:java}
select deptno from dept join (SELECT sal1 from (SELECT first_value(sal) over 
(partition by emp.deptno order by year(hiredate), emp.sal) as sal1, deptno) 
group by deptno where sal1 < 1000) {code}
 Rewrite logical:

{code:java}
// Rewrite logic:
//
// If sorted without offset and fetch = 1, rewrite the sort to be
//   Aggregate(group=(corVar), agg=[any_value(field))
//     project(first_value(field) over (partition by corVar order by (sort 
collation)))
//       originInput{code}
This seems to be a generic optimization for decorrelate containing sorting. But 
unfortunately, I haven't seen proof of a similar conversion anywhere.

 

> 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();
> }{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();
> }{code}



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

Reply via email to