OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExtractFromTime.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * Thank you Howard Hinnant for public domain date algorithms
19  * http://howardhinnant.github.io/date_algorithms.html
20  */
21 
22 #include "ExtractFromTime.h"
23 
24 #ifndef __CUDACC__
25 #include <cstdlib> // abort()
26 #endif
27 
28 namespace {
29 
30 // Number of days until Wednesday (because 2000-03-01 is a Wednesday.)
31 constexpr unsigned MONDAY = 2;
32 constexpr unsigned SUNDAY = 3;
33 constexpr unsigned SATURDAY = 4;
34 
35 // If OFFSET=MONDAY,
36 // then return day-of-era of the Monday of ISO 8601 week 1 in the given year-of-era.
37 // Similarly for SUNDAY and SATURDAY. In all cases, week 1 always contains Jan 4.
38 template <unsigned OFFSET>
39 DEVICE unsigned week_start_from_yoe(unsigned const yoe) {
40  unsigned const march1 = yoe * 365 + yoe / 4 - yoe / 100;
41  unsigned const jan4 = march1 + (MARJAN + 3);
42  unsigned const jan4dow = (jan4 + OFFSET) % 7;
43  return jan4 - jan4dow;
44 }
45 
46 } // namespace
47 
48 extern "C" ALWAYS_INLINE DEVICE int64_t ExtractTimeFromHPTimestamp(const int64_t timeval,
49  const int64_t scale) {
50  return unsigned_mod(floor_div(timeval, scale), kSecsPerDay);
51 }
52 
53 extern "C" ALWAYS_INLINE DEVICE int64_t
54 ExtractTimeFromHPTimestampNullable(const int64_t timeval,
55  const int64_t scale,
56  const int64_t null_val) {
57  if (timeval == null_val) {
58  return null_val;
59  }
60  return ExtractTimeFromHPTimestamp(timeval, scale);
61 }
62 
63 extern "C" ALWAYS_INLINE DEVICE int64_t
64 ExtractTimeFromLPTimestamp(const int64_t timeval) {
65  return unsigned_mod(timeval, kSecsPerDay);
66 }
67 
68 extern "C" ALWAYS_INLINE DEVICE int64_t
69 ExtractTimeFromLPTimestampNullable(const int64_t timeval, const int64_t null_val) {
70  if (timeval == null_val) {
71  return null_val;
72  }
73  return ExtractTimeFromLPTimestamp(timeval);
74 }
75 
76 extern "C" ALWAYS_INLINE DEVICE int64_t extract_hour(const int64_t lcltime) {
77  return unsigned_mod(lcltime, kSecsPerDay) / kSecsPerHour;
78 }
79 
80 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
81 extract_minute(const int64_t lcltime) {
82  return unsigned_mod(lcltime, kSecsPerHour) / kSecsPerMin;
83 }
84 
85 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
86 extract_second(const int64_t lcltime) {
87  return unsigned_mod(lcltime, kSecsPerMin);
88 }
89 
90 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
91 extract_millisecond(const int64_t lcltime) {
92  return unsigned_mod(lcltime, kSecsPerMin * kMilliSecsPerSec);
93 }
94 
95 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
96 extract_microsecond(const int64_t lcltime) {
97  return unsigned_mod(lcltime, kSecsPerMin * kMicroSecsPerSec);
98 }
99 
100 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
101 extract_nanosecond(const int64_t lcltime) {
102  return unsigned_mod(lcltime, kSecsPerMin * kNanoSecsPerSec);
103 }
104 
105 // First day of epoch is Thursday, so + 4 to have Sunday=0.
106 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
107 extract_dow(const int64_t lcltime) {
108  int64_t const days_past_epoch = floor_div(lcltime, kSecsPerDay);
109  return unsigned_mod(days_past_epoch + 4, kDaysPerWeek);
110 }
111 
112 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
113 extract_quarterday(const int64_t lcltime) {
114  return unsigned_mod(lcltime, kSecsPerDay) / kSecsPerQuarterDay + 1;
115 }
116 
117 DEVICE int32_t extract_month_fast(const int64_t lcltime) {
118  STATIC_QUAL const uint32_t cumulative_month_epoch_starts[kMonsPerYear] = {0,
119  2678400,
120  5270400,
121  7948800,
122  10540800,
123  13219200,
124  15897600,
125  18489600,
126  21168000,
127  23760000,
128  26438400,
129  29116800};
130  uint32_t seconds_march_1900 = lcltime + kEpochOffsetYear1900 - kSecsJanToMar1900;
131  uint32_t seconds_past_4year_period = seconds_march_1900 % kSecondsPer4YearCycle;
132  uint32_t year_seconds_past_4year_period =
133  (seconds_past_4year_period / kSecondsPerNonLeapYear) * kSecondsPerNonLeapYear;
134  if (seconds_past_4year_period >=
135  kSecondsPer4YearCycle - kUSecsPerDay) { // if we are in Feb 29th
136  year_seconds_past_4year_period -= kSecondsPerNonLeapYear;
137  }
138  uint32_t seconds_past_march =
139  seconds_past_4year_period - year_seconds_past_4year_period;
140  uint32_t month =
141  seconds_past_march / (30 * kUSecsPerDay); // Will make the correct month either be
142  // the guessed month or month before
143  month = month <= 11 ? month : 11;
144  if (cumulative_month_epoch_starts[month] > seconds_past_march) {
145  month--;
146  }
147  return (month + 2) % 12 + 1;
148 }
149 
150 DEVICE int32_t extract_quarter_fast(const int64_t lcltime) {
151  STATIC_QUAL const uint32_t cumulative_quarter_epoch_starts[4] = {
152  0, 7776000, 15638400, 23587200};
153  STATIC_QUAL const uint32_t cumulative_quarter_epoch_starts_leap_year[4] = {
154  0, 7862400, 15724800, 23673600};
155  uint32_t seconds_1900 = lcltime + kEpochOffsetYear1900;
156  uint32_t leap_years = (seconds_1900 - kSecsJanToMar1900) / kSecondsPer4YearCycle;
157  uint32_t year = (seconds_1900 - leap_years * kSecsPerDay) / kSecondsPerNonLeapYear;
158  uint32_t base_year_leap_years = (year - 1) / 4;
159  uint32_t base_year_seconds =
160  year * kSecondsPerNonLeapYear + base_year_leap_years * kUSecsPerDay;
161  bool is_leap_year = year % 4 == 0 && year != 0;
162  const uint32_t* quarter_offsets = is_leap_year
163  ? cumulative_quarter_epoch_starts_leap_year
164  : cumulative_quarter_epoch_starts;
165  uint32_t partial_year_seconds = seconds_1900 % base_year_seconds;
166  uint32_t quarter = partial_year_seconds / (90 * kUSecsPerDay);
167  quarter = quarter <= 3 ? quarter : 3;
168  if (quarter_offsets[quarter] > partial_year_seconds) {
169  quarter--;
170  }
171  return quarter + 1;
172 }
173 
174 DEVICE int32_t extract_year_fast(const int64_t lcltime) {
175  const uint32_t seconds_1900 = lcltime + kEpochOffsetYear1900;
176  const uint32_t leap_years = (seconds_1900 - kSecsJanToMar1900) / kSecondsPer4YearCycle;
177  const uint32_t year =
178  (seconds_1900 - leap_years * kUSecsPerDay) / kSecondsPerNonLeapYear + 1900;
179  return year;
180 }
181 
182 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
183 extract_epoch(const int64_t timeval) {
184  return timeval;
185 }
186 
187 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
188 extract_dateepoch(const int64_t timeval) {
189  return timeval - unsigned_mod(timeval, kSecsPerDay);
190 }
191 
192 // First day of epoch is Thursday, so + 3 to have Monday=0, then + 1 at the end.
193 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
194 extract_isodow(const int64_t timeval) {
195  int64_t const days_past_epoch = floor_div(timeval, kSecsPerDay);
196  return unsigned_mod(days_past_epoch + 3, kDaysPerWeek) + 1;
197 }
198 
199 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
200 extract_day(const int64_t timeval) {
201  int64_t const day = floor_div(timeval, kSecsPerDay);
202  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
203  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
204  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
205  unsigned const moy = (5 * doy + 2) / 153;
206  return doy - (153 * moy + 2) / 5 + 1;
207 }
208 
209 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
210 extract_day_of_year(const int64_t timeval) {
211  int64_t const day = floor_div(timeval, kSecsPerDay);
212  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
213  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
214  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
215  return doy + (doy < MARJAN ? 1 + JANMAR + (yoe % 4 == 0 && (yoe % 100 != 0 || yoe == 0))
216  : 1 - MARJAN);
217 }
218 
219 template <unsigned OFFSET>
220 ALWAYS_INLINE DEVICE int64_t extract_week(const int64_t timeval) {
221  int64_t const day = floor_div(timeval, kSecsPerDay);
222  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
223  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
224  unsigned week_start = week_start_from_yoe<OFFSET>(yoe);
225  if (doe < week_start) {
226  if (yoe == 0) {
227  // 2000-03-01 is OFFSET days from start of week, week + 9.
228  return (doe + OFFSET) / 7 + 9;
229  } else {
230  week_start = week_start_from_yoe<OFFSET>(yoe - 1);
231  }
232  }
233  return (doe - week_start) / 7 + 1;
234 }
235 
236 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
237 extract_week_monday(const int64_t timeval) {
238  return extract_week<MONDAY>(timeval);
239 }
240 
241 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
242 extract_week_sunday(const int64_t timeval) {
243  return extract_week<SUNDAY>(timeval);
244 }
245 
246 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
247 extract_week_saturday(const int64_t timeval) {
248  return extract_week<SATURDAY>(timeval);
249 }
250 
251 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
252 extract_month(const int64_t timeval) {
253  if (timeval >= 0LL && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
254  return extract_month_fast(timeval);
255  }
256  int64_t const day = floor_div(timeval, kSecsPerDay);
257  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
258  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
259  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
260  unsigned const moy = (5 * doy + 2) / 153;
261  return moy + (moy < 10 ? 3 : -9);
262 }
263 
264 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
265 extract_quarter(const int64_t timeval) {
266  if (timeval >= 0LL && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
267  return extract_quarter_fast(timeval);
268  }
269  constexpr int64_t quarter[12]{1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 1, 1};
270  int64_t const day = floor_div(timeval, kSecsPerDay);
271  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
272  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
273  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
274  unsigned const moy = (5 * doy + 2) / 153;
275  return quarter[moy];
276 }
277 
278 extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t
279 extract_year(const int64_t timeval) {
280  if (timeval >= 0LL && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
281  return extract_year_fast(timeval);
282  }
283  int64_t const day = floor_div(timeval, kSecsPerDay);
284  int64_t const era = floor_div(day - kEpochAdjustedDays, kDaysPer400Years);
285  unsigned const doe = day - kEpochAdjustedDays - era * kDaysPer400Years;
286  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
287  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
288  return 2000 + era * 400 + yoe + (MARJAN <= doy);
289 }
290 
291 /*
292  * @brief support the SQL EXTRACT function
293  */
294 DEVICE int64_t ExtractFromTime(ExtractField field, const int64_t timeval) {
295  switch (field) {
296  case kEPOCH:
297  return extract_epoch(timeval);
298  case kDATEEPOCH:
299  return extract_dateepoch(timeval);
300  case kQUARTERDAY:
301  return extract_quarterday(timeval);
302  case kHOUR:
303  return extract_hour(timeval);
304  case kMINUTE:
305  return extract_minute(timeval);
306  case kSECOND:
307  return extract_second(timeval);
308  case kMILLISECOND:
309  return extract_millisecond(timeval);
310  case kMICROSECOND:
311  return extract_microsecond(timeval);
312  case kNANOSECOND:
313  return extract_nanosecond(timeval);
314  case kDOW:
315  return extract_dow(timeval);
316  case kISODOW:
317  return extract_isodow(timeval);
318  case kDAY:
319  return extract_day(timeval);
320  case kWEEK:
321  return extract_week_monday(timeval);
322  case kWEEK_SUNDAY:
323  return extract_week_sunday(timeval);
324  case kWEEK_SATURDAY:
325  return extract_week_saturday(timeval);
326  case kDOY:
327  return extract_day_of_year(timeval);
328  case kMONTH:
329  return extract_month(timeval);
330  case kQUARTER:
331  return extract_quarter(timeval);
332  case kYEAR:
333  return extract_year(timeval);
334  case kUNKNOWN_FIELD:
335  return -1;
336  }
337 
338 #ifdef __CUDACC__
339  return -1;
340 #else
341  abort();
342 #endif
343 }
static constexpr int64_t kSecsPerDay
static constexpr uint32_t kSecsJanToMar1900
ALWAYS_INLINE DEVICE int64_t ExtractTimeFromHPTimestampNullable(const int64_t timeval, const int64_t scale, const int64_t null_val)
static constexpr uint32_t kUSecsPerDay
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_year(const int64_t timeval)
DEVICE int32_t extract_quarter_fast(const int64_t lcltime)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_second(const int64_t lcltime)
static constexpr int64_t kSecsPerHour
static constexpr int64_t kSecsPerMin
static constexpr int64_t kNanoSecsPerSec
constexpr unsigned JANMAR
ALWAYS_INLINE DEVICE int64_t extract_week(const int64_t timeval)
static constexpr int64_t kSecsPerQuarterDay
#define STATIC_QUAL
ALWAYS_INLINE DEVICE int64_t ExtractTimeFromLPTimestamp(const int64_t timeval)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_minute(const int64_t lcltime)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_month(const int64_t timeval)
DEVICE int64_t floor_div(int64_t const dividend, int64_t const divisor)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_nanosecond(const int64_t lcltime)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_millisecond(const int64_t lcltime)
ALWAYS_INLINE DEVICE int64_t ExtractTimeFromHPTimestamp(const int64_t timeval, const int64_t scale)
static constexpr uint32_t kEpochOffsetYear1900
#define DEVICE
static constexpr int64_t kMilliSecsPerSec
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
DEVICE unsigned week_start_from_yoe(unsigned const yoe)
static constexpr int32_t kEpochAdjustedDays
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_week_sunday(const int64_t timeval)
ALWAYS_INLINE DEVICE int64_t extract_hour(const int64_t lcltime)
static constexpr int32_t kDaysPerWeek
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_quarter(const int64_t timeval)
static constexpr uint32_t kSecondsPer4YearCycle
#define RUNTIME_EXPORT
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_week_monday(const int64_t timeval)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_dateepoch(const int64_t timeval)
static constexpr int64_t kDaysPer400Years
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_day(const int64_t timeval)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_epoch(const int64_t timeval)
constexpr unsigned MARJAN
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_dow(const int64_t lcltime)
DEVICE int64_t ExtractFromTime(ExtractField field, const int64_t timeval)
ExtractField
DEVICE int32_t extract_month_fast(const int64_t lcltime)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_week_saturday(const int64_t timeval)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_isodow(const int64_t timeval)
static constexpr uint32_t kSecondsPerNonLeapYear
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_microsecond(const int64_t lcltime)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_quarterday(const int64_t lcltime)
#define ALWAYS_INLINE
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t extract_day_of_year(const int64_t timeval)
static constexpr int64_t kMicroSecsPerSec
ALWAYS_INLINE DEVICE int64_t ExtractTimeFromLPTimestampNullable(const int64_t timeval, const int64_t null_val)
static constexpr int32_t kMonsPerYear
DEVICE int32_t extract_year_fast(const int64_t lcltime)