OmniSciDB  04ee39c94c
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 "Shared/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  thrift_ti.precision = ti.get_precision();
123  thrift_ti.scale = ti.get_scale();
124  thrift_ti.comp_param = ti.get_comp_param();
125  thrift_ti.size = ti.get_size();
126  return thrift_ti;
127 }
128 
129 inline bool takes_arg(const TargetInfo& target_info) {
130  return target_info.is_agg &&
131  (target_info.agg_kind != kCOUNT || is_distinct_target(target_info));
132 }
133 
134 inline TTargetInfo target_info_to_thrift(const TargetInfo& target_info) {
135  TTargetInfo thrift_target_info;
136  thrift_target_info.is_agg = target_info.is_agg;
137  thrift_target_info.kind = agg_kind_to_thrift(target_info.agg_kind);
138  thrift_target_info.type = type_info_to_thrift(target_info.sql_type);
139  thrift_target_info.arg_type = takes_arg(target_info)
140  ? type_info_to_thrift(target_info.agg_arg_type)
141  : thrift_target_info.type;
142  thrift_target_info.skip_nulls = target_info.skip_null_val;
143  thrift_target_info.is_distinct = target_info.is_distinct;
144  return thrift_target_info;
145 }
146 
147 inline TargetInfo target_info_from_thrift(const TTargetInfo& thrift_target_info) {
148  TargetInfo target_info;
149  target_info.is_agg = thrift_target_info.is_agg;
150  target_info.agg_kind = agg_kind_from_thrift(thrift_target_info.kind);
151  target_info.sql_type = type_info_from_thrift(thrift_target_info.type);
152  target_info.is_distinct = thrift_target_info.is_distinct;
153  target_info.agg_arg_type = takes_arg(target_info)
154  ? type_info_from_thrift(thrift_target_info.arg_type)
155  : SQLTypeInfo(kNULLT, false);
156  target_info.skip_null_val = thrift_target_info.skip_nulls;
157  return target_info;
158 }
159 
160 inline std::vector<TTargetInfo> target_infos_to_thrift(
161  const std::vector<TargetInfo>& targets) {
162  std::vector<TTargetInfo> thrift_targets;
163  for (const auto& target_info : targets) {
164  thrift_targets.push_back(target_info_to_thrift(target_info));
165  }
166  return thrift_targets;
167 }
168 
169 inline std::vector<TargetInfo> target_infos_from_thrift(
170  const std::vector<TTargetInfo>& thrift_targets) {
171  std::vector<TargetInfo> targets;
172  for (const auto& thrift_target_info : thrift_targets) {
173  targets.push_back(target_info_from_thrift(thrift_target_info));
174  }
175  return targets;
176 }
177 
178 #define THRIFT_COUNTDESCRIPTORIMPL_CASE(kind) \
179  case CountDistinctImplType::kind: \
180  return TCountDistinctImplType::kind;
181 
183  const CountDistinctImplType impl_type) {
184  switch (impl_type) {
188  default:
189  CHECK(false);
190  }
191  abort();
192 }
193 
194 #undef THRIFT_COUNTDESCRIPTORIMPL_CASE
195 
196 inline TCountDistinctDescriptor count_distinct_descriptor_to_thrift(
197  const CountDistinctDescriptor& count_distinct_descriptor) {
198  TCountDistinctDescriptor thrift_count_distinct_descriptor;
199  thrift_count_distinct_descriptor.impl_type =
200  count_distinct_impl_type_to_thrift(count_distinct_descriptor.impl_type_);
201  thrift_count_distinct_descriptor.min_val = count_distinct_descriptor.min_val;
202  thrift_count_distinct_descriptor.bitmap_sz_bits =
203  count_distinct_descriptor.bitmap_sz_bits;
204  thrift_count_distinct_descriptor.approximate = count_distinct_descriptor.approximate;
205  thrift_count_distinct_descriptor.device_type =
206  count_distinct_descriptor.device_type == ExecutorDeviceType::GPU ? TDeviceType::GPU
208  thrift_count_distinct_descriptor.sub_bitmap_count =
209  count_distinct_descriptor.sub_bitmap_count;
210  return thrift_count_distinct_descriptor;
211 }
212 
213 #define UNTHRIFT_COUNTDESCRIPTORIMPL_CASE(kind) \
214  case TCountDistinctImplType::kind: \
215  return CountDistinctImplType::kind;
216 
218  const TCountDistinctImplType::type impl_type) {
219  switch (impl_type) {
223  default:
224  CHECK(false);
225  }
226  abort();
227 }
228 
229 #undef UNTHRIFT_COUNTDESCRIPTORIMPL_CASE
230 
232  const TCountDistinctDescriptor& thrift_count_distinct_descriptor) {
233  CountDistinctDescriptor count_distinct_descriptor;
234  count_distinct_descriptor.impl_type_ =
235  count_distinct_impl_type_from_thrift(thrift_count_distinct_descriptor.impl_type);
236  count_distinct_descriptor.min_val = thrift_count_distinct_descriptor.min_val;
237  count_distinct_descriptor.bitmap_sz_bits =
238  thrift_count_distinct_descriptor.bitmap_sz_bits;
239  count_distinct_descriptor.approximate = thrift_count_distinct_descriptor.approximate;
240  count_distinct_descriptor.device_type =
241  thrift_count_distinct_descriptor.device_type == TDeviceType::GPU
244  count_distinct_descriptor.sub_bitmap_count =
245  thrift_count_distinct_descriptor.sub_bitmap_count;
246  return count_distinct_descriptor;
247 }
248 
249 } // namespace ThriftSerializers
250 
251 #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
HOST DEVICE int get_size() const
Definition: sqltypes.h:333
int get_precision() const
Definition: sqltypes.h:326
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
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:330
CountDistinctDescriptor count_distinct_descriptor_from_thrift(const TCountDistinctDescriptor &thrift_count_distinct_descriptor)
HOST DEVICE int get_scale() const
Definition: sqltypes.h:328
bool skip_null_val
Definition: TargetInfo.h:44
CountDistinctImplType impl_type_
SQLTypeInfo agg_arg_type
Definition: TargetInfo.h:43
std::vector< TargetInfo > target_infos_from_thrift(const std::vector< TTargetInfo > &thrift_targets)
bool is_array() const
Definition: sqltypes.h:458
bool is_agg
Definition: TargetInfo.h:40
bool is_distinct_target(const TargetInfo &target_info)
Definition: TargetInfo.h:116
SQLTypeInfoCore get_elem_type() const
Definition: sqltypes.h:632
SQLTypeInfo type_info_from_thrift(const TTypeInfo &thrift_ti, const bool strip_geo_encoding=false)
SQLAgg agg_kind_from_thrift(const TAggKind::type agg)
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:823
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:71
bool takes_arg(const TargetInfo &target_info)
CountDistinctImplType
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:332
#define CHECK(condition)
Definition: Logger.h:187
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 THRIFT_AGGKIND_CASE(kind)
TTargetInfo target_info_to_thrift(const TargetInfo &target_info)
TEncodingType::type encoding_to_thrift(const SQLTypeInfo &type_info)