OmniSciDB  471d68cefb
 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 513 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::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().

515  {
516  if (to_upper(val) == "NULL") {
517  return;
518  }
519  switch (column_type.get_type()) {
520  case kBOOLEAN:
521  case kTINYINT:
522  case kSMALLINT:
523  case kINT:
524  case kBIGINT:
525  case kFLOAT:
526  case kDOUBLE:
527  case kTIME:
528  case kTIMESTAMP:
529  StringToDatum(val, column_type);
530  break;
531  case kDATE: {
532  auto d = StringToDatum(val, column_type);
533  DateDaysOverflowValidator validator(column_type);
534  validator.validate(d.bigintval);
535  break;
536  }
537  case kDECIMAL:
538  case kNUMERIC: {
539  SQLTypeInfo ti(kNUMERIC, 0, 0, false);
540  auto d = StringToDatum(val, ti);
541  auto converted_val = convert_decimal_value_to_scale(d.bigintval, ti, column_type);
542  DecimalOverflowValidator validator(column_type);
543  validator.validate(converted_val);
544  break;
545  }
546  case kTEXT:
547  case kVARCHAR:
548  case kCHAR:
549  if (val.length() > StringDictionary::MAX_STRLEN) {
550  throw std::runtime_error("String too long for column " + column_name + " was " +
551  std::to_string(val.length()) + " max is " +
553  }
554  break;
555  case kARRAY: {
556  if (val.front() != '{' || val.back() != '}') {
557  throw std::runtime_error(column_name +
558  ": arrays should start and end with curly braces");
559  }
560  std::vector<std::string> elements = split(val.substr(1, val.length() - 2), ", ");
561  if (column_type.get_size() > 0) {
562  auto sti = column_type.get_elem_type();
563  size_t expected_size = column_type.get_size() / sti.get_size();
564  size_t actual_size = elements.size();
565  if (actual_size != expected_size) {
566  throw std::runtime_error("Fixed length array column " + column_name +
567  " expects " + std::to_string(expected_size) +
568  " values, received " + std::to_string(actual_size));
569  }
570  }
571  SQLTypeInfo element_ti = column_type.get_elem_type();
572  for (const auto& element : elements) {
573  if (to_upper(element) != "NULL") {
574  validate_literal(element, element_ti, column_name);
575  }
576  }
577  break;
578  }
579  case kPOINT:
580  case kLINESTRING:
581  case kPOLYGON:
582  case kMULTIPOLYGON:
583  if (val.empty()) {
584  return;
585  }
586  try {
588  if (!geo) {
589  throw std::runtime_error("Unexpected geo literal '" + val + "' for column " +
590  column_name);
591  }
592  if (!geo->transform(column_type)) {
593  throw std::runtime_error("Cannot transform SRID for literal '" + val +
594  "' for column " + column_name);
595  } else {
596  auto sql_type = column_type.get_type();
597  auto geo_type = geo->getType();
598  if ((geo_type == Geospatial::GeoBase::GeoType::kPOINT && sql_type != kPOINT) ||
600  sql_type != kLINESTRING) ||
602  sql_type != kPOLYGON) ||
604  sql_type != kMULTIPOLYGON)) {
605  throw std::runtime_error("Geo literal '" + val +
606  "' doesn't match the type "
607  "of column column " +
608  column_name);
609  }
610  }
611  } catch (Geospatial::GeoTypesError& e) {
612  throw std::runtime_error("Unexpected geo literal '" + val + "' for column " +
613  column_name + ": " + e.what());
614  }
615  break;
616  default:
617  CHECK(false) << "validate_literal() does not support type "
618  << column_type.get_type();
619  }
620 }
static std::unique_ptr< GeoBase > createGeoType(const std::string &wkt_or_wkb_hex)
Definition: Types.cpp:919
HOST DEVICE int get_size() const
Definition: sqltypes.h:339
void validate_literal(const std::string &val, SQLTypeInfo column_type, const std::string &column_name)
Definition: DdlUtils.cpp:513
Definition: sqltypes.h:49
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
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(std::string_view s, SQLTypeInfo &ti)
Definition: Datum.cpp:275
std::string to_upper(const std::string &str)
Definition: sqltypes.h:52
Definition: sqltypes.h:53
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
Definition: sqltypes.h:41
#define CHECK(condition)
Definition: Logger.h:209
static constexpr size_t MAX_STRLEN
Definition: sqltypes.h:45
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:850

+ Here is the call graph for this function:

+ Here is the caller graph for this function: