提交 acb341b2 编写于 作者: A Alexey Milovidov

Unification of aggregate function combinators [#CLICKHOUSE-3511].

上级 9d60bcf3
......@@ -71,29 +71,16 @@ list (APPEND dbms_headers src/Functions/IFunction.h src/Functions/FunctionFactor
list (APPEND dbms_sources
src/AggregateFunctions/AggregateFunctionFactory.cpp
src/AggregateFunctions/AggregateFunctionCombinatorFactory.cpp
src/AggregateFunctions/FactoryHelpers.cpp
src/AggregateFunctions/AggregateFunctionState.cpp
src/AggregateFunctions/AggregateFunctionArray.cpp
src/AggregateFunctions/AggregateFunctionNull.cpp
src/AggregateFunctions/AggregateFunctionNothing.cpp
src/AggregateFunctions/AggregateFunctionForEach.cpp
src/AggregateFunctions/AggregateFunctionIf.cpp
src/AggregateFunctions/AggregateFunctionMerge.cpp
src/AggregateFunctions/AggregateFunctionCount.cpp
src/AggregateFunctions/parseAggregateFunctionParameters.cpp)
list (APPEND dbms_headers
src/AggregateFunctions/IAggregateFunction.h
src/AggregateFunctions/IAggregateFunctionCombinator.h
src/AggregateFunctions/AggregateFunctionFactory.h
src/AggregateFunctions/AggregateFunctionCombinatorFactory.h
src/AggregateFunctions/FactoryHelpers.h
src/AggregateFunctions/AggregateFunctionState.h
src/AggregateFunctions/AggregateFunctionArray.h
src/AggregateFunctions/AggregateFunctionNull.h
src/AggregateFunctions/AggregateFunctionNothing.h
src/AggregateFunctions/AggregateFunctionForEach.h
src/AggregateFunctions/AggregateFunctionIf.h
src/AggregateFunctions/AggregateFunctionMerge.h
src/AggregateFunctions/AggregateFunctionCount.h
src/AggregateFunctions/parseAggregateFunctionParameters.h)
list (APPEND dbms_sources src/TableFunctions/TableFunctionFactory.cpp)
......
#include <AggregateFunctions/AggregateFunctionArray.h>
#include <AggregateFunctions/AggregateFunctionCombinatorFactory.h>
namespace DB
{
AggregateFunctionPtr createAggregateFunctionArray(AggregateFunctionPtr & nested, const DataTypes & types)
namespace ErrorCodes
{
return std::make_shared<AggregateFunctionArray>(nested, types);
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
class AggregateFunctionCombinatorArray final : public IAggregateFunctionCombinator
{
public:
String getName() const override { return "Array"; };
DataTypes transformArguments(const DataTypes & arguments) const override
{
DataTypes nested_arguments;
for (const auto & type : arguments)
{
if (const DataTypeArray * array = typeid_cast<const DataTypeArray *>(type.get()))
nested_arguments.push_back(array->getNestedType());
else
throw Exception("Illegal type " + type->getName() + " of argument"
" for aggregate function with " + getName() + " suffix. Must be array.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
}
return nested_arguments;
}
AggregateFunctionPtr transformAggregateFunction(
const AggregateFunctionPtr & nested_function, const DataTypes & arguments, const Array &) const override
{
return std::make_shared<AggregateFunctionArray>(nested_function, arguments);
}
};
void registerAggregateFunctionCombinatorArray(AggregateFunctionCombinatorFactory & factory)
{
factory.registerCombinator(std::make_shared<AggregateFunctionCombinatorArray>());
}
}
#include <Common/StringUtils.h>
#include <AggregateFunctions/AggregateFunctionCombinatorFactory.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
void AggregateFunctionCombinatorFactory::registerCombinator(const AggregateFunctionCombinatorPtr & value)
{
if (!dict.emplace(value->getName(), value).second)
throw Exception("AggregateFunctionCombinatorFactory: the name '" + value->getName() + "' is not unique",
ErrorCodes::LOGICAL_ERROR);
}
AggregateFunctionCombinatorPtr AggregateFunctionCombinatorFactory::tryFindSuffix(const std::string & name) const
{
/// O(N) is ok for just a few combinators.
for (const auto & suffix_value : dict)
if (endsWith(name, suffix_value.first))
return suffix_value.second;
return {};
}
}
#pragma once
#include <AggregateFunctions/IAggregateFunctionCombinator.h>
#include <ext/singleton.h>
#include <string>
#include <unordered_map>
namespace DB
{
/** Create aggregate function combinator by matching suffix in aggregate function name.
*/
class AggregateFunctionCombinatorFactory final: public ext::singleton<AggregateFunctionCombinatorFactory>
{
public:
/// Not thread safe. You must register before using tryGet.
void registerCombinator(const AggregateFunctionCombinatorPtr & value);
/// Example: if the name is 'avgIf', it will return combinator -If.
AggregateFunctionCombinatorPtr tryFindSuffix(const std::string & name) const;
private:
std::unordered_map<std::string, AggregateFunctionCombinatorPtr> dict;
};
}
......@@ -24,14 +24,4 @@ void registerAggregateFunctionCount(AggregateFunctionFactory & factory)
factory.registerFunction("count", createAggregateFunctionCount, AggregateFunctionFactory::CaseInsensitive);
}
AggregateFunctionPtr createAggregateFunctionCountNotNull(const String & name, const DataTypes & argument_types, const Array & parameters)
{
assertNoParameters(name, parameters);
if (argument_types.size() == 1)
return std::make_shared<AggregateFunctionCountNotNullUnary>(argument_types[0]);
else
return std::make_shared<AggregateFunctionCountNotNullVariadic>(argument_types);
}
}
#include <AggregateFunctions/AggregateFunctionFactory.h>
#include <AggregateFunctions/AggregateFunctionCombinatorFactory.h>
#include <DataTypes/DataTypeAggregateFunction.h>
#include <DataTypes/DataTypeArray.h>
......@@ -21,34 +22,9 @@ namespace ErrorCodes
{
extern const int UNKNOWN_AGGREGATE_FUNCTION;
extern const int LOGICAL_ERROR;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
namespace
{
/// Does not check anything.
std::string trimRight(const std::string & in, const char * suffix)
{
return in.substr(0, in.size() - strlen(suffix));
}
}
AggregateFunctionPtr createAggregateFunctionArray(AggregateFunctionPtr & nested, const DataTypes & argument_types);
AggregateFunctionPtr createAggregateFunctionForEach(AggregateFunctionPtr & nested, const DataTypes & argument_types);
AggregateFunctionPtr createAggregateFunctionIf(AggregateFunctionPtr & nested, const DataTypes & argument_types);
AggregateFunctionPtr createAggregateFunctionState(AggregateFunctionPtr & nested, const DataTypes & argument_types, const Array & parameters);
AggregateFunctionPtr createAggregateFunctionMerge(const String & name, AggregateFunctionPtr & nested, const DataTypes & argument_types);
AggregateFunctionPtr createAggregateFunctionNullUnary(AggregateFunctionPtr & nested);
AggregateFunctionPtr createAggregateFunctionNullVariadic(AggregateFunctionPtr & nested, const DataTypes & argument_types);
AggregateFunctionPtr createAggregateFunctionCountNotNull(const String & name, const DataTypes & argument_types, const Array & parameters);
AggregateFunctionPtr createAggregateFunctionNothing();
void AggregateFunctionFactory::registerFunction(const String & name, Creator creator, CaseSensitiveness case_sensitiveness)
{
if (creator == nullptr)
......@@ -72,52 +48,29 @@ AggregateFunctionPtr AggregateFunctionFactory::get(
const Array & parameters,
int recursion_level) const
{
bool has_nullable_types = false;
bool has_null_types = false;
for (const auto & arg_type : argument_types)
{
if (arg_type->isNullable())
{
has_nullable_types = true;
if (arg_type->onlyNull())
{
has_null_types = true;
break;
}
}
}
/// If one of types is Nullable, we apply aggregate function combinator "Null".
if (has_nullable_types)
if (std::any_of(argument_types.begin(), argument_types.end(),
[](const auto & type) { return type->isNullable(); }))
{
/// Special case for 'count' function. It could be called with Nullable arguments
/// - that means - count number of calls, when all arguments are not NULL.
if (Poco::toLower(name) == "count")
return createAggregateFunctionCountNotNull(name, argument_types, parameters);
AggregateFunctionCombinatorPtr combinator = AggregateFunctionCombinatorFactory::instance().tryFindSuffix("Null");
if (!combinator)
throw Exception("Logical error: cannot find aggregate function combinator to apply a function to Nullable arguments.", ErrorCodes::LOGICAL_ERROR);
DataTypes nested_types = combinator->transformArguments(argument_types);
AggregateFunctionPtr nested_function;
if (has_null_types)
{
nested_function = createAggregateFunctionNothing();
}
else
{
DataTypes nested_argument_types;
nested_argument_types.reserve(argument_types.size());
for (const auto & arg_type : argument_types)
nested_argument_types.push_back(removeNullable(arg_type));
nested_function = getImpl(name, nested_argument_types, parameters, recursion_level);
}
if (argument_types.size() == 1)
return createAggregateFunctionNullUnary(nested_function);
else
return createAggregateFunctionNullVariadic(nested_function, argument_types);
/// A little hack - if we have NULL arguments, don't even create nested function.
/// Combinator will check if nested_function was created.
if (name == "count" || std::none_of(argument_types.begin(), argument_types.end(),
[](const auto & type) { return type->onlyNull(); }))
nested_function = getImpl(name, nested_types, parameters, recursion_level);
return combinator->transformAggregateFunction(nested_function, argument_types, parameters);
}
else
return getImpl(name, argument_types, parameters, recursion_level);
return getImpl(name, argument_types, parameters, recursion_level);
}
......@@ -127,14 +80,12 @@ AggregateFunctionPtr AggregateFunctionFactory::getImpl(
const Array & parameters,
int recursion_level) const
{
/// Find by exact match.
auto it = aggregate_functions.find(name);
if (it != aggregate_functions.end())
{
auto it = aggregate_functions.find(name);
if (it != aggregate_functions.end())
return it->second(name, argument_types, parameters);
}
return it->second(name, argument_types, parameters);
/// Find by case-insensitive name.
/// Combinators cannot apply for case insensitive (SQL-style) aggregate function names. Only for native names.
if (recursion_level == 0)
{
......@@ -147,78 +98,15 @@ AggregateFunctionPtr AggregateFunctionFactory::getImpl(
/// For every aggregate function 'agg' and combiner '-Comb' there is combined aggregate function with name 'aggComb',
/// that can have different number and/or types of arguments, different result type and different behaviour.
if (endsWith(name, "State"))
if (AggregateFunctionCombinatorPtr combinator = AggregateFunctionCombinatorFactory::instance().tryFindSuffix(name))
{
AggregateFunctionPtr nested = get(trimRight(name, "State"), argument_types, parameters, recursion_level + 1);
return createAggregateFunctionState(nested, argument_types, parameters);
}
if (endsWith(name, "Merge"))
{
if (argument_types.size() != 1)
throw Exception("Incorrect number of arguments for aggregate function " + name, ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
const DataTypeAggregateFunction * function = typeid_cast<const DataTypeAggregateFunction *>(argument_types[0].get());
if (!function)
throw Exception("Illegal type " + argument_types[0]->getName() + " of argument for aggregate function " + name
+ " must be AggregateFunction(...)", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
AggregateFunctionPtr nested = get(trimRight(name, "Merge"), function->getArgumentsDataTypes(), parameters, recursion_level + 1);
if (nested->getName() != function->getFunctionName())
throw Exception("Illegal type " + argument_types[0]->getName() + " of argument for aggregate function " + name
+ ", because it corresponds to different aggregate function: " + function->getFunctionName() + " instead of " + nested->getName(),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return createAggregateFunctionMerge(name, nested, argument_types);
}
if (endsWith(name, "If"))
{
if (argument_types.empty())
throw Exception("Incorrect number of arguments for aggregate function " + name,
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
if (combinator->getName() == "Null")
throw Exception("Aggregate function combinator 'Null' is only for internal usage", ErrorCodes::UNKNOWN_AGGREGATE_FUNCTION);
if (!typeid_cast<const DataTypeUInt8 *>(argument_types.back().get()))
throw Exception("Illegal type " + argument_types.back()->getName() + " of last argument for aggregate function " + name,
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
DataTypes nested_dt = argument_types;
nested_dt.pop_back();
AggregateFunctionPtr nested = get(trimRight(name, "If"), nested_dt, parameters, recursion_level + 1);
return createAggregateFunctionIf(nested, argument_types);
}
if (endsWith(name, "Array"))
{
DataTypes nested_arguments;
for (const auto & type : argument_types)
{
if (const DataTypeArray * array = typeid_cast<const DataTypeArray *>(type.get()))
nested_arguments.push_back(array->getNestedType());
else
throw Exception("Illegal type " + type->getName() + " of argument"
" for aggregate function " + name + ". Must be array.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
}
AggregateFunctionPtr nested = get(trimRight(name, "Array"), nested_arguments, parameters, recursion_level + 1);
return createAggregateFunctionArray(nested, argument_types);
}
if (endsWith(name, "ForEach"))
{
DataTypes nested_arguments;
for (const auto & type : argument_types)
{
if (const DataTypeArray * array = typeid_cast<const DataTypeArray *>(type.get()))
nested_arguments.push_back(array->getNestedType());
else
throw Exception("Illegal type " + type->getName() + " of argument"
" for aggregate function " + name + ". Must be array.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
}
AggregateFunctionPtr nested = get(trimRight(name, "ForEach"), nested_arguments, parameters, recursion_level + 1);
return createAggregateFunctionForEach(nested, argument_types);
String nested_name = name.substr(0, name.size() - combinator->getName().size());
DataTypes nested_types = combinator->transformArguments(argument_types);
AggregateFunctionPtr nested_function = getImpl(nested_name, nested_types, parameters, recursion_level + 1);
return combinator->transformAggregateFunction(nested_function, argument_types, parameters);
}
throw Exception("Unknown aggregate function " + name, ErrorCodes::UNKNOWN_AGGREGATE_FUNCTION);
......@@ -241,16 +129,8 @@ bool AggregateFunctionFactory::isAggregateFunctionName(const String & name, int
if (recursion_level == 0 && case_insensitive_aggregate_functions.count(Poco::toLower(name)))
return true;
if (endsWith(name, "State"))
return isAggregateFunctionName(trimRight(name, "State"), recursion_level + 1);
if (endsWith(name, "Merge"))
return isAggregateFunctionName(trimRight(name, "Merge"), recursion_level + 1);
if (endsWith(name, "If"))
return isAggregateFunctionName(trimRight(name, "If"), recursion_level + 1);
if (endsWith(name, "Array"))
return isAggregateFunctionName(trimRight(name, "Array"), recursion_level + 1);
if (endsWith(name, "ForEach"))
return isAggregateFunctionName(trimRight(name, "ForEach"), recursion_level + 1);
if (AggregateFunctionCombinatorPtr combinator = AggregateFunctionCombinatorFactory::instance().tryFindSuffix(name))
return isAggregateFunctionName(name.substr(0, name.size() - combinator->getName().size()), recursion_level + 1);
return false;
}
......
#include "AggregateFunctionForEach.h"
#include <AggregateFunctions/AggregateFunctionForEach.h>
#include <AggregateFunctions/AggregateFunctionCombinatorFactory.h>
namespace DB
{
AggregateFunctionPtr createAggregateFunctionForEach(AggregateFunctionPtr & nested, const DataTypes & arguments)
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
class AggregateFunctionCombinatorForEach final : public IAggregateFunctionCombinator
{
public:
String getName() const override { return "ForEach"; };
DataTypes transformArguments(const DataTypes & arguments) const override
{
DataTypes nested_arguments;
for (const auto & type : arguments)
{
if (const DataTypeArray * array = typeid_cast<const DataTypeArray *>(type.get()))
nested_arguments.push_back(array->getNestedType());
else
throw Exception("Illegal type " + type->getName() + " of argument"
" for aggregate function with " + getName() + " suffix. Must be array.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
}
return nested_arguments;
}
AggregateFunctionPtr transformAggregateFunction(
const AggregateFunctionPtr & nested_function, const DataTypes & arguments, const Array &) const override
{
return std::make_shared<AggregateFunctionForEach>(nested_function, arguments);
}
};
void registerAggregateFunctionCombinatorForEach(AggregateFunctionCombinatorFactory & factory)
{
return std::make_shared<AggregateFunctionForEach>(nested, arguments);
factory.registerCombinator(std::make_shared<AggregateFunctionCombinatorForEach>());
}
}
#include <AggregateFunctions/AggregateFunctionIf.h>
#include <AggregateFunctions/AggregateFunctionCombinatorFactory.h>
namespace DB
{
AggregateFunctionPtr createAggregateFunctionIf(AggregateFunctionPtr & nested, const DataTypes & types)
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
class AggregateFunctionCombinatorIf final : public IAggregateFunctionCombinator
{
public:
String getName() const override { return "If"; };
DataTypes transformArguments(const DataTypes & arguments) const override
{
if (arguments.empty())
throw Exception("Incorrect number of arguments for aggregate function with " + getName() + " suffix",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
if (!typeid_cast<const DataTypeUInt8 *>(arguments.back().get()))
throw Exception("Illegal type " + arguments.back()->getName() + " of last argument for aggregate function with " + getName() + " suffix",
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return DataTypes(arguments.begin(), std::prev(arguments.end()));
}
AggregateFunctionPtr transformAggregateFunction(
const AggregateFunctionPtr & nested_function, const DataTypes & arguments, const Array &) const override
{
return std::make_shared<AggregateFunctionIf>(nested_function, arguments);
}
};
void registerAggregateFunctionCombinatorIf(AggregateFunctionCombinatorFactory & factory)
{
return std::make_shared<AggregateFunctionIf>(nested, types);
factory.registerCombinator(std::make_shared<AggregateFunctionCombinatorIf>());
}
}
#include <AggregateFunctions/AggregateFunctionMerge.h>
#include <AggregateFunctions/FactoryHelpers.h>
#include <AggregateFunctions/AggregateFunctionCombinatorFactory.h>
#include <DataTypes/DataTypeAggregateFunction.h>
namespace DB
{
AggregateFunctionPtr createAggregateFunctionMerge(const String & name, AggregateFunctionPtr & nested, const DataTypes & argument_types)
namespace ErrorCodes
{
assertUnary(name, argument_types);
return std::make_shared<AggregateFunctionMerge>(nested, *argument_types[0]);
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int BAD_ARGUMENTS;
}
class AggregateFunctionCombinatorMerge final : public IAggregateFunctionCombinator
{
public:
String getName() const override { return "Merge"; };
DataTypes transformArguments(const DataTypes & arguments) const override
{
if (arguments.size() != 1)
throw Exception("Incorrect number of arguments for aggregate function with " + getName() + " suffix", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
const DataTypePtr & argument = arguments[0];
const DataTypeAggregateFunction * function = typeid_cast<const DataTypeAggregateFunction *>(argument.get());
if (!function)
throw Exception("Illegal type " + argument->getName() + " of argument for aggregate function with " + getName() + " suffix"
+ " must be AggregateFunction(...)", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return function->getArgumentsDataTypes();
}
AggregateFunctionPtr transformAggregateFunction(
const AggregateFunctionPtr & nested_function, const DataTypes & arguments, const Array &) const override
{
const DataTypePtr & argument = arguments[0];
const DataTypeAggregateFunction * function = typeid_cast<const DataTypeAggregateFunction *>(argument.get());
if (!function)
throw Exception("Illegal type " + argument->getName() + " of argument for aggregate function with " + getName() + " suffix"
+ " must be AggregateFunction(...)", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
if (nested_function->getName() != function->getFunctionName())
throw Exception("Illegal type " + argument->getName() + " of argument for aggregate function with " + getName() + " suffix"
+ ", because it corresponds to different aggregate function: " + function->getFunctionName() + " instead of " + nested_function->getName(),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return std::make_shared<AggregateFunctionMerge>(nested_function, *argument);
}
};
void registerAggregateFunctionCombinatorMerge(AggregateFunctionCombinatorFactory & factory)
{
factory.registerCombinator(std::make_shared<AggregateFunctionCombinatorMerge>());
}
}
......@@ -23,7 +23,7 @@ private:
AggregateFunctionPtr nested_func;
public:
AggregateFunctionMerge(AggregateFunctionPtr nested_, const IDataType & argument)
AggregateFunctionMerge(const AggregateFunctionPtr & nested_, const IDataType & argument)
: nested_func(nested_)
{
const DataTypeAggregateFunction * data_type = typeid_cast<const DataTypeAggregateFunction *>(&argument);
......
#include <AggregateFunctions/AggregateFunctionNothing.h>
namespace DB
{
AggregateFunctionPtr createAggregateFunctionNothing()
{
return std::make_shared<AggregateFunctionNothing>();
}
}
#pragma once
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeNothing.h>
#include <Columns/IColumn.h>
#include <AggregateFunctions/IAggregateFunction.h>
......@@ -21,7 +22,7 @@ public:
DataTypePtr getReturnType() const override
{
return std::make_shared<DataTypeNothing>();
return std::make_shared<DataTypeNullable>(std::make_shared<DataTypeNothing>());
}
void create(AggregateDataPtr) const override
......
#include <DataTypes/DataTypeNullable.h>
#include <AggregateFunctions/AggregateFunctionNull.h>
#include <AggregateFunctions/AggregateFunctionNothing.h>
#include <AggregateFunctions/AggregateFunctionCount.h>
#include <AggregateFunctions/AggregateFunctionCombinatorFactory.h>
namespace DB
{
AggregateFunctionPtr createAggregateFunctionNullUnary(AggregateFunctionPtr & nested)
namespace ErrorCodes
{
const DataTypePtr & nested_return_type = nested->getReturnType();
if (nested_return_type && !nested_return_type->canBeInsideNullable())
return std::make_shared<AggregateFunctionNullUnary<false>>(nested);
else
return std::make_shared<AggregateFunctionNullUnary<true>>(nested);
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
AggregateFunctionPtr createAggregateFunctionNullVariadic(AggregateFunctionPtr & nested, const DataTypes & types)
class AggregateFunctionCombinatorNull final : public IAggregateFunctionCombinator
{
public:
String getName() const override { return "Null"; };
DataTypes transformArguments(const DataTypes & arguments) const override
{
size_t size = arguments.size();
DataTypes res(size);
for (size_t i = 0; i < size; ++i)
res[i] = removeNullable(arguments[i]);
return res;
}
AggregateFunctionPtr transformAggregateFunction(
const AggregateFunctionPtr & nested_function, const DataTypes & arguments, const Array &) const override
{
bool has_nullable_types = false;
bool has_null_types = false;
for (const auto & arg_type : arguments)
{
if (arg_type->isNullable())
{
has_nullable_types = true;
if (arg_type->onlyNull())
{
has_null_types = true;
break;
}
}
}
if (!has_nullable_types)
throw Exception("Aggregate function combinator 'Null' requires at least one argument to be Nullable", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
/// Special case for 'count' function. It could be called with Nullable arguments
/// - that means - count number of calls, when all arguments are not NULL.
if (nested_function && nested_function->getName() == "count")
{
if (arguments.size() == 1)
return std::make_shared<AggregateFunctionCountNotNullUnary>(arguments[0]);
else
return std::make_shared<AggregateFunctionCountNotNullVariadic>(arguments);
}
if (has_null_types)
return std::make_shared<AggregateFunctionNothing>();
bool return_type_is_nullable = nested_function->getReturnType()->canBeInsideNullable();
if (arguments.size() == 1)
{
if (return_type_is_nullable)
return std::make_shared<AggregateFunctionNullUnary<true>>(nested_function);
else
return std::make_shared<AggregateFunctionNullUnary<false>>(nested_function);
}
else
{
if (return_type_is_nullable)
return std::make_shared<AggregateFunctionNullVariadic<true>>(nested_function, arguments);
else
return std::make_shared<AggregateFunctionNullVariadic<false>>(nested_function, arguments);
}
}
};
void registerAggregateFunctionCombinatorNull(AggregateFunctionCombinatorFactory & factory)
{
const DataTypePtr & nested_return_type = nested->getReturnType();
if (nested_return_type && !nested_return_type->canBeInsideNullable())
return std::make_shared<AggregateFunctionNullVariadic<false>>(nested, types);
else
return std::make_shared<AggregateFunctionNullVariadic<true>>(nested, types);
factory.registerCombinator(std::make_shared<AggregateFunctionCombinatorNull>());
}
}
#include <AggregateFunctions/AggregateFunctionState.h>
#include <AggregateFunctions/AggregateFunctionMerge.h>
#include <Common/typeid_cast.h>
#include <AggregateFunctions/AggregateFunctionCombinatorFactory.h>
#include <DataTypes/DataTypeAggregateFunction.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int BAD_ARGUMENTS;
}
class AggregateFunctionCombinatorState final : public IAggregateFunctionCombinator
{
public:
String getName() const override { return "State"; };
DataTypes transformArguments(const DataTypes & arguments) const override
{
return arguments;
}
AggregateFunctionPtr transformAggregateFunction(
const AggregateFunctionPtr & nested_function, const DataTypes & arguments, const Array & params) const override
{
return std::make_shared<AggregateFunctionState>(nested_function, arguments, params);
}
};
void registerAggregateFunctionCombinatorState(AggregateFunctionCombinatorFactory & factory)
{
factory.registerCombinator(std::make_shared<AggregateFunctionCombinatorState>());
}
DataTypePtr AggregateFunctionState::getReturnType() const
{
auto ptr = std::make_shared<DataTypeAggregateFunction>(nested_func, arguments, params);
/// Special case: it is -MergeState combinator. TODO Do we need this code?
/// Special case: it is -MergeState combinator.
/// We must return AggregateFunction(agg, ...) instead of AggregateFunction(aggMerge, ...)
if (typeid_cast<const AggregateFunctionMerge *>(ptr->getFunction().get()))
{
if (arguments.size() != 1)
......@@ -29,10 +56,4 @@ DataTypePtr AggregateFunctionState::getReturnType() const
return ptr;
}
AggregateFunctionPtr createAggregateFunctionState(AggregateFunctionPtr & nested, const DataTypes & arguments, const Array & params)
{
return std::make_shared<AggregateFunctionState>(nested, arguments, params);
}
}
......@@ -3,29 +3,16 @@ add_headers_and_sources(clickhouse_aggregate_functions .)
list(REMOVE_ITEM clickhouse_aggregate_functions_sources
AggregateFunctionFactory.cpp
AggregateFunctionState.cpp
AggregateFunctionArray.cpp
AggregateFunctionNull.cpp
AggregateFunctionNothing.cpp
AggregateFunctionForEach.cpp
AggregateFunctionIf.cpp
AggregateFunctionMerge.cpp
AggregateFunctionCount.cpp
AggregateFunctionCombinatorFactory.cpp
parseAggregateFunctionParameters.cpp
FactoryHelpers.cpp
)
list(REMOVE_ITEM clickhouse_aggregate_functions_headers
AggregateFunction.h
IAggregateFunction.h
IAggregateFunctionCombinator.h
AggregateFunctionFactory.h
AggregateFunctionState.h
AggregateFunctionArray.h
AggregateFunctionNull.h
AggregateFunctionNothing.h
AggregateFunctionForEach.h
AggregateFunctionIf.h
AggregateFunctionMerge.h
AggregateFunctionCount.h
AggregateFunctionCombinatorFactory.h
parseAggregateFunctionParameters.h
FactoryHelpers.h
)
......
#include <AggregateFunctions/registerAggregateFunctions.h>
#include <AggregateFunctions/AggregateFunctionFactory.h>
#include <AggregateFunctions/AggregateFunctionCombinatorFactory.h>
namespace DB
{
void registerAggregateFunctionAvg(AggregateFunctionFactory & factory);
void registerAggregateFunctionCount(AggregateFunctionFactory & factory);
void registerAggregateFunctionGroupArray(AggregateFunctionFactory & factory);
void registerAggregateFunctionGroupUniqArray(AggregateFunctionFactory & factory);
void registerAggregateFunctionGroupArrayInsertAt(AggregateFunctionFactory & factory);
void registerAggregateFunctionsQuantile(AggregateFunctionFactory & factory);
void registerAggregateFunctionsSequenceMatch(AggregateFunctionFactory & factory);
void registerAggregateFunctionsMinMaxAny(AggregateFunctionFactory & factory);
void registerAggregateFunctionsStatisticsStable(AggregateFunctionFactory & factory);
void registerAggregateFunctionsStatisticsSimple(AggregateFunctionFactory & factory);
void registerAggregateFunctionSum(AggregateFunctionFactory & factory);
void registerAggregateFunctionSumMap(AggregateFunctionFactory & factory);
void registerAggregateFunctionsUniq(AggregateFunctionFactory & factory);
void registerAggregateFunctionUniqUpTo(AggregateFunctionFactory & factory);
void registerAggregateFunctionTopK(AggregateFunctionFactory & factory);
void registerAggregateFunctionsBitwise(AggregateFunctionFactory & factory);
void registerAggregateFunctionAvg(AggregateFunctionFactory &);
void registerAggregateFunctionCount(AggregateFunctionFactory &);
void registerAggregateFunctionGroupArray(AggregateFunctionFactory &);
void registerAggregateFunctionGroupUniqArray(AggregateFunctionFactory &);
void registerAggregateFunctionGroupArrayInsertAt(AggregateFunctionFactory &);
void registerAggregateFunctionsQuantile(AggregateFunctionFactory &);
void registerAggregateFunctionsSequenceMatch(AggregateFunctionFactory &);
void registerAggregateFunctionsMinMaxAny(AggregateFunctionFactory &);
void registerAggregateFunctionsStatisticsStable(AggregateFunctionFactory &);
void registerAggregateFunctionsStatisticsSimple(AggregateFunctionFactory &);
void registerAggregateFunctionSum(AggregateFunctionFactory &);
void registerAggregateFunctionSumMap(AggregateFunctionFactory &);
void registerAggregateFunctionsUniq(AggregateFunctionFactory &);
void registerAggregateFunctionUniqUpTo(AggregateFunctionFactory &);
void registerAggregateFunctionTopK(AggregateFunctionFactory &);
void registerAggregateFunctionsBitwise(AggregateFunctionFactory &);
void registerAggregateFunctionCombinatorIf(AggregateFunctionCombinatorFactory &);
void registerAggregateFunctionCombinatorArray(AggregateFunctionCombinatorFactory &);
void registerAggregateFunctionCombinatorForEach(AggregateFunctionCombinatorFactory &);
void registerAggregateFunctionCombinatorState(AggregateFunctionCombinatorFactory &);
void registerAggregateFunctionCombinatorMerge(AggregateFunctionCombinatorFactory &);
void registerAggregateFunctionCombinatorNull(AggregateFunctionCombinatorFactory &);
void registerAggregateFunctions()
{
auto & factory = AggregateFunctionFactory::instance();
registerAggregateFunctionAvg(factory);
registerAggregateFunctionCount(factory);
registerAggregateFunctionGroupArray(factory);
registerAggregateFunctionGroupUniqArray(factory);
registerAggregateFunctionGroupArrayInsertAt(factory);
registerAggregateFunctionsQuantile(factory);
registerAggregateFunctionsSequenceMatch(factory);
registerAggregateFunctionsMinMaxAny(factory);
registerAggregateFunctionsStatisticsStable(factory);
registerAggregateFunctionsStatisticsSimple(factory);
registerAggregateFunctionSum(factory);
registerAggregateFunctionSumMap(factory);
registerAggregateFunctionsUniq(factory);
registerAggregateFunctionUniqUpTo(factory);
registerAggregateFunctionTopK(factory);
registerAggregateFunctionsBitwise(factory);
{
auto & factory = AggregateFunctionFactory::instance();
registerAggregateFunctionAvg(factory);
registerAggregateFunctionCount(factory);
registerAggregateFunctionGroupArray(factory);
registerAggregateFunctionGroupUniqArray(factory);
registerAggregateFunctionGroupArrayInsertAt(factory);
registerAggregateFunctionsQuantile(factory);
registerAggregateFunctionsSequenceMatch(factory);
registerAggregateFunctionsMinMaxAny(factory);
registerAggregateFunctionsStatisticsStable(factory);
registerAggregateFunctionsStatisticsSimple(factory);
registerAggregateFunctionSum(factory);
registerAggregateFunctionSumMap(factory);
registerAggregateFunctionsUniq(factory);
registerAggregateFunctionUniqUpTo(factory);
registerAggregateFunctionTopK(factory);
registerAggregateFunctionsBitwise(factory);
}
{
auto & factory = AggregateFunctionCombinatorFactory::instance();
registerAggregateFunctionCombinatorIf(factory);
registerAggregateFunctionCombinatorArray(factory);
registerAggregateFunctionCombinatorForEach(factory);
registerAggregateFunctionCombinatorState(factory);
registerAggregateFunctionCombinatorMerge(factory);
registerAggregateFunctionCombinatorNull(factory);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册