OmniSciDB  a667adc9c8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
anonymous_namespace{Datum.cpp} Namespace Reference

Functions

template<typename T >
minValue (unsigned const fieldsize)
 
template<typename T >
maxValue (unsigned const fieldsize)
 
std::string toString (SQLTypeInfo const &ti, unsigned const fieldsize)
 
template<typename T , typename U = long double>
parseFloatAsInteger (std::string_view s, SQLTypeInfo const &ti)
 
bool hasCommonSuffix (char const *const ptr, char const *const end)
 
template<typename T >
parseInteger (std::string_view s, SQLTypeInfo const &ti)
 

Function Documentation

bool anonymous_namespace{Datum.cpp}::hasCommonSuffix ( char const *const  ptr,
char const *const  end 
)
inline

Definition at line 186 of file Datum.cpp.

Referenced by parseInteger().

186  {
187  return *ptr == '.' && (ptr + 1 == end || (ptr[1] == '0' && ptr + 2 == end));
188 }

+ Here is the caller graph for this function:

template<typename T >
T anonymous_namespace{Datum.cpp}::maxValue ( unsigned const  fieldsize)

Definition at line 135 of file Datum.cpp.

135  {
136  return ~minValue<T>(fieldsize);
137 }
template<typename T >
T anonymous_namespace{Datum.cpp}::minValue ( unsigned const  fieldsize)

Definition at line 129 of file Datum.cpp.

References omnisci.dtypes::T.

129  {
130  static_assert(std::is_signed_v<T>);
131  return T(-1) << (fieldsize - 1);
132 }
template<typename T , typename U = long double>
T anonymous_namespace{Datum.cpp}::parseFloatAsInteger ( std::string_view  s,
SQLTypeInfo const ti 
)

Definition at line 149 of file Datum.cpp.

References SQLTypeInfo::get_type_name(), and omnisci.dtypes::T.

149  {
150  // Use stack memory if s is small enough before resorting to dynamic memory.
151  constexpr size_t bufsize = 64;
152  char c_str[bufsize];
153  std::string str;
154  char const* str_begin;
155  char* str_end;
156  if (s.size() < bufsize) {
157  s.copy(c_str, s.size());
158  c_str[s.size()] = '\0';
159  str_begin = c_str;
160  } else {
161  str = s;
162  str_begin = str.c_str();
163  }
164  U value = strtold(str_begin, &str_end);
165  if (str_begin == str_end) {
166  throw std::runtime_error("Unable to parse " + std::string(s) + " to " +
167  ti.get_type_name());
168  } else if (str_begin + s.size() != str_end) {
169  throw std::runtime_error(std::string("Unexpected character \"") + *str_end +
170  "\" encountered in " + ti.get_type_name() + " value " +
171  std::string(s));
172  }
173  value = std::round(value);
174  if (!std::isfinite(value)) {
175  throw std::runtime_error("Invalid conversion from \"" + std::string(s) + "\" to " +
176  ti.get_type_name());
177  } else if (value < static_cast<U>(std::numeric_limits<T>::min()) ||
178  static_cast<U>(std::numeric_limits<T>::max()) < value) {
179  throw std::runtime_error("Integer " + std::string(s) + " is out of range for " +
180  ti.get_type_name());
181  }
182  return static_cast<T>(value);
183 }

+ Here is the call graph for this function:

template<typename T >
T anonymous_namespace{Datum.cpp}::parseInteger ( std::string_view  s,
SQLTypeInfo const ti 
)

Definition at line 191 of file Datum.cpp.

References SQLTypeInfo::get_comp_param(), SQLTypeInfo::get_compression(), SQLTypeInfo::get_notnull(), SQLTypeInfo::get_type_name(), hasCommonSuffix(), kENCODING_FIXED, omnisci.dtypes::T, and toString().

191  {
192  T retval{0};
193  char const* const end = s.data() + s.size();
194  auto [ptr, error_code] = std::from_chars(s.data(), end, retval);
195  if (ptr != end) {
196  if (error_code != std::errc() || !hasCommonSuffix(ptr, end)) {
197  retval = parseFloatAsInteger<T>(s, ti);
198  }
199  } else if (error_code != std::errc()) {
200  if (error_code == std::errc::result_out_of_range) {
201  throw std::runtime_error("Integer " + std::string(s) + " is out of range for " +
202  ti.get_type_name());
203  }
204  throw std::runtime_error("Invalid conversion from \"" + std::string(s) + "\" to " +
205  ti.get_type_name());
206  }
207  // Bounds checking based on SQLTypeInfo.
208  unsigned const fieldsize =
209  ti.get_compression() == kENCODING_FIXED ? ti.get_comp_param() : 8 * sizeof(T);
210  if (fieldsize < 8 * sizeof(T)) {
211  if (maxValue<T>(fieldsize) < retval) {
212  throw std::runtime_error("Integer " + std::string(s) +
213  " exceeds maximum value for " + toString(ti, fieldsize));
214  } else if (ti.get_notnull()) {
215  if (retval < minValue<T>(fieldsize)) {
216  throw std::runtime_error("Integer " + std::string(s) +
217  " exceeds minimum value for " + toString(ti, fieldsize));
218  }
219  } else {
220  if (retval <= minValue<T>(fieldsize)) {
221  throw std::runtime_error("Integer " + std::string(s) +
222  " exceeds minimum value for nullable " +
223  toString(ti, fieldsize));
224  }
225  }
226  } else if (!ti.get_notnull() && retval == std::numeric_limits<T>::min()) {
227  throw std::runtime_error("Integer " + std::string(s) +
228  " exceeds minimum value for nullable " +
229  toString(ti, fieldsize));
230  }
231  return retval;
232 }
std::string toString(const ExtArgumentType &sig_type)
bool hasCommonSuffix(char const *const ptr, char const *const end)
Definition: Datum.cpp:186

+ Here is the call graph for this function:

std::string anonymous_namespace{Datum.cpp}::toString ( SQLTypeInfo const ti,
unsigned const  fieldsize 
)

Definition at line 139 of file Datum.cpp.

References SQLTypeInfo::get_type_name(), and to_string().

139  {
140  return ti.get_type_name() + '(' + std::to_string(fieldsize) + ')';
141 }
std::string to_string(char const *&&v)

+ Here is the call graph for this function: