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

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

namespace DB
{

namespace ErrorCodes
{
    extern const int ILLEGAL_AGGREGATION;
}

namespace
{

class CollectAggregateFunctionNodesVisitor : public ConstInDepthQueryTreeVisitor<CollectAggregateFunctionNodesVisitor>
{
public:
    explicit CollectAggregateFunctionNodesVisitor(QueryTreeNodes * aggregate_function_nodes_)
        : aggregate_function_nodes(aggregate_function_nodes_)
    {}

    explicit CollectAggregateFunctionNodesVisitor(String assert_no_aggregates_place_message_)
        : assert_no_aggregates_place_message(std::move(assert_no_aggregates_place_message_))
    {}

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

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

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

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

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

        has_aggregate_functions = true;
    }

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

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

    bool hasAggregateFunctions() const
    {
        return has_aggregate_functions;
    }

private:
    String assert_no_aggregates_place_message;
    QueryTreeNodes * aggregate_function_nodes = nullptr;
    bool only_check = false;
    bool has_aggregate_functions = false;
};

}

QueryTreeNodes collectAggregateFunctionNodes(const QueryTreeNodePtr & node)
{
    QueryTreeNodes result;
    CollectAggregateFunctionNodesVisitor visitor(&result);
    visitor.visit(node);

    return result;
}

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

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

    return visitor.hasAggregateFunctions();
}

void assertNoAggregateFunctionNodes(const QueryTreeNodePtr & node, const String & assert_no_aggregates_place_message)
{
    CollectAggregateFunctionNodesVisitor visitor(assert_no_aggregates_place_message);
    visitor.visit(node);
}

void assertNoGroupingFunctionNodes(const QueryTreeNodePtr & node, const String & assert_no_grouping_function_place_message)
{
    assertNoFunctionNodes(node, "grouping", ErrorCodes::ILLEGAL_AGGREGATION, "GROUPING", assert_no_grouping_function_place_message);
}

}