2017-08-18 17:06:22 +00:00
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2021-01-22 12:19:06 +00:00
|
|
|
#include <Common/CurrentThread.h>
|
2017-08-18 17:06:22 +00:00
|
|
|
#include <Common/Exception.h>
|
2022-11-05 14:53:08 +00:00
|
|
|
#include <Common/KnownObjectNames.h>
|
2019-04-03 11:13:22 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2020-10-14 12:19:29 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2014-01-28 16:45:10 +00:00
|
|
|
|
2019-07-30 12:15:02 +00:00
|
|
|
|
2014-01-28 16:45:10 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int UNKNOWN_FUNCTION;
|
2017-08-18 17:06:22 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 20:06:03 +00:00
|
|
|
void TableFunctionFactory::registerFunction(
|
2022-11-01 09:13:10 +00:00
|
|
|
const std::string & name, Value value, CaseSensitiveness case_sensitiveness)
|
2017-08-18 17:06:22 +00:00
|
|
|
{
|
2022-11-01 09:13:10 +00:00
|
|
|
if (!table_functions.emplace(name, value).second)
|
2017-08-18 17:06:22 +00:00
|
|
|
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-11-01 09:13:10 +00:00
|
|
|
&& !case_insensitive_table_functions.emplace(Poco::toLower(name), value).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);
|
2022-11-05 14:53:08 +00:00
|
|
|
|
|
|
|
KnownTableFunctionNames::instance().add(name, (case_sensitiveness == CaseInsensitive));
|
2017-08-18 17:06:22 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 16:45:10 +00:00
|
|
|
TableFunctionPtr TableFunctionFactory::get(
|
2020-10-14 12:19:29 +00:00
|
|
|
const ASTPtr & ast_function,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context) const
|
2014-01-28 16:45:10 +00:00
|
|
|
{
|
2020-10-14 12:19:29 +00:00
|
|
|
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
|
|
|
{
|
2020-10-14 12:19:29 +00:00
|
|
|
auto hints = getHints(table_function->name);
|
2019-04-03 11:13:22 +00:00
|
|
|
if (!hints.empty())
|
2020-10-14 12:19:29 +00:00
|
|
|
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
|
2020-10-14 12:19:29 +00:00
|
|
|
throw Exception(ErrorCodes::UNKNOWN_FUNCTION, "Unknown table function {}", table_function->name);
|
2019-04-03 11:13:22 +00:00
|
|
|
}
|
2017-08-18 17:06:22 +00:00
|
|
|
|
2020-10-14 12:19:29 +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,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr) const
|
2019-07-29 13:50:13 +00:00
|
|
|
{
|
|
|
|
String name = getAliasToOrName(name_param);
|
2021-01-23 10:18:40 +00:00
|
|
|
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
|
|
|
{
|
2022-10-26 16:45:23 +00:00
|
|
|
res = it->second.creator();
|
2022-08-26 01:41:52 +00:00
|
|
|
}
|
2021-01-23 10:18:40 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
it = case_insensitive_table_functions.find(Poco::toLower(name));
|
|
|
|
if (case_insensitive_table_functions.end() != it)
|
2022-10-26 16:45:23 +00:00
|
|
|
res = it->second.creator();
|
2021-01-23 10:18:40 +00:00
|
|
|
}
|
2019-07-29 13:50:13 +00:00
|
|
|
|
2021-01-23 10:18:40 +00:00
|
|
|
if (!res)
|
|
|
|
return nullptr;
|
2019-07-29 13:50:13 +00:00
|
|
|
|
2021-01-23 10:18:40 +00:00
|
|
|
if (CurrentThread::isInitialized())
|
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
auto query_context = CurrentThread::get().getQueryContext();
|
2021-01-23 10:18:40 +00:00
|
|
|
if (query_context && query_context->getSettingsRef().log_queries)
|
|
|
|
query_context->addQueryFactoriesInfo(Context::QueryLogFactories::TableFunction, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2014-01-28 16:45:10 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 13:10:34 +00:00
|
|
|
bool TableFunctionFactory::isTableFunctionName(const std::string & name) const
|
|
|
|
{
|
2022-04-18 10:18:43 +00:00
|
|
|
return table_functions.contains(name);
|
2018-07-24 13:10:34 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:45:23 +00:00
|
|
|
std::optional<TableFunctionProperties> TableFunctionFactory::tryGetProperties(const String & name) const
|
2022-08-26 01:41:52 +00:00
|
|
|
{
|
2022-10-26 16:45:23 +00:00
|
|
|
return tryGetPropertiesImpl(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<TableFunctionProperties> TableFunctionFactory::tryGetPropertiesImpl(const String & name_param) const
|
|
|
|
{
|
|
|
|
String name = getAliasToOrName(name_param);
|
|
|
|
Value found;
|
|
|
|
|
|
|
|
/// Find by exact match.
|
|
|
|
if (auto it = table_functions.find(name); it != table_functions.end())
|
|
|
|
{
|
|
|
|
found = it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto jt = case_insensitive_table_functions.find(Poco::toLower(name)); jt != case_insensitive_table_functions.end())
|
|
|
|
found = jt->second;
|
|
|
|
|
|
|
|
if (found.creator)
|
|
|
|
return found.properties;
|
2022-08-26 01:41:52 +00:00
|
|
|
|
2022-10-26 16:45:23 +00:00
|
|
|
return {};
|
2022-08-26 01:41:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 03:24:05 +00:00
|
|
|
TableFunctionFactory & TableFunctionFactory::instance()
|
|
|
|
{
|
|
|
|
static TableFunctionFactory ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-01-28 16:45:10 +00:00
|
|
|
}
|