throw in AggregateFunctionCombinatorIf instead

This commit is contained in:
Nickita Taranov 2021-08-08 22:31:48 +03:00
parent 36aaabfd3a
commit 3b13b79d65
4 changed files with 11 additions and 19 deletions

View File

@ -1,5 +1,5 @@
#include <AggregateFunctions/AggregateFunctionIf.h>
#include <AggregateFunctions/AggregateFunctionCombinatorFactory.h> #include <AggregateFunctions/AggregateFunctionCombinatorFactory.h>
#include <AggregateFunctions/AggregateFunctionIf.h>
#include "AggregateFunctionNull.h" #include "AggregateFunctionNull.h"
@ -11,6 +11,7 @@ namespace ErrorCodes
extern const int LOGICAL_ERROR; extern const int LOGICAL_ERROR;
extern const int ILLEGAL_TYPE_OF_ARGUMENT; extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int ILLEGAL_AGGREGATION;
} }
class AggregateFunctionCombinatorIf final : public IAggregateFunctionCombinator class AggregateFunctionCombinatorIf final : public IAggregateFunctionCombinator
@ -37,6 +38,10 @@ public:
const DataTypes & arguments, const DataTypes & arguments,
const Array & params) const override const Array & params) const override
{ {
if (nested_function->getName().find(getName()) != String::npos)
{
throw Exception(ErrorCodes::ILLEGAL_AGGREGATION, "nested function for {0}-combinator must not have {0}-combinator", getName());
}
return std::make_shared<AggregateFunctionIf>(nested_function, arguments, params); return std::make_shared<AggregateFunctionIf>(nested_function, arguments, params);
} }
}; };

View File

@ -49,7 +49,6 @@ namespace ErrorCodes
extern const int BAD_ARGUMENTS; extern const int BAD_ARGUMENTS;
extern const int SYNTAX_ERROR; extern const int SYNTAX_ERROR;
extern const int LOGICAL_ERROR; extern const int LOGICAL_ERROR;
extern const int ILLEGAL_AGGREGATION;
} }
@ -521,8 +520,8 @@ bool ParserFilterClause::parseImpl(Pos & pos, ASTPtr & node, Expected & expected
assert(node); assert(node);
ASTFunction & function = dynamic_cast<ASTFunction &>(*node); ASTFunction & function = dynamic_cast<ASTFunction &>(*node);
ParserToken parser_openging_bracket(TokenType::OpeningRoundBracket); ParserToken parser_opening_bracket(TokenType::OpeningRoundBracket);
if (!parser_openging_bracket.ignore(pos, expected)) if (!parser_opening_bracket.ignore(pos, expected))
{ {
return false; return false;
} }
@ -534,7 +533,7 @@ bool ParserFilterClause::parseImpl(Pos & pos, ASTPtr & node, Expected & expected
} }
ParserExpressionList parser_condition(false); ParserExpressionList parser_condition(false);
ASTPtr condition; ASTPtr condition;
if (!parser_condition.parse(pos, condition, expected)) if (!parser_condition.parse(pos, condition, expected) || condition->children.size() != 1)
{ {
return false; return false;
} }
@ -545,17 +544,6 @@ bool ParserFilterClause::parseImpl(Pos & pos, ASTPtr & node, Expected & expected
return false; return false;
} }
if (function.name.find("If") != String::npos)
{
throw Exception(
ErrorCodes::ILLEGAL_AGGREGATION,
"Filter clause provided for an aggregating function (" + function.name + ") already containing If suffix");
}
if (condition->children.empty())
{
throw Exception(ErrorCodes::SYNTAX_ERROR, "Empty condition for WHERE");
}
function.name += "If"; function.name += "If";
function.arguments->children.push_back(condition->children[0]); function.arguments->children.push_back(condition->children[0]);
return true; return true;

View File

@ -1 +1 @@
SELECT sumForEachMergeArray(y) FROM (SELECT sumForEachStateForEachIfArrayIfMerge(x) AS y FROM (SELECT sumForEachStateForEachIfArrayIfState([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]], [1], 1) AS x)); SELECT sumForEachMergeArray(y) FROM (SELECT sumForEachStateForEachIfArrayMerge(x) AS y FROM (SELECT sumForEachStateForEachIfArrayState([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]], [1]) AS x));

View File

@ -1,4 +1,3 @@
SELECT argMax(number, number + 1) FILTER(WHERE number != 99) FROM numbers(100) ; SELECT argMax(number, number + 1) FILTER(WHERE number != 99) FROM numbers(100) ;
SELECT sum(number) FILTER(WHERE number % 2 == 0) FROM numbers(100); SELECT sum(number) FILTER(WHERE number % 2 == 0) FROM numbers(100);
SELECT sumIfOrNull(number, number % 2 == 1) FILTER(WHERE number % 2 == 0) FROM numbers(100); -- { clientError 184 } SELECT sumIfOrNull(number, number % 2 == 1) FILTER(WHERE number % 2 == 0) FROM numbers(100); -- { serverError 184 }
SELECT sum(number) FILTER(WHERE) FROM numbers(100); -- { clientError 62 }