OmniSciDB  a5dc49c757
 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 558 of file DdlUtils.cpp.

References CHECK, convert_decimal_value_to_scale(), Geospatial::GeoTypesFactory::createGeoType(), SQLTypeInfo::get_elem_type(), SQLTypeInfo::get_max_strlen(), 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, split(), StringToDatum(), to_string(), to_upper(), DecimalOverflowValidator::validate(), and DateDaysOverflowValidator::validate().

Referenced by ddl_utils::validate_and_set_default_value().

560  {
561  if (to_upper(val) == "NULL") {
562  return;
563  }
564  switch (column_type.get_type()) {
565  case kBOOLEAN:
566  case kTINYINT:
567  case kSMALLINT:
568  case kINT:
569  case kBIGINT:
570  case kFLOAT:
571  case kDOUBLE:
572  case kTIME:
573  case kTIMESTAMP:
574  StringToDatum(val, column_type);
575  break;
576  case kDATE: {
577  auto d = StringToDatum(val, column_type);
578  DateDaysOverflowValidator validator(column_type);
579  validator.validate(d.bigintval);
580  break;
581  }
582  case kDECIMAL:
583  case kNUMERIC: {
584  SQLTypeInfo ti(kNUMERIC, 0, 0, false);
585  auto d = StringToDatum(val, ti);
586  auto converted_val = convert_decimal_value_to_scale(d.bigintval, ti, column_type);
587  DecimalOverflowValidator validator(column_type);
588  validator.validate(converted_val);
589  break;
590  }
591  case kTEXT:
592  case kVARCHAR:
593  case kCHAR:
594  if (column_type.get_max_strlen() < val.length()) {
595  throw std::runtime_error("String too long for column " + column_name + " was " +
596  std::to_string(val.length()) + " max is " +
597  std::to_string(column_type.get_max_strlen()));
598  }
599  break;
600  case kARRAY: {
601  if (val.front() != '{' || val.back() != '}') {
602  throw std::runtime_error(column_name +
603  ": arrays should start and end with curly braces");
604  }
605  std::vector<std::string> elements = split(val.substr(1, val.length() - 2), ", ");
606  if (column_type.get_size() > 0) {
607  auto sti = column_type.get_elem_type();
608  size_t expected_size = column_type.get_size() / sti.get_size();
609  size_t actual_size = elements.size();
610  if (actual_size != expected_size) {
611  throw std::runtime_error("Fixed length array column " + column_name +
612  " expects " + std::to_string(expected_size) +
613  " values, received " + std::to_string(actual_size));
614  }
615  }
616  SQLTypeInfo element_ti = column_type.get_elem_type();
617  for (const auto& element : elements) {
618  if (to_upper(element) != "NULL") {
619  validate_literal(element, element_ti, column_name);
620  }
621  }
622  break;
623  }
624  case kPOINT:
625  case kMULTIPOINT:
626  case kLINESTRING:
627  case kMULTILINESTRING:
628  case kPOLYGON:
629  case kMULTIPOLYGON:
630  if (val.empty()) {
631  return;
632  }
633  try {
634  const bool validate_with_geos_if_available = false;
636  val, validate_with_geos_if_available);
637  if (!geo) {
638  throw std::runtime_error("Unexpected geo literal '" + val + "' for column " +
639  column_name);
640  }
641  if (!geo->transform(column_type)) {
642  throw std::runtime_error("Cannot transform SRID for literal '" + val +
643  "' for column " + column_name);
644  } else {
645  auto sql_type = column_type.get_type();
646  auto geo_type = geo->getType();
647  if ((geo_type == Geospatial::GeoBase::GeoType::kPOINT && sql_type != kPOINT) ||
649  sql_type != kMULTIPOINT) ||
651  sql_type != kLINESTRING) ||
653  sql_type != kMULTILINESTRING) ||
655  sql_type != kPOLYGON) ||
657  sql_type != kMULTIPOLYGON)) {
658  throw std::runtime_error("Geo literal '" + val +
659  "' doesn't match the type "
660  "of column column " +
661  column_name);
662  }
663  }
664  } catch (Geospatial::GeoTypesError& e) {
665  throw std::runtime_error("Unexpected geo literal '" + val + "' for column " +
666  column_name + ": " + e.what());
667  }
668  break;
669  default:
670  CHECK(false) << "validate_literal() does not support type "
671  << column_type.get_type();
672  }
673 }
HOST DEVICE int get_size() const
Definition: sqltypes.h:403
void validate_literal(const std::string &val, SQLTypeInfo column_type, const std::string &column_name)
Definition: DdlUtils.cpp:558
Definition: sqltypes.h:76
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:391
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:339
std::string to_upper(const std::string &str)
Definition: sqltypes.h:79
Definition: sqltypes.h:80
static std::unique_ptr< GeoBase > createGeoType(const std::string &wkt_or_wkb_hex, const bool validate_with_geos_if_available)
Definition: Types.cpp:1085
int64_t convert_decimal_value_to_scale(const int64_t decimal_value, const SQLTypeInfo &type_info, const SQLTypeInfo &new_type_info)
Definition: Datum.cpp:624
Definition: sqltypes.h:68
#define CHECK(condition)
Definition: Logger.h:291
Definition: sqltypes.h:72
HOST DEVICE size_t get_max_strlen() const
Definition: sqltypes.h:405
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:977

+ Here is the call graph for this function:

+ Here is the caller graph for this function: