OmniSciDB  085a039ca4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NoneEncoder.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 NONE_ENCODER_H
18 #define NONE_ENCODER_H
19 
20 #include "AbstractBuffer.h"
21 #include "Encoder.h"
22 
23 #include <Shared/DatumFetchers.h>
24 #include <Shared/Iteration.h>
25 
26 #include <tbb/parallel_for.h>
27 #include <tbb/parallel_reduce.h>
28 #include <tuple>
29 
30 template <typename T>
32  return std::is_integral<T>::value ? inline_int_null_value<T>()
33  : inline_fp_null_value<T>();
34 }
35 
36 template <typename T>
37 class NoneEncoder : public Encoder {
38  public:
41  }
42 
43  size_t getNumElemsForBytesEncodedDataAtIndices(const int8_t* index_data,
44  const std::vector<size_t>& selected_idx,
45  const size_t byte_limit) override {
46  UNREACHABLE()
47  << "getNumElemsForBytesEncodedDataAtIndices unexpectedly called for non varlen"
48  " encoder";
49  return {};
50  }
51 
52  std::shared_ptr<ChunkMetadata> appendEncodedDataAtIndices(
53  const int8_t*,
54  int8_t* data,
55  const std::vector<size_t>& selected_idx) override {
56  std::shared_ptr<ChunkMetadata> chunk_metadata;
57  // NOTE: the use of `execute_over_contiguous_indices` is an optimization;
58  // it prevents having to copy or move the indexed data and instead performs
59  // an append over contiguous sections of indices.
61  selected_idx, [&](const size_t start_pos, const size_t end_pos) {
62  size_t elem_count = end_pos - start_pos;
63  auto data_ptr = data + sizeof(T) * selected_idx[start_pos];
64  chunk_metadata = appendData(data_ptr, elem_count, SQLTypeInfo{}, false);
65  });
66 
67  return chunk_metadata;
68  }
69 
70  std::shared_ptr<ChunkMetadata> appendEncodedData(const int8_t*,
71  int8_t* data,
72  const size_t start_idx,
73  const size_t num_elements) override {
74  auto current_data = data + sizeof(T) * start_idx;
75  return appendData(current_data, num_elements, SQLTypeInfo{}, false);
76  }
77 
78  std::shared_ptr<ChunkMetadata> appendData(int8_t*& src_data,
79  const size_t num_elems_to_append,
80  const SQLTypeInfo&,
81  const bool replicating = false,
82  const int64_t offset = -1) override {
83  if (offset == 0 && num_elems_to_append >= num_elems_) {
85  }
86  T* unencodedData = reinterpret_cast<T*>(src_data);
87  std::vector<T> encoded_data;
88  if (replicating) {
89  if (num_elems_to_append > 0) {
90  encoded_data.resize(num_elems_to_append);
91  T data = validateDataAndUpdateStats(unencodedData[0]);
92  std::fill(encoded_data.begin(), encoded_data.end(), data);
93  }
94  } else {
95  updateStats(src_data, num_elems_to_append);
96  }
97  if (offset == -1) {
98  num_elems_ += num_elems_to_append;
99  buffer_->append(
100  replicating ? reinterpret_cast<int8_t*>(encoded_data.data()) : src_data,
101  num_elems_to_append * sizeof(T));
102  if (!replicating) {
103  src_data += num_elems_to_append * sizeof(T);
104  }
105  } else {
106  num_elems_ = offset + num_elems_to_append;
107  CHECK(!replicating);
108  CHECK_GE(offset, 0);
109  buffer_->write(
110  src_data, num_elems_to_append * sizeof(T), static_cast<size_t>(offset));
111  }
112  auto chunk_metadata = std::make_shared<ChunkMetadata>();
113  getMetadata(chunk_metadata);
114  return chunk_metadata;
115  }
116 
117  void getMetadata(const std::shared_ptr<ChunkMetadata>& chunkMetadata) override {
118  Encoder::getMetadata(chunkMetadata); // call on parent class
119  chunkMetadata->fillChunkStats(dataMin, dataMax, has_nulls);
120  }
121 
122  // Only called from the executor for synthesized meta-information.
123  std::shared_ptr<ChunkMetadata> getMetadata(const SQLTypeInfo& ti) override {
124  auto chunk_metadata = std::make_shared<ChunkMetadata>(ti, 0, 0, ChunkStats{});
125  chunk_metadata->fillChunkStats(dataMin, dataMax, has_nulls);
126  return chunk_metadata;
127  }
128 
129  // Only called from the executor for synthesized meta-information.
130  void updateStats(const int64_t val, const bool is_null) override {
131  if (is_null) {
132  has_nulls = true;
133  } else {
134  const auto data = static_cast<T>(val);
135  dataMin = std::min(dataMin, data);
136  dataMax = std::max(dataMax, data);
137  }
138  }
139 
140  // Only called from the executor for synthesized meta-information.
141  void updateStats(const double val, const bool is_null) override {
142  if (is_null) {
143  has_nulls = true;
144  } else {
145  const auto data = static_cast<T>(val);
146  dataMin = std::min(dataMin, data);
147  dataMax = std::max(dataMax, data);
148  }
149  }
150 
151  void updateStats(const int8_t* const src_data, const size_t num_elements) override {
152  const T* unencoded_data = reinterpret_cast<const T*>(src_data);
153  for (size_t i = 0; i < num_elements; ++i) {
154  validateDataAndUpdateStats(unencoded_data[i]);
155  }
156  }
157 
158  void updateStatsEncoded(const int8_t* const dst_data,
159  const size_t num_elements) override {
160  const T* data = reinterpret_cast<const T*>(dst_data);
161 
163  tbb::blocked_range(size_t(0), num_elements),
164  std::tuple(dataMin, dataMax, has_nulls),
165  [&](const auto& range, auto init) {
166  auto [min, max, nulls] = init;
167  for (size_t i = range.begin(); i < range.end(); i++) {
168  if (data[i] != none_encoded_null_value<T>()) {
170  min = std::min(min, data[i]);
171  max = std::max(max, data[i]);
172  } else {
173  nulls = true;
174  }
175  }
176  return std::tuple(min, max, nulls);
177  },
178  [&](auto lhs, auto rhs) {
179  const auto [lhs_min, lhs_max, lhs_nulls] = lhs;
180  const auto [rhs_min, rhs_max, rhs_nulls] = rhs;
181  return std::tuple(std::min(lhs_min, rhs_min),
182  std::max(lhs_max, rhs_max),
183  lhs_nulls || rhs_nulls);
184  });
185  }
186 
187  void updateStats(const std::vector<std::string>* const src_data,
188  const size_t start_idx,
189  const size_t num_elements) override {
190  UNREACHABLE();
191  }
192 
193  void updateStats(const std::vector<ArrayDatum>* const src_data,
194  const size_t start_idx,
195  const size_t num_elements) override {
196  UNREACHABLE();
197  }
198 
199  // Only called from the executor for synthesized meta-information.
200  void reduceStats(const Encoder& that) override {
201  const auto that_typed = static_cast<const NoneEncoder&>(that);
202  if (that_typed.has_nulls) {
203  has_nulls = true;
204  }
205  dataMin = std::min(dataMin, that_typed.dataMin);
206  dataMax = std::max(dataMax, that_typed.dataMax);
207  }
208 
209  void writeMetadata(FILE* f) override {
210  // assumes pointer is already in right place
211  fwrite((int8_t*)&num_elems_, sizeof(size_t), 1, f);
212  fwrite((int8_t*)&dataMin, sizeof(T), 1, f);
213  fwrite((int8_t*)&dataMax, sizeof(T), 1, f);
214  fwrite((int8_t*)&has_nulls, sizeof(bool), 1, f);
215  }
216 
217  void readMetadata(FILE* f) override {
218  // assumes pointer is already in right place
219  fread((int8_t*)&num_elems_, sizeof(size_t), 1, f);
220  fread((int8_t*)&dataMin, sizeof(T), 1, f);
221  fread((int8_t*)&dataMax, sizeof(T), 1, f);
222  fread((int8_t*)&has_nulls, sizeof(bool), 1, f);
223  }
224 
225  bool resetChunkStats(const ChunkStats& stats) override {
226  const auto new_min = DatumFetcher::getDatumVal<T>(stats.min);
227  const auto new_max = DatumFetcher::getDatumVal<T>(stats.max);
228 
229  if (dataMin == new_min && dataMax == new_max && has_nulls == stats.has_nulls) {
230  return false;
231  }
232 
233  dataMin = new_min;
234  dataMax = new_max;
235  has_nulls = stats.has_nulls;
236  return true;
237  }
238 
239  void copyMetadata(const Encoder* copyFromEncoder) override {
240  num_elems_ = copyFromEncoder->getNumElems();
241  auto castedEncoder = reinterpret_cast<const NoneEncoder<T>*>(copyFromEncoder);
242  dataMin = castedEncoder->dataMin;
243  dataMax = castedEncoder->dataMax;
244  has_nulls = castedEncoder->has_nulls;
245  }
246 
247  void resetChunkStats() override {
248  dataMin = std::numeric_limits<T>::max();
249  dataMax = std::numeric_limits<T>::lowest();
250  has_nulls = false;
251  }
252 
255  bool has_nulls;
256 
257  private:
258  T validateDataAndUpdateStats(const T& unencoded_data) {
259  if (unencoded_data == none_encoded_null_value<T>()) {
260  has_nulls = true;
261  } else {
262  decimal_overflow_validator_.validate(unencoded_data);
263  dataMin = std::min(dataMin, unencoded_data);
264  dataMax = std::max(dataMax, unencoded_data);
265  }
266  return unencoded_data;
267  }
268 }; // class NoneEncoder
269 
270 #endif // NONE_ENCODER_H
void updateStats(const int8_t *const src_data, const size_t num_elements) override
Definition: NoneEncoder.h:151
size_t num_elems_
Definition: Encoder.h:288
void writeMetadata(FILE *f) override
Definition: NoneEncoder.h:209
DecimalOverflowValidator decimal_overflow_validator_
Definition: Encoder.h:292
#define UNREACHABLE()
Definition: Logger.h:267
#define CHECK_GE(x, y)
Definition: Logger.h:236
bool has_nulls
Definition: ChunkMetadata.h:29
constexpr double f
Definition: Utm.h:31
void updateStats(const int64_t val, const bool is_null) override
Definition: NoneEncoder.h:130
virtual void getMetadata(const std::shared_ptr< ChunkMetadata > &chunkMetadata)
Definition: Encoder.cpp:227
void resetChunkStats() override
Definition: NoneEncoder.h:247
void execute_over_contiguous_indices(const std::vector< size_t > &indices, std::function< void(const size_t, const size_t)> to_execute)
Definition: Iteration.h:22
std::shared_ptr< ChunkMetadata > appendEncodedDataAtIndices(const int8_t *, int8_t *data, const std::vector< size_t > &selected_idx) override
Definition: NoneEncoder.h:52
void updateStats(const std::vector< std::string > *const src_data, const size_t start_idx, const size_t num_elements) override
Definition: NoneEncoder.h:187
DEVICE void fill(ARGS &&...args)
Definition: gpu_enabled.h:60
CONSTEXPR DEVICE bool is_null(const T &value)
Data_Namespace::AbstractBuffer * buffer_
Definition: Encoder.h:290
std::shared_ptr< ChunkMetadata > appendData(int8_t *&src_data, const size_t num_elems_to_append, const SQLTypeInfo &, const bool replicating=false, const int64_t offset=-1) override
Definition: NoneEncoder.h:78
void init(LogOptions const &log_opts)
Definition: Logger.cpp:306
size_t getNumElems() const
Definition: Encoder.h:284
An AbstractBuffer is a unit of data management for a data manager.
void getMetadata(const std::shared_ptr< ChunkMetadata > &chunkMetadata) override
Definition: NoneEncoder.h:117
virtual void write(int8_t *src, const size_t num_bytes, const size_t offset=0, const MemoryLevel src_buffer_type=CPU_LEVEL, const int src_device_id=-1)=0
Value parallel_reduce(const blocked_range< Int > &range, const Value &identity, const RealBody &real_body, const Reduction &reduction, const Partitioner &p=Partitioner())
Parallel iteration with reduction.
T none_encoded_null_value()
Definition: NoneEncoder.h:31
bool resetChunkStats(const ChunkStats &stats) override
: Reset chunk level stats (min, max, nulls) using new values from the argument.
Definition: NoneEncoder.h:225
size_t getNumElemsForBytesEncodedDataAtIndices(const int8_t *index_data, const std::vector< size_t > &selected_idx, const size_t byte_limit) override
Definition: NoneEncoder.h:43
bool has_nulls
Definition: NoneEncoder.h:255
void updateStats(const double val, const bool is_null) override
Definition: NoneEncoder.h:141
void updateStats(const std::vector< ArrayDatum > *const src_data, const size_t start_idx, const size_t num_elements) override
Definition: NoneEncoder.h:193
std::shared_ptr< ChunkMetadata > getMetadata(const SQLTypeInfo &ti) override
Definition: NoneEncoder.h:123
virtual void append(int8_t *src, const size_t num_bytes, const MemoryLevel src_buffer_type=CPU_LEVEL, const int device_id=-1)=0
#define CHECK(condition)
Definition: Logger.h:223
std::shared_ptr< ChunkMetadata > appendEncodedData(const int8_t *, int8_t *data, const size_t start_idx, const size_t num_elements) override
Definition: NoneEncoder.h:70
NoneEncoder(Data_Namespace::AbstractBuffer *buffer)
Definition: NoneEncoder.h:39
void updateStatsEncoded(const int8_t *const dst_data, const size_t num_elements) override
Definition: NoneEncoder.h:158
void reduceStats(const Encoder &that) override
Definition: NoneEncoder.h:200
void copyMetadata(const Encoder *copyFromEncoder) override
Definition: NoneEncoder.h:239
T validateDataAndUpdateStats(const T &unencoded_data)
Definition: NoneEncoder.h:258
void validate(T value) const
Definition: Encoder.h:54
void readMetadata(FILE *f) override
Definition: NoneEncoder.h:217