[ https://issues.apache.org/jira/browse/CALCITE-4333?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17248015#comment-17248015 ]
Ruben Q L edited comment on CALCITE-4333 at 12/11/20, 4:30 PM: --------------------------------------------------------------- [~danny0405] unfortunately, the proposed fix is insufficient, as soon as the LogicalSort is not at the top of the plan (e.g. it is the input of a final LogicalProject), it is not decorrelated (and it could/should be). Example: {code} @Test void testSortLimitWithCorrelateInput2() { final String sql = "" + "SELECT ename||deptno FROM\n" + "(SELECT deptno, ename\n" + " FROM\n" + " (SELECT DISTINCT deptno FROM emp) t1,\n" + " LATERAL (\n" + " SELECT ename, sal\n" + " FROM emp\n" + " WHERE deptno = t1.deptno)\n" + " ORDER BY ename DESC\n" + " LIMIT 3)"; sql(sql).ok(); } {code} Expected: {noformat} LogicalProject(EXPR$0=[||($1, CAST($0):VARCHAR NOT NULL)]) LogicalSort(sort0=[$1], dir0=[DESC], fetch=[3]) LogicalProject(DEPTNO=[$0], ENAME=[$1]) LogicalJoin(condition=[=($0, $3)], joinType=[inner]) LogicalAggregate(group=[{0}]) LogicalProject(DEPTNO=[$7]) LogicalTableScan(table=[[CATALOG, SALES, EMP]]) LogicalProject(ENAME=[$1], SAL=[$5], DEPTNO=[$7]) LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {noformat} But it is: {noformat} LogicalProject(EXPR$0=[||($1, CAST($0):VARCHAR NOT NULL)]) LogicalSort(sort0=[$1], dir0=[DESC], fetch=[3]) LogicalProject(DEPTNO=[$0], ENAME=[$1]) LogicalCorrelate(correlation=[$cor0], joinType=[inner], requiredColumns=[{0}]) LogicalAggregate(group=[{0}]) LogicalProject(DEPTNO=[$7]) LogicalTableScan(table=[[CATALOG, SALES, EMP]]) LogicalProject(ENAME=[$1], SAL=[$5]) LogicalFilter(condition=[=($7, $cor0.DEPTNO)]) LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {noformat} was (Author: rubenql): [~danny0405] unfortunately, the proposed fix is insufficient, as soon as the LogicalSort is not at the top of the plan (e.g. it is the input of a final LogicalProject), it is not decorrelated. Example: {code} @Test void testSortLimitWithCorrelateInput2() { final String sql = "" + "SELECT ename||deptno FROM\n" + "(SELECT deptno, ename\n" + " FROM\n" + " (SELECT DISTINCT deptno FROM emp) t1,\n" + " LATERAL (\n" + " SELECT ename, sal\n" + " FROM emp\n" + " WHERE deptno = t1.deptno)\n" + " ORDER BY ename DESC\n" + " LIMIT 3)"; sql(sql).ok(); } {code} Expected: {noformat} LogicalProject(EXPR$0=[||($1, CAST($0):VARCHAR NOT NULL)]) LogicalSort(sort0=[$1], dir0=[DESC], fetch=[3]) LogicalProject(DEPTNO=[$0], ENAME=[$1]) LogicalJoin(condition=[=($0, $3)], joinType=[inner]) LogicalAggregate(group=[{0}]) LogicalProject(DEPTNO=[$7]) LogicalTableScan(table=[[CATALOG, SALES, EMP]]) LogicalProject(ENAME=[$1], SAL=[$5], DEPTNO=[$7]) LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {noformat} But it is: {noformat} LogicalProject(EXPR$0=[||($1, CAST($0):VARCHAR NOT NULL)]) LogicalSort(sort0=[$1], dir0=[DESC], fetch=[3]) LogicalProject(DEPTNO=[$0], ENAME=[$1]) LogicalCorrelate(correlation=[$cor0], joinType=[inner], requiredColumns=[{0}]) LogicalAggregate(group=[{0}]) LogicalProject(DEPTNO=[$7]) LogicalTableScan(table=[[CATALOG, SALES, EMP]]) LogicalProject(ENAME=[$1], SAL=[$5]) LogicalFilter(condition=[=($7, $cor0.DEPTNO)]) LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {noformat} > The Sort rel should be decorrelated even though it has fetch or limit when > its parent is not a Correlate > -------------------------------------------------------------------------------------------------------- > > Key: CALCITE-4333 > URL: https://issues.apache.org/jira/browse/CALCITE-4333 > Project: Calcite > Issue Type: Bug > Components: core > Affects Versions: 1.26.0 > Reporter: Danny Chen > Assignee: Danny Chen > Priority: Major > Labels: pull-request-available > Fix For: 1.27.0 > > Time Spent: 1h 10m > Remaining Estimate: 0h > > Check this test in SqlToRelConverterTest: > {code:java} > @Test void testSortLimitWithCorrelateInput() { > final String sql = "" + > "SELECT deptno, ename\n" + > " FROM\n" + > " (SELECT DISTINCT deptno FROM emp) t1,\n" + > " LATERAL (\n" + > " SELECT ename, sal\n" + > " FROM emp\n" + > " WHERE deptno = t1.deptno)\n" + > " ORDER BY ename DESC\n" + > " LIMIT 3"; > sql(sql).ok(); > } > {code} > The current plan is: > {code:xml} > LogicalSort(sort0=[$1], dir0=[DESC], fetch=[3]) > LogicalProject(DEPTNO=[$0], ENAME=[$1]) > LogicalCorrelate(correlation=[$cor0], joinType=[inner], > requiredColumns=[{0}]) > LogicalAggregate(group=[{0}]) > LogicalProject(DEPTNO=[$7]) > LogicalTableScan(table=[[CATALOG, SALES, EMP]]) > LogicalProject(ENAME=[$1], SAL=[$5]) > LogicalFilter(condition=[=($7, $cor0.DEPTNO)]) > LogicalTableScan(table=[[CATALOG, SALES, EMP]]) > {code} > It actually can be decorrelated to: > {code:xml} > LogicalSort(sort0=[$1], dir0=[DESC], fetch=[3]) > LogicalProject(DEPTNO=[$0], ENAME=[$1]) > LogicalJoin(condition=[=($0, $3)], joinType=[inner]) > LogicalAggregate(group=[{0}]) > LogicalProject(DEPTNO=[$7]) > LogicalTableScan(table=[[CATALOG, SALES, EMP]]) > LogicalProject(ENAME=[$1], SAL=[$5], DEPTNO=[$7]) > LogicalTableScan(table=[[CATALOG, SALES, EMP]]) > {code} -- This message was sent by Atlassian Jira (v8.3.4#803005)