aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Analyzer/FunctionNode.cpp
blob: f5bcdc103d2c62fe740d09ad3af93a68fe165a1f (plain) (blame)
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
#include <Analyzer/FunctionNode.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 <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;

        if (isNameOfInFunction(function_name) && i == 1)
            argument_column.type = std::make_shared<DataTypeSet>();
        else
            argument_column.type = argument->getResultType();

        auto * constant = argument->as<ConstantNode>();
        if (constant && !isNotCreatable(argument_column.type))
            argument_column.column = argument_column.type->createColumnConst(1, constant->getValue());

        argument_columns.push_back(std::move(argument_column));
    }

    return argument_columns;
}

void FunctionNode::resolveAsFunction(FunctionBasePtr function_value)
{
    function_name = function_value->getName();
    function = std::move(function_value);
    kind = FunctionKind::ORDINARY;
}

void FunctionNode::resolveAsAggregateFunction(AggregateFunctionPtr aggregate_function_value)
{
    function_name = aggregate_function_value->getName();
    function = std::move(aggregate_function_value);
    kind = FunctionKind::AGGREGATE;
}

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 (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);
    }
}

bool FunctionNode::isEqualImpl(const IQueryTreeNode & rhs) 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())
        return false;

    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;
    else if (lhs_result_type && !rhs_result_type)
        return false;
    else if (!lhs_result_type && rhs_result_type)
        return false;

    return true;
}

void FunctionNode::updateTreeHashImpl(HashState & hash_state) const
{
    hash_state.update(function_name.size());
    hash_state.update(function_name);
    hash_state.update(isOrdinaryFunction());
    hash_state.update(isAggregateFunction());
    hash_state.update(isWindowFunction());

    if (!isResolved())
        return;

    if (auto result_type = getResultType())
    {
        auto result_type_name = result_type->getName();
        hash_state.update(result_type_name.size());
        hash_state.update(result_type_name);
    }
}

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->wrap_with_nullable = wrap_with_nullable;

    return result_function;
}

ASTPtr FunctionNode::toASTImpl(const ConvertToASTOptions & options) const
{
    auto function_ast = std::make_shared<ASTFunction>();

    function_ast->name = function_name;

    if (isWindowFunction())
    {
        function_ast->is_window_function = true;
        function_ast->kind = ASTFunction::Kind::WINDOW_FUNCTION;
    }

    auto new_options = options;
    /// To avoid surrounding constants with several internal casts.
    if (function_name == "_CAST" && (*getArguments().begin())->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();
    }

    const auto & arguments = getArguments();
    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);
    }

    return function_ast;
}

}