Repository: calcite
Updated Branches:
  refs/heads/master 458e2e1b2 -> d3b35a4b5


[CALCITE-2110] ArrayIndexOutOfBoundsException in RexSimplify when using 
ReduceExpressionsRule.JOIN_INSTANCE

Test case is from the JIRA case. (Godfrey He)


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

Branch: refs/heads/master
Commit: 2a171fb86a64e06208564d02ab24bca23d426a0a
Parents: 458e2e1
Author: Julian Hyde <[email protected]>
Authored: Tue Dec 26 16:56:11 2017 -0800
Committer: Julian Hyde <[email protected]>
Committed: Mon Jan 1 22:35:29 2018 -0800

----------------------------------------------------------------------
 .../org/apache/calcite/rex/RexSimplify.java     | 20 +++++++--
 .../apache/calcite/test/RelOptRulesTest.java    | 23 ++++++++++
 .../org/apache/calcite/test/RelOptRulesTest.xml | 44 ++++++++++++++++----
 3 files changed, 74 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/2a171fb8/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 34e3814..e1b0258 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
@@ -1015,7 +1015,10 @@ public class RexSimplify {
           removeUpperBound = true;
         } else {
           // Remove this term as it is contained in current upper bound
-          terms.set(terms.indexOf(term), rexBuilder.makeLiteral(true));
+          final int index = terms.indexOf(term);
+          if (index >= 0) {
+            terms.set(index, rexBuilder.makeLiteral(true));
+          }
         }
         break;
       }
@@ -1049,7 +1052,10 @@ public class RexSimplify {
           removeUpperBound = true;
         } else {
           // Remove this term as it is contained in current upper bound
-          terms.set(terms.indexOf(term), rexBuilder.makeLiteral(true));
+          final int index = terms.indexOf(term);
+          if (index >= 0) {
+            terms.set(index, rexBuilder.makeLiteral(true));
+          }
         }
         break;
       }
@@ -1084,7 +1090,10 @@ public class RexSimplify {
           removeLowerBound = true;
         } else {
           // Remove this term as it is contained in current lower bound
-          terms.set(terms.indexOf(term), rexBuilder.makeLiteral(true));
+          final int index = terms.indexOf(term);
+          if (index >= 0) {
+            terms.set(index, rexBuilder.makeLiteral(true));
+          }
         }
         break;
       }
@@ -1118,7 +1127,10 @@ public class RexSimplify {
           removeLowerBound = true;
         } else {
           // Remove this term as it is contained in current lower bound
-          terms.set(terms.indexOf(term), rexBuilder.makeLiteral(true));
+          final int index = terms.indexOf(term);
+          if (index >= 0) {
+            terms.set(index, rexBuilder.makeLiteral(true));
+          }
         }
         break;
       }

http://git-wip-us.apache.org/repos/asf/calcite/blob/2a171fb8/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 a738a00..057954b 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -2697,6 +2697,29 @@ public class RelOptRulesTest extends RelOptTestBase {
         FilterProjectTransposeRule.INSTANCE);
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-2110";>[CALCITE-2110]
+   * ArrayIndexOutOfBoundsException in RexSimplify when using
+   * ReduceExpressionsRule.JOIN_INSTANCE</a>. */
+  @Test public void testCorrelationScalarAggAndFilter() {
+    final String sql = "SELECT e1.empno\n"
+        + "FROM emp e1, dept d1 where e1.deptno = d1.deptno\n"
+        + "and e1.deptno < 10 and d1.deptno < 15\n"
+        + "and e1.sal > (select avg(sal) from emp e2 where e1.empno = 
e2.empno)";
+    HepProgram program = new HepProgramBuilder()
+        .addRuleInstance(ReduceExpressionsRule.PROJECT_INSTANCE)
+        .addRuleInstance(ReduceExpressionsRule.FILTER_INSTANCE)
+        .addRuleInstance(ReduceExpressionsRule.JOIN_INSTANCE)
+        .build();
+    sql(sql)
+        .withDecorrelation(true)
+        .withTrim(true)
+        .expand(true)
+        .withPre(program)
+        .with(program)
+        .checkUnchanged();
+  }
+
   @Test public void testProjectWindowTransposeRule() {
     HepProgram program = new HepProgramBuilder()
         .addRuleInstance(ProjectToWindowRule.PROJECT)

http://git-wip-us.apache.org/repos/asf/calcite/blob/2a171fb8/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 d573517..bde75fe 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -384,6 +384,32 @@ LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], 
HIREDATE=[$4], SAL=[$
 ]]>
         </Resource>
     </TestCase>
+    <TestCase name="testCorrelationScalarAggAndFilter">
+        <Resource name="sql">
+            <![CDATA[SELECT e1.empno
+FROM emp e1, dept d1 where e1.deptno = d1.deptno
+and e1.deptno < 10 and d1.deptno < 15
+and e1.sal > (select avg(sal) from emp e2 where e1.empno = e2.empno)]]>
+        </Resource>
+        <Resource name="planBefore">
+            <![CDATA[
+LogicalProject(EMPNO=[$0])
+  LogicalJoin(condition=[AND(=($0, $4), >($1, $5))], joinType=[inner])
+    LogicalJoin(condition=[=($2, $3)], joinType=[inner])
+      LogicalFilter(condition=[<($2, 10)])
+        LogicalProject(EMPNO=[$0], SAL=[$5], DEPTNO=[$7])
+          LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+      LogicalFilter(condition=[<($0, 15)])
+        LogicalProject(DEPTNO=[$0])
+          LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+    LogicalAggregate(group=[{0}], EXPR$0=[AVG($1)])
+      LogicalProject(EMPNO=[$1], SAL=[$0])
+        LogicalProject(SAL=[$1], EMPNO=[$0])
+          LogicalProject(EMPNO=[$0], SAL=[$5])
+            LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+        </Resource>
+    </TestCase>
     <TestCase name="testDecorrelateExists">
         <Resource name="sql">
             <![CDATA[select * from sales.emp
@@ -5782,14 +5808,14 @@ LogicalAggregate(group=[{}], EXPR$0=[SUM($4)])
         </Resource>
     </TestCase>
     <TestCase name="testPushAggregateSumThroughJoinAfterAggregateReduce">
-      <Resource name="sql">
-        <![CDATA[select e.job,sum(sal)
+        <Resource name="sql">
+            <![CDATA[select e.job,sum(sal)
 from (select * from sales.emp where empno = 10) as e
 join sales.dept as d on e.job = d.name
 group by e.job,d.name]]>
-      </Resource>
-      <Resource name="planBefore">
-        <![CDATA[
+        </Resource>
+        <Resource name="planBefore">
+            <![CDATA[
 LogicalAggregate(group=[{}], EXPR$0=[SUM($5)])
   LogicalJoin(condition=[=($2, $10)], joinType=[inner])
     LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], 
SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
@@ -5797,9 +5823,9 @@ LogicalAggregate(group=[{}], EXPR$0=[SUM($5)])
         LogicalTableScan(table=[[CATALOG, SALES, EMP]])
     LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
 ]]>
-      </Resource>
-      <Resource name="planAfter">
-        <![CDATA[
+        </Resource>
+        <Resource name="planAfter">
+            <![CDATA[
 LogicalProject(EXPR$0=[CASE(=($1, 0), null, $0)])
   LogicalAggregate(group=[{}], EXPR$0=[$SUM0($5)], agg#1=[$SUM0($6)])
     LogicalProject(JOB=[$0], EXPR$0=[$1], $f2=[$2], NAME=[$3], $f1=[$4], 
$f5=[CAST(*($1, $4)):INTEGER NOT NULL], $f6=[*($2, $4)])
@@ -5811,7 +5837,7 @@ LogicalProject(EXPR$0=[CASE(=($1, 0), null, $0)])
         LogicalAggregate(group=[{1}], agg#0=[COUNT()])
           LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
 ]]>
-      </Resource>
+        </Resource>
     </TestCase>
     <TestCase name="testReduceConstantsIsNotNull">
         <Resource name="sql">

Reply via email to