OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Datum.cpp File Reference
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <stdexcept>
#include <string>
#include "Logger.h"
#include "StringTransform.h"
#include "DateConverters.h"
#include "TimeGM.h"
#include "sqltypes.h"
+ Include dependency graph for Datum.cpp:

Go to the source code of this file.

Functions

int64_t parse_numeric (const std::string &s, SQLTypeInfo &ti)
 
Datum StringToDatum (const std::string &s, SQLTypeInfo &ti)
 
bool DatumEqual (const Datum a, const Datum b, const SQLTypeInfo &ti)
 
std::string DatumToString (Datum d, const SQLTypeInfo &ti)
 
SQLTypes decimal_to_int_type (const SQLTypeInfo &ti)
 
int64_t convert_decimal_value_to_scale (const int64_t decimal_value, const SQLTypeInfo &type_info, const SQLTypeInfo &new_type_info)
 

Function Documentation

int64_t convert_decimal_value_to_scale ( const int64_t  decimal_value,
const SQLTypeInfo type_info,
const SQLTypeInfo new_type_info 
)

Definition at line 284 of file Datum.cpp.

References SQLTypeInfoCore< TYPE_FACET_PACK >::get_scale().

Referenced by Importer_NS::TypedImportBuffer::add_value(), Analyzer::Constant::cast_number(), anonymous_namespace{ArrowImporter.h}::ArrowValue< Decimal128 >::operator const DATA_TYPE(), and anonymous_namespace{TypedDataAccessors.h}::put_scalar().

286  {
287  auto converted_decimal_value = decimal_value;
288  if (new_type_info.get_scale() > type_info.get_scale()) {
289  for (int i = 0; i < new_type_info.get_scale() - type_info.get_scale(); i++) {
290  converted_decimal_value *= 10;
291  }
292  } else if (new_type_info.get_scale() < type_info.get_scale()) {
293  for (int i = 0; i < type_info.get_scale() - new_type_info.get_scale(); i++) {
294  if (converted_decimal_value > 0) {
295  converted_decimal_value = (converted_decimal_value + 5) / 10;
296  } else {
297  converted_decimal_value = (converted_decimal_value - 5) / 10;
298  }
299  }
300  }
301  return converted_decimal_value;
302 }
HOST DEVICE int get_scale() const
Definition: sqltypes.h:331

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool DatumEqual ( const Datum  a,
const Datum  b,
const SQLTypeInfo ti 
)

Definition at line 153 of file Datum.cpp.

References Datum::bigintval, Datum::boolval, Datum::doubleval, Datum::floatval, SQLTypeInfoCore< TYPE_FACET_PACK >::get_compression(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_type(), Datum::intval, kBIGINT, kBOOLEAN, kCHAR, kDATE, kDECIMAL, kDOUBLE, kENCODING_DICT, kFLOAT, kINT, kINTERVAL_DAY_TIME, kINTERVAL_YEAR_MONTH, kNUMERIC, kSMALLINT, kTEXT, kTIME, kTIMESTAMP, kTINYINT, kVARCHAR, Datum::smallintval, Datum::stringval, and Datum::tinyintval.

Referenced by ChunkMetadata::operator==().

153  {
154  switch (ti.get_type()) {
155  case kBOOLEAN:
156  return a.boolval == b.boolval;
157  case kBIGINT:
158  case kNUMERIC:
159  case kDECIMAL:
160  return a.bigintval == b.bigintval;
161  case kINT:
162  return a.intval == b.intval;
163  case kSMALLINT:
164  return a.smallintval == b.smallintval;
165  case kTINYINT:
166  return a.tinyintval == b.tinyintval;
167  case kFLOAT:
168  return a.floatval == b.floatval;
169  case kDOUBLE:
170  return a.doubleval == b.doubleval;
171  case kTIME:
172  case kTIMESTAMP:
173  case kDATE:
174  case kINTERVAL_DAY_TIME:
176  return a.bigintval == b.bigintval;
177  case kTEXT:
178  case kVARCHAR:
179  case kCHAR:
180  if (ti.get_compression() == kENCODING_DICT) {
181  return a.intval == b.intval;
182  }
183  return *a.stringval == *b.stringval;
184  default:
185  return false;
186  }
187  return false;
188 }
int8_t tinyintval
Definition: sqltypes.h:126
Definition: sqltypes.h:52
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:334
bool boolval
Definition: sqltypes.h:125
int32_t intval
Definition: sqltypes.h:128
float floatval
Definition: sqltypes.h:130
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
int64_t bigintval
Definition: sqltypes.h:129
int16_t smallintval
Definition: sqltypes.h:127
std::string * stringval
Definition: sqltypes.h:134
Definition: sqltypes.h:55
Definition: sqltypes.h:56
Definition: sqltypes.h:44
Definition: sqltypes.h:48
double doubleval
Definition: sqltypes.h:131

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string DatumToString ( Datum  d,
const SQLTypeInfo ti 
)

Definition at line 193 of file Datum.cpp.

References Datum::bigintval, Datum::boolval, Datum::doubleval, Datum::floatval, SQLTypeInfoCore< TYPE_FACET_PACK >::get_dimension(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_scale(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_type(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_type_name(), Datum::intval, kBIGINT, kBOOLEAN, kCHAR, kDATE, kDECIMAL, kDOUBLE, kFLOAT, kINT, kINTERVAL_DAY_TIME, kINTERVAL_YEAR_MONTH, kNUMERIC, kSMALLINT, kTEXT, kTIME, kTIMESTAMP, kTINYINT, kVARCHAR, Datum::smallintval, Datum::stringval, Datum::tinyintval, and to_string().

Referenced by Analyzer::Constant::cast_to_string(), datum_to_string(), anonymous_namespace{ArrowImporter.h}::ArrowValue< int64_t >::operator const std::string(), Analyzer::Constant::toString(), and Fragmenter_Namespace::InsertOrderFragmenter::updateChunkStats().

193  {
194  switch (ti.get_type()) {
195  case kBOOLEAN:
196  if (d.boolval) {
197  return "t";
198  }
199  return "f";
200  case kNUMERIC:
201  case kDECIMAL: {
202  char str[ti.get_dimension() + 1];
203  double v = (double)d.bigintval / pow(10, ti.get_scale());
204  sprintf(str, "%*.*f", ti.get_dimension(), ti.get_scale(), v);
205  return std::string(str);
206  }
207  case kINT:
208  return std::to_string(d.intval);
209  case kSMALLINT:
210  return std::to_string(d.smallintval);
211  case kTINYINT:
212  return std::to_string(d.tinyintval);
213  case kBIGINT:
214  return std::to_string(d.bigintval);
215  case kFLOAT:
216  return std::to_string(d.floatval);
217  case kDOUBLE:
218  return std::to_string(d.doubleval);
219  case kTIME: {
220  std::tm tm_struct;
221  gmtime_r(reinterpret_cast<time_t*>(&d.bigintval), &tm_struct);
222  char buf[9];
223  strftime(buf, 9, "%T", &tm_struct);
224  return std::string(buf);
225  }
226  case kTIMESTAMP: {
227  std::tm tm_struct{0};
228  if (ti.get_dimension() > 0) {
229  std::string t = std::to_string(d.bigintval);
230  int cp = t.length() - ti.get_dimension();
231  time_t sec = std::stoll(t.substr(0, cp));
232  t = t.substr(cp);
233  gmtime_r(&sec, &tm_struct);
234  char buf[21];
235  strftime(buf, 21, "%F %T.", &tm_struct);
236  return std::string(buf) += t;
237  } else {
238  time_t sec = static_cast<time_t>(d.bigintval);
239  gmtime_r(&sec, &tm_struct);
240  char buf[20];
241  strftime(buf, 20, "%F %T", &tm_struct);
242  return std::string(buf);
243  }
244  }
245  case kDATE: {
246  std::tm tm_struct;
247  time_t ntimeval = static_cast<time_t>(d.bigintval);
248  gmtime_r(&ntimeval, &tm_struct);
249  char buf[11];
250  strftime(buf, 11, "%F", &tm_struct);
251  return std::string(buf);
252  }
253  case kINTERVAL_DAY_TIME:
254  return std::to_string(d.bigintval) + " ms (day-time interval)";
256  return std::to_string(d.bigintval) + " month(s) (year-month interval)";
257  case kTEXT:
258  case kVARCHAR:
259  case kCHAR:
260  return *d.stringval;
261  default:
262  throw std::runtime_error("Internal error: invalid type " + ti.get_type_name() +
263  " in DatumToString.");
264  }
265  return "";
266 }
int8_t tinyintval
Definition: sqltypes.h:126
Definition: sqltypes.h:52
bool boolval
Definition: sqltypes.h:125
HOST DEVICE int get_scale() const
Definition: sqltypes.h:331
int32_t intval
Definition: sqltypes.h:128
std::string to_string(char const *&&v)
std::string get_type_name() const
Definition: sqltypes.h:429
float floatval
Definition: sqltypes.h:130
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
int64_t bigintval
Definition: sqltypes.h:129
int16_t smallintval
Definition: sqltypes.h:127
std::string * stringval
Definition: sqltypes.h:134
Definition: sqltypes.h:55
Definition: sqltypes.h:56
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:328
Definition: sqltypes.h:44
Definition: sqltypes.h:48
double doubleval
Definition: sqltypes.h:131

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

SQLTypes decimal_to_int_type ( const SQLTypeInfo ti)

Definition at line 268 of file Datum.cpp.

References CHECK(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_size(), kBIGINT, kINT, kNULLT, kSMALLINT, and kTINYINT.

Referenced by Importer_NS::TypedImportBuffer::add_arrow_values(), Importer_NS::TypedImportBuffer::add_value(), CodeGenerator::codegen(), CodeGenerator::codegenIntConst(), anonymous_namespace{LogicalIR.cpp}::contains_unsafe_division(), Importer_NS::Loader::distributeToShards(), extract_from_datum(), anonymous_namespace{ColumnIR.cpp}::get_col_decoder(), CgenState::getOrAddLiteral(), ResultSet::makeTargetValue(), Importer_NS::NullArrayDatum(), Importer_NS::NullDatum(), Importer_NS::TypedImportBuffer::pop_value(), Importer_NS::TDatumToDatum(), anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::visitBinOper(), and anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::visitUOper().

268  {
269  switch (ti.get_size()) {
270  case 1:
271  return kTINYINT;
272  case 2:
273  return kSMALLINT;
274  case 4:
275  return kINT;
276  case 8:
277  return kBIGINT;
278  default:
279  CHECK(false);
280  }
281  return kNULLT;
282 }
HOST DEVICE int get_size() const
Definition: sqltypes.h:336
CHECK(cgen_state)
Definition: sqltypes.h:48

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int64_t parse_numeric ( const std::string &  s,
SQLTypeInfo ti 
)

Definition at line 37 of file Datum.cpp.

References assert(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_dimension(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_scale(), run_benchmark_import::result, SQLTypeInfoCore< TYPE_FACET_PACK >::set_dimension(), SQLTypeInfoCore< TYPE_FACET_PACK >::set_notnull(), SQLTypeInfoCore< TYPE_FACET_PACK >::set_scale(), and to_string().

Referenced by StringToDatum().

37  {
38  assert(s.length() <= 20);
39  size_t dot = s.find_first_of('.', 0);
40  std::string before_dot;
41  std::string after_dot;
42  if (dot != std::string::npos) {
43  // make .99 as 0.99, or std::stoll below throws exception 'std::invalid_argument'
44  before_dot = (0 == dot) ? "0" : s.substr(0, dot);
45  after_dot = s.substr(dot + 1);
46  } else {
47  before_dot = s;
48  after_dot = "0";
49  }
50  const bool is_negative = before_dot.find_first_of('-', 0) != std::string::npos;
51  const int64_t sign = is_negative ? -1 : 1;
52  int64_t result;
53  result = std::abs(std::stoll(before_dot));
54  int64_t fraction = 0;
55  const size_t before_dot_digits = before_dot.length() - (is_negative ? 1 : 0);
56  if (!after_dot.empty()) {
57  fraction = std::stoll(after_dot);
58  }
59  if (ti.get_dimension() == 0) {
60  // set the type info based on the literal string
61  ti.set_scale(after_dot.length());
62  ti.set_dimension(before_dot_digits + ti.get_scale());
63  ti.set_notnull(false);
64  } else {
65  if (before_dot_digits + ti.get_scale() > static_cast<size_t>(ti.get_dimension())) {
66  throw std::runtime_error("numeric value " + s +
67  " exceeds the maximum precision of " +
69  }
70  for (ssize_t i = 0; i < static_cast<ssize_t>(after_dot.length()) - ti.get_scale();
71  i++) {
72  fraction /= 10; // truncate the digits after decimal point.
73  }
74  }
75  // the following loop can be made more efficient if needed
76  for (int i = 0; i < ti.get_scale(); i++) {
77  result *= 10;
78  }
79  if (result < 0) {
80  result -= fraction;
81  } else {
82  result += fraction;
83  }
84  return result * sign;
85 }
HOST DEVICE int get_scale() const
Definition: sqltypes.h:331
void set_dimension(int d)
Definition: sqltypes.h:418
void set_scale(int s)
Definition: sqltypes.h:421
std::string to_string(char const *&&v)
void set_notnull(bool n)
Definition: sqltypes.h:423
int64_t const int32_t sz assert(dest)
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:328

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Datum StringToDatum ( const std::string &  s,
SQLTypeInfo ti 
)

Definition at line 90 of file Datum.cpp.

References Datum::bigintval, Datum::boolval, Datum::doubleval, Datum::floatval, SQLTypeInfoCore< TYPE_FACET_PACK >::get_dimension(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_type(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_type_name(), Datum::intval, kARRAY, kBIGINT, kBOOLEAN, kDATE, kDECIMAL, kDOUBLE, kFLOAT, kINT, kLINESTRING, kMULTIPOLYGON, kNUMERIC, kPOINT, kPOLYGON, kSMALLINT, kTIME, kTIMESTAMP, kTINYINT, parse_numeric(), Datum::smallintval, Datum::tinyintval, and to_upper().

Referenced by Importer_NS::TypedImportBuffer::add_value(), Parser::FixedPtLiteral::analyze(), Analyzer::Constant::cast_from_string(), anonymous_namespace{ArrowImporter.h}::ArrowValue< std::string >::operator const bool(), anonymous_namespace{ArrowImporter.h}::ArrowValue< std::string >::operator const DATA_TYPE(), populate_TColumn(), and Importer_NS::StringToArray().

90  {
91  Datum d;
92  try {
93  switch (ti.get_type()) {
94  case kARRAY:
95  break;
96  case kBOOLEAN:
97  if (s == "t" || s == "T" || s == "1" || to_upper(s) == "TRUE") {
98  d.boolval = true;
99  } else if (s == "f" || s == "F" || s == "0" || to_upper(s) == "FALSE") {
100  d.boolval = false;
101  } else {
102  throw std::runtime_error("Invalid string for boolean " + s);
103  }
104  break;
105  case kNUMERIC:
106  case kDECIMAL:
107  d.bigintval = parse_numeric(s, ti);
108  break;
109  case kBIGINT:
110  d.bigintval = std::stoll(s);
111  break;
112  case kINT:
113  d.intval = std::stoi(s);
114  break;
115  case kSMALLINT:
116  d.smallintval = std::stoi(s);
117  break;
118  case kTINYINT:
119  d.tinyintval = std::stoi(s);
120  break;
121  case kFLOAT:
122  d.floatval = std::stof(s);
123  break;
124  case kDOUBLE:
125  d.doubleval = std::stod(s);
126  break;
127  case kTIME:
129  break;
130  case kTIMESTAMP:
132  break;
133  case kDATE:
135  break;
136  case kPOINT:
137  case kLINESTRING:
138  case kPOLYGON:
139  case kMULTIPOLYGON:
140  throw std::runtime_error("Internal error: geometry type in StringToDatum.");
141  default:
142  throw std::runtime_error("Internal error: invalid type in StringToDatum.");
143  }
144  } catch (const std::invalid_argument&) {
145  throw std::runtime_error("Invalid conversion from string to " + ti.get_type_name());
146  } catch (const std::out_of_range&) {
147  throw std::runtime_error("Got out of range error during conversion from string to " +
148  ti.get_type_name());
149  }
150  return d;
151 }
int8_t tinyintval
Definition: sqltypes.h:126
Definition: sqltypes.h:52
bool boolval
Definition: sqltypes.h:125
int32_t intval
Definition: sqltypes.h:128
std::string get_type_name() const
Definition: sqltypes.h:429
int64_t parse_numeric(const std::string &s, SQLTypeInfo &ti)
Definition: Datum.cpp:37
float floatval
Definition: sqltypes.h:130
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
int64_t bigintval
Definition: sqltypes.h:129
int16_t smallintval
Definition: sqltypes.h:127
std::string to_upper(const std::string &str)
Definition: sqltypes.h:56
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:328
Definition: sqltypes.h:48
double doubleval
Definition: sqltypes.h:131

+ Here is the call graph for this function:

+ Here is the caller graph for this function: