private static Query maybeRewriteDistinctToGroupBy(Query query) {
    Objects.requireNonNull(query, "query");
    if (!query.distinct()) {
      return query;
    }
    if (query.distinct() && !query.groupBy().isEmpty()) {
      throw new UnsupportedOperationException(String.format("Both DISTINCT and groupBy (%s) are present. Use either.", query.groupBy()));
    }

    // "select distinct a, b, c from table"  equivalent to "select a, b, c from table group by a, b, c"
    return ImmutableQuery.builder().from(query).groupBy(query.projections()).build();
  }
Converts query of type {@code select distinct a, b, c from table} to {@code select a, b, c from table group by a, b, c}
so same pipelines (like {@link Group}) can be (re-)used. Does nothing if this is not a {@code distinct} query.
@param query query to be converted
@return new version of the query with projections copied to {@code groupBy} list or unmodified query.
@throws UnsupportedOperationException if query is "distinct" and {@code groupBy} is not empty
criteria/mongo/src/org/immutables/criteria/mongo/AggregationQuery.java