YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
224M
  QLValue() { }
62
1.50k
  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.29M
  InternalType type() const { return pb_.value_case(); }
69
69.1M
  const QLValuePB& value() const { return pb_; }
70
14.9M
  QLValuePB* mutable_value() { return &pb_; }
71
72
  //------------------------------------ Nullness methods -----------------------------------
73
  // Is the value null?
74
723M
  static bool IsNull(const QLValuePB& pb) { return pb.value_case() == QLValuePB::VALUE_NOT_SET; }
75
15.5M
  bool IsNull() const { return IsNull(pb_); }
76
  // Set the value to null by clearing all existing values.
77
3.63M
  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
724k
  bool IsVirtual() const {
84
724k
    return type() == QLValuePB::kVirtualValue;
85
724k
  }
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
42.3M
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
42.3M
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
8.34k
        << "Value: " << pb.ShortDebugString(); \
105
42.3M
    return pb.BOOST_PP_CAT(name, _value)(); \
106
42.3M
  } \
Unexecuted instantiation: yb::QLValue::uint32_value(yb::QLValuePB const&)
Unexecuted instantiation: yb::QLValue::uint64_value(yb::QLValuePB const&)
Unexecuted instantiation: yb::QLValue::gin_null_value(yb::QLValuePB const&)
yb::QLValue::int32_value(yb::QLValuePB const&)
Line
Count
Source
102
4.44M
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
4.44M
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
19
        << "Value: " << pb.ShortDebugString(); \
105
4.44M
    return pb.BOOST_PP_CAT(name, _value)(); \
106
4.44M
  } \
yb::QLValue::int8_value(yb::QLValuePB const&)
Line
Count
Source
102
559k
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
559k
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
2
        << "Value: " << pb.ShortDebugString(); \
105
559k
    return pb.BOOST_PP_CAT(name, _value)(); \
106
559k
  } \
yb::QLValue::int16_value(yb::QLValuePB const&)
Line
Count
Source
102
63.6k
  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
63.6k
    return pb.BOOST_PP_CAT(name, _value)(); \
106
63.6k
  } \
yb::QLValue::int64_value(yb::QLValuePB const&)
Line
Count
Source
102
3.85M
  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
3.85M
    return pb.BOOST_PP_CAT(name, _value)(); \
106
3.85M
  } \
yb::QLValue::float_value(yb::QLValuePB const&)
Line
Count
Source
102
601
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
601
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
601
    return pb.BOOST_PP_CAT(name, _value)(); \
106
601
  } \
yb::QLValue::double_value(yb::QLValuePB const&)
Line
Count
Source
102
738
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
738
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
738
    return pb.BOOST_PP_CAT(name, _value)(); \
106
738
  } \
yb::QLValue::decimal_value(yb::QLValuePB const&)
Line
Count
Source
102
11.4k
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
11.4k
    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
  } \
yb::QLValue::map_value(yb::QLValuePB const&)
Line
Count
Source
102
599k
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
599k
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
23
        << "Value: " << pb.ShortDebugString(); \
105
599k
    return pb.BOOST_PP_CAT(name, _value)(); \
106
599k
  } \
yb::QLValue::binary_value(yb::QLValuePB const&)
Line
Count
Source
102
1.67M
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
1.67M
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
1
        << "Value: " << pb.ShortDebugString(); \
105
1.67M
    return pb.BOOST_PP_CAT(name, _value)(); \
106
1.67M
  } \
yb::QLValue::jsonb_value(yb::QLValuePB const&)
Line
Count
Source
102
18.8k
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
18.8k
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
87
        << "Value: " << pb.ShortDebugString(); \
105
18.8k
    return pb.BOOST_PP_CAT(name, _value)(); \
106
18.8k
  } \
yb::QLValue::string_value(yb::QLValuePB const&)
Line
Count
Source
102
28.9M
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
28.9M
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
9.39k
        << "Value: " << pb.ShortDebugString(); \
105
28.9M
    return pb.BOOST_PP_CAT(name, _value)(); \
106
28.9M
  } \
yb::QLValue::bool_value(yb::QLValuePB const&)
Line
Count
Source
102
1.90M
  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.90M
    return pb.BOOST_PP_CAT(name, _value)(); \
106
1.90M
  } \
yb::QLValue::date_value(yb::QLValuePB const&)
Line
Count
Source
102
156
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
156
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
156
    return pb.BOOST_PP_CAT(name, _value)(); \
106
156
  } \
yb::QLValue::time_value(yb::QLValuePB const&)
Line
Count
Source
102
72
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
72
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
72
    return pb.BOOST_PP_CAT(name, _value)(); \
106
72
  } \
yb::QLValue::set_value(yb::QLValuePB const&)
Line
Count
Source
102
262k
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
262k
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
60
        << "Value: " << pb.ShortDebugString(); \
105
262k
    return pb.BOOST_PP_CAT(name, _value)(); \
106
262k
  } \
yb::QLValue::list_value(yb::QLValuePB const&)
Line
Count
Source
102
3.02k
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
3.02k
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
3.02k
    return pb.BOOST_PP_CAT(name, _value)(); \
106
3.02k
  } \
yb::QLValue::frozen_value(yb::QLValuePB const&)
Line
Count
Source
102
453
  static type BOOST_PP_CAT(name, _value)(const QLValuePB& pb) { \
103
453
    CHECK(pb.BOOST_PP_CAT(has_, BOOST_PP_CAT(name, _value))()) \
104
0
        << "Value: " << pb.ShortDebugString(); \
105
453
    return pb.BOOST_PP_CAT(name, _value)(); \
106
453
  } \
107
  \
108
7.38M
  type BOOST_PP_CAT(name, _value)() const { \
109
7.38M
    return BOOST_PP_CAT(name, _value)(pb_); \
110
7.38M
  } \
Unexecuted instantiation: yb::QLValue::uint32_value() const
Unexecuted instantiation: yb::QLValue::uint64_value() const
Unexecuted instantiation: yb::QLValue::gin_null_value() const
yb::QLValue::int32_value() const
Line
Count
Source
108
100k
  type BOOST_PP_CAT(name, _value)() const { \
109
100k
    return BOOST_PP_CAT(name, _value)(pb_); \
110
100k
  } \
yb::QLValue::int8_value() const
Line
Count
Source
108
518k
  type BOOST_PP_CAT(name, _value)() const { \
109
518k
    return BOOST_PP_CAT(name, _value)(pb_); \
110
518k
  } \
yb::QLValue::int16_value() const
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
  } \
yb::QLValue::int64_value() const
Line
Count
Source
108
3.28M
  type BOOST_PP_CAT(name, _value)() const { \
109
3.28M
    return BOOST_PP_CAT(name, _value)(pb_); \
110
3.28M
  } \
yb::QLValue::float_value() const
Line
Count
Source
108
145
  type BOOST_PP_CAT(name, _value)() const { \
109
145
    return BOOST_PP_CAT(name, _value)(pb_); \
110
145
  } \
yb::QLValue::double_value() const
Line
Count
Source
108
195
  type BOOST_PP_CAT(name, _value)() const { \
109
195
    return BOOST_PP_CAT(name, _value)(pb_); \
110
195
  } \
yb::QLValue::decimal_value() const
Line
Count
Source
108
117
  type BOOST_PP_CAT(name, _value)() const { \
109
117
    return BOOST_PP_CAT(name, _value)(pb_); \
110
117
  } \
yb::QLValue::map_value() const
Line
Count
Source
108
59
  type BOOST_PP_CAT(name, _value)() const { \
109
59
    return BOOST_PP_CAT(name, _value)(pb_); \
110
59
  } \
yb::QLValue::binary_value() const
Line
Count
Source
108
954k
  type BOOST_PP_CAT(name, _value)() const { \
109
954k
    return BOOST_PP_CAT(name, _value)(pb_); \
110
954k
  } \
yb::QLValue::string_value() const
Line
Count
Source
108
716k
  type BOOST_PP_CAT(name, _value)() const { \
109
716k
    return BOOST_PP_CAT(name, _value)(pb_); \
110
716k
  } \
yb::QLValue::bool_value() const
Line
Count
Source
108
1.77M
  type BOOST_PP_CAT(name, _value)() const { \
109
1.77M
    return BOOST_PP_CAT(name, _value)(pb_); \
110
1.77M
  } \
yb::QLValue::date_value() const
Line
Count
Source
108
15
  type BOOST_PP_CAT(name, _value)() const { \
109
15
    return BOOST_PP_CAT(name, _value)(pb_); \
110
15
  } \
yb::QLValue::time_value() const
Line
Count
Source
108
5
  type BOOST_PP_CAT(name, _value)() const { \
109
5
    return BOOST_PP_CAT(name, _value)(pb_); \
110
5
  } \
yb::QLValue::jsonb_value() const
Line
Count
Source
108
125
  type BOOST_PP_CAT(name, _value)() const { \
109
125
    return BOOST_PP_CAT(name, _value)(pb_); \
110
125
  } \
yb::QLValue::set_value() const
Line
Count
Source
108
1
  type BOOST_PP_CAT(name, _value)() const { \
109
1
    return BOOST_PP_CAT(name, _value)(pb_); \
110
1
  } \
yb::QLValue::list_value() const
Line
Count
Source
108
1
  type BOOST_PP_CAT(name, _value)() const { \
109
1
    return BOOST_PP_CAT(name, _value)(pb_); \
110
1
  } \
yb::QLValue::frozen_value() const
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
1.43M
  static Timestamp timestamp_value(const QLValuePB& pb) {
135
1.43M
    return Timestamp(timestamp_value_pb(pb));
136
1.43M
  }
137
138
8.60M
  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
8.60M
    CHECK
(pb.has_timestamp_value()) << "Value: " << pb.ShortDebugString()34.0k
;
141
8.60M
    return pb.timestamp_value();
142
8.60M
  }
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.35M
  static const std::string& inetaddress_value_pb(const QLValuePB& pb) {
153
1.35M
    CHECK
(pb.has_inetaddress_value()) << "Value: " << pb.ShortDebugString()68
;
154
1.35M
    return pb.inetaddress_value();
155
1.35M
  }
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
612
  InetAddress inetaddress_value() const {
164
612
    return inetaddress_value(pb_);
165
612
  }
166
167
375k
  static const std::string& uuid_value_pb(const QLValuePB& pb) {
168
18.4E
    CHECK(pb.has_uuid_value()) << "Value: " << pb.ShortDebugString();
169
375k
    return pb.uuid_value();
170
375k
  }
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
919
  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
919
    DCHECK
(pb.has_timeuuid_value()) << "Value: " << pb.ShortDebugString()0
;
182
919
    return pb.timeuuid_value();
183
919
  }
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
148
  util::VarInt varint_value() const {
196
148
    return varint_value(pb_);
197
148
  }
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.89M
  virtual void set_int32_value(int32_t val) {
212
1.89M
    pb_.set_int32_value(val);
213
1.89M
  }
214
3.77M
  virtual void set_int64_value(int64_t val) {
215
3.77M
    pb_.set_int64_value(val);
216
3.77M
  }
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
183
  virtual void set_float_value(float val) {
224
183
    pb_.set_float_value(val);
225
183
  }
226
92.6k
  virtual void set_double_value(double val) {
227
92.6k
    pb_.set_double_value(val);
228
92.6k
  }
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
12.7k
  virtual void set_jsonb_value(std::string&& val) {
238
12.7k
    pb_.set_jsonb_value(std::move(val));
239
12.7k
  }
240
20.0k
  void set_jsonb_value(const std::string& val) {
241
20.0k
    pb_.set_jsonb_value(val);
242
20.0k
  }
243
1.26M
  virtual void set_bool_value(bool val) {
244
1.26M
    pb_.set_bool_value(val);
245
1.26M
  }
246
6.36k
  virtual void set_string_value(const std::string& val) {
247
6.36k
    pb_.set_string_value(val);
248
6.36k
  }
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
100k
  virtual void set_string_value(const char* val, size_t size) {
256
100k
    pb_.set_string_value(val, size);
257
100k
  }
258
40
  virtual void set_timestamp_value(const Timestamp& val) {
259
40
    pb_.set_timestamp_value(val.ToInt64());
260
40
  }
261
1.43M
  virtual void set_timestamp_value(int64_t val) {
262
1.43M
    pb_.set_timestamp_value(val);
263
1.43M
  }
264
379
  virtual void set_date_value(uint32_t val) {
265
379
    pb_.set_date_value(val);
266
379
  }
267
342
  virtual void set_time_value(int64_t val) {
268
342
    pb_.set_time_value(val);
269
342
  }
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
41.1M
  virtual void set_binary_value(const void* val, size_t size) {
277
41.1M
    pb_.set_binary_value(val, size);
278
41.1M
  }
279
280
  static QLValuePB Primitive(const std::string& str);
281
282
  static QLValuePB Primitive(double value);
283
284
  static QLValuePB Primitive(int32_t value);
285
286
47.0k
  static QLValuePB Primitive(int64_t value) {
287
47.0k
    return PrimitiveInt64(value);
288
47.0k
  }
289
290
  static QLValuePB PrimitiveInt64(int64_t value);
291
292
  static void AppendPrimitiveArray(QLValuePB* arr) {
293
  }
294
295
  template <class Val, class... Args>
296
  static void AppendPrimitiveArray(QLValuePB* arr, const Val& val, Args&&... args) {
297
    *arr->mutable_list_value()->mutable_elems()->Add() = Primitive(val);
298
    AppendPrimitiveArray(arr, std::forward<Args>(args)...);
299
  }
300
301
  template <class... Args>
302
  static QLValuePB PrimitiveArray(Args&&... args) {
303
    QLValuePB result;
304
    AppendPrimitiveArray(&result, std::forward<Args>(args)...);
305
    return result;
306
  }
307
308
  static void set_inetaddress_value(const InetAddress& val, QLValuePB* pb);
309
310
834k
  void set_inetaddress_value(const InetAddress& val) {
311
834k
    set_inetaddress_value(val, &pb_);
312
834k
  }
313
314
941k
  static void set_uuid_value(const Uuid& val, QLValuePB* out) {
315
941k
    val.ToBytes(out->mutable_uuid_value());
316
941k
  }
317
318
263k
  void set_uuid_value(const Uuid& val) {
319
263k
    set_uuid_value(val, &pb_);
320
263k
  }
321
322
  virtual void set_timeuuid_value(const Uuid& val);
323
118
  virtual void set_varint_value(const util::VarInt& val) {
324
118
    pb_.set_varint_value(val.EncodeToComparable());
325
118
  }
326
327
0
  virtual void set_gin_null_value(uint8_t val) {
328
0
    pb_.set_gin_null_value(val);
329
0
  }
330
331
  //--------------------------------- mutable value methods ----------------------------------
332
0
  std::string* mutable_decimal_value() {
333
0
    return pb_.mutable_decimal_value();
334
0
  }
335
84
  std::string* mutable_jsonb_value() {
336
84
    return pb_.mutable_jsonb_value();
337
84
  }
338
0
  std::string* mutable_varint_value() {
339
0
    return pb_.mutable_varint_value();
340
0
  }
341
20.9M
  std::string* mutable_string_value() {
342
20.9M
    return pb_.mutable_string_value();
343
20.9M
  }
344
420k
  std::string* mutable_binary_value() {
345
420k
    return pb_.mutable_binary_value();
346
420k
  }
347
95
  QLMapValuePB* mutable_map_value() {
348
95
    return pb_.mutable_map_value();
349
95
  }
350
1
  QLSeqValuePB* mutable_set_value() {
351
1
    return pb_.mutable_set_value();
352
1
  }
353
9
  QLSeqValuePB* mutable_list_value() {
354
9
    return pb_.mutable_list_value();
355
9
  }
356
1
  QLSeqValuePB* mutable_frozen_value() {
357
1
    return pb_.mutable_frozen_value();
358
1
  }
359
360
  // To extend/construct collections we return freshly allocated elements for the caller to set.
361
1.13M
  virtual QLValuePB* add_map_key() {
362
1.13M
    return pb_.mutable_map_value()->add_keys();
363
1.13M
  }
364
1.13M
  virtual QLValuePB* add_map_value() {
365
1.13M
    return pb_.mutable_map_value()->add_values();
366
1.13M
  }
367
208k
  virtual QLValuePB* add_set_elem() {
368
208k
    return pb_.mutable_set_value()->add_elems();
369
208k
  }
370
5.66k
  virtual QLValuePB* add_list_elem() {
371
5.66k
    return pb_.mutable_list_value()->add_elems();
372
5.66k
  }
373
110
  virtual QLValuePB* add_frozen_elem() {
374
110
    return pb_.mutable_frozen_value()->add_elems();
375
110
  }
376
377
  // For collections, the call to `mutable_foo` takes care of setting the correct type to `foo`
378
  // internally and allocating the message if needed
379
  // TODO(neil) Change these set to "mutable_xxx_value()".
380
670k
  virtual void set_map_value() {
381
670k
    pb_.mutable_map_value();
382
670k
  }
383
208k
  virtual void set_set_value() {
384
208k
    pb_.mutable_set_value();
385
208k
  }
386
837
  virtual void set_list_value() {
387
837
    pb_.mutable_list_value();
388
837
  }
389
57
  virtual void set_frozen_value() {
390
57
    pb_.mutable_frozen_value();
391
57
  }
392
393
  //----------------------------------- assignment methods ----------------------------------
394
7.17M
  QLValue& operator=(const QLValuePB& other) {
395
7.17M
    pb_ = other;
396
7.17M
    return *this;
397
7.17M
  }
398
18.4M
  QLValue& operator=(QLValuePB&& other) {
399
18.4M
    pb_ = std::move(other);
400
18.4M
    return *this;
401
18.4M
  }
402
403
  //----------------------------------- comparison methods -----------------------------------
404
0
  virtual bool Comparable(const QLValue& other) const {
405
0
    return type() == other.type() || EitherIsNull(other) || EitherIsVirtual(other);
406
0
  }
407
53.5k
  virtual bool BothNotNull(const QLValue& other) const {
408
53.5k
    return !IsNull() && !other.IsNull();
409
53.5k
  }
410
0
  virtual bool BothNull(const QLValue& other) const {
411
0
    return IsNull() && other.IsNull();
412
0
  }
413
0
  virtual bool EitherIsNull(const QLValue& other) const {
414
0
    return IsNull() || other.IsNull();
415
0
  }
416
0
  virtual bool EitherIsVirtual(const QLValue& other) const {
417
0
    return IsVirtual() || other.IsVirtual();
418
0
  }
419
420
  virtual int CompareTo(const QLValue& other) const;
421
422
  // In YCQL null is not comparable with regular values (w.r.t. ordering).
423
53.5k
  virtual bool operator <(const QLValue& v) const {
424
53.5k
    return BothNotNull(v) && CompareTo(v) < 0;
425
53.5k
  }
426
43
  virtual bool operator >(const QLValue& v) const {
427
43
    return BothNotNull(v) && CompareTo(v) > 0;
428
43
  }
429
430
  // In YCQL equality holds for null values.
431
0
  virtual bool operator <=(const QLValue& v) const {
432
0
    return (BothNotNull(v) && CompareTo(v) <= 0) || BothNull(v);
433
0
  }
434
0
  virtual bool operator >=(const QLValue& v) const {
435
0
    return (BothNotNull(v) && CompareTo(v) >= 0) || BothNull(v);
436
0
  }
437
0
  virtual bool operator ==(const QLValue& v) const {
438
0
    return (BothNotNull(v) && CompareTo(v) == 0) || BothNull(v);
439
0
  }
440
0
  virtual bool operator !=(const QLValue& v) const {
441
0
    return !(*this == v);
442
0
  }
443
444
  //----------------------------- serializer / deserializer ---------------------------------
445
  static void Serialize(const std::shared_ptr<QLType>& ql_type,
446
                        const QLClient& client,
447
                        const QLValuePB& pb,
448
                        faststring* buffer);
449
450
  void Serialize(const std::shared_ptr<QLType>& ql_type,
451
                 const QLClient& client,
452
                 faststring* buffer) const;
453
  CHECKED_STATUS Deserialize(const std::shared_ptr<QLType>& ql_type,
454
                             const QLClient& client,
455
                             Slice* data);
456
457
  //------------------------------------ debug string ---------------------------------------
458
  // Return a string for debugging.
459
  std::string ToValueString(const QuotesType quotes_type = QuotesType::kDoubleQuotes) const;
460
  std::string ToString() const;
461
462
 private:
463
  // Deserialize a CQL number (8, 16, 32 and 64-bit integer). <num_type> is the parsed integer type.
464
  // <converter> converts the number from network byte-order to machine order and <data_type>
465
  // is the coverter's return type. The converter's return type <data_type> is unsigned while
466
  // <num_type> may be signed or unsigned. <setter> sets the value in QLValue.
467
  template<typename num_type, typename data_type>
468
  CHECKED_STATUS CQLDeserializeNum(
469
      size_t len, data_type (*converter)(const void*), void (QLValue::*setter)(num_type),
470
      Slice* data);
471
472
  // Deserialize a CQL floating point number (float or double). <float_type> is the parsed floating
473
  // point type. <converter> converts the number from network byte-order to machine order and
474
  // <data_type> is the coverter's return type. The converter's return type <data_type> is an
475
  // integer type. <setter> sets the value in QLValue.
476
  template<typename float_type, typename data_type>
477
  CHECKED_STATUS CQLDeserializeFloat(
478
      size_t len, data_type (*converter)(const void*), void (QLValue::*setter)(float_type),
479
      Slice* data);
480
481
  // TODO(neil) This should be changed to shared_ptr<QLValuePB>. That way, we assign the pointers
482
  // instead of copying the same value many times during expression evaluation.
483
  // Protobuf value.
484
  QLValuePB pb_;
485
};
486
487
//--------------------------------------------------------------------------------------------------
488
// QLValuePB operators
489
bool operator <(const QLValuePB& lhs, const QLValuePB& rhs);
490
bool operator >(const QLValuePB& lhs, const QLValuePB& rhs);
491
bool operator <=(const QLValuePB& lhs, const QLValuePB& rhs);
492
bool operator >=(const QLValuePB& lhs, const QLValuePB& rhs);
493
bool operator ==(const QLValuePB& lhs, const QLValuePB& rhs);
494
bool operator !=(const QLValuePB& lhs, const QLValuePB& rhs);
495
496
bool operator <(const QLValuePB& lhs, const QLValue& rhs);
497
bool operator >(const QLValuePB& lhs, const QLValue& rhs);
498
bool operator <=(const QLValuePB& lhs, const QLValue& rhs);
499
bool operator >=(const QLValuePB& lhs, const QLValue& rhs);
500
bool operator ==(const QLValuePB& lhs, const QLValue& rhs);
501
bool operator !=(const QLValuePB& lhs, const QLValue& rhs);
502
503
InternalType type(const QLValuePB& v);
504
bool IsNull(const QLValuePB& v);
505
void SetNull(QLValuePB* v);
506
bool EitherIsNull(const QLValuePB& lhs, const QLValuePB& rhs);
507
bool BothNotNull(const QLValuePB& lhs, const QLValuePB& rhs);
508
bool BothNull(const QLValuePB& lhs, const QLValuePB& rhs);
509
bool Comparable(const QLValuePB& lhs, const QLValuePB& rhs);
510
int Compare(const QLValuePB& lhs, const QLValuePB& rhs);
511
bool EitherIsNull(const QLValuePB& lhs, const QLValue& rhs);
512
bool Comparable(const QLValuePB& lhs, const QLValue& rhs);
513
bool BothNotNull(const QLValuePB& lhs, const QLValue& rhs);
514
bool BothNull(const QLValuePB& lhs, const QLValue& rhs);
515
int Compare(const QLValuePB& lhs, const QLValue& rhs);
516
int Compare(const QLSeqValuePB& lhs, const QLSeqValuePB& rhs);
517
int Compare(const bool lhs, const bool rhs);
518
519
#define YB_SET_INT_VALUE(ql_valuepb, input, bits) \
520
0
  case DataType::BOOST_PP_CAT(INT, bits): { \
521
0
    auto value = CheckedStoInt<BOOST_PP_CAT(BOOST_PP_CAT(int, bits), _t)>(input); \
522
0
    RETURN_NOT_OK(value); \
523
0
    ql_valuepb->BOOST_PP_CAT(BOOST_PP_CAT(set_int, bits), _value)(*value); \
524
0
  } break;
525
526
} // namespace yb
527
528
#endif // YB_COMMON_QL_VALUE_H