private static RexNode adjustFilter(
          HiveLoptMultiJoin multiJoin,
          HiveLoptJoinTree left,
          HiveLoptJoinTree right,
          RexNode condition,
          int factorAdded,
          List<Integer> origJoinOrder,
          List<RelDataTypeField> origFields) {
    final List<Integer> newJoinOrder = new ArrayList<>();
    left.getTreeOrder(newJoinOrder);
    right.getTreeOrder(newJoinOrder);

    int totalFields =
            left.getJoinTree().getRowType().getFieldCount()
                    + right.getJoinTree().getRowType().getFieldCount()
                    - multiJoin.getNumFieldsInJoinFactor(factorAdded);
    int [] adjustments = new int[totalFields];

    // go through each factor and adjust relative to the original
    // join order
    boolean needAdjust = false;
    int nFieldsNew = 0;
    for (int newPos = 0; newPos < newJoinOrder.size(); newPos++) {
      int nFieldsOld = 0;

      // no need to make any adjustments on the newly added factor
      int factor = newJoinOrder.get(newPos);
      if (factor != factorAdded) {
        // locate the position of the factor in the original join
        // ordering
        for (int pos : origJoinOrder) {
          if (factor == pos) {
            break;
          }
          nFieldsOld += multiJoin.getNumFieldsInJoinFactor(pos);
        }

        // fill in the adjustment array for this factor
        if (remapJoinReferences(
                multiJoin,
                factor,
                newJoinOrder,
                newPos,
                adjustments,
                nFieldsOld,
                nFieldsNew,
                false)) {
          needAdjust = true;
        }
      }
      nFieldsNew += multiJoin.getNumFieldsInJoinFactor(factor);
    }

    if (needAdjust) {
      RexBuilder rexBuilder =
              multiJoin.getMultiJoinRel().getCluster().getRexBuilder();
      condition =
              condition.accept(
                      new RelOptUtil.RexInputConverter(
                              rexBuilder,
                              origFields,
                              left.getJoinTree().getRowType().getFieldList(),
                              right.getJoinTree().getRowType().getFieldList(),
                              adjustments));
    }

    return condition;
  }
Adjusts a filter to reflect a newly added factor in the middle of an
existing join tree.

@param multiJoin join factors being optimized
@param left left subtree of the join
@param right right subtree of the join
@param condition current join condition
@param factorAdded index corresponding to the newly added factor
@param origJoinOrder original join order, before factor was pushed into
the tree
@param origFields fields from the original join before the factor was
added

@return modified join condition reflecting addition of the new factor
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveLoptOptimizeJoinRule.java