2017-07-11 17:43:51 +00:00
|
|
|
#include <AggregateFunctions/parseAggregateFunctionParameters.h>
|
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
extern const int PARAMETERS_TO_AGGREGATE_FUNCTIONS_MUST_BE_LITERALS;
|
|
|
|
}
|
|
|
|
|
|
|
|
Array getAggregateFunctionParametersArray(const ASTPtr & expression_list, const std::string & error_context)
|
|
|
|
{
|
2019-03-15 17:09:14 +00:00
|
|
|
const ASTs & parameters = expression_list->children;
|
2017-07-11 17:43:51 +00:00
|
|
|
if (parameters.empty())
|
|
|
|
throw Exception("Parameters list to aggregate functions cannot be empty", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
Array params_row(parameters.size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < parameters.size(); ++i)
|
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * literal = parameters[i]->as<ASTLiteral>();
|
2019-03-08 09:45:34 +00:00
|
|
|
if (!literal)
|
2017-07-11 17:43:51 +00:00
|
|
|
{
|
|
|
|
throw Exception("Parameters to aggregate functions must be literals" + (error_context.empty() ? "" : " (in " + error_context +")"),
|
|
|
|
ErrorCodes::PARAMETERS_TO_AGGREGATE_FUNCTIONS_MUST_BE_LITERALS);
|
|
|
|
}
|
|
|
|
|
2019-03-08 09:45:34 +00:00
|
|
|
params_row[i] = literal->value;
|
2017-07-11 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return params_row;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void getAggregateFunctionNameAndParametersArray(
|
|
|
|
const std::string & aggregate_function_name_with_params,
|
|
|
|
std::string & aggregate_function_name,
|
|
|
|
Array & aggregate_function_parameters,
|
|
|
|
const std::string & error_context)
|
|
|
|
{
|
|
|
|
if (aggregate_function_name_with_params.back() != ')')
|
|
|
|
{
|
|
|
|
aggregate_function_name = aggregate_function_name_with_params;
|
|
|
|
aggregate_function_parameters = Array();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t pos = aggregate_function_name_with_params.find('(');
|
|
|
|
if (pos == std::string::npos || pos + 2 >= aggregate_function_name_with_params.size())
|
|
|
|
throw Exception(aggregate_function_name_with_params + " doesn't look like aggregate function name in " + error_context + ".",
|
|
|
|
ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
aggregate_function_name = aggregate_function_name_with_params.substr(0, pos);
|
|
|
|
std::string parameters_str = aggregate_function_name_with_params.substr(pos + 1, aggregate_function_name_with_params.size() - pos - 2);
|
|
|
|
|
|
|
|
if (aggregate_function_name.empty())
|
|
|
|
throw Exception(aggregate_function_name_with_params + " doesn't look like aggregate function name in " + error_context + ".",
|
|
|
|
ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
ParserExpressionList params_parser(false);
|
|
|
|
ASTPtr args_ast = parseQuery(params_parser,
|
|
|
|
parameters_str.data(), parameters_str.data() + parameters_str.size(),
|
2018-04-16 15:11:13 +00:00
|
|
|
"parameters of aggregate function in " + error_context, 0);
|
2017-07-11 17:43:51 +00:00
|
|
|
|
2019-03-11 12:49:39 +00:00
|
|
|
if (args_ast->children.empty())
|
2017-07-11 17:43:51 +00:00
|
|
|
throw Exception("Incorrect list of parameters to aggregate function "
|
|
|
|
+ aggregate_function_name, ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
aggregate_function_parameters = getAggregateFunctionParametersArray(args_ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|