#1746 - Wrong SQL generated when using setDistinct and order by on aggregated column

This commit is contained in:
rob bygrave
2019-07-01 22:50:44 +12:00
parent 7f2e4f54ff
commit 52398317bc
5 changed files with 59 additions and 9 deletions
+20 -7
View File
@@ -80,7 +80,6 @@ public final class OrderBy<T> implements Serializable {
* Add a property with ascending order to this OrderBy.
*/
public Query<T> asc(String propertyName, String collation) {
list.add(new Property(propertyName, true, collation));
return query;
}
@@ -89,7 +88,6 @@ public final class OrderBy<T> implements Serializable {
* Add a property with descending order to this OrderBy.
*/
public Query<T> desc(String propertyName) {
list.add(new Property(propertyName, false));
return query;
}
@@ -98,7 +96,6 @@ public final class OrderBy<T> implements Serializable {
* Add a property with descending order to this OrderBy.
*/
public Query<T> desc(String propertyName, String collation) {
list.add(new Property(propertyName, false, collation));
return query;
}
@@ -108,7 +105,6 @@ public final class OrderBy<T> implements Serializable {
* Return true if the property is known to be contained in the order by clause.
*/
public boolean containsProperty(String propertyName) {
for (Property aList : list) {
if (propertyName.equals(aList.getProperty())) {
return true;
@@ -161,10 +157,9 @@ public final class OrderBy<T> implements Serializable {
* Return a copy of the OrderBy.
*/
public OrderBy<T> copy() {
OrderBy<T> copy = new OrderBy<>();
for (Property aList : list) {
copy.add(aList.copy());
for (Property property : list) {
copy.add(property.copy());
}
return copy;
}
@@ -241,6 +236,18 @@ public final class OrderBy<T> implements Serializable {
return this;
}
/**
* Return true if this order by can be used in select clause.
*/
public boolean supportsSelect() {
for (Property property : list) {
if (!property.supportsSelect()) {
return false;
}
}
return true;
}
/**
* A property and its ascending descending order.
*/
@@ -401,6 +408,12 @@ public final class OrderBy<T> implements Serializable {
this.ascending = ascending;
}
/**
* Support use in select clause if no collation or nulls ordering.
*/
boolean supportsSelect() {
return nulls == null && collation == null;
}
}
private void parse(String orderByClause) {
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.query;
import io.ebean.CountDistinctOrder;
import io.ebean.OrderBy;
import io.ebean.Query;
import io.ebean.RawSql;
import io.ebean.RawSqlBuilder;
@@ -600,7 +601,10 @@ class CQueryBuilder {
}
if (distinct && dbOrderBy != null && !query.isSingleAttribute()) {
// add the orderBy columns to the select clause (due to distinct)
sb.append(", ").append(DbOrderByTrim.trim(dbOrderBy));
final OrderBy<?> orderBy = query.getOrderBy();
if (orderBy != null && orderBy.supportsSelect()) {
sb.append(", ").append(DbOrderByTrim.trim(dbOrderBy));
}
}
}