OmniSciDB  c07336695a
DateTimeUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 OmniSci, 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 #pragma once
18 
19 #include "DateAdd.h"
20 #include "DateTruncate.h"
21 
22 #include <cstdint>
23 #include <ctime>
24 #include <map>
25 #include <string>
26 
27 #include "../Shared/sqldefs.h"
28 
29 namespace {
30 
31 static const std::map<std::pair<int32_t, ExtractField>, std::pair<SQLOps, int64_t>>
34  {{6, kMILLISECOND}, {kDIVIDE, kMilliSecsPerSec}},
35  {{6, kNANOSECOND}, {kMULTIPLY, kMilliSecsPerSec}},
36  {{9, kMILLISECOND}, {kDIVIDE, kMicroSecsPerSec}},
37  {{9, kMICROSECOND}, {kDIVIDE, kMilliSecsPerSec}}};
38 
39 static const std::map<std::pair<int32_t, DatetruncField>, int64_t>
40  datetrunc_precision_lookup = {{{6, dtMILLISECOND}, kMilliSecsPerSec},
41  {{9, dtMICROSECOND}, kMilliSecsPerSec},
43 
44 } // namespace
45 
46 namespace DateTimeUtils {
47 
48 constexpr inline int64_t get_timestamp_precision_scale(const int32_t dimen) {
49  switch (dimen) {
50  case 0:
51  return 1;
52  case 3:
53  return kMilliSecsPerSec;
54  case 6:
55  return kMicroSecsPerSec;
56  case 9:
57  return kNanoSecsPerSec;
58  default:
59  throw std::runtime_error("Unknown dimen = " + std::to_string(dimen));
60  }
61  return -1;
62 }
63 
64 constexpr inline int64_t get_dateadd_timestamp_precision_scale(const DateaddField field) {
65  switch (field) {
66  case daMILLISECOND:
67  return kMilliSecsPerSec;
68  case daMICROSECOND:
69  return kMicroSecsPerSec;
70  case daNANOSECOND:
71  return kNanoSecsPerSec;
72  default:
73  throw std::runtime_error("Unknown field = " + std::to_string(field));
74  }
75  return -1;
76 }
77 
78 constexpr inline int64_t get_extract_timestamp_precision_scale(const ExtractField field) {
79  switch (field) {
80  case kMILLISECOND:
81  return kMilliSecsPerSec;
82  case kMICROSECOND:
83  return kMicroSecsPerSec;
84  case kNANOSECOND:
85  return kNanoSecsPerSec;
86  default:
87  throw std::runtime_error("Unknown field = " + std::to_string(field));
88  }
89  return -1;
90 }
91 
92 constexpr inline bool is_subsecond_extract_field(const ExtractField& field) {
93  return field == kMILLISECOND || field == kMICROSECOND || field == kNANOSECOND;
94 }
95 
96 constexpr inline bool is_subsecond_dateadd_field(const DateaddField field) {
97  return field == daMILLISECOND || field == daMICROSECOND || field == daNANOSECOND;
98 }
99 
100 constexpr inline bool is_subsecond_datetrunc_field(const DatetruncField field) {
101  return field == dtMILLISECOND || field == dtMICROSECOND || field == dtNANOSECOND;
102 }
103 
104 const inline std::pair<SQLOps, int64_t> get_dateadd_high_precision_adjusted_scale(
105  const DateaddField field,
106  int32_t dimen) {
107  switch (field) {
108  case daNANOSECOND:
109  switch (dimen) {
110  case 9:
111  return {};
112  case 6:
113  return {kDIVIDE, kMilliSecsPerSec};
114  case 3:
115  return {kDIVIDE, kMicroSecsPerSec};
116  default:
117  throw std::runtime_error("Unknown dimen = " + std::to_string(dimen));
118  }
119  case daMICROSECOND:
120  switch (dimen) {
121  case 9:
122  return {kMULTIPLY, kMilliSecsPerSec};
123  case 6:
124  return {};
125  case 3:
126  return {kDIVIDE, kMilliSecsPerSec};
127  default:
128  throw std::runtime_error("Unknown dimen = " + std::to_string(dimen));
129  }
130  case daMILLISECOND:
131  switch (dimen) {
132  case 9:
133  return {kMULTIPLY, kMicroSecsPerSec};
134  case 6:
135  return {kMULTIPLY, kMilliSecsPerSec};
136  case 3:
137  return {};
138  default:
139  throw std::runtime_error("Unknown dimen = " + std::to_string(dimen));
140  }
141  default:
142  throw std::runtime_error("Unknown field = " + std::to_string(field));
143  }
144  return {};
145 }
146 
147 const inline std::pair<SQLOps, int64_t> get_extract_high_precision_adjusted_scale(
148  const ExtractField& field,
149  const int32_t dimen) {
150  const auto result = extract_precision_lookup.find(std::make_pair(dimen, field));
151  if (result != extract_precision_lookup.end()) {
152  return result->second;
153  }
154  return {};
155 }
156 
158  const int32_t dimen) {
159  const auto result = datetrunc_precision_lookup.find(std::make_pair(dimen, field));
160  if (result != datetrunc_precision_lookup.end()) {
161  return result->second;
162  }
163  return -1;
164 }
165 
166 } // namespace DateTimeUtils
constexpr int64_t get_extract_timestamp_precision_scale(const ExtractField field)
Definition: DateTimeUtils.h:78
static constexpr int64_t kNanoSecsPerSec
constexpr int64_t get_dateadd_timestamp_precision_scale(const DateaddField field)
Definition: DateTimeUtils.h:64
const std::pair< SQLOps, int64_t > get_extract_high_precision_adjusted_scale(const ExtractField &field, const int32_t dimen)
static const std::map< std::pair< int32_t, ExtractField >, std::pair< SQLOps, int64_t > > extract_precision_lookup
Definition: DateTimeUtils.h:32
std::string to_string(char const *&&v)
static const std::map< std::pair< int32_t, DatetruncField >, int64_t > datetrunc_precision_lookup
Definition: DateTimeUtils.h:40
static constexpr int64_t kMilliSecsPerSec
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
const std::pair< SQLOps, int64_t > get_dateadd_high_precision_adjusted_scale(const DateaddField field, int32_t dimen)
DateaddField
Definition: DateAdd.h:42
DatetruncField
Definition: DateTruncate.h:42
constexpr bool is_subsecond_datetrunc_field(const DatetruncField field)
ExtractField
constexpr bool is_subsecond_dateadd_field(const DateaddField field)
Definition: DateTimeUtils.h:96
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:48
constexpr bool is_subsecond_extract_field(const ExtractField &field)
Definition: DateTimeUtils.h:92
static constexpr int64_t kMicroSecsPerSec
const int64_t get_datetrunc_high_precision_scale(const DatetruncField &field, const int32_t dimen)