@SuppressWarnings("unchecked")
  @Override
  public Object process(Node nd, Stack<Node> stack,
                        NodeProcessorCtx procContext, Object... nodeOutputs)
      throws SemanticException {

    GroupByOperator groupByOperator = (GroupByOperator) nd;
    GroupByDesc desc = groupByOperator.getConf();

    if (desc.getMode() != Mode.HASH || groupByOperator.getStatistics().getBasicStatsState() != State.COMPLETE
        || groupByOperator.getStatistics().getColumnStatsState() != State.COMPLETE) {
      return null;
    }

    // compute product of distinct values of grouping columns
    List<ColStatistics> colStats = new ArrayList<>();
    for (int i = 0; i < desc.getKeys().size(); i++) {
      ColumnInfo ci = groupByOperator.getSchema().getSignature().get(i);
      colStats.add(
          groupByOperator.getStatistics().getColumnStatisticsFromColName(ci.getInternalName()));
    }
    Statistics parentStats = groupByOperator.getParentOperators().get(0).getStatistics();
    long ndvProduct = StatsUtils.computeNDVGroupingColumns(
        colStats, parentStats, true);
    // if ndvProduct is 0 then column stats state must be partial and we are missing
    if (ndvProduct == 0) {
      return null;
    }

    long numRows = parentStats.getNumRows();
    if (ndvProduct > numRows) {
      ndvProduct = numRows;
    }

    // change the min reduction for hash group by
    float defaultMinReductionHashAggrFactor = desc.getMinReductionHashAggr();
    float defaultMinReductionHashAggrFactorLowerBound = desc.getMinReductionHashAggrLowerBound();
    float minReductionHashAggrFactor = 1f - ((float) ndvProduct / numRows);
    if (minReductionHashAggrFactor < defaultMinReductionHashAggrFactorLowerBound) {
      minReductionHashAggrFactor = defaultMinReductionHashAggrFactorLowerBound;
    }
    if (minReductionHashAggrFactor < defaultMinReductionHashAggrFactor) {
      desc.setMinReductionHashAggr(minReductionHashAggrFactor);
      if (LOG.isDebugEnabled()) {
        LOG.debug("Minimum reduction for hash group by operator {} set to {}", groupByOperator, minReductionHashAggrFactor);
      }
    }

    return null;
  }
SetHashGroupByMinReduction determines the min reduction to perform
a hash aggregation for a group by.
ql/src/java/org/apache/hadoop/hive/ql/optimizer/SetHashGroupByMinReduction.java