OmniSciDB  bf83d84833
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 "Logger/Logger.h"
36 
37 namespace ThriftSerializers {
38 
39 #define THRIFT_LAYOUT_CASE(layout) \
40  case QueryDescriptionType::layout: \
41  return TResultSetLayout::layout;
42 
44  switch (layout) {
49  default:
50  CHECK(false) << static_cast<int>(layout);
51  }
52  abort();
53 }
54 
55 #undef THRIFT_LAYOUT_CASE
56 
57 #define UNTHRIFT_LAYOUT_CASE(layout) \
58  case TResultSetLayout::layout: \
59  return QueryDescriptionType::layout;
60 
62  switch (layout) {
67  default:
68  CHECK(false) << static_cast<int>(layout);
69  }
70  abort();
71 }
72 
73 #undef UNTHRIFT_LAYOUT_CASE
74 
75 #define THRIFT_AGGKIND_CASE(kind) \
76  case k##kind: \
77  return TAggKind::kind;
78 
80  switch (agg) {
86  THRIFT_AGGKIND_CASE(APPROX_COUNT_DISTINCT)
87  THRIFT_AGGKIND_CASE(SAMPLE)
88  THRIFT_AGGKIND_CASE(SINGLE_VALUE)
89  default:
90  CHECK(false) << static_cast<int>(agg);
91  }
92  abort();
93 }
94 
95 #undef THRIFT_AGGKIND_CASE
96 
97 #define UNTHRIFT_AGGKIND_CASE(kind) \
98  case TAggKind::kind: \
99  return k##kind;
100 
102  switch (agg) {
108  UNTHRIFT_AGGKIND_CASE(APPROX_COUNT_DISTINCT)
109  UNTHRIFT_AGGKIND_CASE(SAMPLE)
110  UNTHRIFT_AGGKIND_CASE(SINGLE_VALUE)
111  default:
112  CHECK(false) << static_cast<int>(agg);
113  }
114  abort();
115 }
116 
117 #undef UNTHRIFT_AGGKIND_CASE
118 
120  const std::vector<TColumnRange>& thrift_column_ranges) {
121  AggregatedColRange column_ranges;
122  for (const auto& thrift_column_range : thrift_column_ranges) {
123  PhysicalInput phys_input{thrift_column_range.col_id, thrift_column_range.table_id};
124  switch (thrift_column_range.type) {
125  case TExpressionRangeType::INTEGER:
126  column_ranges.setColRange(
127  phys_input,
128  ExpressionRange::makeIntRange(thrift_column_range.int_min,
129  thrift_column_range.int_max,
130  thrift_column_range.bucket,
131  thrift_column_range.has_nulls));
132  break;
133  case TExpressionRangeType::FLOAT:
134  column_ranges.setColRange(
135  phys_input,
136  ExpressionRange::makeFloatRange(thrift_column_range.fp_min,
137  thrift_column_range.fp_max,
138  thrift_column_range.has_nulls));
139  break;
140  case TExpressionRangeType::DOUBLE:
141  column_ranges.setColRange(
142  phys_input,
143  ExpressionRange::makeDoubleRange(thrift_column_range.fp_min,
144  thrift_column_range.fp_max,
145  thrift_column_range.has_nulls));
146  break;
147  case TExpressionRangeType::INVALID:
148  column_ranges.setColRange(phys_input, ExpressionRange::makeInvalidRange());
149  break;
150  default:
151  CHECK(false);
152  }
153  }
154  return column_ranges;
155 }
156 
158  const std::vector<TDictionaryGeneration>& thrift_string_dictionary_generations) {
159  StringDictionaryGenerations string_dictionary_generations;
160  for (const auto& thrift_string_dictionary_generation :
161  thrift_string_dictionary_generations) {
162  string_dictionary_generations.setGeneration(
163  thrift_string_dictionary_generation.dict_id,
164  thrift_string_dictionary_generation.entry_count);
165  }
166  return string_dictionary_generations;
167 }
168 
169 inline TTypeInfo type_info_to_thrift(const SQLTypeInfo& ti) {
170  TTypeInfo thrift_ti;
171  thrift_ti.type =
173  thrift_ti.encoding = encoding_to_thrift(ti);
174  thrift_ti.nullable = !ti.get_notnull();
175  thrift_ti.is_array = ti.is_array();
176  // TODO: Properly serialize geospatial subtype. For now, the value in precision is the
177  // same as the value in scale; overload the precision field with the subtype of the
178  // geospatial type (currently kGEOMETRY or kGEOGRAPHY)
179  thrift_ti.precision =
180  IS_GEO(ti.get_type()) ? static_cast<int32_t>(ti.get_subtype()) : ti.get_precision();
181  thrift_ti.scale = ti.get_scale();
182  thrift_ti.comp_param = ti.get_comp_param();
183  thrift_ti.size = ti.get_size();
184  return thrift_ti;
185 }
186 
187 inline bool takes_arg(const TargetInfo& target_info) {
188  return target_info.is_agg &&
189  (target_info.agg_kind != kCOUNT || is_distinct_target(target_info));
190 }
191 
192 inline std::vector<TargetMetaInfo> target_meta_infos_from_thrift(
193  const TRowDescriptor& row_desc) {
194  std::vector<TargetMetaInfo> target_meta_infos;
195  for (const auto& col : row_desc) {
196  target_meta_infos.emplace_back(col.col_name, type_info_from_thrift(col.col_type));
197  }
198  return target_meta_infos;
199 }
200 
201 inline void fixup_geo_column_descriptor(TColumnType& col_type,
202  const SQLTypes subtype,
203  const int output_srid) {
204  col_type.col_type.precision = static_cast<int>(subtype);
205  col_type.col_type.scale = output_srid;
206 }
207 
208 inline TColumnType target_meta_info_to_thrift(const TargetMetaInfo& target,
209  const size_t idx) {
210  TColumnType proj_info;
211  proj_info.col_name = target.get_resname();
212  if (proj_info.col_name.empty()) {
213  proj_info.col_name = "result_" + std::to_string(idx + 1);
214  }
215  const auto& target_ti = target.get_type_info();
216  proj_info.col_type.type = type_to_thrift(target_ti);
217  proj_info.col_type.encoding = encoding_to_thrift(target_ti);
218  proj_info.col_type.nullable = !target_ti.get_notnull();
219  proj_info.col_type.is_array = target_ti.get_type() == kARRAY;
220  if (IS_GEO(target_ti.get_type())) {
222  proj_info, target_ti.get_subtype(), target_ti.get_output_srid());
223  } else {
224  proj_info.col_type.precision = target_ti.get_precision();
225  proj_info.col_type.scale = target_ti.get_scale();
226  }
227  if (target_ti.get_type() == kDATE) {
228  proj_info.col_type.size = target_ti.get_size();
229  }
230  proj_info.col_type.comp_param =
231  (target_ti.is_date_in_days() && target_ti.get_comp_param() == 0)
232  ? 32
233  : target_ti.get_comp_param();
234  return proj_info;
235 }
236 
237 inline TRowDescriptor target_meta_infos_to_thrift(
238  const std::vector<TargetMetaInfo>& targets) {
239  TRowDescriptor row_desc;
240  size_t i = 0;
241  for (const auto& target : targets) {
242  row_desc.push_back(target_meta_info_to_thrift(target, i));
243  ++i;
244  }
245  return row_desc;
246 }
247 
248 inline TTargetInfo target_info_to_thrift(const TargetInfo& target_info) {
249  TTargetInfo thrift_target_info;
250  thrift_target_info.is_agg = target_info.is_agg;
251  thrift_target_info.kind = agg_kind_to_thrift(target_info.agg_kind);
252  thrift_target_info.type = type_info_to_thrift(target_info.sql_type);
253  thrift_target_info.arg_type = takes_arg(target_info)
254  ? type_info_to_thrift(target_info.agg_arg_type)
255  : thrift_target_info.type;
256  thrift_target_info.skip_nulls = target_info.skip_null_val;
257  thrift_target_info.is_distinct = target_info.is_distinct;
258  return thrift_target_info;
259 }
260 
261 inline TargetInfo target_info_from_thrift(const TTargetInfo& thrift_target_info) {
262  TargetInfo target_info;
263  target_info.is_agg = thrift_target_info.is_agg;
264  target_info.agg_kind = agg_kind_from_thrift(thrift_target_info.kind);
265  target_info.sql_type = type_info_from_thrift(thrift_target_info.type);
266  target_info.is_distinct = thrift_target_info.is_distinct;
267  target_info.agg_arg_type = takes_arg(target_info)
268  ? type_info_from_thrift(thrift_target_info.arg_type)
269  : SQLTypeInfo(kNULLT, false);
270  target_info.skip_null_val = thrift_target_info.skip_nulls;
271  return target_info;
272 }
273 
274 inline std::vector<TTargetInfo> target_infos_to_thrift(
275  const std::vector<TargetInfo>& targets) {
276  std::vector<TTargetInfo> thrift_targets;
277  for (const auto& target_info : targets) {
278  thrift_targets.push_back(target_info_to_thrift(target_info));
279  }
280  return thrift_targets;
281 }
282 
283 inline std::vector<TargetInfo> target_infos_from_thrift(
284  const std::vector<TTargetInfo>& thrift_targets) {
285  std::vector<TargetInfo> targets;
286  for (const auto& thrift_target_info : thrift_targets) {
287  targets.push_back(target_info_from_thrift(thrift_target_info));
288  }
289  return targets;
290 }
291 
292 #define THRIFT_COUNTDESCRIPTORIMPL_CASE(kind) \
293  case CountDistinctImplType::kind: \
294  return TCountDistinctImplType::kind;
295 
297  const CountDistinctImplType impl_type) {
298  switch (impl_type) {
302  default:
303  CHECK(false);
304  }
305  abort();
306 }
307 
308 #undef THRIFT_COUNTDESCRIPTORIMPL_CASE
309 
310 inline TCountDistinctDescriptor count_distinct_descriptor_to_thrift(
311  const CountDistinctDescriptor& count_distinct_descriptor) {
312  TCountDistinctDescriptor thrift_count_distinct_descriptor;
313  thrift_count_distinct_descriptor.impl_type =
314  count_distinct_impl_type_to_thrift(count_distinct_descriptor.impl_type_);
315  thrift_count_distinct_descriptor.min_val = count_distinct_descriptor.min_val;
316  thrift_count_distinct_descriptor.bitmap_sz_bits =
317  count_distinct_descriptor.bitmap_sz_bits;
318  thrift_count_distinct_descriptor.approximate = count_distinct_descriptor.approximate;
319  thrift_count_distinct_descriptor.device_type =
320  count_distinct_descriptor.device_type == ExecutorDeviceType::GPU ? TDeviceType::GPU
321  : TDeviceType::CPU;
322  thrift_count_distinct_descriptor.sub_bitmap_count =
323  count_distinct_descriptor.sub_bitmap_count;
324  return thrift_count_distinct_descriptor;
325 }
326 
327 #define UNTHRIFT_COUNTDESCRIPTORIMPL_CASE(kind) \
328  case TCountDistinctImplType::kind: \
329  return CountDistinctImplType::kind;
330 
332  const TCountDistinctImplType::type impl_type) {
333  switch (impl_type) {
337  default:
338  CHECK(false);
339  }
340  abort();
341 }
342 
343 #undef UNTHRIFT_COUNTDESCRIPTORIMPL_CASE
344 
346  const TCountDistinctDescriptor& thrift_count_distinct_descriptor) {
347  CountDistinctDescriptor count_distinct_descriptor;
348  count_distinct_descriptor.impl_type_ =
349  count_distinct_impl_type_from_thrift(thrift_count_distinct_descriptor.impl_type);
350  count_distinct_descriptor.min_val = thrift_count_distinct_descriptor.min_val;
351  count_distinct_descriptor.bitmap_sz_bits =
352  thrift_count_distinct_descriptor.bitmap_sz_bits;
353  count_distinct_descriptor.approximate = thrift_count_distinct_descriptor.approximate;
354  count_distinct_descriptor.device_type =
355  thrift_count_distinct_descriptor.device_type == TDeviceType::GPU
358  count_distinct_descriptor.sub_bitmap_count =
359  thrift_count_distinct_descriptor.sub_bitmap_count;
360  return count_distinct_descriptor;
361 }
362 
363 } // namespace ThriftSerializers
364 
365 #endif // QUERYENGINE_THRIFTSERIALIZERS_H
TCountDistinctImplType::type count_distinct_impl_type_to_thrift(const CountDistinctImplType impl_type)
AggregatedColRange column_ranges_from_thrift(const std::vector< TColumnRange > &thrift_column_ranges)
std::vector< TTargetInfo > target_infos_to_thrift(const std::vector< TargetInfo > &targets)
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:312
SQLAgg
Definition: sqldefs.h:71
StringDictionaryGenerations string_dictionary_generations_from_thrift(const std::vector< TDictionaryGeneration > &thrift_string_dictionary_generations)
HOST DEVICE int get_size() const
Definition: sqltypes.h:321
Descriptor for the storage layout use for (approximate) count distinct operations.
TDatumType::type type_to_thrift(const SQLTypeInfo &type_info)
SQLTypes
Definition: sqltypes.h:37
#define UNTHRIFT_AGGKIND_CASE(kind)
TCountDistinctDescriptor count_distinct_descriptor_to_thrift(const CountDistinctDescriptor &count_distinct_descriptor)
TRowDescriptor target_meta_infos_to_thrift(const std::vector< TargetMetaInfo > &targets)
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 int get_scale() const
Definition: sqltypes.h:316
CountDistinctDescriptor count_distinct_descriptor_from_thrift(const TCountDistinctDescriptor &thrift_count_distinct_descriptor)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:311
bool skip_null_val
Definition: TargetInfo.h:44
CountDistinctImplType impl_type_
const SQLTypeInfo & get_type_info() const
static ExpressionRange makeFloatRange(const float fp_min, const float fp_max, const bool has_nulls)
std::string to_string(char const *&&v)
SQLTypeInfo agg_arg_type
Definition: TargetInfo.h:43
std::vector< TargetInfo > target_infos_from_thrift(const std::vector< TTargetInfo > &thrift_targets)
bool is_agg
Definition: TargetInfo.h:40
void setGeneration(const uint32_t id, const uint64_t generation)
bool is_distinct_target(const TargetInfo &target_info)
Definition: TargetInfo.h:130
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
int get_precision() const
Definition: sqltypes.h:314
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
void fixup_geo_column_descriptor(TColumnType &col_type, const SQLTypes subtype, const int output_srid)
#define THRIFT_COUNTDESCRIPTORIMPL_CASE(kind)
static ExpressionRange makeDoubleRange(const double fp_min, const double fp_max, const bool has_nulls)
Definition: sqltypes.h:52
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)
TColumnType target_meta_info_to_thrift(const TargetMetaInfo &target, const size_t idx)
CountDistinctImplType
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:320
#define CHECK(condition)
Definition: Logger.h:197
SQLTypes type
Definition: sqltypes.h:730
QueryDescriptionType
Definition: Types.h:26
std::vector< TargetMetaInfo > target_meta_infos_from_thrift(const TRowDescriptor &row_desc)
TAggKind::type agg_kind_to_thrift(const SQLAgg agg)
void setColRange(const PhysicalInput &, const ExpressionRange &)
TResultSetLayout::type layout_to_thrift(const QueryDescriptionType layout)
static ExpressionRange makeInvalidRange()
const std::string & get_resname() const
#define THRIFT_LAYOUT_CASE(layout)
bool is_distinct
Definition: TargetInfo.h:45
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:318
TTypeInfo type_info_to_thrift(const SQLTypeInfo &ti)
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:697
#define UNTHRIFT_LAYOUT_CASE(layout)
#define IS_GEO(T)
Definition: sqltypes.h:242
#define THRIFT_AGGKIND_CASE(kind)
bool is_array() const
Definition: sqltypes.h:486
TTargetInfo target_info_to_thrift(const TargetInfo &target_info)
TEncodingType::type encoding_to_thrift(const SQLTypeInfo &type_info)