/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/ptree/parse_tree.h
Line | Count | Source |
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 | | // Parse Tree Declaration. |
16 | | // |
17 | | // This modules includes declarations for parse tree. The parser whose rules are defined in |
18 | | // parser_gram.y will link the tree nodes together to form this parse tree. |
19 | | //-------------------------------------------------------------------------------------------------- |
20 | | |
21 | | #ifndef YB_YQL_CQL_QL_PTREE_PARSE_TREE_H_ |
22 | | #define YB_YQL_CQL_QL_PTREE_PARSE_TREE_H_ |
23 | | |
24 | | #include <functional> |
25 | | #include <memory> |
26 | | |
27 | | #include "yb/client/yb_table_name.h" |
28 | | |
29 | | #include "yb/rpc/rpc_fwd.h" |
30 | | |
31 | | #include "yb/util/mem_tracker.h" |
32 | | #include "yb/util/memory/arena.h" |
33 | | |
34 | | #include "yb/yql/cql/ql/ptree/tree_node.h" |
35 | | #include "yb/yql/cql/ql/util/ql_env.h" |
36 | | |
37 | | namespace yb { |
38 | | namespace ql { |
39 | | |
40 | | // Parse Tree |
41 | | class ParseTree { |
42 | | public: |
43 | | //------------------------------------------------------------------------------------------------ |
44 | | // Public types. |
45 | | typedef std::unique_ptr<ParseTree> UniPtr; |
46 | | typedef std::unique_ptr<const ParseTree> UniPtrConst; |
47 | | |
48 | | //------------------------------------------------------------------------------------------------ |
49 | | // Public functions. |
50 | | |
51 | | // Constructs a parse tree. The parse tree saves a reference to the statement string. |
52 | | ParseTree(const std::string& stmt, bool reparsed, const MemTrackerPtr& mem_tracker = nullptr, |
53 | | const bool internal = false); |
54 | | ~ParseTree(); |
55 | | |
56 | | // Run semantics analysis. |
57 | | CHECKED_STATUS Analyze(SemContext *sem_context); |
58 | | |
59 | | // Access function for stmt_. |
60 | 10.5M | const std::string& stmt() const { |
61 | 10.5M | return stmt_; |
62 | 10.5M | } |
63 | | |
64 | | // Access function to reparsed_. |
65 | 6.82k | bool reparsed() const { |
66 | 6.82k | return reparsed_; |
67 | 6.82k | } |
68 | 9.11M | void clear_reparsed() const { |
69 | 9.11M | reparsed_ = false; |
70 | 9.11M | } |
71 | | |
72 | | // Access functions to root_. |
73 | 340k | void set_root(const TreeNode::SharedPtr& root) { |
74 | 340k | root_ = root; |
75 | 340k | } |
76 | 10.0M | const TreeNode::SharedPtr& root() const { |
77 | 10.0M | return root_; |
78 | 10.0M | } |
79 | | |
80 | | // Access function to ptree_mem_. |
81 | 11.0M | MemoryContext *PTreeMem() const { |
82 | 11.0M | return &ptree_mem_; |
83 | 11.0M | } |
84 | | |
85 | | // Access function to psem_mem_. |
86 | 3.77M | MemoryContext *PSemMem() const { |
87 | 3.77M | return &psem_mem_; |
88 | 3.77M | } |
89 | | |
90 | | // Access function to stale_. |
91 | 18.2M | bool stale() const { |
92 | 18.2M | return stale_; |
93 | 18.2M | } |
94 | | |
95 | 2.52k | void set_stale() const { |
96 | 2.52k | stale_ = true; |
97 | 2.52k | } |
98 | | |
99 | 129k | bool internal() const { |
100 | 129k | return internal_; |
101 | 129k | } |
102 | | |
103 | | // Add table to the set of tables used during semantic analysis. |
104 | | void AddAnalyzedTable(const client::YBTableName& table_name); |
105 | | |
106 | | // Clear the metadata cache of the tables used to analyze this parse tree. |
107 | | void ClearAnalyzedTableCache(QLEnv *ql_env) const; |
108 | | |
109 | | // Add type to the set of types used during semantic analysis. |
110 | | void AddAnalyzedUDType(const std::string& keyspace_name, const std::string& type_name); |
111 | | |
112 | | // Clear the metadata cache of the types used to analyze this parse tree. |
113 | | void ClearAnalyzedUDTypeCache(QLEnv *ql_env) const; |
114 | | |
115 | | private: |
116 | | // The SQL statement. |
117 | | const std::string& stmt_; |
118 | | |
119 | | // Has this statement been reparsed? |
120 | | mutable std::atomic<bool> reparsed_ = {false}; |
121 | | |
122 | | std::shared_ptr<BufferAllocator> buffer_allocator_; |
123 | | |
124 | | // Set of tables used during semantic analysis. |
125 | | std::unordered_set<client::YBTableName, boost::hash<client::YBTableName>> analyzed_tables_; |
126 | | |
127 | | // Set of types used during semantic analysis. |
128 | | std::unordered_set<std::pair<string, string>, |
129 | | boost::hash<std::pair<string, string>>> analyzed_types_; |
130 | | |
131 | | // Parse tree memory pool. This pool is used to allocate parse tree and its nodes. This pool |
132 | | // should be part of the generated parse tree that is stored within parse_context. Once the |
133 | | // parse tree is destructed, it's also gone. |
134 | | mutable Arena ptree_mem_; |
135 | | |
136 | | // Semantic analysis memory pool. This pool is used to allocate memory for storing semantic |
137 | | // analysis results in the parse tree. When a parse tree is analyzed, the parse tree is reset to |
138 | | // release the previous analysis result and this pool should be reset to free the associated |
139 | | // memory. This pool should be part of the generated parse tree also that is stored within |
140 | | // sem_context. Once the parse tree is destructed, it's also gone too. |
141 | | mutable Arena psem_mem_; |
142 | | |
143 | | // Root node of the parse tree. |
144 | | TreeNode::SharedPtr root_; |
145 | | |
146 | | // Is this parse tree stale? |
147 | | mutable std::atomic<bool> stale_ = {false}; |
148 | | |
149 | | // Was this generated internally? Used to to bypass authorization enforcement. |
150 | | bool internal_ = false; |
151 | | }; |
152 | | |
153 | | } // namespace ql |
154 | | } // namespace yb |
155 | | |
156 | | #endif // YB_YQL_CQL_QL_PTREE_PARSE_TREE_H_ |