OmniSciDB  c07336695a
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 "Shared/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 == std::string("SAMPLE") || agg_name == std::string("LAST_SAMPLE")) {
114  return kSAMPLE;
115  }
116  throw std::runtime_error("Aggregate function " + agg_name + " not supported");
117 }
118 
119 inline SQLTypes to_sql_type(const std::string& type_name) {
120  if (type_name == std::string("BIGINT")) {
121  return kBIGINT;
122  }
123  if (type_name == std::string("INTEGER")) {
124  return kINT;
125  }
126  if (type_name == std::string("TINYINT")) {
127  return kTINYINT;
128  }
129  if (type_name == std::string("SMALLINT")) {
130  return kSMALLINT;
131  }
132  if (type_name == std::string("FLOAT")) {
133  return kFLOAT;
134  }
135  if (type_name == std::string("DOUBLE")) {
136  return kDOUBLE;
137  }
138  if (type_name == std::string("DECIMAL")) {
139  return kDECIMAL;
140  }
141  if (type_name == std::string("CHAR") || type_name == std::string("VARCHAR")) {
142  return kTEXT;
143  }
144  if (type_name == std::string("BOOLEAN")) {
145  return kBOOLEAN;
146  }
147  if (type_name == std::string("TIMESTAMP")) {
148  return kTIMESTAMP;
149  }
150  if (type_name == std::string("DATE")) {
151  return kDATE;
152  }
153  if (type_name == std::string("TIME")) {
154  return kTIME;
155  }
156  if (type_name == std::string("NULL")) {
157  return kNULLT;
158  }
159  if (type_name == std::string("ARRAY")) {
160  return kARRAY;
161  }
162  if (type_name == std::string("INTERVAL_DAY") ||
163  type_name == std::string("INTERVAL_HOUR") ||
164  type_name == std::string("INTERVAL_MINUTE") ||
165  type_name == std::string("INTERVAL_SECOND")) {
166  return kINTERVAL_DAY_TIME;
167  }
168  if (type_name == std::string("INTERVAL_MONTH") ||
169  type_name == std::string("INTERVAL_YEAR")) {
170  return kINTERVAL_YEAR_MONTH;
171  }
172  if (type_name == std::string("ANY")) {
173  return kEVAL_CONTEXT_TYPE;
174  }
175 
176  throw std::runtime_error("Unsupported type: " + type_name);
177 }
178 
179 namespace Analyzer {
180 
181 class Constant;
182 class Expr;
183 
184 } // namespace Analyzer
185 
186 SQLTypeInfo get_agg_type(const SQLAgg agg_kind, const Analyzer::Expr* arg_expr);
187 
188 ExtractField to_datepart_field(const std::string&);
189 
190 DateaddField to_dateadd_field(const std::string&);
191 
192 DatetruncField to_datediff_field(const std::string&);
193 
194 std::shared_ptr<Analyzer::Constant> make_fp_constant(const int64_t val,
195  const SQLTypeInfo& ti);
196 
197 #endif // QUERYENGINE_CALCITEDESERIALIZERUTILS_H
SQLTypes to_sql_type(const std::string &type_name)
SQLAgg
Definition: sqldefs.h:71
Definition: sqltypes.h:51
SQLTypes
Definition: sqltypes.h:40
std::shared_ptr< Analyzer::Constant > make_fp_constant(const int64_t val, const SQLTypeInfo &ti)
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:71
DateaddField
Definition: DateAdd.h:42
DatetruncField
Definition: DateTruncate.h:42
SQLOps to_sql_op(const std::string &op_str)
DatetruncField to_datediff_field(const std::string &)
Definition: sqldefs.h:37
Definition: sqldefs.h:71
DateaddField to_dateadd_field(const std::string &)
Definition: sqldefs.h:34
Definition: sqltypes.h:54
Definition: sqltypes.h:55
Definition: sqldefs.h:40
Definition: sqldefs.h:32
Definition: sqldefs.h:71
ExtractField
Definition: sqldefs.h:53
Definition: sqldefs.h:31
Definition: sqldefs.h:33
Definition: sqltypes.h:47
Definition: sqldefs.h:71
ExtractField to_datepart_field(const std::string &)
Definition: sqldefs.h:71
Definition: sqldefs.h:39