2012-12-18 20:06:36 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
2022-07-04 07:01:39 +00:00
|
|
|
#include <Common/register_objects.h>
|
2018-07-25 16:08:23 +00:00
|
|
|
#include <Common/IFactoryWithAliases.h>
|
2023-04-28 10:10:42 +00:00
|
|
|
#include <Common/FunctionDocumentation.h>
|
2021-05-15 17:33:15 +00:00
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
#include <Functions/IFunctionAdaptors.h>
|
2017-08-14 04:23:38 +00:00
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
2012-12-18 20:06:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-08-27 20:06:03 +00:00
|
|
|
using FunctionCreator = std::function<FunctionOverloadResolverPtr(ContextPtr)>;
|
2023-04-28 10:10:42 +00:00
|
|
|
using FunctionFactoryData = std::pair<FunctionCreator, FunctionDocumentation>;
|
2022-08-27 20:06:03 +00:00
|
|
|
|
2016-07-09 23:37:29 +00:00
|
|
|
/** Creates function by name.
|
2023-08-14 23:24:41 +00:00
|
|
|
* The provided Context is guaranteed to outlive the created function. Functions may use it for
|
|
|
|
* things like settings, current database, permission checks, etc.
|
2012-12-18 20:06:36 +00:00
|
|
|
*/
|
2022-08-27 20:06:03 +00:00
|
|
|
class FunctionFactory : private boost::noncopyable, public IFactoryWithAliases<FunctionFactoryData>
|
2012-12-18 20:06:36 +00:00
|
|
|
{
|
2017-08-18 17:06:22 +00:00
|
|
|
public:
|
2019-08-22 03:24:05 +00:00
|
|
|
static FunctionFactory & instance();
|
|
|
|
|
2018-02-02 08:33:36 +00:00
|
|
|
template <typename Function>
|
2023-04-28 10:10:42 +00:00
|
|
|
void registerFunction(FunctionDocumentation doc = {}, CaseSensitiveness case_sensitiveness = CaseSensitive)
|
2018-02-02 08:33:36 +00:00
|
|
|
{
|
2022-08-27 20:06:03 +00:00
|
|
|
registerFunction<Function>(Function::name, std::move(doc), case_sensitiveness);
|
2018-02-02 08:33:36 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 04:23:38 +00:00
|
|
|
template <typename Function>
|
2023-04-28 10:10:42 +00:00
|
|
|
void registerFunction(const std::string & name, FunctionDocumentation doc = {}, CaseSensitiveness case_sensitiveness = CaseSensitive)
|
2017-08-14 04:23:38 +00:00
|
|
|
{
|
2021-05-15 17:33:15 +00:00
|
|
|
if constexpr (std::is_base_of_v<IFunction, Function>)
|
2022-08-27 20:06:03 +00:00
|
|
|
registerFunction(name, &adaptFunctionToOverloadResolver<Function>, std::move(doc), case_sensitiveness);
|
2018-02-02 08:33:36 +00:00
|
|
|
else
|
2022-08-27 20:06:03 +00:00
|
|
|
registerFunction(name, &Function::create, std::move(doc), case_sensitiveness);
|
2014-08-22 00:57:20 +00:00
|
|
|
}
|
2017-08-18 17:06:22 +00:00
|
|
|
|
2022-04-15 22:20:47 +00:00
|
|
|
/// This function is used by YQL - innovative transactional DBMS that depends on ClickHouse by source code.
|
2020-05-11 21:18:50 +00:00
|
|
|
std::vector<std::string> getAllNames() const;
|
|
|
|
|
2021-10-02 21:46:51 +00:00
|
|
|
bool has(const std::string & name) const;
|
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
/// Throws an exception if not found.
|
2021-06-01 12:20:52 +00:00
|
|
|
FunctionOverloadResolverPtr get(const std::string & name, ContextPtr context) const;
|
2017-08-18 17:06:22 +00:00
|
|
|
|
|
|
|
/// Returns nullptr if not found.
|
2021-06-01 12:20:52 +00:00
|
|
|
FunctionOverloadResolverPtr tryGet(const std::string & name, ContextPtr context) const;
|
2017-08-18 17:06:22 +00:00
|
|
|
|
2019-12-12 14:16:59 +00:00
|
|
|
/// The same methods to get developer interface implementation.
|
2021-06-01 12:20:52 +00:00
|
|
|
FunctionOverloadResolverPtr getImpl(const std::string & name, ContextPtr context) const;
|
|
|
|
FunctionOverloadResolverPtr tryGetImpl(const std::string & name, ContextPtr context) const;
|
2019-12-09 14:41:55 +00:00
|
|
|
|
2021-05-04 17:26:09 +00:00
|
|
|
/// Register a function by its name.
|
|
|
|
/// No locking, you must register all functions before usage of get.
|
|
|
|
void registerFunction(
|
|
|
|
const std::string & name,
|
2022-08-27 20:06:03 +00:00
|
|
|
FunctionCreator creator,
|
2023-04-28 10:10:42 +00:00
|
|
|
FunctionDocumentation doc = {},
|
2021-05-04 17:26:09 +00:00
|
|
|
CaseSensitiveness case_sensitiveness = CaseSensitive);
|
|
|
|
|
2023-04-28 10:10:42 +00:00
|
|
|
FunctionDocumentation getDocumentation(const std::string & name) const;
|
2022-08-27 20:06:03 +00:00
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
private:
|
2020-06-14 07:44:02 +00:00
|
|
|
using Functions = std::unordered_map<std::string, Value>;
|
2017-08-18 17:06:22 +00:00
|
|
|
|
|
|
|
Functions functions;
|
|
|
|
Functions case_insensitive_functions;
|
2018-02-06 19:34:53 +00:00
|
|
|
|
|
|
|
template <typename Function>
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionOverloadResolverPtr adaptFunctionToOverloadResolver(ContextPtr context)
|
2018-02-06 19:34:53 +00:00
|
|
|
{
|
2021-05-15 17:33:15 +00:00
|
|
|
return std::make_unique<FunctionToOverloadResolverAdaptor>(Function::create(context));
|
2018-02-06 19:34:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 07:44:02 +00:00
|
|
|
const Functions & getMap() const override { return functions; }
|
2018-07-25 16:08:23 +00:00
|
|
|
|
2020-06-14 07:44:02 +00:00
|
|
|
const Functions & getCaseInsensitiveMap() const override { return case_insensitive_functions; }
|
2018-07-25 16:08:23 +00:00
|
|
|
|
|
|
|
String getFactoryName() const override { return "FunctionFactory"; }
|
2012-12-18 20:06:36 +00:00
|
|
|
};
|
|
|
|
|
2022-10-04 15:29:33 +00:00
|
|
|
const String & getFunctionCanonicalNameIfAny(const String & name);
|
|
|
|
|
2012-12-18 20:06:36 +00:00
|
|
|
}
|