@Override
  public void onMatch(RelOptRuleCall call) {
    JoinPredicateInfo joinPredInfo;
    if (call.rel(0) instanceof HiveMultiJoin) {
      HiveMultiJoin multiJoin = call.rel(0);
      joinPredInfo = multiJoin.getJoinPredicateInfo();
    } else if (call.rel(0) instanceof HiveJoin) {
      HiveJoin hiveJoin = call.rel(0);
      joinPredInfo = hiveJoin.getJoinPredicateInfo();
    } else if (call.rel(0) instanceof Join) {
      Join join = call.rel(0);
      try {
        joinPredInfo =  HiveCalciteUtil.JoinPredicateInfo.constructJoinPredicateInfo(join);
      } catch (CalciteSemanticException e) {
        throw new RuntimeException(e);
      }
    } else {
      return;
    }

    for (RelNode child : call.rel(0).getInputs()) {
      if (((HepRelVertex)child).getCurrentRel() instanceof Exchange) {
        return;
      }
    }

    // Get key columns from inputs. Those are the columns on which we will distribute on.
    // It is also the columns we will sort on.
    List<RelNode> newInputs = new ArrayList<RelNode>();
    for (int i=0; i<call.rel(0).getInputs().size(); i++) {
      List<Integer> joinKeyPositions = new ArrayList<Integer>();
      ImmutableList.Builder<RexNode> joinExprsBuilder = new ImmutableList.Builder<RexNode>();
      Set<String> keySet = Sets.newHashSet();
      ImmutableList.Builder<RelFieldCollation> collationListBuilder =
              new ImmutableList.Builder<RelFieldCollation>();
      for (int j = 0; j < joinPredInfo.getEquiJoinPredicateElements().size(); j++) {
        JoinLeafPredicateInfo joinLeafPredInfo = joinPredInfo.
            getEquiJoinPredicateElements().get(j);
        for (RexNode joinExprNode : joinLeafPredInfo.getJoinExprs(i)) {
          if (keySet.add(joinExprNode.toString())) {
            joinExprsBuilder.add(joinExprNode);
          }
        }
        for (int pos : joinLeafPredInfo.getProjsJoinKeysInChildSchema(i)) {
          if (!joinKeyPositions.contains(pos)) {
            joinKeyPositions.add(pos);
            collationListBuilder.add(new RelFieldCollation(pos, RelFieldCollation.Direction.ASCENDING, defaultAscNullDirection));
          }
        }
      }
      HiveSortExchange exchange = HiveSortExchange.create(call.rel(0).getInput(i),
              new HiveRelDistribution(RelDistribution.Type.HASH_DISTRIBUTED, joinKeyPositions),
              new HiveRelCollation(collationListBuilder.build()),
              joinExprsBuilder.build());
      newInputs.add(exchange);
    }

    RelNode newOp;
    if (call.rel(0) instanceof HiveMultiJoin) {
      HiveMultiJoin multiJoin = call.rel(0);
      newOp = multiJoin.copy(multiJoin.getTraitSet(), newInputs);
    } else if (call.rel(0) instanceof Join) {
      Join join = call.rel(0);
      newOp = join.copy(join.getTraitSet(), join.getCondition(),
          newInputs.get(0), newInputs.get(1), join.getJoinType(),
          join.isSemiJoinDone());
    } else {
      return;
    }

    call.getPlanner().onCopy(call.rel(0), newOp);

    call.transformTo(newOp);
  }
Not an optimization rule.
Rule to aid in translation from Calcite tree -&gt; Hive tree.
Transforms :
  Left     Right                  Left                    Right
      \   /           -&gt;             \                   /
      Join                          HashExchange       HashExchange
                                            \         /
                                                Join
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveInsertExchange4JoinRule.java