OmniSciDB  bf83d84833
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 
25 #include <tbb/parallel_for.h>
26 #include <tbb/parallel_reduce.h>
27 #include <tuple>
28 
29 template <typename T>
31  return std::is_integral<T>::value ? inline_int_null_value<T>()
32  : inline_fp_null_value<T>();
33 }
34 
35 template <typename T>
36 class NoneEncoder : public Encoder {
37  public:
39  : Encoder(buffer)
40  , dataMin(std::numeric_limits<T>::max())
41  , dataMax(std::numeric_limits<T>::lowest())
42  , has_nulls(false) {}
43 
44  std::shared_ptr<ChunkMetadata> appendData(int8_t*& src_data,
45  const size_t num_elems_to_append,
46  const SQLTypeInfo&,
47  const bool replicating = false,
48  const int64_t offset = -1) override {
49  T* unencodedData = reinterpret_cast<T*>(src_data);
50  std::vector<T> encoded_data;
51  if (replicating) {
52  encoded_data.resize(num_elems_to_append);
53  }
54  for (size_t i = 0; i < num_elems_to_append; ++i) {
55  size_t ri = replicating ? 0 : i;
56  T data = validateDataAndUpdateStats(unencodedData[ri]);
57  if (replicating) {
58  encoded_data[i] = data;
59  }
60  }
61  if (offset == -1) {
62  num_elems_ += num_elems_to_append;
63  buffer_->append(
64  replicating ? reinterpret_cast<int8_t*>(encoded_data.data()) : src_data,
65  num_elems_to_append * sizeof(T));
66  if (!replicating) {
67  src_data += num_elems_to_append * sizeof(T);
68  }
69  } else {
70  num_elems_ = offset + num_elems_to_append;
71  CHECK(!replicating);
72  CHECK_GE(offset, 0);
73  buffer_->write(
74  src_data, num_elems_to_append * sizeof(T), static_cast<size_t>(offset));
75  }
76  auto chunk_metadata = std::make_shared<ChunkMetadata>();
77  getMetadata(chunk_metadata);
78  return chunk_metadata;
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  validateDataAndUpdateStats(unencoded_data[i]);
119  }
120  }
121 
122  void updateStatsEncoded(const int8_t* const dst_data,
123  const size_t num_elements) override {
124  const T* data = reinterpret_cast<const T*>(dst_data);
125 
126  std::tie(dataMin, dataMax, has_nulls) = tbb::parallel_reduce(
127  tbb::blocked_range(0UL, num_elements),
128  std::tuple(dataMin, 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] != none_encoded_null_value<T>()) {
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 NoneEncoder&>(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 writeMetadata(FILE* f) override {
174  // assumes pointer is already in right place
175  fwrite((int8_t*)&num_elems_, sizeof(size_t), 1, f);
176  fwrite((int8_t*)&dataMin, sizeof(T), 1, f);
177  fwrite((int8_t*)&dataMax, sizeof(T), 1, f);
178  fwrite((int8_t*)&has_nulls, sizeof(bool), 1, f);
179  }
180 
181  void readMetadata(FILE* f) override {
182  // assumes pointer is already in right place
183  fread((int8_t*)&num_elems_, sizeof(size_t), 1, f);
184  fread((int8_t*)&dataMin, sizeof(T), 1, f);
185  fread((int8_t*)&dataMax, sizeof(T), 1, f);
186  fread((int8_t*)&has_nulls, sizeof(bool), 1, f);
187  }
188 
189  bool resetChunkStats(const ChunkStats& stats) override {
190  const auto new_min = DatumFetcher::getDatumVal<T>(stats.min);
191  const auto new_max = DatumFetcher::getDatumVal<T>(stats.max);
192 
193  if (dataMin == new_min && dataMax == new_max && has_nulls == stats.has_nulls) {
194  return false;
195  }
196 
197  dataMin = new_min;
198  dataMax = new_max;
199  has_nulls = stats.has_nulls;
200  return true;
201  }
202 
203  void copyMetadata(const Encoder* copyFromEncoder) override {
204  num_elems_ = copyFromEncoder->getNumElems();
205  auto castedEncoder = reinterpret_cast<const NoneEncoder<T>*>(copyFromEncoder);
206  dataMin = castedEncoder->dataMin;
207  dataMax = castedEncoder->dataMax;
208  has_nulls = castedEncoder->has_nulls;
209  }
210 
213  bool has_nulls;
214 
215  private:
216  T validateDataAndUpdateStats(const T& unencoded_data) {
217  if (unencoded_data == none_encoded_null_value<T>()) {
218  has_nulls = true;
219  } else {
220  decimal_overflow_validator_.validate(unencoded_data);
221  dataMin = std::min(dataMin, unencoded_data);
222  dataMax = std::max(dataMax, unencoded_data);
223  }
224  return unencoded_data;
225  }
226 }; // class NoneEncoder
227 
228 #endif // NONE_ENCODER_H
void updateStats(const int8_t *const src_data, const size_t num_elements) override
Definition: NoneEncoder.h:115
size_t num_elems_
Definition: Encoder.h:232
void writeMetadata(FILE *f) override
Definition: NoneEncoder.h:173
DecimalOverflowValidator decimal_overflow_validator_
Definition: Encoder.h:236
#define UNREACHABLE()
Definition: Logger.h:241
#define CHECK_GE(x, y)
Definition: Logger.h:210
bool has_nulls
Definition: ChunkMetadata.h:28
void updateStats(const int64_t val, const bool is_null) override
Definition: NoneEncoder.h:94
virtual void getMetadata(const std::shared_ptr< ChunkMetadata > &chunkMetadata)
Definition: Encoder.cpp:227
void updateStats(const std::vector< std::string > *const src_data, const size_t start_idx, const size_t num_elements) override
Definition: NoneEncoder.h:151
CONSTEXPR DEVICE bool is_null(const T &value)
Data_Namespace::AbstractBuffer * buffer_
Definition: Encoder.h:234
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:44
void init(LogOptions const &log_opts)
Definition: Logger.cpp:280
size_t getNumElems() const
Definition: Encoder.h:228
An AbstractBuffer is a unit of data management for a data manager.
void getMetadata(const std::shared_ptr< ChunkMetadata > &chunkMetadata) override
Definition: NoneEncoder.h:81
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
void validate(T value)
Definition: Encoder.h:54
T none_encoded_null_value()
Definition: NoneEncoder.h:30
bool resetChunkStats(const ChunkStats &stats) override
: Reset chunk level stats (min, max, nulls) using new values from the argument.
Definition: NoneEncoder.h:189
bool has_nulls
Definition: NoneEncoder.h:213
void updateStats(const double val, const bool is_null) override
Definition: NoneEncoder.h:105
void updateStats(const std::vector< ArrayDatum > *const src_data, const size_t start_idx, const size_t num_elements) override
Definition: NoneEncoder.h:157
std::shared_ptr< ChunkMetadata > getMetadata(const SQLTypeInfo &ti) override
Definition: NoneEncoder.h:87
virtual void append(int8_t *src, const size_t num_bytes, const MemoryLevel src_buffer_type=CPU_LEVEL, const int device_id=-1)=0
bool g_enable_watchdog false
Definition: Execute.cpp:76
#define CHECK(condition)
Definition: Logger.h:197
NoneEncoder(Data_Namespace::AbstractBuffer *buffer)
Definition: NoneEncoder.h:38
void updateStatsEncoded(const int8_t *const dst_data, const size_t num_elements) override
Definition: NoneEncoder.h:122
void reduceStats(const Encoder &that) override
Definition: NoneEncoder.h:164
void copyMetadata(const Encoder *copyFromEncoder) override
Definition: NoneEncoder.h:203
T validateDataAndUpdateStats(const T &unencoded_data)
Definition: NoneEncoder.h:216
void readMetadata(FILE *f) override
Definition: NoneEncoder.h:181