OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ThriftTypesConvert.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 THRIFT_TYPE_CONVERT_H
18 #define THRIFT_TYPE_CONVERT_H
19 
20 #include "gen-cpp/heavy_types.h"
21 
22 #include "Logger/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 || type == kCOLUMN || type == kCOLUMN_LIST) {
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 kMULTIPOINT:
66  return TDatumType::MULTIPOINT;
67  case kLINESTRING:
68  return TDatumType::LINESTRING;
69  case kMULTILINESTRING:
70  return TDatumType::MULTILINESTRING;
71  case kPOLYGON:
72  return TDatumType::POLYGON;
73  case kMULTIPOLYGON:
74  return TDatumType::MULTIPOLYGON;
75  case kGEOMETRY:
76  return TDatumType::GEOMETRY;
77  case kGEOGRAPHY:
78  return TDatumType::GEOGRAPHY;
79  default:
80  break;
81  }
82  abort();
83 }
84 
86  switch (type) {
87  case TDatumType::BOOL:
88  return kBOOLEAN;
89  case TDatumType::TINYINT:
90  return kTINYINT;
91  case TDatumType::SMALLINT:
92  return kSMALLINT;
93  case TDatumType::INT:
94  return kINT;
95  case TDatumType::BIGINT:
96  return kBIGINT;
97  case TDatumType::FLOAT:
98  return kFLOAT;
99  case TDatumType::DECIMAL:
100  return kDECIMAL;
101  case TDatumType::DOUBLE:
102  return kDOUBLE;
103  case TDatumType::STR:
104  return kTEXT;
105  case TDatumType::TIME:
106  return kTIME;
107  case TDatumType::TIMESTAMP:
108  return kTIMESTAMP;
109  case TDatumType::DATE:
110  return kDATE;
111  case TDatumType::INTERVAL_DAY_TIME:
112  return kINTERVAL_DAY_TIME;
113  case TDatumType::INTERVAL_YEAR_MONTH:
114  return kINTERVAL_YEAR_MONTH;
115  case TDatumType::POINT:
116  return kPOINT;
117  case TDatumType::MULTIPOINT:
118  return kMULTIPOINT;
119  case TDatumType::LINESTRING:
120  return kLINESTRING;
121  case TDatumType::MULTILINESTRING:
122  return kMULTILINESTRING;
123  case TDatumType::POLYGON:
124  return kPOLYGON;
125  case TDatumType::MULTIPOLYGON:
126  return kMULTIPOLYGON;
127  case TDatumType::GEOMETRY:
128  return kGEOMETRY;
129  case TDatumType::GEOGRAPHY:
130  return kGEOGRAPHY;
131  default:
132  break;
133  }
134  abort();
135 }
136 
137 #define THRIFT_ENCODING_CASE(encoding) \
138  case kENCODING_##encoding: \
139  return TEncodingType::encoding;
140 
141 #define UNTHRIFT_ENCODING_CASE(encoding) \
142  case TEncodingType::encoding: \
143  return kENCODING_##encoding;
144 
146  switch (type_info.get_compression()) {
148  THRIFT_ENCODING_CASE(FIXED)
152  THRIFT_ENCODING_CASE(SPARSE)
153  THRIFT_ENCODING_CASE(GEOINT)
154  THRIFT_ENCODING_CASE(DATE_IN_DAYS)
155  default:
156  CHECK(false);
157  }
158  abort();
159 }
160 
161 #undef ENCODING_CASE
162 
164  switch (tEncodingType) {
170  UNTHRIFT_ENCODING_CASE(SPARSE)
171  UNTHRIFT_ENCODING_CASE(GEOINT)
172  UNTHRIFT_ENCODING_CASE(DATE_IN_DAYS)
173  default:
174  CHECK(false);
175  }
176  abort();
177 }
178 
179 inline std::string thrift_to_name(const TTypeInfo& ti) {
180  const auto type = thrift_to_type(ti.type);
181  const auto encoding = thrift_to_encoding(ti.encoding);
182  auto internal_ti = SQLTypeInfo(ti.is_array ? kARRAY : type,
183  0,
184  0,
185  !ti.nullable,
186  encoding,
187  ti.comp_param,
188  ti.is_array ? type : kNULLT);
189  if (type == kDECIMAL || type == kNUMERIC) {
190  internal_ti.set_precision(ti.precision);
191  internal_ti.set_scale(ti.scale);
192  } else if (type == kTIMESTAMP) {
193  internal_ti.set_precision(ti.precision);
194  }
195  if (IS_GEO(type)) {
196  internal_ti.set_subtype(static_cast<SQLTypes>(ti.precision));
197  internal_ti.set_input_srid(ti.scale);
198  internal_ti.set_output_srid(ti.scale);
199  }
200  internal_ti.set_size(ti.size);
201  return internal_ti.get_type_name();
202 }
203 
204 inline std::string thrift_to_encoding_name(const TTypeInfo& ti) {
205  const auto type = thrift_to_type(ti.type);
206  const auto encoding = thrift_to_encoding(ti.encoding);
207  auto internal_ti = SQLTypeInfo(ti.is_array ? kARRAY : type,
208  0,
209  0,
210  !ti.nullable,
211  encoding,
212  0,
213  ti.is_array ? type : kNULLT);
214  return internal_ti.get_compression_name();
215 }
216 
217 inline SQLTypeInfo type_info_from_thrift(const TTypeInfo& thrift_ti,
218  const bool strip_geo_encoding = false) {
219  SQLTypeInfo type_info;
220  const auto ti = thrift_to_type(thrift_ti.type);
221  if (IS_GEO(ti)) {
222  const auto base_type = static_cast<SQLTypes>(thrift_ti.precision);
223  CHECK_LT(base_type, kSQLTYPE_LAST);
224  type_info = SQLTypeInfo(
225  ti,
226  thrift_ti.scale,
227  thrift_ti.scale,
228  !thrift_ti.nullable,
229  strip_geo_encoding ? kENCODING_NONE : thrift_to_encoding(thrift_ti.encoding),
230  thrift_ti.comp_param,
231  base_type);
232  } else if (thrift_ti.is_array) {
233  type_info = SQLTypeInfo(kARRAY,
234  thrift_ti.precision,
235  thrift_ti.scale,
236  !thrift_ti.nullable,
237  thrift_to_encoding(thrift_ti.encoding),
238  thrift_ti.comp_param,
239  ti);
240  type_info.set_size(thrift_ti.size);
241  } else {
242  type_info = SQLTypeInfo(ti,
243  thrift_ti.precision,
244  thrift_ti.scale,
245  !thrift_ti.nullable,
246  thrift_to_encoding(thrift_ti.encoding),
247  thrift_ti.comp_param,
248  kNULLT);
249  }
250  if (type_info.is_dict_encoded_string() || type_info.is_subtype_dict_encoded_string()) {
251  const auto& dict_key = thrift_ti.dict_key;
252  type_info.setStringDictKey({dict_key.db_id, dict_key.dict_id});
253  }
254  return type_info;
255 }
256 
257 #endif // THRIFT_TYPE_CONVERT_H
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:392
void set_size(int s)
Definition: sqltypes.h:476
#define THRIFT_ENCODING_CASE(encoding)
Definition: sqltypes.h:76
TDatumType::type type_to_thrift(const SQLTypeInfo &type_info)
SQLTypes
Definition: sqltypes.h:65
EncodingType thrift_to_encoding(const TEncodingType::type tEncodingType)
SQLTypes thrift_to_type(const TDatumType::type &type)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:391
EncodingType
Definition: sqltypes.h:240
bool is_subtype_dict_encoded_string() const
Definition: sqltypes.h:649
SQLTypeInfo type_info_from_thrift(const TTypeInfo &thrift_ti, const bool strip_geo_encoding=false)
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:303
Definition: sqltypes.h:79
Definition: sqltypes.h:80
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:399
void setStringDictKey(const shared::StringDictKey &dict_key)
Definition: sqltypes.h:1061
#define UNTHRIFT_ENCODING_CASE(encoding)
Definition: sqltypes.h:68
#define CHECK(condition)
Definition: Logger.h:291
bool is_dict_encoded_string() const
Definition: sqltypes.h:641
Definition: sqltypes.h:72
#define IS_GEO(T)
Definition: sqltypes.h:310
TEncodingType::type encoding_to_thrift(const SQLTypeInfo &type_info)