OmniSciDB  dfae7c3b14
ThriftSerializers.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 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 
23 #ifndef QUERYENGINE_THRIFTSERIALIZERS_H
24 #define QUERYENGINE_THRIFTSERIALIZERS_H
25 
26 #include "gen-cpp/serialized_result_set_types.h"
27 
28 #include "CompilationOptions.h"
30 #include "Descriptors/Types.h"
31 
33 #include "Logger/Logger.h"
34 
35 namespace ThriftSerializers {
36 
37 #define THRIFT_LAYOUT_CASE(layout) \
38  case QueryDescriptionType::layout: \
39  return TResultSetLayout::layout;
40 
42  switch (layout) {
47  default:
48  CHECK(false);
49  }
50  abort();
51 }
52 
53 #undef THRIFT_LAYOUT_CASE
54 
55 #define UNTHRIFT_LAYOUT_CASE(layout) \
56  case TResultSetLayout::layout: \
57  return QueryDescriptionType::layout;
58 
60  switch (layout) {
65  default:
66  CHECK(false);
67  }
68  abort();
69 }
70 
71 #undef UNTHRIFT_LAYOUT_CASE
72 
73 #define THRIFT_AGGKIND_CASE(kind) \
74  case k##kind: \
75  return TAggKind::kind;
76 
78  switch (agg) {
84  THRIFT_AGGKIND_CASE(APPROX_COUNT_DISTINCT)
85  THRIFT_AGGKIND_CASE(SAMPLE)
86  default:
87  CHECK(false);
88  }
89  abort();
90 }
91 
92 #undef THRIFT_AGGKIND_CASE
93 
94 #define UNTHRIFT_AGGKIND_CASE(kind) \
95  case TAggKind::kind: \
96  return k##kind;
97 
99  switch (agg) {
105  UNTHRIFT_AGGKIND_CASE(APPROX_COUNT_DISTINCT)
106  UNTHRIFT_AGGKIND_CASE(SAMPLE)
107  default:
108  CHECK(false);
109  }
110  abort();
111 }
112 
113 #undef UNTHRIFT_AGGKIND_CASE
114 
115 inline TTypeInfo type_info_to_thrift(const SQLTypeInfo& ti) {
116  TTypeInfo thrift_ti;
117  thrift_ti.type =
119  thrift_ti.encoding = encoding_to_thrift(ti);
120  thrift_ti.nullable = !ti.get_notnull();
121  thrift_ti.is_array = ti.is_array();
122  // TODO: Properly serialize geospatial subtype. For now, the value in precision is the
123  // same as the value in scale; overload the precision field with the subtype of the
124  // geospatial type (currently kGEOMETRY or kGEOGRAPHY)
125  thrift_ti.precision =
126  IS_GEO(ti.get_type()) ? static_cast<int32_t>(ti.get_subtype()) : ti.get_precision();
127  thrift_ti.scale = ti.get_scale();
128  thrift_ti.comp_param = ti.get_comp_param();
129  thrift_ti.size = ti.get_size();
130  return thrift_ti;
131 }
132 
133 inline bool takes_arg(const TargetInfo& target_info) {
134  return target_info.is_agg &&
135  (target_info.agg_kind != kCOUNT || is_distinct_target(target_info));
136 }
137 
138 inline TTargetInfo target_info_to_thrift(const TargetInfo& target_info) {
139  TTargetInfo thrift_target_info;
140  thrift_target_info.is_agg = target_info.is_agg;
141  thrift_target_info.kind = agg_kind_to_thrift(target_info.agg_kind);
142  thrift_target_info.type = type_info_to_thrift(target_info.sql_type);
143  thrift_target_info.arg_type = takes_arg(target_info)
144  ? type_info_to_thrift(target_info.agg_arg_type)
145  : thrift_target_info.type;
146  thrift_target_info.skip_nulls = target_info.skip_null_val;
147  thrift_target_info.is_distinct = target_info.is_distinct;
148  return thrift_target_info;
149 }
150 
151 inline TargetInfo target_info_from_thrift(const TTargetInfo& thrift_target_info) {
152  TargetInfo target_info;
153  target_info.is_agg = thrift_target_info.is_agg;
154  target_info.agg_kind = agg_kind_from_thrift(thrift_target_info.kind);
155  target_info.sql_type = type_info_from_thrift(thrift_target_info.type);
156  target_info.is_distinct = thrift_target_info.is_distinct;
157  target_info.agg_arg_type = takes_arg(target_info)
158  ? type_info_from_thrift(thrift_target_info.arg_type)
159  : SQLTypeInfo(kNULLT, false);
160  target_info.skip_null_val = thrift_target_info.skip_nulls;
161  return target_info;
162 }
163 
164 inline std::vector<TTargetInfo> target_infos_to_thrift(
165  const std::vector<TargetInfo>& targets) {
166  std::vector<TTargetInfo> thrift_targets;
167  for (const auto& target_info : targets) {
168  thrift_targets.push_back(target_info_to_thrift(target_info));
169  }
170  return thrift_targets;
171 }
172 
173 inline std::vector<TargetInfo> target_infos_from_thrift(
174  const std::vector<TTargetInfo>& thrift_targets) {
175  std::vector<TargetInfo> targets;
176  for (const auto& thrift_target_info : thrift_targets) {
177  targets.push_back(target_info_from_thrift(thrift_target_info));
178  }
179  return targets;
180 }
181 
182 #define THRIFT_COUNTDESCRIPTORIMPL_CASE(kind) \
183  case CountDistinctImplType::kind: \
184  return TCountDistinctImplType::kind;
185 
187  const CountDistinctImplType impl_type) {
188  switch (impl_type) {
192  default:
193  CHECK(false);
194  }
195  abort();
196 }
197 
198 #undef THRIFT_COUNTDESCRIPTORIMPL_CASE
199 
200 inline TCountDistinctDescriptor count_distinct_descriptor_to_thrift(
201  const CountDistinctDescriptor& count_distinct_descriptor) {
202  TCountDistinctDescriptor thrift_count_distinct_descriptor;
203  thrift_count_distinct_descriptor.impl_type =
204  count_distinct_impl_type_to_thrift(count_distinct_descriptor.impl_type_);
205  thrift_count_distinct_descriptor.min_val = count_distinct_descriptor.min_val;
206  thrift_count_distinct_descriptor.bitmap_sz_bits =
207  count_distinct_descriptor.bitmap_sz_bits;
208  thrift_count_distinct_descriptor.approximate = count_distinct_descriptor.approximate;
209  thrift_count_distinct_descriptor.device_type =
210  count_distinct_descriptor.device_type == ExecutorDeviceType::GPU ? TDeviceType::GPU
211  : TDeviceType::CPU;
212  thrift_count_distinct_descriptor.sub_bitmap_count =
213  count_distinct_descriptor.sub_bitmap_count;
214  return thrift_count_distinct_descriptor;
215 }
216 
217 #define UNTHRIFT_COUNTDESCRIPTORIMPL_CASE(kind) \
218  case TCountDistinctImplType::kind: \
219  return CountDistinctImplType::kind;
220 
222  const TCountDistinctImplType::type impl_type) {
223  switch (impl_type) {
227  default:
228  CHECK(false);
229  }
230  abort();
231 }
232 
233 #undef UNTHRIFT_COUNTDESCRIPTORIMPL_CASE
234 
236  const TCountDistinctDescriptor& thrift_count_distinct_descriptor) {
237  CountDistinctDescriptor count_distinct_descriptor;
238  count_distinct_descriptor.impl_type_ =
239  count_distinct_impl_type_from_thrift(thrift_count_distinct_descriptor.impl_type);
240  count_distinct_descriptor.min_val = thrift_count_distinct_descriptor.min_val;
241  count_distinct_descriptor.bitmap_sz_bits =
242  thrift_count_distinct_descriptor.bitmap_sz_bits;
243  count_distinct_descriptor.approximate = thrift_count_distinct_descriptor.approximate;
244  count_distinct_descriptor.device_type =
245  thrift_count_distinct_descriptor.device_type == TDeviceType::GPU
248  count_distinct_descriptor.sub_bitmap_count =
249  thrift_count_distinct_descriptor.sub_bitmap_count;
250  return count_distinct_descriptor;
251 }
252 
253 } // namespace ThriftSerializers
254 
255 #endif // QUERYENGINE_THRIFTSERIALIZERS_H
TCountDistinctImplType::type count_distinct_impl_type_to_thrift(const CountDistinctImplType impl_type)
std::vector< TTargetInfo > target_infos_to_thrift(const std::vector< TargetInfo > &targets)
SQLAgg
Definition: sqldefs.h:71
bool is_array() const
Definition: sqltypes.h:425
int get_precision() const
Definition: sqltypes.h:262
Descriptor for the storage layout use for (approximate) count distinct operations.
TDatumType::type type_to_thrift(const SQLTypeInfo &type_info)
#define UNTHRIFT_AGGKIND_CASE(kind)
TCountDistinctDescriptor count_distinct_descriptor_to_thrift(const CountDistinctDescriptor &count_distinct_descriptor)
QueryDescriptionType layout_from_thrift(const TResultSetLayout::type layout)
TargetInfo target_info_from_thrift(const TTargetInfo &thrift_target_info)
SQLTypeInfo sql_type
Definition: TargetInfo.h:42
CountDistinctDescriptor count_distinct_descriptor_from_thrift(const TCountDistinctDescriptor &thrift_count_distinct_descriptor)
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:268
HOST DEVICE int get_size() const
Definition: sqltypes.h:269
bool skip_null_val
Definition: TargetInfo.h:44
CountDistinctImplType impl_type_
HOST DEVICE int get_scale() const
Definition: sqltypes.h:264
SQLTypeInfo agg_arg_type
Definition: TargetInfo.h:43
std::vector< TargetInfo > target_infos_from_thrift(const std::vector< TTargetInfo > &thrift_targets)
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:266
bool is_agg
Definition: TargetInfo.h:40
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:260
bool is_distinct_target(const TargetInfo &target_info)
Definition: TargetInfo.h:129
SQLTypeInfo type_info_from_thrift(const TTypeInfo &thrift_ti, const bool strip_geo_encoding=false)
SQLAgg agg_kind_from_thrift(const TAggKind::type agg)
SQLAgg agg_kind
Definition: TargetInfo.h:41
#define THRIFT_COUNTDESCRIPTORIMPL_CASE(kind)
CountDistinctImplType count_distinct_impl_type_from_thrift(const TCountDistinctImplType::type impl_type)
#define UNTHRIFT_COUNTDESCRIPTORIMPL_CASE(kind)
Definition: sqldefs.h:76
bool takes_arg(const TargetInfo &target_info)
CountDistinctImplType
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:624
#define CHECK(condition)
Definition: Logger.h:197
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:259
QueryDescriptionType
Definition: Types.h:26
TAggKind::type agg_kind_to_thrift(const SQLAgg agg)
TResultSetLayout::type layout_to_thrift(const QueryDescriptionType layout)
#define THRIFT_LAYOUT_CASE(layout)
bool is_distinct
Definition: TargetInfo.h:45
TTypeInfo type_info_to_thrift(const SQLTypeInfo &ti)
#define UNTHRIFT_LAYOUT_CASE(layout)
#define IS_GEO(T)
Definition: sqltypes.h:174
#define THRIFT_AGGKIND_CASE(kind)
TTargetInfo target_info_to_thrift(const TargetInfo &target_info)
TEncodingType::type encoding_to_thrift(const SQLTypeInfo &type_info)