rubenada commented on a change in pull request #1369: [CALCITE-2979] Add a 
block-based nested loop join algorithm (Khawla Mouhoubi)
URL: https://github.com/apache/calcite/pull/1369#discussion_r315170167
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableBatchNestedLoopJoin.java
 ##########
 @@ -0,0 +1,252 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.adapter.enumerable;
+
+import org.apache.calcite.linq4j.function.Predicate2;
+import org.apache.calcite.linq4j.tree.BlockBuilder;
+import org.apache.calcite.linq4j.tree.DeclarationStatement;
+import org.apache.calcite.linq4j.tree.Expression;
+import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.linq4j.tree.ParameterExpression;
+import org.apache.calcite.linq4j.tree.Primitive;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelCollationTraitDef;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelWriter;
+import org.apache.calcite.rel.core.CorrelationId;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.metadata.RelMdCollation;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexProgramBuilder;
+import org.apache.calcite.util.BuiltInMethod;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.Pair;
+
+import com.google.common.collect.ImmutableList;
+
+import java.lang.reflect.Modifier;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+/** Implementation of batch nested loop join in
+ * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention 
enumerable calling convention}. */
+public class EnumerableBatchNestedLoopJoin extends Join implements 
EnumerableRel {
+
+  private final ImmutableBitSet requiredColumns;
+  protected EnumerableBatchNestedLoopJoin(
+      RelOptCluster cluster,
+      RelTraitSet traits,
+      RelNode left,
+      RelNode right,
+      RexNode condition,
+      Set<CorrelationId> variablesSet,
+      ImmutableBitSet requiredColumns,
+      JoinRelType joinType) {
+    super(cluster, traits, left, right, condition, variablesSet, joinType);
+    this.requiredColumns = requiredColumns;
+  }
+
+  public static EnumerableBatchNestedLoopJoin create(
+      RelNode left,
+      RelNode right,
+      RexNode condition,
+      ImmutableBitSet requiredColumns,
+      Set<CorrelationId> variablesSet,
+      JoinRelType joinType) {
+    final RelOptCluster cluster = left.getCluster();
+    final RelMetadataQuery mq = cluster.getMetadataQuery();
+    final RelTraitSet traitSet =
+        cluster.traitSetOf(EnumerableConvention.INSTANCE)
+            .replaceIfs(RelCollationTraitDef.INSTANCE,
+                () -> RelMdCollation.enumerableBatchNestedLoopJoin(mq, left, 
right, joinType));
+    return new EnumerableBatchNestedLoopJoin(
+        cluster,
+        traitSet,
+        left,
+        right,
+        condition,
+        variablesSet,
+        requiredColumns,
+        joinType);
+  }
+
+  @Override public EnumerableBatchNestedLoopJoin copy(RelTraitSet traitSet,
+      RexNode condition, RelNode left, RelNode right, JoinRelType joinType,
+      boolean semiJoinDone) {
+    return new EnumerableBatchNestedLoopJoin(getCluster(), traitSet,
+        left, right, condition, variablesSet, requiredColumns, joinType);
+  }
+
+  @Override public RelOptCost computeSelfCost(
+      final RelOptPlanner planner,
+      final RelMetadataQuery mq) {
+    double rowCount = mq.getRowCount(this);
+
+    final double rightRowCount = right.estimateRowCount(mq);
+    final double leftRowCount = left.estimateRowCount(mq);
+    if (Double.isInfinite(leftRowCount) || Double.isInfinite(rightRowCount)) {
+      return planner.getCostFactory().makeInfiniteCost();
+    }
+
+    Double restartCount = mq.getRowCount(getLeft()) / variablesSet.size();
+
+    RelOptCost rightCost = planner.getCost(getRight(), mq);
+    RelOptCost rescanCost =
+        rightCost.multiplyBy(Math.max(1.0, restartCount - 1));
+
+    // TODO Add cost of last loop (the one that looks for the match)
+    return planner.getCostFactory().makeCost(
+        rowCount + leftRowCount, 0, 0).plus(rescanCost);
+  }
+
+  @Override public RelWriter explainTerms(RelWriter pw) {
+    super.explainTerms(pw);
+    return pw.item("batchSize", variablesSet.size());
+  }
+
+  public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
 
 Review comment:
   `@Override` annotation is missing.
   Also, in this method (and in other methods of the class) there is a mix 
between local variables defined as `final`, and local variables not defined as 
`final` but which are effectively final, I think this should be homogenized.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to