提交 6a20c110 authored 作者: Thomas Mueller's avatar Thomas Mueller

GROUP BY queries with a self-join (join to the same table) that were grouped by…

GROUP BY queries with a self-join (join to the same table) that were grouped by columns with indexes returned the wrong result in some cases.
上级 e407882c
...@@ -240,14 +240,14 @@ public class Select extends Query { ...@@ -240,14 +240,14 @@ public class Select extends Query {
if (index.getIndexType().getScan()) { if (index.getIndexType().getScan()) {
continue; continue;
} }
if (isGroupSortedIndex(index)) { if (isGroupSortedIndex(topTableFilter, index)) {
return index; return index;
} }
} }
return null; return null;
} }
private boolean isGroupSortedIndex(Index index) { private boolean isGroupSortedIndex(TableFilter tableFilter, Index index) {
// check that all the GROUP BY expressions are part of the index // check that all the GROUP BY expressions are part of the index
Column[] indexColumns = index.getColumns(); Column[] indexColumns = index.getColumns();
// also check that the first columns in the index are grouped // also check that the first columns in the index are grouped
...@@ -263,9 +263,11 @@ public class Select extends Query { ...@@ -263,9 +263,11 @@ public class Select extends Query {
} }
ExpressionColumn exprCol = (ExpressionColumn) expr; ExpressionColumn exprCol = (ExpressionColumn) expr;
for (int j = 0; j < indexColumns.length; ++j) { for (int j = 0; j < indexColumns.length; ++j) {
if (indexColumns[j].equals(exprCol.getColumn())) { if (tableFilter == exprCol.getTableFilter()) {
grouped[j] = true; if (indexColumns[j].equals(exprCol.getColumn())) {
continue outerLoop; grouped[j] = true;
continue outerLoop;
}
} }
} }
// We didn't find a matching index column // We didn't find a matching index column
......
--- special grammar and test cases --------------------------------------------------------------------------------------------- --- special grammar and test cases ---------------------------------------------------------------------------------------------
create table test(id identity) as select x from system_range(1, 4);
> ok
select a.id from test a inner join test b on a.id > b.id and b.id < 3 group by a.id;
> ID
> --
> 2
> 3
> 4
> rows: 3
drop table test;
> ok
create table test(id identity); create table test(id identity);
> ok > ok
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论