private static CharSequence genPartitionClause(Table tbl, List<TransformSpec> partTransformSpec, int specId, 
    Map<String, String> partSpec, HiveConf conf) {
    boolean predPresent = partSpec.values().stream().anyMatch(Objects::nonNull);
    
    StringBuilder whereClause = new StringBuilder(" where ").append(
      partSpec.entrySet().stream()
        .filter(part -> part.getValue() != null)
        .map(part -> unparseIdentifier(part.getKey(), conf) + " = " 
            + genPartValueString(getColTypeOf(tbl, part.getKey()), part.getValue()))
        .collect(Collectors.joining(" and "))
    );

    if (specId >= 0) {
      whereClause.append((predPresent) ? " and " : "")
          .append(unparseIdentifier(PARTITION_SPEC_ID.getName(), conf) + "=" + specId);
      predPresent = true;
    }

    StringBuilder groupByClause = new StringBuilder(" group by ").append((
      (partTransformSpec != null) ?
          partTransformSpec.stream().map(spec -> spec.toHiveExpr(conf)) :
          tbl.getPartColNames().stream().map(col -> unparseIdentifier(col, conf))
      )
      .collect(Collectors.joining(", "))
    );

    // attach the predicate and group by to the return clause
    return predPresent ? whereClause.append(groupByClause) : groupByClause;
  }
ColumnStatsSemanticAnalyzer.
Handles semantic analysis and rewrite for gathering column statistics both at the level of a
partition and a table. Note that table statistics are implemented in SemanticAnalyzer.
ql/src/java/org/apache/hadoop/hive/ql/parse/ColumnStatsSemanticAnalyzer.java