private StringBuilder generateExportQuery(List<FieldSchema> partCols, ASTNode tokRefOrNameExportTable,
      ASTNode tableTree, String newTableName) throws SemanticException {
    StringBuilder rewrittenQueryStr = new StringBuilder("insert into ").append(newTableName);
    addPartitionColsToInsert(partCols, rewrittenQueryStr);
    rewrittenQueryStr.append(" select * from ").append(getFullTableNameForSQL(tokRefOrNameExportTable));
    //builds partition spec so we can build suitable WHERE clause
    TableSpec exportTableSpec = new TableSpec(db, conf, tableTree, false, true);
    if (exportTableSpec.getPartSpec() != null) {
      StringBuilder whereClause = null;
      int partColsIdx = -1; //keep track of corresponding col in partCols
      for (Map.Entry<String, String> ent : exportTableSpec.getPartSpec().entrySet()) {
        partColsIdx++;
        if (ent.getValue() == null) {
          continue; //partial spec
        }
        if (whereClause == null) {
          whereClause = new StringBuilder(" WHERE ");
        }
        if (whereClause.length() > " WHERE ".length()) {
          whereClause.append(" AND ");
        }
        whereClause.append(HiveUtils.unparseIdentifier(ent.getKey(), conf))
            .append(" = ").append(genPartValueString(partCols.get(partColsIdx).getType(), ent.getValue()));
      }
      if (whereClause != null) {
        rewrittenQueryStr.append(whereClause);
      }
    }
    return rewrittenQueryStr;
  }
Generate
insert into newTableName select * from ts <where partition spec>
for EXPORT command.
ql/src/java/org/apache/hadoop/hive/ql/parse/AcidExportSemanticAnalyzer.java