OmniSciDB  c0231cc57d
 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  throw std::runtime_error("Aggregate function " + agg_name + " not supported");
128 }
129 
130 inline SQLTypes to_sql_type(const std::string& type_name) {
131  if (type_name == std::string("BIGINT")) {
132  return kBIGINT;
133  }
134  if (type_name == std::string("INTEGER")) {
135  return kINT;
136  }
137  if (type_name == std::string("TINYINT")) {
138  return kTINYINT;
139  }
140  if (type_name == std::string("SMALLINT")) {
141  return kSMALLINT;
142  }
143  if (type_name == std::string("FLOAT")) {
144  return kFLOAT;
145  }
146  if (type_name == std::string("REAL")) {
147  return kFLOAT;
148  }
149  if (type_name == std::string("DOUBLE")) {
150  return kDOUBLE;
151  }
152  if (type_name == std::string("DECIMAL")) {
153  return kDECIMAL;
154  }
155  if (type_name == std::string("CHAR") || type_name == std::string("VARCHAR") ||
156  type_name == std::string("SYMBOL")) {
157  return kTEXT;
158  }
159  if (type_name == std::string("BOOLEAN")) {
160  return kBOOLEAN;
161  }
162  if (type_name == std::string("TIMESTAMP")) {
163  return kTIMESTAMP;
164  }
165  if (type_name == std::string("DATE")) {
166  return kDATE;
167  }
168  if (type_name == std::string("TIME")) {
169  return kTIME;
170  }
171  if (type_name == std::string("NULL")) {
172  return kNULLT;
173  }
174  if (type_name == std::string("ARRAY")) {
175  return kARRAY;
176  }
177  if (type_name == std::string("INTERVAL_DAY") ||
178  type_name == std::string("INTERVAL_HOUR") ||
179  type_name == std::string("INTERVAL_MINUTE") ||
180  type_name == std::string("INTERVAL_SECOND")) {
181  return kINTERVAL_DAY_TIME;
182  }
183  if (type_name == std::string("INTERVAL_MONTH") ||
184  type_name == std::string("INTERVAL_YEAR")) {
185  return kINTERVAL_YEAR_MONTH;
186  }
187  if (type_name == std::string("ANY")) {
188  return kEVAL_CONTEXT_TYPE;
189  }
190  if (type_name == std::string("TEXT")) {
191  return kTEXT;
192  }
193  if (type_name == std::string("POINT")) {
194  return kPOINT;
195  }
196  if (type_name == std::string("MULTIPOINT")) {
197  return kMULTIPOINT;
198  }
199  if (type_name == std::string("LINESTRING")) {
200  return kLINESTRING;
201  }
202  if (type_name == std::string("MULTILINESTRING")) {
203  return kMULTILINESTRING;
204  }
205  if (type_name == std::string("POLYGON")) {
206  return kPOLYGON;
207  }
208  if (type_name == std::string("MULTIPOLYGON")) {
209  return kMULTIPOLYGON;
210  }
211  if (type_name == std::string("GEOMETRY")) {
212  return kGEOMETRY;
213  }
214  if (type_name == std::string("GEOGRAPHY")) {
215  return kGEOGRAPHY;
216  }
217 
218  throw std::runtime_error("Unsupported type: " + type_name);
219 }
220 
221 namespace Analyzer {
222 
223 class Constant;
224 class Expr;
225 
226 } // namespace Analyzer
227 
228 SQLTypeInfo get_agg_type(const SQLAgg agg_kind, const Analyzer::Expr* arg_expr);
229 
230 ExtractField to_datepart_field(const std::string&);
231 
232 DateaddField to_dateadd_field(const std::string&);
233 
234 DatetruncField to_datediff_field(const std::string&);
235 
236 std::shared_ptr<Analyzer::Constant> make_fp_constant(const int64_t val,
237  const SQLTypeInfo& ti);
238 
239 #endif // QUERYENGINE_CALCITEDESERIALIZERUTILS_H
SQLTypes to_sql_type(const std::string &type_name)
SQLAgg
Definition: sqldefs.h:72
Definition: sqltypes.h:63
SQLTypes
Definition: sqltypes.h:52
SQLAgg to_agg_kind(const std::string &agg_name)
SQLOps
Definition: sqldefs.h:28
Definition: sqldefs.h:34
Definition: sqldefs.h:35
Definition: sqldefs.h:37
Definition: sqldefs.h:48
Definition: sqldefs.h:29
Definition: sqldefs.h:40
SQLTypeInfo get_agg_type(const SQLAgg agg_kind, const Analyzer::Expr *arg_expr)
Definition: sqldefs.h:74
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:36
Definition: sqldefs.h:76
DatetruncField to_datediff_field(const std::string &field)
Definition: sqldefs.h:33
Definition: sqltypes.h:66
Definition: sqltypes.h:67
Definition: sqldefs.h:39
Definition: sqldefs.h:31
Definition: sqldefs.h:77
ExtractField
Definition: sqldefs.h:52
Definition: sqldefs.h:30
std::shared_ptr< Analyzer::Constant > make_fp_constant(const int64_t val, const SQLTypeInfo &ti)
Definition: sqldefs.h:32
Definition: sqltypes.h:59
constexpr auto type_name() noexcept
Definition: sqldefs.h:75
Definition: sqldefs.h:73
Definition: sqldefs.h:38
DateaddField to_dateadd_field(const std::string &field)