private RexNode getAvgInput(RexNode input, RexBuilder rexBuilder, RelDataTypeFactory typeFactory) {
    if (input.getType().getSqlTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      // GenericUDAFSum implicitly casts texts into DOUBLE. That conversion could generate NULL when the text is not a
      // valid numeric expression.
      // So, we have to explicitly cast those types here. Otherwise, The COUNT UDF can evaluate values with inconsistent
      // semantics with SUM.
      // For example, if a set of values are `"10"`, `"invalid"` and `"20"`, the average should be (10 + 20) / 2 = 15.
      // Without excluding invalid numeric texts, the result becomes (10 + 20) / 3 = 10.
      // Additionally, SUM and COUNT should refer to the same cast expression so that they can reuse the same column
      // as their input. Otherwise, we may need double columns of `x` for SUM and `CAST(x AS DOUBLE)` for COUNT.
      final RelDataType targetType = typeFactory.createSqlType(SqlTypeName.DOUBLE);
      final RelDataType nullableTargetType = typeFactory.createTypeWithNullability(targetType, true);
      return rexBuilder.makeCast(nullableTargetType, input);
    }
    return input;
  }
Do a shallow clone of oldAggRel and update aggCalls. Could be refactored
into Aggregate and subclasses - but it's only needed for some
subclasses.

@param relBuilder Builder of relational expressions; at the top of its
                  stack is its input
@param oldAggregate LogicalAggregate to clone.
@param newCalls  New list of AggregateCalls
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveAggregateReduceFunctionsRule.java