OmniSciDB  95562058bd
 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 updateStats(const std::vector<std::string>* const src_data,
123  const size_t start_idx,
124  const size_t num_elements) override {
125  UNREACHABLE();
126  }
127 
128  void updateStats(const std::vector<ArrayDatum>* const src_data,
129  const size_t start_idx,
130  const size_t num_elements) override {
131  UNREACHABLE();
132  }
133 
134  // Only called from the executor for synthesized meta-information.
135  void reduceStats(const Encoder& that) override {
136  const auto that_typed = static_cast<const NoneEncoder&>(that);
137  if (that_typed.has_nulls) {
138  has_nulls = true;
139  }
140  dataMin = std::min(dataMin, that_typed.dataMin);
141  dataMax = std::max(dataMax, that_typed.dataMax);
142  }
143 
144  void writeMetadata(FILE* f) override {
145  // assumes pointer is already in right place
146  fwrite((int8_t*)&num_elems_, sizeof(size_t), 1, f);
147  fwrite((int8_t*)&dataMin, sizeof(T), 1, f);
148  fwrite((int8_t*)&dataMax, sizeof(T), 1, f);
149  fwrite((int8_t*)&has_nulls, sizeof(bool), 1, f);
150  }
151 
152  void readMetadata(FILE* f) override {
153  // assumes pointer is already in right place
154  fread((int8_t*)&num_elems_, sizeof(size_t), 1, f);
155  fread((int8_t*)&dataMin, sizeof(T), 1, f);
156  fread((int8_t*)&dataMax, sizeof(T), 1, f);
157  fread((int8_t*)&has_nulls, sizeof(bool), 1, f);
158  }
159 
160  bool resetChunkStats(const ChunkStats& stats) override {
161  const auto new_min = DatumFetcher::getDatumVal<T>(stats.min);
162  const auto new_max = DatumFetcher::getDatumVal<T>(stats.max);
163 
164  if (dataMin == new_min && dataMax == new_max && has_nulls == stats.has_nulls) {
165  return false;
166  }
167 
168  dataMin = new_min;
169  dataMax = new_max;
170  has_nulls = stats.has_nulls;
171  return true;
172  }
173 
174  void copyMetadata(const Encoder* copyFromEncoder) override {
175  num_elems_ = copyFromEncoder->getNumElems();
176  auto castedEncoder = reinterpret_cast<const NoneEncoder<T>*>(copyFromEncoder);
177  dataMin = castedEncoder->dataMin;
178  dataMax = castedEncoder->dataMax;
179  has_nulls = castedEncoder->has_nulls;
180  }
181 
184  bool has_nulls;
185 
186  private:
187  T validateDataAndUpdateStats(const T& unencoded_data) {
188  if (unencoded_data == none_encoded_null_value<T>()) {
189  has_nulls = true;
190  } else {
191  decimal_overflow_validator_.validate(unencoded_data);
192  dataMin = std::min(dataMin, unencoded_data);
193  dataMax = std::max(dataMax, unencoded_data);
194  }
195  return unencoded_data;
196  }
197 }; // class NoneEncoder
198 
199 #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:218
void writeMetadata(FILE *f) override
Definition: NoneEncoder.h:144
DecimalOverflowValidator decimal_overflow_validator_
Definition: Encoder.h:222
#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:122
Data_Namespace::AbstractBuffer * buffer_
Definition: Encoder.h:220
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
size_t getNumElems() const
Definition: Encoder.h:214
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:160
bool is_null(const T &v, const SQLTypeInfo &t)
bool has_nulls
Definition: NoneEncoder.h:184
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:128
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:74
#define CHECK(condition)
Definition: Logger.h:197
NoneEncoder(Data_Namespace::AbstractBuffer *buffer)
Definition: NoneEncoder.h:38
void reduceStats(const Encoder &that) override
Definition: NoneEncoder.h:135
void copyMetadata(const Encoder *copyFromEncoder) override
Definition: NoneEncoder.h:174
T validateDataAndUpdateStats(const T &unencoded_data)
Definition: NoneEncoder.h:187
void readMetadata(FILE *f) override
Definition: NoneEncoder.h:152