2014-01-28 16:45:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <TableFunctions/ITableFunction.h>
|
2019-07-29 13:50:13 +00:00
|
|
|
#include <Common/IFactoryWithAliases.h>
|
2019-04-03 11:13:22 +00:00
|
|
|
#include <Common/NamePrompter.h>
|
2017-08-18 17:06:22 +00:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
2019-08-22 03:24:05 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
2017-08-18 17:06:22 +00:00
|
|
|
|
2014-01-28 16:45:10 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
class Context;
|
2017-06-10 09:04:31 +00:00
|
|
|
|
2019-07-29 13:50:13 +00:00
|
|
|
using TableFunctionCreator = std::function<TableFunctionPtr()>;
|
2022-10-26 16:45:23 +00:00
|
|
|
|
|
|
|
struct TableFunctionFactoryData
|
|
|
|
{
|
|
|
|
TableFunctionCreator creator;
|
|
|
|
TableFunctionProperties properties;
|
|
|
|
|
|
|
|
TableFunctionFactoryData() = default;
|
|
|
|
TableFunctionFactoryData(const TableFunctionFactoryData &) = default;
|
|
|
|
TableFunctionFactoryData & operator = (const TableFunctionFactoryData &) = default;
|
|
|
|
|
|
|
|
template <typename Creator>
|
|
|
|
requires (!std::is_same_v<Creator, TableFunctionFactoryData>)
|
|
|
|
TableFunctionFactoryData(Creator creator_, TableFunctionProperties properties_ = {}) /// NOLINT
|
|
|
|
: creator(std::forward<Creator>(creator_)), properties(std::move(properties_))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-10 09:04:31 +00:00
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/** Lets you get a table function by its name.
|
2014-01-28 16:45:10 +00:00
|
|
|
*/
|
2022-08-27 20:06:03 +00:00
|
|
|
class TableFunctionFactory final: private boost::noncopyable, public IFactoryWithAliases<TableFunctionFactoryData>
|
2014-01-28 16:45:10 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-08-22 03:24:05 +00:00
|
|
|
static TableFunctionFactory & instance();
|
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
/// Register a function by its name.
|
2017-06-13 02:06:53 +00:00
|
|
|
/// No locking, you must register all functions before usage of get.
|
2022-08-27 20:06:03 +00:00
|
|
|
void registerFunction(
|
|
|
|
const std::string & name,
|
2022-11-01 09:13:10 +00:00
|
|
|
Value value,
|
2022-08-27 20:06:03 +00:00
|
|
|
CaseSensitiveness case_sensitiveness = CaseSensitive);
|
2017-08-18 17:06:22 +00:00
|
|
|
|
2017-06-10 09:04:31 +00:00
|
|
|
template <typename Function>
|
2022-08-27 20:06:03 +00:00
|
|
|
void registerFunction(Documentation doc = {}, CaseSensitiveness case_sensitiveness = CaseSensitive)
|
2017-06-10 09:04:31 +00:00
|
|
|
{
|
2022-08-26 01:41:52 +00:00
|
|
|
auto creator = []() -> TableFunctionPtr { return std::make_shared<Function>(); };
|
2022-11-01 09:13:10 +00:00
|
|
|
registerFunction(Function::name,
|
|
|
|
TableFunctionFactoryData{std::move(creator), {std::move(doc), table_functions_allowed_in_readonly_mode.contains(Function::name)}} ,
|
|
|
|
case_sensitiveness);
|
2017-06-10 09:04:31 +00:00
|
|
|
}
|
2017-08-18 17:06:22 +00:00
|
|
|
|
|
|
|
/// Throws an exception if not found.
|
2021-04-10 23:33:54 +00:00
|
|
|
TableFunctionPtr get(const ASTPtr & ast_function, ContextPtr context) const;
|
2019-07-29 13:50:13 +00:00
|
|
|
|
|
|
|
/// Returns nullptr if not found.
|
2021-04-10 23:33:54 +00:00
|
|
|
TableFunctionPtr tryGet(const std::string & name, ContextPtr context) const;
|
2018-07-27 21:33:30 +00:00
|
|
|
|
2022-10-26 16:45:23 +00:00
|
|
|
std::optional<TableFunctionProperties> tryGetProperties(const String & name) const;
|
2022-08-26 01:41:52 +00:00
|
|
|
|
2018-07-24 13:10:34 +00:00
|
|
|
bool isTableFunctionName(const std::string & name) const;
|
|
|
|
|
2019-07-29 13:50:13 +00:00
|
|
|
private:
|
2020-06-14 07:44:02 +00:00
|
|
|
using TableFunctions = std::unordered_map<std::string, Value>;
|
2017-08-18 17:06:22 +00:00
|
|
|
|
2020-06-14 07:44:02 +00:00
|
|
|
const TableFunctions & getMap() const override { return table_functions; }
|
2019-04-03 11:13:22 +00:00
|
|
|
|
2020-06-14 07:44:02 +00:00
|
|
|
const TableFunctions & getCaseInsensitiveMap() const override { return case_insensitive_table_functions; }
|
2019-07-29 13:50:13 +00:00
|
|
|
|
|
|
|
String getFactoryName() const override { return "TableFunctionFactory"; }
|
|
|
|
|
2022-10-26 16:45:23 +00:00
|
|
|
std::optional<TableFunctionProperties> tryGetPropertiesImpl(const String & name) const;
|
|
|
|
|
2019-07-29 13:50:13 +00:00
|
|
|
TableFunctions table_functions;
|
|
|
|
TableFunctions case_insensitive_table_functions;
|
2022-11-01 09:13:10 +00:00
|
|
|
|
|
|
|
inline static const NameSet table_functions_allowed_in_readonly_mode = {
|
|
|
|
"null", "view", "viewIfPermitted", "numbers", "numbers_mt", "generateRandom", "values", "cluster", "clusterAllReplicas"
|
|
|
|
};
|
2014-01-28 16:45:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|