public RelNode trim(RelBuilder relBuilder, RelNode root) {
    try {
      REL_BUILDER.set(relBuilder);
      final int fieldCount = root.getRowType().getFieldCount();
      final ImmutableBitSet fieldsUsed = ImmutableBitSet.range(fieldCount);
      final Set<RelDataTypeField> extraFields = Collections.emptySet();
      final TrimResult trimResult =
          dispatchTrimFields(root, fieldsUsed, extraFields);
      if (!trimResult.right.isIdentity()) {
        throw new IllegalArgumentException();
      }
      if (LOG.isDebugEnabled()) {
        LOG.debug(
            RelOptUtil.dumpPlan("Plan after trimming unused fields",
                trimResult.left, SqlExplainFormat.TEXT,
                SqlExplainLevel.EXPPLAN_ATTRIBUTES));
      }
      return trimResult.left;
    } finally {
      REL_BUILDER.remove();
    }
  }
Trims unused fields from a relational expression.

<p>We presume that all fields of the relational expression are wanted by
its consumer, so only trim fields that are not used within the tree.

@param relBuilder Rel builder
@param root Root node of relational expression
@return Trimmed relational expression
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/RelFieldTrimmer.java