aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/SelectQueryOptions.h
blob: c91329c869c187512b5e0424858b2ba4d0fcb8d5 (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/QueryProcessingStage.h>
#include <optional>

namespace DB
{

/**
 * to_stage
 * - the stage to which the query is to be executed. By default - till to the end.
 *   You can perform till the intermediate aggregation state, which are combined from different servers for distributed query processing.
 *
 * subquery_depth
 * - to control the limit on the depth of nesting of subqueries. For subqueries, a value that is incremented by one is passed;
 *   for INSERT SELECT, a value 1 is passed instead of 0.
 *
 * only_analyze
 * - the object was created only for query analysis.
 *
 * is_subquery
 * - there could be some specific for subqueries. Ex. there's no need to pass duplicated columns in results, cause of indirect results.
 *
 * is_internal
 * - the object was created only for internal queries.
 */
struct SelectQueryOptions
{
    QueryProcessingStage::Enum to_stage;
    size_t subquery_depth;
    bool only_analyze = false;
    bool modify_inplace = false;
    bool remove_duplicates = false;
    bool ignore_quota = false;
    bool ignore_limits = false;
    /// This flag is needed to analyze query ignoring table projections.
    /// It is needed because we build another one InterpreterSelectQuery while analyzing projections.
    /// It helps to avoid infinite recursion.
    bool ignore_projections = false;
    /// This flag is also used for projection analysis.
    /// It is needed because lazy normal projections require special planning in FetchColumns stage, such as adding WHERE transform.
    /// It is also used to avoid adding aggregating step when aggregate projection is chosen.
    bool is_projection_query = false;
    /// This flag is needed for projection description.
    /// Otherwise, keys for GROUP BY may be removed as constants.
    bool ignore_ast_optimizations = false;
    bool ignore_alias = false;
    bool is_internal = false;
    bool is_subquery = false; // non-subquery can also have subquery_depth > 0, e.g. insert select
    bool with_all_cols = false; /// asterisk include materialized and aliased columns
    bool settings_limit_offset_done = false;
    bool is_explain = false; /// The value is true if it's explain statement.
    bool is_create_parameterized_view = false;
    /// Bypass setting constraints for some internal queries such as projection ASTs.
    bool ignore_setting_constraints = false;

    /// These two fields are used to evaluate shardNum() and shardCount() function when
    /// prefer_localhost_replica == 1 and local instance is selected. They are needed because local
    /// instance might have multiple shards and scalars can only hold one value.
    std::optional<UInt32> shard_num;
    std::optional<UInt32> shard_count;

    SelectQueryOptions(
        QueryProcessingStage::Enum stage = QueryProcessingStage::Complete,
        size_t depth = 0,
        bool is_subquery_ = false,
        bool settings_limit_offset_done_ = false)
        : to_stage(stage), subquery_depth(depth), is_subquery(is_subquery_),
        settings_limit_offset_done(settings_limit_offset_done_)
    {}

    SelectQueryOptions copy() const { return *this; }

    SelectQueryOptions subquery() const
    {
        SelectQueryOptions out = *this;
        out.to_stage = QueryProcessingStage::Complete;
        ++out.subquery_depth;
        out.is_subquery = true;
        return out;
    }

    SelectQueryOptions createParameterizedView() const
    {
        SelectQueryOptions out = *this;
        out.is_create_parameterized_view = true;
        return out;
    }

    SelectQueryOptions & analyze(bool dry_run = true)
    {
        only_analyze = dry_run;
        return *this;
    }

    SelectQueryOptions & modify(bool value = true)
    {
        modify_inplace = value;
        return *this;
    }

    SelectQueryOptions & noModify() { return modify(false); }

    SelectQueryOptions & removeDuplicates(bool value = true)
    {
        remove_duplicates = value;
        return *this;
    }

    SelectQueryOptions & noSubquery()
    {
        subquery_depth = 0;
        return *this;
    }

    SelectQueryOptions & ignoreLimits(bool value = true)
    {
        ignore_limits = value;
        return *this;
    }

    SelectQueryOptions & ignoreProjections(bool value = true)
    {
        ignore_projections = value;
        return *this;
    }

    SelectQueryOptions & projectionQuery(bool value = true)
    {
        is_projection_query = value;
        return *this;
    }

    SelectQueryOptions & ignoreAlias(bool value = true)
    {
        ignore_alias = value;
        return *this;
    }

    SelectQueryOptions & ignoreASTOptimizations(bool value = true)
    {
        ignore_ast_optimizations = value;
        return *this;
    }

    SelectQueryOptions & ignoreSettingConstraints(bool value = true)
    {
        ignore_setting_constraints = value;
        return *this;
    }

    SelectQueryOptions & setInternal(bool value = false)
    {
        is_internal = value;
        return *this;
    }

    SelectQueryOptions & setWithAllColumns(bool value = true)
    {
        with_all_cols = value;
        return *this;
    }

    SelectQueryOptions & setShardInfo(UInt32 shard_num_, UInt32 shard_count_)
    {
        shard_num = shard_num_;
        shard_count = shard_count_;
        return *this;
    }

    SelectQueryOptions & setExplain(bool value = true)
    {
        is_explain = value;
        return *this;
    }
};

}