forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathFunctionNode.cpp
More file actions
326 lines (260 loc) · 10.8 KB
/
FunctionNode.cpp
File metadata and controls
326 lines (260 loc) · 10.8 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <Analyzer/FunctionNode.h>
#include <Columns/ColumnConst.h>
#include <Common/SipHash.h>
#include <Common/FieldVisitorToString.h>
#include <IO/WriteBufferFromString.h>
#include <IO/Operators.h>
#include <DataTypes/IDataType.h>
#include <DataTypes/DataTypeSet.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTSetQuery.h>
#include <Functions/IFunction.h>
#include <AggregateFunctions/IAggregateFunction.h>
#include <Analyzer/Utils.h>
#include <Analyzer/ConstantNode.h>
#include <Analyzer/IdentifierNode.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
FunctionNode::FunctionNode(String function_name_)
: IQueryTreeNode(children_size)
, function_name(function_name_)
{
children[parameters_child_index] = std::make_shared<ListNode>();
children[arguments_child_index] = std::make_shared<ListNode>();
}
const DataTypes & FunctionNode::getArgumentTypes() const
{
if (!function)
throw Exception(ErrorCodes::LOGICAL_ERROR,
"Function {} is not resolved",
function_name);
return function->getArgumentTypes();
}
ColumnsWithTypeAndName FunctionNode::getArgumentColumns() const
{
const auto & arguments = getArguments().getNodes();
size_t arguments_size = arguments.size();
ColumnsWithTypeAndName argument_columns;
argument_columns.reserve(arguments.size());
for (size_t i = 0; i < arguments_size; ++i)
{
const auto & argument = arguments[i];
ColumnWithTypeAndName argument_column;
auto * constant = argument->as<ConstantNode>();
if (isNameOfInFunction(function_name) && i == 1)
{
argument_column.type = std::make_shared<DataTypeSet>();
if (constant)
{
/// Created but not filled for the analysis during function resolution.
FutureSetPtr empty_set;
argument_column.column = ColumnConst::create(ColumnSet::create(1, empty_set), 1);
}
}
else
argument_column.type = argument->getResultType();
if (constant && !isNotCreatable(argument_column.type))
argument_column.column = constant->getColumn();
argument_columns.push_back(std::move(argument_column));
}
return argument_columns;
}
AggregateFunctionPtr FunctionNode::getAggregateFunction() const
{
if (kind == FunctionKind::UNKNOWN || kind == FunctionKind::ORDINARY)
return {};
return std::static_pointer_cast<const IAggregateFunction>(function);
}
void FunctionNode::resolveAsFunction(FunctionBasePtr function_value)
{
function_name = function_value->getName();
function = std::move(function_value);
kind = FunctionKind::ORDINARY;
nulls_action = NullsAction::EMPTY;
}
void FunctionNode::resolveAsAggregateFunction(AggregateFunctionPtr aggregate_function_value)
{
function_name = aggregate_function_value->getName();
function = std::move(aggregate_function_value);
kind = FunctionKind::AGGREGATE;
/** When the function is resolved, we do not need the nulls action anymore.
* The only thing that the nulls action does is map from one function to another.
* Thus, the nulls action is encoded in the function name and does not make sense anymore.
* Keeping the nulls action may lead to incorrect comparison of functions, e.g., count() and count() IGNORE NULLS are the same function.
*/
nulls_action = NullsAction::EMPTY;
}
void FunctionNode::resolveAsWindowFunction(AggregateFunctionPtr window_function_value)
{
if (!hasWindow())
throw Exception(ErrorCodes::LOGICAL_ERROR,
"Trying to resolve FunctionNode without window definition as a window function {}", window_function_value->getName());
resolveAsAggregateFunction(window_function_value);
kind = FunctionKind::WINDOW;
}
void FunctionNode::dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, size_t indent) const
{
buffer << std::string(indent, ' ') << "FUNCTION id: " << format_state.getNodeId(this);
if (hasAlias())
buffer << ", alias: " << getAlias();
buffer << ", function_name: " << function_name;
std::string function_type = "ordinary";
if (isAggregateFunction())
function_type = "aggregate";
else if (isWindowFunction())
function_type = "window";
buffer << ", function_type: " << function_type;
if (nulls_action == NullsAction::RESPECT_NULLS)
buffer << ", nulls_action : RESPECT_NULLS";
else if (nulls_action == NullsAction::IGNORE_NULLS)
buffer << ", nulls_action : IGNORE_NULLS";
if (function)
buffer << ", result_type: " + getResultType()->getName();
const auto & parameters = getParameters();
if (!parameters.getNodes().empty())
{
buffer << '\n' << std::string(indent + 2, ' ') << "PARAMETERS\n";
parameters.dumpTreeImpl(buffer, format_state, indent + 4);
}
const auto & arguments = getArguments();
if (!arguments.getNodes().empty())
{
buffer << '\n' << std::string(indent + 2, ' ') << "ARGUMENTS\n";
arguments.dumpTreeImpl(buffer, format_state, indent + 4);
}
if (hasWindow())
{
buffer << '\n' << std::string(indent + 2, ' ') << "WINDOW\n";
getWindowNode()->dumpTreeImpl(buffer, format_state, indent + 4);
}
if (!settings_changes.empty())
{
buffer << '\n' << std::string(indent + 2, ' ') << "SETTINGS";
for (const auto & change : settings_changes)
buffer << fmt::format(" {}={}", change.name, fieldToString(change.value));
}
}
bool FunctionNode::isEqualImpl(const IQueryTreeNode & rhs, CompareOptions compare_options) const
{
const auto & rhs_typed = assert_cast<const FunctionNode &>(rhs);
if (function_name != rhs_typed.function_name || isAggregateFunction() != rhs_typed.isAggregateFunction()
|| isOrdinaryFunction() != rhs_typed.isOrdinaryFunction() || isWindowFunction() != rhs_typed.isWindowFunction()
|| nulls_action != rhs_typed.nulls_action || settings_changes != rhs_typed.settings_changes)
return false;
/// is_operator is ignored here because it affects only AST formatting
if (!compare_options.compare_types)
return true;
if (isResolved() != rhs_typed.isResolved())
return false;
if (!isResolved())
return true;
auto lhs_result_type = getResultType();
auto rhs_result_type = rhs.getResultType();
if (lhs_result_type && rhs_result_type && !lhs_result_type->equals(*rhs_result_type))
return false;
if (lhs_result_type && !rhs_result_type)
return false;
if (!lhs_result_type && rhs_result_type)
return false;
return true;
}
void FunctionNode::updateTreeHashImpl(HashState & hash_state, CompareOptions compare_options) const
{
hash_state.update(function_name.size());
hash_state.update(function_name);
hash_state.update(isOrdinaryFunction());
hash_state.update(isAggregateFunction());
hash_state.update(isWindowFunction());
hash_state.update(nulls_action);
hash_state.update(settings_changes.size());
for (const auto & change : settings_changes)
{
hash_state.update(change.name.size());
hash_state.update(change.name);
const auto & value_dump = change.value.dump();
hash_state.update(value_dump.size());
hash_state.update(value_dump);
}
/// is_operator is ignored here because it affects only AST formatting
if (!compare_options.compare_types)
return;
if (!isResolved())
return;
if (auto result_type = getResultType())
result_type->updateHash(hash_state);
}
QueryTreeNodePtr FunctionNode::cloneImpl() const
{
auto result_function = std::make_shared<FunctionNode>(function_name);
/** This is valid for clone method to reuse same function pointers
* because ordinary functions or aggregate functions must be stateless.
*/
result_function->function = function;
result_function->kind = kind;
result_function->nulls_action = nulls_action;
result_function->wrap_with_nullable = wrap_with_nullable;
result_function->is_operator = is_operator;
result_function->settings_changes = settings_changes;
return result_function;
}
ASTPtr FunctionNode::toASTImpl(const ConvertToASTOptions & options) const
{
auto function_ast = make_intrusive<ASTFunction>();
function_ast->name = function_name;
function_ast->nulls_action = nulls_action;
function_ast->is_operator = is_operator;
if (isWindowFunction())
{
function_ast->is_window_function = true;
function_ast->kind = ASTFunction::Kind::WINDOW_FUNCTION;
}
const auto & arguments = getArguments();
auto new_options = options;
const auto & argument_nodes = arguments.getNodes();
/// To avoid surrounding constants with several internal casts.
if (function_name == "_CAST" && !argument_nodes.empty() && argument_nodes[0]->getNodeType() == QueryTreeNodeType::CONSTANT)
new_options.add_cast_for_constants = false;
const auto & parameters = getParameters();
if (!parameters.getNodes().empty())
{
function_ast->children.push_back(parameters.toAST(new_options));
function_ast->parameters = function_ast->children.back();
}
/// We have to avoid cast for second argument of `IN` functions - it can be a quite big
/// tuple, and adding a type may significantly increase query size.
/// It should be safe because set type for `column IN tuple` is deduced from `column` type.
if (isNameOfInFunction(function_name) && argument_nodes.size() > 1 && argument_nodes[1]->getNodeType() == QueryTreeNodeType::CONSTANT
&& !static_cast<const ConstantNode *>(argument_nodes[1].get())->hasSourceExpression())
{
auto expression_list_ast = make_intrusive<ASTExpressionList>();
expression_list_ast->children.push_back(argument_nodes[0]->toAST(new_options));
auto arg_options = new_options;
arg_options.add_cast_for_constants = false;
expression_list_ast->children.push_back(argument_nodes[1]->toAST(arg_options));
function_ast->children.push_back(expression_list_ast);
}
else
function_ast->children.push_back(arguments.toAST(new_options));
function_ast->arguments = function_ast->children.back();
auto window_node = getWindowNode();
if (window_node)
{
if (auto * identifier_node = window_node->as<IdentifierNode>())
function_ast->window_name = identifier_node->getIdentifier().getFullName();
else
function_ast->window_definition = window_node->toAST(new_options);
}
if (!settings_changes.empty())
{
auto settings_ast = make_intrusive<ASTSetQuery>();
settings_ast->changes = settings_changes;
settings_ast->is_standalone = false;
function_ast->arguments->children.push_back(settings_ast);
}
return function_ast;
}
}