提交 4bef1ee5 authored 作者: Thomas Mueller's avatar Thomas Mueller

The optimization for "group by" was not working correctly if the group by column…

The optimization for "group by" was not working correctly if the group by column was aliased in the select list.
上级 1ee27d68
......@@ -15,6 +15,7 @@ import org.h2.command.CommandInterface;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.engine.Constants;
import org.h2.engine.Database;
import org.h2.engine.Session;
import org.h2.expression.Comparison;
import org.h2.expression.ConditionAndOr;
......@@ -261,7 +262,7 @@ public class Select extends Query {
if (!groupByExpression[i]) {
continue;
}
Expression expr = expressions.get(i);
Expression expr = expressions.get(i).getNonAliasExpression();
if (!(expr instanceof ExpressionColumn)) {
return false;
}
......@@ -642,6 +643,8 @@ public class Select extends Query {
}
private void expandColumnList() {
Database db = session.getDatabase();
// the expressions may change within the loop
for (int i = 0; i < expressions.size(); i++) {
Expression expr = expressions.get(i);
......@@ -661,8 +664,8 @@ public class Select extends Query {
} else {
TableFilter filter = null;
for (TableFilter f : filters) {
if (tableAlias.equals(f.getTableAlias())) {
if (schemaName == null || schemaName.equals(f.getSchemaName())) {
if (db.equalsIdentifiers(tableAlias, f.getTableAlias())) {
if (schemaName == null || db.equalsIdentifiers(schemaName, f.getSchemaName())) {
filter = f;
break;
}
......@@ -706,7 +709,7 @@ public class Select extends Query {
expressionSQL = null;
}
if (orderList != null) {
initOrder(expressions, expressionSQL, orderList, visibleColumnCount, distinct, filters);
initOrder(session, expressions, expressionSQL, orderList, visibleColumnCount, distinct, filters);
}
distinctColumnCount = expressions.size();
if (having != null) {
......@@ -717,6 +720,8 @@ public class Select extends Query {
havingIndex = -1;
}
Database db = session.getDatabase();
// first the select list (visible columns),
// then 'ORDER BY' expressions,
// then 'HAVING' expressions,
......@@ -731,7 +736,7 @@ public class Select extends Query {
int found = -1;
for (int j = 0; j < expSize; j++) {
String s2 = expressionSQL.get(j);
if (s2.equals(sql)) {
if (db.equalsIdentifiers(s2, sql)) {
found = j;
break;
}
......@@ -740,7 +745,7 @@ public class Select extends Query {
// special case: GROUP BY a column alias
for (int j = 0; j < expSize; j++) {
Expression e = expressions.get(j);
if (sql.equals(e.getAlias())) {
if (db.equalsIdentifiers(sql, e.getAlias())) {
found = j;
break;
}
......
......@@ -271,3 +271,26 @@ EXPLAIN SELECT * FROM TEST WHERE ID IN (10, 20) AND DATA IN (1, 2);
;
DROP TABLE TEST;
-------------------------------------------------------------------------------
-- Optimize GROUP BY
-------------------------------------------------------------------------------
-- This code snippet shows how GROUP BY is using an index.
-- Initialize the data
CREATE TABLE TEST(ID INT PRIMARY KEY, DATA INT);
INSERT INTO TEST SELECT X, X/10 FROM SYSTEM_RANGE(1, 100);
-- Display the query plan
EXPLAIN SELECT ID X, COUNT(*) FROM TEST GROUP BY ID;
--> SELECT
--> ID AS X,
--> COUNT(*)
--> FROM PUBLIC.TEST
--> /* PUBLIC.PRIMARY_KEY_2 */
--> GROUP BY ID
--> /* group sorted */
;
DROP TABLE TEST;
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论