OmniSciDB  72180abbfe
 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_view s, SQLTypeInfo &ti)
 
Datum StringToDatum (std::string_view 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 319 of file Datum.cpp.

References SQLTypeInfo::get_scale().

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

321  {
322  auto converted_decimal_value = decimal_value;
323  if (new_type_info.get_scale() > type_info.get_scale()) {
324  for (int i = 0; i < new_type_info.get_scale() - type_info.get_scale(); i++) {
325  converted_decimal_value *= 10;
326  }
327  } else if (new_type_info.get_scale() < type_info.get_scale()) {
328  for (int i = 0; i < type_info.get_scale() - new_type_info.get_scale(); i++) {
329  if (converted_decimal_value > 0) {
330  converted_decimal_value = (converted_decimal_value + 5) / 10;
331  } else {
332  converted_decimal_value = (converted_decimal_value - 5) / 10;
333  }
334  }
335  }
336  return converted_decimal_value;
337 }
HOST DEVICE int get_scale() const
Definition: sqltypes.h:262

+ 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 187 of file Datum.cpp.

References Datum::bigintval, Datum::boolval, Datum::doubleval, Datum::floatval, SQLTypeInfo::get_compression(), SQLTypeInfo::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==().

187  {
188  switch (ti.get_type()) {
189  case kBOOLEAN:
190  return a.boolval == b.boolval;
191  case kBIGINT:
192  case kNUMERIC:
193  case kDECIMAL:
194  return a.bigintval == b.bigintval;
195  case kINT:
196  return a.intval == b.intval;
197  case kSMALLINT:
198  return a.smallintval == b.smallintval;
199  case kTINYINT:
200  return a.tinyintval == b.tinyintval;
201  case kFLOAT:
202  return a.floatval == b.floatval;
203  case kDOUBLE:
204  return a.doubleval == b.doubleval;
205  case kTIME:
206  case kTIMESTAMP:
207  case kDATE:
208  case kINTERVAL_DAY_TIME:
210  return a.bigintval == b.bigintval;
211  case kTEXT:
212  case kVARCHAR:
213  case kCHAR:
214  if (ti.get_compression() == kENCODING_DICT) {
215  return a.intval == b.intval;
216  }
217  return *a.stringval == *b.stringval;
218  default:
219  return false;
220  }
221  return false;
222 }
int8_t tinyintval
Definition: sqltypes.h:133
Definition: sqltypes.h:50
bool boolval
Definition: sqltypes.h:132
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:257
int32_t intval
Definition: sqltypes.h:135
float floatval
Definition: sqltypes.h:137
int64_t bigintval
Definition: sqltypes.h:136
int16_t smallintval
Definition: sqltypes.h:134
std::string * stringval
Definition: sqltypes.h:141
Definition: sqltypes.h:53
Definition: sqltypes.h:54
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:265
Definition: sqltypes.h:42
Definition: sqltypes.h:46
double doubleval
Definition: sqltypes.h:138

+ 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 227 of file Datum.cpp.

References Datum::bigintval, Datum::boolval, Datum::doubleval, Datum::floatval, SQLTypeInfo::get_dimension(), SQLTypeInfo::get_scale(), SQLTypeInfo::get_type(), SQLTypeInfo::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(), Fragmenter_Namespace::InsertOrderFragmenter::updateChunkStats(), and ScalarExprToSql::visitConstant().

227  {
228  switch (ti.get_type()) {
229  case kBOOLEAN:
230  if (d.boolval) {
231  return "t";
232  }
233  return "f";
234  case kNUMERIC:
235  case kDECIMAL: {
236  // we need to consider buf_size including the scale and null terminator
237  char str[ti.get_dimension() + ti.get_scale() + 2];
238  double v = (double)d.bigintval / pow(10, ti.get_scale());
239  sprintf(str, "%*.*f", ti.get_dimension(), ti.get_scale(), v);
240  return std::string(str);
241  }
242  case kINT:
243  return std::to_string(d.intval);
244  case kSMALLINT:
245  return std::to_string(d.smallintval);
246  case kTINYINT:
247  return std::to_string(d.tinyintval);
248  case kBIGINT:
249  return std::to_string(d.bigintval);
250  case kFLOAT:
251  return std::to_string(d.floatval);
252  case kDOUBLE:
253  return std::to_string(d.doubleval);
254  case kTIME: {
255  std::tm tm_struct;
256  gmtime_r(reinterpret_cast<time_t*>(&d.bigintval), &tm_struct);
257  char buf[9];
258  strftime(buf, 9, "%T", &tm_struct);
259  return std::string(buf);
260  }
261  case kTIMESTAMP: {
262  std::tm tm_struct{0};
263  if (ti.get_dimension() > 0) {
264  std::string t = std::to_string(d.bigintval);
265  int cp = std::max(static_cast<int>(t.length()) - ti.get_dimension(), 1);
266  time_t sec = std::stoll(t.substr(0, cp));
267  t = t.substr(cp);
268  gmtime_r(&sec, &tm_struct);
269  char buf[21];
270  strftime(buf, 21, "%F %T.", &tm_struct);
271  return std::string(buf) += t;
272  } else {
273  time_t sec = static_cast<time_t>(d.bigintval);
274  gmtime_r(&sec, &tm_struct);
275  char buf[20];
276  strftime(buf, 20, "%F %T", &tm_struct);
277  return std::string(buf);
278  }
279  }
280  case kDATE: {
281  std::tm tm_struct;
282  time_t ntimeval = static_cast<time_t>(d.bigintval);
283  gmtime_r(&ntimeval, &tm_struct);
284  char buf[11];
285  strftime(buf, 11, "%F", &tm_struct);
286  return std::string(buf);
287  }
288  case kINTERVAL_DAY_TIME:
289  return std::to_string(d.bigintval) + " ms (day-time interval)";
291  return std::to_string(d.bigintval) + " month(s) (year-month interval)";
292  case kTEXT:
293  case kVARCHAR:
294  case kCHAR:
295  return *d.stringval;
296  default:
297  throw std::runtime_error("Internal error: invalid type " + ti.get_type_name() +
298  " in DatumToString.");
299  }
300  return "";
301 }
int8_t tinyintval
Definition: sqltypes.h:133
Definition: sqltypes.h:50
bool boolval
Definition: sqltypes.h:132
HOST DEVICE int get_scale() const
Definition: sqltypes.h:262
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:257
int32_t intval
Definition: sqltypes.h:135
std::string to_string(char const *&&v)
float floatval
Definition: sqltypes.h:137
int64_t bigintval
Definition: sqltypes.h:136
int16_t smallintval
Definition: sqltypes.h:134
std::string * stringval
Definition: sqltypes.h:141
Definition: sqltypes.h:53
Definition: sqltypes.h:54
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:259
std::string get_type_name() const
Definition: sqltypes.h:360
Definition: sqltypes.h:42
Definition: sqltypes.h:46
double doubleval
Definition: sqltypes.h:138

+ 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 303 of file Datum.cpp.

References CHECK(), SQLTypeInfo::get_size(), kBIGINT, kINT, kNULLT, kSMALLINT, and kTINYINT.

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

303  {
304  switch (ti.get_size()) {
305  case 1:
306  return kTINYINT;
307  case 2:
308  return kSMALLINT;
309  case 4:
310  return kINT;
311  case 8:
312  return kBIGINT;
313  default:
314  CHECK(false);
315  }
316  return kNULLT;
317 }
HOST DEVICE int get_size() const
Definition: sqltypes.h:267
CHECK(cgen_state)
Definition: sqltypes.h:46

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int64_t parse_numeric ( const std::string_view  s,
SQLTypeInfo ti 
)

Definition at line 69 of file Datum.cpp.

References assert(), SQLTypeInfo::get_dimension(), SQLTypeInfo::get_scale(), run_benchmark_import::result, SQLTypeInfo::set_dimension(), SQLTypeInfo::set_notnull(), SQLTypeInfo::set_scale(), and to_string().

Referenced by StringToDatum().

69  {
70  assert(s.length() <= 20);
71  size_t dot = s.find_first_of('.', 0);
72  std::string before_dot;
73  std::string after_dot;
74  if (dot != std::string::npos) {
75  // make .99 as 0.99, or std::stoll below throws exception 'std::invalid_argument'
76  before_dot = (0 == dot) ? "0" : s.substr(0, dot);
77  after_dot = s.substr(dot + 1);
78  } else {
79  before_dot = s;
80  after_dot = "0";
81  }
82  const bool is_negative = before_dot.find_first_of('-', 0) != std::string::npos;
83  const int64_t sign = is_negative ? -1 : 1;
84  int64_t result;
85  result = std::abs(std::stoll(before_dot));
86  int64_t fraction = 0;
87  const size_t before_dot_digits = before_dot.length() - (is_negative ? 1 : 0);
88  if (!after_dot.empty()) {
89  fraction = std::stoll(after_dot);
90  }
91  if (ti.get_dimension() == 0) {
92  // set the type info based on the literal string
93  ti.set_scale(after_dot.length());
94  ti.set_dimension(before_dot_digits + ti.get_scale());
95  ti.set_notnull(false);
96  } else {
97  if (before_dot_digits + ti.get_scale() > static_cast<size_t>(ti.get_dimension())) {
98  throw std::runtime_error("numeric value " + std::string(s) +
99  " exceeds the maximum precision of " +
101  }
102  for (ssize_t i = 0; i < static_cast<ssize_t>(after_dot.length()) - ti.get_scale();
103  i++) {
104  fraction /= 10; // truncate the digits after decimal point.
105  }
106  }
107  // the following loop can be made more efficient if needed
108  for (int i = 0; i < ti.get_scale(); i++) {
109  result *= 10;
110  }
111  if (result < 0) {
112  result -= fraction;
113  } else {
114  result += fraction;
115  }
116  return result * sign;
117 }
HOST DEVICE int get_scale() const
Definition: sqltypes.h:262
std::string to_string(char const *&&v)
void set_scale(int s)
Definition: sqltypes.h:352
int64_t const int32_t sz assert(dest)
void set_dimension(int d)
Definition: sqltypes.h:349
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:259
void set_notnull(bool n)
Definition: sqltypes.h:354

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Datum StringToDatum ( std::string_view  s,
SQLTypeInfo ti 
)

Definition at line 122 of file Datum.cpp.

References Datum::bigintval, Datum::boolval, Datum::doubleval, Datum::floatval, SQLTypeInfo::get_dimension(), SQLTypeInfo::get_type(), SQLTypeInfo::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 import_export::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 import_export::StringToArray().

122  {
123  Datum d;
124  try {
125  switch (ti.get_type()) {
126  case kARRAY:
127  break;
128  case kBOOLEAN:
129  if (s == "t" || s == "T" || s == "1" || to_upper(std::string(s)) == "TRUE") {
130  d.boolval = true;
131  } else if (s == "f" || s == "F" || s == "0" ||
132  to_upper(std::string(s)) == "FALSE") {
133  d.boolval = false;
134  } else {
135  throw std::runtime_error("Invalid string for boolean " + std::string(s));
136  }
137  break;
138  case kNUMERIC:
139  case kDECIMAL:
140  d.bigintval = parse_numeric(s, ti);
141  break;
142  case kBIGINT:
143  d.bigintval = std::stoll(std::string(s));
144  break;
145  case kINT:
146  d.intval = std::stoi(std::string(s));
147  break;
148  case kSMALLINT:
149  d.smallintval = std::stoi(std::string(s));
150  break;
151  case kTINYINT:
152  d.tinyintval = std::stoi(std::string(s));
153  break;
154  case kFLOAT:
155  d.floatval = std::stof(std::string(s));
156  break;
157  case kDOUBLE:
158  d.doubleval = std::stod(std::string(s));
159  break;
160  case kTIME:
161  d.bigintval = DateTimeStringValidate<kTIME>()(std::string(s), ti.get_dimension());
162  break;
163  case kTIMESTAMP:
164  d.bigintval =
165  DateTimeStringValidate<kTIMESTAMP>()(std::string(s), ti.get_dimension());
166  break;
167  case kDATE:
168  d.bigintval = DateTimeStringValidate<kDATE>()(std::string(s), ti.get_dimension());
169  break;
170  case kPOINT:
171  case kLINESTRING:
172  case kPOLYGON:
173  case kMULTIPOLYGON:
174  throw std::runtime_error("Internal error: geometry type in StringToDatum.");
175  default:
176  throw std::runtime_error("Internal error: invalid type in StringToDatum.");
177  }
178  } catch (const std::invalid_argument&) {
179  throw std::runtime_error("Invalid conversion from string to " + ti.get_type_name());
180  } catch (const std::out_of_range&) {
181  throw std::runtime_error("Got out of range error during conversion from string to " +
182  ti.get_type_name());
183  }
184  return d;
185 }
int8_t tinyintval
Definition: sqltypes.h:133
Definition: sqltypes.h:50
int64_t parse_numeric(const std::string_view s, SQLTypeInfo &ti)
Definition: Datum.cpp:69
bool boolval
Definition: sqltypes.h:132
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:257
int32_t intval
Definition: sqltypes.h:135
float floatval
Definition: sqltypes.h:137
int64_t bigintval
Definition: sqltypes.h:136
int16_t smallintval
Definition: sqltypes.h:134
std::string to_upper(const std::string &str)
Definition: sqltypes.h:54
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:259
std::string get_type_name() const
Definition: sqltypes.h:360
Definition: sqltypes.h:46
double doubleval
Definition: sqltypes.h:138

+ Here is the call graph for this function:

+ Here is the caller graph for this function: