提交 0d27db6c authored 作者: Thomas Mueller's avatar Thomas Mueller

Queries "group by" that contain a subquery with an aggregate function returned…

Queries "group by" that contain a subquery with an aggregate function returned the wrong result in some cases.
上级 7a163bbc
...@@ -21,6 +21,8 @@ Change Log ...@@ -21,6 +21,8 @@ Change Log
<ul><li>Improved spatial index and data type. <ul><li>Improved spatial index and data type.
</li><li>Issue 467: OSGi Class Loader (ability to create reference to class </li><li>Issue 467: OSGi Class Loader (ability to create reference to class
in other ClassLoader, for example in another OSGi bundle). in other ClassLoader, for example in another OSGi bundle).
</li><li>Queries "group by" that contain a subquery with an aggregate function
returned the wrong result in some cases.
</li><li>Fix bug in unique and non-unique hash indexes which manifested as incorrect results </li><li>Fix bug in unique and non-unique hash indexes which manifested as incorrect results
when the search key was a different cardinal type from the table index key. when the search key was a different cardinal type from the table index key.
e.g. where the one was INT and the other was LONG e.g. where the one was INT and the other was LONG
......
...@@ -160,7 +160,8 @@ public class Select extends Query { ...@@ -160,7 +160,8 @@ public class Select extends Query {
private void queryGroupSorted(int columnCount, ResultTarget result) { private void queryGroupSorted(int columnCount, ResultTarget result) {
int rowNumber = 0; int rowNumber = 0;
setCurrentRowNumber(0); setCurrentRowNumber(0);
Value[] previousKeyValues = null; currentGroup = null;
Value[] previousKeyValues = null;
while (topTableFilter.next()) { while (topTableFilter.next()) {
setCurrentRowNumber(rowNumber + 1); setCurrentRowNumber(rowNumber + 1);
if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) { if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) {
...@@ -312,6 +313,7 @@ public class Select extends Query { ...@@ -312,6 +313,7 @@ public class Select extends Query {
ValueHashMap<HashMap<Expression, Object>> groups = ValueHashMap.newInstance(); ValueHashMap<HashMap<Expression, Object>> groups = ValueHashMap.newInstance();
int rowNumber = 0; int rowNumber = 0;
setCurrentRowNumber(0); setCurrentRowNumber(0);
currentGroup = null;
ValueArray defaultGroup = ValueArray.get(new Value[0]); ValueArray defaultGroup = ValueArray.get(new Value[0]);
while (topTableFilter.next()) { while (topTableFilter.next()) {
setCurrentRowNumber(rowNumber + 1); setCurrentRowNumber(rowNumber + 1);
...@@ -604,8 +606,6 @@ public class Select extends Query { ...@@ -604,8 +606,6 @@ public class Select extends Query {
throw DbException.getUnsupportedException("FOR UPDATE && AGGREGATE"); throw DbException.getUnsupportedException("FOR UPDATE && AGGREGATE");
} else if (topTableFilter.getJoin() != null) { } else if (topTableFilter.getJoin() != null) {
throw DbException.getUnsupportedException("FOR UPDATE && JOIN"); throw DbException.getUnsupportedException("FOR UPDATE && JOIN");
} else if (topTableFilter.getJoin() != null) {
throw DbException.getUnsupportedException("FOR UPDATE && JOIN");
} }
} }
topTableFilter.lock(session, exclusive, exclusive); topTableFilter.lock(session, exclusive, exclusive);
......
...@@ -40,6 +40,7 @@ public class TestOptimizations extends TestBase { ...@@ -40,6 +40,7 @@ public class TestOptimizations extends TestBase {
@Override @Override
public void test() throws Exception { public void test() throws Exception {
deleteDb("optimizations"); deleteDb("optimizations");
testGroupSubquery();
testAnalyzeLob(); testAnalyzeLob();
testLike(); testLike();
testExistsSubquery(); testExistsSubquery();
...@@ -73,6 +74,28 @@ public class TestOptimizations extends TestBase { ...@@ -73,6 +74,28 @@ public class TestOptimizations extends TestBase {
testConvertOrToIn(); testConvertOrToIn();
deleteDb("optimizations"); deleteDb("optimizations");
} }
private void testGroupSubquery() throws Exception {
Connection conn = getConnection("optimizations");
Statement stat = conn.createStatement();
stat.execute("create table t1(id int)");
stat.execute("create table t2(id int)");
stat.execute("insert into t1 values(2), (2), (3)");
stat.execute("insert into t2 values(2), (3)");
stat.execute("create index t1id_index on t1(id)");
ResultSet rs;
rs = stat.executeQuery("select id, (select count(*) from t2 " +
"where t2.id = t1.id) cc from t1 group by id order by id");
rs.next();
assertEquals(2, rs.getInt(1));
assertEquals(1, rs.getInt(2));
rs.next();
assertEquals(3, rs.getInt(1));
assertEquals(1, rs.getInt(2));
rs.next();
stat.execute("drop table t1, t2");
conn.close();
}
private void testAnalyzeLob() throws Exception { private void testAnalyzeLob() throws Exception {
Connection conn = getConnection("optimizations"); Connection conn = getConnection("optimizations");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论