Repository: calcite Updated Branches: refs/heads/master d624a56ef -> 98f932c94
[CALCITE-2590] Remove redundant CAST when operand has exactly the same type as it is casted to Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/98f932c9 Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/98f932c9 Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/98f932c9 Branch: refs/heads/master Commit: 98f932c94d584b474187d995c96950d8d3e5ecd7 Parents: d624a56 Author: Vladimir Sitnikov <[email protected]> Authored: Sun Sep 23 00:06:51 2018 +0300 Committer: Vladimir Sitnikov <[email protected]> Committed: Sun Sep 23 16:07:06 2018 +0300 ---------------------------------------------------------------------- .../main/java/org/apache/calcite/rex/RexSimplify.java | 3 +++ .../java/org/apache/calcite/test/RelOptRulesTest.java | 2 +- .../java/org/apache/calcite/test/RexProgramTest.java | 13 +++++++++++++ .../org/apache/calcite/test/RelOptRulesTest.xml | 10 +++++----- core/src/test/resources/sql/misc.iq | 2 +- core/src/test/resources/sql/sub-query.iq | 2 +- 6 files changed, 24 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/98f932c9/core/src/main/java/org/apache/calcite/rex/RexSimplify.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java index 1e70a53..c905fb8 100644 --- a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java +++ b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java @@ -1250,6 +1250,9 @@ public class RexSimplify { return Objects.requireNonNull( Iterables.getOnlyElement(reducedValues)); default: + if (operand.getType().equals(e.getType())) { + return simplify_(operand); + } return e; } } http://git-wip-us.apache.org/repos/asf/calcite/blob/98f932c9/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java index 4dc1fb2..c191f71 100644 --- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java +++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java @@ -2435,7 +2435,7 @@ public class RelOptRulesTest extends RelOptTestBase { .build(); // The resulting plan should have no cast expressions - checkPlanning(program, + checkPlanUnchanged(new HepPlanner(program), "select cast(d.name as varchar(128)), cast(e.empno as integer) " + "from dept as d inner join emp as e " + "on cast(d.deptno as integer) = cast(e.deptno as integer) " http://git-wip-us.apache.org/repos/asf/calcite/blob/98f932c9/core/src/test/java/org/apache/calcite/test/RexProgramTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/calcite/test/RexProgramTest.java b/core/src/test/java/org/apache/calcite/test/RexProgramTest.java index 268b479..c17457c 100644 --- a/core/src/test/java/org/apache/calcite/test/RexProgramTest.java +++ b/core/src/test/java/org/apache/calcite/test/RexProgramTest.java @@ -701,6 +701,19 @@ public class RexProgramTest extends RexProgramBuilderBase { varCharType11, rexBuilder.makeInputRef(varCharType10, 0))), is(true)); } + @Test public void removeRedundantCast() { + checkSimplify(cast(vInt(), nullable(tInt())), "?0.int0"); + checkSimplify(cast(vInt(), tInt()), "CAST(?0.int0):INTEGER NOT NULL"); + checkSimplify(cast(vIntNotNull(), nullable(tInt())), "CAST(?0.notNullInt0):INTEGER"); + checkSimplify(cast(vIntNotNull(), tInt()), "?0.notNullInt0"); + + // Nested int int cast is removed + checkSimplify(cast(cast(vVarchar(), tInt()), tInt()), "CAST(?0.varchar0):INTEGER NOT NULL"); + checkSimplify(cast(cast(vVarchar(), tInt()), tVarchar()), + "CAST(CAST(?0.varchar0):INTEGER NOT NULL):VARCHAR CHARACTER SET \"ISO-8859-1\" " + + "COLLATE \"ISO-8859-1$en_US$primary\" NOT NULL"); + } + /** Unit test for {@link org.apache.calcite.rex.RexUtil#toCnf}. */ @Test public void testCnf() { final RelDataType booleanType = http://git-wip-us.apache.org/repos/asf/calcite/blob/98f932c9/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml ---------------------------------------------------------------------- diff --git a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml index 262adb7..143383e 100644 --- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml +++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml @@ -2077,13 +2077,13 @@ LogicalProject(X=[$0], Y=[$1]) </Resource> <Resource name="planBefore"> <![CDATA[ -LogicalProject(EXPR$0=[CAST($1):VARCHAR(128) CHARACTER SET "ISO-8859-1" COLLATE "ISO-8859-1$en_US$primary" NOT NULL], EXPR$1=[CAST($2):INTEGER NOT NULL]) +LogicalProject(EXPR$0=[CAST($1):VARCHAR(128) CHARACTER SET "ISO-8859-1" COLLATE "ISO-8859-1$en_US$primary" NOT NULL], EXPR$1=[$2]) LogicalFilter(condition=[=(CAST(CAST($4):VARCHAR(1) CHARACTER SET "ISO-8859-1" COLLATE "ISO-8859-1$en_US$primary" NOT NULL):VARCHAR(7) CHARACTER SET "ISO-8859-1" COLLATE "ISO-8859-1$en_US$primary" NOT NULL, 'Manager')]) LogicalProject(DEPTNO=[$0], NAME=[$1], EMPNO=[$3], ENAME=[$4], JOB=[$5], MGR=[$6], HIREDATE=[$7], SAL=[$8], COMM=[$9], DEPTNO1=[$10], SLACKER=[$11]) LogicalJoin(condition=[=($2, $12)], joinType=[inner]) - LogicalProject(DEPTNO=[$0], NAME=[$1], DEPTNO0=[CAST($0):INTEGER NOT NULL]) + LogicalProject(DEPTNO=[$0], NAME=[$1], DEPTNO0=[$0]) LogicalTableScan(table=[[CATALOG, SALES, DEPT]]) - LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8], DEPTNO0=[CAST($7):INTEGER NOT NULL]) + LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8], DEPTNO0=[$7]) LogicalTableScan(table=[[CATALOG, SALES, EMP]]) ]]> </Resource> @@ -7066,13 +7066,13 @@ order by cast(d.deptno as integer) offset 1]]> <Resource name="planBefore"> <![CDATA[ LogicalSort(sort0=[$1], dir0=[ASC], offset=[1]) - LogicalProject(DEPTNO=[$0], EXPR$1=[CAST($0):INTEGER NOT NULL]) + LogicalProject(DEPTNO=[$0], EXPR$1=[$0]) LogicalTableScan(table=[[CATALOG, SALES, DEPT]]) ]]> </Resource> <Resource name="planAfter"> <![CDATA[ -LogicalProject(DEPTNO=[$0], EXPR$1=[CAST($0):INTEGER NOT NULL]) +LogicalProject(DEPTNO=[$0], EXPR$1=[$0]) LogicalSort(sort0=[$0], dir0=[ASC], offset=[1]) LogicalTableScan(table=[[CATALOG, SALES, DEPT]]) ]]> http://git-wip-us.apache.org/repos/asf/calcite/blob/98f932c9/core/src/test/resources/sql/misc.iq ---------------------------------------------------------------------- diff --git a/core/src/test/resources/sql/misc.iq b/core/src/test/resources/sql/misc.iq index fdb691d..51c99ba 100644 --- a/core/src/test/resources/sql/misc.iq +++ b/core/src/test/resources/sql/misc.iq @@ -473,7 +473,7 @@ EnumerableCalc(expr#0..7=[{inputs}], expr#8=[IS NULL($t5)], expr#9=[IS NULL($t7) EnumerableCalc(expr#0..3=[{inputs}], expr#4=[true], deptno=[$t0], $f0=[$t4]) EnumerableTableScan(table=[[hr, depts]]) EnumerableAggregate(group=[{0}], agg#0=[MIN($1)]) - EnumerableCalc(expr#0..3=[{inputs}], expr#4=[90], expr#5=[+($t0, $t4)], expr#6=[true], expr#7=[CAST($t5):INTEGER NOT NULL], expr#8=[=($t5, $t7)], $f4=[$t5], $f0=[$t6], $condition=[$t8]) + EnumerableCalc(expr#0..3=[{inputs}], expr#4=[90], expr#5=[+($t0, $t4)], expr#6=[true], $f4=[$t5], $f0=[$t6], $condition=[$t6]) EnumerableTableScan(table=[[hr, depts]]) !plan http://git-wip-us.apache.org/repos/asf/calcite/blob/98f932c9/core/src/test/resources/sql/sub-query.iq ---------------------------------------------------------------------- diff --git a/core/src/test/resources/sql/sub-query.iq b/core/src/test/resources/sql/sub-query.iq index 05ecded..868e55d 100644 --- a/core/src/test/resources/sql/sub-query.iq +++ b/core/src/test/resources/sql/sub-query.iq @@ -600,7 +600,7 @@ where empno IN ( !ok EnumerableCalc(expr#0..4=[{inputs}], SAL=[$t4]) EnumerableJoin(condition=[AND(=($1, $3), =($0, $2))], joinType=[inner]) - EnumerableCalc(expr#0..2=[{inputs}], expr#3=[CAST($t1):VARCHAR(14) CHARACTER SET "ISO-8859-1" COLLATE "ISO-8859-1$en_US$primary"], expr#4=[=($t3, $t1)], proj#0..1=[{exprs}], $condition=[$t4]) + EnumerableCalc(expr#0..2=[{inputs}], expr#3=[IS NOT NULL($t1)], proj#0..1=[{exprs}], $condition=[$t3]) EnumerableTableScan(table=[[scott, DEPT]]) EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], JOB=[$t2], SAL=[$t5]) EnumerableTableScan(table=[[scott, EMP]])
