ClickHouse/src/Functions/FunctionFactory.h

83 lines
2.7 KiB
C++
Raw Normal View History

#pragma once
#include <Functions/IFunctionAdaptors.h>
#include <Common/IFactoryWithAliases.h>
#include <Interpreters/Context.h>
2017-08-14 04:23:38 +00:00
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
namespace DB
{
class Context;
2016-07-09 23:37:29 +00:00
/** Creates function by name.
* Function could use for initialization (take ownership of shared_ptr, for example)
* some dictionaries from Context.
*/
2019-12-09 14:41:55 +00:00
class FunctionFactory : private boost::noncopyable, public IFactoryWithAliases<std::function<FunctionOverloadResolverImplPtr(const Context &)>>
{
public:
static FunctionFactory & instance();
2018-02-02 08:33:36 +00:00
template <typename Function>
void registerFunction(CaseSensitiveness case_sensitiveness = CaseSensitive)
2018-02-02 08:33:36 +00:00
{
registerFunction<Function>(Function::name, case_sensitiveness);
2018-02-02 08:33:36 +00:00
}
2017-08-14 04:23:38 +00:00
template <typename Function>
void registerFunction(const std::string & name, CaseSensitiveness case_sensitiveness = CaseSensitive)
2017-08-14 04:23:38 +00:00
{
2018-02-02 08:33:36 +00:00
if constexpr (std::is_base_of<IFunction, Function>::value)
registerFunction(name, &createDefaultFunction<Function>, case_sensitiveness);
2018-02-02 08:33:36 +00:00
else
registerFunction(name, &Function::create, case_sensitiveness);
}
2020-05-11 21:22:34 +00:00
/// This function is used by YQL - internal Yandex product that depends on ClickHouse by source code.
2020-05-11 21:18:50 +00:00
std::vector<std::string> getAllNames() const;
/// Throws an exception if not found.
2019-12-08 21:06:37 +00:00
FunctionOverloadResolverPtr get(const std::string & name, const Context & context) const;
/// Returns nullptr if not found.
2019-12-08 21:06:37 +00:00
FunctionOverloadResolverPtr tryGet(const std::string & name, const Context & context) const;
/// The same methods to get developer interface implementation.
2019-12-09 14:41:55 +00:00
FunctionOverloadResolverImplPtr getImpl(const std::string & name, const Context & context) const;
FunctionOverloadResolverImplPtr tryGetImpl(const std::string & name, const Context & context) const;
private:
2020-06-14 07:44:02 +00:00
using Functions = std::unordered_map<std::string, Value>;
Functions functions;
Functions case_insensitive_functions;
template <typename Function>
2019-12-09 14:41:55 +00:00
static FunctionOverloadResolverImplPtr createDefaultFunction(const Context & context)
{
return std::make_unique<DefaultOverloadResolver>(Function::create(context));
}
2020-06-14 07:44:02 +00:00
const Functions & getMap() const override { return functions; }
2020-06-14 07:44:02 +00:00
const Functions & getCaseInsensitiveMap() const override { return case_insensitive_functions; }
String getFactoryName() const override { return "FunctionFactory"; }
/// Register a function by its name.
/// No locking, you must register all functions before usage of get.
void registerFunction(
const std::string & name,
2020-06-14 07:44:02 +00:00
Value creator,
CaseSensitiveness case_sensitiveness = CaseSensitive);
};
}