YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/pggate/pg_value.cc
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
#include "yb/yql/pggate/pg_value.h"
16
17
#include "yb/common/ql_value.h"
18
19
#include "yb/util/decimal.h"
20
#include "yb/util/status.h"
21
#include "yb/util/status_format.h"
22
23
#include "yb/yql/pggate/ybc_pg_typedefs.h"
24
25
namespace yb {
26
namespace pggate {
27
28
29
Status PgValueFromPB(const YBCPgTypeEntity *type_entity,
30
                     YBCPgTypeAttrs type_attrs,
31
                     const QLValuePB& ql_value,
32
                     uint64_t* datum,
33
57.4k
                     bool *is_null) {
34
35
  // Handling null values.
36
57.4k
  if (ql_value.value_case() == QLValuePB::VALUE_NOT_SET) {
37
0
    *is_null = true;
38
0
    *datum = 0;
39
0
    return Status::OK();
40
0
  }
41
42
57.4k
  *is_null = false;
43
57.4k
  switch (type_entity->yb_type) {
44
0
    case YB_YQL_DATA_TYPE_INT8: {
45
0
      SCHECK(ql_value.has_int8_value(), InternalError, "Unexpected type in the QL value");
46
0
      int8_t val = ql_value.int8_value();
47
0
      *datum = type_entity->yb_to_datum(reinterpret_cast<uint8_t *>(&val), 0, &type_attrs);
48
0
      break;
49
0
    }
50
51
0
    case YB_YQL_DATA_TYPE_INT16: {
52
0
      SCHECK(ql_value.has_int16_value(), InternalError, "Unexpected type in the QL value");
53
0
      int16_t val = ql_value.int16_value();
54
0
      *datum = type_entity->yb_to_datum(reinterpret_cast<uint8_t *>(&val), 0, &type_attrs);
55
0
      break;
56
0
    }
57
58.6k
    case YB_YQL_DATA_TYPE_INT32: {
58
58.6k
      SCHECK(ql_value.has_int32_value(), InternalError, "Unexpected type in the QL value");
59
58.6k
      int32_t val = ql_value.int32_value();
60
58.6k
      *datum = type_entity->yb_to_datum(reinterpret_cast<uint8_t *>(&val), 0, &type_attrs);
61
58.6k
      break;
62
58.6k
    }
63
64
3.92k
    case YB_YQL_DATA_TYPE_INT64: {
65
3.92k
      SCHECK(ql_value.has_int64_value(), InternalError, "Unexpected type in the QL value");
66
3.92k
      int64_t val = ql_value.int64_value();
67
3.92k
      *datum = type_entity->yb_to_datum(reinterpret_cast<uint8_t *>(&val), 0, &type_attrs);
68
3.92k
      break;
69
3.92k
    }
70
71
0
    case YB_YQL_DATA_TYPE_UINT32: {
72
0
      SCHECK(ql_value.has_uint32_value(), InternalError, "Unexpected type in the QL value");
73
0
      uint32_t val = ql_value.uint32_value();
74
0
      *datum = type_entity->yb_to_datum(reinterpret_cast<uint8_t *>(&val), 0, &type_attrs);
75
0
      break;
76
0
    }
77
78
0
    case YB_YQL_DATA_TYPE_UINT64: {
79
0
      SCHECK(ql_value.has_uint64_value(), InternalError, "Unexpected type in the QL value");
80
0
      uint64_t val = ql_value.uint64_value();
81
0
      *datum = type_entity->yb_to_datum(reinterpret_cast<uint8_t *>(&val), 0, &type_attrs);
82
0
      break;
83
0
    }
84
85
38
    case YB_YQL_DATA_TYPE_STRING: {
86
38
      SCHECK(ql_value.has_string_value(), InternalError, "Unexpected type in the QL value");
87
38
      auto size = ql_value.string_value().size();
88
38
      auto val = const_cast<char *>(ql_value.string_value().c_str());
89
38
      *datum = type_entity->yb_to_datum(reinterpret_cast<uint8_t *>(val), size, &type_attrs);
90
38
      break;
91
38
    }
92
93
0
    case YB_YQL_DATA_TYPE_BOOL: {
94
0
      SCHECK(ql_value.has_bool_value(), InternalError, "Unexpected type in the QL value");
95
0
      bool val = ql_value.bool_value();
96
0
      *datum = type_entity->yb_to_datum(reinterpret_cast<uint8_t *>(&val), 0, &type_attrs);
97
0
      break;
98
0
    }
99
100
0
    case YB_YQL_DATA_TYPE_FLOAT: {
101
0
      SCHECK(ql_value.has_float_value(), InternalError, "Unexpected type in the QL value");
102
0
      float val = ql_value.float_value();
103
0
      *datum = type_entity->yb_to_datum(reinterpret_cast<uint8_t *>(&val), 0, &type_attrs);
104
0
      break;
105
0
    }
106
107
0
    case YB_YQL_DATA_TYPE_DOUBLE: {
108
0
      SCHECK(ql_value.has_double_value(), InternalError, "Unexpected type in the QL value");
109
0
      double val = ql_value.double_value();
110
0
      *datum = type_entity->yb_to_datum(reinterpret_cast<uint8_t *>(&val), 0, &type_attrs);
111
0
      break;
112
0
    }
113
114
0
    case YB_YQL_DATA_TYPE_BINARY: {
115
0
      SCHECK(ql_value.has_binary_value(), InternalError, "Unexpected type in the QL value");
116
0
      auto size = ql_value.binary_value().size();
117
0
      auto val = const_cast<char *>(ql_value.binary_value().c_str());
118
0
      *datum = type_entity->yb_to_datum(reinterpret_cast<uint8_t *>(val), size, &type_attrs);
119
0
      break;
120
0
    }
121
122
0
    case YB_YQL_DATA_TYPE_TIMESTAMP: {
123
      // Timestamp encoded as int64 (QL) value.
124
0
      SCHECK(ql_value.has_int64_value(), InternalError, "Unexpected type in the QL value");
125
0
      int64_t val = ql_value.int64_value();
126
0
      *datum = type_entity->yb_to_datum(reinterpret_cast<uint8_t *>(&val), 0, &type_attrs);
127
0
      break;
128
0
    }
129
130
0
    case YB_YQL_DATA_TYPE_DECIMAL: {
131
0
      SCHECK(ql_value.has_decimal_value(), InternalError, "Unexpected type in the QL value");
132
0
      util::Decimal yb_decimal;
133
0
      if (!yb_decimal.DecodeFromComparable(ql_value.decimal_value()).ok()) {
134
0
        return STATUS_SUBSTITUTE(InternalError,
135
0
                                  "Failed to deserialize DECIMAL from $1",
136
0
                                  ql_value.decimal_value());
137
0
      }
138
0
      auto plaintext = yb_decimal.ToString();
139
0
      auto val = const_cast<char *>(plaintext.c_str());
140
0
      *datum = type_entity->yb_to_datum(reinterpret_cast<uint8_t *>(val),
141
0
                                        plaintext.size(),
142
0
                                        &type_attrs);
143
0
      break;
144
0
    }
145
146
0
    case YB_YQL_DATA_TYPE_GIN_NULL: {
147
0
      SCHECK(ql_value.has_gin_null_value(), InternalError, "Unexpected type in the QL value");
148
0
      uint8_t val = ql_value.gin_null_value();
149
0
      *datum = type_entity->yb_to_datum(&val, 0, &type_attrs);
150
0
      break;
151
0
    }
152
153
0
    YB_PG_UNSUPPORTED_TYPES_IN_SWITCH:
154
0
    YB_PG_INVALID_TYPES_IN_SWITCH:
155
0
      return STATUS_SUBSTITUTE(InternalError, "unsupported type $0", type_entity->yb_type);
156
56.8k
  }
157
158
56.8k
  return Status::OK();
159
56.8k
}
160
161
162
Status PgValueToPB(const YBCPgTypeEntity *type_entity,
163
                   uint64_t datum,
164
                   bool is_null,
165
5.74k
                   QLValue* ql_value) {
166
5.74k
  if (is_null) {
167
0
    ql_value->SetNull();
168
0
    return Status::OK();
169
0
  }
170
171
5.74k
  switch (type_entity->yb_type) {
172
0
    case YB_YQL_DATA_TYPE_INT8: {
173
0
      int8_t value;
174
0
      type_entity->datum_to_yb(datum, &value, nullptr);
175
0
      ql_value->set_int8_value(value);
176
0
      break;
177
0
    }
178
0
    case YB_YQL_DATA_TYPE_INT16: {
179
0
      int16_t value;
180
0
      type_entity->datum_to_yb(datum, &value, nullptr);
181
0
      ql_value->set_int16_value(value);
182
0
      break;
183
0
    }
184
0
    case YB_YQL_DATA_TYPE_INT32: {
185
0
      int32_t value;
186
0
      type_entity->datum_to_yb(datum, &value, nullptr);
187
0
      ql_value->set_int32_value(value);
188
0
      break;
189
0
    }
190
5.74k
    case YB_YQL_DATA_TYPE_INT64: {
191
5.74k
      int64_t value;
192
5.74k
      type_entity->datum_to_yb(datum, &value, nullptr);
193
5.74k
      ql_value->set_int64_value(value);
194
5.74k
      break;
195
0
    }
196
0
    case YB_YQL_DATA_TYPE_UINT32: {
197
0
      uint32_t value;
198
0
      type_entity->datum_to_yb(datum, &value, nullptr);
199
0
      ql_value->set_uint32_value(value);
200
0
      break;
201
0
    }
202
0
    case YB_YQL_DATA_TYPE_UINT64: {
203
0
      uint64_t value;
204
0
      type_entity->datum_to_yb(datum, &value, nullptr);
205
0
      ql_value->set_uint64_value(value);
206
0
      break;
207
0
    }
208
0
    case YB_YQL_DATA_TYPE_STRING: {
209
0
      char *value;
210
0
      int64_t bytes = type_entity->datum_fixed_size;
211
0
      type_entity->datum_to_yb(datum, &value, &bytes);
212
0
      ql_value->set_string_value(value, bytes);
213
0
      break;
214
0
    }
215
0
    case YB_YQL_DATA_TYPE_BOOL: {
216
0
      bool value;
217
0
      type_entity->datum_to_yb(datum, &value, nullptr);
218
0
      ql_value->set_bool_value(value);
219
0
      break;
220
0
    }
221
0
    case YB_YQL_DATA_TYPE_FLOAT: {
222
0
      float value;
223
0
      type_entity->datum_to_yb(datum, &value, nullptr);
224
0
      ql_value->set_float_value(value);
225
0
      break;
226
0
    }
227
0
    case YB_YQL_DATA_TYPE_DOUBLE: {
228
0
      double value;
229
0
      type_entity->datum_to_yb(datum, &value, nullptr);
230
0
      ql_value->set_double_value(value);
231
0
      break;
232
0
    }
233
0
    case YB_YQL_DATA_TYPE_BINARY: {
234
0
      uint8_t *value;
235
0
      int64_t bytes = type_entity->datum_fixed_size;
236
0
      type_entity->datum_to_yb(datum, &value, &bytes);
237
0
      ql_value->set_binary_value(value, bytes);
238
0
      break;
239
0
    }
240
0
    case YB_YQL_DATA_TYPE_TIMESTAMP: {
241
0
      int64_t value;
242
0
      type_entity->datum_to_yb(datum, &value, nullptr);
243
0
      ql_value->set_int64_value(value);
244
0
      break;
245
0
    }
246
0
    case YB_YQL_DATA_TYPE_DECIMAL: {
247
0
      char* plaintext;
248
0
      type_entity->datum_to_yb(datum, &plaintext, nullptr);
249
0
      util::Decimal yb_decimal(plaintext);
250
0
      ql_value->set_decimal_value(yb_decimal.EncodeToComparable());
251
0
      break;
252
0
    }
253
0
    case YB_YQL_DATA_TYPE_GIN_NULL: {
254
0
      uint8_t value;
255
0
      type_entity->datum_to_yb(datum, &value, nullptr);
256
0
      ql_value->set_gin_null_value(value);
257
0
      break;
258
0
    }
259
0
    YB_PG_UNSUPPORTED_TYPES_IN_SWITCH:
260
0
    YB_PG_INVALID_TYPES_IN_SWITCH:
261
0
      return STATUS_SUBSTITUTE(InternalError, "unsupported type $0", type_entity->yb_type);
262
5.74k
  }
263
5.74k
  return Status::OK();
264
5.74k
}
265
266
}  // namespace pggate
267
}  // namespace yb