OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ddl_utils::anonymous_namespace{DdlUtils.cpp} Namespace Reference

Functions

void validate_literal (const std::string &val, SQLTypeInfo column_type, const std::string &column_name)
 

Function Documentation

void ddl_utils::anonymous_namespace{DdlUtils.cpp}::validate_literal ( const std::string &  val,
SQLTypeInfo  column_type,
const std::string &  column_name 
)

Definition at line 523 of file DdlUtils.cpp.

References CHECK, convert_decimal_value_to_scale(), Geospatial::GeoTypesFactory::createGeoType(), SQLTypeInfo::get_elem_type(), SQLTypeInfo::get_size(), SQLTypeInfo::get_type(), kARRAY, kBIGINT, kBOOLEAN, kCHAR, kDATE, kDECIMAL, kDOUBLE, kFLOAT, kINT, Geospatial::GeoBase::kLINESTRING, kLINESTRING, Geospatial::GeoBase::kMULTILINESTRING, kMULTILINESTRING, Geospatial::GeoBase::kMULTIPOINT, kMULTIPOINT, Geospatial::GeoBase::kMULTIPOLYGON, kMULTIPOLYGON, kNUMERIC, Geospatial::GeoBase::kPOINT, kPOINT, Geospatial::GeoBase::kPOLYGON, kPOLYGON, kSMALLINT, kTEXT, kTIME, kTIMESTAMP, kTINYINT, kVARCHAR, StringDictionary::MAX_STRLEN, split(), StringToDatum(), to_string(), to_upper(), DecimalOverflowValidator::validate(), and DateDaysOverflowValidator::validate().

Referenced by ddl_utils::validate_and_set_default_value().

525  {
526  if (to_upper(val) == "NULL") {
527  return;
528  }
529  switch (column_type.get_type()) {
530  case kBOOLEAN:
531  case kTINYINT:
532  case kSMALLINT:
533  case kINT:
534  case kBIGINT:
535  case kFLOAT:
536  case kDOUBLE:
537  case kTIME:
538  case kTIMESTAMP:
539  StringToDatum(val, column_type);
540  break;
541  case kDATE: {
542  auto d = StringToDatum(val, column_type);
543  DateDaysOverflowValidator validator(column_type);
544  validator.validate(d.bigintval);
545  break;
546  }
547  case kDECIMAL:
548  case kNUMERIC: {
549  SQLTypeInfo ti(kNUMERIC, 0, 0, false);
550  auto d = StringToDatum(val, ti);
551  auto converted_val = convert_decimal_value_to_scale(d.bigintval, ti, column_type);
552  DecimalOverflowValidator validator(column_type);
553  validator.validate(converted_val);
554  break;
555  }
556  case kTEXT:
557  case kVARCHAR:
558  case kCHAR:
559  if (val.length() > StringDictionary::MAX_STRLEN) {
560  throw std::runtime_error("String too long for column " + column_name + " was " +
561  std::to_string(val.length()) + " max is " +
563  }
564  break;
565  case kARRAY: {
566  if (val.front() != '{' || val.back() != '}') {
567  throw std::runtime_error(column_name +
568  ": arrays should start and end with curly braces");
569  }
570  std::vector<std::string> elements = split(val.substr(1, val.length() - 2), ", ");
571  if (column_type.get_size() > 0) {
572  auto sti = column_type.get_elem_type();
573  size_t expected_size = column_type.get_size() / sti.get_size();
574  size_t actual_size = elements.size();
575  if (actual_size != expected_size) {
576  throw std::runtime_error("Fixed length array column " + column_name +
577  " expects " + std::to_string(expected_size) +
578  " values, received " + std::to_string(actual_size));
579  }
580  }
581  SQLTypeInfo element_ti = column_type.get_elem_type();
582  for (const auto& element : elements) {
583  if (to_upper(element) != "NULL") {
584  validate_literal(element, element_ti, column_name);
585  }
586  }
587  break;
588  }
589  case kPOINT:
590  case kMULTIPOINT:
591  case kLINESTRING:
592  case kMULTILINESTRING:
593  case kPOLYGON:
594  case kMULTIPOLYGON:
595  if (val.empty()) {
596  return;
597  }
598  try {
600  if (!geo) {
601  throw std::runtime_error("Unexpected geo literal '" + val + "' for column " +
602  column_name);
603  }
604  if (!geo->transform(column_type)) {
605  throw std::runtime_error("Cannot transform SRID for literal '" + val +
606  "' for column " + column_name);
607  } else {
608  auto sql_type = column_type.get_type();
609  auto geo_type = geo->getType();
610  if ((geo_type == Geospatial::GeoBase::GeoType::kPOINT && sql_type != kPOINT) ||
612  sql_type != kMULTIPOINT) ||
614  sql_type != kLINESTRING) ||
616  sql_type != kMULTILINESTRING) ||
618  sql_type != kPOLYGON) ||
620  sql_type != kMULTIPOLYGON)) {
621  throw std::runtime_error("Geo literal '" + val +
622  "' doesn't match the type "
623  "of column column " +
624  column_name);
625  }
626  }
627  } catch (Geospatial::GeoTypesError& e) {
628  throw std::runtime_error("Unexpected geo literal '" + val + "' for column " +
629  column_name + ": " + e.what());
630  }
631  break;
632  default:
633  CHECK(false) << "validate_literal() does not support type "
634  << column_type.get_type();
635  }
636 }
static std::unique_ptr< GeoBase > createGeoType(const std::string &wkt_or_wkb_hex)
Definition: Types.cpp:1061
HOST DEVICE int get_size() const
Definition: sqltypes.h:390
void validate_literal(const std::string &val, SQLTypeInfo column_type, const std::string &column_name)
Definition: DdlUtils.cpp:523
Definition: sqltypes.h:64
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:380
std::string to_string(char const *&&v)
std::vector< std::string > split(std::string_view str, std::string_view delim, std::optional< size_t > maxsplit)
split apart a string into a vector of substrings
Datum StringToDatum(const std::string_view s, SQLTypeInfo &ti)
Definition: Datum.cpp:337
std::string to_upper(const std::string &str)
Definition: sqltypes.h:67
Definition: sqltypes.h:68
int64_t convert_decimal_value_to_scale(const int64_t decimal_value, const SQLTypeInfo &type_info, const SQLTypeInfo &new_type_info)
Definition: Datum.cpp:622
Definition: sqltypes.h:56
#define CHECK(condition)
Definition: Logger.h:289
static constexpr size_t MAX_STRLEN
Definition: sqltypes.h:60
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:957

+ Here is the call graph for this function:

+ Here is the caller graph for this function: