OmniSciDB  17c254d2f8
 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 template <typename T>
27  return std::is_integral<T>::value ? inline_int_null_value<T>()
28  : inline_fp_null_value<T>();
29 }
30 
31 template <typename T>
32 class NoneEncoder : public Encoder {
33  public:
35  : Encoder(buffer)
36  , dataMin(std::numeric_limits<T>::max())
37  , dataMax(std::numeric_limits<T>::lowest())
38  , has_nulls(false) {}
39 
40  std::shared_ptr<ChunkMetadata> appendData(int8_t*& src_data,
41  const size_t num_elems_to_append,
42  const SQLTypeInfo&,
43  const bool replicating = false,
44  const int64_t offset = -1) override {
45  T* unencodedData = reinterpret_cast<T*>(src_data);
46  std::vector<T> encoded_data;
47  if (replicating) {
48  encoded_data.resize(num_elems_to_append);
49  }
50  for (size_t i = 0; i < num_elems_to_append; ++i) {
51  size_t ri = replicating ? 0 : i;
52  T data = unencodedData[ri];
53  if (replicating) {
54  encoded_data[i] = data;
55  }
56  if (data == none_encoded_null_value<T>()) {
57  has_nulls = true;
58  } else {
60  dataMin = std::min(dataMin, data);
61  dataMax = std::max(dataMax, data);
62  }
63  }
64  if (offset == -1) {
65  num_elems_ += num_elems_to_append;
66  buffer_->append(
67  replicating ? reinterpret_cast<int8_t*>(encoded_data.data()) : src_data,
68  num_elems_to_append * sizeof(T));
69  if (!replicating) {
70  src_data += num_elems_to_append * sizeof(T);
71  }
72  } else {
73  num_elems_ = offset + num_elems_to_append;
74  CHECK(!replicating);
75  CHECK_GE(offset, 0);
76  buffer_->write(
77  src_data, num_elems_to_append * sizeof(T), static_cast<size_t>(offset));
78  }
79  auto chunk_metadata = std::make_shared<ChunkMetadata>();
80  getMetadata(chunk_metadata);
81  return chunk_metadata;
82  }
83 
84  void getMetadata(const std::shared_ptr<ChunkMetadata>& chunkMetadata) override {
85  Encoder::getMetadata(chunkMetadata); // call on parent class
86  chunkMetadata->fillChunkStats(dataMin, dataMax, has_nulls);
87  }
88 
89  // Only called from the executor for synthesized meta-information.
90  std::shared_ptr<ChunkMetadata> getMetadata(const SQLTypeInfo& ti) override {
91  auto chunk_metadata = std::make_shared<ChunkMetadata>(ti, 0, 0, ChunkStats{});
92  chunk_metadata->fillChunkStats(dataMin, dataMax, has_nulls);
93  return chunk_metadata;
94  }
95 
96  // Only called from the executor for synthesized meta-information.
97  void updateStats(const int64_t val, const bool is_null) override {
98  if (is_null) {
99  has_nulls = true;
100  } else {
101  const auto data = static_cast<T>(val);
102  dataMin = std::min(dataMin, data);
103  dataMax = std::max(dataMax, data);
104  }
105  }
106 
107  // Only called from the executor for synthesized meta-information.
108  void updateStats(const double val, const bool is_null) override {
109  if (is_null) {
110  has_nulls = true;
111  } else {
112  const auto data = static_cast<T>(val);
113  dataMin = std::min(dataMin, data);
114  dataMax = std::max(dataMax, data);
115  }
116  }
117 
118  void updateStats(const int8_t* const dst, const size_t numElements) override {
119  const T* unencodedData = reinterpret_cast<const T*>(dst);
120  for (size_t i = 0; i < numElements; ++i) {
121  T data = unencodedData[i];
122  if (data != none_encoded_null_value<T>()) {
124  dataMin = std::min(dataMin, data);
125  dataMax = std::max(dataMax, data);
126  } else {
127  has_nulls = true;
128  }
129  }
130  }
131 
132  // Only called from the executor for synthesized meta-information.
133  void reduceStats(const Encoder& that) override {
134  const auto that_typed = static_cast<const NoneEncoder&>(that);
135  if (that_typed.has_nulls) {
136  has_nulls = true;
137  }
138  dataMin = std::min(dataMin, that_typed.dataMin);
139  dataMax = std::max(dataMax, that_typed.dataMax);
140  }
141 
142  void writeMetadata(FILE* f) override {
143  // assumes pointer is already in right place
144  fwrite((int8_t*)&num_elems_, sizeof(size_t), 1, f);
145  fwrite((int8_t*)&dataMin, sizeof(T), 1, f);
146  fwrite((int8_t*)&dataMax, sizeof(T), 1, f);
147  fwrite((int8_t*)&has_nulls, sizeof(bool), 1, f);
148  }
149 
150  void readMetadata(FILE* f) override {
151  // assumes pointer is already in right place
152  fread((int8_t*)&num_elems_, sizeof(size_t), 1, f);
153  fread((int8_t*)&dataMin, sizeof(T), 1, f);
154  fread((int8_t*)&dataMax, sizeof(T), 1, f);
155  fread((int8_t*)&has_nulls, sizeof(bool), 1, f);
156  }
157 
158  bool resetChunkStats(const ChunkStats& stats) override {
159  const auto new_min = DatumFetcher::getDatumVal<T>(stats.min);
160  const auto new_max = DatumFetcher::getDatumVal<T>(stats.max);
161 
162  if (dataMin == new_min && dataMax == new_max && has_nulls == stats.has_nulls) {
163  return false;
164  }
165 
166  dataMin = new_min;
167  dataMax = new_max;
168  has_nulls = stats.has_nulls;
169  return true;
170  }
171 
172  void copyMetadata(const Encoder* copyFromEncoder) override {
173  num_elems_ = copyFromEncoder->getNumElems();
174  auto castedEncoder = reinterpret_cast<const NoneEncoder<T>*>(copyFromEncoder);
175  dataMin = castedEncoder->dataMin;
176  dataMax = castedEncoder->dataMax;
177  has_nulls = castedEncoder->has_nulls;
178  }
179 
182  bool has_nulls;
183 
184 }; // class NoneEncoder
185 
186 #endif // NONE_ENCODER_H
void updateStats(const int8_t *const dst, const size_t numElements) override
Definition: NoneEncoder.h:118
size_t num_elems_
Definition: Encoder.h:191
void writeMetadata(FILE *f) override
Definition: NoneEncoder.h:142
DecimalOverflowValidator decimal_overflow_validator_
Definition: Encoder.h:195
#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:97
virtual void getMetadata(const std::shared_ptr< ChunkMetadata > &chunkMetadata)
Definition: Encoder.cpp:227
CHECK(cgen_state)
Data_Namespace::AbstractBuffer * buffer_
Definition: Encoder.h:193
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:40
size_t getNumElems() const
Definition: Encoder.h:187
An AbstractBuffer is a unit of data management for a data manager.
void getMetadata(const std::shared_ptr< ChunkMetadata > &chunkMetadata) override
Definition: NoneEncoder.h:84
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:26
bool resetChunkStats(const ChunkStats &stats) override
: Reset chunk level stats (min, max, nulls) using new values from the argument.
Definition: NoneEncoder.h:158
bool is_null(const T &v, const SQLTypeInfo &t)
bool has_nulls
Definition: NoneEncoder.h:182
void updateStats(const double val, const bool is_null) override
Definition: NoneEncoder.h:108
std::shared_ptr< ChunkMetadata > getMetadata(const SQLTypeInfo &ti) override
Definition: NoneEncoder.h:90
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:73
NoneEncoder(Data_Namespace::AbstractBuffer *buffer)
Definition: NoneEncoder.h:34
void reduceStats(const Encoder &that) override
Definition: NoneEncoder.h:133
void copyMetadata(const Encoder *copyFromEncoder) override
Definition: NoneEncoder.h:172
void readMetadata(FILE *f) override
Definition: NoneEncoder.h:150