Repository: calcite
Updated Branches:
  refs/heads/master da57c903f -> 439ca73b8


[CALCITE-2695] Simplify casts which are only widening nullability (Zoltan 
Haindrich)

Close apache/calcite#934


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/439ca73b
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/439ca73b
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/439ca73b

Branch: refs/heads/master
Commit: 439ca73b8a335213b5f2764514b14e17c9d3c216
Parents: 463255c
Author: Zoltan Haindrich <[email protected]>
Authored: Thu Nov 22 18:31:09 2018 +0100
Committer: Jesus Camacho Rodriguez <[email protected]>
Committed: Tue Nov 27 20:33:11 2018 -0800

----------------------------------------------------------------------
 .../apache/calcite/rex/RexProgramBuilder.java   |  2 +-
 .../org/apache/calcite/rex/RexSimplify.java     | 13 +++++++++----
 .../java/org/apache/calcite/test/JdbcTest.java  |  2 +-
 .../org/apache/calcite/test/RexProgramTest.java |  9 ++++++++-
 .../org/apache/calcite/test/RelOptRulesTest.xml |  2 +-
 .../calcite/test/SqlToRelConverterTest.xml      | 14 +++++++-------
 core/src/test/resources/sql/agg.iq              |  2 +-
 core/src/test/resources/sql/some.iq             |  2 +-
 core/src/test/resources/sql/sub-query.iq        | 20 ++++++++++----------
 9 files changed, 39 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/439ca73b/core/src/main/java/org/apache/calcite/rex/RexProgramBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rex/RexProgramBuilder.java 
b/core/src/main/java/org/apache/calcite/rex/RexProgramBuilder.java
index 94329e2..f871b9d 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexProgramBuilder.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexProgramBuilder.java
@@ -326,7 +326,7 @@ public class RexProgramBuilder {
   private RexLocalRef registerInternal(RexNode expr, boolean force) {
     final RexSimplify simplify =
         new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, 
RexUtil.EXECUTOR);
-    expr = simplify.simplify(expr);
+    expr = simplify.simplifyPreservingType(expr);
 
     RexLocalRef ref;
     final Pair<String, String> key;

http://git-wip-us.apache.org/repos/asf/calcite/blob/439ca73b/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 17946f2..a8e95e9 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
@@ -1535,7 +1535,11 @@ public class RexSimplify {
   }
 
   private RexNode simplifyCast(RexCall e) {
-    final RexNode operand = e.getOperands().get(0);
+    RexNode operand = e.getOperands().get(0);
+    operand = simplify(operand, UNKNOWN);
+    if (sameTypeOrNarrowsNullability(e.getType(), operand.getType())) {
+      return operand;
+    }
     switch (operand.getKind()) {
     case LITERAL:
       final RexLiteral literal = (RexLiteral) operand;
@@ -1564,10 +1568,11 @@ public class RexSimplify {
       return Objects.requireNonNull(
           Iterables.getOnlyElement(reducedValues));
     default:
-      if (operand.getType().equals(e.getType())) {
-        return simplify(operand, UNKNOWN);
+      if (operand == e.getOperands().get(0)) {
+        return e;
+      } else {
+        return rexBuilder.makeCast(e.getType(), operand);
       }
-      return e;
     }
   }
 

http://git-wip-us.apache.org/repos/asf/calcite/blob/439ca73b/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 e1d2917..f84f24f 100644
--- a/core/src/test/java/org/apache/calcite/test/JdbcTest.java
+++ b/core/src/test/java/org/apache/calcite/test/JdbcTest.java
@@ -3472,7 +3472,7 @@ public class JdbcTest {
         .typeIs(
             "[deptno INTEGER NOT NULL, empid INTEGER NOT NULL, S REAL, FIVE 
INTEGER NOT NULL, M REAL, C BIGINT NOT NULL]")
         .explainContains(""
-            + "EnumerableCalc(expr#0..7=[{inputs}], expr#8=[0], expr#9=[>($t4, 
$t8)], expr#10=[CAST($t5):JavaType(class java.lang.Float)], expr#11=[null], 
expr#12=[CASE($t9, $t10, $t11)], expr#13=[5], deptno=[$t1], empid=[$t0], 
S=[$t12], FIVE=[$t13], M=[$t6], C=[$t7])\n"
+            + "EnumerableCalc(expr#0..7=[{inputs}], expr#8=[0], expr#9=[>($t4, 
$t8)], expr#10=[null], expr#11=[CASE($t9, $t5, $t10)], expr#12=[5], 
deptno=[$t1], empid=[$t0], S=[$t11], FIVE=[$t12], M=[$t6], C=[$t7])\n"
             + "  EnumerableWindow(window#0=[window(partition {1} order by [0] 
rows between $4 PRECEDING and CURRENT ROW aggs [COUNT($3), $SUM0($3), MIN($2), 
COUNT()])])\n"
             + "    EnumerableCalc(expr#0..4=[{inputs}], expr#5=[+($t3, $t0)], 
proj#0..1=[{exprs}], salary=[$t3], $3=[$t5])\n"
             + "      EnumerableTableScan(table=[[hr, emps]])\n")

http://git-wip-us.apache.org/repos/asf/calcite/blob/439ca73b/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 5f50144..b52937f 100644
--- a/core/src/test/java/org/apache/calcite/test/RexProgramTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RexProgramTest.java
@@ -739,7 +739,7 @@ public class RexProgramTest extends RexProgramBuilderBase {
   @Test public void removeRedundantCast() {
     checkSimplify(cast(vInt(), nullable(tInt())), "?0.int0");
     checkSimplifyUnchanged(cast(vInt(), tInt()));
-    checkSimplifyUnchanged(cast(vIntNotNull(), nullable(tInt())));
+    checkSimplify(cast(vIntNotNull(), nullable(tInt())), "?0.notNullInt0");
     checkSimplify(cast(vIntNotNull(), tInt()), "?0.notNullInt0");
 
     // Nested int int cast is removed
@@ -2112,6 +2112,13 @@ public class RexProgramTest extends 
RexProgramBuilderBase {
         "2011-07-20 01:23:45");
   }
 
+  @Test public void testRemovalOfNullabilityWideningCast() {
+    RexNode expr = cast(isTrue(vBoolNotNull()), tBoolean(true));
+    assertThat(expr.getType().isNullable(), is(true));
+    RexNode result = simplify.simplifyUnknownAs(expr, RexUnknownAs.UNKNOWN);
+    assertThat(result.getType().isNullable(), is(false));
+  }
+
   @Test public void testCompareTimestampWithTimeZone() {
     final TimestampWithTimeZoneString timestampLTZChar1 =
         new TimestampWithTimeZoneString("2011-07-20 10:34:56 
America/Los_Angeles");

http://git-wip-us.apache.org/repos/asf/calcite/blob/439ca73b/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 883338a..ba38082 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -7926,7 +7926,7 @@ LogicalProject(DEPTNO=[$1])
         <Resource name="planAfter">
             <![CDATA[
 LogicalProject(EMPNO=[$0], D=[CASE(=($9, 0), false, IS NULL(CASE(true, 
CAST($7):INTEGER, null)), null, IS NOT NULL($12), true, <($10, $9), null, 
false)])
-  LogicalJoin(condition=[=(CAST($7):INTEGER, $11)], joinType=[left])
+  LogicalJoin(condition=[=($7, $11)], joinType=[left])
     LogicalJoin(condition=[true], joinType=[inner])
       LogicalTableScan(table=[[CATALOG, SALES, EMP]])
       LogicalAggregate(group=[{}], c=[COUNT()], ck=[COUNT($0)])

http://git-wip-us.apache.org/repos/asf/calcite/blob/439ca73b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
----------------------------------------------------------------------
diff --git 
a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml 
b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
index a289675..4f5b8d9 100644
--- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
@@ -327,7 +327,7 @@ LogicalProject(DEPTNO=[$0], NAME=[$1])
     <TestCase name="testSelectOverDistinct">
         <Resource name="plan">
             <![CDATA[
-LogicalProject(EXPR$0=[CASE(>(COUNT(DISTINCT $7) OVER (ROWS BETWEEN 10 
PRECEDING AND CURRENT ROW), 0), CAST($SUM0(DISTINCT $7) OVER (ROWS BETWEEN 10 
PRECEDING AND CURRENT ROW)):INTEGER, null)])
+LogicalProject(EXPR$0=[CASE(>(COUNT(DISTINCT $7) OVER (ROWS BETWEEN 10 
PRECEDING AND CURRENT ROW), 0), $SUM0(DISTINCT $7) OVER (ROWS BETWEEN 10 
PRECEDING AND CURRENT ROW), null)])
   LogicalTableScan(table=[[CATALOG, SALES, EMP]])
 ]]>
         </Resource>
@@ -690,7 +690,7 @@ LogicalProject(EXPR$0=[NOT(LIKE('a', 'b', 'c'))])
     <TestCase name="testOverMultiple">
         <Resource name="plan">
             <![CDATA[
-LogicalProject(EXPR$0=[CASE(>(COUNT($5) OVER (PARTITION BY $2 ORDER BY $4 ROWS 
BETWEEN 2 PRECEDING AND CURRENT ROW), 0), CAST($SUM0($5) OVER (PARTITION BY $2 
ORDER BY $4 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)):INTEGER, null)], 
EXPR$1=[CASE(>(COUNT($7) OVER (PARTITION BY $2 ORDER BY $4 ROWS BETWEEN 2 
PRECEDING AND CURRENT ROW), 0), CAST($SUM0($7) OVER (PARTITION BY $2 ORDER BY 
$4 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)):INTEGER, null)], 
EXPR$2=[CASE(>=(COUNT() OVER (PARTITION BY $2 ORDER BY $4 ROWS BETWEEN 3 
PRECEDING AND CURRENT ROW), 2), CASE(>(COUNT($7) OVER (PARTITION BY $2 ORDER BY 
$4 ROWS BETWEEN 3 PRECEDING AND CURRENT ROW), 0), CAST($SUM0($7) OVER 
(PARTITION BY $2 ORDER BY $4 ROWS BETWEEN 3 PRECEDING AND CURRENT 
ROW)):INTEGER, null), null)])
+LogicalProject(EXPR$0=[CASE(>(COUNT($5) OVER (PARTITION BY $2 ORDER BY $4 ROWS 
BETWEEN 2 PRECEDING AND CURRENT ROW), 0), $SUM0($5) OVER (PARTITION BY $2 ORDER 
BY $4 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), null)], 
EXPR$1=[CASE(>(COUNT($7) OVER (PARTITION BY $2 ORDER BY $4 ROWS BETWEEN 2 
PRECEDING AND CURRENT ROW), 0), $SUM0($7) OVER (PARTITION BY $2 ORDER BY $4 
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), null)], EXPR$2=[CASE(>=(COUNT() OVER 
(PARTITION BY $2 ORDER BY $4 ROWS BETWEEN 3 PRECEDING AND CURRENT ROW), 2), 
CASE(>(COUNT($7) OVER (PARTITION BY $2 ORDER BY $4 ROWS BETWEEN 3 PRECEDING AND 
CURRENT ROW), 0), $SUM0($7) OVER (PARTITION BY $2 ORDER BY $4 ROWS BETWEEN 3 
PRECEDING AND CURRENT ROW), null), null)])
   LogicalFilter(condition=[>(-($7, $5), 999)])
     LogicalTableScan(table=[[CATALOG, SALES, EMP]])
 ]]>
@@ -753,7 +753,7 @@ LogicalProject(C_NATIONKEY=[$1], FAKE_COL3=[$2])
     <TestCase name="testOverAvg">
         <Resource name="plan">
             <![CDATA[
-LogicalProject(EXPR$0=[CASE(>(COUNT($5) OVER (PARTITION BY $2 ORDER BY $4 ROWS 
BETWEEN 2 PRECEDING AND CURRENT ROW), 0), CAST($SUM0($5) OVER (PARTITION BY $2 
ORDER BY $4 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)):INTEGER, null)], 
EXPR$1=[CAST(/(CASE(>(COUNT($5) OVER (PARTITION BY $2 ORDER BY $4 ROWS BETWEEN 
2 PRECEDING AND CURRENT ROW), 0), CAST($SUM0($5) OVER (PARTITION BY $2 ORDER BY 
$4 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)):INTEGER, null), COUNT($5) OVER 
(PARTITION BY $2 ORDER BY $4 ROWS BETWEEN 2 PRECEDING AND CURRENT 
ROW))):INTEGER])
+LogicalProject(EXPR$0=[CASE(>(COUNT($5) OVER (PARTITION BY $2 ORDER BY $4 ROWS 
BETWEEN 2 PRECEDING AND CURRENT ROW), 0), $SUM0($5) OVER (PARTITION BY $2 ORDER 
BY $4 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), null)], 
EXPR$1=[CAST(/(CASE(>(COUNT($5) OVER (PARTITION BY $2 ORDER BY $4 ROWS BETWEEN 
2 PRECEDING AND CURRENT ROW), 0), CAST($SUM0($5) OVER (PARTITION BY $2 ORDER BY 
$4 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)):INTEGER, null), COUNT($5) OVER 
(PARTITION BY $2 ORDER BY $4 ROWS BETWEEN 2 PRECEDING AND CURRENT 
ROW))):INTEGER])
   LogicalTableScan(table=[[CATALOG, SALES, EMP]])
 ]]>
         </Resource>
@@ -1245,7 +1245,7 @@ window w1 as (partition by job order by hiredate rows 2 
preceding)]]>
         </Resource>
         <Resource name="plan">
             <![CDATA[
-LogicalProject(EXPR$0=[CASE(>(COUNT($5) OVER (PARTITION BY $2 ORDER BY $4 ROWS 
BETWEEN 2 PRECEDING AND CURRENT ROW), 0), CAST($SUM0($5) OVER (PARTITION BY $2 
ORDER BY $4 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)):INTEGER, null)], 
EXPR$1=[/(CASE(>(COUNT(CAST($5):REAL NOT NULL) OVER (PARTITION BY $2 ORDER BY 
$4 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 0), CAST($SUM0(CAST($5):REAL NOT 
NULL) OVER (PARTITION BY $2 ORDER BY $4 ROWS BETWEEN 2 PRECEDING AND CURRENT 
ROW)):REAL, null), COUNT(CAST($5):REAL NOT NULL) OVER (PARTITION BY $2 ORDER BY 
$4 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW))])
+LogicalProject(EXPR$0=[CASE(>(COUNT($5) OVER (PARTITION BY $2 ORDER BY $4 ROWS 
BETWEEN 2 PRECEDING AND CURRENT ROW), 0), $SUM0($5) OVER (PARTITION BY $2 ORDER 
BY $4 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), null)], 
EXPR$1=[/(CASE(>(COUNT(CAST($5):REAL NOT NULL) OVER (PARTITION BY $2 ORDER BY 
$4 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 0), CAST($SUM0(CAST($5):REAL NOT 
NULL) OVER (PARTITION BY $2 ORDER BY $4 ROWS BETWEEN 2 PRECEDING AND CURRENT 
ROW)):REAL, null), COUNT(CAST($5):REAL NOT NULL) OVER (PARTITION BY $2 ORDER BY 
$4 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW))])
   LogicalTableScan(table=[[CATALOG, SALES, EMP]])
 ]]>
         </Resource>
@@ -2155,7 +2155,7 @@ from emp]]>
         </Resource>
         <Resource name="plan">
             <![CDATA[
-LogicalProject(EMPNO=[$0], EXPR$1=[CAST(NOT(AND(IS TRUE($11), IS NOT 
NULL($9)))):BOOLEAN])
+LogicalProject(EMPNO=[$0], EXPR$1=[CAST(IS NOT TRUE($11)):BOOLEAN])
   LogicalJoin(condition=[=($9, $10)], joinType=[left])
     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]])
@@ -2174,7 +2174,7 @@ from emp]]>
         </Resource>
         <Resource name="plan">
             <![CDATA[
-LogicalProject(EMPNO=[$0], EXPR$1=[CAST(NOT(AND(IS TRUE($11), IS NOT 
NULL($9)))):BOOLEAN])
+LogicalProject(EMPNO=[$0], EXPR$1=[CAST(IS NOT TRUE($11)):BOOLEAN])
   LogicalJoin(condition=[=($9, $10)], joinType=[left])
     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]])
@@ -2194,7 +2194,7 @@ from emp]]>
         </Resource>
         <Resource name="plan">
             <![CDATA[
-LogicalProject(EMPNO=[$0], EXPR$1=[CAST(NOT(AND(IS TRUE($11), IS NOT 
NULL($9)))):BOOLEAN])
+LogicalProject(EMPNO=[$0], EXPR$1=[CAST(IS NOT TRUE($11)):BOOLEAN])
   LogicalJoin(condition=[=($9, $10)], joinType=[left])
     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]])

http://git-wip-us.apache.org/repos/asf/calcite/blob/439ca73b/core/src/test/resources/sql/agg.iq
----------------------------------------------------------------------
diff --git a/core/src/test/resources/sql/agg.iq 
b/core/src/test/resources/sql/agg.iq
index b7688fc..3d4468d 100755
--- a/core/src/test/resources/sql/agg.iq
+++ b/core/src/test/resources/sql/agg.iq
@@ -2148,7 +2148,7 @@ from (values (1,2),(3,4));
 (1 row)
 
 !ok
-EnumerableCalc(expr#0=[{inputs}], expr#1=[0], expr#2=[=($t0, $t1)], 
expr#3=[null], expr#4=[CAST($t0):BIGINT], expr#5=[CASE($t2, $t3, $t4)], 
EXPR$0=[$t5])
+EnumerableCalc(expr#0=[{inputs}], expr#1=[0], expr#2=[=($t0, $t1)], 
expr#3=[null], expr#4=[CASE($t2, $t3, $t0)], EXPR$0=[$t4])
   EnumerableAggregate(group=[{}], agg#0=[COUNT($0)])
     EnumerableAggregate(group=[{0}])
       EnumerableCalc(expr#0..1=[{inputs}], expr#2=['1'], $f0=[$t2])

http://git-wip-us.apache.org/repos/asf/calcite/blob/439ca73b/core/src/test/resources/sql/some.iq
----------------------------------------------------------------------
diff --git a/core/src/test/resources/sql/some.iq 
b/core/src/test/resources/sql/some.iq
index 065476c..6eb7e92 100644
--- a/core/src/test/resources/sql/some.iq
+++ b/core/src/test/resources/sql/some.iq
@@ -108,7 +108,7 @@ from "scott".emp;
 (14 rows)
 
 !ok
-EnumerableCalc(expr#0..10=[{inputs}], expr#11=[0], expr#12=[=($t1, $t11)], 
expr#13=[false], expr#14=[CAST($t13):BOOLEAN], expr#15=[AND($t12, $t14)], 
expr#16=[<=($t8, $t0)], expr#17=[IS TRUE($t16)], expr#18=[true], 
expr#19=[CAST($t18):BOOLEAN], expr#20=[NOT($t12)], expr#21=[AND($t17, $t19, 
$t20)], expr#22=[>($t1, $t2)], expr#23=[null], expr#24=[CAST($t23):BOOLEAN], 
expr#25=[NOT($t17)], expr#26=[AND($t22, $t24, $t20, $t25)], 
expr#27=[NOT($t22)], expr#28=[AND($t16, $t20, $t25, $t27)], expr#29=[OR($t15, 
$t21, $t26, $t28)], expr#30=[NOT($t29)], EMPNO=[$t3], ENAME=[$t4], JOB=[$t5], 
MGR=[$t6], HIREDATE=[$t7], SAL=[$t8], COMM=[$t9], DEPTNO=[$t10], X=[$t30])
+EnumerableCalc(expr#0..10=[{inputs}], expr#11=[0], expr#12=[=($t1, $t11)], 
expr#13=[false], expr#14=[AND($t12, $t13)], expr#15=[<=($t8, $t0)], expr#16=[IS 
TRUE($t15)], expr#17=[true], expr#18=[NOT($t12)], expr#19=[AND($t16, $t17, 
$t18)], expr#20=[>($t1, $t2)], expr#21=[null], expr#22=[CAST($t21):BOOLEAN], 
expr#23=[NOT($t16)], expr#24=[AND($t20, $t22, $t18, $t23)], 
expr#25=[NOT($t20)], expr#26=[AND($t15, $t18, $t23, $t25)], expr#27=[OR($t14, 
$t19, $t24, $t26)], expr#28=[NOT($t27)], EMPNO=[$t3], ENAME=[$t4], JOB=[$t5], 
MGR=[$t6], HIREDATE=[$t7], SAL=[$t8], COMM=[$t9], DEPTNO=[$t10], X=[$t28])
   EnumerableJoin(condition=[true], joinType=[inner])
     EnumerableAggregate(group=[{}], m=[MAX($6)], c=[COUNT()], d=[COUNT($6)])
       EnumerableTableScan(table=[[scott, EMP]])

http://git-wip-us.apache.org/repos/asf/calcite/blob/439ca73b/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 864f62d..e29948f 100644
--- a/core/src/test/resources/sql/sub-query.iq
+++ b/core/src/test/resources/sql/sub-query.iq
@@ -1009,7 +1009,7 @@ EnumerableCalc(expr#0..3=[{inputs}], expr#4=[false], 
expr#5=[=($t2, $t4)], expr#
     EnumerableLimit(fetch=[1])
       EnumerableSort(sort0=[$0], dir0=[DESC])
         EnumerableAggregate(group=[{0}], c=[COUNT()])
-          EnumerableCalc(expr#0..2=[{inputs}], expr#3=[true], expr#4=[10], 
expr#5=[CAST($t0):TINYINT], expr#6=[=($t4, $t5)], cs=[$t3], $condition=[$t6])
+          EnumerableCalc(expr#0..2=[{inputs}], expr#3=[true], expr#4=[10], 
expr#5=[=($t4, $t0)], cs=[$t3], $condition=[$t5])
             EnumerableTableScan(table=[[scott, DEPT]])
 !plan
 
@@ -1038,7 +1038,7 @@ from "scott".emp;
 (14 rows)
 
 !ok
-EnumerableCalc(expr#0..3=[{inputs}], expr#4=[IS NULL($t3)], expr#5=[false], 
expr#6=[CAST($t5):BOOLEAN], expr#7=[AND($t4, $t6)], expr#8=[=($t2, $t5)], 
expr#9=[null], expr#10=[IS NULL($t9)], expr#11=[OR($t8, $t10)], expr#12=[IS 
TRUE($t11)], expr#13=[null], expr#14=[CAST($t13):BOOLEAN], expr#15=[NOT($t4)], 
expr#16=[AND($t12, $t14, $t15)], expr#17=[IS NOT NULL($t2)], expr#18=[true], 
expr#19=[CAST($t18):BOOLEAN], expr#20=[IS NOT TRUE($t11)], expr#21=[AND($t17, 
$t19, $t20, $t15)], expr#22=[NOT($t17)], expr#23=[AND($t6, $t20, $t15, $t22)], 
expr#24=[OR($t7, $t16, $t21, $t23)], expr#25=[NOT($t24)], SAL=[$t1], 
EXPR$1=[$t25])
+EnumerableCalc(expr#0..3=[{inputs}], expr#4=[IS NULL($t3)], expr#5=[false], 
expr#6=[AND($t4, $t5)], expr#7=[=($t2, $t5)], expr#8=[null], expr#9=[IS 
NULL($t8)], expr#10=[OR($t7, $t9)], expr#11=[IS TRUE($t10)], expr#12=[null], 
expr#13=[CAST($t12):BOOLEAN], expr#14=[NOT($t4)], expr#15=[AND($t11, $t13, 
$t14)], expr#16=[IS NOT NULL($t2)], expr#17=[true], expr#18=[IS NOT 
TRUE($t10)], expr#19=[AND($t16, $t17, $t18, $t14)], expr#20=[NOT($t16)], 
expr#21=[AND($t5, $t18, $t14, $t20)], expr#22=[OR($t6, $t15, $t19, $t21)], 
expr#23=[NOT($t22)], SAL=[$t1], EXPR$1=[$t23])
   EnumerableJoin(condition=[true], joinType=[left])
     EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], SAL=[$t5])
       EnumerableTableScan(table=[[scott, EMP]])
@@ -1074,7 +1074,7 @@ from "scott".emp;
 (14 rows)
 
 !ok
-EnumerableCalc(expr#0..3=[{inputs}], expr#4=[IS NULL($t3)], expr#5=[false], 
expr#6=[CAST($t5):BOOLEAN], expr#7=[AND($t4, $t6)], expr#8=[=($t2, $t5)], 
expr#9=[IS TRUE($t8)], expr#10=[null], expr#11=[CAST($t10):BOOLEAN], 
expr#12=[NOT($t4)], expr#13=[AND($t9, $t11, $t12)], expr#14=[IS NOT NULL($t2)], 
expr#15=[true], expr#16=[CAST($t15):BOOLEAN], expr#17=[IS NOT TRUE($t8)], 
expr#18=[AND($t14, $t16, $t17, $t12)], expr#19=[NOT($t14)], expr#20=[AND($t6, 
$t17, $t12, $t19)], expr#21=[OR($t7, $t13, $t18, $t20)], expr#22=[NOT($t21)], 
SAL=[$t1], EXPR$1=[$t22])
+EnumerableCalc(expr#0..3=[{inputs}], expr#4=[IS NULL($t3)], expr#5=[false], 
expr#6=[AND($t4, $t5)], expr#7=[=($t2, $t5)], expr#8=[IS TRUE($t7)], 
expr#9=[null], expr#10=[CAST($t9):BOOLEAN], expr#11=[NOT($t4)], 
expr#12=[AND($t8, $t10, $t11)], expr#13=[IS NOT NULL($t2)], expr#14=[true], 
expr#15=[IS NOT TRUE($t7)], expr#16=[AND($t13, $t14, $t15, $t11)], 
expr#17=[NOT($t13)], expr#18=[AND($t5, $t15, $t11, $t17)], expr#19=[OR($t6, 
$t12, $t16, $t18)], expr#20=[NOT($t19)], SAL=[$t1], EXPR$1=[$t20])
   EnumerableJoin(condition=[true], joinType=[left])
     EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], SAL=[$t5])
       EnumerableTableScan(table=[[scott, EMP]])
@@ -1110,7 +1110,7 @@ from "scott".emp;
 (14 rows)
 
 !ok
-EnumerableCalc(expr#0..3=[{inputs}], expr#4=[IS NULL($t3)], expr#5=[false], 
expr#6=[CAST($t5):BOOLEAN], expr#7=[AND($t4, $t6)], expr#8=[=($t2, $t5)], 
expr#9=[null], expr#10=[IS NULL($t9)], expr#11=[OR($t8, $t10)], expr#12=[IS 
TRUE($t11)], expr#13=[null], expr#14=[CAST($t13):BOOLEAN], expr#15=[NOT($t4)], 
expr#16=[AND($t12, $t14, $t15)], expr#17=[IS NOT NULL($t2)], expr#18=[true], 
expr#19=[CAST($t18):BOOLEAN], expr#20=[IS NOT TRUE($t11)], expr#21=[AND($t17, 
$t19, $t20, $t15)], expr#22=[NOT($t17)], expr#23=[AND($t6, $t20, $t15, $t22)], 
expr#24=[OR($t7, $t16, $t21, $t23)], expr#25=[NOT($t24)], SAL=[$t1], 
EXPR$1=[$t25])
+EnumerableCalc(expr#0..3=[{inputs}], expr#4=[IS NULL($t3)], expr#5=[false], 
expr#6=[AND($t4, $t5)], expr#7=[=($t2, $t5)], expr#8=[null], expr#9=[IS 
NULL($t8)], expr#10=[OR($t7, $t9)], expr#11=[IS TRUE($t10)], expr#12=[null], 
expr#13=[CAST($t12):BOOLEAN], expr#14=[NOT($t4)], expr#15=[AND($t11, $t13, 
$t14)], expr#16=[IS NOT NULL($t2)], expr#17=[true], expr#18=[IS NOT 
TRUE($t10)], expr#19=[AND($t16, $t17, $t18, $t14)], expr#20=[NOT($t16)], 
expr#21=[AND($t5, $t18, $t14, $t20)], expr#22=[OR($t6, $t15, $t19, $t21)], 
expr#23=[NOT($t22)], SAL=[$t1], EXPR$1=[$t23])
   EnumerableJoin(condition=[true], joinType=[left])
     EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], SAL=[$t5])
       EnumerableTableScan(table=[[scott, EMP]])
@@ -1146,7 +1146,7 @@ from "scott".emp;
 (14 rows)
 
 !ok
-EnumerableCalc(expr#0..3=[{inputs}], expr#4=[IS NULL($t3)], expr#5=[false], 
expr#6=[CAST($t5):BOOLEAN], expr#7=[AND($t4, $t6)], expr#8=[=($t2, $t5)], 
expr#9=[null], expr#10=[IS NULL($t9)], expr#11=[OR($t8, $t10)], expr#12=[IS 
TRUE($t11)], expr#13=[null], expr#14=[CAST($t13):BOOLEAN], expr#15=[NOT($t4)], 
expr#16=[AND($t12, $t14, $t15)], expr#17=[IS NOT NULL($t2)], expr#18=[true], 
expr#19=[CAST($t18):BOOLEAN], expr#20=[IS NOT TRUE($t11)], expr#21=[AND($t17, 
$t19, $t20, $t15)], expr#22=[NOT($t17)], expr#23=[AND($t6, $t20, $t15, $t22)], 
expr#24=[OR($t7, $t16, $t21, $t23)], expr#25=[NOT($t24)], SAL=[$t1], 
EXPR$1=[$t25])
+EnumerableCalc(expr#0..3=[{inputs}], expr#4=[IS NULL($t3)], expr#5=[false], 
expr#6=[AND($t4, $t5)], expr#7=[=($t2, $t5)], expr#8=[null], expr#9=[IS 
NULL($t8)], expr#10=[OR($t7, $t9)], expr#11=[IS TRUE($t10)], expr#12=[null], 
expr#13=[CAST($t12):BOOLEAN], expr#14=[NOT($t4)], expr#15=[AND($t11, $t13, 
$t14)], expr#16=[IS NOT NULL($t2)], expr#17=[true], expr#18=[IS NOT 
TRUE($t10)], expr#19=[AND($t16, $t17, $t18, $t14)], expr#20=[NOT($t16)], 
expr#21=[AND($t5, $t18, $t14, $t20)], expr#22=[OR($t6, $t15, $t19, $t21)], 
expr#23=[NOT($t22)], SAL=[$t1], EXPR$1=[$t23])
   EnumerableJoin(condition=[true], joinType=[left])
     EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], SAL=[$t5])
       EnumerableTableScan(table=[[scott, EMP]])
@@ -1182,7 +1182,7 @@ from "scott".emp;
 (14 rows)
 
 !ok
-EnumerableCalc(expr#0..3=[{inputs}], expr#4=[IS NULL($t3)], expr#5=[false], 
expr#6=[CAST($t5):BOOLEAN], expr#7=[AND($t4, $t6)], expr#8=[=($t2, $t5)], 
expr#9=[null], expr#10=[IS NULL($t9)], expr#11=[OR($t8, $t10)], expr#12=[IS 
TRUE($t11)], expr#13=[null], expr#14=[CAST($t13):BOOLEAN], expr#15=[NOT($t4)], 
expr#16=[AND($t12, $t14, $t15)], expr#17=[IS NOT NULL($t2)], expr#18=[true], 
expr#19=[CAST($t18):BOOLEAN], expr#20=[IS NOT TRUE($t11)], expr#21=[AND($t17, 
$t19, $t20, $t15)], expr#22=[NOT($t17)], expr#23=[AND($t6, $t20, $t15, $t22)], 
expr#24=[OR($t7, $t16, $t21, $t23)], expr#25=[NOT($t24)], SAL=[$t1], 
EXPR$1=[$t25])
+EnumerableCalc(expr#0..3=[{inputs}], expr#4=[IS NULL($t3)], expr#5=[false], 
expr#6=[AND($t4, $t5)], expr#7=[=($t2, $t5)], expr#8=[null], expr#9=[IS 
NULL($t8)], expr#10=[OR($t7, $t9)], expr#11=[IS TRUE($t10)], expr#12=[null], 
expr#13=[CAST($t12):BOOLEAN], expr#14=[NOT($t4)], expr#15=[AND($t11, $t13, 
$t14)], expr#16=[IS NOT NULL($t2)], expr#17=[true], expr#18=[IS NOT 
TRUE($t10)], expr#19=[AND($t16, $t17, $t18, $t14)], expr#20=[NOT($t16)], 
expr#21=[AND($t5, $t18, $t14, $t20)], expr#22=[OR($t6, $t15, $t19, $t21)], 
expr#23=[NOT($t22)], SAL=[$t1], EXPR$1=[$t23])
   EnumerableJoin(condition=[true], joinType=[left])
     EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], SAL=[$t5])
       EnumerableTableScan(table=[[scott, EMP]])
@@ -1252,14 +1252,14 @@ from "scott".emp;
 (14 rows)
 
 !ok
-EnumerableCalc(expr#0..3=[{inputs}], expr#4=[IS NULL($t3)], expr#5=[false], 
expr#6=[CAST($t5):BOOLEAN], expr#7=[AND($t4, $t6)], expr#8=[=($t2, $t5)], 
expr#9=[IS TRUE($t8)], expr#10=[null], expr#11=[CAST($t10):BOOLEAN], 
expr#12=[NOT($t4)], expr#13=[AND($t9, $t11, $t12)], expr#14=[IS NOT NULL($t2)], 
expr#15=[true], expr#16=[CAST($t15):BOOLEAN], expr#17=[IS NOT TRUE($t8)], 
expr#18=[AND($t14, $t16, $t17, $t12)], expr#19=[NOT($t14)], expr#20=[AND($t6, 
$t17, $t12, $t19)], expr#21=[OR($t7, $t13, $t18, $t20)], expr#22=[NOT($t21)], 
SAL=[$t1], EXPR$1=[$t22])
+EnumerableCalc(expr#0..3=[{inputs}], expr#4=[IS NULL($t3)], expr#5=[false], 
expr#6=[AND($t4, $t5)], expr#7=[=($t2, $t5)], expr#8=[IS TRUE($t7)], 
expr#9=[null], expr#10=[CAST($t9):BOOLEAN], expr#11=[NOT($t4)], 
expr#12=[AND($t8, $t10, $t11)], expr#13=[IS NOT NULL($t2)], expr#14=[true], 
expr#15=[IS NOT TRUE($t7)], expr#16=[AND($t13, $t14, $t15, $t11)], 
expr#17=[NOT($t13)], expr#18=[AND($t5, $t15, $t11, $t17)], expr#19=[OR($t6, 
$t12, $t16, $t18)], expr#20=[NOT($t19)], SAL=[$t1], EXPR$1=[$t20])
   EnumerableJoin(condition=[true], joinType=[left])
     EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], SAL=[$t5])
       EnumerableTableScan(table=[[scott, EMP]])
     EnumerableLimit(fetch=[1])
       EnumerableSort(sort0=[$0], dir0=[DESC])
         EnumerableAggregate(group=[{0}], c=[COUNT()])
-          EnumerableCalc(expr#0..2=[{inputs}], expr#3=[true], expr#4=[10], 
expr#5=[CAST($t0):TINYINT], expr#6=[=($t4, $t5)], cs=[$t3], $condition=[$t6])
+          EnumerableCalc(expr#0..2=[{inputs}], expr#3=[true], expr#4=[10], 
expr#5=[=($t4, $t0)], cs=[$t3], $condition=[$t5])
             EnumerableTableScan(table=[[scott, DEPT]])
 !plan
 
@@ -1731,7 +1731,7 @@ select sal from "scott".emp e
 !ok
 EnumerableCalc(expr#0..3=[{inputs}], SAL=[$t2])
   EnumerableJoin(condition=[=($0, $3)], joinType=[inner])
-    EnumerableCalc(expr#0..2=[{inputs}], expr#3=[10], 
expr#4=[CAST($t0):TINYINT], expr#5=[=($t3, $t4)], DEPTNO=[$t0], 
$condition=[$t5])
+    EnumerableCalc(expr#0..2=[{inputs}], expr#3=[10], expr#4=[=($t3, $t0)], 
DEPTNO=[$t0], $condition=[$t4])
       EnumerableTableScan(table=[[scott, DEPT]])
     EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], SAL=[$t5], DEPTNO=[$t7])
       EnumerableTableScan(table=[[scott, EMP]])
@@ -1881,7 +1881,7 @@ EnumerableCalc(expr#0..4=[{inputs}], expr#5=[false], 
expr#6=[=($t4, $t5)], expr#
   EnumerableJoin(condition=[=($2, $3)], joinType=[left])
     EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], SAL=[$t5], DEPTNO=[$t7])
       EnumerableTableScan(table=[[scott, EMP]])
-    EnumerableCalc(expr#0..2=[{inputs}], expr#3=[true], expr#4=[10], 
expr#5=[CAST($t0):TINYINT], expr#6=[=($t4, $t5)], DEPTNO=[$t0], $f1=[$t3], 
$condition=[$t6])
+    EnumerableCalc(expr#0..2=[{inputs}], expr#3=[true], expr#4=[10], 
expr#5=[=($t4, $t0)], DEPTNO=[$t0], $f1=[$t3], $condition=[$t5])
       EnumerableTableScan(table=[[scott, DEPT]])
 !plan
 

Reply via email to