OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CalciteDeserializerUtils.h
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 
17 #ifndef QUERYENGINE_CALCITEDESERIALIZERUTILS_H
18 #define QUERYENGINE_CALCITEDESERIALIZERUTILS_H
19 
20 #include "DateAdd.h"
21 #include "DateTruncate.h"
22 
23 #include "../Shared/sqldefs.h"
24 #include "../Shared/sqltypes.h"
25 #include "Logger/Logger.h"
26 
27 inline SQLOps to_sql_op(const std::string& op_str) {
28  if (op_str == std::string(">")) {
29  return kGT;
30  }
31  if (op_str == std::string("IS NOT DISTINCT FROM")) {
32  return kBW_EQ;
33  }
34  if (op_str == std::string(">=")) {
35  return kGE;
36  }
37  if (op_str == std::string("<")) {
38  return kLT;
39  }
40  if (op_str == std::string("<=")) {
41  return kLE;
42  }
43  if (op_str == std::string("=")) {
44  return kEQ;
45  }
46  if (op_str == std::string("<>")) {
47  return kNE;
48  }
49  if (op_str == std::string("+")) {
50  return kPLUS;
51  }
52  if (op_str == std::string("-")) {
53  return kMINUS;
54  }
55  if (op_str == std::string("*")) {
56  return kMULTIPLY;
57  }
58  if (op_str == std::string("/")) {
59  return kDIVIDE;
60  }
61  if (op_str == "MOD") {
62  return kMODULO;
63  }
64  if (op_str == std::string("AND")) {
65  return kAND;
66  }
67  if (op_str == std::string("OR")) {
68  return kOR;
69  }
70  if (op_str == std::string("CAST")) {
71  return kCAST;
72  }
73  if (op_str == std::string("NOT")) {
74  return kNOT;
75  }
76  if (op_str == std::string("IS NULL")) {
77  return kISNULL;
78  }
79  if (op_str == std::string("IS NOT NULL")) {
80  return kISNOTNULL;
81  }
82  if (op_str == std::string("PG_UNNEST")) {
83  return kUNNEST;
84  }
85  if (op_str == std::string("PG_ANY") || op_str == std::string("PG_ALL")) {
86  throw std::runtime_error("Invalid use of " + op_str + " operator");
87  }
88  if (op_str == std::string("IN")) {
89  return kIN;
90  }
91  return kFUNCTION;
92 }
93 
94 inline SQLAgg to_agg_kind(const std::string& agg_name) {
95  if (agg_name == std::string("COUNT")) {
96  return kCOUNT;
97  }
98  if (agg_name == std::string("MIN")) {
99  return kMIN;
100  }
101  if (agg_name == std::string("MAX")) {
102  return kMAX;
103  }
104  if (agg_name == std::string("SUM")) {
105  return kSUM;
106  }
107  if (agg_name == std::string("AVG")) {
108  return kAVG;
109  }
110  if (agg_name == std::string("APPROX_COUNT_DISTINCT")) {
111  return kAPPROX_COUNT_DISTINCT;
112  }
113  if (agg_name == "APPROX_MEDIAN" || agg_name == "APPROX_PERCENTILE" ||
114  agg_name == "APPROX_QUANTILE") {
115  return kAPPROX_QUANTILE;
116  }
117  if (agg_name == std::string("SAMPLE") || agg_name == std::string("LAST_SAMPLE")) {
118  return kSAMPLE;
119  }
120  if (agg_name == std::string("SINGLE_VALUE")) {
121  return kSINGLE_VALUE;
122  }
123  throw std::runtime_error("Aggregate function " + agg_name + " not supported");
124 }
125 
126 inline SQLTypes to_sql_type(const std::string& type_name) {
127  if (type_name == std::string("BIGINT")) {
128  return kBIGINT;
129  }
130  if (type_name == std::string("INTEGER")) {
131  return kINT;
132  }
133  if (type_name == std::string("TINYINT")) {
134  return kTINYINT;
135  }
136  if (type_name == std::string("SMALLINT")) {
137  return kSMALLINT;
138  }
139  if (type_name == std::string("FLOAT")) {
140  return kFLOAT;
141  }
142  if (type_name == std::string("REAL")) {
143  return kFLOAT;
144  }
145  if (type_name == std::string("DOUBLE")) {
146  return kDOUBLE;
147  }
148  if (type_name == std::string("DECIMAL")) {
149  return kDECIMAL;
150  }
151  if (type_name == std::string("CHAR") || type_name == std::string("VARCHAR")) {
152  return kTEXT;
153  }
154  if (type_name == std::string("BOOLEAN")) {
155  return kBOOLEAN;
156  }
157  if (type_name == std::string("TIMESTAMP")) {
158  return kTIMESTAMP;
159  }
160  if (type_name == std::string("DATE")) {
161  return kDATE;
162  }
163  if (type_name == std::string("TIME")) {
164  return kTIME;
165  }
166  if (type_name == std::string("NULL")) {
167  return kNULLT;
168  }
169  if (type_name == std::string("ARRAY")) {
170  return kARRAY;
171  }
172  if (type_name == std::string("INTERVAL_DAY") ||
173  type_name == std::string("INTERVAL_HOUR") ||
174  type_name == std::string("INTERVAL_MINUTE") ||
175  type_name == std::string("INTERVAL_SECOND")) {
176  return kINTERVAL_DAY_TIME;
177  }
178  if (type_name == std::string("INTERVAL_MONTH") ||
179  type_name == std::string("INTERVAL_YEAR")) {
180  return kINTERVAL_YEAR_MONTH;
181  }
182  if (type_name == std::string("ANY")) {
183  return kEVAL_CONTEXT_TYPE;
184  }
185  if (type_name == std::string("TEXT")) {
186  return kTEXT;
187  }
188  if (type_name == std::string("POINT")) {
189  return kPOINT;
190  }
191  if (type_name == std::string("LINESTRING")) {
192  return kLINESTRING;
193  }
194  if (type_name == std::string("POLYGON")) {
195  return kPOLYGON;
196  }
197  if (type_name == std::string("MULTIPOLYGON")) {
198  return kMULTIPOLYGON;
199  }
200  if (type_name == std::string("GEOMETRY")) {
201  return kGEOMETRY;
202  }
203  if (type_name == std::string("GEOGRAPHY")) {
204  return kGEOGRAPHY;
205  }
206 
207  throw std::runtime_error("Unsupported type: " + type_name);
208 }
209 
210 namespace Analyzer {
211 
212 class Constant;
213 class Expr;
214 
215 } // namespace Analyzer
216 
217 SQLTypeInfo get_agg_type(const SQLAgg agg_kind, const Analyzer::Expr* arg_expr);
218 
219 ExtractField to_datepart_field(const std::string&);
220 
221 DateaddField to_dateadd_field(const std::string&);
222 
223 DatetruncField to_datediff_field(const std::string&);
224 
225 std::shared_ptr<Analyzer::Constant> make_fp_constant(const int64_t val,
226  const SQLTypeInfo& ti);
227 
228 #endif // QUERYENGINE_CALCITEDESERIALIZERUTILS_H
SQLTypes to_sql_type(const std::string &type_name)
SQLAgg
Definition: sqldefs.h:71
Definition: sqltypes.h:49
SQLTypes
Definition: sqltypes.h:38
SQLAgg to_agg_kind(const std::string &agg_name)
SQLOps
Definition: sqldefs.h:29
Definition: sqldefs.h:35
Definition: sqldefs.h:36
Definition: sqldefs.h:38
Definition: sqldefs.h:49
Definition: sqldefs.h:30
Definition: sqldefs.h:41
SQLTypeInfo get_agg_type(const SQLAgg agg_kind, const Analyzer::Expr *arg_expr)
Definition: sqldefs.h:73
ExtractField to_datepart_field(const std::string &field)
DateaddField
Definition: DateAdd.h:42
DatetruncField
Definition: DateTruncate.h:27
SQLOps to_sql_op(const std::string &op_str)
Definition: sqldefs.h:37
Definition: sqldefs.h:75
DatetruncField to_datediff_field(const std::string &field)
Definition: sqldefs.h:34
Definition: sqltypes.h:52
Definition: sqltypes.h:53
Definition: sqldefs.h:40
Definition: sqldefs.h:32
Definition: sqldefs.h:76
ExtractField
Definition: sqldefs.h:53
Definition: sqldefs.h:31
std::shared_ptr< Analyzer::Constant > make_fp_constant(const int64_t val, const SQLTypeInfo &ti)
Definition: sqldefs.h:33
Definition: sqltypes.h:45
constexpr auto type_name() noexcept
Definition: sqldefs.h:74
Definition: sqldefs.h:72
Definition: sqldefs.h:39
DateaddField to_dateadd_field(const std::string &field)