YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/common/ql_value.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
//
14
// This file contains the QLValue class that represents QL values.
15
16
#ifndef YB_COMMON_QL_VALUE_H
17
#define YB_COMMON_QL_VALUE_H
18
19
#include <stdint.h>
20
21
#include <glog/logging.h>
22
23
#include "yb/common/common_fwd.h"
24
#include "yb/common/common_types.pb.h"
25
#include "yb/common/ql_datatype.h"
26
27
#include "yb/util/bytes_formatter.h"
28
#include "yb/util/net/inetaddress.h"
29
#include "yb/util/timestamp.h"
30
#include "yb/util/uuid.h"
31
#include "yb/util/varint.h"
32
33
// The list of unsupported datatypes to use in switch statements
34
#define QL_UNSUPPORTED_TYPES_IN_SWITCH \
35
0
  case NULL_VALUE_TYPE: FALLTHROUGH_INTENDED; \
36
0
  case TUPLE: FALLTHROUGH_INTENDED;     \
37
0
  case TYPEARGS: FALLTHROUGH_INTENDED;  \
38
0
  case UNKNOWN_DATA
39
40
#define QL_INVALID_TYPES_IN_SWITCH     \
41
0
  case UINT8:  FALLTHROUGH_INTENDED;    \
42
0
  case UINT16: FALLTHROUGH_INTENDED;    \
43
0
  case UINT32: FALLTHROUGH_INTENDED;    \
44
0
  case UINT64: FALLTHROUGH_INTENDED;    \
45
0
  case GIN_NULL
46
47
namespace yb {
48
49
//--------------------------------------------------------------------------------------------------
50
void AppendToKey(const QLValuePB &value_pb, std::string *bytes);
51
52
//--------------------------------------------------------------------------------------------------
53
// An abstract class that defines a QL value interface to support different implementations
54
// for in-memory / serialization trade-offs.
55
class QLValue {
56
 public:
57
  // Shared_ptr.
58
  typedef std::shared_ptr<QLValue> SharedPtr;
59
60
  // Constructors & destructors.
61
132M
  QLValue() { }
62
1.95k
  explicit QLValue(const QLValuePB& pb) : pb_(pb) { }
63
0
  explicit QLValue(QLValuePB&& pb) : pb_(std::move(pb)) { }
64
  virtual ~QLValue();
65
66
  //-----------------------------------------------------------------------------------------
67
  // Access functions to value and type.
68
2.01M
  InternalType type() const { return pb_.value_case(); }
69
43.1M
  const QLValuePB& value() const { return pb_; }
70
9.24M
  QLValuePB* mutable_value() { return &pb_; }
71
72
  //------------------------------------ Nullness methods -----------------------------------
73
  // Is the value null?
74
282M
  static bool IsNull(const QLValuePB& pb) { return pb.value_case() == QLValuePB::VALUE_NOT_SET; }
75
14.5M
  bool IsNull() const { return IsNull(pb_); }
76
  // Set the value to null by clearing all existing values.
77
3.74M
  void SetNull() { pb_.Clear(); }
78
79
  //-------------------------------- virtual value methods ----------------------------------
80
0
  static bool IsVirtual(const QLValuePB& pb) {
81
0
    return pb.value_case() == QLValuePB::kVirtualValue;
82
0
  }
83
707k
  bool IsVirtual() const {
84
707k
    return type() == QLValuePB::kVirtualValue;
85
707k
  }
86
0
  static bool IsMax(const QLValuePB& pb) {
87
0
    return IsVirtual(pb) && pb.virtual_value() == QLVirtualValuePB::LIMIT_MAX;
88
0
  }
89
0
  static bool IsMin(const QLValuePB& pb) {
90
0
    return IsVirtual(pb) && pb.virtual_value() == QLVirtualValuePB::LIMIT_MIN;
91
0
  }
92
0
  bool IsMax() const { return IsMax(pb_); }
93
0
  bool IsMin() const { return IsMin(pb_); }
94
0
  void SetMax() { pb_.set_virtual_value(QLVirtualValuePB::LIMIT_MAX); }
95
0
  void SetMin() { pb_.set_virtual_value(QLVirtualValuePB::LIMIT_MIN); }
96
97
  //----------------------------------- get value methods -----------------------------------
98
  // Get different datatype values. CHECK failure will result if the value stored is not of the
99
  // expected datatype or the value is null.
100
101
  #define QLVALUE_PRIMITIVE_GETTER(type, name) \
102
33.0M
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
4.39k
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
4.39k
        << "Value: " << pb.ShortDebugString(); \
105
33.0M
    return pb.BOOST_PP_CAT(name, _value)(); \
106
33.0M
  } \
Unexecuted instantiation: _ZN2yb7QLValue12uint32_valueERKNS_9QLValuePBE
Unexecuted instantiation: _ZN2yb7QLValue12uint64_valueERKNS_9QLValuePBE
Unexecuted instantiation: _ZN2yb7QLValue14gin_null_valueERKNS_9QLValuePBE
_ZN2yb7QLValue11int64_valueERKNS_9QLValuePBE
Line
Count
Source
102
2.57M
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
158
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
158
        << "Value: " << pb.ShortDebugString(); \
105
2.57M
    return pb.BOOST_PP_CAT(name, _value)(); \
106
2.57M
  } \
_ZN2yb7QLValue11int16_valueERKNS_9QLValuePBE
Line
Count
Source
102
63.7k
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
0
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
63.7k
    return pb.BOOST_PP_CAT(name, _value)(); \
106
63.7k
  } \
_ZN2yb7QLValue12double_valueERKNS_9QLValuePBE
Line
Count
Source
102
732
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
0
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
732
    return pb.BOOST_PP_CAT(name, _value)(); \
106
732
  } \
_ZN2yb7QLValue11float_valueERKNS_9QLValuePBE
Line
Count
Source
102
598
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
0
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
598
    return pb.BOOST_PP_CAT(name, _value)(); \
106
598
  } \
_ZN2yb7QLValue12string_valueERKNS_9QLValuePBE
Line
Count
Source
102
21.7M
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
4.58k
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
4.58k
        << "Value: " << pb.ShortDebugString(); \
105
21.7M
    return pb.BOOST_PP_CAT(name, _value)(); \
106
21.7M
  } \
_ZN2yb7QLValue10int8_valueERKNS_9QLValuePBE
Line
Count
Source
102
307k
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
0
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
307k
    return pb.BOOST_PP_CAT(name, _value)(); \
106
307k
  } \
_ZN2yb7QLValue11int32_valueERKNS_9QLValuePBE
Line
Count
Source
102
4.46M
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
18.4E
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
18.4E
        << "Value: " << pb.ShortDebugString(); \
105
4.46M
    return pb.BOOST_PP_CAT(name, _value)(); \
106
4.46M
  } \
_ZN2yb7QLValue11jsonb_valueERKNS_9QLValuePBE
Line
Count
Source
102
7.74k
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
18.4E
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
18.4E
        << "Value: " << pb.ShortDebugString(); \
105
7.74k
    return pb.BOOST_PP_CAT(name, _value)(); \
106
7.74k
  } \
_ZN2yb7QLValue10bool_valueERKNS_9QLValuePBE
Line
Count
Source
102
1.94M
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
18.4E
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
18.4E
        << "Value: " << pb.ShortDebugString(); \
105
1.94M
    return pb.BOOST_PP_CAT(name, _value)(); \
106
1.94M
  } \
_ZN2yb7QLValue12binary_valueERKNS_9QLValuePBE
Line
Count
Source
102
1.10M
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
15
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
15
        << "Value: " << pb.ShortDebugString(); \
105
1.10M
    return pb.BOOST_PP_CAT(name, _value)(); \
106
1.10M
  } \
_ZN2yb7QLValue10date_valueERKNS_9QLValuePBE
Line
Count
Source
102
90
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
0
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
90
    return pb.BOOST_PP_CAT(name, _value)(); \
106
90
  } \
_ZN2yb7QLValue13decimal_valueERKNS_9QLValuePBE
Line
Count
Source
102
11.4k
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
0
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
11.4k
    return pb.BOOST_PP_CAT(name, _value)(); \
106
11.4k
  } \
_ZN2yb7QLValue10time_valueERKNS_9QLValuePBE
Line
Count
Source
102
71
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
0
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
71
    return pb.BOOST_PP_CAT(name, _value)(); \
106
71
  } \
_ZN2yb7QLValue10list_valueERKNS_9QLValuePBE
Line
Count
Source
102
2.95k
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
0
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
2.95k
    return pb.BOOST_PP_CAT(name, _value)(); \
106
2.95k
  } \
_ZN2yb7QLValue9map_valueERKNS_9QLValuePBE
Line
Count
Source
102
573k
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
24
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
24
        << "Value: " << pb.ShortDebugString(); \
105
573k
    return pb.BOOST_PP_CAT(name, _value)(); \
106
573k
  } \
_ZN2yb7QLValue9set_valueERKNS_9QLValuePBE
Line
Count
Source
102
260k
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
11
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
11
        << "Value: " << pb.ShortDebugString(); \
105
260k
    return pb.BOOST_PP_CAT(name, _value)(); \
106
260k
  } \
_ZN2yb7QLValue12frozen_valueERKNS_9QLValuePBE
Line
Count
Source
102
449
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
0
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
449
    return pb.BOOST_PP_CAT(name, _value)(); \
106
449
  } \
107
  \
108
5.23M
  type BOOST_PP_CAT(name, _value)() const { \
109
5.23M
    return BOOST_PP_CAT(name, _value)(pb_); \
110
5.23M
  } \
Unexecuted instantiation: _ZNK2yb7QLValue12uint32_valueEv
Unexecuted instantiation: _ZNK2yb7QLValue12uint64_valueEv
Unexecuted instantiation: _ZNK2yb7QLValue14gin_null_valueEv
_ZNK2yb7QLValue11int64_valueEv
Line
Count
Source
108
1.91M
  type BOOST_PP_CAT(name, _value)() const { \
109
1.91M
    return BOOST_PP_CAT(name, _value)(pb_); \
110
1.91M
  } \
_ZNK2yb7QLValue11int16_valueEv
Line
Count
Source
108
42.0k
  type BOOST_PP_CAT(name, _value)() const { \
109
42.0k
    return BOOST_PP_CAT(name, _value)(pb_); \
110
42.0k
  } \
_ZNK2yb7QLValue12double_valueEv
Line
Count
Source
108
195
  type BOOST_PP_CAT(name, _value)() const { \
109
195
    return BOOST_PP_CAT(name, _value)(pb_); \
110
195
  } \
_ZNK2yb7QLValue11float_valueEv
Line
Count
Source
108
145
  type BOOST_PP_CAT(name, _value)() const { \
109
145
    return BOOST_PP_CAT(name, _value)(pb_); \
110
145
  } \
_ZNK2yb7QLValue12string_valueEv
Line
Count
Source
108
698k
  type BOOST_PP_CAT(name, _value)() const { \
109
698k
    return BOOST_PP_CAT(name, _value)(pb_); \
110
698k
  } \
_ZNK2yb7QLValue10int8_valueEv
Line
Count
Source
108
266k
  type BOOST_PP_CAT(name, _value)() const { \
109
266k
    return BOOST_PP_CAT(name, _value)(pb_); \
110
266k
  } \
_ZNK2yb7QLValue11int32_valueEv
Line
Count
Source
108
100k
  type BOOST_PP_CAT(name, _value)() const { \
109
100k
    return BOOST_PP_CAT(name, _value)(pb_); \
110
100k
  } \
_ZNK2yb7QLValue11jsonb_valueEv
Line
Count
Source
108
125
  type BOOST_PP_CAT(name, _value)() const { \
109
125
    return BOOST_PP_CAT(name, _value)(pb_); \
110
125
  } \
_ZNK2yb7QLValue10bool_valueEv
Line
Count
Source
108
1.76M
  type BOOST_PP_CAT(name, _value)() const { \
109
1.76M
    return BOOST_PP_CAT(name, _value)(pb_); \
110
1.76M
  } \
_ZNK2yb7QLValue12binary_valueEv
Line
Count
Source
108
449k
  type BOOST_PP_CAT(name, _value)() const { \
109
449k
    return BOOST_PP_CAT(name, _value)(pb_); \
110
449k
  } \
_ZNK2yb7QLValue10date_valueEv
Line
Count
Source
108
15
  type BOOST_PP_CAT(name, _value)() const { \
109
15
    return BOOST_PP_CAT(name, _value)(pb_); \
110
15
  } \
_ZNK2yb7QLValue13decimal_valueEv
Line
Count
Source
108
117
  type BOOST_PP_CAT(name, _value)() const { \
109
117
    return BOOST_PP_CAT(name, _value)(pb_); \
110
117
  } \
_ZNK2yb7QLValue10time_valueEv
Line
Count
Source
108
5
  type BOOST_PP_CAT(name, _value)() const { \
109
5
    return BOOST_PP_CAT(name, _value)(pb_); \
110
5
  } \
_ZNK2yb7QLValue10list_valueEv
Line
Count
Source
108
1
  type BOOST_PP_CAT(name, _value)() const { \
109
1
    return BOOST_PP_CAT(name, _value)(pb_); \
110
1
  } \
_ZNK2yb7QLValue9map_valueEv
Line
Count
Source
108
59
  type BOOST_PP_CAT(name, _value)() const { \
109
59
    return BOOST_PP_CAT(name, _value)(pb_); \
110
59
  } \
_ZNK2yb7QLValue9set_valueEv
Line
Count
Source
108
1
  type BOOST_PP_CAT(name, _value)() const { \
109
1
    return BOOST_PP_CAT(name, _value)(pb_); \
110
1
  } \
_ZNK2yb7QLValue12frozen_valueEv
Line
Count
Source
108
31
  type BOOST_PP_CAT(name, _value)() const { \
109
31
    return BOOST_PP_CAT(name, _value)(pb_); \
110
31
  } \
111
112
  QLVALUE_PRIMITIVE_GETTER(int8_t, int8);
113
  QLVALUE_PRIMITIVE_GETTER(int16_t, int16);
114
  QLVALUE_PRIMITIVE_GETTER(int32_t, int32);
115
  QLVALUE_PRIMITIVE_GETTER(int64_t, int64);
116
  QLVALUE_PRIMITIVE_GETTER(uint32_t, uint32);
117
  QLVALUE_PRIMITIVE_GETTER(uint64_t, uint64);
118
  QLVALUE_PRIMITIVE_GETTER(float, float);
119
  QLVALUE_PRIMITIVE_GETTER(double, double);
120
  QLVALUE_PRIMITIVE_GETTER(bool, bool);
121
  QLVALUE_PRIMITIVE_GETTER(const std::string&, decimal);
122
  QLVALUE_PRIMITIVE_GETTER(const std::string&, string);
123
  QLVALUE_PRIMITIVE_GETTER(const std::string&, binary);
124
  QLVALUE_PRIMITIVE_GETTER(const std::string&, jsonb);
125
  QLVALUE_PRIMITIVE_GETTER(uint32_t, date);
126
  QLVALUE_PRIMITIVE_GETTER(int64_t, time);
127
  QLVALUE_PRIMITIVE_GETTER(const QLMapValuePB&, map);
128
  QLVALUE_PRIMITIVE_GETTER(const QLSeqValuePB&, set);
129
  QLVALUE_PRIMITIVE_GETTER(const QLSeqValuePB&, list);
130
  QLVALUE_PRIMITIVE_GETTER(const QLSeqValuePB&, frozen);
131
  QLVALUE_PRIMITIVE_GETTER(uint8_t, gin_null);
132
  #undef QLVALUE_PRIMITIVE_GETTER
133
134
672k
  static Timestamp timestamp_value(const QLValuePB& pb) {
135
672k
    return Timestamp(timestamp_value_pb(pb));
136
672k
  }
137
138
4.29M
  static int64_t timestamp_value_pb(const QLValuePB& pb) {
139
    // Caller of this function should already read and know the PB value type before calling.
140
27.2k
    CHECK(pb.has_timestamp_value()) << "Value: " << pb.ShortDebugString();
141
4.29M
    return pb.timestamp_value();
142
4.29M
  }
143
144
37
  Timestamp timestamp_value() const {
145
37
    return timestamp_value(pb_);
146
37
  }
147
148
0
  int64_t timestamp_value_pb() const {
149
0
    return timestamp_value_pb(pb_);
150
0
  }
151
152
1.25M
  static const std::string& inetaddress_value_pb(const QLValuePB& pb) {
153
22
    CHECK(pb.has_inetaddress_value()) << "Value: " << pb.ShortDebugString();
154
1.25M
    return pb.inetaddress_value();
155
1.25M
  }
156
157
  static InetAddress inetaddress_value(const QLValuePB& pb);
158
159
0
  const std::string& inetaddress_value_pb() const {
160
0
    return inetaddress_value_pb(pb_);
161
0
  }
162
163
304
  InetAddress inetaddress_value() const {
164
304
    return inetaddress_value(pb_);
165
304
  }
166
167
369k
  static const std::string& uuid_value_pb(const QLValuePB& pb) {
168
72
    CHECK(pb.has_uuid_value()) << "Value: " << pb.ShortDebugString();
169
369k
    return pb.uuid_value();
170
369k
  }
171
  static Uuid uuid_value(const QLValuePB& pb);
172
0
  const std::string& uuid_value_pb() const {
173
0
    return uuid_value_pb(pb_);
174
0
  }
175
12
  Uuid uuid_value() const {
176
12
    return uuid_value(pb_);
177
12
  }
178
179
885
  static const std::string& timeuuid_value_pb(const QLValuePB& pb) {
180
    // Caller of this function should already read and know the PB value type before calling.
181
0
    DCHECK(pb.has_timeuuid_value()) << "Value: " << pb.ShortDebugString();
182
885
    return pb.timeuuid_value();
183
885
  }
184
185
  static Uuid timeuuid_value(const QLValuePB& pb);
186
187
0
  const std::string& timeuuid_value_pb() const {
188
0
    return timeuuid_value_pb(pb_);
189
0
  }
190
50
  Uuid timeuuid_value() const {
191
50
    return timeuuid_value(pb_);
192
50
  }
193
194
  static util::VarInt varint_value(const QLValuePB& pb);
195
136
  util::VarInt varint_value() const {
196
136
    return varint_value(pb_);
197
136
  }
198
199
87
  void AppendToKeyBytes(string *bytes) const {
200
87
    AppendToKey(pb_, bytes);
201
87
  }
202
203
  //----------------------------------- set value methods -----------------------------------
204
  // Set different datatype values.
205
40.5k
  virtual void set_int8_value(int8_t val) {
206
40.5k
    pb_.set_int8_value(val);
207
40.5k
  }
208
21.3k
  virtual void set_int16_value(int16_t val) {
209
21.3k
    pb_.set_int16_value(val);
210
21.3k
  }
211
1.80M
  virtual void set_int32_value(int32_t val) {
212
1.80M
    pb_.set_int32_value(val);
213
1.80M
  }
214
2.30M
  virtual void set_int64_value(int64_t val) {
215
2.30M
    pb_.set_int64_value(val);
216
2.30M
  }
217
0
  virtual void set_uint32_value(uint32_t val) {
218
0
    pb_.set_uint32_value(val);
219
0
  }
220
0
  virtual void set_uint64_value(uint64_t val) {
221
0
    pb_.set_uint64_value(val);
222
0
  }
223
160
  virtual void set_float_value(float val) {
224
160
    pb_.set_float_value(val);
225
160
  }
226
241
  virtual void set_double_value(double val) {
227
241
    pb_.set_double_value(val);
228
241
  }
229
  // val is a serialized binary representation of a decimal, not a plaintext
230
0
  virtual void set_decimal_value(const std::string& val) {
231
0
    pb_.set_decimal_value(val);
232
0
  }
233
  // val is a serialized binary representation of a decimal, not a plaintext
234
324
  virtual void set_decimal_value(std::string&& val) {
235
324
    pb_.set_decimal_value(std::move(val));
236
324
  }
237
7.76k
  virtual void set_jsonb_value(std::string&& val) {
238
7.76k
    pb_.set_jsonb_value(std::move(val));
239
7.76k
  }
240
13.8k
  void set_jsonb_value(const std::string& val) {
241
13.8k
    pb_.set_jsonb_value(val);
242
13.8k
  }
243
1.27M
  virtual void set_bool_value(bool val) {
244
1.27M
    pb_.set_bool_value(val);
245
1.27M
  }
246
6.35k
  virtual void set_string_value(const std::string& val) {
247
6.35k
    pb_.set_string_value(val);
248
6.35k
  }
249
297
  virtual void set_string_value(std::string&& val) {
250
297
    pb_.set_string_value(std::move(val));
251
297
  }
252
15
  virtual void set_string_value(const char* val) {
253
15
    pb_.set_string_value(val);
254
15
  }
255
0
  virtual void set_string_value(const char* val, size_t size) {
256
0
    pb_.set_string_value(val, size);
257
0
  }
258
40
  virtual void set_timestamp_value(const Timestamp& val) {
259
40
    pb_.set_timestamp_value(val.ToInt64());
260
40
  }
261
670k
  virtual void set_timestamp_value(int64_t val) {
262
670k
    pb_.set_timestamp_value(val);
263
670k
  }
264
343
  virtual void set_date_value(uint32_t val) {
265
343
    pb_.set_date_value(val);
266
343
  }
267
341
  virtual void set_time_value(int64_t val) {
268
341
    pb_.set_time_value(val);
269
341
  }
270
33
  virtual void set_binary_value(const std::string& val) {
271
33
    pb_.set_binary_value(val);
272
33
  }
273
5
  virtual void set_binary_value(std::string&& val) {
274
5
    pb_.set_binary_value(std::move(val));
275
5
  }
276
15.2M
  virtual void set_binary_value(const void* val, size_t size) {
277
15.2M
    pb_.set_binary_value(val, size);
278
15.2M
  }
279
280
  static void set_inetaddress_value(const InetAddress& val, QLValuePB* pb);
281
282
874k
  void set_inetaddress_value(const InetAddress& val) {
283
874k
    set_inetaddress_value(val, &pb_);
284
874k
  }
285
286
921k
  static void set_uuid_value(const Uuid& val, QLValuePB* out) {
287
921k
    val.ToBytes(out->mutable_uuid_value());
288
921k
  }
289
290
272k
  void set_uuid_value(const Uuid& val) {
291
272k
    set_uuid_value(val, &pb_);
292
272k
  }
293
294
  virtual void set_timeuuid_value(const Uuid& val);
295
118
  virtual void set_varint_value(const util::VarInt& val) {
296
118
    pb_.set_varint_value(val.EncodeToComparable());
297
118
  }
298
299
0
  virtual void set_gin_null_value(uint8_t val) {
300
0
    pb_.set_gin_null_value(val);
301
0
  }
302
303
  //--------------------------------- mutable value methods ----------------------------------
304
0
  std::string* mutable_decimal_value() {
305
0
    return pb_.mutable_decimal_value();
306
0
  }
307
57
  std::string* mutable_jsonb_value() {
308
57
    return pb_.mutable_jsonb_value();
309
57
  }
310
0
  std::string* mutable_varint_value() {
311
0
    return pb_.mutable_varint_value();
312
0
  }
313
16.2M
  std::string* mutable_string_value() {
314
16.2M
    return pb_.mutable_string_value();
315
16.2M
  }
316
439k
  std::string* mutable_binary_value() {
317
439k
    return pb_.mutable_binary_value();
318
439k
  }
319
95
  QLMapValuePB* mutable_map_value() {
320
95
    return pb_.mutable_map_value();
321
95
  }
322
1
  QLSeqValuePB* mutable_set_value() {
323
1
    return pb_.mutable_set_value();
324
1
  }
325
9
  QLSeqValuePB* mutable_list_value() {
326
9
    return pb_.mutable_list_value();
327
9
  }
328
1
  QLSeqValuePB* mutable_frozen_value() {
329
1
    return pb_.mutable_frozen_value();
330
1
  }
331
332
  // To extend/construct collections we return freshly allocated elements for the caller to set.
333
1.17M
  virtual QLValuePB* add_map_key() {
334
1.17M
    return pb_.mutable_map_value()->add_keys();
335
1.17M
  }
336
1.17M
  virtual QLValuePB* add_map_value() {
337
1.17M
    return pb_.mutable_map_value()->add_values();
338
1.17M
  }
339
214k
  virtual QLValuePB* add_set_elem() {
340
214k
    return pb_.mutable_set_value()->add_elems();
341
214k
  }
342
5.51k
  virtual QLValuePB* add_list_elem() {
343
5.51k
    return pb_.mutable_list_value()->add_elems();
344
5.51k
  }
345
107
  virtual QLValuePB* add_frozen_elem() {
346
107
    return pb_.mutable_frozen_value()->add_elems();
347
107
  }
348
349
  // For collections, the call to `mutable_foo` takes care of setting the correct type to `foo`
350
  // internally and allocating the message if needed
351
  // TODO(neil) Change these set to "mutable_xxx_value()".
352
689k
  virtual void set_map_value() {
353
689k
    pb_.mutable_map_value();
354
689k
  }
355
214k
  virtual void set_set_value() {
356
214k
    pb_.mutable_set_value();
357
214k
  }
358
766
  virtual void set_list_value() {
359
766
    pb_.mutable_list_value();
360
766
  }
361
53
  virtual void set_frozen_value() {
362
53
    pb_.mutable_frozen_value();
363
53
  }
364
365
  //----------------------------------- assignment methods ----------------------------------
366
6.78M
  QLValue& operator=(const QLValuePB& other) {
367
6.78M
    pb_ = other;
368
6.78M
    return *this;
369
6.78M
  }
370
18.2M
  QLValue& operator=(QLValuePB&& other) {
371
18.2M
    pb_ = std::move(other);
372
18.2M
    return *this;
373
18.2M
  }
374
375
  //----------------------------------- comparison methods -----------------------------------
376
0
  virtual bool Comparable(const QLValue& other) const {
377
0
    return type() == other.type() || EitherIsNull(other) || EitherIsVirtual(other);
378
0
  }
379
53.8k
  virtual bool BothNotNull(const QLValue& other) const {
380
53.8k
    return !IsNull() && !other.IsNull();
381
53.8k
  }
382
0
  virtual bool BothNull(const QLValue& other) const {
383
0
    return IsNull() && other.IsNull();
384
0
  }
385
0
  virtual bool EitherIsNull(const QLValue& other) const {
386
0
    return IsNull() || other.IsNull();
387
0
  }
388
0
  virtual bool EitherIsVirtual(const QLValue& other) const {
389
0
    return IsVirtual() || other.IsVirtual();
390
0
  }
391
392
  virtual int CompareTo(const QLValue& other) const;
393
394
  // In YCQL null is not comparable with regular values (w.r.t. ordering).
395
53.8k
  virtual bool operator <(const QLValue& v) const {
396
53.8k
    return BothNotNull(v) && CompareTo(v) < 0;
397
53.8k
  }
398
42
  virtual bool operator >(const QLValue& v) const {
399
42
    return BothNotNull(v) && CompareTo(v) > 0;
400
42
  }
401
402
  // In YCQL equality holds for null values.
403
0
  virtual bool operator <=(const QLValue& v) const {
404
0
    return (BothNotNull(v) && CompareTo(v) <= 0) || BothNull(v);
405
0
  }
406
0
  virtual bool operator >=(const QLValue& v) const {
407
0
    return (BothNotNull(v) && CompareTo(v) >= 0) || BothNull(v);
408
0
  }
409
0
  virtual bool operator ==(const QLValue& v) const {
410
0
    return (BothNotNull(v) && CompareTo(v) == 0) || BothNull(v);
411
0
  }
412
0
  virtual bool operator !=(const QLValue& v) const {
413
0
    return !(*this == v);
414
0
  }
415
416
  //----------------------------- serializer / deserializer ---------------------------------
417
  static void Serialize(const std::shared_ptr<QLType>& ql_type,
418
                        const QLClient& client,
419
                        const QLValuePB& pb,
420
                        faststring* buffer);
421
422
  void Serialize(const std::shared_ptr<QLType>& ql_type,
423
                 const QLClient& client,
424
                 faststring* buffer) const;
425
  CHECKED_STATUS Deserialize(const std::shared_ptr<QLType>& ql_type,
426
                             const QLClient& client,
427
                             Slice* data);
428
429
  //------------------------------------ debug string ---------------------------------------
430
  // Return a string for debugging.
431
  std::string ToValueString(const QuotesType quotes_type = QuotesType::kDoubleQuotes) const;
432
  std::string ToString() const;
433
434
 private:
435
  // Deserialize a CQL number (8, 16, 32 and 64-bit integer). <num_type> is the parsed integer type.
436
  // <converter> converts the number from network byte-order to machine order and <data_type>
437
  // is the coverter's return type. The converter's return type <data_type> is unsigned while
438
  // <num_type> may be signed or unsigned. <setter> sets the value in QLValue.
439
  template<typename num_type, typename data_type>
440
  CHECKED_STATUS CQLDeserializeNum(
441
      size_t len, data_type (*converter)(const void*), void (QLValue::*setter)(num_type),
442
      Slice* data);
443
444
  // Deserialize a CQL floating point number (float or double). <float_type> is the parsed floating
445
  // point type. <converter> converts the number from network byte-order to machine order and
446
  // <data_type> is the coverter's return type. The converter's return type <data_type> is an
447
  // integer type. <setter> sets the value in QLValue.
448
  template<typename float_type, typename data_type>
449
  CHECKED_STATUS CQLDeserializeFloat(
450
      size_t len, data_type (*converter)(const void*), void (QLValue::*setter)(float_type),
451
      Slice* data);
452
453
  // TODO(neil) This should be changed to shared_ptr<QLValuePB>. That way, we assign the pointers
454
  // instead of copying the same value many times during expression evaluation.
455
  // Protobuf value.
456
  QLValuePB pb_;
457
};
458
459
//--------------------------------------------------------------------------------------------------
460
// QLValuePB operators
461
bool operator <(const QLValuePB& lhs, const QLValuePB& rhs);
462
bool operator >(const QLValuePB& lhs, const QLValuePB& rhs);
463
bool operator <=(const QLValuePB& lhs, const QLValuePB& rhs);
464
bool operator >=(const QLValuePB& lhs, const QLValuePB& rhs);
465
bool operator ==(const QLValuePB& lhs, const QLValuePB& rhs);
466
bool operator !=(const QLValuePB& lhs, const QLValuePB& rhs);
467
468
bool operator <(const QLValuePB& lhs, const QLValue& rhs);
469
bool operator >(const QLValuePB& lhs, const QLValue& rhs);
470
bool operator <=(const QLValuePB& lhs, const QLValue& rhs);
471
bool operator >=(const QLValuePB& lhs, const QLValue& rhs);
472
bool operator ==(const QLValuePB& lhs, const QLValue& rhs);
473
bool operator !=(const QLValuePB& lhs, const QLValue& rhs);
474
475
InternalType type(const QLValuePB& v);
476
bool IsNull(const QLValuePB& v);
477
void SetNull(QLValuePB* v);
478
bool EitherIsNull(const QLValuePB& lhs, const QLValuePB& rhs);
479
bool BothNotNull(const QLValuePB& lhs, const QLValuePB& rhs);
480
bool BothNull(const QLValuePB& lhs, const QLValuePB& rhs);
481
bool Comparable(const QLValuePB& lhs, const QLValuePB& rhs);
482
int Compare(const QLValuePB& lhs, const QLValuePB& rhs);
483
bool EitherIsNull(const QLValuePB& lhs, const QLValue& rhs);
484
bool Comparable(const QLValuePB& lhs, const QLValue& rhs);
485
bool BothNotNull(const QLValuePB& lhs, const QLValue& rhs);
486
bool BothNull(const QLValuePB& lhs, const QLValue& rhs);
487
int Compare(const QLValuePB& lhs, const QLValue& rhs);
488
int Compare(const QLSeqValuePB& lhs, const QLSeqValuePB& rhs);
489
int Compare(const bool lhs, const bool rhs);
490
491
#define YB_SET_INT_VALUE(ql_valuepb, input, bits) \
492
0
  case DataType::BOOST_PP_CAT(INT, bits): { \
493
0
    auto value = CheckedStoInt<BOOST_PP_CAT(BOOST_PP_CAT(int, bits), _t)>(input); \
494
0
    RETURN_NOT_OK(value); \
495
0
    ql_valuepb->BOOST_PP_CAT(BOOST_PP_CAT(set_int, bits), _value)(*value); \
496
0
  } break;
497
498
} // namespace yb
499
500
#endif // YB_COMMON_QL_VALUE_H