2011-09-19 03:40:05 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
2017-08-18 17:06:22 +00:00
|
|
|
|
2017-07-10 04:34:14 +00:00
|
|
|
#include <ext/singleton.h>
|
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>
|
|
|
|
|
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-01-21 04:24:28 +00:00
|
|
|
using DataTypePtr = std::shared_ptr<IDataType>;
|
|
|
|
using DataTypes = std::vector<DataTypePtr>;
|
|
|
|
|
|
|
|
|
2016-08-05 15:44:19 +00:00
|
|
|
/** Creates an aggregate function by name.
|
2011-09-19 03:40:05 +00:00
|
|
|
*/
|
2017-07-10 04:34:14 +00:00
|
|
|
class AggregateFunctionFactory final : public ext::singleton<AggregateFunctionFactory>
|
2011-09-19 03:40:05 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
friend class StorageSystemFunctions;
|
2016-07-14 05:22:09 +00:00
|
|
|
|
2011-09-19 03:40:05 +00:00
|
|
|
public:
|
2017-08-18 17:06:22 +00:00
|
|
|
using Creator = std::function<AggregateFunctionPtr(const String &, const DataTypes &, const Array &)>;
|
|
|
|
|
|
|
|
/// For compatibility with SQL, it's possible to specify that certain aggregate function name is case insensitive.
|
|
|
|
enum CaseSensitiveness
|
|
|
|
{
|
|
|
|
CaseSensitive,
|
|
|
|
CaseInsensitive
|
|
|
|
};
|
2017-07-10 23:30:17 +00:00
|
|
|
|
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,
|
|
|
|
Creator creator,
|
|
|
|
CaseSensitiveness case_sensitiveness = CaseSensitive);
|
|
|
|
|
|
|
|
/// Throws an exception if not found.
|
2017-07-10 23:30:17 +00:00
|
|
|
AggregateFunctionPtr get(
|
|
|
|
const String & name,
|
|
|
|
const DataTypes & argument_types,
|
|
|
|
const Array & parameters = {},
|
|
|
|
int recursion_level = 0) const;
|
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
/// Returns nullptr if not found.
|
|
|
|
AggregateFunctionPtr tryGet(
|
|
|
|
const String & name,
|
|
|
|
const DataTypes & argument_types,
|
|
|
|
const Array & parameters = {}) const;
|
2017-07-10 23:30:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool isAggregateFunctionName(const String & name, int recursion_level = 0) 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,
|
|
|
|
int recursion_level) const;
|
2016-07-12 13:02:52 +00:00
|
|
|
|
2015-09-24 12:40:36 +00:00
|
|
|
private:
|
2017-08-18 17:06:22 +00:00
|
|
|
using AggregateFunctions = std::unordered_map<String, Creator>;
|
|
|
|
|
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;
|
2015-09-24 12:40:36 +00:00
|
|
|
};
|
2011-09-19 03:40:05 +00:00
|
|
|
|
|
|
|
}
|