2011-09-19 03:40:05 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
2018-07-25 16:08:23 +00:00
|
|
|
#include <Common/IFactoryWithAliases.h>
|
2017-08-18 17:06:22 +00:00
|
|
|
|
2016-12-29 23:17:05 +00:00
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2020-07-05 23:50:20 +00:00
|
|
|
#include <optional>
|
2017-08-18 17:06:22 +00:00
|
|
|
|
2011-09-19 03:40:05 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
class Context;
|
2017-01-21 04:24:28 +00:00
|
|
|
class IDataType;
|
2017-08-18 17:06:22 +00:00
|
|
|
|
2017-12-18 01:11:48 +00:00
|
|
|
using DataTypePtr = std::shared_ptr<const IDataType>;
|
2017-01-21 04:24:28 +00:00
|
|
|
using DataTypes = std::vector<DataTypePtr>;
|
|
|
|
|
2020-10-25 20:33:01 +00:00
|
|
|
/**
|
|
|
|
* The invoker has arguments: name of aggregate function, types of arguments, values of parameters.
|
2018-07-25 16:08:23 +00:00
|
|
|
* Parameters are for "parametric" aggregate functions.
|
|
|
|
* For example, in quantileWeighted(0.9)(x, weight), 0.9 is "parameter" and x, weight are "arguments".
|
|
|
|
*/
|
|
|
|
using AggregateFunctionCreator = std::function<AggregateFunctionPtr(const String &, const DataTypes &, const Array &)>;
|
|
|
|
|
2020-06-14 07:44:02 +00:00
|
|
|
struct AggregateFunctionWithProperties
|
|
|
|
{
|
|
|
|
AggregateFunctionCreator creator;
|
|
|
|
AggregateFunctionProperties properties;
|
|
|
|
|
|
|
|
AggregateFunctionWithProperties() = default;
|
|
|
|
AggregateFunctionWithProperties(const AggregateFunctionWithProperties &) = default;
|
2020-06-17 13:18:52 +00:00
|
|
|
AggregateFunctionWithProperties & operator = (const AggregateFunctionWithProperties &) = default;
|
2020-06-14 07:44:02 +00:00
|
|
|
|
|
|
|
template <typename Creator, std::enable_if_t<!std::is_same_v<Creator, AggregateFunctionWithProperties>> * = nullptr>
|
|
|
|
AggregateFunctionWithProperties(Creator creator_, AggregateFunctionProperties properties_ = {})
|
|
|
|
: creator(std::forward<Creator>(creator_)), properties(std::move(properties_))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
|
2016-08-05 15:44:19 +00:00
|
|
|
/** Creates an aggregate function by name.
|
2011-09-19 03:40:05 +00:00
|
|
|
*/
|
2020-06-14 07:44:02 +00:00
|
|
|
class AggregateFunctionFactory final : private boost::noncopyable, public IFactoryWithAliases<AggregateFunctionWithProperties>
|
2011-09-19 03:40:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-08-22 03:24:05 +00:00
|
|
|
static AggregateFunctionFactory & instance();
|
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
/// Register a function by its name.
|
|
|
|
/// No locking, you must register all functions before usage of get.
|
|
|
|
void registerFunction(
|
|
|
|
const String & name,
|
2020-06-14 07:44:02 +00:00
|
|
|
Value creator,
|
2017-08-18 17:06:22 +00:00
|
|
|
CaseSensitiveness case_sensitiveness = CaseSensitive);
|
|
|
|
|
|
|
|
/// Throws an exception if not found.
|
2020-11-08 15:36:20 +00:00
|
|
|
AggregateFunctionPtr
|
|
|
|
get(const String & name,
|
2017-07-10 23:30:17 +00:00
|
|
|
const DataTypes & argument_types,
|
2020-06-14 07:44:02 +00:00
|
|
|
const Array & parameters,
|
2020-11-08 15:36:20 +00:00
|
|
|
AggregateFunctionProperties & out_properties) const;
|
2017-07-10 23:30:17 +00:00
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
/// Returns nullptr if not found.
|
|
|
|
AggregateFunctionPtr tryGet(
|
|
|
|
const String & name,
|
|
|
|
const DataTypes & argument_types,
|
2020-06-14 07:44:02 +00:00
|
|
|
const Array & parameters,
|
|
|
|
AggregateFunctionProperties & out_properties) const;
|
2017-07-10 23:30:17 +00:00
|
|
|
|
2020-07-05 23:50:20 +00:00
|
|
|
/// Get properties if the aggregate function exists.
|
|
|
|
std::optional<AggregateFunctionProperties> tryGetProperties(const String & name) const;
|
|
|
|
|
2020-11-08 15:36:20 +00:00
|
|
|
bool isAggregateFunctionName(const String & name) const;
|
2011-09-19 03:40:05 +00:00
|
|
|
|
2015-09-24 12:40:36 +00:00
|
|
|
private:
|
2017-07-10 23:30:17 +00:00
|
|
|
AggregateFunctionPtr getImpl(
|
|
|
|
const String & name,
|
|
|
|
const DataTypes & argument_types,
|
|
|
|
const Array & parameters,
|
2020-06-14 07:44:02 +00:00
|
|
|
AggregateFunctionProperties & out_properties,
|
2020-11-08 15:36:20 +00:00
|
|
|
bool has_null_arguments) const;
|
2016-07-12 13:02:52 +00:00
|
|
|
|
2020-11-08 15:36:20 +00:00
|
|
|
std::optional<AggregateFunctionProperties> tryGetPropertiesImpl(const String & name) const;
|
2020-07-05 23:50:20 +00:00
|
|
|
|
2020-06-14 07:44:02 +00:00
|
|
|
using AggregateFunctions = std::unordered_map<String, Value>;
|
2017-08-18 17:06:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctions aggregate_functions;
|
2016-07-14 05:22:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Case insensitive aggregate functions will be additionally added here with lowercased name.
|
|
|
|
AggregateFunctions case_insensitive_aggregate_functions;
|
2018-07-25 16:08:23 +00:00
|
|
|
|
2020-06-14 07:44:02 +00:00
|
|
|
const AggregateFunctions & getMap() const override { return aggregate_functions; }
|
2018-07-25 16:08:23 +00:00
|
|
|
|
2020-06-14 07:44:02 +00:00
|
|
|
const AggregateFunctions & getCaseInsensitiveMap() const override { return case_insensitive_aggregate_functions; }
|
2018-07-25 16:08:23 +00:00
|
|
|
|
|
|
|
String getFactoryName() const override { return "AggregateFunctionFactory"; }
|
|
|
|
|
2015-09-24 12:40:36 +00:00
|
|
|
};
|
2011-09-19 03:40:05 +00:00
|
|
|
|
|
|
|
}
|