OmniSciDB  2e3a973ef4
DateDaysEncoder.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, 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 DATE_DAYS_ENCODER_H
18 #define DATE_DAYS_ENCODER_H
19 
20 #include "Logger/Logger.h"
21 
22 #include <iostream>
23 #include <memory>
24 #include "AbstractBuffer.h"
25 #include "Encoder.h"
26 
27 #include <Shared/DatumFetchers.h>
28 
29 template <typename T, typename V>
30 class DateDaysEncoder : public Encoder {
31  public:
33  : Encoder(buffer)
34  , dataMin(std::numeric_limits<T>::max())
35  , dataMax(std::numeric_limits<T>::min())
36  , has_nulls(false) {}
37 
38  std::shared_ptr<ChunkMetadata> appendData(int8_t*& src_data,
39  const size_t num_elems_to_append,
40  const SQLTypeInfo& ti,
41  const bool replicating = false,
42  const int64_t offset = -1) override {
43  CHECK(ti.is_date_in_days());
44  T* unencoded_data = reinterpret_cast<T*>(src_data);
45  auto encoded_data = std::make_unique<V[]>(num_elems_to_append);
46  for (size_t i = 0; i < num_elems_to_append; ++i) {
47  size_t ri = replicating ? 0 : i;
48  encoded_data.get()[i] = encodeDataAndUpdateStats(unencoded_data[ri]);
49  }
50 
51  if (offset == -1) {
52  num_elems_ += num_elems_to_append;
53  buffer_->append(reinterpret_cast<int8_t*>(encoded_data.get()),
54  num_elems_to_append * sizeof(V));
55  if (!replicating) {
56  src_data += num_elems_to_append * sizeof(T);
57  }
58  } else {
59  num_elems_ = offset + num_elems_to_append;
60  CHECK(!replicating);
61  CHECK_GE(offset, 0);
62  buffer_->write(reinterpret_cast<int8_t*>(encoded_data.get()),
63  num_elems_to_append * sizeof(V),
64  static_cast<size_t>(offset));
65  }
66 
67  auto chunk_metadata = std::make_shared<ChunkMetadata>();
68  getMetadata(chunk_metadata);
69  return chunk_metadata;
70  }
71 
72  void getMetadata(const std::shared_ptr<ChunkMetadata>& chunkMetadata) override {
73  Encoder::getMetadata(chunkMetadata);
74  chunkMetadata->fillChunkStats(dataMin, dataMax, has_nulls);
75  }
76 
77  // Only called from the executor for synthesized meta-information.
78  std::shared_ptr<ChunkMetadata> getMetadata(const SQLTypeInfo& ti) override {
79  auto chunk_metadata = std::make_shared<ChunkMetadata>(ti, 0, 0, ChunkStats{});
80  chunk_metadata->fillChunkStats(dataMin, dataMax, has_nulls);
81  return chunk_metadata;
82  }
83 
84  // Only called from the executor for synthesized meta-information.
85  void updateStats(const int64_t val, const bool is_null) override {
86  if (is_null) {
87  has_nulls = true;
88  } else {
89  const auto data = static_cast<T>(val);
90  dataMin = std::min(dataMin, data);
91  dataMax = std::max(dataMax, data);
92  }
93  }
94 
95  // Only called from the executor for synthesized meta-information.
96  void updateStats(const double val, const bool is_null) override {
97  if (is_null) {
98  has_nulls = true;
99  } else {
100  const auto data = static_cast<T>(val);
101  dataMin = std::min(dataMin, data);
102  dataMax = std::max(dataMax, data);
103  }
104  }
105 
106  void updateStats(const int8_t* const src_data, const size_t num_elements) override {
107  const T* unencoded_data = reinterpret_cast<const T*>(src_data);
108  for (size_t i = 0; i < num_elements; ++i) {
109  encodeDataAndUpdateStats(unencoded_data[i]);
110  }
111  }
112 
113  void updateStats(const std::vector<std::string>* const src_data,
114  const size_t start_idx,
115  const size_t num_elements) override {
116  UNREACHABLE();
117  }
118 
119  void updateStats(const std::vector<ArrayDatum>* const src_data,
120  const size_t start_idx,
121  const size_t num_elements) override {
122  UNREACHABLE();
123  }
124 
125  // Only called from the executor for synthesized meta-information.
126  void reduceStats(const Encoder& that) override {
127  const auto that_typed = static_cast<const DateDaysEncoder<T, V>&>(that);
128  if (that_typed.has_nulls) {
129  has_nulls = true;
130  }
131  dataMin = std::min(dataMin, that_typed.dataMin);
132  dataMax = std::max(dataMax, that_typed.dataMax);
133  }
134 
135  void copyMetadata(const Encoder* copyFromEncoder) override {
136  num_elems_ = copyFromEncoder->getNumElems();
137  auto castedEncoder = reinterpret_cast<const DateDaysEncoder<T, V>*>(copyFromEncoder);
138  dataMin = castedEncoder->dataMin;
139  dataMax = castedEncoder->dataMax;
140  has_nulls = castedEncoder->has_nulls;
141  }
142 
143  void writeMetadata(FILE* f) override {
144  // assumes pointer is already in right place
145  fwrite((int8_t*)&num_elems_, sizeof(size_t), 1, f);
146  fwrite((int8_t*)&dataMin, sizeof(T), 1, f);
147  fwrite((int8_t*)&dataMax, sizeof(T), 1, f);
148  fwrite((int8_t*)&has_nulls, sizeof(bool), 1, f);
149  }
150 
151  void readMetadata(FILE* f) override {
152  // assumes pointer is already in right place
153  fread((int8_t*)&num_elems_, sizeof(size_t), 1, f);
154  fread((int8_t*)&dataMin, 1, sizeof(T), f);
155  fread((int8_t*)&dataMax, 1, sizeof(T), f);
156  fread((int8_t*)&has_nulls, 1, sizeof(bool), f);
157  }
158 
159  bool resetChunkStats(const ChunkStats& stats) override {
160  const auto new_min = DatumFetcher::getDatumVal<T>(stats.min);
161  const auto new_max = DatumFetcher::getDatumVal<T>(stats.max);
162 
163  if (dataMin == new_min && dataMax == new_max && has_nulls == stats.has_nulls) {
164  return false;
165  }
166 
167  dataMin = new_min;
168  dataMax = new_max;
169  has_nulls = stats.has_nulls;
170  return true;
171  }
172 
175  bool has_nulls;
176 
177  private:
178  V encodeDataAndUpdateStats(const T& unencoded_data) {
179  V encoded_data;
180  if (unencoded_data == std::numeric_limits<V>::min()) {
181  has_nulls = true;
182  encoded_data = static_cast<V>(unencoded_data);
183  } else {
184  date_days_overflow_validator_.validate(unencoded_data);
185  encoded_data = DateConverters::get_epoch_days_from_seconds(unencoded_data);
186  const T data = DateConverters::get_epoch_seconds_from_days(encoded_data);
187  dataMax = std::max(dataMax, data);
188  dataMin = std::min(dataMin, data);
189  }
190  return encoded_data;
191  }
192 }; // DateDaysEncoder
193 
194 #endif // DATE_DAYS_ENCODER_H
size_t num_elems_
Definition: Encoder.h:218
void updateStats(const int8_t *const src_data, const size_t num_elements) override
void updateStats(const std::vector< ArrayDatum > *const src_data, const size_t start_idx, const size_t num_elements) override
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
void updateStats(const int64_t val, const bool is_null) override
#define UNREACHABLE()
Definition: Logger.h:241
#define CHECK_GE(x, y)
Definition: Logger.h:210
bool has_nulls
Definition: ChunkMetadata.h:28
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
void readMetadata(FILE *f) override
int64_t get_epoch_seconds_from_days(const int64_t days)
Data_Namespace::AbstractBuffer * buffer_
Definition: Encoder.h:220
V encodeDataAndUpdateStats(const T &unencoded_data)
void writeMetadata(FILE *f) override
void updateStats(const double val, const bool is_null) override
void validate(T value)
Definition: Encoder.h:122
An AbstractBuffer is a unit of data management for a data manager.
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
DateDaysOverflowValidator date_days_overflow_validator_
Definition: Encoder.h:223
std::shared_ptr< ChunkMetadata > getMetadata(const SQLTypeInfo &ti) override
void getMetadata(const std::shared_ptr< ChunkMetadata > &chunkMetadata) override
bool is_null(const T &v, const SQLTypeInfo &t)
virtual void append(int8_t *src, const size_t num_bytes, const MemoryLevel src_buffer_type=CPU_LEVEL, const int device_id=-1)=0
void reduceStats(const Encoder &that) override
void copyMetadata(const Encoder *copyFromEncoder) override
size_t getNumElems() const
Definition: Encoder.h:214
#define CHECK(condition)
Definition: Logger.h:197
bool resetChunkStats(const ChunkStats &stats) override
: Reset chunk level stats (min, max, nulls) using new values from the argument.
int64_t get_epoch_days_from_seconds(const int64_t seconds)
DateDaysEncoder(Data_Namespace::AbstractBuffer *buffer)
bool is_date_in_days() const
Definition: sqltypes.h:632