This is an automated email from the ASF dual-hosted git repository.

rubenql pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
     new 43cf101  [CALCITE-4261] Join with three tables causes 
IllegalArgumentException in EnumerableBatchNestedLoopJoinRule
43cf101 is described below

commit 43cf101576e3108f94aa51ffe4257a1d2a37823e
Author: rubenada <rube...@gmail.com>
AuthorDate: Fri Sep 18 09:18:07 2020 +0100

    [CALCITE-4261] Join with three tables causes IllegalArgumentException in 
EnumerableBatchNestedLoopJoinRule
---
 .../EnumerableBatchNestedLoopJoinRule.java         | 16 +++++++--------
 .../EnumerableBatchNestedLoopJoinTest.java         | 24 ++++++++++++++++++++++
 2 files changed, 32 insertions(+), 8 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableBatchNestedLoopJoinRule.java
 
b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableBatchNestedLoopJoinRule.java
index eefa7df..a2c0d0a 100644
--- 
a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableBatchNestedLoopJoinRule.java
+++ 
b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableBatchNestedLoopJoinRule.java
@@ -92,16 +92,17 @@ public class EnumerableBatchNestedLoopJoinRule
     final RelBuilder relBuilder = call.builder();
 
     final Set<CorrelationId> correlationIds = new HashSet<>();
-    final ArrayList<RexNode> corrVar = new ArrayList<>();
+    final List<RexNode> corrVarList = new ArrayList<>();
 
     final int batchSize = config.batchSize();
     for (int i = 0; i < batchSize; i++) {
       CorrelationId correlationId = cluster.createCorrel();
       correlationIds.add(correlationId);
-      corrVar.add(
+      corrVarList.add(
           rexBuilder.makeCorrel(join.getLeft().getRowType(),
               correlationId));
     }
+    final RexNode corrVar0 = corrVarList.get(0);
 
     final ImmutableBitSet.Builder requiredColumns = ImmutableBitSet.builder();
 
@@ -114,11 +115,11 @@ public class EnumerableBatchNestedLoopJoinRule
               input.getIndex() - leftFieldCount);
         }
         requiredColumns.set(field);
-        return rexBuilder.makeFieldAccess(corrVar.get(0), field);
+        return rexBuilder.makeFieldAccess(corrVar0, field);
       }
     });
 
-    List<RexNode> conditionList = new ArrayList<>();
+    final List<RexNode> conditionList = new ArrayList<>();
     conditionList.add(condition);
 
     // Add batchSize-1 other conditions
@@ -126,7 +127,7 @@ public class EnumerableBatchNestedLoopJoinRule
       final int corrIndex = i;
       final RexNode condition2 = condition.accept(new RexShuttle() {
         @Override public RexNode visitCorrelVariable(RexCorrelVariable 
variable) {
-          return corrVar.get(corrIndex);
+          return variable.equals(corrVar0) ? corrVarList.get(corrIndex) : 
variable;
         }
       });
       conditionList.add(condition2);
@@ -134,9 +135,8 @@ public class EnumerableBatchNestedLoopJoinRule
 
     // Push a filter with batchSize disjunctions
     relBuilder.push(join.getRight()).filter(relBuilder.or(conditionList));
-    RelNode right = relBuilder.build();
+    final RelNode right = relBuilder.build();
 
-    JoinRelType joinType = join.getJoinType();
     call.transformTo(
         EnumerableBatchNestedLoopJoin.create(
             convert(join.getLeft(), join.getLeft().getTraitSet()
@@ -146,7 +146,7 @@ public class EnumerableBatchNestedLoopJoinRule
             join.getCondition(),
             requiredColumns.build(),
             correlationIds,
-            joinType));
+            join.getJoinType()));
   }
 
   /** Rule configuration. */
diff --git 
a/core/src/test/java/org/apache/calcite/test/enumerable/EnumerableBatchNestedLoopJoinTest.java
 
b/core/src/test/java/org/apache/calcite/test/enumerable/EnumerableBatchNestedLoopJoinTest.java
index 3c6095e..ad588cc 100644
--- 
a/core/src/test/java/org/apache/calcite/test/enumerable/EnumerableBatchNestedLoopJoinTest.java
+++ 
b/core/src/test/java/org/apache/calcite/test/enumerable/EnumerableBatchNestedLoopJoinTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.calcite.test.enumerable;
 
+import org.apache.calcite.adapter.enumerable.EnumerableBatchNestedLoopJoinRule;
 import org.apache.calcite.adapter.enumerable.EnumerableRules;
 import org.apache.calcite.adapter.java.ReflectiveSchema;
 import org.apache.calcite.config.CalciteConnectionProperty;
@@ -217,6 +218,29 @@ class EnumerableBatchNestedLoopJoinTest {
         .returnsUnordered("EXPR$0=1");
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-4261";>[CALCITE-4261]
+   * Join with three tables causes IllegalArgumentException
+   * in EnumerableBatchNestedLoopJoinRule</a>. */
+  @Test void doubleInnerBatchJoinTestSQL() {
+    tester(false, new JdbcTest.HrSchema())
+        .query("select e.name, d.name as dept, l.name as location "
+            + "from emps e join depts d on d.deptno <> e.salary "
+            + "join locations l on e.empid <> l.empid and d.deptno = l.empid")
+        .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
+          planner.removeRule(EnumerableRules.ENUMERABLE_CORRELATE_RULE);
+          // Use a small batch size, otherwise we will run into Janino's
+          // "InternalCompilerException: Code of method grows beyond 64 KB".
+          planner.addRule(
+              
EnumerableBatchNestedLoopJoinRule.Config.DEFAULT.withBatchSize(10).toRule());
+        })
+        .explainContains("EnumerableBatchNestedLoopJoin")
+        .returnsUnordered("name=Bill; dept=Sales; location=San Francisco",
+            "name=Eric; dept=Sales; location=San Francisco",
+            "name=Sebastian; dept=Sales; location=San Francisco",
+            "name=Theodore; dept=Sales; location=San Francisco");
+  }
+
   private CalciteAssert.AssertThat tester(boolean forceDecorrelate,
       Object schema) {
     return CalciteAssert.that()

Reply via email to