YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/ptree/pt_column_definition.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
// Column Definition Tree node definition.
16
//--------------------------------------------------------------------------------------------------
17
18
#ifndef YB_YQL_CQL_QL_PTREE_PT_COLUMN_DEFINITION_H
19
#define YB_YQL_CQL_QL_PTREE_PT_COLUMN_DEFINITION_H
20
21
#include "yb/yql/cql/ql/ptree/list_node.h"
22
#include "yb/yql/cql/ql/ptree/tree_node.h"
23
#include "yb/yql/cql/ql/ptree/pt_type.h"
24
25
namespace yb {
26
namespace ql {
27
28
//--------------------------------------------------------------------------------------------------
29
// Table column.
30
// Usage:
31
//   CREATE TABLE (column_definitions)
32
class PTColumnDefinition : public TreeNode {
33
 public:
34
  //------------------------------------------------------------------------------------------------
35
  // Public types.
36
  typedef MCSharedPtr<PTColumnDefinition> SharedPtr;
37
  typedef MCSharedPtr<const PTColumnDefinition> SharedPtrConst;
38
39
  //------------------------------------------------------------------------------------------------
40
  // Constructor and destructor.
41
  PTColumnDefinition(MemoryContext *memctx,
42
                     YBLocationPtr loc,
43
                     const MCSharedPtr<MCString>& name,
44
                     const PTBaseType::SharedPtr& datatype,
45
                     const PTListNode::SharedPtr& qualifiers);
46
47
  virtual ~PTColumnDefinition();
48
49
  template<typename... TypeArgs>
50
  inline static PTColumnDefinition::SharedPtr MakeShared(MemoryContext *memctx,
51
5.37k
                                                         TypeArgs&&... args) {
52
5.37k
    return MCMakeShared<PTColumnDefinition>(memctx, std::forward<TypeArgs>(args)...);
53
5.37k
  }
54
55
  // Node semantics analysis.
56
  virtual CHECKED_STATUS Analyze(SemContext *sem_context) override;
57
58
  // Node type.
59
3.58k
  virtual TreeNodeOpcode opcode() const override {
60
3.58k
    return TreeNodeOpcode::kPTColumnDefinition;
61
3.58k
  }
62
63
  // Access function for is_primary_key_.
64
6.95k
  bool is_primary_key() const {
65
6.95k
    return is_primary_key_;
66
6.95k
  }
67
2.99k
  void set_is_primary_key() {
68
2.99k
    is_primary_key_ = true;
69
2.99k
  }
70
71
  // Access function for is_hash_key_.
72
5.56k
  bool is_hash_key() const {
73
5.56k
    return is_hash_key_;
74
5.56k
  }
75
2.31k
  void set_is_hash_key() {
76
2.31k
    is_primary_key_ = true;
77
2.31k
    is_hash_key_ = true;
78
2.31k
  }
79
80
  // Access function for is_static_.
81
6.31k
  bool is_static() const {
82
6.31k
    return is_static_;
83
6.31k
  }
84
46
  void set_is_static() {
85
46
    is_static_ = true;
86
46
  }
87
88
  // Access function for order_.
89
6.18k
  int32_t order() const {
90
6.18k
    return order_;
91
6.18k
  }
92
5.23k
  void set_order(int32 order) {
93
5.23k
    order_ = order;
94
5.23k
  }
95
96
6.18k
  SortingType sorting_type() {
97
6.18k
    return sorting_type_;
98
6.18k
  }
99
100
328
  void set_sorting_type(SortingType sorting_type) {
101
328
    sorting_type_ = sorting_type;
102
328
  }
103
104
979
  const MCSharedPtr<MCString>& name() const {
105
979
    return name_;
106
979
  }
107
108
  // "coldef_name" is the metdata for the name of a column.
109
  // - For UserTable and OLD IndexTable, this name is identical with user-given text.
110
  // - For NEW IndexTable, this name is a mangled text of the user-given text.
111
  //   . Name of catalog::IndexTable::ExprColumn is generated by mangling original names.
112
  //   . Each column in an INDEX is defined as an expression (ColumnRef, JsonRef, ...).
113
7.92k
  const MCString& coldef_name() const {
114
7.92k
    return coldef_name_;
115
7.92k
  }
116
117
  // Column name that users created.
118
  // We used this name in catalog::Table::Column and catalog::Table::IndexInfo.
119
700
  const char *yb_name() const {
120
700
    return name_->c_str();
121
700
  }
122
123
0
  const PTBaseType::SharedPtr& datatype() const {
124
0
    return datatype_;
125
0
  }
126
127
7.81k
  virtual std::shared_ptr<QLType> ql_type() const {
128
7.81k
    return datatype_->ql_type();
129
7.81k
  }
130
131
9.56k
  virtual bool is_counter() const {
132
9.56k
    return datatype_->is_counter();
133
9.56k
  }
134
135
0
  virtual PTExprPtr colexpr() const {
136
0
    return nullptr;
137
0
  }
138
139
1.73k
  virtual int32_t indexed_ref() const {
140
1.73k
    return indexed_ref_;
141
1.73k
  }
142
143
0
  virtual void set_indexed_ref(int32_t col_id) {
144
0
    indexed_ref_ = col_id;
145
0
  }
146
147
  void AddIndexedRef(int32_t id);
148
149
 protected:
150
  MCSharedPtr<MCString> name_;
151
  PTBaseType::SharedPtr datatype_;
152
  PTListNode::SharedPtr qualifiers_;
153
  bool is_primary_key_;
154
  bool is_hash_key_;
155
  bool is_static_;
156
  int32_t order_;
157
  // Sorting order. Only relevant when this key is a primary key.
158
  SortingType sorting_type_;
159
160
  // Ref column_id.
161
  // - In a TABLE, indexed_ref_ of a column is "-1" as it doesn't reference any column.
162
  // - In an INDEX, this column is referencing to a column in data-table. Our current index design
163
  //   only allow referencing ONE column.
164
  //
165
  // Example for scalar index
166
  //   TABLE (a, b, c)
167
  //   INDEX (c) -> INDEX is a table whose column 'c' is referencing TABLE(c)
168
  // Example for JSON index
169
  //   TABLE (a, b, j)
170
  //   INDEX (j->>'b') -> INDEX is a table whose column 'j->>b' is referencing to TABLE(j)
171
  int32 indexed_ref_ = -1;
172
173
  // Mangled name.
174
  MCString coldef_name_;
175
};
176
177
// IndexColumn - Name of an expression that is used for indexing.
178
// Usage:
179
//   PRIMARY KEY (index_columns)
180
//   INDEX ON tab (index_columns) include (index_columns)
181
class PTIndexColumn : public PTColumnDefinition {
182
 public:
183
  //------------------------------------------------------------------------------------------------
184
  // Public types.
185
  typedef MCSharedPtr<PTIndexColumn> SharedPtr;
186
  typedef MCSharedPtr<const PTIndexColumn> SharedPtrConst;
187
188
  //------------------------------------------------------------------------------------------------
189
  // Constructor and destructor.
190
  explicit PTIndexColumn(MemoryContext *memctx,
191
                         YBLocationPtr loc,
192
                         const MCSharedPtr<MCString>& name,
193
                         const PTExprPtr& expr);
194
  virtual ~PTIndexColumn();
195
196
  template<typename... TypeArgs>
197
4.72k
  inline static PTIndexColumn::SharedPtr MakeShared(MemoryContext *memctx, TypeArgs&&... args) {
198
4.72k
    return MCMakeShared<PTIndexColumn>(memctx, std::forward<TypeArgs>(args)...);
199
4.72k
  }
std::__1::shared_ptr<yb::ql::PTIndexColumn> yb::ql::PTIndexColumn::MakeShared<std::__1::shared_ptr<yb::ql::YBLocation> const&, std::__1::shared_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, yb::internal::ArenaAllocatorBase<char, yb::internal::ArenaTraits> > >&, std::__1::shared_ptr<yb::ql::PTRef>&>(yb::internal::ArenaBase<yb::internal::ArenaTraits>*, std::__1::shared_ptr<yb::ql::YBLocation> const&, std::__1::shared_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, yb::internal::ArenaAllocatorBase<char, yb::internal::ArenaTraits> > >&, std::__1::shared_ptr<yb::ql::PTRef>&)
Line
Count
Source
197
1.07k
  inline static PTIndexColumn::SharedPtr MakeShared(MemoryContext *memctx, TypeArgs&&... args) {
198
1.07k
    return MCMakeShared<PTIndexColumn>(memctx, std::forward<TypeArgs>(args)...);
199
1.07k
  }
std::__1::shared_ptr<yb::ql::PTIndexColumn> yb::ql::PTIndexColumn::MakeShared<std::__1::shared_ptr<yb::ql::Location>, std::__1::shared_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, yb::internal::ArenaAllocatorBase<char, yb::internal::ArenaTraits> > > const&, std::__1::shared_ptr<yb::ql::PTRef>&>(yb::internal::ArenaBase<yb::internal::ArenaTraits>*, std::__1::shared_ptr<yb::ql::Location>&&, std::__1::shared_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, yb::internal::ArenaAllocatorBase<char, yb::internal::ArenaTraits> > > const&, std::__1::shared_ptr<yb::ql::PTRef>&)
Line
Count
Source
197
3.61k
  inline static PTIndexColumn::SharedPtr MakeShared(MemoryContext *memctx, TypeArgs&&... args) {
198
3.61k
    return MCMakeShared<PTIndexColumn>(memctx, std::forward<TypeArgs>(args)...);
199
3.61k
  }
std::__1::shared_ptr<yb::ql::PTIndexColumn> yb::ql::PTIndexColumn::MakeShared<std::__1::shared_ptr<yb::ql::Location>, std::__1::shared_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, yb::internal::ArenaAllocatorBase<char, yb::internal::ArenaTraits> > >, std::__1::shared_ptr<yb::ql::PTExpr>&>(yb::internal::ArenaBase<yb::internal::ArenaTraits>*, std::__1::shared_ptr<yb::ql::Location>&&, std::__1::shared_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, yb::internal::ArenaAllocatorBase<char, yb::internal::ArenaTraits> > >&&, std::__1::shared_ptr<yb::ql::PTExpr>&)
Line
Count
Source
197
35
  inline static PTIndexColumn::SharedPtr MakeShared(MemoryContext *memctx, TypeArgs&&... args) {
198
35
    return MCMakeShared<PTIndexColumn>(memctx, std::forward<TypeArgs>(args)...);
199
35
  }
200
201
  virtual CHECKED_STATUS Analyze(SemContext *sem_context) override;
202
203
  CHECKED_STATUS SetupPrimaryKey(SemContext *sem_context);
204
  CHECKED_STATUS SetupHashKey(SemContext *sem_context);
205
  CHECKED_STATUS SetupCoveringIndexColumn(SemContext *sem_context);
206
207
  std::shared_ptr<QLType> ql_type() const override;
208
209
718
  virtual bool is_counter() const override {
210
718
    return false;
211
718
  }
212
213
1.96k
  PTExprPtr colexpr() const override {
214
1.96k
    return colexpr_;
215
1.96k
  }
216
217
 private:
218
  // Indexing column is computed based on the value of an expression.
219
  // Example:
220
  //   - TABLE tab(a, j)
221
  //   - INDEX (j->>'x') ==> colexpr_ = j->'x'
222
  //   - When user insert to TABLE tab, YugaByte inserts to INDEX (j->'x', ybctid)
223
  //   - DEFAULT: colexpr == EXPR_NOT_YET
224
  PTExprPtr colexpr_;
225
226
  // "coldef_" is pointer to the definition of an index column.
227
  // - If the column of the same name is previously-defined, we use that definition.
228
  // - Otherwise, we use this node as the column definition.
229
  PTColumnDefinition *coldef_ = nullptr;
230
};
231
232
}  // namespace ql
233
}  // namespace yb
234
235
#endif  // YB_YQL_CQL_QL_PTREE_PT_COLUMN_DEFINITION_H