OmniSciDB  a5dc49c757
 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 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 #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("ENCODE_TEXT")) {
74  return kENCODE_TEXT;
75  }
76  if (op_str == std::string("NOT")) {
77  return kNOT;
78  }
79  if (op_str == std::string("IS NULL")) {
80  return kISNULL;
81  }
82  if (op_str == std::string("IS NOT NULL")) {
83  return kISNOTNULL;
84  }
85  if (op_str == std::string("PG_UNNEST")) {
86  return kUNNEST;
87  }
88  if (op_str == std::string("PG_ANY") || op_str == std::string("PG_ALL")) {
89  throw std::runtime_error("Invalid use of " + op_str + " operator");
90  }
91  if (op_str == std::string("IN")) {
92  return kIN;
93  }
94  return kFUNCTION;
95 }
96 
97 inline SQLAgg to_agg_kind(const std::string& agg_name) {
98  if (agg_name == std::string("COUNT")) {
99  return kCOUNT;
100  }
101  if (agg_name == std::string("MIN")) {
102  return kMIN;
103  }
104  if (agg_name == std::string("MAX")) {
105  return kMAX;
106  }
107  if (agg_name == std::string("SUM")) {
108  return kSUM;
109  }
110  if (agg_name == std::string("AVG")) {
111  return kAVG;
112  }
113  if (agg_name == std::string("APPROX_COUNT_DISTINCT")) {
114  return kAPPROX_COUNT_DISTINCT;
115  }
116  if (agg_name == "APPROX_MEDIAN" || agg_name == "APPROX_PERCENTILE" ||
117  agg_name == "APPROX_QUANTILE") {
118  return kAPPROX_QUANTILE;
119  }
120  if (agg_name == std::string("ANY_VALUE") || agg_name == std::string("SAMPLE") ||
121  agg_name == std::string("LAST_SAMPLE")) {
122  return kSAMPLE;
123  }
124  if (agg_name == std::string("SINGLE_VALUE")) {
125  return kSINGLE_VALUE;
126  }
127  if (agg_name == "MODE") {
128  return kMODE;
129  }
130  if (agg_name == "COUNT_IF") {
131  return kCOUNT_IF;
132  }
133  if (agg_name == "SUM_IF") {
134  return kSUM_IF;
135  }
136  throw std::runtime_error("Aggregate function " + agg_name + " not supported");
137 }
138 
139 inline SQLTypes to_sql_type(const std::string& type_name) {
140  if (type_name == std::string("BIGINT")) {
141  return kBIGINT;
142  }
143  if (type_name == std::string("INTEGER")) {
144  return kINT;
145  }
146  if (type_name == std::string("TINYINT")) {
147  return kTINYINT;
148  }
149  if (type_name == std::string("SMALLINT")) {
150  return kSMALLINT;
151  }
152  if (type_name == std::string("FLOAT")) {
153  return kFLOAT;
154  }
155  if (type_name == std::string("REAL")) {
156  return kFLOAT;
157  }
158  if (type_name == std::string("DOUBLE")) {
159  return kDOUBLE;
160  }
161  if (type_name == std::string("DECIMAL")) {
162  return kDECIMAL;
163  }
164  if (type_name == std::string("CHAR") || type_name == std::string("VARCHAR") ||
165  type_name == std::string("SYMBOL")) {
166  return kTEXT;
167  }
168  if (type_name == std::string("BOOLEAN")) {
169  return kBOOLEAN;
170  }
171  if (type_name == std::string("TIMESTAMP")) {
172  return kTIMESTAMP;
173  }
174  if (type_name == std::string("DATE")) {
175  return kDATE;
176  }
177  if (type_name == std::string("TIME")) {
178  return kTIME;
179  }
180  if (type_name == std::string("NULL")) {
181  return kNULLT;
182  }
183  if (type_name == std::string("ARRAY")) {
184  return kARRAY;
185  }
186  if (type_name == std::string("INTERVAL_DAY") ||
187  type_name == std::string("INTERVAL_HOUR") ||
188  type_name == std::string("INTERVAL_MINUTE") ||
189  type_name == std::string("INTERVAL_SECOND")) {
190  return kINTERVAL_DAY_TIME;
191  }
192  if (type_name == std::string("INTERVAL_MONTH") ||
193  type_name == std::string("INTERVAL_YEAR")) {
194  return kINTERVAL_YEAR_MONTH;
195  }
196  if (type_name == std::string("ANY")) {
197  return kEVAL_CONTEXT_TYPE;
198  }
199  if (type_name == std::string("TEXT")) {
200  return kTEXT;
201  }
202  if (type_name == std::string("POINT")) {
203  return kPOINT;
204  }
205  if (type_name == std::string("MULTIPOINT")) {
206  return kMULTIPOINT;
207  }
208  if (type_name == std::string("LINESTRING")) {
209  return kLINESTRING;
210  }
211  if (type_name == std::string("MULTILINESTRING")) {
212  return kMULTILINESTRING;
213  }
214  if (type_name == std::string("POLYGON")) {
215  return kPOLYGON;
216  }
217  if (type_name == std::string("MULTIPOLYGON")) {
218  return kMULTIPOLYGON;
219  }
220  if (type_name == std::string("GEOMETRY")) {
221  return kGEOMETRY;
222  }
223  if (type_name == std::string("GEOGRAPHY")) {
224  return kGEOGRAPHY;
225  }
226 
227  throw std::runtime_error("Unsupported type: " + type_name);
228 }
229 
230 namespace Analyzer {
231 
232 class Constant;
233 class Expr;
234 
235 } // namespace Analyzer
236 
237 SQLTypeInfo get_agg_type(const SQLAgg agg_kind, const Analyzer::Expr* arg_expr);
238 
239 ExtractField to_datepart_field(const std::string&);
240 
241 DateaddField to_dateadd_field(const std::string&);
242 
243 DatetruncField to_datediff_field(const std::string&);
244 
245 std::shared_ptr<Analyzer::Constant> make_fp_constant(const int64_t val,
246  const SQLTypeInfo& ti);
247 
248 #endif // QUERYENGINE_CALCITEDESERIALIZERUTILS_H
SQLTypes to_sql_type(const std::string &type_name)
SQLAgg
Definition: sqldefs.h:76
Definition: sqltypes.h:76
SQLTypes
Definition: sqltypes.h:65
SQLAgg to_agg_kind(const std::string &agg_name)
SQLOps
Definition: sqldefs.h:31
Definition: sqldefs.h:37
Definition: sqldefs.h:38
Definition: sqldefs.h:40
Definition: sqldefs.h:51
Definition: sqldefs.h:32
Definition: sqldefs.h:43
SQLTypeInfo get_agg_type(const SQLAgg agg_kind, const Analyzer::Expr *arg_expr)
Definition: sqldefs.h:78
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:39
Definition: sqldefs.h:80
DatetruncField to_datediff_field(const std::string &field)
Definition: sqldefs.h:36
Definition: sqltypes.h:79
Definition: sqltypes.h:80
Definition: sqldefs.h:42
Definition: sqldefs.h:34
Definition: sqldefs.h:81
ExtractField
Definition: sqldefs.h:55
Definition: sqldefs.h:33
std::shared_ptr< Analyzer::Constant > make_fp_constant(const int64_t val, const SQLTypeInfo &ti)
Definition: sqldefs.h:35
Definition: sqltypes.h:72
constexpr auto type_name() noexcept
Definition: sqldefs.h:79
Definition: sqldefs.h:77
Definition: sqldefs.h:41
Definition: sqldefs.h:86
DateaddField to_dateadd_field(const std::string &field)