OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Datum.cpp File Reference
#include <algorithm>
#include <cassert>
#include <cctype>
#include <charconv>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <stdexcept>
#include <string>
#include "DateConverters.h"
#include "DateTimeParser.h"
#include "Logger/Logger.h"
#include "QueryEngine/DateTimeUtils.h"
#include "StringTransform.h"
#include "misc.h"
#include "sqltypes.h"
+ Include dependency graph for Datum.cpp:

Go to the source code of this file.

Namespaces

 anonymous_namespace{Datum.cpp}
 

Functions

int64_t anonymous_namespace{Datum.cpp}::convert_decimal_value_to_scale_internal (const int64_t decimal_value, int const dscale)
 
int64_t parse_numeric (const std::string_view s, SQLTypeInfo &ti)
 
template<typename T >
anonymous_namespace{Datum.cpp}::minValue (unsigned const fieldsize)
 
template<typename T >
anonymous_namespace{Datum.cpp}::maxValue (unsigned const fieldsize)
 
std::string anonymous_namespace{Datum.cpp}::toString (SQLTypeInfo const &ti, unsigned const fieldsize)
 
template<typename T , typename U = long double>
anonymous_namespace{Datum.cpp}::parseFloatAsInteger (std::string_view s, SQLTypeInfo const &ti)
 
bool anonymous_namespace{Datum.cpp}::hasCommonSuffix (char const *const ptr, char const *const end)
 
template<typename T >
anonymous_namespace{Datum.cpp}::parseInteger (std::string_view s, SQLTypeInfo const &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 473 of file Datum.cpp.

References anonymous_namespace{Datum.cpp}::convert_decimal_value_to_scale_internal(), and SQLTypeInfo::get_scale().

Referenced by import_export::TypedImportBuffer::addDefaultValues(), Analyzer::Constant::cast_number(), anonymous_namespace{ArrowImporter.h}::ArrowValue< arrow::Decimal128 >::operator DATA_TYPE(), parse_numeric(), anonymous_namespace{TypedDataAccessors.h}::put_scalar(), and ddl_utils::anonymous_namespace{DdlUtils.cpp}::validate_literal().

475  {
476  int const dscale = new_type_info.get_scale() - type_info.get_scale();
477  return convert_decimal_value_to_scale_internal(decimal_value, dscale);
478 }
HOST DEVICE int get_scale() const
Definition: sqltypes.h:334
int64_t convert_decimal_value_to_scale_internal(const int64_t decimal_value, int const dscale)
Definition: Datum.cpp:77

+ 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 342 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, kLINESTRING, kMULTIPOLYGON, kNUMERIC, kPOINT, kPOLYGON, kSMALLINT, kTEXT, kTIME, kTIMESTAMP, kTINYINT, kVARCHAR, Datum::smallintval, Datum::stringval, and Datum::tinyintval.

Referenced by ChunkMetadata::operator==(), FixedLengthArrayNoneEncoder::resetChunkStats(), and ArrayNoneEncoder::resetChunkStats().

342  {
343  switch (ti.get_type()) {
344  case kBOOLEAN:
345  return a.boolval == b.boolval;
346  case kBIGINT:
347  case kNUMERIC:
348  case kDECIMAL:
349  return a.bigintval == b.bigintval;
350  case kINT:
351  return a.intval == b.intval;
352  case kSMALLINT:
353  return a.smallintval == b.smallintval;
354  case kTINYINT:
355  return a.tinyintval == b.tinyintval;
356  case kFLOAT:
357  return a.floatval == b.floatval;
358  case kDOUBLE:
359  return a.doubleval == b.doubleval;
360  case kTIME:
361  case kTIMESTAMP:
362  case kDATE:
363  case kINTERVAL_DAY_TIME:
365  return a.bigintval == b.bigintval;
366  case kTEXT:
367  case kVARCHAR:
368  case kCHAR:
369  case kPOINT:
370  case kLINESTRING:
371  case kPOLYGON:
372  case kMULTIPOLYGON:
373  if (ti.get_compression() == kENCODING_DICT) {
374  return a.intval == b.intval;
375  }
376  if (a.stringval == nullptr && b.stringval == nullptr) {
377  return true;
378  }
379  if (a.stringval == nullptr || b.stringval == nullptr) {
380  return false;
381  }
382  return *a.stringval == *b.stringval;
383  default:
384  return false;
385  }
386  return false;
387 }
int8_t tinyintval
Definition: sqltypes.h:212
Definition: sqltypes.h:49
int8_t boolval
Definition: sqltypes.h:211
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
int32_t intval
Definition: sqltypes.h:214
float floatval
Definition: sqltypes.h:216
int64_t bigintval
Definition: sqltypes.h:215
int16_t smallintval
Definition: sqltypes.h:213
std::string * stringval
Definition: sqltypes.h:220
Definition: sqltypes.h:52
Definition: sqltypes.h:53
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:337
Definition: sqltypes.h:41
Definition: sqltypes.h:45
double doubleval
Definition: sqltypes.h:217

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

References Datum::bigintval, Datum::boolval, CHECK_EQ, CHECK_LE, CHECK_LT, Datum::doubleval, Datum::floatval, shared::formatDate(), shared::formatDateTime(), shared::formatHMS(), 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, to_string(), and SQLTypeInfo::to_string().

Referenced by Analyzer::Constant::cast_to_string(), foreign_storage::datetime_to_string(), ChunkMetadata::dump(), anonymous_namespace{ArrowImporter.h}::ArrowValue< int64_t >::operator DATA_TYPE(), import_export::anonymous_namespace{QueryExporterCSV.cpp}::target_value_to_string(), Analyzer::Constant::toString(), Fragmenter_Namespace::InsertOrderFragmenter::updateChunkStats(), and ScalarExprToSql::visitConstant().

392  {
393  constexpr size_t buf_size = 64;
394  char buf[buf_size]; // Hold "2000-03-01 12:34:56.123456789" and large years.
395  switch (ti.get_type()) {
396  case kBOOLEAN:
397  if (d.boolval) {
398  return "t";
399  }
400  return "f";
401  case kNUMERIC:
402  case kDECIMAL: {
403  double v = (double)d.bigintval / pow(10, ti.get_scale());
404  int size = snprintf(buf, buf_size, "%*.*f", ti.get_dimension(), ti.get_scale(), v);
405  CHECK_LE(0, size) << v << ' ' << ti.to_string();
406  CHECK_LT(size_t(size), buf_size) << v << ' ' << ti.to_string();
407  return buf;
408  }
409  case kINT:
410  return std::to_string(d.intval);
411  case kSMALLINT:
412  return std::to_string(d.smallintval);
413  case kTINYINT:
414  return std::to_string(d.tinyintval);
415  case kBIGINT:
416  return std::to_string(d.bigintval);
417  case kFLOAT:
418  return std::to_string(d.floatval);
419  case kDOUBLE:
420  return std::to_string(d.doubleval);
421  case kTIME: {
422  size_t const len = shared::formatHMS(buf, buf_size, d.bigintval);
423  CHECK_EQ(8u, len); // 8 == strlen("HH:MM:SS")
424  return buf;
425  }
426  case kTIMESTAMP: {
427  unsigned const dim = ti.get_dimension(); // assumes dim <= 9
428  size_t const len = shared::formatDateTime(buf, buf_size, d.bigintval, dim);
429  CHECK_LE(19u + bool(dim) + dim, len); // 19 = strlen("YYYY-MM-DD HH:MM:SS")
430  return buf;
431  }
432  case kDATE: {
433  size_t const len = shared::formatDate(buf, buf_size, d.bigintval);
434  CHECK_LE(10u, len); // 10 == strlen("YYYY-MM-DD")
435  return buf;
436  }
437  case kINTERVAL_DAY_TIME:
438  return std::to_string(d.bigintval) + " ms (day-time interval)";
440  return std::to_string(d.bigintval) + " month(s) (year-month interval)";
441  case kTEXT:
442  case kVARCHAR:
443  case kCHAR:
444  if (d.stringval == nullptr) {
445  return "NULL";
446  }
447  return *d.stringval;
448  default:
449  throw std::runtime_error("Internal error: invalid type " + ti.get_type_name() +
450  " in DatumToString.");
451  }
452  return "";
453 }
int8_t tinyintval
Definition: sqltypes.h:212
#define CHECK_EQ(x, y)
Definition: Logger.h:217
Definition: sqltypes.h:49
HOST DEVICE int get_scale() const
Definition: sqltypes.h:334
int8_t boolval
Definition: sqltypes.h:211
size_t formatDateTime(char *buf, size_t const max, int64_t const timestamp, int const dimension)
Definition: misc.cpp:43
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
int32_t intval
Definition: sqltypes.h:214
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:216
std::string to_string() const
Definition: sqltypes.h:472
int64_t bigintval
Definition: sqltypes.h:215
int16_t smallintval
Definition: sqltypes.h:213
std::string * stringval
Definition: sqltypes.h:220
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:219
Definition: sqltypes.h:52
Definition: sqltypes.h:53
#define CHECK_LE(x, y)
Definition: Logger.h:220
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:331
std::string get_type_name() const
Definition: sqltypes.h:432
Definition: sqltypes.h:41
Definition: sqltypes.h:45
double doubleval
Definition: sqltypes.h:217

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

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

Referenced by import_export::TypedImportBuffer::add_value(), CodeGenerator::codegen(), CodeGenerator::codegenIntConst(), anonymous_namespace{LogicalIR.cpp}::contains_unsafe_division(), extract_from_datum(), import_export::Loader::fillShardRow(), anonymous_namespace{ColumnIR.cpp}::get_col_decoder(), CgenState::getOrAddLiteral(), 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().

455  {
456  switch (ti.get_size()) {
457  case 1:
458  return kTINYINT;
459  case 2:
460  return kSMALLINT;
461  case 4:
462  return kINT;
463  case 8:
464  return kBIGINT;
465  default:
466  CHECK(false);
467  }
468  return kNULLT;
469 }
HOST DEVICE int get_size() const
Definition: sqltypes.h:339
#define CHECK(condition)
Definition: Logger.h:209
Definition: sqltypes.h:45

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

References convert_decimal_value_to_scale(), anonymous_namespace{Datum.cpp}::convert_decimal_value_to_scale_internal(), SQLTypeInfo::get_dimension(), SQLTypeInfo::get_scale(), kNUMERIC, run_benchmark_import::result, SQLTypeInfo::set_dimension(), SQLTypeInfo::set_notnull(), and SQLTypeInfo::set_scale().

Referenced by StringToDatum().

108  {
109  // if we are given a dimension, first parse to the maximum precision of the string
110  // and then convert to the correct size
111  if (ti.get_dimension() != 0) {
112  SQLTypeInfo ti_string(kNUMERIC, 0, 0, false);
113  return convert_decimal_value_to_scale(parse_numeric(s, ti_string), ti_string, ti);
114  }
115  size_t dot = s.find_first_of('.', 0);
116  std::string before_dot;
117  std::string after_dot;
118  if (dot != std::string::npos) {
119  // make .99 as 0.99, or std::stoll below throws exception 'std::invalid_argument'
120  before_dot = (0 == dot) ? "0" : s.substr(0, dot);
121  after_dot = s.substr(dot + 1);
122  } else {
123  before_dot = s;
124  after_dot = "0";
125  }
126  const bool is_negative = before_dot.find_first_of('-', 0) != std::string::npos;
127  const int64_t sign = is_negative ? -1 : 1;
128  int64_t result;
129  result = std::abs(std::stoll(before_dot));
130  int64_t fraction = 0;
131  const size_t before_dot_digits = before_dot.length() - (is_negative ? 1 : 0);
132 
133  constexpr int max_digits = std::numeric_limits<int64_t>::digits10;
134  if (!after_dot.empty()) {
135  int64_t next_digit = 0;
136  // After dot will be used to scale integer part so make sure it wont overflow
137  if (after_dot.size() + before_dot_digits > max_digits) {
138  if (before_dot_digits >= max_digits) {
139  after_dot = "0";
140  } else {
141  next_digit = std::stoll(after_dot.substr(max_digits - before_dot_digits, 1));
142  after_dot = after_dot.substr(0, max_digits - before_dot_digits);
143  }
144  }
145  fraction = std::stoll(after_dot);
146  fraction += next_digit >= 5 ? 1 : 0;
147  }
148 
149  // set the type info based on the literal string
150  ti.set_scale(static_cast<int>(after_dot.length()));
151  ti.set_dimension(static_cast<int>(before_dot_digits + ti.get_scale()));
152  ti.set_notnull(false);
153  if (ti.get_scale()) {
154  result = convert_decimal_value_to_scale_internal(result, ti.get_scale());
155  }
156  result += fraction;
157 
158  return result * sign;
159 }
int64_t parse_numeric(const std::string_view s, SQLTypeInfo &ti)
Definition: Datum.cpp:108
HOST DEVICE int get_scale() const
Definition: sqltypes.h:334
void set_scale(int s)
Definition: sqltypes.h:424
int64_t convert_decimal_value_to_scale(const int64_t decimal_value, const SQLTypeInfo &type_info, const SQLTypeInfo &new_type_info)
Definition: Datum.cpp:473
void set_dimension(int d)
Definition: sqltypes.h:421
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:331
void set_notnull(bool n)
Definition: sqltypes.h:426
int64_t convert_decimal_value_to_scale_internal(const int64_t decimal_value, int const dscale)
Definition: Datum.cpp:77

+ 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 275 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, kCOLUMN, kCOLUMN_LIST, 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(), import_export::TypedImportBuffer::addDefaultValues(), Parser::FixedPtLiteral::analyze(), Analyzer::Constant::cast_from_string(), anonymous_namespace{ArrowImporter.h}::ArrowValue< std::string >::operator DATA_TYPE(), populate_TColumn(), import_export::StringToArray(), and ddl_utils::anonymous_namespace{DdlUtils.cpp}::validate_literal().

275  {
276  Datum d;
277  try {
278  switch (ti.get_type()) {
279  case kARRAY:
280  case kCOLUMN:
281  case kCOLUMN_LIST:
282  break;
283  case kBOOLEAN:
284  if (s == "t" || s == "T" || s == "1" || to_upper(std::string(s)) == "TRUE") {
285  d.boolval = true;
286  } else if (s == "f" || s == "F" || s == "0" ||
287  to_upper(std::string(s)) == "FALSE") {
288  d.boolval = false;
289  } else {
290  throw std::runtime_error("Invalid string for boolean " + std::string(s));
291  }
292  break;
293  case kNUMERIC:
294  case kDECIMAL:
295  d.bigintval = parse_numeric(s, ti);
296  break;
297  case kBIGINT:
298  d.bigintval = parseInteger<int64_t>(s, ti);
299  break;
300  case kINT:
301  d.intval = parseInteger<int32_t>(s, ti);
302  break;
303  case kSMALLINT:
304  d.smallintval = parseInteger<int16_t>(s, ti);
305  break;
306  case kTINYINT:
307  d.tinyintval = parseInteger<int8_t>(s, ti);
308  break;
309  case kFLOAT:
310  d.floatval = std::stof(std::string(s));
311  break;
312  case kDOUBLE:
313  d.doubleval = std::stod(std::string(s));
314  break;
315  case kTIME:
316  d.bigintval = dateTimeParse<kTIME>(s, ti.get_dimension());
317  break;
318  case kTIMESTAMP:
319  d.bigintval = dateTimeParse<kTIMESTAMP>(s, ti.get_dimension());
320  break;
321  case kDATE:
322  d.bigintval = dateTimeParse<kDATE>(s, ti.get_dimension());
323  break;
324  case kPOINT:
325  case kLINESTRING:
326  case kPOLYGON:
327  case kMULTIPOLYGON:
328  throw std::runtime_error("Internal error: geometry type in StringToDatum.");
329  default:
330  throw std::runtime_error("Internal error: invalid type in StringToDatum: " +
331  ti.get_type_name());
332  }
333  } catch (const std::invalid_argument&) {
334  throw std::runtime_error("Invalid conversion from string to " + ti.get_type_name());
335  } catch (const std::out_of_range&) {
336  throw std::runtime_error("Got out of range error during conversion from string to " +
337  ti.get_type_name());
338  }
339  return d;
340 }
int8_t tinyintval
Definition: sqltypes.h:212
Definition: sqltypes.h:49
int64_t parse_numeric(const std::string_view s, SQLTypeInfo &ti)
Definition: Datum.cpp:108
int8_t boolval
Definition: sqltypes.h:211
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
int32_t intval
Definition: sqltypes.h:214
float floatval
Definition: sqltypes.h:216
int64_t bigintval
Definition: sqltypes.h:215
int16_t smallintval
Definition: sqltypes.h:213
std::string to_upper(const std::string &str)
Definition: sqltypes.h:53
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:331
std::string get_type_name() const
Definition: sqltypes.h:432
Definition: sqltypes.h:45
double doubleval
Definition: sqltypes.h:217

+ Here is the call graph for this function:

+ Here is the caller graph for this function: