aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Planner/PlannerExpressionAnalysis.h
blob: 792cfdec2ffed49d8f86921d20941ddb786f28db (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
#pragma once

#include <Core/ColumnsWithTypeAndName.h>
#include <Core/InterpolateDescription.h>

#include <Analyzer/IQueryTreeNode.h>

#include <Interpreters/ActionsDAG.h>

#include <Planner/PlannerContext.h>
#include <Planner/PlannerAggregation.h>
#include <Planner/PlannerWindowFunctions.h>
#include <Planner/PlannerQueryProcessingInfo.h>

namespace DB
{

struct ProjectionAnalysisResult
{
    ActionsDAGPtr projection_actions;
    Names projection_column_names;
    NamesWithAliases projection_column_names_with_display_aliases;
    ActionsDAGPtr project_names_actions;
};

struct FilterAnalysisResult
{
    ActionsDAGPtr filter_actions;
    std::string filter_column_name;
    bool remove_filter_column = false;
};

struct AggregationAnalysisResult
{
    ActionsDAGPtr before_aggregation_actions;
    Names aggregation_keys;
    AggregateDescriptions aggregate_descriptions;
    GroupingSetsParamsList grouping_sets_parameters_list;
    bool group_by_with_constant_keys = false;
};

struct WindowAnalysisResult
{
    ActionsDAGPtr before_window_actions;
    std::vector<WindowDescription> window_descriptions;
};

struct SortAnalysisResult
{
    ActionsDAGPtr before_order_by_actions;
    bool has_with_fill = false;
};

struct LimitByAnalysisResult
{
    ActionsDAGPtr before_limit_by_actions;
    Names limit_by_column_names;
};

class PlannerExpressionsAnalysisResult
{
public:
    explicit PlannerExpressionsAnalysisResult(ProjectionAnalysisResult projection_analysis_result_)
        : projection_analysis_result(std::move(projection_analysis_result_))
    {}

    const ProjectionAnalysisResult & getProjection() const
    {
        return projection_analysis_result;
    }

    bool hasWhere() const
    {
        return where_analysis_result.filter_actions != nullptr;
    }

    const FilterAnalysisResult & getWhere() const
    {
        return where_analysis_result;
    }

    void addWhere(FilterAnalysisResult where_analysis_result_)
    {
        where_analysis_result = std::move(where_analysis_result_);
    }

    bool hasAggregation() const
    {
        return !aggregation_analysis_result.aggregation_keys.empty() || !aggregation_analysis_result.aggregate_descriptions.empty();
    }

    const AggregationAnalysisResult & getAggregation() const
    {
        return aggregation_analysis_result;
    }

    void addAggregation(AggregationAnalysisResult aggregation_analysis_result_)
    {
        aggregation_analysis_result = std::move(aggregation_analysis_result_);
    }

    bool hasHaving() const
    {
        return having_analysis_result.filter_actions != nullptr;
    }

    const FilterAnalysisResult & getHaving() const
    {
        return having_analysis_result;
    }

    void addHaving(FilterAnalysisResult having_analysis_result_)
    {
        having_analysis_result = std::move(having_analysis_result_);
    }

    bool hasWindow() const
    {
        return !window_analysis_result.window_descriptions.empty();
    }

    const WindowAnalysisResult & getWindow() const
    {
        return window_analysis_result;
    }

    void addWindow(WindowAnalysisResult window_analysis_result_)
    {
        window_analysis_result = std::move(window_analysis_result_);
    }

    bool hasSort() const
    {
        return sort_analysis_result.before_order_by_actions != nullptr;
    }

    const SortAnalysisResult & getSort() const
    {
        return sort_analysis_result;
    }

    void addSort(SortAnalysisResult sort_analysis_result_)
    {
        sort_analysis_result = std::move(sort_analysis_result_);
    }

    bool hasLimitBy() const
    {
        return limit_by_analysis_result.before_limit_by_actions != nullptr;
    }

    const LimitByAnalysisResult & getLimitBy() const
    {
        return limit_by_analysis_result;
    }

    void addLimitBy(LimitByAnalysisResult limit_by_analysis_result_)
    {
        limit_by_analysis_result = std::move(limit_by_analysis_result_);
    }

private:
    ProjectionAnalysisResult projection_analysis_result;
    FilterAnalysisResult where_analysis_result;
    AggregationAnalysisResult aggregation_analysis_result;
    FilterAnalysisResult having_analysis_result;
    WindowAnalysisResult window_analysis_result;
    SortAnalysisResult sort_analysis_result;
    LimitByAnalysisResult limit_by_analysis_result;
};

/// Build expression analysis result for query tree, join tree input columns and planner context
PlannerExpressionsAnalysisResult buildExpressionAnalysisResult(const QueryTreeNodePtr & query_tree,
    const ColumnsWithTypeAndName & join_tree_input_columns,
    const PlannerContextPtr & planner_context,
    const PlannerQueryProcessingInfo & planner_query_processing_info);

}