Convert a string to an interval object.
123138 {
123141 bool negative;
123142 bool found_any = false;
123145 DatePartSpecifier specifier;
123146 string specifier_str;
123147
123148 result.days = 0;
123149 result.micros = 0;
123150 result.months = 0;
123151
123152 if (len == 0) {
123153 return false;
123154 }
123155
123156 switch (str[pos]) {
123157 case '@':
123158 pos++;
123159 goto standard_interval;
123160 case 'P':
123161 case 'p':
123162 pos++;
123163 goto posix_interval;
123164 default:
123165 goto standard_interval;
123166 }
123167standard_interval:
123168
123169 for (; pos < len; pos++) {
123170 char c = str[pos];
123171 if (c == ' ' || c == '\t' || c == '\n') {
123172
123173 continue;
123174 } else if (c >= '0' && c <= '9') {
123175
123176 negative = false;
123177 goto interval_parse_number;
123178 } else if (c == '-') {
123179
123180 negative = true;
123181 pos++;
123182 goto interval_parse_number;
123183 } else if (c == 'a' || c == 'A') {
123184
123185 goto interval_parse_ago;
123186 } else {
123187
123188 return false;
123189 }
123190 }
123191 goto end_of_string;
123192interval_parse_number:
123193 start_pos = pos;
123194 for (; pos < len; pos++) {
123195 char c = str[pos];
123196 if (c >= '0' && c <= '9') {
123197
123198 continue;
123199 } else if (c == ':') {
123200
123201 goto interval_parse_time;
123202 } else {
123203 if (pos == start_pos) {
123204 return false;
123205 }
123206
123207 string_t nr_string(str + start_pos, UnsafeNumericCast<uint32_t>(pos - start_pos));
123208 number = Cast::Operation<string_t, int64_t>(nr_string);
123209 fraction = 0;
123210 if (c == '.') {
123211
123213 for (++pos; pos < len && StringUtil::CharacterIsDigit(str[pos]); ++pos, mult /= 10) {
123214 if (mult > 0) {
123215 fraction +=
int64_t(str[pos] -
'0') * mult;
123216 }
123217 }
123218 }
123219 if (negative) {
123220 number = -number;
123221 fraction = -fraction;
123222 }
123223 goto interval_parse_identifier;
123224 }
123225 }
123226 goto end_of_string;
123227interval_parse_time : {
123228
123229 dtime_t time;
123231 if (!Time::TryConvertInterval(str + start_pos, len - start_pos, pos, time)) {
123232 return false;
123233 }
123234 result.micros += time.micros;
123235 found_any = true;
123236 if (negative) {
123237 result.micros = -result.micros;
123238 }
123239 goto end_of_string;
123240}
123241interval_parse_identifier:
123242 for (; pos < len; pos++) {
123243 char c = str[pos];
123244 if (c == ' ' || c == '\t' || c == '\n') {
123245
123246 continue;
123247 } else {
123248 break;
123249 }
123250 }
123251
123252 start_pos = pos;
123253 for (; pos < len; pos++) {
123254 char c = str[pos];
123255 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
123256
123257 continue;
123258 } else {
123259 break;
123260 }
123261 }
123262 specifier_str = string(str + start_pos, pos - start_pos);
123263
123264
123265 if (specifier_str.empty() && !found_any) {
123266 IntervalTryAddition<int64_t>(result.micros, number, MICROS_PER_SEC);
123267 IntervalTryAddition<int64_t>(result.micros, fraction, 1);
123268 found_any = true;
123269
123270 for (; pos < len; pos++) {
123271 char c = str[pos];
123272 if (c == ' ' || c == '\t' || c == '\n') {
123273 continue;
123274 } else {
123275 return false;
123276 }
123277 }
123278 goto end_of_string;
123279 }
123280
123281 if (!TryGetDatePartSpecifier(specifier_str, specifier)) {
123282 HandleCastError::AssignError(
StringUtil::Format(
"extract specifier \"%s\" not recognized", specifier_str),
123283 error_message);
123284 return false;
123285 }
123286
123287 switch (specifier) {
123288 case DatePartSpecifier::MILLENNIUM:
123289 IntervalTryAddition<int32_t>(result.months, number, MONTHS_PER_MILLENIUM, fraction);
123290 break;
123291 case DatePartSpecifier::CENTURY:
123292 IntervalTryAddition<int32_t>(result.months, number, MONTHS_PER_CENTURY, fraction);
123293 break;
123294 case DatePartSpecifier::DECADE:
123295 IntervalTryAddition<int32_t>(result.months, number, MONTHS_PER_DECADE, fraction);
123296 break;
123297 case DatePartSpecifier::YEAR:
123298 IntervalTryAddition<int32_t>(result.months, number, MONTHS_PER_YEAR, fraction);
123299 break;
123300 case DatePartSpecifier::QUARTER:
123301 IntervalTryAddition<int32_t>(result.months, number, MONTHS_PER_QUARTER, fraction);
123302
123303 fraction *= MONTHS_PER_QUARTER;
123304 fraction %= MICROS_PER_SEC;
123305 IntervalTryAddition<int32_t>(result.days, 0,
DAYS_PER_MONTH, fraction);
123306 break;
123307 case DatePartSpecifier::MONTH:
123308 IntervalTryAddition<int32_t>(result.months, number, 1);
123309 IntervalTryAddition<int32_t>(result.days, 0,
DAYS_PER_MONTH, fraction);
123310 break;
123311 case DatePartSpecifier::DAY:
123312 IntervalTryAddition<int32_t>(result.days, number, 1);
123313 IntervalTryAddition<int64_t>(result.micros, 0, MICROS_PER_DAY, fraction);
123314 break;
123315 case DatePartSpecifier::WEEK:
123316 IntervalTryAddition<int32_t>(result.days, number, DAYS_PER_WEEK, fraction);
123317
123318 fraction *= DAYS_PER_WEEK;
123319 fraction %= MICROS_PER_SEC;
123320 IntervalTryAddition<int64_t>(result.micros, 0, MICROS_PER_DAY, fraction);
123321 break;
123322 case DatePartSpecifier::MICROSECONDS:
123323
123324 number += (fraction * 2) / MICROS_PER_SEC;
123325 IntervalTryAddition<int64_t>(result.micros, number, 1);
123326 break;
123327 case DatePartSpecifier::MILLISECONDS:
123328 IntervalTryAddition<int64_t>(result.micros, number, MICROS_PER_MSEC, fraction);
123329 break;
123330 case DatePartSpecifier::SECOND:
123331 IntervalTryAddition<int64_t>(result.micros, number, MICROS_PER_SEC, fraction);
123332 break;
123333 case DatePartSpecifier::MINUTE:
123334 IntervalTryAddition<int64_t>(result.micros, number, MICROS_PER_MINUTE, fraction);
123335 break;
123336 case DatePartSpecifier::HOUR:
123337 IntervalTryAddition<int64_t>(result.micros, number, MICROS_PER_HOUR, fraction);
123338 break;
123339 default:
123340 HandleCastError::AssignError(
123341 StringUtil::Format(
"extract specifier \"%s\" not supported for interval", specifier_str), error_message);
123342 return false;
123343 }
123344 found_any = true;
123345 goto standard_interval;
123346interval_parse_ago:
123347 D_ASSERT(str[pos] == 'a' || str[pos] == 'A');
123348
123349 if (len - pos < 3) {
123350 return false;
123351 }
123352 pos++;
123353 if (!(str[pos] == 'g' || str[pos] == 'G')) {
123354 return false;
123355 }
123356 pos++;
123357 if (!(str[pos] == 'o' || str[pos] == 'O')) {
123358 return false;
123359 }
123360 pos++;
123361
123362 for (; pos < len; pos++) {
123363 char c = str[pos];
123364 if (c == ' ' || c == '\t' || c == '\n') {
123365 continue;
123366 } else {
123367 return false;
123368 }
123369 }
123370
123371 if (result.months == NumericLimits<int32_t>::Minimum() || result.days == NumericLimits<int32_t>::Minimum()) {
123372 throw OutOfRangeException("AGO interval value is out of range");
123373 }
123374
123375 result.months = -result.months;
123376 result.days = -result.days;
123377 result.micros = -result.micros;
123378 goto end_of_string;
123379end_of_string:
123380 if (!found_any) {
123381
123382 return false;
123383 }
123384 return true;
123385posix_interval:
123386 return false;
123387}
static constexpr const int64_t DAYS_PER_MONTH
only used for interval comparison/ordering purposes, in which case a month counts as 30 days
Definition duckdb.hpp:5258
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237