ClickHouse/src/TableFunctions/TableFunctionFactory.cpp

104 lines
3.0 KiB
C++
Raw Normal View History

#include <TableFunctions/TableFunctionFactory.h>
#include <Interpreters/Context.h>
2021-01-22 12:19:06 +00:00
#include <Common/CurrentThread.h>
#include <Common/Exception.h>
2019-04-03 11:13:22 +00:00
#include <IO/WriteHelpers.h>
#include <Parsers/ASTFunction.h>
2019-07-30 12:15:02 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int UNKNOWN_FUNCTION;
extern const int LOGICAL_ERROR;
}
2022-08-26 01:41:52 +00:00
void TableFunctionFactory::registerFunction(const std::string & name, TableFunctionCreator creator, Doc doc, CaseSensitiveness case_sensitiveness)
{
2022-08-26 01:41:52 +00:00
if (!table_functions.emplace(name, TableFunctionData{creator, doc}).second)
throw Exception("TableFunctionFactory: the table function name '" + name + "' is not unique",
ErrorCodes::LOGICAL_ERROR);
2019-07-29 13:50:13 +00:00
if (case_sensitiveness == CaseInsensitive
2022-08-26 01:41:52 +00:00
&& !case_insensitive_table_functions.emplace(Poco::toLower(name), TableFunctionData{creator, doc}).second)
2019-07-29 16:20:17 +00:00
throw Exception("TableFunctionFactory: the case insensitive table function name '" + name + "' is not unique",
2019-07-29 13:50:13 +00:00
ErrorCodes::LOGICAL_ERROR);
}
TableFunctionPtr TableFunctionFactory::get(
const ASTPtr & ast_function,
ContextPtr context) const
{
const auto * table_function = ast_function->as<ASTFunction>();
auto res = tryGet(table_function->name, context);
2019-07-29 13:50:13 +00:00
if (!res)
2019-04-03 11:13:22 +00:00
{
auto hints = getHints(table_function->name);
2019-04-03 11:13:22 +00:00
if (!hints.empty())
throw Exception(ErrorCodes::UNKNOWN_FUNCTION, "Unknown table function {}. Maybe you meant: {}", table_function->name , toString(hints));
2019-04-03 11:13:22 +00:00
else
throw Exception(ErrorCodes::UNKNOWN_FUNCTION, "Unknown table function {}", table_function->name);
2019-04-03 11:13:22 +00:00
}
res->parseArguments(ast_function, context);
2019-07-29 13:50:13 +00:00
return res;
}
TableFunctionPtr TableFunctionFactory::tryGet(
const std::string & name_param,
ContextPtr) const
2019-07-29 13:50:13 +00:00
{
String name = getAliasToOrName(name_param);
TableFunctionPtr res;
2019-07-29 13:50:13 +00:00
auto it = table_functions.find(name);
if (table_functions.end() != it)
2022-08-26 01:41:52 +00:00
{
res = it->second.first();
}
else
{
it = case_insensitive_table_functions.find(Poco::toLower(name));
if (case_insensitive_table_functions.end() != it)
2022-08-26 01:41:52 +00:00
res = it->second.first();
}
2019-07-29 13:50:13 +00:00
if (!res)
return nullptr;
2019-07-29 13:50:13 +00:00
if (CurrentThread::isInitialized())
{
auto query_context = CurrentThread::get().getQueryContext();
if (query_context && query_context->getSettingsRef().log_queries)
query_context->addQueryFactoriesInfo(Context::QueryLogFactories::TableFunction, name);
}
return res;
}
bool TableFunctionFactory::isTableFunctionName(const std::string & name) const
{
return table_functions.contains(name);
}
2022-08-26 01:41:52 +00:00
Doc TableFunctionFactory::getDocumentation(const std::string & name) const
{
auto it = table_functions.find(name);
if (it == table_functions.end())
throw Exception(ErrorCodes::UNKNOWN_FUNCTION, "Unknown table function {}", name);
return it->second.second;
}
TableFunctionFactory & TableFunctionFactory::instance()
{
static TableFunctionFactory ret;
return ret;
}
}