OmniSciDB  c07336695a
AggregateUtils.h
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 
17 #ifndef QUERYENGINE_AGGREGATEUTILS_H
18 #define QUERYENGINE_AGGREGATEUTILS_H
19 
20 #include "../Shared/sqltypes.h"
21 #include "BufferCompaction.h"
22 #include "TypePunning.h"
23 
24 #include "Shared/Logger.h"
25 
26 inline void set_component(int8_t* group_by_buffer,
27  const size_t comp_sz,
28  const int64_t val,
29  const size_t index = 0) {
30  switch (comp_sz) {
31  case 1: {
32  group_by_buffer[index] = static_cast<int8_t>(val);
33  break;
34  }
35  case 2: {
36  int16_t* buffer_ptr = reinterpret_cast<int16_t*>(group_by_buffer);
37  buffer_ptr[index] = (int16_t)val;
38  break;
39  }
40  case 4: {
41  int32_t* buffer_ptr = reinterpret_cast<int32_t*>(group_by_buffer);
42  buffer_ptr[index] = (int32_t)val;
43  break;
44  }
45  case 8: {
46  int64_t* buffer_ptr = reinterpret_cast<int64_t*>(group_by_buffer);
47  buffer_ptr[index] = val;
48  break;
49  }
50  case 0: {
51  break;
52  }
53  default:
54  CHECK(false);
55  }
56 }
57 
58 inline int64_t float_to_double_bin(int32_t val, bool nullable = false) {
59  const float null_float = NULL_FLOAT;
60  if (nullable && val == *reinterpret_cast<const int32_t*>(may_alias_ptr(&null_float))) {
61  double null_double = NULL_DOUBLE;
62  return *reinterpret_cast<const int64_t*>(may_alias_ptr(&null_double));
63  }
64  double res = *reinterpret_cast<const float*>(may_alias_ptr(&val));
65  return *reinterpret_cast<const int64_t*>(may_alias_ptr(&res));
66 }
67 
68 inline std::vector<int64_t> compact_init_vals(
69  const size_t cmpt_size,
70  const std::vector<int64_t>& init_vec,
71  const QueryMemoryDescriptor& query_mem_desc) {
72  std::vector<int64_t> cmpt_res(cmpt_size, 0);
73  int8_t* buffer_ptr = reinterpret_cast<int8_t*>(&cmpt_res[0]);
74  for (size_t col_idx = 0, init_vec_idx = 0, col_count = query_mem_desc.getSlotCount();
75  col_idx < col_count;
76  ++col_idx) {
77  const auto chosen_bytes =
78  static_cast<unsigned>(query_mem_desc.getPaddedSlotWidthBytes(col_idx));
79  if (chosen_bytes == 0) {
80  continue;
81  }
82  if (chosen_bytes == sizeof(int64_t)) {
83  buffer_ptr = align_to_int64(buffer_ptr);
84  }
85  CHECK_LT(init_vec_idx, init_vec.size());
86  set_component(buffer_ptr, chosen_bytes, init_vec[init_vec_idx++]);
87  buffer_ptr += chosen_bytes;
88  }
89  return cmpt_res;
90 }
91 #endif // QUERYENGINE_AGGREGATEUTILS_H
#define NULL_DOUBLE
Definition: sqltypes.h:177
int64_t float_to_double_bin(int32_t val, bool nullable=false)
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
std::vector< int64_t > compact_init_vals(const size_t cmpt_size, const std::vector< int64_t > &init_vec, const QueryMemoryDescriptor &query_mem_desc)
#define NULL_FLOAT
Definition: sqltypes.h:176
#define CHECK_LT(x, y)
Definition: Logger.h:197
void set_component(int8_t *group_by_buffer, const size_t comp_sz, const int64_t val, const size_t index=0)
#define CHECK(condition)
Definition: Logger.h:187
FORCE_INLINE HOST DEVICE T align_to_int64(T addr)