OmniSciDB  16c4e035a1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FixedLengthEncoder.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 FIXED_LENGTH_ENCODER_H
18 #define FIXED_LENGTH_ENCODER_H
19 #include "Logger/Logger.h"
20 
21 #include <iostream>
22 #include <memory>
23 #include <stdexcept>
24 #include "AbstractBuffer.h"
25 #include "Encoder.h"
26 
27 #include <Shared/DatumFetchers.h>
28 #include <tbb/parallel_for.h>
29 #include <tbb/parallel_reduce.h>
30 #include <tuple>
31 
32 template <typename T, typename V>
33 class FixedLengthEncoder : public Encoder {
34  public:
37  }
38 
39  size_t getNumElemsForBytesEncodedData(const int8_t* index_data,
40  const int start_idx,
41  const size_t num_elements,
42  const size_t byte_limit) override {
43  UNREACHABLE() << "getNumElemsForBytesEncodedData unexpectedly called for non varlen"
44  " encoder";
45  return {};
46  }
47 
48  std::shared_ptr<ChunkMetadata> appendEncodedDataAtIndices(
49  const int8_t*,
50  int8_t* data,
51  const std::vector<size_t>& selected_idx) override {
52  std::vector<V> data_subset;
53  data_subset.reserve(selected_idx.size());
54  auto encoded_data = reinterpret_cast<V*>(data);
55  for (const auto& index : selected_idx) {
56  data_subset.emplace_back(encoded_data[index]);
57  }
58  auto append_data = reinterpret_cast<int8_t*>(data_subset.data());
60  append_data, selected_idx.size(), SQLTypeInfo{}, false, -1, true);
61  }
62 
63  std::shared_ptr<ChunkMetadata> appendEncodedData(const int8_t*,
64  int8_t* data,
65  const size_t start_idx,
66  const size_t num_elements) override {
67  auto current_data = data + start_idx * sizeof(V);
69  current_data, num_elements, SQLTypeInfo{}, false, -1, true);
70  }
71 
72  std::shared_ptr<ChunkMetadata> appendData(int8_t*& src_data,
73  const size_t num_elems_to_append,
74  const SQLTypeInfo& ti,
75  const bool replicating = false,
76  const int64_t offset = -1) override {
78  src_data, num_elems_to_append, ti, replicating, offset, false);
79  }
80 
81  void getMetadata(const std::shared_ptr<ChunkMetadata>& chunkMetadata) override {
82  Encoder::getMetadata(chunkMetadata); // call on parent class
83  chunkMetadata->fillChunkStats(dataMin, dataMax, has_nulls);
84  }
85 
86  // Only called from the executor for synthesized meta-information.
87  std::shared_ptr<ChunkMetadata> getMetadata(const SQLTypeInfo& ti) override {
88  auto chunk_metadata = std::make_shared<ChunkMetadata>(ti, 0, 0, ChunkStats{});
89  chunk_metadata->fillChunkStats(dataMin, dataMax, has_nulls);
90  return chunk_metadata;
91  }
92 
93  // Only called from the executor for synthesized meta-information.
94  void updateStats(const int64_t val, const bool is_null) override {
95  if (is_null) {
96  has_nulls = true;
97  } else {
98  const auto data = static_cast<T>(val);
99  dataMin = std::min(dataMin, data);
100  dataMax = std::max(dataMax, data);
101  }
102  }
103 
104  // Only called from the executor for synthesized meta-information.
105  void updateStats(const double val, const bool is_null) override {
106  if (is_null) {
107  has_nulls = true;
108  } else {
109  const auto data = static_cast<T>(val);
110  dataMin = std::min(dataMin, data);
111  dataMax = std::max(dataMax, data);
112  }
113  }
114 
115  void updateStats(const int8_t* const src_data, const size_t num_elements) override {
116  const T* unencoded_data = reinterpret_cast<const T*>(src_data);
117  for (size_t i = 0; i < num_elements; ++i) {
118  encodeDataAndUpdateStats(unencoded_data[i]);
119  }
120  }
121 
122  void updateStatsEncoded(const int8_t* const dst_data,
123  const size_t num_elements) override {
124  const V* data = reinterpret_cast<const V*>(dst_data);
125 
127  tbb::blocked_range(size_t(0), num_elements),
128  std::tuple(static_cast<V>(dataMin), static_cast<V>(dataMax), has_nulls),
129  [&](const auto& range, auto init) {
130  auto [min, max, nulls] = init;
131  for (size_t i = range.begin(); i < range.end(); i++) {
132  if (data[i] != std::numeric_limits<V>::min()) {
134  min = std::min(min, data[i]);
135  max = std::max(max, data[i]);
136  } else {
137  nulls = true;
138  }
139  }
140  return std::tuple(min, max, nulls);
141  },
142  [&](auto lhs, auto rhs) {
143  const auto [lhs_min, lhs_max, lhs_nulls] = lhs;
144  const auto [rhs_min, rhs_max, rhs_nulls] = rhs;
145  return std::tuple(std::min(lhs_min, rhs_min),
146  std::max(lhs_max, rhs_max),
147  lhs_nulls || rhs_nulls);
148  });
149  }
150 
151  void updateStats(const std::vector<std::string>* const src_data,
152  const size_t start_idx,
153  const size_t num_elements) override {
154  UNREACHABLE();
155  }
156 
157  void updateStats(const std::vector<ArrayDatum>* const src_data,
158  const size_t start_idx,
159  const size_t num_elements) override {
160  UNREACHABLE();
161  }
162 
163  // Only called from the executor for synthesized meta-information.
164  void reduceStats(const Encoder& that) override {
165  const auto that_typed = static_cast<const FixedLengthEncoder<T, V>&>(that);
166  if (that_typed.has_nulls) {
167  has_nulls = true;
168  }
169  dataMin = std::min(dataMin, that_typed.dataMin);
170  dataMax = std::max(dataMax, that_typed.dataMax);
171  }
172 
173  void copyMetadata(const Encoder* copyFromEncoder) override {
174  num_elems_ = copyFromEncoder->getNumElems();
175  auto castedEncoder =
176  reinterpret_cast<const FixedLengthEncoder<T, V>*>(copyFromEncoder);
177  dataMin = castedEncoder->dataMin;
178  dataMax = castedEncoder->dataMax;
179  has_nulls = castedEncoder->has_nulls;
180  }
181 
182  void writeMetadata(FILE* f) override {
183  // assumes pointer is already in right place
184  fwrite((int8_t*)&num_elems_, sizeof(size_t), 1, f);
185  fwrite((int8_t*)&dataMin, sizeof(T), 1, f);
186  fwrite((int8_t*)&dataMax, sizeof(T), 1, f);
187  fwrite((int8_t*)&has_nulls, sizeof(bool), 1, f);
188  }
189 
190  void readMetadata(FILE* f) override {
191  // assumes pointer is already in right place
192  fread((int8_t*)&num_elems_, sizeof(size_t), 1, f);
193  fread((int8_t*)&dataMin, 1, sizeof(T), f);
194  fread((int8_t*)&dataMax, 1, sizeof(T), f);
195  fread((int8_t*)&has_nulls, 1, sizeof(bool), f);
196  }
197 
198  bool resetChunkStats(const ChunkStats& stats) override {
199  const auto new_min = DatumFetcher::getDatumVal<T>(stats.min);
200  const auto new_max = DatumFetcher::getDatumVal<T>(stats.max);
201 
202  if (dataMin == new_min && dataMax == new_max && has_nulls == stats.has_nulls) {
203  return false;
204  }
205 
206  dataMin = new_min;
207  dataMax = new_max;
208  has_nulls = stats.has_nulls;
209  return true;
210  }
211 
212  void resetChunkStats() override {
213  dataMin = std::numeric_limits<T>::max();
214  dataMax = std::numeric_limits<T>::lowest();
215  has_nulls = false;
216  }
217 
220  bool has_nulls;
221 
222  private:
223  std::shared_ptr<ChunkMetadata> appendEncodedOrUnencodedData(
224  int8_t*& src_data,
225  const size_t num_elems_to_append,
226  const SQLTypeInfo& ti,
227  const bool replicating,
228  const int64_t offset,
229  const bool is_encoded) {
230  if (offset == 0 &&
231  num_elems_to_append >=
232  num_elems_) { // we're rewriting entire buffer so fully recompute metadata
233  resetChunkStats();
234  }
235 
236  CHECK(!is_encoded || !replicating); // do not support replicating of encoded data
237 
238  T* unencoded_data = reinterpret_cast<T*>(src_data);
239  std::vector<V> encoded_data;
240  V* data_to_write = nullptr;
241  if (!is_encoded) {
242  encoded_data.resize(num_elems_to_append);
243  data_to_write = encoded_data.data();
244  for (size_t i = 0; i < num_elems_to_append; ++i) {
245  size_t ri = replicating ? 0 : i;
246  encoded_data[i] = encodeDataAndUpdateStats(unencoded_data[ri]);
247  }
248  } else {
249  data_to_write = reinterpret_cast<V*>(src_data);
250  for (size_t i = 0; i < num_elems_to_append; ++i) {
251  updateStatsWithAlreadyEncoded(data_to_write[i]);
252  }
253  }
254 
255  // assume always CPU_BUFFER?
256  if (offset == -1) {
257  num_elems_ += num_elems_to_append;
258  buffer_->append(reinterpret_cast<int8_t*>(data_to_write),
259  num_elems_to_append * sizeof(V));
260  if (!replicating) {
261  src_data += num_elems_to_append * sizeof(T);
262  }
263  } else {
264  num_elems_ = offset + num_elems_to_append;
265  CHECK(!replicating);
266  CHECK_GE(offset, 0);
267  buffer_->write(reinterpret_cast<int8_t*>(data_to_write),
268  num_elems_to_append * sizeof(V),
269  static_cast<size_t>(offset));
270  }
271  auto chunk_metadata = std::make_shared<ChunkMetadata>();
272  getMetadata(chunk_metadata);
273  return chunk_metadata;
274  }
275 
276  void updateStatsWithAlreadyEncoded(const V& encoded_data) {
277  if (encoded_data == std::numeric_limits<V>::min()) {
278  has_nulls = true;
279  } else {
280  dataMin = std::min<T>(dataMin, encoded_data);
281  dataMax = std::max<T>(dataMax, encoded_data);
282  }
283  }
284 
285  V encodeDataAndUpdateStats(const T& unencoded_data) {
286  V encoded_data = static_cast<V>(unencoded_data);
287  if (unencoded_data != encoded_data) {
288  decimal_overflow_validator_.validate(unencoded_data);
289  LOG(ERROR) << "Fixed encoding failed, Unencoded: " +
290  std::to_string(unencoded_data) +
291  " encoded: " + std::to_string(encoded_data);
292  } else {
293  T data = unencoded_data;
294  if (data == std::numeric_limits<V>::min()) {
295  has_nulls = true;
296  } else {
298  dataMin = std::min(dataMin, data);
299  dataMax = std::max(dataMax, data);
300  }
301  }
302  return encoded_data;
303  }
304 }; // FixedLengthEncoder
305 
306 #endif // FIXED_LENGTH_ENCODER_H
void updateStats(const int8_t *const src_data, const size_t num_elements) override
size_t num_elems_
Definition: Encoder.h:289
std::shared_ptr< ChunkMetadata > appendData(int8_t *&src_data, const size_t num_elems_to_append, const SQLTypeInfo &ti, const bool replicating=false, const int64_t offset=-1) override
DecimalOverflowValidator decimal_overflow_validator_
Definition: Encoder.h:293
#define LOG(tag)
Definition: Logger.h:205
std::shared_ptr< ChunkMetadata > appendEncodedDataAtIndices(const int8_t *, int8_t *data, const std::vector< size_t > &selected_idx) override
#define UNREACHABLE()
Definition: Logger.h:255
#define CHECK_GE(x, y)
Definition: Logger.h:224
bool has_nulls
Definition: ChunkMetadata.h:28
std::shared_ptr< ChunkMetadata > appendEncodedData(const int8_t *, int8_t *data, const size_t start_idx, const size_t num_elements) override
void resetChunkStats() override
void updateStats(const int64_t val, const bool is_null) override
void updateStats(const std::vector< std::string > *const src_data, const size_t start_idx, const size_t num_elements) override
std::shared_ptr< ChunkMetadata > appendEncodedOrUnencodedData(int8_t *&src_data, const size_t num_elems_to_append, const SQLTypeInfo &ti, const bool replicating, const int64_t offset, const bool is_encoded)
std::string to_string(char const *&&v)
virtual void getMetadata(const std::shared_ptr< ChunkMetadata > &chunkMetadata)
Definition: Encoder.cpp:227
CONSTEXPR DEVICE bool is_null(const T &value)
Data_Namespace::AbstractBuffer * buffer_
Definition: Encoder.h:291
void init(LogOptions const &log_opts)
Definition: Logger.cpp:305
void copyMetadata(const Encoder *copyFromEncoder) override
void readMetadata(FILE *f) override
size_t getNumElems() const
Definition: Encoder.h:285
void updateStats(const double val, const bool is_null) override
V encodeDataAndUpdateStats(const T &unencoded_data)
An AbstractBuffer is a unit of data management for a data manager.
void getMetadata(const std::shared_ptr< ChunkMetadata > &chunkMetadata) override
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.
void updateStats(const std::vector< ArrayDatum > *const src_data, const size_t start_idx, const size_t num_elements) override
bool resetChunkStats(const ChunkStats &stats) override
: Reset chunk level stats (min, max, nulls) using new values from the argument.
void updateStatsEncoded(const int8_t *const dst_data, const size_t num_elements) override
FixedLengthEncoder(Data_Namespace::AbstractBuffer *buffer)
void writeMetadata(FILE *f) override
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:211
char * f
size_t getNumElemsForBytesEncodedData(const int8_t *index_data, const int start_idx, const size_t num_elements, const size_t byte_limit) override
void validate(T value) const
Definition: Encoder.h:54
void updateStatsWithAlreadyEncoded(const V &encoded_data)
void reduceStats(const Encoder &that) override
std::shared_ptr< ChunkMetadata > getMetadata(const SQLTypeInfo &ti) override