From 936fc156406caaecf7794804bfd19cd4bda03c1b Mon Sep 17 00:00:00 2001 From: Michael Kolupaev Date: Mon, 13 Jan 2014 11:29:24 +0000 Subject: [PATCH] clickhouse: better error message when an aggregate function is missing. [#METR-9590] --- dbms/include/DB/Core/ErrorCodes.h | 1 + dbms/src/Interpreters/ExpressionAnalyzer.cpp | 23 +++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/dbms/include/DB/Core/ErrorCodes.h b/dbms/include/DB/Core/ErrorCodes.h index 3f611a264ef..28c4e0cc24f 100644 --- a/dbms/include/DB/Core/ErrorCodes.h +++ b/dbms/include/DB/Core/ErrorCodes.h @@ -220,6 +220,7 @@ namespace ErrorCodes EMPTY_QUERY, UNKNOWN_LOAD_BALANCING, CANNOT_STATVFS, + NOT_AN_AGGREGATE, POCO_EXCEPTION = 1000, STD_EXCEPTION, diff --git a/dbms/src/Interpreters/ExpressionAnalyzer.cpp b/dbms/src/Interpreters/ExpressionAnalyzer.cpp index c20430ab1e1..5c776a23205 100644 --- a/dbms/src/Interpreters/ExpressionAnalyzer.cpp +++ b/dbms/src/Interpreters/ExpressionAnalyzer.cpp @@ -703,7 +703,28 @@ void ExpressionAnalyzer::getActionsImpl(ASTPtr ast, bool no_subqueries, bool onl && actions_stack.getSampleBlock().has(ast->getColumnName())) return; - if (ASTFunction * node = dynamic_cast(&*ast)) + if (ASTIdentifier * node = dynamic_cast(&*ast)) + { + std::string name = node->getColumnName(); + if (!only_consts && !actions_stack.getSampleBlock().has(name)) + { + /// Запрошенного столбца нет в блоке. + /// Если такой столбец есть до агрегации, значит пользователь наверно забыл окружить его агрегатной функцией или добавить в GROUP BY. + + bool found = false; + for (NamesAndTypesList::const_iterator it = columns_after_array_join.begin(); + it != columns_after_array_join.end(); ++it) + if (it->first == name) + found = true; + + if (found) + throw Exception("Column " + name + " is not under aggregate function and not in GROUP BY.", + ErrorCodes::NOT_AN_AGGREGATE); + else + throw Exception("Unknown identifier: " + name, ErrorCodes::UNKNOWN_IDENTIFIER); + } + } + else if (ASTFunction * node = dynamic_cast(&*ast)) { if (node->kind == ASTFunction::LAMBDA_EXPRESSION) throw Exception("Unexpected expression", ErrorCodes::UNEXPECTED_EXPRESSION);