forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionFactory.cpp
More file actions
182 lines (150 loc) · 5.42 KB
/
FunctionFactory.cpp
File metadata and controls
182 lines (150 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <Functions/FunctionFactory.h>
#include <Functions/IFunctionAdaptors.h>
#include <Functions/DateTimeTransforms.h>
#include <Interpreters/Context.h>
#include <Common/Exception.h>
#include <Common/CurrentThread.h>
#include <Core/Settings.h>
#include <Poco/String.h>
#include <IO/WriteHelpers.h>
#include <AggregateFunctions/AggregateFunctionFactory.h>
namespace DB
{
namespace Setting
{
extern const SettingsBool log_queries;
extern const SettingsBool use_legacy_to_time;
}
namespace ErrorCodes
{
extern const int UNKNOWN_FUNCTION;
extern const int LOGICAL_ERROR;
}
const String & getFunctionCanonicalNameIfAny(const String & name)
{
return FunctionFactory::instance().getCanonicalNameIfAny(name);
}
void FunctionFactory::registerFunction(
const std::string & name,
FunctionCreator creator,
FunctionDocumentation doc,
Case case_sensitiveness)
{
if (!functions.emplace(name, FunctionFactoryData{creator, doc}).second)
throw Exception(ErrorCodes::LOGICAL_ERROR, "FunctionFactory: the function name '{}' is not unique", name);
String function_name_lowercase = Poco::toLower(name);
if (isAlias(name) || isAlias(function_name_lowercase))
throw Exception(ErrorCodes::LOGICAL_ERROR, "FunctionFactory: the function name '{}' is already registered as alias",
name);
if (case_sensitiveness == Case::Insensitive)
{
if (!case_insensitive_functions.emplace(function_name_lowercase, FunctionFactoryData{creator, doc}).second)
throw Exception(ErrorCodes::LOGICAL_ERROR, "FunctionFactory: the case insensitive function name '{}' is not unique",
name);
case_insensitive_name_mapping[function_name_lowercase] = name;
}
}
void FunctionFactory::registerFunction(
const std::string & name,
FunctionSimpleCreator creator,
FunctionDocumentation doc,
Case case_sensitiveness)
{
registerFunction(name, [my_creator = std::move(creator)](ContextPtr context)
{
return std::make_unique<FunctionToOverloadResolverAdaptor>(my_creator(context));
}, std::move(doc), std::move(case_sensitiveness));
}
FunctionOverloadResolverPtr FunctionFactory::getImpl(
const std::string & name,
ContextPtr context) const
{
auto res = tryGetImpl(name, context);
if (!res)
{
String extra_info;
if (AggregateFunctionFactory::instance().hasNameOrAlias(name))
extra_info = ". There is an aggregate function with the same name, but ordinary function is expected here";
auto hints = this->getHints(name);
if (!hints.empty())
throw Exception(ErrorCodes::UNKNOWN_FUNCTION, "Unknown function {}{}. Maybe you meant: {}", name, extra_info, toString(hints));
throw Exception(ErrorCodes::UNKNOWN_FUNCTION, "Unknown function {}{}", name, extra_info);
}
return res;
}
std::vector<std::string> FunctionFactory::getAllNames() const
{
std::vector<std::string> res;
res.reserve(functions.size());
for (const auto & func : functions)
res.emplace_back(func.first);
return res;
}
FunctionOverloadResolverPtr FunctionFactory::get(
const std::string & name,
ContextPtr context) const
{
return getImpl(name, context);
}
bool FunctionFactory::has(const std::string & name) const
{
String canonical_name = getAliasToOrName(name);
if (functions.contains(canonical_name))
return true;
canonical_name = Poco::toLower(canonical_name);
return case_insensitive_functions.contains(canonical_name);
}
FunctionOverloadResolverPtr FunctionFactory::tryGetImpl(
const std::string & name_param,
ContextPtr context) const
{
String name = getAliasToOrName(name_param);
FunctionOverloadResolverPtr res;
auto it = functions.find(name);
if (functions.end() != it)
res = it->second.first(context);
else
{
name = Poco::toLower(name);
it = case_insensitive_functions.find(name);
if (case_insensitive_functions.end() != it)
res = it->second.first(context);
}
if (!res)
return nullptr;
if (CurrentThread::isInitialized())
{
auto query_context = CurrentThread::get().getQueryContext();
if (query_context && query_context->getSettingsRef()[Setting::log_queries])
query_context->addQueryFactoriesInfo(Context::QueryLogFactories::Function, name);
/// There is a legacy toTime function that has the same name as toTime function for Time data type, so we need to
/// check this setting here and decide if we need to change the function to get
if (query_context && Poco::toLower(name) == "totime" && query_context->getSettingsRef()[Setting::use_legacy_to_time])
{
it = functions.find(ToTimeWithFixedDateImpl::name);
if (functions.end() != it)
res = it->second.first(context);
}
}
return res;
}
FunctionOverloadResolverPtr FunctionFactory::tryGet(
const std::string & name,
ContextPtr context) const
{
auto impl = tryGetImpl(name, context);
return impl ? std::move(impl) : nullptr;
}
FunctionFactory & FunctionFactory::instance()
{
static FunctionFactory ret;
return ret;
}
FunctionDocumentation FunctionFactory::getDocumentation(const std::string & name) const
{
auto it = functions.find(name);
if (it == functions.end())
throw Exception(ErrorCodes::UNKNOWN_FUNCTION, "Unknown function {}", name);
return it->second.second;
}
}