private @Nullable RexNode removeExtraFilters(
      List<Integer> keys,
      int nFields,
      RexNode condition) {
    // recursively walk the expression; if all sub-expressions are
    // removed from one side of the expression, just return what remains
    // from the other side
    assert condition instanceof RexCall;
    RexCall call = (RexCall) condition;
    if (condition.isA(SqlKind.AND)) {
      List<RexNode> operands = call.getOperands();
      RexNode left =
          removeExtraFilters(
              keys,
              nFields,
              operands.get(0));
      RexNode right =
          removeExtraFilters(
              keys,
              nFields,
              operands.get(1));
      if (left == null) {
        return right;
      }
      if (right == null) {
        return left;
      }
      return rexBuilder.makeCall(
          SqlStdOperatorTable.AND,
          left,
          right);
    }

    // determine which side of the equality filter references the join
    // operand we're interested in; then, check if it is contained in
    // our key list
    assert call.getOperator() == SqlStdOperatorTable.EQUALS;
    List<RexNode> operands = call.getOperands();
    assert operands.get(0) instanceof RexInputRef;
    assert operands.get(1) instanceof RexInputRef;
    int idx = ((RexInputRef) operands.get(0)).getIndex();
    if (idx < nFields) {
      if (!keys.contains(idx)) {
        return null;
      }
    } else {
      idx = ((RexInputRef) operands.get(1)).getIndex();
      if (!keys.contains(idx)) {
        return null;
      }
    }
    return condition;
  }
Removes from an expression any sub-expressions that reference key values
that aren't contained in a key list passed in. The keys represent join
keys on one side of a join. The subexpressions are all assumed to be of
the form "tab1.col1 = tab2.col2".

@param keys join keys from one side of the join
@param nFields number of fields in the side of the join for which the
keys correspond
@param condition original expression

@return modified expression with filters that don't reference specified
keys removed
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveLoptSemiJoinOptimizer.java