private static Path extractPath(Expression expression) {
    if (expression instanceof Path) {
      return (Path) expression;
    }

    if (expression instanceof Call) {
      Call call = (Call) expression;
      if (call.operator().arity() != Operator.Arity.UNARY) {
        throw new IllegalArgumentException("Expected unary operator but got " + call);
      }

      Expression arg = call.arguments().get(0);
      Preconditions.checkArgument(arg instanceof Path, "expected path got %s", arg);

      return (Path) arg;
    }

    throw new IllegalArgumentException("Can't extract path from " + expression);
  }
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