aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Analyzer/WindowFunctionsUtils.cpp
blob: 811ccc2cd1a024a487c2f5e5b4a51f946ac13d1c (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
#include <Analyzer/WindowFunctionsUtils.h>

#include <Analyzer/IQueryTreeNode.h>
#include <Analyzer/InDepthQueryTreeVisitor.h>
#include <Analyzer/FunctionNode.h>

namespace DB
{

namespace ErrorCodes
{
    extern const int ILLEGAL_AGGREGATION;
}

namespace
{

class CollectWindowFunctionNodeVisitor : public ConstInDepthQueryTreeVisitor<CollectWindowFunctionNodeVisitor>
{
public:
    explicit CollectWindowFunctionNodeVisitor(QueryTreeNodes * window_function_nodes_)
        : window_function_nodes(window_function_nodes_)
    {}

    explicit CollectWindowFunctionNodeVisitor(String assert_no_window_functions_place_message_)
        : assert_no_window_functions_place_message(std::move(assert_no_window_functions_place_message_))
    {}

    explicit CollectWindowFunctionNodeVisitor(bool only_check_)
        : only_check(only_check_)
    {}

    void visitImpl(const QueryTreeNodePtr & node)
    {
        if (only_check && has_window_functions)
            return;

        auto * function_node = node->as<FunctionNode>();
        if (!function_node || !function_node->isWindowFunction())
            return;

        if (!assert_no_window_functions_place_message.empty())
            throw Exception(ErrorCodes::ILLEGAL_AGGREGATION,
                "Window function {} is found {} in query",
                function_node->formatASTForErrorMessage(),
                assert_no_window_functions_place_message);

        if (window_function_nodes)
            window_function_nodes->push_back(node);

        has_window_functions = true;
    }

    bool needChildVisit(const QueryTreeNodePtr &, const QueryTreeNodePtr & child_node) const
    {
        if (only_check && has_window_functions)
            return false;

        auto child_node_type = child_node->getNodeType();
        return !(child_node_type == QueryTreeNodeType::QUERY || child_node_type == QueryTreeNodeType::UNION);
    }

    bool hasWindowFunctions() const
    {
        return has_window_functions;
    }
private:
    QueryTreeNodes * window_function_nodes = nullptr;
    String assert_no_window_functions_place_message;
    bool only_check = false;
    bool has_window_functions = false;
};

}

QueryTreeNodes collectWindowFunctionNodes(const QueryTreeNodePtr & node)
{
    QueryTreeNodes window_function_nodes;
    CollectWindowFunctionNodeVisitor visitor(&window_function_nodes);
    visitor.visit(node);

    return window_function_nodes;
}

bool hasWindowFunctionNodes(const QueryTreeNodePtr & node)
{
    CollectWindowFunctionNodeVisitor visitor(true /*only_check*/);
    visitor.visit(node);

    return visitor.hasWindowFunctions();
}

void collectWindowFunctionNodes(const QueryTreeNodePtr & node, QueryTreeNodes & result)
{
    CollectWindowFunctionNodeVisitor visitor(&result);
    visitor.visit(node);
}

void assertNoWindowFunctionNodes(const QueryTreeNodePtr & node, const String & assert_no_window_functions_place_message)
{
    CollectWindowFunctionNodeVisitor visitor(assert_no_window_functions_place_message);
    visitor.visit(node);
}

}