private RexInputRef field(RelBuilder builder, int inputCount, int offset) {
    for (int inputOrdinal = 0; ;) {
      final RelNode r = builder.peek(inputCount, inputOrdinal);
      if (offset < r.getRowType().getFieldCount()) {
        return builder.field(inputCount, inputOrdinal, offset);
      }
      ++inputOrdinal;
      offset -= r.getRowType().getFieldCount();
    }
  }
Returns a reference to a particular field, by offset, across several
inputs on a {@link RelBuilder}'s stack.
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSubQueryRemoveRule.java