YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/client/value.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include "yb/client/value.h"
34
35
#include <glog/logging.h>
36
37
#include "yb/client/value-internal.h"
38
#include "yb/common/ql_type.h"
39
#include "yb/common/types.h"
40
#include "yb/gutil/strings/substitute.h"
41
#include "yb/gutil/mathlimits.h"
42
#include "yb/util/status.h"
43
44
using std::shared_ptr;
45
using std::string;
46
using strings::Substitute;
47
48
namespace yb {
49
namespace client {
50
51
YBValue::YBValue(Data* d)
52
0
: data_(d) {
53
0
}
54
55
0
YBValue::~YBValue() {
56
0
  if (data_->type_ == Data::SLICE) {
57
0
    delete[] data_->slice_val_.data();
58
0
  }
59
0
  delete data_;
60
0
}
61
62
0
YBValue* YBValue::Clone() const {
63
0
  switch (data_->type_) {
64
0
    case Data::INT:
65
0
      return YBValue::FromInt(data_->int_val_);
66
0
    case Data::DOUBLE:
67
0
      return YBValue::FromDouble(data_->double_val_);
68
0
    case Data::FLOAT:
69
0
      return YBValue::FromFloat(data_->float_val_);
70
0
    case Data::SLICE:
71
0
      return YBValue::CopyString(data_->slice_val_);
72
0
  }
73
0
  LOG(FATAL);
74
0
}
75
76
0
YBValue* YBValue::FromInt(int64_t v) {
77
0
  auto d = new Data;
78
0
  d->type_ = Data::INT;
79
0
  d->int_val_ = v;
80
81
0
  return new YBValue(d);
82
0
}
83
84
0
YBValue* YBValue::FromDouble(double v) {
85
0
  auto d = new Data;
86
0
  d->type_ = Data::DOUBLE;
87
0
  d->double_val_ = v;
88
89
0
  return new YBValue(d);
90
0
}
91
92
93
0
YBValue* YBValue::FromFloat(float v) {
94
0
  auto d = new Data;
95
0
  d->type_ = Data::FLOAT;
96
0
  d->float_val_ = v;
97
98
0
  return new YBValue(d);
99
0
}
100
101
0
YBValue* YBValue::FromBool(bool v) {
102
0
  auto d = new Data;
103
0
  d->type_ = Data::INT;
104
0
  d->int_val_ = v ? 1 : 0;
105
106
0
  return new YBValue(d);
107
0
}
108
109
0
YBValue* YBValue::CopyString(Slice s) {
110
0
  auto copy = new uint8_t[s.size()];
111
0
  memcpy(copy, s.data(), s.size());
112
113
0
  auto d = new Data;
114
0
  d->type_ = Data::SLICE;
115
0
  d->slice_val_ = Slice(copy, s.size());
116
117
0
  return new YBValue(d);
118
0
}
119
120
Status YBValue::Data::CheckTypeAndGetPointer(const string& col_name,
121
                                             const shared_ptr<QLType>& tp,
122
0
                                             void** val_void) {
123
0
  const TypeInfo* ti = tp->type_info();
124
0
  switch (ti->physical_type()) {
125
0
    case yb::INT8:
126
0
    case yb::INT16:
127
0
    case yb::INT32:
128
0
    case yb::INT64:
129
0
      RETURN_NOT_OK(CheckAndPointToInt(col_name, ti->size(), val_void));
130
0
      break;
131
132
0
    case yb::BOOL:
133
0
      RETURN_NOT_OK(CheckAndPointToBool(col_name, val_void));
134
0
      break;
135
136
0
    case yb::FLOAT:
137
0
      RETURN_NOT_OK(CheckValType(col_name, YBValue::Data::FLOAT, "float"));
138
0
      *val_void = &float_val_;
139
0
      break;
140
141
0
    case yb::DOUBLE:
142
0
      RETURN_NOT_OK(CheckValType(col_name, YBValue::Data::DOUBLE, "double"));
143
0
      *val_void = &double_val_;
144
0
      break;
145
146
0
    case yb::BINARY:
147
0
      RETURN_NOT_OK(CheckAndPointToString(col_name, val_void));
148
0
      break;
149
150
0
    default:
151
0
      return STATUS(InvalidArgument, Substitute("cannot determine value for column $0 (type $1)",
152
0
                                                col_name, ti->name()));
153
0
  }
154
0
  return Status::OK();
155
0
}
156
157
Status YBValue::Data::CheckValType(const string& col_name,
158
                                     YBValue::Data::Type type,
159
0
                                     const char* type_str) const {
160
0
  if (type_ != type) {
161
0
    return STATUS(InvalidArgument,
162
0
        Substitute("non-$0 value for $0 column $1", type_str, col_name));
163
0
  }
164
0
  return Status::OK();
165
0
}
166
167
Status YBValue::Data::CheckAndPointToBool(const string& col_name,
168
0
                                            void** val_void) {
169
0
  RETURN_NOT_OK(CheckValType(col_name, YBValue::Data::INT, "bool"));
170
0
  int64_t int_val = int_val_;
171
0
  if (int_val != 0 && int_val != 1) {
172
0
    return STATUS(InvalidArgument,
173
0
        Substitute("value $0 out of range for boolean column '$1'",
174
0
                   int_val, col_name));
175
0
  }
176
0
  *val_void = &int_val_;
177
0
  return Status::OK();
178
0
}
179
180
Status YBValue::Data::CheckAndPointToInt(const string& col_name,
181
                                           size_t int_size,
182
0
                                           void** val_void) {
183
0
  RETURN_NOT_OK(CheckValType(col_name, YBValue::Data::INT, "int"));
184
185
0
  int64_t int_min, int_max;
186
0
  if (int_size == 8) {
187
0
    int_min = MathLimits<int64_t>::kMin;
188
0
    int_max = MathLimits<int64_t>::kMax;
189
0
  } else {
190
0
    size_t int_bits = int_size * 8 - 1;
191
0
    int_max = (1LL << int_bits) - 1;
192
0
    int_min = -int_max - 1;
193
0
  }
194
195
0
  int64_t int_val = int_val_;
196
0
  if (int_val < int_min || int_val > int_max) {
197
0
    return STATUS(InvalidArgument,
198
0
        Substitute("value $0 out of range for $1-bit signed integer column '$2'",
199
0
                   int_val, int_size * 8, col_name));
200
0
  }
201
202
0
  *val_void = &int_val_;
203
0
  return Status::OK();
204
0
}
205
206
Status YBValue::Data::CheckAndPointToString(const string& col_name,
207
0
                                              void** val_void) {
208
0
  RETURN_NOT_OK(CheckValType(col_name, YBValue::Data::SLICE, "string"));
209
0
  *val_void = &slice_val_;
210
0
  return Status::OK();
211
0
}
212
213
} // namespace client
214
} // namespace yb