korlov42 commented on a change in pull request #8456:
URL: https://github.com/apache/ignite/pull/8456#discussion_r531078939



##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rel/IgniteMergeJoin.java
##########
@@ -0,0 +1,634 @@
+/*
+ * 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.ignite.internal.processors.query.calcite.rel;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+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.RelCollation;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelDistribution;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelInput;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelNodes;
+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.JoinInfo;
+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.rex.RexNode;
+import org.apache.calcite.sql.SqlExplainLevel;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.ImmutableIntList;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.Util;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.CorrelationTrait;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.DistributionFunction;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistribution;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.RewindabilityTrait;
+import org.apache.ignite.internal.processors.query.calcite.trait.TraitUtils;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.TraitsAwareIgniteRel;
+import org.apache.ignite.internal.processors.query.calcite.util.Commons;
+import org.apache.ignite.internal.util.typedef.F;
+import org.jetbrains.annotations.Nullable;
+
+import static 
org.apache.calcite.rel.RelDistribution.Type.BROADCAST_DISTRIBUTED;
+import static org.apache.calcite.rel.RelDistribution.Type.HASH_DISTRIBUTED;
+import static org.apache.calcite.rel.RelDistribution.Type.SINGLETON;
+import static org.apache.calcite.rel.core.JoinRelType.INNER;
+import static org.apache.calcite.rel.core.JoinRelType.LEFT;
+import static org.apache.calcite.rel.core.JoinRelType.RIGHT;
+import static org.apache.calcite.util.NumberUtil.multiply;
+import static 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions.broadcast;
+import static 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions.hash;
+import static 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions.single;
+
+/** */
+public class IgniteMergeJoin extends Join implements TraitsAwareIgniteRel {
+    /** */
+    public IgniteMergeJoin(RelOptCluster cluster, RelTraitSet traitSet, 
RelNode left, RelNode right,
+        RexNode condition, Set<CorrelationId> variablesSet, JoinRelType 
joinType) {
+        super(cluster, traitSet, left, right, condition, variablesSet, 
joinType);
+    }
+
+    /** */
+    public IgniteMergeJoin(RelInput input) {
+        this(input.getCluster(),
+            input.getTraitSet().replace(IgniteConvention.INSTANCE),
+            input.getInputs().get(0),
+            input.getInputs().get(1),
+            input.getExpression("condition"),
+            
ImmutableSet.copyOf(Commons.transform(input.getIntegerList("variablesSet"), 
CorrelationId::new)),
+            input.getEnum("joinType", JoinRelType.class));
+    }
+
+    /** {@inheritDoc} */
+    @Override public Join copy(RelTraitSet traitSet, RexNode condition, 
RelNode left, RelNode right,
+        JoinRelType joinType, boolean semiJoinDone) {
+        return new IgniteMergeJoin(getCluster(), traitSet, left, right, 
condition, variablesSet, joinType);
+    }
+
+    /** {@inheritDoc} */
+    @Override public <T> T accept(IgniteRelVisitor<T> visitor) {
+        return visitor.visit(this);
+    }
+
+    /** {@inheritDoc} */
+    @Override public IgniteRel clone(RelOptCluster cluster, List<IgniteRel> 
inputs) {
+        return new IgniteMergeJoin(cluster, getTraitSet(), inputs.get(0), 
inputs.get(1), getCondition(), getVariablesSet(), getJoinType());
+    }
+
+    /** {@inheritDoc} */
+    @Override public RelWriter explainTerms(RelWriter pw) {
+        return super.explainTerms(pw)
+            .itemIf(
+                "variablesSet",
+                Commons.transform(variablesSet.asList(), CorrelationId::getId),
+                pw.getDetailLevel() == SqlExplainLevel.ALL_ATTRIBUTES
+            );
+    }
+
+    /** {@inheritDoc} */
+    @Override public List<Pair<RelTraitSet, List<RelTraitSet>>> 
deriveCollation(
+        RelTraitSet nodeTraits,
+        List<RelTraitSet> inputTraits
+    ) {
+        RelTraitSet left = inputTraits.get(0), right = inputTraits.get(1);
+        RelCollation leftCollation = TraitUtils.collation(left), 
rightCollation = TraitUtils.collation(right);
+
+        List<Integer> newLeftCollation, newRightCollation;
+
+        if (isPrefix(leftCollation.getKeys(), joinInfo.leftKeys)) { // 
preserve left collation
+            newLeftCollation = new ArrayList<>(leftCollation.getKeys());
+
+            Map<Integer, Integer> leftToRight = joinInfo.pairs().stream()
+                .collect(Collectors.toMap(p -> p.source, p -> p.target));
+
+            newRightCollation = 
newLeftCollation.stream().map(leftToRight::get).collect(Collectors.toList());
+        }
+        else if (isPrefix(rightCollation.getKeys(), joinInfo.rightKeys)) { // 
preserve right collation
+            newRightCollation = new ArrayList<>(rightCollation.getKeys());
+
+            Map<Integer, Integer> rightToLeft = joinInfo.pairs().stream()
+                .collect(Collectors.toMap(p -> p.target, p -> p.source));
+
+            newLeftCollation = 
newRightCollation.stream().map(rightToLeft::get).collect(Collectors.toList());
+        }
+        else { // generate new collations
+            // TODO: generate permutations when there will be multitraits
+
+            newLeftCollation = new ArrayList<>(joinInfo.leftKeys);
+            newRightCollation = new ArrayList<>(joinInfo.rightKeys);
+        }
+
+        leftCollation = createCollation(newLeftCollation);
+        rightCollation = createCollation(newRightCollation);
+
+        return ImmutableList.of(
+            Pair.of(
+                nodeTraits.replace(leftCollation),
+                ImmutableList.of(
+                    left.replace(leftCollation),
+                    right.replace(rightCollation)
+                )
+            )
+        );
+    }
+
+    /** {@inheritDoc} */
+    @Override public List<Pair<RelTraitSet, List<RelTraitSet>>> 
deriveRewindability(
+        RelTraitSet nodeTraits,
+        List<RelTraitSet> inputTraits
+    ) {
+        // The node is rewindable only if both sources are rewindable.
+
+        RelTraitSet left = inputTraits.get(0), right = inputTraits.get(1);
+
+        RewindabilityTrait leftRewindability = TraitUtils.rewindability(left);
+        RewindabilityTrait rightRewindability = 
TraitUtils.rewindability(right);
+
+        RelTraitSet outTraits, leftTraits, rightTraits;
+
+        if (leftRewindability.rewindable() && rightRewindability.rewindable()) 
{
+            outTraits = nodeTraits.replace(RewindabilityTrait.REWINDABLE);
+            leftTraits = left.replace(RewindabilityTrait.REWINDABLE);
+            rightTraits = right.replace(RewindabilityTrait.REWINDABLE);
+        }
+        else {
+            outTraits = nodeTraits.replace(RewindabilityTrait.ONE_WAY);
+            leftTraits = left.replace(RewindabilityTrait.ONE_WAY);
+            rightTraits = right.replace(RewindabilityTrait.ONE_WAY);
+        }
+
+        return ImmutableList.of(Pair.of(outTraits, 
ImmutableList.of(leftTraits, rightTraits)));
+    }
+
+    /** {@inheritDoc} */
+    @Override public List<Pair<RelTraitSet, List<RelTraitSet>>> 
deriveDistribution(
+        RelTraitSet nodeTraits,
+        List<RelTraitSet> inputTraits
+    ) {
+        // Tere are several rules:
+        // 1) any join is possible on broadcast or single distribution
+        // 2) hash distributed join is possible when join keys equal to source 
distribution keys
+        // 3) hash and broadcast distributed tables can be joined when join 
keys equal to hash
+        //    distributed table distribution keys and:
+        //      3.1) it's a left join and a hash distributed table is at left
+        //      3.2) it's a right join and a hash distributed table is at right
+        //      3.3) it's an inner join, this case a hash distributed table 
may be at any side
+
+        RelTraitSet left = inputTraits.get(0), right = inputTraits.get(1);
+
+        List<Pair<RelTraitSet, List<RelTraitSet>>> res = new ArrayList<>();
+
+        IgniteDistribution leftDistr = TraitUtils.distribution(left);
+        IgniteDistribution rightDistr = TraitUtils.distribution(right);
+
+        RelTraitSet outTraits, leftTraits, rightTraits;
+
+        if (leftDistr == broadcast() || rightDistr == broadcast()) {
+            outTraits = nodeTraits.replace(broadcast());
+            leftTraits = left.replace(broadcast());
+            rightTraits = right.replace(broadcast());
+
+            res.add(Pair.of(outTraits, ImmutableList.of(leftTraits, 
rightTraits)));
+        }
+
+        if (leftDistr == single() || rightDistr == single()) {
+            outTraits = nodeTraits.replace(single());
+            leftTraits = left.replace(single());
+            rightTraits = right.replace(single());
+
+            res.add(Pair.of(outTraits, ImmutableList.of(leftTraits, 
rightTraits)));
+        }
+
+        if (!F.isEmpty(joinInfo.pairs())) {
+            Set<DistributionFunction> functions = new HashSet<>();
+
+            if (leftDistr.getType() == HASH_DISTRIBUTED
+                && Objects.equals(joinInfo.leftKeys, leftDistr.getKeys()))
+                functions.add(leftDistr.function());
+
+            if (rightDistr.getType() == HASH_DISTRIBUTED
+                && Objects.equals(joinInfo.rightKeys, rightDistr.getKeys()))
+                functions.add(rightDistr.function());
+
+            functions.add(DistributionFunction.hash());
+
+            for (DistributionFunction function : functions) {
+                leftTraits = left.replace(hash(joinInfo.leftKeys, function));
+                rightTraits = right.replace(hash(joinInfo.rightKeys, 
function));
+
+                // TODO distribution multitrait support
+                outTraits = nodeTraits.replace(hash(joinInfo.leftKeys, 
function));
+                res.add(Pair.of(outTraits, ImmutableList.of(leftTraits, 
rightTraits)));
+
+                outTraits = nodeTraits.replace(hash(joinInfo.rightKeys, 
function));
+                res.add(Pair.of(outTraits, ImmutableList.of(leftTraits, 
rightTraits)));
+
+                if (joinType == INNER || joinType == LEFT) {
+                    outTraits = nodeTraits.replace(hash(joinInfo.leftKeys, 
function));
+                    leftTraits = left.replace(hash(joinInfo.leftKeys, 
function));
+                    rightTraits = right.replace(broadcast());
+
+                    res.add(Pair.of(outTraits, ImmutableList.of(leftTraits, 
rightTraits)));
+                }
+
+                if (joinType == INNER || joinType == RIGHT) {
+                    outTraits = nodeTraits.replace(hash(joinInfo.rightKeys, 
function));
+                    leftTraits = left.replace(broadcast());
+                    rightTraits = right.replace(hash(joinInfo.rightKeys, 
function));
+
+                    res.add(Pair.of(outTraits, ImmutableList.of(leftTraits, 
rightTraits)));
+                }
+            }
+        }
+
+        if (!res.isEmpty())
+            return ImmutableList.of();

Review comment:
       fixed




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


Reply via email to