mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
BUG: @OrderBy on chained properties do not work with distinct (fix)
This commit is contained in:
1 parent
b2e8e628fa
commit
6561f6f5e7
6 files changed
+63
-35
No files matched your search
@@ -585,16 +585,11 @@ final class CQueryBuilder {
|
|||||||
}
|
}
|
||||||
if (distinct && dbOrderBy != null) {
|
if (distinct && dbOrderBy != null) {
|
||||||
// add the orderBy columns to the select clause (due to distinct)
|
// add the orderBy columns to the select clause (due to distinct)
|
||||||
final OrderBy<?> orderBy = query.getOrderBy();
|
String[] tokens = DbOrderByTrim.trim(dbOrderBy).split(",");
|
||||||
if (orderBy != null && orderBy.supportsSelect()) {
|
for (String token : tokens) {
|
||||||
String trimmed = DbOrderByTrim.trim(dbOrderBy);
|
token = token.trim();
|
||||||
if (query.isSingleAttribute() && select.selectSql().startsWith(trimmed)) {
|
if (!DbOrderByTrim.contains(select.selectSql(), token)) {
|
||||||
// NOP, already in SQL
|
sb.append(", ").append(token);
|
||||||
// TODO: what to do if we select("id").orderBy("prop,id")?
|
|
||||||
// Can we live with a query like "select t0.id, t0.prop, t0.id from"
|
|
||||||
// or should we elliminate the second "t0.id" from select
|
|
||||||
} else {
|
|
||||||
sb.append(", ").append(trimmed);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -643,7 +638,7 @@ final class CQueryBuilder {
|
|||||||
private void appendHistoryAsOfPredicate() {
|
private void appendHistoryAsOfPredicate() {
|
||||||
if (query.isAsOfBaseTable()) {
|
if (query.isAsOfBaseTable()) {
|
||||||
query.incrementAsOfTableCount();
|
query.incrementAsOfTableCount();
|
||||||
if (!historySupport.isStandardsBased()){
|
if (!historySupport.isStandardsBased()) {
|
||||||
appendAndOrWhere();
|
appendAndOrWhere();
|
||||||
sb.append(historySupport.asOfPredicate(request.baseTableAlias()));
|
sb.append(historySupport.asOfPredicate(request.baseTableAlias()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
final class DbOrderByTrim {
|
final class DbOrderByTrim {
|
||||||
|
|
||||||
private static final Pattern orderByTrim = Pattern.compile("(?i) asc\\b| desc\\b|\\b nulls first\\b|\\b nulls last\\b");
|
private static final Pattern orderByTrim = Pattern.compile("(?i) asc\\b| desc\\b| nulls first\\b| nulls last\\b");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert the dbOrderBy clause to be safe for adding to select or distinct on.
|
* Convert the dbOrderBy clause to be safe for adding to select or distinct on.
|
||||||
@@ -14,4 +14,21 @@ final class DbOrderByTrim {
|
|||||||
return orderByTrim.matcher(dbOrderBy).replaceAll("");
|
return orderByTrim.matcher(dbOrderBy).replaceAll("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks, if <code>sql</code> contains the <code>column</code>.
|
||||||
|
* <p>
|
||||||
|
* SQL is normally comma separated: "t0.id, t1.id, t2.name"
|
||||||
|
*/
|
||||||
|
static boolean contains(String sql, String column) {
|
||||||
|
if (sql.endsWith(column)) { // simplest way. sql ends with the column
|
||||||
|
return true;
|
||||||
|
} else if (sql.contains(column)) {
|
||||||
|
// We need to check, if it is really the correct column,
|
||||||
|
// sql="t0.name_short, t0.name_long" will match on column="t0.name"
|
||||||
|
return sql.contains(column + ",");
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -172,7 +172,7 @@ public final class SqlTreeBuilder {
|
|||||||
String[] split = idCols.split(",");
|
String[] split = idCols.split(",");
|
||||||
for (String col : split) {
|
for (String col : split) {
|
||||||
col = col.trim();
|
col = col.trim();
|
||||||
if (!dbOrderBy.contains(col)) {
|
if (!DbOrderByTrim.contains(dbOrderBy, col)) {
|
||||||
sb.append(", ").append(col);
|
sb.append(", ").append(col);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -149,9 +149,9 @@ public class TestAggregationCount extends BaseTestCase {
|
|||||||
|
|
||||||
String sql = sqlOf(query, 5);
|
String sql = sqlOf(query, 5);
|
||||||
if (isH2()) {
|
if (isH2()) {
|
||||||
assertThat(sql).contains("select distinct t0.id, t0.name, count(u1.id), sum(u1.my_units), sum(u1.my_units * u1.amount), sum(u1.my_units), t0.name from tevent_one t0 ");
|
assertThat(sql).contains("select distinct t0.id, t0.name, count(u1.id), sum(u1.my_units), sum(u1.my_units * u1.amount) from tevent_one t0 ");
|
||||||
} else if (isPostgresCompatible()) {
|
} else if (isPostgresCompatible()) {
|
||||||
assertThat(sql).contains("t0.name, count(u1.id), sum(u1.my_units), sum(u1.my_units * u1.amount), sum(u1.my_units), t0.name from tevent_one t0 ");
|
assertThat(sql).contains("t0.name, count(u1.id), sum(u1.my_units), sum(u1.my_units * u1.amount) from tevent_one t0 ");
|
||||||
}
|
}
|
||||||
assertThat(sql).contains("from tevent_one t0 join tevent_many u1 on u1.event_id = t0.id ");
|
assertThat(sql).contains("from tevent_one t0 join tevent_many u1 on u1.event_id = t0.id ");
|
||||||
assertThat(sql).contains(" group by t0.id, t0.name ");
|
assertThat(sql).contains(" group by t0.id, t0.name ");
|
||||||
|
|||||||
@@ -1,12 +1,18 @@
|
|||||||
package org.tests.query.orderby;
|
package org.tests.query.orderby;
|
||||||
|
|
||||||
import io.ebean.xtest.BaseTestCase;
|
|
||||||
import io.ebean.DB;
|
import io.ebean.DB;
|
||||||
import io.ebean.Query;
|
import io.ebean.Query;
|
||||||
import io.ebean.xtest.IgnorePlatform;
|
|
||||||
import io.ebean.annotation.Platform;
|
import io.ebean.annotation.Platform;
|
||||||
|
import io.ebean.xtest.BaseTestCase;
|
||||||
|
import io.ebean.xtest.IgnorePlatform;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.tests.model.basic.*;
|
import org.tests.model.basic.Customer;
|
||||||
|
import org.tests.model.basic.EBasic;
|
||||||
|
import org.tests.model.basic.EBasicTree;
|
||||||
|
import org.tests.model.basic.MRole;
|
||||||
|
import org.tests.model.basic.MUser;
|
||||||
|
import org.tests.model.basic.MUserType;
|
||||||
|
import org.tests.model.basic.ResetBasicData;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -65,13 +71,23 @@ public class TestOrderByWithDistinct extends BaseTestCase {
|
|||||||
|
|
||||||
query.findList();
|
query.findList();
|
||||||
// we expect t2.name in this query
|
// we expect t2.name in this query
|
||||||
assertSql(query).startsWith("select distinct t0.id, t0.parent_id, t0.ref_id, t1.id, t1.parent_id, t1.ref_id, t2.name "
|
if (platformDistinctOn()) {
|
||||||
+ "from e_basic_tree t0 "
|
assertSql(query).startsWith("select distinct on (t0.id, t2.name, t1.id) t0.id, t0.parent_id, t0.ref_id, t1.id, t1.parent_id, t1.ref_id, t2.name "
|
||||||
+ "left join e_basic_tree t1 on t1.parent_id = t0.id "
|
+ "from e_basic_tree t0 "
|
||||||
+ "join e_basic_tree u1 on u1.parent_id = t0.id "
|
+ "left join e_basic_tree t1 on t1.parent_id = t0.id "
|
||||||
+ "join e_basic u2 on u2.id = u1.ref_id left "
|
+ "join e_basic_tree u1 on u1.parent_id = t0.id "
|
||||||
+ "join e_basic t2 on t2.id = t1.ref_id where u2.status = ? order by t0.id, t2.name");
|
+ "join e_basic u2 on u2.id = u1.ref_id left "
|
||||||
|
+ "join e_basic t2 on t2.id = t1.ref_id where u2.status = ? order by t0.id, t2.name");
|
||||||
|
} else {
|
||||||
|
assertSql(query).startsWith("select distinct t0.id, t0.parent_id, t0.ref_id, t1.id, t1.parent_id, t1.ref_id, t2.name "
|
||||||
|
+ "from e_basic_tree t0 "
|
||||||
|
+ "left join e_basic_tree t1 on t1.parent_id = t0.id "
|
||||||
|
+ "join e_basic_tree u1 on u1.parent_id = t0.id "
|
||||||
|
+ "join e_basic u2 on u2.id = u1.ref_id left "
|
||||||
|
+ "join e_basic t2 on t2.id = t1.ref_id where u2.status = ? order by t0.id, t2.name");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOrderByWithDistinct() {
|
public void testOrderByWithDistinct() {
|
||||||
Query<MUser> query = DB.find(MUser.class);
|
Query<MUser> query = DB.find(MUser.class);
|
||||||
@@ -86,9 +102,9 @@ public class TestOrderByWithDistinct extends BaseTestCase {
|
|||||||
assertSql(query).contains("from muser t0 limit 1000");
|
assertSql(query).contains("from muser t0 limit 1000");
|
||||||
}
|
}
|
||||||
query = DB.find(MUser.class)
|
query = DB.find(MUser.class)
|
||||||
.where()
|
.where()
|
||||||
.eq("roles.roleName", "A")
|
.eq("roles.roleName", "A")
|
||||||
.query();
|
.query();
|
||||||
query.findList();
|
query.findList();
|
||||||
assertSql(query).doesNotContain("order by");
|
assertSql(query).doesNotContain("order by");
|
||||||
assertSql(query).contains("select distinct");
|
assertSql(query).contains("select distinct");
|
||||||
@@ -103,12 +119,12 @@ public class TestOrderByWithDistinct extends BaseTestCase {
|
|||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
/*
|
/*
|
||||||
* Original conversation:
|
* Original conversation:
|
||||||
* https://groups.google.com/forum/?fromgroups=#!topic/ebean/uuvi1btdCDQ%5B1-25-false%5D
|
* https://groups.google.com/forum/?fromgroups=#!topic/ebean/uuvi1btdCDQ%5B1-25-false%5D
|
||||||
*
|
*
|
||||||
* This test exposes what may be a general problem with columns required by the order by phrase being omitted from the select.
|
* This test exposes what may be a general problem with columns required by the order by phrase being omitted from the select.
|
||||||
* I'm not sure this exposes all causes of the problem.
|
* I'm not sure this exposes all causes of the problem.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
MUserType ut = new MUserType("md");
|
MUserType ut = new MUserType("md");
|
||||||
DB.save(ut);
|
DB.save(ut);
|
||||||
|
|||||||
@@ -60,9 +60,9 @@ public class TestOrderByWithDistinctTake2 extends BaseTestCase {
|
|||||||
String generatedSql = sqlOf(query);
|
String generatedSql = sqlOf(query);
|
||||||
|
|
||||||
if (platformDistinctOn()) {
|
if (platformDistinctOn()) {
|
||||||
assertThat(generatedSql).contains("select distinct on (t0.name, t0.id) t0.id, t0.name, t0.id");
|
assertThat(generatedSql).contains("select distinct on (t0.name, t0.id) t0.id, t0.name");
|
||||||
} else {
|
} else {
|
||||||
assertThat(generatedSql).contains("select distinct t0.id, t0.name, t0.id");
|
assertThat(generatedSql).contains("select distinct t0.id, t0.name");
|
||||||
}
|
}
|
||||||
assertThat(generatedSql).contains("order by t0.name, t0.id desc");
|
assertThat(generatedSql).contains("order by t0.name, t0.id desc");
|
||||||
assertThat(generatedSql).contains("from o_customer t0 join contact u1 on u1.customer_id = t0.id");
|
assertThat(generatedSql).contains("from o_customer t0 join contact u1 on u1.customer_id = t0.id");
|
||||||
|
|||||||
Reference in new issue
Block a user