private static String genRewrittenQuery(Table tbl, List<String> colNames, List<String> colTypes,
      HiveConf conf, List<TransformSpec> partTransformSpec, int specId, Map<String, String> partSpec, 
      boolean isPartitionStats, boolean useTableValues) {
    StringBuilder rewrittenQueryBuilder = new StringBuilder("select ");

    StringBuilder columnNamesBuilder = new StringBuilder();
    StringBuilder columnDummyValuesBuilder = new StringBuilder();
    for (int i = 0; i < colNames.size(); i++) {
      if (i > 0) {
        rewrittenQueryBuilder.append(", ");
        columnNamesBuilder.append(", ");
        columnDummyValuesBuilder.append(", ");
      }

      final String columnName = unparseIdentifier(colNames.get(i), conf);
      final TypeInfo typeInfo = TypeInfoUtils.getTypeInfoFromTypeString(colTypes.get(i));
      
      try {
        genComputeStats(rewrittenQueryBuilder, conf, i, columnName, typeInfo);
      } catch (SemanticException e) {
        throw new RuntimeException(e);
      }

      columnNamesBuilder.append(columnName);

      columnDummyValuesBuilder.append(
          "cast(null as " + typeInfo + ")");
    }

    if (isPartitionStats) {
      if (partTransformSpec == null) {
        for (FieldSchema fs : tbl.getPartCols()) {
          String identifier = unparseIdentifier(fs.getName(), conf);
          rewrittenQueryBuilder.append(", ").append(identifier);
          columnNamesBuilder.append(", ").append(identifier);

          columnDummyValuesBuilder.append(", cast(null as ")
            .append(TypeInfoUtils.getTypeInfoFromTypeString(fs.getType()).toString())
            .append(")");
        }
      } else {
        rewrittenQueryBuilder.append(", ")
          .append(TransformSpec.toNamedStruct(partTransformSpec, conf));
      }
    }

    rewrittenQueryBuilder.append(" from ");
    if (useTableValues) {
      //TABLE(VALUES(cast(null as int),cast(null as string))) AS tablename(col1,col2)
      rewrittenQueryBuilder.append("table(values(")
        // Values
        .append(columnDummyValuesBuilder)
        .append(")) as ")
        .append(unparseIdentifier(tbl.getTableName() ,conf))
        .append("(")
        // Columns
        .append(columnNamesBuilder)
        .append(")");
    } else {
      rewrittenQueryBuilder.append(unparseIdentifier(tbl.getDbName(), conf))
        .append(".")
        .append(unparseIdentifier(tbl.getTableName(), conf));
      
      if (tbl.getMetaTable() != null) {
        rewrittenQueryBuilder.append(".")
          .append(unparseIdentifier(tbl.getMetaTable(), conf));
      }
    }

    // If partition level statistics is requested, add predicate and group by as needed to rewritten
    // query
    if (isPartitionStats) {
      rewrittenQueryBuilder.append(genPartitionClause(tbl, partTransformSpec, specId, partSpec, conf));
    }

    String rewrittenQuery = rewrittenQueryBuilder.toString();
    rewrittenQuery = new VariableSubstitution(
        () -> SessionState.get().getHiveVariables()).substitute(conf, rewrittenQuery);
    return rewrittenQuery;
  }
Generates a SQL statement that will compute the stats for all columns
included in the input table.
ql/src/java/org/apache/hadoop/hive/ql/parse/ColumnStatsSemanticAnalyzer.java