OmniSciDB  085a039ca4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ColSlotContext.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, 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 
25 #pragma once
26 
27 #include "Logger/Logger.h"
28 
29 #include <algorithm>
30 #include <string>
31 #include <unordered_map>
32 #include <vector>
33 
34 struct SlotSize {
35  int8_t padded_size; // size of the slot
36  int8_t logical_size; // size of the element in the slot
37 };
38 
39 inline bool operator==(const SlotSize& lhs, const SlotSize& rhs) {
40  return lhs.padded_size == rhs.padded_size && lhs.logical_size == rhs.logical_size;
41 }
42 
43 namespace Analyzer {
44 class Expr;
45 }
46 
48  public:
50 
51  ColSlotContext(const std::vector<Analyzer::Expr*>& col_expr_list,
52  const std::vector<int64_t>& col_exprs_to_not_project);
53 
54  void setAllSlotsSize(const int8_t slot_width_size);
55 
56  void setAllSlotsPaddedSize(const int8_t padded_size);
57 
58  void setAllUnsetSlotsPaddedSize(const int8_t padded_size);
59 
61 
62  void validate() const;
63 
64  size_t getColCount() const;
65  size_t getSlotCount() const;
66 
67  const SlotSize& getSlotInfo(const size_t slot_idx) const {
68  CHECK_LT(slot_idx, slot_sizes_.size());
69  return slot_sizes_[slot_idx];
70  }
71 
72  void setPaddedSlotWidthBytes(const size_t slot_idx, const int8_t bytes) {
73  CHECK_LT(slot_idx, slot_sizes_.size());
74  slot_sizes_[slot_idx].padded_size = bytes;
75  }
76 
77  const std::vector<size_t>& getSlotsForCol(const size_t col_idx) const {
78  CHECK_LT(col_idx, col_to_slot_map_.size());
79  return col_to_slot_map_[col_idx];
80  }
81 
82  size_t getAllSlotsPaddedSize() const;
83 
84  size_t getAllSlotsAlignedPaddedSize() const;
85 
86  size_t getAlignedPaddedSizeForRange(const size_t end) const;
87 
88  size_t getTotalBytesOfColumnarBuffers(const size_t entry_count) const;
89 
90  int8_t getMinPaddedByteSize(const int8_t actual_min_byte_width) const;
91 
92  size_t getCompactByteWidth() const;
93 
94  size_t getColOnlyOffInBytes(const size_t slot_idx) const;
95 
96  bool empty();
97 
98  void clear();
99 
100  void addColumn(const std::vector<std::tuple<int8_t, int8_t>>& slots_for_col);
101 
102  bool operator==(const ColSlotContext& that) const {
103  return std::equal(
104  slot_sizes_.cbegin(), slot_sizes_.cend(), that.slot_sizes_.cbegin()) &&
105  std::equal(col_to_slot_map_.cbegin(),
106  col_to_slot_map_.cend(),
107  that.col_to_slot_map_.cbegin());
108  }
109 
110  bool operator!=(const ColSlotContext& that) const { return !(*this == that); }
111 
112  void alignPaddedSlots(const bool sort_on_gpu);
113 
114  int64_t varlenOutputElementSize(const size_t slot_idx) const;
115 
116  bool hasVarlenOutput() const { return !varlen_output_slot_map_.empty(); }
117 
118  bool slotIsVarlen(const size_t slot_idx) const {
119  return varlen_output_slot_map_.count(slot_idx) > 0;
120  }
121 
122  std::string toString() const {
123  std::string str{"Col Slot Context State\n"};
124  if (slot_sizes_.empty()) {
125  str += "\tEmpty";
126  return str;
127  }
128  str += "\tN | P , L\n";
129  for (size_t i = 0; i < slot_sizes_.size(); i++) {
130  const auto& slot_size = slot_sizes_[i];
131  str += "\t" + std::to_string(i) + " | " + std::to_string(slot_size.padded_size) +
132  " , " + std::to_string(slot_size.logical_size) + "\n";
133  }
134  return str;
135  }
136 
137  private:
138  void addSlotForColumn(const int8_t logical_size, const size_t column_idx);
139 
140  void addSlotForColumn(const int8_t padded_size,
141  const int8_t logical_size,
142  const size_t column_idx);
143 
144  std::vector<SlotSize> slot_sizes_;
145  std::vector<std::vector<size_t>> col_to_slot_map_;
146 
147  using ArraySize = int64_t;
148  using SlotIndex = size_t;
149  std::unordered_map<SlotIndex, ArraySize> varlen_output_slot_map_;
150 };
int8_t getMinPaddedByteSize(const int8_t actual_min_byte_width) const
void alignPaddedSlots(const bool sort_on_gpu)
bool slotIsVarlen(const size_t slot_idx) const
int8_t logical_size
void sort_on_gpu(int64_t *val_buff, int32_t *idx_buff, const uint64_t entry_count, const bool desc, const uint32_t chosen_bytes, ThrustAllocator &alloc, const int device_id)
std::unordered_map< SlotIndex, ArraySize > varlen_output_slot_map_
void setPaddedSlotWidthBytes(const size_t slot_idx, const int8_t bytes)
size_t getAllSlotsPaddedSize() const
size_t getAllSlotsAlignedPaddedSize() const
std::vector< std::vector< size_t > > col_to_slot_map_
bool hasVarlenOutput() const
std::vector< SlotSize > slot_sizes_
void setAllSlotsSize(const int8_t slot_width_size)
std::string to_string(char const *&&v)
const SlotSize & getSlotInfo(const size_t slot_idx) const
size_t getColOnlyOffInBytes(const size_t slot_idx) const
size_t getCompactByteWidth() const
size_t getAlignedPaddedSizeForRange(const size_t end) const
bool operator==(const ColSlotContext &that) const
void validate() const
int64_t varlenOutputElementSize(const size_t slot_idx) const
void addColumn(const std::vector< std::tuple< int8_t, int8_t >> &slots_for_col)
#define CHECK_LT(x, y)
Definition: Logger.h:233
void setAllSlotsPaddedSize(const int8_t padded_size)
int8_t padded_size
bool operator==(const SlotSize &lhs, const SlotSize &rhs)
size_t getTotalBytesOfColumnarBuffers(const size_t entry_count) const
void addSlotForColumn(const int8_t logical_size, const size_t column_idx)
std::string toString() const
size_t getSlotCount() const
void setAllSlotsPaddedSizeToLogicalSize()
size_t getColCount() const
const std::vector< size_t > & getSlotsForCol(const size_t col_idx) const
bool operator!=(const ColSlotContext &that) const
void setAllUnsetSlotsPaddedSize(const int8_t padded_size)