OmniSciDB  29e35f4d58
CalciteDeserializerUtils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 "Shared/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, false);
30  case kMIN:
31  case kMAX:
32  return arg_expr->get_type_info();
33  case kSUM:
34  return arg_expr->get_type_info().is_integer() ? SQLTypeInfo(kBIGINT, false)
35  : arg_expr->get_type_info();
36  case kAVG:
37  return SQLTypeInfo(kDOUBLE, false);
39  return SQLTypeInfo(kBIGINT, false);
40  case kSINGLE_VALUE:
41  if (arg_expr->get_type_info().is_varlen()) {
42  throw std::runtime_error("SINGLE_VALUE not supported on '" +
43  arg_expr->get_type_info().get_type_name() + "' input.");
44  }
45  case kSAMPLE:
46  return arg_expr->get_type_info();
47  default:
48  CHECK(false);
49  }
50  CHECK(false);
51  return SQLTypeInfo();
52 }
53 
54 ExtractField to_datepart_field(const std::string& field) {
55  ExtractField fieldno;
56  if (boost::iequals(field, "year") || boost::iequals(field, "yy") ||
57  boost::iequals(field, "yyyy")) {
58  fieldno = kYEAR;
59  } else if (boost::iequals(field, "quarter") || boost::iequals(field, "qq") ||
60  boost::iequals(field, "q")) {
61  fieldno = kQUARTER;
62  } else if (boost::iequals(field, "month") || boost::iequals(field, "mm") ||
63  boost::iequals(field, "m")) {
64  fieldno = kMONTH;
65  } else if (boost::iequals(field, "dayofyear") || boost::iequals(field, "dy") ||
66  boost::iequals(field, "y")) {
67  fieldno = kDOY;
68  } else if (boost::iequals(field, "day") || boost::iequals(field, "dd") ||
69  boost::iequals(field, "d")) {
70  fieldno = kDAY;
71  } else if (boost::iequals(field, "hour") || boost::iequals(field, "hh")) {
72  fieldno = kHOUR;
73  } else if (boost::iequals(field, "minute") || boost::iequals(field, "mi") ||
74  boost::iequals(field, "n")) {
75  fieldno = kMINUTE;
76  } else if (boost::iequals(field, "second") || boost::iequals(field, "ss") ||
77  boost::iequals(field, "s")) {
78  fieldno = kSECOND;
79  } else if (boost::iequals(field, "millisecond") || boost::iequals(field, "ms")) {
80  fieldno = kMILLISECOND;
81  } else if (boost::iequals(field, "microsecond") || boost::iequals(field, "us")) {
82  fieldno = kMICROSECOND;
83  } else if (boost::iequals(field, "nanosecond") || boost::iequals(field, "ns")) {
84  fieldno = kNANOSECOND;
85  } else if (boost::iequals(field, "weekday") || boost::iequals(field, "dw")) {
86  fieldno = kISODOW;
87  } else if (boost::iequals(field, "quarterday") || boost::iequals(field, "dq")) {
88  fieldno = kQUARTERDAY;
89  } else {
90  throw std::runtime_error("Unsupported field in DATEPART function: " + field);
91  }
92  return fieldno;
93 }
94 
95 DateaddField to_dateadd_field(const std::string& field) {
96  DateaddField fieldno;
97  if (boost::iequals(field, "year") || boost::iequals(field, "yy") ||
98  boost::iequals(field, "yyyy") || boost::iequals(field, "sql_tsi_year")) {
99  fieldno = daYEAR;
100  } else if (boost::iequals(field, "quarter") || boost::iequals(field, "qq") ||
101  boost::iequals(field, "q") || boost::iequals(field, "sql_tsi_quarter")) {
102  fieldno = daQUARTER;
103  } else if (boost::iequals(field, "month") || boost::iequals(field, "mm") ||
104  boost::iequals(field, "m") || boost::iequals(field, "sql_tsi_month")) {
105  fieldno = daMONTH;
106  } else if (boost::iequals(field, "day") || boost::iequals(field, "dd") ||
107  boost::iequals(field, "d") || boost::iequals(field, "sql_tsi_day")) {
108  fieldno = daDAY;
109  } else if (boost::iequals(field, "week") || boost::iequals(field, "ww") ||
110  boost::iequals(field, "w") || boost::iequals(field, "sql_tsi_week")) {
111  fieldno = daWEEK;
112  } else if (boost::iequals(field, "hour") || boost::iequals(field, "hh") ||
113  boost::iequals(field, "sql_tsi_hour")) {
114  fieldno = daHOUR;
115  } else if (boost::iequals(field, "minute") || boost::iequals(field, "mi") ||
116  boost::iequals(field, "n") || boost::iequals(field, "sql_tsi_minute")) {
117  fieldno = daMINUTE;
118  } else if (boost::iequals(field, "second") || boost::iequals(field, "ss") ||
119  boost::iequals(field, "s") || boost::iequals(field, "sql_tsi_second")) {
120  fieldno = daSECOND;
121  } else if (boost::iequals(field, "millisecond") || boost::iequals(field, "ms")) {
122  fieldno = daMILLISECOND;
123  } else if (boost::iequals(field, "microsecond") || boost::iequals(field, "us") ||
124  boost::iequals(field, "sql_tsi_microsecond") ||
125  boost::iequals(field, "frac_second")) {
126  fieldno = daMICROSECOND;
127  } else if (boost::iequals(field, "nanosecond") || boost::iequals(field, "ns") ||
128  boost::iequals(field, "sql_tsi_frac_second")) {
129  fieldno = daNANOSECOND;
130  } else if (boost::iequals(field, "weekday") || boost::iequals(field, "dw")) {
131  fieldno = daWEEKDAY;
132  } else if (boost::iequals(field, "decade") || boost::iequals(field, "dc")) {
133  fieldno = daDECADE;
134  } else {
135  throw std::runtime_error("Unsupported field in DATEADD function: " + field);
136  }
137  return fieldno;
138 }
139 
141  DatetruncField fieldno;
142  if (boost::iequals(field, "year") || boost::iequals(field, "yy") ||
143  boost::iequals(field, "yyyy")) {
144  fieldno = dtYEAR;
145  } else if (boost::iequals(field, "quarter") || boost::iequals(field, "qq") ||
146  boost::iequals(field, "q")) {
147  fieldno = dtQUARTER;
148  } else if (boost::iequals(field, "month") || boost::iequals(field, "mm") ||
149  boost::iequals(field, "m")) {
150  fieldno = dtMONTH;
151  } else if (boost::iequals(field, "week") || boost::iequals(field, "ww") ||
152  boost::iequals(field, "w")) {
153  fieldno = dtWEEK;
154  } else if (boost::iequals(field, "day") || boost::iequals(field, "dd") ||
155  boost::iequals(field, "d")) {
156  fieldno = dtDAY;
157  } else if (boost::iequals(field, "hour") || boost::iequals(field, "hh")) {
158  fieldno = dtHOUR;
159  } else if (boost::iequals(field, "minute") || boost::iequals(field, "mi") ||
160  boost::iequals(field, "n")) {
161  fieldno = dtMINUTE;
162  } else if (boost::iequals(field, "second") || boost::iequals(field, "ss") ||
163  boost::iequals(field, "s")) {
164  fieldno = dtSECOND;
165  } else if (boost::iequals(field, "millisecond") || boost::iequals(field, "ms")) {
166  fieldno = dtMILLISECOND;
167  } else if (boost::iequals(field, "microsecond") || boost::iequals(field, "us")) {
168  fieldno = dtMICROSECOND;
169  } else if (boost::iequals(field, "nanosecond") || boost::iequals(field, "ns")) {
170  fieldno = dtNANOSECOND;
171  } else {
172  throw std::runtime_error("Unsupported field in DATEDIFF function: " + field);
173  }
174  return fieldno;
175 }
176 
177 std::shared_ptr<Analyzer::Constant> make_fp_constant(const int64_t val,
178  const SQLTypeInfo& ti) {
179  Datum d;
180  switch (ti.get_type()) {
181  case kFLOAT:
182  d.floatval = val;
183  break;
184  case kDOUBLE:
185  d.doubleval = val;
186  break;
187  default:
188  CHECK(false);
189  }
190  return makeExpr<Analyzer::Constant>(ti, false, d);
191 }
SQLAgg
Definition: sqldefs.h:71
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
bool is_varlen() const
Definition: sqltypes.h:491
SQLTypeInfo get_agg_type(const SQLAgg agg_kind, const Analyzer::Expr *arg_expr)
Definition: DateAdd.h:47
Definition: sqldefs.h:73
std::string get_type_name() const
Definition: sqltypes.h:429
ExtractField to_datepart_field(const std::string &field)
float floatval
Definition: sqltypes.h:130
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
bool is_integer() const
Definition: sqltypes.h:479
Definition: DateAdd.h:43
DateaddField
Definition: DateAdd.h:42
DatetruncField
Definition: DateTruncate.h:42
Definition: DateAdd.h:56
bool g_bigint_count
Definition: sqldefs.h:75
DatetruncField to_datediff_field(const std::string &field)
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
Definition: sqldefs.h:76
ExtractField
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
#define CHECK(condition)
Definition: Logger.h:193
std::shared_ptr< Analyzer::Constant > make_fp_constant(const int64_t val, const SQLTypeInfo &ti)
Definition: DateAdd.h:46
Definition: sqltypes.h:48
Definition: sqldefs.h:74
Definition: sqldefs.h:72
DateaddField to_dateadd_field(const std::string &field)
double doubleval
Definition: sqltypes.h:131