OmniSciDB  a47db9e897
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ThriftTypesConvert.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 THRIFT_TYPE_CONVERT_H
18 #define THRIFT_TYPE_CONVERT_H
19 
20 #include "gen-cpp/mapd_types.h"
21 
22 #include "Logger.h"
23 
24 #include <string>
25 
26 inline TDatumType::type type_to_thrift(const SQLTypeInfo& type_info) {
27  SQLTypes type = type_info.get_type();
28  if (type == kARRAY) {
29  type = type_info.get_subtype();
30  }
31  switch (type) {
32  case kBOOLEAN:
33  return TDatumType::BOOL;
34  case kTINYINT:
35  return TDatumType::TINYINT;
36  case kSMALLINT:
37  return TDatumType::SMALLINT;
38  case kINT:
39  return TDatumType::INT;
40  case kBIGINT:
41  return TDatumType::BIGINT;
42  case kFLOAT:
43  return TDatumType::FLOAT;
44  case kNUMERIC:
45  case kDECIMAL:
46  return TDatumType::DECIMAL;
47  case kDOUBLE:
48  return TDatumType::DOUBLE;
49  case kTEXT:
50  case kVARCHAR:
51  case kCHAR:
52  return TDatumType::STR;
53  case kTIME:
54  return TDatumType::TIME;
55  case kTIMESTAMP:
56  return TDatumType::TIMESTAMP;
57  case kDATE:
58  return TDatumType::DATE;
59  case kINTERVAL_DAY_TIME:
60  return TDatumType::INTERVAL_DAY_TIME;
62  return TDatumType::INTERVAL_YEAR_MONTH;
63  case kPOINT:
64  return TDatumType::POINT;
65  case kLINESTRING:
66  return TDatumType::LINESTRING;
67  case kPOLYGON:
68  return TDatumType::POLYGON;
69  case kMULTIPOLYGON:
70  return TDatumType::MULTIPOLYGON;
71  case kGEOMETRY:
72  return TDatumType::GEOMETRY;
73  case kGEOGRAPHY:
74  return TDatumType::GEOGRAPHY;
75  default:
76  break;
77  }
78  abort();
79 }
80 
82  switch (type) {
83  case TDatumType::BOOL:
84  return kBOOLEAN;
85  case TDatumType::TINYINT:
86  return kTINYINT;
87  case TDatumType::SMALLINT:
88  return kSMALLINT;
89  case TDatumType::INT:
90  return kINT;
91  case TDatumType::BIGINT:
92  return kBIGINT;
93  case TDatumType::FLOAT:
94  return kFLOAT;
95  case TDatumType::DECIMAL:
96  return kDECIMAL;
97  case TDatumType::DOUBLE:
98  return kDOUBLE;
99  case TDatumType::STR:
100  return kTEXT;
101  case TDatumType::TIME:
102  return kTIME;
103  case TDatumType::TIMESTAMP:
104  return kTIMESTAMP;
105  case TDatumType::DATE:
106  return kDATE;
107  case TDatumType::INTERVAL_DAY_TIME:
108  return kINTERVAL_DAY_TIME;
109  case TDatumType::INTERVAL_YEAR_MONTH:
110  return kINTERVAL_YEAR_MONTH;
111  case TDatumType::POINT:
112  return kPOINT;
113  case TDatumType::LINESTRING:
114  return kLINESTRING;
115  case TDatumType::POLYGON:
116  return kPOLYGON;
117  case TDatumType::MULTIPOLYGON:
118  return kMULTIPOLYGON;
119  case TDatumType::GEOMETRY:
120  return kGEOMETRY;
121  case TDatumType::GEOGRAPHY:
122  return kGEOGRAPHY;
123  default:
124  break;
125  }
126  abort();
127 }
128 
129 #define THRIFT_ENCODING_CASE(encoding) \
130  case kENCODING_##encoding: \
131  return TEncodingType::encoding;
132 
133 #define UNTHRIFT_ENCODING_CASE(encoding) \
134  case TEncodingType::encoding: \
135  return kENCODING_##encoding;
136 
138  switch (type_info.get_compression()) {
144  THRIFT_ENCODING_CASE(SPARSE)
145  THRIFT_ENCODING_CASE(GEOINT)
146  THRIFT_ENCODING_CASE(DATE_IN_DAYS)
147  default:
148  CHECK(false);
149  }
150  abort();
151 }
152 
153 #undef ENCODING_CASE
154 
156  switch (tEncodingType) {
162  UNTHRIFT_ENCODING_CASE(SPARSE)
163  UNTHRIFT_ENCODING_CASE(GEOINT)
164  UNTHRIFT_ENCODING_CASE(DATE_IN_DAYS)
165  default:
166  CHECK(false);
167  }
168  abort();
169 }
170 
171 inline std::string thrift_to_name(const TTypeInfo& ti) {
172  const auto type = thrift_to_type(ti.type);
173  const auto encoding = thrift_to_encoding(ti.encoding);
174  auto internal_ti = SQLTypeInfo(ti.is_array ? kARRAY : type,
175  0,
176  0,
177  !ti.nullable,
178  encoding,
179  ti.comp_param,
180  ti.is_array ? type : kNULLT);
181  if (type == kDECIMAL || type == kNUMERIC) {
182  internal_ti.set_precision(ti.precision);
183  internal_ti.set_scale(ti.scale);
184  } else if (type == kTIMESTAMP) {
185  internal_ti.set_precision(ti.precision);
186  }
187  if (IS_GEO(type)) {
188  internal_ti.set_subtype(static_cast<SQLTypes>(ti.precision));
189  internal_ti.set_input_srid(ti.scale);
190  internal_ti.set_output_srid(ti.scale);
191  }
192  internal_ti.set_size(ti.size);
193  return internal_ti.get_type_name();
194 }
195 
196 inline std::string thrift_to_encoding_name(const TTypeInfo& ti) {
197  const auto type = thrift_to_type(ti.type);
198  const auto encoding = thrift_to_encoding(ti.encoding);
199  auto internal_ti = SQLTypeInfo(ti.is_array ? kARRAY : type,
200  0,
201  0,
202  !ti.nullable,
203  encoding,
204  0,
205  ti.is_array ? type : kNULLT);
206  return internal_ti.get_compression_name();
207 }
208 
209 inline SQLTypeInfo type_info_from_thrift(const TTypeInfo& thrift_ti,
210  const bool strip_geo_encoding = false) {
211  const auto ti = thrift_to_type(thrift_ti.type);
212  if (IS_GEO(ti)) {
213  const auto base_type = static_cast<SQLTypes>(thrift_ti.precision);
214  CHECK_LT(base_type, kSQLTYPE_LAST);
215  return SQLTypeInfo(
216  ti,
217  thrift_ti.scale,
218  thrift_ti.scale,
219  !thrift_ti.nullable,
220  strip_geo_encoding ? kENCODING_NONE : thrift_to_encoding(thrift_ti.encoding),
221  thrift_ti.comp_param,
222  base_type);
223  }
224  if (thrift_ti.is_array) {
225  auto ati = SQLTypeInfo(kARRAY,
226  thrift_ti.precision,
227  thrift_ti.scale,
228  !thrift_ti.nullable,
229  thrift_to_encoding(thrift_ti.encoding),
230  thrift_ti.comp_param,
231  ti);
232  ati.set_size(thrift_ti.size);
233  return ati;
234  }
235  return SQLTypeInfo(ti,
236  thrift_ti.precision,
237  thrift_ti.scale,
238  !thrift_ti.nullable,
239  thrift_to_encoding(thrift_ti.encoding),
240  thrift_ti.comp_param,
241  kNULLT);
242 }
243 
244 #endif // THRIFT_TYPE_CONVERT_H
#define THRIFT_ENCODING_CASE(encoding)
Definition: sqltypes.h:52
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:334
TDatumType::type type_to_thrift(const SQLTypeInfo &type_info)
SQLTypes
Definition: sqltypes.h:41
EncodingType
Definition: encodetypes.h:22
EncodingType thrift_to_encoding(const TEncodingType::type tEncodingType)
SQLTypes thrift_to_type(const TDatumType::type &type)
CHECK(cgen_state)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
SQLTypeInfo type_info_from_thrift(const TTypeInfo &thrift_ti, const bool strip_geo_encoding=false)
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
std::string thrift_to_encoding_name(const TTypeInfo &ti)
std::string thrift_to_name(const TTypeInfo &ti)
#define CHECK_LT(x, y)
Definition: Logger.h:200
Definition: sqltypes.h:55
Definition: sqltypes.h:56
#define UNTHRIFT_ENCODING_CASE(encoding)
Definition: sqltypes.h:44
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:327
Definition: sqltypes.h:48
#define IS_GEO(T)
Definition: sqltypes.h:167
TEncodingType::type encoding_to_thrift(const SQLTypeInfo &type_info)