Repository: calcite Updated Branches: refs/heads/master 352aaeade -> 84b55ef58
[CALCITE-1018] SortJoinTransposeRule not firing due to getMaxRowCount(RelSubset) returning null Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/84b55ef5 Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/84b55ef5 Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/84b55ef5 Branch: refs/heads/master Commit: 84b55ef5877f947548bb19ce1133fb0abd9a6d29 Parents: 352aaea Author: maryannxue <[email protected]> Authored: Fri Dec 11 22:17:16 2015 -0500 Committer: maryannxue <[email protected]> Committed: Fri Dec 11 22:17:16 2015 -0500 ---------------------------------------------------------------------- .../adapter/enumerable/EnumerableLimit.java | 15 ++++++++ .../calcite/rel/metadata/RelMdMaxRowCount.java | 16 ++++++++ .../java/org/apache/calcite/test/JdbcTest.java | 7 ++-- core/src/test/resources/sql/join.oq | 40 ++++++++++---------- 4 files changed, 55 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/84b55ef5/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableLimit.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableLimit.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableLimit.java index 5d1dc63..cea8755 100644 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableLimit.java +++ b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableLimit.java @@ -98,6 +98,21 @@ public class EnumerableLimit extends SingleRel implements EnumerableRel { .itemIf("fetch", fetch, fetch != null); } + @Override public double getRows() { + double rowCount = super.getRows(); + final int offset = + this.offset == null ? 0 : RexLiteral.intValue(this.offset); + rowCount = Math.max(rowCount - offset, 0D); + + if (this.fetch != null) { + final int limit = RexLiteral.intValue(this.fetch); + if (limit < rowCount) { + return (double) limit; + } + } + return rowCount; + } + public Result implement(EnumerableRelImplementor implementor, Prefer pref) { final BlockBuilder builder = new BlockBuilder(); final EnumerableRel child = (EnumerableRel) getInput(); http://git-wip-us.apache.org/repos/asf/calcite/blob/84b55ef5/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMaxRowCount.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMaxRowCount.java b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMaxRowCount.java index c7b3b75..1d2a378 100644 --- a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMaxRowCount.java +++ b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMaxRowCount.java @@ -16,6 +16,7 @@ */ package org.apache.calcite.rel.metadata; +import org.apache.calcite.plan.volcano.RelSubset; import org.apache.calcite.rel.RelNode; import org.apache.calcite.rel.core.Aggregate; import org.apache.calcite.rel.core.Filter; @@ -133,6 +134,21 @@ public class RelMdMaxRowCount { return (double) values.getTuples().size(); } + public Double getMaxRowCount(RelSubset rel) { + // FIXME This is a short-term fix for CALCITE-1018. A complete + // solution will come with CALCITE-794. + for (RelNode node : rel.getRels()) { + if (node instanceof Sort) { + Sort sort = (Sort) node; + if (sort.fetch != null) { + return (double) RexLiteral.intValue(sort.fetch); + } + } + } + + return Double.POSITIVE_INFINITY; + } + // Catch-all rule when none of the others apply. public Double getMaxRowCount(RelNode rel) { return null; http://git-wip-us.apache.org/repos/asf/calcite/blob/84b55ef5/core/src/test/java/org/apache/calcite/test/JdbcTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/calcite/test/JdbcTest.java b/core/src/test/java/org/apache/calcite/test/JdbcTest.java index 4c203d6..391729d 100644 --- a/core/src/test/java/org/apache/calcite/test/JdbcTest.java +++ b/core/src/test/java/org/apache/calcite/test/JdbcTest.java @@ -3058,9 +3058,10 @@ public class JdbcTest { .query("select \"store_id\", \"grocery_sqft\" from \"store\"\n" + "where \"store_id\" < 10\n" + "order by 1 fetch first 5 rows only") - .explainContains("PLAN=EnumerableLimit(fetch=[5])\n" - + " EnumerableCalc(expr#0..23=[{inputs}], expr#24=[10], expr#25=[<($t0, $t24)], store_id=[$t0], grocery_sqft=[$t16], $condition=[$t25])\n" - + " EnumerableTableScan(table=[[foodmart2, store]])\n") + .explainContains("PLAN=EnumerableCalc(expr#0..23=[{inputs}], store_id=[$t0], grocery_sqft=[$t16])\n" + + " EnumerableLimit(fetch=[5])\n" + + " EnumerableCalc(expr#0..23=[{inputs}], expr#24=[10], expr#25=[<($t0, $t24)], proj#0..23=[{exprs}], $condition=[$t25])\n" + + " EnumerableTableScan(table=[[foodmart2, store]])\n") .returns("store_id=0; grocery_sqft=null\n" + "store_id=1; grocery_sqft=17475\n" + "store_id=2; grocery_sqft=22271\n" http://git-wip-us.apache.org/repos/asf/calcite/blob/84b55ef5/core/src/test/resources/sql/join.oq ---------------------------------------------------------------------- diff --git a/core/src/test/resources/sql/join.oq b/core/src/test/resources/sql/join.oq index 43cdad1..9cd9e2c 100644 --- a/core/src/test/resources/sql/join.oq +++ b/core/src/test/resources/sql/join.oq @@ -256,32 +256,32 @@ using (deptno); !ok -### [CALCITE-892] Implement SortJoinTransposeRule -### Limit and sort are different Enumerable operators, thus it does not apply -select * from "scott".emp e left join ( +### [CALCITE-1018] SortJoinTransposeRule not firing due to getMaxRowCount(RelSubset) returning null +select * from (select * from "scott".emp) e left join ( select * from "scott".dept d) using (deptno) -order by sal limit 10; -+-------+--------+----------+------+------------+---------+---------+--------+---------+------------+----------+ -| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | DEPTNO0 | DNAME | LOC | -+-------+--------+----------+------+------------+---------+---------+--------+---------+------------+----------+ -| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | | 20 | 20 | RESEARCH | DALLAS | -| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | | 30 | 30 | SALES | CHICAGO | -| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | | 20 | 20 | RESEARCH | DALLAS | -| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 | 30 | SALES | CHICAGO | -| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 | 30 | SALES | CHICAGO | -| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | | 10 | 10 | ACCOUNTING | NEW YORK | -| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 | 30 | SALES | CHICAGO | -| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 | 30 | SALES | CHICAGO | -| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | | 10 | 10 | ACCOUNTING | NEW YORK | -| 7698 | BLAKE | MANAGER | 7839 | 1981-01-05 | 2850.00 | | 30 | 30 | SALES | CHICAGO | -+-------+--------+----------+------+------------+---------+---------+--------+---------+------------+----------+ +order by empno limit 10; ++-------+--------+-----------+------+------------+---------+---------+--------+---------+------------+----------+ +| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | DEPTNO0 | DNAME | LOC | ++-------+--------+-----------+------+------------+---------+---------+--------+---------+------------+----------+ +| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | | 20 | 20 | RESEARCH | DALLAS | +| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 | 30 | SALES | CHICAGO | +| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 | 30 | SALES | CHICAGO | +| 7566 | JONES | MANAGER | 7839 | 1981-02-04 | 2975.00 | | 20 | 20 | RESEARCH | DALLAS | +| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 | 30 | SALES | CHICAGO | +| 7698 | BLAKE | MANAGER | 7839 | 1981-01-05 | 2850.00 | | 30 | 30 | SALES | CHICAGO | +| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | | 10 | 10 | ACCOUNTING | NEW YORK | +| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | | 20 | 20 | RESEARCH | DALLAS | +| 7839 | KING | PRESIDENT | | 1981-11-17 | 5000.00 | | 10 | 10 | ACCOUNTING | NEW YORK | +| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 | 30 | SALES | CHICAGO | ++-------+--------+-----------+------+------------+---------+---------+--------+---------+------------+----------+ (10 rows) !ok EnumerableLimit(fetch=[10]) - EnumerableSort(sort0=[$5], dir0=[ASC]) + EnumerableSort(sort0=[$0], dir0=[ASC]) EnumerableJoin(condition=[=($7, $8)], joinType=[left]) - EnumerableTableScan(table=[[scott, EMP]]) + EnumerableLimit(fetch=[10]) + EnumerableTableScan(table=[[scott, EMP]]) EnumerableTableScan(table=[[scott, DEPT]]) !plan
