@Override
  protected void buildSelectClauseForInsert(StringBuilder query) {
    // Need list of columns. If the source table is null, don't throw
    // an exception, but skip this part of the query
    if (sourceTab != null) {
      List<FieldSchema> cols = sourceTab.getSd().getCols();
      query.append("0, t2.writeId, t2.rowId DIV CEIL(numRows / ").append(numberOfBuckets)
          .append("), t2.rowId, t2.writeId, t2.data from (select ")
          .append("count(ROW__ID.writeId) over() as numRows, ");
      if (StringUtils.isNotBlank(orderByClause)) {
        // in case of reordering the data the writeids cannot be kept.
        query.append("MAX(ROW__ID.writeId) over() as writeId, row_number() OVER (").append(orderByClause);
      } else {
        query.append(
            "ROW__ID.writeId as writeId, row_number() OVER (order by ROW__ID.writeId ASC, ROW__ID.bucketId ASC, ROW__ID.rowId ASC");
      }
      query.append(") - 1 AS rowId, NAMED_STRUCT(");
      for (int i = 0; i < cols.size(); ++i) {
        query.append(i == 0 ? "'" : ", '").append(cols.get(i).getName()).append("', `").append(cols.get(i).getName())
            .append("`");
      }
      query.append(") as data");
    }
  }
Builds query strings that help with REBALANCE compaction of CRUD.
ql/src/java/org/apache/hadoop/hive/ql/txn/compactor/CompactionQueryBuilderForRebalance.java