/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/ptree/sem_state.h
Line | Count | Source (jump to first uncovered line) |
1 | | //-------------------------------------------------------------------------------------------------- |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | // |
15 | | // The SemState module defines the states of semantic process for expressions. Semantic states are |
16 | | // different from semantic context. |
17 | | // - The states consists of attributes that are used to process a tree node. |
18 | | // - The context consists of attributes that are used for the entire compilation. |
19 | | //-------------------------------------------------------------------------------------------------- |
20 | | |
21 | | #ifndef YB_YQL_CQL_QL_PTREE_SEM_STATE_H_ |
22 | | #define YB_YQL_CQL_QL_PTREE_SEM_STATE_H_ |
23 | | |
24 | | #include "yb/common/common_fwd.h" |
25 | | #include "yb/common/ql_datatype.h" |
26 | | |
27 | | #include "yb/util/memory/mc_types.h" |
28 | | #include "yb/util/strongly_typed_bool.h" |
29 | | |
30 | | #include "yb/yql/cql/ql/ptree/ptree_fwd.h" |
31 | | |
32 | | namespace yb { |
33 | | namespace ql { |
34 | | |
35 | | class SelectScanInfo; |
36 | | class WhereExprState; |
37 | | class IfExprState; |
38 | | class IdxPredicateState; |
39 | | class PTColumnDefinition; |
40 | | class PTDmlStmt; |
41 | | |
42 | | //-------------------------------------------------------------------------------------------------- |
43 | | // This class represents the state variables for the analyzing process of one tree node. This |
44 | | // is just a stack variable that is constructed when a treenode is being processed and destructed |
45 | | // when that process is done. |
46 | | // |
47 | | // Example: |
48 | | // - Suppose user type the following statements |
49 | | // CREATE TABLE tab(id INT PRIMARY KEY); |
50 | | // INSERT INTO tab(id) values(expr); |
51 | | // - When analyzing INSERT, we would do the following. |
52 | | // { |
53 | | // // Create a new state for sem_context. |
54 | | // SemState new_expr_state(sem_context, DataType::INT); |
55 | | // |
56 | | // // Run express analyzer knowing that its expected type INT (=== targeted column type). |
57 | | // expr->Analyze(sem_context); |
58 | | // |
59 | | // // When exiting this scope, sem_state are auto-switched back to the previous state. |
60 | | // } |
61 | | // |
62 | | class SemState { |
63 | | public: |
64 | | // Constructor: Create a new sem_state to use and save the existing state to previous_state_. |
65 | | explicit SemState(SemContext *sem_context, |
66 | | const QLTypePtr& expected_ql_type = DefaultQLType(), |
67 | | InternalType expected_internal_type = InternalType::VALUE_NOT_SET, |
68 | | const MCSharedPtr<MCString>& bindvar_name = nullptr, |
69 | | const ColumnDesc *lhs_col = nullptr, |
70 | | NullIsAllowed allow_null = NullIsAllowed::kTrue); |
71 | | |
72 | | // Destructor: Reset sem_context back to previous_state_. |
73 | | virtual ~SemState(); |
74 | | |
75 | | // Read previous state. |
76 | 2 | const SemState *previous_state() const { |
77 | 2 | return previous_state_; |
78 | 2 | } |
79 | | |
80 | | // Reset the sem_context back to its previous state. |
81 | | void ResetContextState(); |
82 | | |
83 | | // State variable for index analysis. |
84 | 539k | void SetScanState(SelectScanInfo *scan_state) { |
85 | 539k | scan_state_ = scan_state; |
86 | 539k | } |
87 | 2.38M | SelectScanInfo *scan_state() { |
88 | 2.38M | return scan_state_; |
89 | 2.38M | } |
90 | | |
91 | | // State variable to checking key condition. |
92 | 1.65M | bool void_primary_key_condition() const { |
93 | 1.65M | return void_primary_key_condition_; |
94 | 1.65M | } |
95 | | |
96 | 542 | void set_void_primary_key_condition(bool val) { |
97 | 542 | void_primary_key_condition_ = val; |
98 | 542 | } |
99 | | |
100 | | // Update state variable for where clause. |
101 | 100k | void SetWhereState(WhereExprState *where_state) { |
102 | 100k | where_state_ = where_state; |
103 | 100k | } |
104 | 1.99M | WhereExprState *where_state() const { return where_state_; } |
105 | | |
106 | | // Update state variable for if clause. |
107 | 0 | void SetIfState(IfExprState *if_state) { |
108 | 0 | if_state_ = if_state; |
109 | 0 | } |
110 | 0 | IfExprState *if_state() const { return if_state_; } |
111 | | |
112 | | // Update state variable for orderby clause. |
113 | 230 | void set_validate_orderby_expr(bool validate_orderby_expr) { |
114 | 230 | validate_orderby_expr_ = validate_orderby_expr; |
115 | 230 | } |
116 | 1.53M | bool validate_orderby_expr() const { |
117 | 1.53M | return validate_orderby_expr_; |
118 | 1.53M | } |
119 | | |
120 | | // Update the expr states. |
121 | | void SetExprState(const std::shared_ptr<QLType>& ql_type, |
122 | | InternalType internal_type, |
123 | | const MCSharedPtr<MCString>& bindvar_name = nullptr, |
124 | | const ColumnDesc *lhs_col = nullptr, |
125 | | NullIsAllowed allow_null = NullIsAllowed::kTrue); |
126 | | |
127 | | // Update state variable for index predicate clause i.e, where clause. |
128 | 132 | void SetIdxPredicateState(IdxPredicateState *idx_predicate_state) { |
129 | 132 | idx_predicate_state_ = idx_predicate_state; |
130 | 132 | } |
131 | 3.17M | IdxPredicateState *idx_predicate_state() const { return idx_predicate_state_; } |
132 | | |
133 | | // Set the current state using previous state's values. |
134 | | void CopyPreviousStates(); |
135 | | |
136 | | // Set the current state using previous state's values. |
137 | | void CopyPreviousWhereState(); |
138 | | |
139 | | // Set the current state using previous state's values. |
140 | | void CopyPreviousIfState(); |
141 | | |
142 | | // Access function for expression states. |
143 | 2.27M | const std::shared_ptr<QLType>& expected_ql_type() const { return expected_ql_type_; } |
144 | 160k | NullIsAllowed expected_ql_type_accepts_null() const { return allow_null_ql_type_; } |
145 | 1.53M | InternalType expected_internal_type() const { return expected_internal_type_; } |
146 | | |
147 | | // Return the hash column descriptor on LHS if available. |
148 | 14.8k | const ColumnDesc *lhs_col() const { return lhs_col_; } |
149 | | const ColumnDesc *hash_col() const; |
150 | | |
151 | | void set_bindvar_name(std::string bindvar_name); |
152 | 248k | const MCSharedPtr<MCString>& bindvar_name() const { return bindvar_name_; } |
153 | | |
154 | 2.83M | PTDmlStmt *current_dml_stmt() const { |
155 | 2.83M | return current_dml_stmt_; |
156 | 2.83M | } |
157 | | |
158 | 323k | void set_current_dml_stmt(PTDmlStmt *stmt) { |
159 | 323k | current_dml_stmt_ = stmt; |
160 | 323k | } |
161 | | |
162 | 3.06M | bool processing_set_clause() const { return processing_set_clause_; } |
163 | 3.32k | void set_processing_set_clause(bool value) { processing_set_clause_ = value; } |
164 | | |
165 | 1.54M | bool processing_assignee() const { return processing_assignee_; } |
166 | 14.0k | void set_processing_assignee(bool value) { processing_assignee_ = value; } |
167 | | |
168 | 765 | void set_index_select_prefix_length(size_t val) { index_select_prefix_length_ = val; } |
169 | 1.53M | size_t index_select_prefix_length() const { return index_select_prefix_length_; } |
170 | | |
171 | 784 | void set_selecting_from_index(bool val) { selecting_from_index_ = val; } |
172 | 3.68M | bool selecting_from_index() const { return selecting_from_index_; } |
173 | | |
174 | 4.02k | void set_processing_column_definition(bool val) { processing_column_definition_ = val; } |
175 | 14.3k | bool processing_column_definition() const { return processing_column_definition_; } |
176 | | |
177 | 2.02M | bool processing_if_clause() const { return processing_if_clause_; } |
178 | 327 | void set_processing_if_clause(bool value) { processing_if_clause_ = value; } |
179 | | |
180 | 186 | bool allowing_aggregate() const { |
181 | 186 | return allowing_aggregate_; |
182 | 186 | } |
183 | 541k | void set_allowing_aggregate(bool val) { |
184 | 541k | allowing_aggregate_ = val; |
185 | 541k | } |
186 | | |
187 | 1.53M | bool allowing_column_refs() const { |
188 | 1.53M | return allowing_column_refs_; |
189 | 1.53M | } |
190 | 1.00M | void set_allowing_column_refs(bool val) { |
191 | 1.00M | allowing_column_refs_ = val; |
192 | 1.00M | } |
193 | | |
194 | 3.84k | void set_processing_index_column(PTColumnDefinition *index_column) { |
195 | 3.84k | index_column_ = index_column; |
196 | 3.84k | } |
197 | 5.04k | bool is_processing_index_column() const { |
198 | 5.04k | return (index_column_ != nullptr); |
199 | 5.04k | } |
200 | | void add_index_column_ref(int32_t col_id); |
201 | | |
202 | | bool is_uncovered_index_select() const; |
203 | | |
204 | | bool is_partial_index_select() const; |
205 | | |
206 | | private: |
207 | | static const QLTypePtr& DefaultQLType(); |
208 | | |
209 | | // Context that owns this SemState. |
210 | | SemContext *sem_context_; |
211 | | |
212 | | // The current dml statement being processed. |
213 | | PTDmlStmt *current_dml_stmt_ = nullptr; |
214 | | |
215 | | // Save the previous state to reset when done. |
216 | | SemState *previous_state_ = nullptr; |
217 | | bool was_reset = false; |
218 | | |
219 | | // States to process an expression node. |
220 | | std::shared_ptr<QLType> expected_ql_type_; // The expected sql type of an expression. |
221 | | NullIsAllowed allow_null_ql_type_; // Does the expected type accept NULL? |
222 | | InternalType expected_internal_type_; // The expected internal type of an expression. |
223 | | |
224 | | MCSharedPtr<MCString> bindvar_name_ = nullptr; |
225 | | |
226 | | // State variables for index analysis. |
227 | | SelectScanInfo *scan_state_ = nullptr; |
228 | | |
229 | | // State variable for analyzing key condition. |
230 | | // - If select-scan is using nested INDEX key, primary key condition should be dropped off, and |
231 | | // this flag should be set to true. Error on primary key condition should be ignored because |
232 | | // the secondary index key is used in place of primary key. |
233 | | // - Currently, this flag is only used for query that has nested INDEX query. YugaByte does not |
234 | | // have any other kinds of nested query |
235 | | bool void_primary_key_condition_ = false; |
236 | | |
237 | | // State variables for where expression. |
238 | | WhereExprState *where_state_ = nullptr; |
239 | | |
240 | | // State variables for if expression. |
241 | | IfExprState *if_state_ = nullptr; |
242 | | |
243 | | // State variables for orderby expression. |
244 | | bool validate_orderby_expr_ = false; |
245 | | |
246 | | // State variables for index predicate expression i.e., where clause in case of partial indexes. |
247 | | IdxPredicateState *idx_predicate_state_ = nullptr; |
248 | | |
249 | | // Predicate for selecting data from an index instead of a user table. |
250 | | bool selecting_from_index_ = false; |
251 | | |
252 | | // Length of prefix of cols in index table that have a sub-clause in WHERE with =/IN operator. |
253 | | size_t index_select_prefix_length_ = 0; |
254 | | |
255 | | // Predicate for processing a column definition in a table. |
256 | | bool processing_column_definition_ = false; |
257 | | |
258 | | // Descriptor for the LHS column. |
259 | | const ColumnDesc *lhs_col_ = nullptr; |
260 | | |
261 | | // State variables for if clause. |
262 | | bool processing_if_clause_ = false; |
263 | | |
264 | | // State variable for set clause. |
265 | | bool processing_set_clause_ = false; |
266 | | |
267 | | // State variable for assignee. |
268 | | bool processing_assignee_ = false; |
269 | | |
270 | | // State variable for aggregate function. |
271 | | bool allowing_aggregate_ = false; |
272 | | |
273 | | // State variable for allowing column references. |
274 | | bool allowing_column_refs_ = false; |
275 | | |
276 | | // State variable for processing index column. |
277 | | PTColumnDefinition *index_column_ = nullptr; |
278 | | }; |
279 | | |
280 | | } // namespace ql |
281 | | } // namespace yb |
282 | | |
283 | | #endif // YB_YQL_CQL_QL_PTREE_SEM_STATE_H_ |