OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OutputBufferInitialization.cpp
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 
18 #include "BufferCompaction.h"
20 #include "TypePunning.h"
21 
22 #include "../Analyzer/Analyzer.h"
23 
24 extern bool g_bigint_count;
25 
26 std::vector<int64_t> init_agg_val_vec(const std::vector<TargetInfo>& targets,
28  std::vector<int64_t> agg_init_vals;
29  agg_init_vals.reserve(query_mem_desc.getSlotCount());
30  const bool is_group_by{query_mem_desc.isGroupBy()};
31  for (size_t target_idx = 0, agg_col_idx = 0; target_idx < targets.size();
32  ++target_idx, ++agg_col_idx) {
33  CHECK_LT(agg_col_idx, query_mem_desc.getSlotCount());
34  const auto agg_info = targets[target_idx];
35  const auto& agg_ti = agg_info.sql_type;
36  if (!agg_info.is_agg || agg_info.agg_kind == kSAMPLE) {
37  if (agg_info.agg_kind == kSAMPLE && agg_ti.is_string() &&
38  agg_ti.get_compression() != kENCODING_NONE) {
39  agg_init_vals.push_back(
40  get_agg_initial_val(agg_info.agg_kind,
41  agg_ti,
42  is_group_by,
43  query_mem_desc.getCompactByteWidth()));
44  continue;
45  }
46  if (query_mem_desc.getPaddedSlotWidthBytes(agg_col_idx) > 0) {
47  agg_init_vals.push_back(0);
48  }
49  if (agg_info.is_varlen_projection) {
50  continue;
51  }
52  if (agg_ti.is_array() ||
53  (agg_ti.is_string() && agg_ti.get_compression() == kENCODING_NONE)) {
54  agg_init_vals.push_back(0);
55  }
56  if (agg_ti.is_geometry()) {
57  agg_init_vals.push_back(0);
58  for (auto i = 1; i < agg_ti.get_physical_coord_cols(); ++i) {
59  agg_init_vals.push_back(0);
60  agg_init_vals.push_back(0);
61  }
62  }
63  continue;
64  }
65  CHECK_GT(query_mem_desc.getPaddedSlotWidthBytes(agg_col_idx), 0);
66  const bool float_argument_input = takes_float_argument(agg_info);
67  const auto chosen_bytes = query_mem_desc.isLogicalSizedColumnsAllowed()
68  ? query_mem_desc.getPaddedSlotWidthBytes(agg_col_idx)
69  : query_mem_desc.getCompactByteWidth();
70  auto init_ti = get_compact_type(agg_info);
71  if (!is_group_by) {
72  init_ti.set_notnull(false);
73  }
74  agg_init_vals.push_back(
75  get_agg_initial_val(agg_info.agg_kind,
76  init_ti,
77  is_group_by || float_argument_input,
78  (float_argument_input ? sizeof(float) : chosen_bytes)));
79  if (kAVG == agg_info.agg_kind) {
80  ++agg_col_idx;
81  agg_init_vals.push_back(0);
82  }
83  }
84  return agg_init_vals;
85 }
86 
87 std::pair<int64_t, int64_t> inline_int_max_min(const size_t byte_width) {
88  switch (byte_width) {
89  case 1:
90  return std::make_pair(std::numeric_limits<int8_t>::max(),
91  std::numeric_limits<int8_t>::min());
92  case 2:
93  return std::make_pair(std::numeric_limits<int16_t>::max(),
94  std::numeric_limits<int16_t>::min());
95  case 4:
96  return std::make_pair(std::numeric_limits<int32_t>::max(),
97  std::numeric_limits<int32_t>::min());
98  case 8:
99  return std::make_pair(std::numeric_limits<int64_t>::max(),
100  std::numeric_limits<int64_t>::min());
101  default:
102  abort();
103  }
104 }
105 
106 std::pair<uint64_t, uint64_t> inline_uint_max_min(const size_t byte_width) {
107  switch (byte_width) {
108  case 1:
109  return std::make_pair(std::numeric_limits<uint8_t>::max(),
110  std::numeric_limits<uint8_t>::min());
111  case 2:
112  return std::make_pair(std::numeric_limits<uint16_t>::max(),
113  std::numeric_limits<uint16_t>::min());
114  case 4:
115  return std::make_pair(std::numeric_limits<uint32_t>::max(),
116  std::numeric_limits<uint32_t>::min());
117  case 8:
118  return std::make_pair(std::numeric_limits<uint64_t>::max(),
119  std::numeric_limits<uint64_t>::min());
120  default:
121  abort();
122  }
123 }
124 
125 // TODO(alex): proper types for aggregate
126 int64_t get_agg_initial_val(const SQLAgg agg,
127  const SQLTypeInfo& ti,
128  const bool enable_compaction,
129  const unsigned min_byte_width_to_compact) {
130  CHECK(!ti.is_string() || (agg == kSINGLE_VALUE || agg == kSAMPLE));
131  const auto byte_width =
132  enable_compaction
133  ? compact_byte_width(static_cast<unsigned>(get_bit_width(ti) >> 3),
134  unsigned(min_byte_width_to_compact))
135  : sizeof(int64_t);
136  CHECK(ti.get_logical_size() < 0 ||
137  byte_width >= static_cast<unsigned>(ti.get_logical_size()));
138  switch (agg) {
139  case kSUM: {
140  if (!ti.get_notnull()) {
141  if (ti.is_fp()) {
142  switch (byte_width) {
143  case 4: {
144  const float null_float = inline_fp_null_val(ti);
145  return *reinterpret_cast<const int32_t*>(may_alias_ptr(&null_float));
146  }
147  case 8: {
148  const double null_double = inline_fp_null_val(ti);
149  return *reinterpret_cast<const int64_t*>(may_alias_ptr(&null_double));
150  }
151  default:
152  CHECK(false);
153  }
154  } else {
155  return inline_int_null_val(ti);
156  }
157  }
158  switch (byte_width) {
159  case 4: {
160  const float zero_float{0.};
161  return ti.is_fp()
162  ? *reinterpret_cast<const int32_t*>(may_alias_ptr(&zero_float))
163  : 0;
164  }
165  case 8: {
166  const double zero_double{0.};
167  return ti.is_fp()
168  ? *reinterpret_cast<const int64_t*>(may_alias_ptr(&zero_double))
169  : 0;
170  }
171  default:
172  CHECK(false);
173  }
174  }
175  case kAVG:
176  case kCOUNT:
178  return 0;
179  case kAPPROX_QUANTILE:
180  return {}; // Init value is a quantile::TDigest* set elsewhere.
181  case kMIN: {
182  switch (byte_width) {
183  case 1: {
184  CHECK(!ti.is_fp());
185  return ti.get_notnull() ? std::numeric_limits<int8_t>::max()
186  : inline_int_null_val(ti);
187  }
188  case 2: {
189  CHECK(!ti.is_fp());
190  return ti.get_notnull() ? std::numeric_limits<int16_t>::max()
191  : inline_int_null_val(ti);
192  }
193  case 4: {
194  const float max_float = std::numeric_limits<float>::max();
195  const float null_float =
196  ti.is_fp() ? static_cast<float>(inline_fp_null_val(ti)) : 0.;
197  return ti.is_fp()
198  ? (ti.get_notnull()
199  ? *reinterpret_cast<const int32_t*>(may_alias_ptr(&max_float))
200  : *reinterpret_cast<const int32_t*>(
201  may_alias_ptr(&null_float)))
202  : (ti.get_notnull() ? std::numeric_limits<int32_t>::max()
203  : inline_int_null_val(ti));
204  }
205  case 8: {
206  const double max_double = std::numeric_limits<double>::max();
207  const double null_double{ti.is_fp() ? inline_fp_null_val(ti) : 0.};
208  return ti.is_fp() ? (ti.get_notnull() ? *reinterpret_cast<const int64_t*>(
209  may_alias_ptr(&max_double))
210  : *reinterpret_cast<const int64_t*>(
211  may_alias_ptr(&null_double)))
212  : (ti.get_notnull() ? std::numeric_limits<int64_t>::max()
213  : inline_int_null_val(ti));
214  }
215  default:
216  CHECK(false);
217  }
218  }
219  case kSINGLE_VALUE:
220  case kSAMPLE:
221  case kMAX: {
222  switch (byte_width) {
223  case 1: {
224  CHECK(!ti.is_fp());
225  return ti.get_notnull() ? std::numeric_limits<int8_t>::min()
226  : inline_int_null_val(ti);
227  }
228  case 2: {
229  CHECK(!ti.is_fp());
230  return ti.get_notnull() ? std::numeric_limits<int16_t>::min()
231  : inline_int_null_val(ti);
232  }
233  case 4: {
234  const float min_float = -std::numeric_limits<float>::max();
235  const float null_float =
236  ti.is_fp() ? static_cast<float>(inline_fp_null_val(ti)) : 0.;
237  return (ti.is_fp())
238  ? (ti.get_notnull()
239  ? *reinterpret_cast<const int32_t*>(may_alias_ptr(&min_float))
240  : *reinterpret_cast<const int32_t*>(
241  may_alias_ptr(&null_float)))
242  : (ti.get_notnull() ? std::numeric_limits<int32_t>::min()
243  : inline_int_null_val(ti));
244  }
245  case 8: {
246  const double min_double = -std::numeric_limits<double>::max();
247  const double null_double{ti.is_fp() ? inline_fp_null_val(ti) : 0.};
248  return ti.is_fp() ? (ti.get_notnull() ? *reinterpret_cast<const int64_t*>(
249  may_alias_ptr(&min_double))
250  : *reinterpret_cast<const int64_t*>(
251  may_alias_ptr(&null_double)))
252  : (ti.get_notnull() ? std::numeric_limits<int64_t>::min()
253  : inline_int_null_val(ti));
254  }
255  default:
256  CHECK(false);
257  }
258  }
259  default:
260  abort();
261  }
262 }
263 
264 std::vector<int64_t> init_agg_val_vec(
265  const std::vector<Analyzer::Expr*>& targets,
266  const std::list<std::shared_ptr<Analyzer::Expr>>& quals,
268  std::vector<TargetInfo> target_infos;
269  target_infos.reserve(targets.size());
270  const auto agg_col_count = query_mem_desc.getSlotCount();
271  for (size_t target_idx = 0, agg_col_idx = 0;
272  target_idx < targets.size() && agg_col_idx < agg_col_count;
273  ++target_idx, ++agg_col_idx) {
274  const auto target_expr = targets[target_idx];
275  auto target = get_target_info(target_expr, g_bigint_count);
276  auto arg_expr = agg_arg(target_expr);
277  if (arg_expr) {
278  if (query_mem_desc.getQueryDescriptionType() ==
280  target.is_agg &&
281  (target.agg_kind == kMIN || target.agg_kind == kMAX ||
282  target.agg_kind == kSUM || target.agg_kind == kAVG ||
283  target.agg_kind == kAPPROX_QUANTILE)) {
284  set_notnull(target, false);
285  } else if (constrained_not_null(arg_expr, quals)) {
286  set_notnull(target, true);
287  }
288  }
289  target_infos.push_back(target);
290  }
291  return init_agg_val_vec(target_infos, query_mem_desc);
292 }
293 
294 const Analyzer::Expr* agg_arg(const Analyzer::Expr* expr) {
295  const auto agg_expr = dynamic_cast<const Analyzer::AggExpr*>(expr);
296  return agg_expr ? agg_expr->get_arg() : nullptr;
297 }
298 
300  const std::list<std::shared_ptr<Analyzer::Expr>>& quals) {
301  for (const auto& qual : quals) {
302  auto uoper = std::dynamic_pointer_cast<Analyzer::UOper>(qual);
303  if (!uoper) {
304  continue;
305  }
306  bool is_negated{false};
307  if (uoper->get_optype() == kNOT) {
308  uoper = std::dynamic_pointer_cast<Analyzer::UOper>(uoper->get_own_operand());
309  is_negated = true;
310  }
311  if (uoper && (uoper->get_optype() == kISNOTNULL ||
312  (is_negated && uoper->get_optype() == kISNULL))) {
313  if (*uoper->get_own_operand() == *expr) {
314  return true;
315  }
316  }
317  }
318  return false;
319 }
const Analyzer::Expr * agg_arg(const Analyzer::Expr *expr)
SQLAgg
Definition: sqldefs.h:71
bool constrained_not_null(const Analyzer::Expr *expr, const std::list< std::shared_ptr< Analyzer::Expr >> &quals)
unsigned compact_byte_width(unsigned qw, unsigned low_bound)
bool isLogicalSizedColumnsAllowed() const
TargetInfo get_target_info(const PointerType target_expr, const bool bigint_count)
Definition: TargetInfo.h:92
bool is_fp() const
Definition: sqltypes.h:523
int64_t get_agg_initial_val(const SQLAgg agg, const SQLTypeInfo &ti, const bool enable_compaction, const unsigned min_byte_width_to_compact)
Expr * get_arg() const
Definition: Analyzer.h:1250
bool takes_float_argument(const TargetInfo &target_info)
Definition: TargetInfo.h:157
#define CHECK_GT(x, y)
Definition: Logger.h:223
double inline_fp_null_val(const SQL_TYPE_INFO &ti)
Definition: sqldefs.h:73
const SQLTypeInfo get_compact_type(const TargetInfo &target)
size_t get_bit_width(const SQLTypeInfo &ti)
int get_logical_size() const
Definition: sqltypes.h:349
bool g_bigint_count
Definition: sqldefs.h:75
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
QueryDescriptionType getQueryDescriptionType() const
std::pair< uint64_t, uint64_t > inline_uint_max_min(const size_t byte_width)
#define CHECK_LT(x, y)
Definition: Logger.h:221
Definition: sqldefs.h:76
Descriptor for the result set buffer layout.
#define CHECK(condition)
Definition: Logger.h:211
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
std::pair< int64_t, int64_t > inline_int_max_min(const size_t byte_width)
bool is_string() const
Definition: sqltypes.h:519
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:336
Definition: sqldefs.h:74
std::vector< int64_t > init_agg_val_vec(const std::vector< TargetInfo > &targets, const QueryMemoryDescriptor &query_mem_desc)
Definition: sqldefs.h:72
Definition: sqldefs.h:39
void set_notnull(TargetInfo &target, const bool not_null)