OmniSciDB  dfae7c3b14
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:
36  : Encoder(buffer)
37  , dataMin(std::numeric_limits<T>::max())
38  , dataMax(std::numeric_limits<T>::min())
39  , has_nulls(false) {}
40 
41  std::shared_ptr<ChunkMetadata> appendData(int8_t*& src_data,
42  const size_t num_elems_to_append,
43  const SQLTypeInfo& ti,
44  const bool replicating = false,
45  const int64_t offset = -1) override {
46  T* unencoded_data = reinterpret_cast<T*>(src_data);
47  auto encoded_data = std::make_unique<V[]>(num_elems_to_append);
48  for (size_t i = 0; i < num_elems_to_append; ++i) {
49  size_t ri = replicating ? 0 : i;
50  encoded_data.get()[i] = encodeDataAndUpdateStats(unencoded_data[ri]);
51  }
52 
53  // assume always CPU_BUFFER?
54  if (offset == -1) {
55  num_elems_ += num_elems_to_append;
56  buffer_->append(reinterpret_cast<int8_t*>(encoded_data.get()),
57  num_elems_to_append * sizeof(V));
58  if (!replicating) {
59  src_data += num_elems_to_append * sizeof(T);
60  }
61  } else {
62  num_elems_ = offset + num_elems_to_append;
63  CHECK(!replicating);
64  CHECK_GE(offset, 0);
65  buffer_->write(reinterpret_cast<int8_t*>(encoded_data.get()),
66  num_elems_to_append * sizeof(V),
67  static_cast<size_t>(offset));
68  }
69  auto chunk_metadata = std::make_shared<ChunkMetadata>();
70  getMetadata(chunk_metadata);
71  return chunk_metadata;
72  }
73 
74  void getMetadata(const std::shared_ptr<ChunkMetadata>& chunkMetadata) override {
75  Encoder::getMetadata(chunkMetadata); // call on parent class
76  chunkMetadata->fillChunkStats(dataMin, dataMax, has_nulls);
77  }
78 
79  // Only called from the executor for synthesized meta-information.
80  std::shared_ptr<ChunkMetadata> getMetadata(const SQLTypeInfo& ti) override {
81  auto chunk_metadata = std::make_shared<ChunkMetadata>(ti, 0, 0, ChunkStats{});
82  chunk_metadata->fillChunkStats(dataMin, dataMax, has_nulls);
83  return chunk_metadata;
84  }
85 
86  // Only called from the executor for synthesized meta-information.
87  void updateStats(const int64_t val, const bool is_null) override {
88  if (is_null) {
89  has_nulls = true;
90  } else {
91  const auto data = static_cast<T>(val);
92  dataMin = std::min(dataMin, data);
93  dataMax = std::max(dataMax, data);
94  }
95  }
96 
97  // Only called from the executor for synthesized meta-information.
98  void updateStats(const double val, const bool is_null) override {
99  if (is_null) {
100  has_nulls = true;
101  } else {
102  const auto data = static_cast<T>(val);
103  dataMin = std::min(dataMin, data);
104  dataMax = std::max(dataMax, data);
105  }
106  }
107 
108  void updateStats(const int8_t* const src_data, const size_t num_elements) override {
109  const T* unencoded_data = reinterpret_cast<const T*>(src_data);
110  for (size_t i = 0; i < num_elements; ++i) {
111  encodeDataAndUpdateStats(unencoded_data[i]);
112  }
113  }
114 
115  void updateStats(const std::vector<std::string>* const src_data,
116  const size_t start_idx,
117  const size_t num_elements) override {
118  UNREACHABLE();
119  }
120 
121  void updateStats(const std::vector<ArrayDatum>* const src_data,
122  const size_t start_idx,
123  const size_t num_elements) override {
124  UNREACHABLE();
125  }
126 
127  // Only called from the executor for synthesized meta-information.
128  void reduceStats(const Encoder& that) override {
129  const auto that_typed = static_cast<const FixedLengthEncoder<T, V>&>(that);
130  if (that_typed.has_nulls) {
131  has_nulls = true;
132  }
133  dataMin = std::min(dataMin, that_typed.dataMin);
134  dataMax = std::max(dataMax, that_typed.dataMax);
135  }
136 
137  void copyMetadata(const Encoder* copyFromEncoder) override {
138  num_elems_ = copyFromEncoder->getNumElems();
139  auto castedEncoder =
140  reinterpret_cast<const FixedLengthEncoder<T, V>*>(copyFromEncoder);
141  dataMin = castedEncoder->dataMin;
142  dataMax = castedEncoder->dataMax;
143  has_nulls = castedEncoder->has_nulls;
144  }
145 
146  void writeMetadata(FILE* f) override {
147  // assumes pointer is already in right place
148  fwrite((int8_t*)&num_elems_, sizeof(size_t), 1, f);
149  fwrite((int8_t*)&dataMin, sizeof(T), 1, f);
150  fwrite((int8_t*)&dataMax, sizeof(T), 1, f);
151  fwrite((int8_t*)&has_nulls, sizeof(bool), 1, f);
152  }
153 
154  void readMetadata(FILE* f) override {
155  // assumes pointer is already in right place
156  fread((int8_t*)&num_elems_, sizeof(size_t), 1, f);
157  fread((int8_t*)&dataMin, 1, sizeof(T), f);
158  fread((int8_t*)&dataMax, 1, sizeof(T), f);
159  fread((int8_t*)&has_nulls, 1, sizeof(bool), f);
160  }
161 
162  bool resetChunkStats(const ChunkStats& stats) override {
163  const auto new_min = DatumFetcher::getDatumVal<T>(stats.min);
164  const auto new_max = DatumFetcher::getDatumVal<T>(stats.max);
165 
166  if (dataMin == new_min && dataMax == new_max && has_nulls == stats.has_nulls) {
167  return false;
168  }
169 
170  dataMin = new_min;
171  dataMax = new_max;
172  has_nulls = stats.has_nulls;
173  return true;
174  }
175 
178  bool has_nulls;
179 
180  private:
181  V encodeDataAndUpdateStats(const T& unencoded_data) {
182  V encoded_data = static_cast<V>(unencoded_data);
183  if (unencoded_data != encoded_data) {
184  decimal_overflow_validator_.validate(unencoded_data);
185  LOG(ERROR) << "Fixed encoding failed, Unencoded: " +
186  std::to_string(unencoded_data) +
187  " encoded: " + std::to_string(encoded_data);
188  } else {
189  T data = unencoded_data;
190  if (data == std::numeric_limits<V>::min()) {
191  has_nulls = true;
192  } else {
194  dataMin = std::min(dataMin, data);
195  dataMax = std::max(dataMax, data);
196  }
197  }
198  return encoded_data;
199  }
200 }; // FixedLengthEncoder
201 
202 #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:218
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:222
#define LOG(tag)
Definition: Logger.h:188
#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
void updateStats(const std::vector< std::string > *const src_data, const size_t start_idx, const size_t num_elements) override
std::string to_string(char const *&&v)
virtual void getMetadata(const std::shared_ptr< ChunkMetadata > &chunkMetadata)
Definition: Encoder.cpp:227
Data_Namespace::AbstractBuffer * buffer_
Definition: Encoder.h:220
void copyMetadata(const Encoder *copyFromEncoder) override
void readMetadata(FILE *f) override
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
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 validate(T value)
Definition: Encoder.h:54
bool is_null(const T &v, const SQLTypeInfo &t)
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
size_t getNumElems() const
Definition: Encoder.h:214
#define CHECK(condition)
Definition: Logger.h:197
void reduceStats(const Encoder &that) override
std::shared_ptr< ChunkMetadata > getMetadata(const SQLTypeInfo &ti) override