OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CalciteDeserializerUtils.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 
18 
19 #include "../Analyzer/Analyzer.h"
20 #include "Logger/Logger.h"
21 
22 #include <boost/algorithm/string.hpp>
23 
24 extern bool g_bigint_count;
25 
26 SQLTypeInfo get_agg_type(const SQLAgg agg_kind, const Analyzer::Expr* arg_expr) {
27  switch (agg_kind) {
28  case kCOUNT:
29  return SQLTypeInfo(g_bigint_count ? kBIGINT : kINT, true);
30  case kCOUNT_IF:
31  return SQLTypeInfo(g_bigint_count ? kBIGINT : kINT, false);
32  case kMIN:
33  case kMAX:
34  return arg_expr->get_type_info();
35  case kSUM:
36  case kSUM_IF:
37  return arg_expr->get_type_info().is_integer() ? SQLTypeInfo(kBIGINT, false)
38  : arg_expr->get_type_info();
39  case kAVG:
40  return SQLTypeInfo(kDOUBLE, false);
42  return SQLTypeInfo(kBIGINT, true);
43  case kAPPROX_QUANTILE:
44  return SQLTypeInfo(kDOUBLE, false);
45  case kSINGLE_VALUE:
46  if (arg_expr->get_type_info().is_varlen()) {
47  throw std::runtime_error("SINGLE_VALUE not supported on '" +
48  arg_expr->get_type_info().get_type_name() + "' input.");
49  } // else fall through
50  case kSAMPLE:
51  case kMODE:
52  return arg_expr->get_type_info();
53  default:
54  UNREACHABLE() << "Unsupported agg_kind: " << agg_kind;
55  return {};
56  }
57 }
58 
59 ExtractField to_datepart_field(const std::string& field) {
60  ExtractField fieldno;
61  if (boost::iequals(field, "year") || boost::iequals(field, "yy") ||
62  boost::iequals(field, "yyyy") || boost::iequals(field, "sql_tsi_year")) {
63  fieldno = kYEAR;
64  } else if (boost::iequals(field, "quarter") || boost::iequals(field, "qq") ||
65  boost::iequals(field, "q") || boost::iequals(field, "sql_tsi_quarter")) {
66  fieldno = kQUARTER;
67  } else if (boost::iequals(field, "month") || boost::iequals(field, "mm") ||
68  boost::iequals(field, "m") || boost::iequals(field, "sql_tsi_month")) {
69  fieldno = kMONTH;
70  } else if (boost::iequals(field, "dayofyear") || boost::iequals(field, "dy") ||
71  boost::iequals(field, "y")) {
72  fieldno = kDOY;
73  } else if (boost::iequals(field, "day") || boost::iequals(field, "dd") ||
74  boost::iequals(field, "d") || boost::iequals(field, "sql_tsi_day")) {
75  fieldno = kDAY;
76  } else if (boost::iequals(field, "week") || boost::iequals(field, "ww") ||
77  boost::iequals(field, "w") || boost::iequals(field, "sql_tsi_week")) {
78  fieldno = kWEEK;
79  } else if (boost::iequals(field, "week_sunday")) {
80  fieldno = kWEEK_SUNDAY;
81  } else if (boost::iequals(field, "week_saturday")) {
82  fieldno = kWEEK_SATURDAY;
83  } else if (boost::iequals(field, "hour") || boost::iequals(field, "hh") ||
84  boost::iequals(field, "sql_tsi_hour")) {
85  fieldno = kHOUR;
86  } else if (boost::iequals(field, "minute") || boost::iequals(field, "mi") ||
87  boost::iequals(field, "n") || boost::iequals(field, "sql_tsi_minute")) {
88  fieldno = kMINUTE;
89  } else if (boost::iequals(field, "second") || boost::iequals(field, "ss") ||
90  boost::iequals(field, "s") || boost::iequals(field, "sql_tsi_second")) {
91  fieldno = kSECOND;
92  } else if (boost::iequals(field, "millisecond") || boost::iequals(field, "ms")) {
93  fieldno = kMILLISECOND;
94  } else if (boost::iequals(field, "microsecond") || boost::iequals(field, "us") ||
95  boost::iequals(field, "sql_tsi_microsecond") ||
96  boost::iequals(field, "frac_second")) {
97  fieldno = kMICROSECOND;
98  } else if (boost::iequals(field, "nanosecond") || boost::iequals(field, "ns") ||
99  boost::iequals(field, "sql_tsi_frac_second")) {
100  fieldno = kNANOSECOND;
101  } else if (boost::iequals(field, "weekday") || boost::iequals(field, "dw")) {
102  fieldno = kISODOW;
103  } else if (boost::iequals(field, "quarterday") || boost::iequals(field, "dq")) {
104  fieldno = kQUARTERDAY;
105  } else {
106  throw std::runtime_error("Unsupported field in DATEPART function: " + field);
107  }
108  return fieldno;
109 }
110 
111 DateaddField to_dateadd_field(const std::string& field) {
112  DateaddField fieldno;
113  if (boost::iequals(field, "year") || boost::iequals(field, "yy") ||
114  boost::iequals(field, "yyyy") || boost::iequals(field, "sql_tsi_year")) {
115  fieldno = daYEAR;
116  } else if (boost::iequals(field, "quarter") || boost::iequals(field, "qq") ||
117  boost::iequals(field, "q") || boost::iequals(field, "sql_tsi_quarter")) {
118  fieldno = daQUARTER;
119  } else if (boost::iequals(field, "month") || boost::iequals(field, "mm") ||
120  boost::iequals(field, "m") || boost::iequals(field, "sql_tsi_month")) {
121  fieldno = daMONTH;
122  } else if (boost::iequals(field, "day") || boost::iequals(field, "dd") ||
123  boost::iequals(field, "d") || boost::iequals(field, "sql_tsi_day")) {
124  fieldno = daDAY;
125  } else if (boost::iequals(field, "week") || boost::iequals(field, "ww") ||
126  boost::iequals(field, "w") || boost::iequals(field, "sql_tsi_week")) {
127  fieldno = daWEEK;
128  } else if (boost::iequals(field, "hour") || boost::iequals(field, "hh") ||
129  boost::iequals(field, "sql_tsi_hour")) {
130  fieldno = daHOUR;
131  } else if (boost::iequals(field, "minute") || boost::iequals(field, "mi") ||
132  boost::iequals(field, "n") || boost::iequals(field, "sql_tsi_minute")) {
133  fieldno = daMINUTE;
134  } else if (boost::iequals(field, "second") || boost::iequals(field, "ss") ||
135  boost::iequals(field, "s") || boost::iequals(field, "sql_tsi_second")) {
136  fieldno = daSECOND;
137  } else if (boost::iequals(field, "millisecond") || boost::iequals(field, "ms")) {
138  fieldno = daMILLISECOND;
139  } else if (boost::iequals(field, "microsecond") || boost::iequals(field, "us") ||
140  boost::iequals(field, "sql_tsi_microsecond") ||
141  boost::iequals(field, "frac_second")) {
142  fieldno = daMICROSECOND;
143  } else if (boost::iequals(field, "nanosecond") || boost::iequals(field, "ns") ||
144  boost::iequals(field, "sql_tsi_frac_second")) {
145  fieldno = daNANOSECOND;
146  } else if (boost::iequals(field, "weekday") || boost::iequals(field, "dw")) {
147  fieldno = daWEEKDAY;
148  } else if (boost::iequals(field, "decade") || boost::iequals(field, "dc")) {
149  fieldno = daDECADE;
150  } else if (boost::iequals(field, "century")) {
151  fieldno = daCENTURY;
152  } else if (boost::iequals(field, "millennium")) {
153  fieldno = daMILLENNIUM;
154  } else {
155  throw std::runtime_error("Unsupported field in DATEADD function: " + field);
156  }
157  return fieldno;
158 }
159 
161  DatetruncField fieldno;
162  if (boost::iequals(field, "year") || boost::iequals(field, "yy") ||
163  boost::iequals(field, "yyyy") || boost::iequals(field, "sql_tsi_year")) {
164  fieldno = dtYEAR;
165  } else if (boost::iequals(field, "quarter") || boost::iequals(field, "qq") ||
166  boost::iequals(field, "q") || boost::iequals(field, "sql_tsi_quarter")) {
167  fieldno = dtQUARTER;
168  } else if (boost::iequals(field, "month") || boost::iequals(field, "mm") ||
169  boost::iequals(field, "m") || boost::iequals(field, "sql_tsi_month")) {
170  fieldno = dtMONTH;
171  } else if (boost::iequals(field, "week") || boost::iequals(field, "ww") ||
172  boost::iequals(field, "w") || boost::iequals(field, "sql_tsi_week")) {
173  fieldno = dtWEEK;
174  } else if (boost::iequals(field, "week_sunday")) {
175  fieldno = dtWEEK_SUNDAY;
176  } else if (boost::iequals(field, "week_saturday")) {
177  fieldno = dtWEEK_SATURDAY;
178  } else if (boost::iequals(field, "day") || boost::iequals(field, "dd") ||
179  boost::iequals(field, "d") || boost::iequals(field, "sql_tsi_day")) {
180  fieldno = dtDAY;
181  } else if (boost::iequals(field, "quarterday")) {
182  fieldno = dtQUARTERDAY;
183  } else if (boost::iequals(field, "hour") || boost::iequals(field, "hh") ||
184  boost::iequals(field, "sql_tsi_hour")) {
185  fieldno = dtHOUR;
186  } else if (boost::iequals(field, "minute") || boost::iequals(field, "mi") ||
187  boost::iequals(field, "n") || boost::iequals(field, "sql_tsi_minute")) {
188  fieldno = dtMINUTE;
189  } else if (boost::iequals(field, "second") || boost::iequals(field, "ss") ||
190  boost::iequals(field, "s") || boost::iequals(field, "sql_tsi_second")) {
191  fieldno = dtSECOND;
192  } else if (boost::iequals(field, "millisecond") || boost::iequals(field, "ms")) {
193  fieldno = dtMILLISECOND;
194  } else if (boost::iequals(field, "microsecond") || boost::iequals(field, "us") ||
195  boost::iequals(field, "sql_tsi_microsecond") ||
196  boost::iequals(field, "frac_second")) {
197  fieldno = dtMICROSECOND;
198  } else if (boost::iequals(field, "nanosecond") || boost::iequals(field, "ns") ||
199  boost::iequals(field, "sql_tsi_frac_second")) {
200  fieldno = dtNANOSECOND;
201  } else if (boost::iequals(field, "decade") || boost::iequals(field, "dc")) {
202  fieldno = dtDECADE;
203  } else if (boost::iequals(field, "century")) {
204  fieldno = dtCENTURY;
205  } else if (boost::iequals(field, "millennium")) {
206  fieldno = dtMILLENNIUM;
207  } else {
208  throw std::runtime_error("Unsupported field in DATEDIFF function: " + field);
209  }
210  return fieldno;
211 }
212 
213 std::shared_ptr<Analyzer::Constant> make_fp_constant(const int64_t val,
214  const SQLTypeInfo& ti) {
215  Datum d;
216  switch (ti.get_type()) {
217  case kFLOAT:
218  d.floatval = val;
219  break;
220  case kDOUBLE:
221  d.doubleval = val;
222  break;
223  default:
224  CHECK(false);
225  }
226  return makeExpr<Analyzer::Constant>(ti, false, d);
227 }
SQLAgg
Definition: sqldefs.h:76
bool is_varlen() const
Definition: sqltypes.h:631
#define UNREACHABLE()
Definition: Logger.h:338
SQLTypeInfo get_agg_type(const SQLAgg agg_kind, const Analyzer::Expr *arg_expr)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:391
Definition: DateAdd.h:47
Definition: sqldefs.h:78
ExtractField to_datepart_field(const std::string &field)
float floatval
Definition: Datum.h:77
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:33
Definition: DateAdd.h:43
DateaddField
Definition: DateAdd.h:42
bool is_integer() const
Definition: sqltypes.h:567
DatetruncField
Definition: DateTruncate.h:27
Definition: DateAdd.h:56
bool g_bigint_count
Definition: sqldefs.h:80
DatetruncField to_datediff_field(const std::string &field)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
Definition: sqldefs.h:81
std::string get_type_name() const
Definition: sqltypes.h:484
ExtractField
#define CHECK(condition)
Definition: Logger.h:291
std::shared_ptr< Analyzer::Constant > make_fp_constant(const int64_t val, const SQLTypeInfo &ti)
Definition: DateAdd.h:46
Definition: sqltypes.h:72
Definition: sqldefs.h:79
Definition: Datum.h:71
Definition: sqldefs.h:77
Definition: sqldefs.h:86
DateaddField to_dateadd_field(const std::string &field)
double doubleval
Definition: Datum.h:78