OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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:
34  }
35 
36  std::shared_ptr<ChunkMetadata> appendData(int8_t*& src_data,
37  const size_t num_elems_to_append,
38  const SQLTypeInfo& ti,
39  const bool replicating = false,
40  const int64_t offset = -1) override {
41  CHECK(ti.is_date_in_days());
42  if (offset == 0 && num_elems_to_append >= num_elems_) {
44  }
45  T* unencoded_data = reinterpret_cast<T*>(src_data);
46  auto encoded_data = std::make_unique<V[]>(num_elems_to_append);
47  for (size_t i = 0; i < num_elems_to_append; ++i) {
48  size_t ri = replicating ? 0 : i;
49  encoded_data.get()[i] = encodeDataAndUpdateStats(unencoded_data[ri]);
50  }
51 
52  if (offset == -1) {
53  num_elems_ += num_elems_to_append;
54  buffer_->append(reinterpret_cast<int8_t*>(encoded_data.get()),
55  num_elems_to_append * sizeof(V));
56  if (!replicating) {
57  src_data += num_elems_to_append * sizeof(T);
58  }
59  } else {
60  num_elems_ = offset + num_elems_to_append;
61  CHECK(!replicating);
62  CHECK_GE(offset, 0);
63  buffer_->write(reinterpret_cast<int8_t*>(encoded_data.get()),
64  num_elems_to_append * sizeof(V),
65  static_cast<size_t>(offset));
66  }
67 
68  auto chunk_metadata = std::make_shared<ChunkMetadata>();
69  getMetadata(chunk_metadata);
70  return chunk_metadata;
71  }
72 
73  void getMetadata(const std::shared_ptr<ChunkMetadata>& chunkMetadata) override {
74  Encoder::getMetadata(chunkMetadata);
75  chunkMetadata->fillChunkStats(dataMin, dataMax, has_nulls);
76  }
77 
78  // Only called from the executor for synthesized meta-information.
79  std::shared_ptr<ChunkMetadata> getMetadata(const SQLTypeInfo& ti) override {
80  auto chunk_metadata = std::make_shared<ChunkMetadata>(ti, 0, 0, ChunkStats{});
81  chunk_metadata->fillChunkStats(dataMin, dataMax, has_nulls);
82  return chunk_metadata;
83  }
84 
85  // Only called from the executor for synthesized meta-information.
86  void updateStats(const int64_t val, const bool is_null) override {
87  if (is_null) {
88  has_nulls = true;
89  } else {
90  const auto data = static_cast<T>(val);
91  dataMin = std::min(dataMin, data);
92  dataMax = std::max(dataMax, data);
93  }
94  }
95 
96  // Only called from the executor for synthesized meta-information.
97  void updateStats(const double 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  void updateStats(const int8_t* const src_data, const size_t num_elements) override {
108  const T* unencoded_data = reinterpret_cast<const T*>(src_data);
109  for (size_t i = 0; i < num_elements; ++i) {
110  encodeDataAndUpdateStats(unencoded_data[i]);
111  }
112  }
113 
114  void updateStats(const std::vector<std::string>* const src_data,
115  const size_t start_idx,
116  const size_t num_elements) override {
117  UNREACHABLE();
118  }
119 
120  void updateStats(const std::vector<ArrayDatum>* const src_data,
121  const size_t start_idx,
122  const size_t num_elements) override {
123  UNREACHABLE();
124  }
125 
126  // Only called from the executor for synthesized meta-information.
127  void reduceStats(const Encoder& that) override {
128  const auto that_typed = static_cast<const DateDaysEncoder<T, V>&>(that);
129  if (that_typed.has_nulls) {
130  has_nulls = true;
131  }
132  dataMin = std::min(dataMin, that_typed.dataMin);
133  dataMax = std::max(dataMax, that_typed.dataMax);
134  }
135 
136  void copyMetadata(const Encoder* copyFromEncoder) override {
137  num_elems_ = copyFromEncoder->getNumElems();
138  auto castedEncoder = reinterpret_cast<const DateDaysEncoder<T, V>*>(copyFromEncoder);
139  dataMin = castedEncoder->dataMin;
140  dataMax = castedEncoder->dataMax;
141  has_nulls = castedEncoder->has_nulls;
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, 1, sizeof(T), f);
156  fread((int8_t*)&dataMax, 1, sizeof(T), f);
157  fread((int8_t*)&has_nulls, 1, sizeof(bool), 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 resetChunkStats() override {
175  dataMin = std::numeric_limits<T>::max();
176  dataMax = std::numeric_limits<T>::lowest();
177  has_nulls = false;
178  }
179 
182  bool has_nulls;
183 
184  private:
185  V encodeDataAndUpdateStats(const T& unencoded_data) {
186  V encoded_data;
187  if (unencoded_data == std::numeric_limits<V>::min()) {
188  has_nulls = true;
189  encoded_data = static_cast<V>(unencoded_data);
190  } else {
191  date_days_overflow_validator_.validate(unencoded_data);
192  encoded_data = DateConverters::get_epoch_days_from_seconds(unencoded_data);
193  const T data = DateConverters::get_epoch_seconds_from_days(encoded_data);
194  dataMax = std::max(dataMax, data);
195  dataMin = std::min(dataMin, data);
196  }
197  return encoded_data;
198  }
199 }; // DateDaysEncoder
200 
201 #endif // DATE_DAYS_ENCODER_H
size_t num_elems_
Definition: Encoder.h:237
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:247
#define CHECK_GE(x, y)
Definition: Logger.h:216
bool has_nulls
Definition: ChunkMetadata.h:28
void resetChunkStats() override
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)
CONSTEXPR DEVICE bool is_null(const T &value)
Data_Namespace::AbstractBuffer * buffer_
Definition: Encoder.h:239
V encodeDataAndUpdateStats(const T &unencoded_data)
void writeMetadata(FILE *f) override
size_t getNumElems() const
Definition: Encoder.h:233
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:242
std::shared_ptr< ChunkMetadata > getMetadata(const SQLTypeInfo &ti) override
void getMetadata(const std::shared_ptr< ChunkMetadata > &chunkMetadata) override
bool is_date_in_days() const
Definition: sqltypes.h:721
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
#define CHECK(condition)
Definition: Logger.h:203
bool resetChunkStats(const ChunkStats &stats) override
: Reset chunk level stats (min, max, nulls) using new values from the argument.
char * f
int64_t get_epoch_days_from_seconds(const int64_t seconds)
DateDaysEncoder(Data_Namespace::AbstractBuffer *buffer)