YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/ptree/pt_create_table.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
// Tree node definitions for CREATE TABLE statement.
16
//--------------------------------------------------------------------------------------------------
17
18
#ifndef YB_YQL_CQL_QL_PTREE_PT_CREATE_TABLE_H_
19
#define YB_YQL_CQL_QL_PTREE_PT_CREATE_TABLE_H_
20
21
#include "yb/client/client_fwd.h"
22
23
#include "yb/common/common_fwd.h"
24
25
#include "yb/util/memory/arena.h"
26
27
#include "yb/yql/cql/ql/ptree/ptree_fwd.h"
28
#include "yb/yql/cql/ql/ptree/tree_node.h"
29
30
namespace yb {
31
namespace ql {
32
33
//--------------------------------------------------------------------------------------------------
34
// Constraints.
35
36
enum class PTConstraintType {
37
  kNone = 0,
38
  kPrimaryKey,
39
  kUnique,
40
  kNotNull,
41
};
42
43
class PTConstraint : public TreeNode {
44
 public:
45
  //------------------------------------------------------------------------------------------------
46
  // Public types.
47
  typedef MCSharedPtr<PTConstraint> SharedPtr;
48
  typedef MCSharedPtr<const PTConstraint> SharedPtrConst;
49
50
  //------------------------------------------------------------------------------------------------
51
  // Constructor and destructor.
52
  explicit PTConstraint(MemoryContext *memctx = nullptr, YBLocationPtr loc = nullptr)
53
1.92k
      : TreeNode(memctx, loc) {
54
1.92k
  }
55
1.83k
  virtual ~PTConstraint() {
56
1.83k
  }
57
58
  // Node type.
59
0
  virtual TreeNodeOpcode opcode() const override {
60
0
    return TreeNodeOpcode::kPTConstraint;
61
0
  }
62
63
  virtual PTConstraintType constraint_type() = 0;
64
};
65
66
class PTPrimaryKey : public PTConstraint {
67
 public:
68
  //------------------------------------------------------------------------------------------------
69
  // Public types.
70
  typedef MCSharedPtr<PTPrimaryKey> SharedPtr;
71
  typedef MCSharedPtr<const PTPrimaryKey> SharedPtrConst;
72
73
  //------------------------------------------------------------------------------------------------
74
  // Constructor and destructor.
75
  PTPrimaryKey(MemoryContext *memctx,
76
               YBLocationPtr loc,
77
               const PTListNodePtr& columns_ = nullptr);
78
  virtual ~PTPrimaryKey();
79
80
0
  virtual PTConstraintType constraint_type() override {
81
0
    return PTConstraintType::kPrimaryKey;
82
0
  }
83
84
  template<typename... TypeArgs>
85
1.92k
  inline static PTPrimaryKey::SharedPtr MakeShared(MemoryContext *memctx, TypeArgs&&... args) {
86
1.92k
    return MCMakeShared<PTPrimaryKey>(memctx, std::forward<TypeArgs>(args)...);
87
1.92k
  }
_ZN2yb2ql12PTPrimaryKey10MakeSharedIJNSt3__110shared_ptrINS0_8LocationEEEEEENS4_IS1_EEPNS_8internal9ArenaBaseINS8_11ArenaTraitsEEEDpOT_
Line
Count
Source
85
251
  inline static PTPrimaryKey::SharedPtr MakeShared(MemoryContext *memctx, TypeArgs&&... args) {
86
251
    return MCMakeShared<PTPrimaryKey>(memctx, std::forward<TypeArgs>(args)...);
87
251
  }
_ZN2yb2ql12PTPrimaryKey10MakeSharedIJNSt3__110shared_ptrINS0_8LocationEEERNS4_INS0_12TreeListNodeINS0_8TreeNodeEEEEEEEENS4_IS1_EEPNS_8internal9ArenaBaseINSD_11ArenaTraitsEEEDpOT_
Line
Count
Source
85
1.67k
  inline static PTPrimaryKey::SharedPtr MakeShared(MemoryContext *memctx, TypeArgs&&... args) {
86
1.67k
    return MCMakeShared<PTPrimaryKey>(memctx, std::forward<TypeArgs>(args)...);
87
1.67k
  }
88
89
  // Node semantics analysis.
90
  virtual CHECKED_STATUS Analyze(SemContext *sem_context) override;
91
92
  // Predicate whether this PTPrimary node is a column constraint or a table constraint.
93
  // - Besides the datatype, certain constraints can also be specified when defining a column in
94
  //   the table. Those constraints are column constraints. The following key is column constraint.
95
  //     CREATE TABLE t(i int primary key, j int);
96
  //
97
  // - When creating table, besides column definitions, other elements of the table can also be
98
  //   specified. Those elements are table constraints. The following key is table constraint.
99
  //     CREATE TABLE t(i int, j int, primary key(i));
100
0
  bool is_table_element() const {
101
0
    return columns_ != nullptr;
102
0
  }
103
104
3.52k
  bool is_column_element() const {
105
3.52k
    return columns_ == nullptr;
106
3.52k
  }
107
108
 private:
109
  PTListNodePtr columns_;
110
};
111
112
//--------------------------------------------------------------------------------------------------
113
// Static column qualifier.
114
115
class PTStatic : public TreeNode {
116
 public:
117
  //------------------------------------------------------------------------------------------------
118
  // Public types.
119
  typedef MCSharedPtr<PTStatic> SharedPtr;
120
  typedef MCSharedPtr<const PTStatic> SharedPtrConst;
121
122
  //------------------------------------------------------------------------------------------------
123
  // Constructor and destructor.
124
  explicit PTStatic(MemoryContext *memctx = nullptr, YBLocationPtr loc = nullptr)
125
46
      : TreeNode(memctx, loc) {
126
46
  }
127
46
  virtual ~PTStatic() {
128
46
  }
129
130
  // Node type.
131
0
  virtual TreeNodeOpcode opcode() const override {
132
0
    return TreeNodeOpcode::kPTStatic;
133
0
  }
134
135
  template<typename... TypeArgs>
136
46
  inline static PTStatic::SharedPtr MakeShared(MemoryContext *memctx, TypeArgs&&... args) {
137
46
    return MCMakeShared<PTStatic>(memctx, std::forward<TypeArgs>(args)...);
138
46
  }
139
140
  // Node semantics analysis.
141
  virtual CHECKED_STATUS Analyze(SemContext *sem_context) override;
142
};
143
144
//--------------------------------------------------------------------------------------------------
145
// CREATE TABLE statement.
146
147
class PTCreateTable : public TreeNode {
148
 public:
149
  //------------------------------------------------------------------------------------------------
150
  // Public types.
151
  typedef MCSharedPtr<PTCreateTable> SharedPtr;
152
  typedef MCSharedPtr<const PTCreateTable> SharedPtrConst;
153
154
  //------------------------------------------------------------------------------------------------
155
  // Constructor and destructor.
156
  PTCreateTable(MemoryContext *memctx,
157
                YBLocationPtr loc,
158
                const PTQualifiedNamePtr& name,
159
                const PTListNodePtr& elements,
160
                bool create_if_not_exists,
161
                const PTTablePropertyListNodePtr& table_properties);
162
  virtual ~PTCreateTable();
163
164
  // Node type.
165
12.4k
  virtual TreeNodeOpcode opcode() const override {
166
12.4k
    return TreeNodeOpcode::kPTCreateTable;
167
12.4k
  }
168
169
  // Support for shared_ptr.
170
  template<typename... TypeArgs>
171
1.40k
  inline static PTCreateTable::SharedPtr MakeShared(MemoryContext *memctx, TypeArgs&&... args) {
172
1.40k
    return MCMakeShared<PTCreateTable>(memctx, std::forward<TypeArgs>(args)...);
173
1.40k
  }
174
175
  // Node semantics analysis.
176
  virtual CHECKED_STATUS Analyze(SemContext *sem_context) override;
177
  void PrintSemanticAnalysisResult(SemContext *sem_context);
178
179
  // column lists.
180
1.59k
  const MCList<PTColumnDefinition *>& columns() const {
181
1.59k
    return columns_;
182
1.59k
  }
183
184
4.61k
  const MCList<PTColumnDefinition *>& primary_columns() const {
185
4.61k
    return primary_columns_;
186
4.61k
  }
187
188
3.91k
  const MCList<PTColumnDefinition *>& hash_columns() const {
189
3.91k
    return hash_columns_;
190
3.91k
  }
191
192
8
  bool create_if_not_exists() const {
193
8
    return create_if_not_exists_;
194
8
  }
195
196
  CHECKED_STATUS AppendColumn(SemContext *sem_context,
197
                              PTColumnDefinition *column,
198
                              bool check_duplicate = false);
199
200
  CHECKED_STATUS AppendPrimaryColumn(SemContext *sem_context,
201
                                     PTColumnDefinition *column,
202
                                     bool check_duplicate = false);
203
204
  CHECKED_STATUS AppendHashColumn(SemContext *sem_context,
205
                                  PTColumnDefinition *column,
206
                                  bool check_duplicate = false);
207
208
  virtual CHECKED_STATUS CheckPrimaryType(SemContext *sem_context,
209
                                          const PTColumnDefinition *column) const;
210
211
  // Table name.
212
245
  const PTQualifiedNamePtr& table_name() const {
213
245
    return relation_;
214
245
  }
215
216
  virtual client::YBTableName yb_table_name() const;
217
218
0
  PTTablePropertyListNodePtr table_properties() const {
219
0
    return table_properties_;
220
0
  }
221
222
  virtual CHECKED_STATUS ToTableProperties(TableProperties *table_properties) const;
223
224
  static bool ColumnExists(const MCList<PTColumnDefinition *>& columns,
225
                           const PTColumnDefinition* column);
226
227
 protected:
228
  PTQualifiedNamePtr relation_;
229
  PTListNodePtr elements_;
230
231
  MCList<PTColumnDefinition *> columns_;
232
  MCList<PTColumnDefinition *> primary_columns_;
233
  MCList<PTColumnDefinition *> hash_columns_;
234
235
  bool create_if_not_exists_;
236
  bool contain_counters_;
237
  const PTTablePropertyListNodePtr table_properties_;
238
};
239
240
}  // namespace ql
241
}  // namespace yb
242
243
#endif  // YB_YQL_CQL_QL_PTREE_PT_CREATE_TABLE_H_