提交 980d4471 authored 作者: Thomas Mueller's avatar Thomas Mueller

The optimization for COUNT(..) on columns that are not nullable was also used…

The optimization for COUNT(..) on columns that are not nullable was also used for COUNT(DISTINCT ..), which is incorrect.
上级 23b10725
...@@ -548,7 +548,10 @@ public class Aggregate extends Expression { ...@@ -548,7 +548,10 @@ public class Aggregate extends Expression {
if (visitor.getType() == ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL) { if (visitor.getType() == ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL) {
switch (type) { switch (type) {
case COUNT: case COUNT:
return on.getNullable() == Column.NOT_NULLABLE && visitor.getTable().canGetRowCount(); if (!distinct && on.getNullable() == Column.NOT_NULLABLE) {
return visitor.getTable().canGetRowCount();
}
return false;
case COUNT_ALL: case COUNT_ALL:
return visitor.getTable().canGetRowCount(); return visitor.getTable().canGetRowCount();
case MIN: case MIN:
......
...@@ -37,6 +37,7 @@ public class TestCases extends TestBase { ...@@ -37,6 +37,7 @@ public class TestCases extends TestBase {
} }
public void test() throws Exception { public void test() throws Exception {
testCountDistinctNotNull();
testDependencies(); testDependencies();
testConvertType(); testConvertType();
testSortedSelect(); testSortedSelect();
...@@ -93,6 +94,17 @@ public class TestCases extends TestBase { ...@@ -93,6 +94,17 @@ public class TestCases extends TestBase {
deleteDb("cases"); deleteDb("cases");
} }
private void testCountDistinctNotNull() throws SQLException {
deleteDb("cases");
Connection conn = getConnection("cases");
Statement stat = conn.createStatement();
stat.execute("create table test(id int not null) as select 1 from system_range(1, 10)");
ResultSet rs = stat.executeQuery("select count(distinct id) from test");
rs.next();
assertEquals(1, rs.getInt(1));
conn.close();
}
private void testDependencies() throws SQLException { private void testDependencies() throws SQLException {
deleteDb("cases"); deleteDb("cases");
Connection conn = getConnection("cases"); Connection conn = getConnection("cases");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论