YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/common/ql_type.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
// This module is to define a few supporting functions for QLTYPE.
16
//--------------------------------------------------------------------------------------------------
17
18
#ifndef YB_COMMON_QL_TYPE_H_
19
#define YB_COMMON_QL_TYPE_H_
20
21
#include <boost/optional.hpp>
22
23
#include "yb/common/common_fwd.h"
24
#include "yb/common/value.pb.h"
25
#include "yb/util/status_fwd.h"
26
27
namespace yb {
28
29
// Class for storing the additional fields of user-defined types (compared to primitive QL Types)
30
// Used internally in QLType and only set for user-defined types.
31
class UDTypeInfo {
32
 public:
33
  UDTypeInfo(std::string keyspace_name, std::string name)
34
5.47k
      : keyspace_name_(keyspace_name), name_(name) {
35
5.47k
  }
36
37
10.8k
  const std::string& keyspace_name() const {
38
10.8k
    return keyspace_name_;
39
10.8k
  }
40
41
11.3k
  const std::string& name() const {
42
11.3k
    return name_;
43
11.3k
  }
44
45
10.7k
  const std::string& id() const {
46
10.7k
    return id_;
47
10.7k
  }
48
49
11.5k
  const std::vector<std::string>& field_names() const {
50
11.5k
    return field_names_;
51
11.5k
  }
52
53
352
  const std::string& field_name(size_t index) const {
54
352
    return field_names_[index];
55
352
  }
56
57
  void set_udt_fields(const std::string& type_id,
58
5.43k
                      const std::vector<std::string>& field_names) {
59
5.43k
    id_ = type_id;
60
5.43k
    field_names_ = field_names;
61
5.43k
  }
62
63
 private:
64
  std::string keyspace_name_;
65
  std::string name_;
66
  std::string id_;
67
  std::vector<std::string> field_names_ = {};
68
};
69
70
class QLType {
71
 public:
72
  typedef std::shared_ptr<QLType> SharedPtr;
73
74
  //------------------------------------------------------------------------------------------------
75
  // The Create() functions are to construct QLType objects.
76
  template<DataType data_type>
77
62.2M
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
62.2M
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
62.2M
    return ql_type;
80
62.2M
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE999EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
683k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
683k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
683k
    return ql_type;
80
683k
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE0EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
158k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
158k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
158k
    return ql_type;
80
158k
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE1EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
455k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
455k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
455k
    return ql_type;
80
455k
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE2EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
277k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
277k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
277k
    return ql_type;
80
277k
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE3EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
14.6M
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
14.6M
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
14.6M
    return ql_type;
80
14.6M
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE4EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
667k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
667k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
667k
    return ql_type;
80
667k
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE5EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
24.2M
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
24.2M
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
24.2M
    return ql_type;
80
24.2M
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE6EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
1.60M
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
1.60M
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
1.60M
    return ql_type;
80
1.60M
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE7EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
52.7k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
52.7k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
52.7k
    return ql_type;
80
52.7k
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE8EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
1.04M
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
1.04M
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
1.04M
    return ql_type;
80
1.04M
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE9EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
6.04M
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
6.04M
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
6.04M
    return ql_type;
80
6.04M
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE10EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
7.24M
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
7.24M
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
7.24M
    return ql_type;
80
7.24M
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE11EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
14.5k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
14.5k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
14.5k
    return ql_type;
80
14.5k
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE12EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
159k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
159k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
159k
    return ql_type;
80
159k
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE13EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
619k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
619k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
619k
    return ql_type;
80
619k
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE25EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
11.8k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
11.8k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
11.8k
    return ql_type;
80
11.8k
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE17EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
825k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
825k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
825k
    return ql_type;
80
825k
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE18EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
1.22k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
1.22k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
1.22k
    return ql_type;
80
1.22k
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE23EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
1.36k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
1.36k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
1.36k
    return ql_type;
80
1.36k
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE24EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
1.28k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
1.28k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
1.28k
    return ql_type;
80
1.28k
  }
Unexecuted instantiation: _ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE100EEERKNSt3__110shared_ptrIS0_EEv
Unexecuted instantiation: _ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE101EEERKNSt3__110shared_ptrIS0_EEv
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE102EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
3.46M
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
3.46M
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
3.46M
    return ql_type;
80
3.46M
  }
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE103EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
5.01k
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
5.01k
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
5.01k
    return ql_type;
80
5.01k
  }
Unexecuted instantiation: _ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE20EEERKNSt3__110shared_ptrIS0_EEv
_ZN2yb6QLType19CreatePrimitiveTypeILNS_8DataTypeE104EEERKNSt3__110shared_ptrIS0_EEv
Line
Count
Source
77
117
  static const std::shared_ptr<QLType>& CreatePrimitiveType() {
78
117
    static std::shared_ptr<QLType> ql_type = std::make_shared<QLType>(data_type);
79
117
    return ql_type;
80
117
  }
81
82
  template<DataType data_type>
83
  static std::shared_ptr<QLType> CreateCollectionType(
84
1.62M
      const std::vector<std::shared_ptr<QLType>>& params) {
85
1.62M
    return std::make_shared<QLType>(data_type, params);
86
1.62M
  }
_ZN2yb6QLType20CreateCollectionTypeILNS_8DataTypeE14EEENSt3__110shared_ptrIS0_EERKNS3_6vectorIS5_NS3_9allocatorIS5_EEEE
Line
Count
Source
84
387k
      const std::vector<std::shared_ptr<QLType>>& params) {
85
387k
    return std::make_shared<QLType>(data_type, params);
86
387k
  }
_ZN2yb6QLType20CreateCollectionTypeILNS_8DataTypeE15EEENSt3__110shared_ptrIS0_EERKNS3_6vectorIS5_NS3_9allocatorIS5_EEEE
Line
Count
Source
84
983k
      const std::vector<std::shared_ptr<QLType>>& params) {
85
983k
    return std::make_shared<QLType>(data_type, params);
86
983k
  }
_ZN2yb6QLType20CreateCollectionTypeILNS_8DataTypeE16EEENSt3__110shared_ptrIS0_EERKNS3_6vectorIS5_NS3_9allocatorIS5_EEEE
Line
Count
Source
84
245k
      const std::vector<std::shared_ptr<QLType>>& params) {
85
245k
    return std::make_shared<QLType>(data_type, params);
86
245k
  }
_ZN2yb6QLType20CreateCollectionTypeILNS_8DataTypeE22EEENSt3__110shared_ptrIS0_EERKNS3_6vectorIS5_NS3_9allocatorIS5_EEEE
Line
Count
Source
84
8.04k
      const std::vector<std::shared_ptr<QLType>>& params) {
85
8.04k
    return std::make_shared<QLType>(data_type, params);
86
8.04k
  }
_ZN2yb6QLType20CreateCollectionTypeILNS_8DataTypeE19EEENSt3__110shared_ptrIS0_EERKNS3_6vectorIS5_NS3_9allocatorIS5_EEEE
Line
Count
Source
84
361
      const std::vector<std::shared_ptr<QLType>>& params) {
85
361
    return std::make_shared<QLType>(data_type, params);
86
361
  }
87
88
  // Create all builtin types including collection.
89
  static std::shared_ptr<QLType> Create(DataType data_type,
90
                                        const std::vector<std::shared_ptr<QLType>>& params);
91
92
  // Create primitive types, all builtin types except collection.
93
  static std::shared_ptr<QLType> Create(DataType data_type);
94
95
  // Check type methods.
96
  static bool IsValidPrimaryType(DataType type);
97
98
  // Create map datatype.
99
  static std::shared_ptr<QLType> CreateTypeMap(std::shared_ptr<QLType> key_type,
100
                                               std::shared_ptr<QLType> value_type);
101
  static std::shared_ptr<QLType> CreateTypeMap(DataType key_type, DataType value_type);
102
479
  static std::shared_ptr<QLType> CreateTypeMap() {
103
    // Create default map type: MAP <UNKNOWN -> UNKNOWN>.
104
479
    static const std::shared_ptr<QLType> default_map =
105
479
        CreateTypeMap(QLType::Create(DataType::UNKNOWN_DATA),
106
479
                      QLType::Create(DataType::UNKNOWN_DATA));
107
479
    return default_map;
108
479
  }
109
110
  // Create list datatype.
111
  static std::shared_ptr<QLType> CreateTypeList(std::shared_ptr<QLType> value_type);
112
  static std::shared_ptr<QLType> CreateTypeList(DataType val_type);
113
281
  static std::shared_ptr<QLType> CreateTypeList() {
114
    // Create default list type: LIST <UNKNOWN>.
115
281
    static const std::shared_ptr<QLType> default_list = CreateTypeList(DataType::UNKNOWN_DATA);
116
281
    return default_list;
117
281
  }
118
119
  // Create set datatype.
120
  static std::shared_ptr<QLType> CreateTypeSet(std::shared_ptr<QLType> value_type);
121
  static std::shared_ptr<QLType> CreateTypeSet(DataType value_type);
122
293
  static std::shared_ptr<QLType> CreateTypeSet() {
123
    // Create default set type: SET <UNKNOWN>.
124
293
    static const std::shared_ptr<QLType> default_set = CreateTypeSet(DataType::UNKNOWN_DATA);
125
293
    return default_set;
126
293
  }
127
128
  // Create frozen datatype
129
  static std::shared_ptr<QLType> CreateTypeFrozen(std::shared_ptr<QLType> value_type);
130
0
  static std::shared_ptr<QLType> CreateTypeFrozen() {
131
0
  // Create default frozen type: FROZEN <UNKNOWN>.
132
0
    static const std::shared_ptr<QLType> default_frozen =
133
0
        CreateTypeFrozen(QLType::Create(DataType::UNKNOWN_DATA));
134
0
    return default_frozen;
135
0
  }
136
137
  //------------------------------------------------------------------------------------------------
138
  // Constructors.
139
140
  // Constructor for elementary types
141
89.5k
  explicit QLType(DataType ql_typeid) : id_(ql_typeid), params_(0) {
142
89.5k
  }
143
144
  // Constructor for collection types
145
  QLType(DataType ql_typeid, const std::vector<std::shared_ptr<QLType>>& params)
146
1.62M
      : id_(ql_typeid), params_(params) {
147
1.62M
  }
148
149
  // Constructor for user-defined types
150
  QLType(const std::string& keyspace_name, const std::string& type_name)
151
5.45k
      : id_(USER_DEFINED_TYPE), params_(0) {
152
5.45k
    udtype_info_ = std::make_shared<UDTypeInfo>(keyspace_name, type_name);
153
5.45k
  }
154
155
1.49M
  virtual ~QLType() {
156
1.49M
  }
157
158
  //------------------------------------------------------------------------------------------------
159
  // Protobuf support.
160
161
  void ToQLTypePB(QLTypePB *pb_type) const;
162
  static std::shared_ptr<QLType> FromQLTypePB(const QLTypePB& pb_type);
163
164
  //------------------------------------------------------------------------------------------------
165
  // Access functions.
166
167
553M
  DataType main() const {
168
553M
    return id_;
169
553M
  }
170
171
1.15M
  const std::vector<std::shared_ptr<QLType>>& params() const {
172
1.15M
    return params_;
173
1.15M
  }
174
175
  std::shared_ptr<QLType> keys_type() const;
176
177
  std::shared_ptr<QLType> values_type() const;
178
179
  const QLType::SharedPtr& param_type(size_t member_index = 0) const;
180
181
  const TypeInfo* type_info() const;
182
183
  //------------------------------------------------------------------------------------------------
184
  // Methods for User-Defined types.
185
186
0
  const std::shared_ptr<UDTypeInfo>& udtype_info() const {
187
0
    return udtype_info_;
188
0
  }
189
190
11.5k
  const std::vector<std::string>& udtype_field_names() const {
191
11.5k
    return udtype_info_->field_names();
192
11.5k
  }
193
194
352
  const std::string& udtype_field_name(size_t index) const {
195
352
    return udtype_info_->field_name(index);
196
352
  }
197
198
10.8k
  const std::string& udtype_keyspace_name() const {
199
10.8k
    return udtype_info_->keyspace_name();
200
10.8k
  }
201
202
11.3k
  const std::string& udtype_name() const {
203
11.3k
    return udtype_info_->name();
204
11.3k
  }
205
206
10.7k
  const std::string& udtype_id() const {
207
10.7k
    return udtype_info_->id();
208
10.7k
  }
209
210
  void SetUDTypeFields(const std::string &type_id,
211
                       const std::vector<std::string> &field_names,
212
5.44k
                       const std::vector<std::shared_ptr<QLType>> &field_types) {
213
5.44k
    udtype_info_->set_udt_fields(type_id, field_names);
214
5.44k
    params_ = field_types;
215
5.44k
  }
216
217
  // returns position of "field_name" in udtype_field_names() vector if found, otherwise -1
218
  boost::optional<size_t> GetUDTypeFieldIdxByName(const std::string &field_name) const;
219
220
  // Get the type ids of all UDTs (transitively) referenced by this UDT.
221
11.7k
  std::vector<std::string> GetUserDefinedTypeIds() const {
222
11.7k
    std::vector<std::string> udt_ids;
223
11.7k
    GetUserDefinedTypeIds(&udt_ids);
224
11.7k
    return udt_ids;
225
11.7k
  }
226
227
12.4k
  void GetUserDefinedTypeIds(std::vector<std::string>* udt_ids) const {
228
12.4k
    if (IsUserDefined()) {
229
87
      udt_ids->push_back(udtype_info_->id());
230
87
    }
231
737
    for (auto& param : params_) {
232
737
      param->GetUserDefinedTypeIds(udt_ids);
233
737
    }
234
12.4k
  }
235
236
  // Check whether the type id exists among type ids of all UDTs referenced by this UDT.
237
  static bool DoesUserDefinedTypeIdExist(const QLTypePB& type_pb,
238
                                         const bool transitive,
239
                                         const std::string& udt_id);
240
241
  // Get the type ids of all UDTs referenced by this UDT.
242
  static void GetUserDefinedTypeIds(const QLTypePB& type_pb,
243
                                    const bool transitive,
244
                                    std::vector<std::string>* udt_ids);
245
246
  // Returns the type of given field, or nullptr if that field is not found in this UDT.R
247
  Result<QLType::SharedPtr> GetUDTFieldTypeByName(const std::string& field_name) const;
248
249
  //------------------------------------------------------------------------------------------------
250
  // Predicates.
251
252
203M
  bool IsCollection() const {
253
203M
    return id_ == MAP || id_ == SET || id_ == LIST || id_ == TUPLE;
254
203M
  }
255
256
1.18G
  bool IsUserDefined() const {
257
1.18G
    return id_ == USER_DEFINED_TYPE;
258
1.18G
  }
259
260
936k
  bool IsFrozen() const {
261
936k
    return id_ == FROZEN;
262
936k
  }
263
264
260
  bool IsParametric() const {
265
260
    return IsCollection() || IsUserDefined() || IsFrozen();
266
260
  }
267
268
0
  bool IsElementary() const {
269
0
    return !IsParametric();
270
0
  }
271
272
  // Collections and UDT values are stored as complex objects internally, unlike Elementary and
273
  // Frozen types which are stored as single values.
274
203M
  bool HasComplexValues() const {
275
203M
    return IsCollection() || IsUserDefined();
276
203M
  }
277
278
1.48M
  bool IsUnknown() const {
279
1.48M
    return IsUnknown(id_);
280
1.48M
  }
281
282
71
  bool IsAnyType() const {
283
71
    return IsNull(id_);
284
71
  }
285
286
0
  bool IsInteger() const {
287
0
    return IsInteger(id_);
288
0
  }
289
290
1.62k
  bool IsJson() const {
291
1.62k
    return IsJson(id_);
292
1.62k
  }
293
294
0
  bool IsNumeric() const {
295
0
    return IsNumeric(id_);
296
0
  }
297
298
0
  bool IsValid() const {
299
0
    if (IsElementary()) {
300
0
      return params_.empty();
301
0
    } else {
302
0
      // checking number of params
303
0
      if (id_ == MAP && params_.size() != 2) {
304
0
        return false; // expect two type parameters for maps
305
0
      } else if ((id_ == SET || id_ == LIST) && params_.size() != 1) {
306
0
        return false; // expect one type parameter for set and list
307
0
      } else if (id_ == TUPLE && params_.size() == 0) {
308
0
        return false; // expect at least one type parameters for tuples
309
0
      } else if (id_ == FROZEN && params_.size() != 1) {
310
0
        return false; // expect one type parameter for frozen
311
0
      }
312
0
      // recursively checking params
313
0
      for (const auto &param : params_) {
314
0
        if (!param->IsValid()) return false;
315
0
      }
316
0
      return true;
317
0
    }
318
0
  }
319
320
150
  bool Contains(DataType id) const {
321
63
    for (const std::shared_ptr<QLType>& param : params_) {
322
63
      if (param->Contains(id)) {
323
11
        return true;
324
11
      }
325
63
    }
326
139
    return id_ == id;
327
150
  }
328
329
90
  bool operator ==(const QLType& other) const {
330
90
    if (IsUserDefined()) {
331
10
      return other.IsUserDefined() && udtype_id() == other.udtype_id();
332
10
    }
333
80
    if (id_ == other.id_ && params_.size() == other.params_.size()) {
334
139
      for (size_t i = 0; i < params_.size(); i++) {
335
59
        if (*params_[i] == *other.params_[i]) {
336
59
          continue;
337
59
        }
338
0
        return false;
339
0
      }
340
80
      return true;
341
0
    }
342
343
0
    return false;
344
0
  }
345
346
31
  bool operator !=(const QLType& other) const {
347
31
    return !(*this == other);
348
31
  }
349
350
  //------------------------------------------------------------------------------------------------
351
  // Logging supports.
352
  std::string ToString() const;
353
  void ToString(std::stringstream& os) const;
354
  static const std::string ToCQLString(const DataType& datatype);
355
356
  //------------------------------------------------------------------------------------------------
357
  // static methods
358
  static const int kMaxTypeIndex = DataType::JSONB + 1;
359
360
  // When a new type is added in the enum "DataType", kMaxTypeIndex should be updated for this
361
  // module to work properly. The DCHECKs in this struct would failed if kMaxTypeIndex is wrong.
362
1.88M
  static bool IsValid(DataType type) {
363
1.89M
    return (type >= 0 && type < kMaxTypeIndex);
364
1.88M
  }
365
366
5.30k
  static bool IsInteger(DataType t) {
367
5.30k
    return (t >= INT8 && t <= INT64) || t == VARINT;
368
5.30k
  }
369
370
1.62k
  static bool IsJson(DataType t) {
371
1.62k
    return t == JSONB;
372
1.62k
  }
373
374
5.13k
  static bool IsNumeric(DataType t) {
375
5.13k
    return IsInteger(t) || t == FLOAT || t == DOUBLE || t == DECIMAL;
376
5.13k
  }
377
378
  // NULL_VALUE_TYPE represents type of a null value.
379
445k
  static bool IsNull(DataType t) {
380
445k
    return t == NULL_VALUE_TYPE;
381
445k
  }
382
383
  // Type is not yet set (VOID).
384
1.49M
  static bool IsUnknown(DataType t) {
385
1.49M
    return t == DataType::UNKNOWN_DATA;
386
1.49M
  }
387
388
  // There are a few compatibility modes between different datatypes. We use these modes when it is
389
  // necessary to convert a value from one type to another.
390
  // * kIdentical: The same type (INT8 === INT8).
391
  // * kSimilar: These types share the same logical representation even though they might be
392
  //   represented or implemented differently.
393
  //   - INT8, INT16, INT32, INT64, and VARINT are similar.
394
  //   - DOUBLE and FLOAT are similar.
395
  // * kImplicit: Values can be converted automatically between two different datatypes.
396
  //   - All integer types are convertible to DOUBLE and FLOAT.
397
  // * kExplicit: An explicit CAST must be used to trigger the conversion from one type to another.
398
  //   - DOUBLE and FLOAT are not automatically convertible to integer types.
399
  //   - Once we support "cast" operator, DOUBLE & FLOAT can be explicitly casted to int types.
400
  // * kNotAllowed: No conversion is allowed between two different datatypes.
401
  enum class ConversionMode : int {
402
    kIdentical = 0,
403
    kSimilar = 1,
404
    kImplicit = 2,
405
    kFurtherCheck = 3,
406
    kExplicit = 4,
407
    kNotAllowed = 5,
408
  };
409
410
  static ConversionMode GetConversionMode(DataType left, DataType right);
411
412
0
  static bool IsIdentical(DataType left, DataType right) {
413
0
    return GetConversionMode(left, right) == ConversionMode::kIdentical;
414
0
  }
415
416
  static bool IsSimilar(DataType left, DataType right);
417
418
222
  static bool IsImplicitlyConvertible(DataType left, DataType right) {
419
222
    return GetConversionMode(left, right) <= ConversionMode::kImplicit;
420
222
  }
421
422
  static bool IsPotentiallyConvertible(DataType left, DataType right);
423
424
112
  static bool IsExplicitlyConvertible(DataType left, DataType right) {
425
112
    return GetConversionMode(left, right) <= ConversionMode::kExplicit;
426
112
  }
427
428
  static bool IsImplicitlyConvertible(const std::shared_ptr<QLType>& lhs_type,
429
                                      const std::shared_ptr<QLType>& rhs_type);
430
431
  static bool IsComparable(DataType left, DataType right);
432
433
 private:
434
  //------------------------------------------------------------------------------------------------
435
  // Data members.
436
  DataType id_;
437
  std::vector<std::shared_ptr<QLType>> params_;
438
439
  // Members for User-Defined Types
440
  std::shared_ptr<UDTypeInfo> udtype_info_ = nullptr; // default
441
};
442
443
444
}; // namespace yb
445
446
#endif  // YB_COMMON_QL_TYPE_H_