private static boolean remapJoinReferences(
          HiveLoptMultiJoin multiJoin,
          int factor,
          List<Integer> newJoinOrder,
          int newPos,
          int[] adjustments,
          int offset,
          int newOffset,
          boolean alwaysUseDefault) {
    boolean needAdjust = false;
    int defaultAdjustment = -offset + newOffset;
    if (!alwaysUseDefault
            && multiJoin.isRightFactorInRemovableSelfJoin(factor)
            && (newPos != 0)
            && newJoinOrder.get(newPos - 1).equals(
            multiJoin.getOtherSelfJoinFactor(factor))) {
      int nLeftFields =
              multiJoin.getNumFieldsInJoinFactor(
                      newJoinOrder.get(
                              newPos - 1));
      for (int i = 0;
           i < multiJoin.getNumFieldsInJoinFactor(factor);
           i++) {
        Integer leftOffset = multiJoin.getRightColumnMapping(factor, i);

        // if the left factor doesn't reference the column, then
        // use the default adjustment value
        if (leftOffset == null) {
          adjustments[i + offset] = defaultAdjustment;
        } else {
          adjustments[i + offset] =
                  -(offset + i) + (newOffset - nLeftFields)
                          + leftOffset;
        }
        if (adjustments[i + offset] != 0) {
          needAdjust = true;
        }
      }
    } else {
      if (defaultAdjustment != 0) {
        needAdjust = true;
        for (int i = 0;
             i < multiJoin.getNumFieldsInJoinFactor(
                     newJoinOrder.get(newPos));
             i++) {
          adjustments[i + offset] = defaultAdjustment;
        }
      }
    }

    return needAdjust;
  }
Sets an adjustment array based on where column references for a
particular factor end up as a result of a new join ordering.

<p>If the factor is not the right factor in a removable self-join, then
it needs to be adjusted as follows:

<ul>
<li>First subtract, based on where the factor was in the original join
ordering.
<li>Then add on the number of fields in the factors that now precede this
factor in the new join ordering.
</ul>

<p>If the factor is the right factor in a removable self-join and its
column reference can be mapped to the left factor in the self-join, then:

<ul>
<li>First subtract, based on where the column reference is in the new
join ordering.
<li>Then, add on the number of fields up to the start of the left factor
in the self-join in the new join ordering.
<li>Then, finally add on the offset of the corresponding column from the
left factor.
</ul>

<p>Note that this only applies if both factors in the self-join are in the
join ordering. If they are, then the left factor always precedes the
right factor in the join ordering.

@param multiJoin join factors being optimized
@param factor the factor whose references are being adjusted
@param newJoinOrder the new join ordering containing the factor
@param newPos the position of the factor in the new join ordering
@param adjustments the adjustments array that will be set
@param offset the starting offset within the original join ordering for
the columns of the factor being adjusted
@param newOffset the new starting offset in the new join ordering for the
columns of the factor being adjusted
@param alwaysUseDefault always use the default adjustment value
regardless of whether the factor is the right factor in a removable
self-join

@return true if at least one column from the factor requires adjustment
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveLoptOptimizeJoinRule.java