OmniSciDB  95562058bd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Datum.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
25 #include <cassert>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <stdexcept>
29 #include <string>
30 
31 #include "DateConverters.h"
32 #include "DateTimeParser.h"
33 #include "Logger/Logger.h"
35 #include "StringTransform.h"
36 #include "misc.h"
37 #include "sqltypes.h"
38 
39 std::string SQLTypeInfo::type_name[kSQLTYPE_LAST] = {"NULL",
40  "BOOLEAN",
41  "CHAR",
42  "VARCHAR",
43  "NUMERIC",
44  "DECIMAL",
45  "INTEGER",
46  "SMALLINT",
47  "FLOAT",
48  "DOUBLE",
49  "TIME",
50  "TIMESTAMP",
51  "BIGINT",
52  "TEXT",
53  "DATE",
54  "ARRAY",
55  "INTERVAL_DAY_TIME",
56  "INTERVAL_YEAR_MONTH",
57  "POINT",
58  "LINESTRING",
59  "POLYGON",
60  "MULTIPOLYGON",
61  "TINYINT",
62  "GEOMETRY",
63  "GEOGRAPHY",
64  "EVAL_CONTEXT_TYPE",
65  "VOID",
66  "CURSOR",
67  "COLUMN"};
69  {"NONE", "FIXED", "RL", "DIFF", "DICT", "SPARSE", "COMPRESSED", "DAYS"};
70 
71 int64_t parse_numeric(const std::string_view s, SQLTypeInfo& ti) {
72  assert(s.length() <= 20);
73  size_t dot = s.find_first_of('.', 0);
74  std::string before_dot;
75  std::string after_dot;
76  if (dot != std::string::npos) {
77  // make .99 as 0.99, or std::stoll below throws exception 'std::invalid_argument'
78  before_dot = (0 == dot) ? "0" : s.substr(0, dot);
79  after_dot = s.substr(dot + 1);
80  } else {
81  before_dot = s;
82  after_dot = "0";
83  }
84  const bool is_negative = before_dot.find_first_of('-', 0) != std::string::npos;
85  const int64_t sign = is_negative ? -1 : 1;
86  int64_t result;
87  result = std::abs(std::stoll(before_dot));
88  int64_t fraction = 0;
89  const size_t before_dot_digits = before_dot.length() - (is_negative ? 1 : 0);
90  if (!after_dot.empty()) {
91  fraction = std::stoll(after_dot);
92  }
93  if (ti.get_dimension() == 0) {
94  // set the type info based on the literal string
95  ti.set_scale(static_cast<int>(after_dot.length()));
96  ti.set_dimension(static_cast<int>(before_dot_digits + ti.get_scale()));
97  ti.set_notnull(false);
98  } else {
99  CHECK_GE(ti.get_scale(), 0);
100  if (before_dot_digits + ti.get_scale() > static_cast<size_t>(ti.get_dimension())) {
101  throw std::runtime_error("numeric value " + std::string(s) +
102  " exceeds the maximum precision of " +
104  }
105  for (size_t i = static_cast<size_t>(ti.get_scale()); i < after_dot.length(); ++i) {
106  fraction /= 10; // truncate the digits after decimal point.
107  }
108  }
109  // the following loop can be made more efficient if needed
110  for (int i = 0; i < ti.get_scale(); i++) {
111  result *= 10;
112  }
113  if (result < 0) {
114  result -= fraction;
115  } else {
116  result += fraction;
117  }
118  return result * sign;
119 }
120 
121 /*
122  * @brief convert string to a datum
123  */
124 Datum StringToDatum(std::string_view s, SQLTypeInfo& ti) {
125  Datum d;
126  try {
127  switch (ti.get_type()) {
128  case kARRAY:
129  case kCOLUMN:
130  break;
131  case kBOOLEAN:
132  if (s == "t" || s == "T" || s == "1" || to_upper(std::string(s)) == "TRUE") {
133  d.boolval = true;
134  } else if (s == "f" || s == "F" || s == "0" ||
135  to_upper(std::string(s)) == "FALSE") {
136  d.boolval = false;
137  } else {
138  throw std::runtime_error("Invalid string for boolean " + std::string(s));
139  }
140  break;
141  case kNUMERIC:
142  case kDECIMAL:
143  d.bigintval = parse_numeric(s, ti);
144  break;
145  case kBIGINT:
146  d.bigintval = std::stoll(std::string(s));
147  break;
148  case kINT:
149  d.intval = std::stoi(std::string(s));
150  break;
151  case kSMALLINT:
152  d.smallintval = std::stoi(std::string(s));
153  break;
154  case kTINYINT:
155  d.tinyintval = std::stoi(std::string(s));
156  break;
157  case kFLOAT:
158  d.floatval = std::stof(std::string(s));
159  break;
160  case kDOUBLE:
161  d.doubleval = std::stod(std::string(s));
162  break;
163  case kTIME:
165  break;
166  case kTIMESTAMP:
168  break;
169  case kDATE:
171  break;
172  case kPOINT:
173  case kLINESTRING:
174  case kPOLYGON:
175  case kMULTIPOLYGON:
176  throw std::runtime_error("Internal error: geometry type in StringToDatum.");
177  default:
178  throw std::runtime_error("Internal error: invalid type in StringToDatum.");
179  }
180  } catch (const std::invalid_argument&) {
181  throw std::runtime_error("Invalid conversion from string to " + ti.get_type_name());
182  } catch (const std::out_of_range&) {
183  throw std::runtime_error("Got out of range error during conversion from string to " +
184  ti.get_type_name());
185  }
186  return d;
187 }
188 
189 bool DatumEqual(const Datum a, const Datum b, const SQLTypeInfo& ti) {
190  switch (ti.get_type()) {
191  case kBOOLEAN:
192  return a.boolval == b.boolval;
193  case kBIGINT:
194  case kNUMERIC:
195  case kDECIMAL:
196  return a.bigintval == b.bigintval;
197  case kINT:
198  return a.intval == b.intval;
199  case kSMALLINT:
200  return a.smallintval == b.smallintval;
201  case kTINYINT:
202  return a.tinyintval == b.tinyintval;
203  case kFLOAT:
204  return a.floatval == b.floatval;
205  case kDOUBLE:
206  return a.doubleval == b.doubleval;
207  case kTIME:
208  case kTIMESTAMP:
209  case kDATE:
210  case kINTERVAL_DAY_TIME:
212  return a.bigintval == b.bigintval;
213  case kTEXT:
214  case kVARCHAR:
215  case kCHAR:
216  case kPOINT:
217  case kLINESTRING:
218  case kPOLYGON:
219  case kMULTIPOLYGON:
220  if (ti.get_compression() == kENCODING_DICT) {
221  return a.intval == b.intval;
222  }
223  if (a.stringval == nullptr && b.stringval == nullptr) {
224  return true;
225  }
226  if (a.stringval == nullptr || b.stringval == nullptr) {
227  return false;
228  }
229  return *a.stringval == *b.stringval;
230  default:
231  return false;
232  }
233  return false;
234 }
235 
236 /*
237  * @brief convert datum to string
238  */
239 std::string DatumToString(Datum d, const SQLTypeInfo& ti) {
240  constexpr size_t buf_size = 64;
241  char buf[buf_size]; // Hold "2000-03-01 12:34:56.123456789" and large years.
242  switch (ti.get_type()) {
243  case kBOOLEAN:
244  if (d.boolval) {
245  return "t";
246  }
247  return "f";
248  case kNUMERIC:
249  case kDECIMAL: {
250  double v = (double)d.bigintval / pow(10, ti.get_scale());
251  int size = snprintf(buf, buf_size, "%*.*f", ti.get_dimension(), ti.get_scale(), v);
252  CHECK_LE(0, size) << v << ' ' << ti.to_string();
253  CHECK_LT(size_t(size), buf_size) << v << ' ' << ti.to_string();
254  return buf;
255  }
256  case kINT:
257  return std::to_string(d.intval);
258  case kSMALLINT:
259  return std::to_string(d.smallintval);
260  case kTINYINT:
261  return std::to_string(d.tinyintval);
262  case kBIGINT:
263  return std::to_string(d.bigintval);
264  case kFLOAT:
265  return std::to_string(d.floatval);
266  case kDOUBLE:
267  return std::to_string(d.doubleval);
268  case kTIME: {
269  size_t const len = shared::formatHMS(buf, buf_size, d.bigintval);
270  CHECK_EQ(8u, len); // 8 == strlen("HH:MM:SS")
271  return buf;
272  }
273  case kTIMESTAMP: {
274  unsigned const dim = ti.get_dimension(); // assumes dim <= 9
275  size_t const len = shared::formatDateTime(buf, buf_size, d.bigintval, dim);
276  CHECK_LE(19u + bool(dim) + dim, len); // 19 = strlen("YYYY-MM-DD HH:MM:SS")
277  return buf;
278  }
279  case kDATE: {
280  size_t const len = shared::formatDate(buf, buf_size, d.bigintval);
281  CHECK_LE(10u, len); // 10 == strlen("YYYY-MM-DD")
282  return buf;
283  }
284  case kINTERVAL_DAY_TIME:
285  return std::to_string(d.bigintval) + " ms (day-time interval)";
287  return std::to_string(d.bigintval) + " month(s) (year-month interval)";
288  case kTEXT:
289  case kVARCHAR:
290  case kCHAR:
291  if (d.stringval == nullptr) {
292  return "NULL";
293  }
294  return *d.stringval;
295  default:
296  throw std::runtime_error("Internal error: invalid type " + ti.get_type_name() +
297  " in DatumToString.");
298  }
299  return "";
300 }
301 
303  switch (ti.get_size()) {
304  case 1:
305  return kTINYINT;
306  case 2:
307  return kSMALLINT;
308  case 4:
309  return kINT;
310  case 8:
311  return kBIGINT;
312  default:
313  CHECK(false);
314  }
315  return kNULLT;
316 }
317 
318 int64_t convert_decimal_value_to_scale(const int64_t decimal_value,
319  const SQLTypeInfo& type_info,
320  const SQLTypeInfo& new_type_info) {
321  auto converted_decimal_value = decimal_value;
322  if (new_type_info.get_scale() > type_info.get_scale()) {
323  for (int i = 0; i < new_type_info.get_scale() - type_info.get_scale(); i++) {
324  converted_decimal_value *= 10;
325  }
326  } else if (new_type_info.get_scale() < type_info.get_scale()) {
327  for (int i = 0; i < type_info.get_scale() - new_type_info.get_scale(); i++) {
328  if (converted_decimal_value > 0) {
329  converted_decimal_value = (converted_decimal_value + 5) / 10;
330  } else {
331  converted_decimal_value = (converted_decimal_value - 5) / 10;
332  }
333  }
334  }
335  return converted_decimal_value;
336 }
int8_t tinyintval
Definition: sqltypes.h:135
#define CHECK_EQ(x, y)
Definition: Logger.h:205
HOST DEVICE int get_size() const
Definition: sqltypes.h:269
std::string DatumToString(Datum d, const SQLTypeInfo &ti)
Definition: Datum.cpp:239
Definition: sqltypes.h:51
SQLTypes
Definition: sqltypes.h:40
int64_t parse_numeric(const std::string_view s, SQLTypeInfo &ti)
Definition: Datum.cpp:71
bool boolval
Definition: sqltypes.h:134
HOST DEVICE int get_scale() const
Definition: sqltypes.h:264
int64_t dateTimeParse< kTIMESTAMP >(std::string_view str, unsigned const dim)
int64_t dateTimeParse< kDATE >(std::string_view str, unsigned const dim)
#define CHECK_GE(x, y)
Definition: Logger.h:210
size_t formatDateTime(char *buf, size_t const max, int64_t const timestamp, int const dimension)
Definition: misc.cpp:43
Constants for Builtin SQL Types supported by OmniSci.
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:259
int32_t intval
Definition: sqltypes.h:137
std::string to_string(char const *&&v)
size_t formatHMS(char *buf, size_t const max, int64_t const unixtime)
Definition: misc.cpp:80
float floatval
Definition: sqltypes.h:139
std::string to_string() const
Definition: sqltypes.h:395
bool DatumEqual(const Datum a, const Datum b, const SQLTypeInfo &ti)
Definition: Datum.cpp:189
static std::string type_name[kSQLTYPE_LAST]
Definition: sqltypes.h:666
void set_scale(int s)
Definition: sqltypes.h:354
int64_t bigintval
Definition: sqltypes.h:138
int16_t smallintval
Definition: sqltypes.h:136
Datum StringToDatum(std::string_view s, SQLTypeInfo &ti)
Definition: Datum.cpp:124
std::string * stringval
Definition: sqltypes.h:143
std::string to_upper(const std::string &str)
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:302
size_t formatDate(char *buf, size_t const max, int64_t const unixtime)
Definition: misc.cpp:25
#define CHECK_LT(x, y)
Definition: Logger.h:207
Definition: sqltypes.h:54
Definition: sqltypes.h:55
static std::string comp_name[kENCODING_LAST]
Definition: sqltypes.h:667
int64_t const int32_t sz assert(dest)
#define CHECK_LE(x, y)
Definition: Logger.h:208
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:267
int64_t convert_decimal_value_to_scale(const int64_t decimal_value, const SQLTypeInfo &type_info, const SQLTypeInfo &new_type_info)
Definition: Datum.cpp:318
void set_dimension(int d)
Definition: sqltypes.h:351
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:261
std::string get_type_name() const
Definition: sqltypes.h:362
int64_t dateTimeParse< kTIME >(std::string_view str, unsigned const dim)
Definition: sqltypes.h:43
void set_notnull(bool n)
Definition: sqltypes.h:356
#define CHECK(condition)
Definition: Logger.h:197
Definition: sqltypes.h:47
double doubleval
Definition: sqltypes.h:140