zzwqqq commented on code in PR #5233:
URL: https://github.com/apache/calcite/pull/5233#discussion_r3980743807


##########
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableIEJoin.java:
##########
@@ -0,0 +1,273 @@
+/*
+ * 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.adapter.java.JavaTypeFactory;
+import org.apache.calcite.linq4j.function.Function1;
+import org.apache.calcite.linq4j.tree.BlockBuilder;
+import org.apache.calcite.linq4j.tree.Expression;
+import org.apache.calcite.linq4j.tree.ExpressionType;
+import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.linq4j.tree.ParameterExpression;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelNodes;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.metadata.RelMdUtil;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.calcite.util.BuiltInMethod;
+import org.apache.calcite.util.Util;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
+
+import static java.util.Objects.requireNonNull;
+
+/** Implementation of an inner IEJoin with two inequality predicates in
+ * {@link EnumerableConvention enumerable calling convention}. */
+public class EnumerableIEJoin extends Join implements EnumerableRel {
+  private final ImmutableList<Condition> conditions;
+
+  /** Creates an EnumerableIEJoin.
+   *
+   * <p>Use {@link #create} unless you know what you're doing. */
+  protected EnumerableIEJoin(RelOptCluster cluster, RelTraitSet traitSet,
+      RelNode left, RelNode right, RexNode condition) {
+    super(cluster, traitSet, ImmutableList.of(), left, right, condition,
+        ImmutableSet.of(), JoinRelType.INNER);
+    final List<RexNode> conjunctions = RelOptUtil.conjunctions(condition);
+    if (conjunctions.size() != 2) {
+      throw new IllegalArgumentException(
+          "condition must contain exactly two supported cross-input 
inequalities");
+    }
+    final int leftFieldCount = left.getRowType().getFieldCount();
+    final Condition first =
+        analyzeConjunction(conjunctions.get(0), leftFieldCount);
+    final Condition second =
+        analyzeConjunction(conjunctions.get(1), leftFieldCount);
+    if (first == null || second == null) {
+      throw new IllegalArgumentException(
+          "condition must contain supported cross-input inequalities");
+    }
+    final ImmutableList<Condition> conditions = ImmutableList.of(first, 
second);
+    for (Condition inequality : conditions) {
+      if (!supportsKeyTypes(left, right, inequality)) {
+        throw new IllegalArgumentException("unsupported IEJoin key types: left 
"
+            + 
left.getRowType().getFieldList().get(inequality.leftKey).getType()
+            + ", right "
+            + 
right.getRowType().getFieldList().get(inequality.rightKey).getType());
+      }
+    }
+    this.conditions = conditions;
+  }
+
+  /** Creates an EnumerableIEJoin. */
+  public static EnumerableIEJoin create(RelNode left, RelNode right,
+      RexNode condition) {
+    return new EnumerableIEJoin(left.getCluster(),
+        left.getCluster().traitSetOf(EnumerableConvention.INSTANCE),
+        left, right, condition);
+  }
+
+  @Override public EnumerableIEJoin copy(RelTraitSet traitSet,
+      RexNode condition, RelNode left, RelNode right, JoinRelType joinType,
+      boolean semiJoinDone) {
+    if (joinType != JoinRelType.INNER) {
+      throw new IllegalArgumentException("EnumerableIEJoin only supports inner 
joins");
+    }
+    return new EnumerableIEJoin(getCluster(), traitSet, left, right,
+        condition);
+  }
+
+  @Override public @Nullable RelOptCost computeSelfCost(RelOptPlanner planner,
+      RelMetadataQuery mq) {
+    final double leftRows = mq.getRowCount(left);
+    final double rightRows = mq.getRowCount(right);
+    double outputRows = mq.getRowCount(this);
+    if (RelNodes.COMPARATOR.compare(left, right) > 0) {
+      outputRows = RelMdUtil.addEpsilon(outputRows);
+    }
+    final double inputRows = leftRows + rightRows;
+    // IEJoin sorts the union twice, scans it once, then emits the result.
+    final double cost =
+        2D * Util.nLogN(inputRows) + inputRows + outputRows;
+    return planner.getCostFactory().makeCost(cost, 0, 0);
+  }
+
+  @Override public Result implement(EnumerableRelImplementor implementor,

Review Comment:
   Thanks. I've added examples of the generated Java next to the selectors, 
comparators, and final ieJoin call.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to