private static RexNode addFilters(
          HiveLoptMultiJoin multiJoin,
          HiveLoptJoinTree leftTree,
          int leftIdx,
          HiveLoptJoinTree rightTree,
          List<RexNode> filtersToAdd,
          boolean adjust) {
    // loop through the remaining filters to be added and pick out the
    // ones that reference only the factors in the new join tree
    final RexBuilder rexBuilder =
            multiJoin.getMultiJoinRel().getCluster().getRexBuilder();
    final ImmutableBitSet.Builder childFactorBuilder =
            ImmutableBitSet.builder();
    childFactorBuilder.addAll(rightTree.getTreeOrder());
    if (leftIdx >= 0) {
      childFactorBuilder.set(leftIdx);
    } else {
      childFactorBuilder.addAll(leftTree.getTreeOrder());
    }
    for (int child : rightTree.getTreeOrder()) {
      childFactorBuilder.set(child);
    }

    final ImmutableBitSet childFactor = childFactorBuilder.build();
    RexNode condition = null;
    final ListIterator<RexNode> filterIter = filtersToAdd.listIterator();
    while (filterIter.hasNext()) {
      RexNode joinFilter = filterIter.next();
      ImmutableBitSet filterBitmap =
              multiJoin.getFactorsRefByJoinFilter(joinFilter);

      // if all factors in the join filter are in the join tree,
      // AND the filter to the current join condition
      if (childFactor.contains(filterBitmap)) {
        if (condition == null) {
          condition = joinFilter;
        } else {
          condition =
                  rexBuilder.makeCall(
                          SqlStdOperatorTable.AND,
                          condition,
                          joinFilter);
        }
        filterIter.remove();
      }
    }

    if (adjust && (condition != null)) {
      int [] adjustments = new int[multiJoin.getNumTotalFields()];
      if (needsAdjustment(
              multiJoin,
              adjustments,
              leftTree,
              rightTree,
              false)) {
        condition =
                condition.accept(
                        new RelOptUtil.RexInputConverter(
                                rexBuilder,
                                multiJoin.getMultiJoinFields(),
                                leftTree.getJoinTree().getRowType().getFieldList(),
                                rightTree.getJoinTree().getRowType().getFieldList(),
                                adjustments));
      }
    }

    if (condition == null) {
      condition = rexBuilder.makeLiteral(true);
    }

    return condition;
  }
Determines which join filters can be added to the current join tree. Note
that the join filter still reflects the original join ordering. It will
only be adjusted to reflect the new join ordering if the "adjust"
parameter is set to true.

@param multiJoin join factors being optimized
@param leftTree left subtree of the join tree
@param leftIdx if &ge; 0, only consider filters that reference leftIdx in
leftTree; otherwise, consider all filters that reference any factor in
leftTree
@param rightTree right subtree of the join tree
@param filtersToAdd remaining join filters that need to be added; those
that are added are removed from the list
@param adjust if true, adjust filter to reflect new join ordering

@return AND'd expression of the join filters that can be added to the
current join tree
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveLoptOptimizeJoinRule.java