OmniSciDB  343343d194
 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 "Shared/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  ChunkMetadata appendData(int8_t*& srcData,
39  const size_t numAppendElems,
40  const SQLTypeInfo& ti,
41  const bool replicating = false) override {
42  CHECK(ti.is_date_in_days());
43  T* unencodedData = reinterpret_cast<T*>(srcData);
44  auto encodedData = std::make_unique<V[]>(numAppendElems);
45  for (size_t i = 0; i < numAppendElems; ++i) {
46  size_t ri = replicating ? 0 : i;
47  if (unencodedData[ri] == std::numeric_limits<V>::min()) {
48  has_nulls = true;
49  encodedData.get()[i] = static_cast<V>(unencodedData[ri]);
50  } else {
51  date_days_overflow_validator_.validate(unencodedData[ri]);
52  encodedData.get()[i] =
54  const T data = DateConverters::get_epoch_seconds_from_days(encodedData.get()[i]);
55  dataMax = std::max(dataMax, data);
56  dataMin = std::min(dataMin, data);
57  }
58  }
59  num_elems_ += numAppendElems;
60 
61  buffer_->append((int8_t*)(encodedData.get()), numAppendElems * sizeof(V));
62  ChunkMetadata chunkMetadata;
63  getMetadata(chunkMetadata);
64  if (!replicating) {
65  srcData += numAppendElems * sizeof(T);
66  }
67  return chunkMetadata;
68  }
69 
70  void getMetadata(ChunkMetadata& chunkMetadata) override {
71  Encoder::getMetadata(chunkMetadata);
72  chunkMetadata.fillChunkStats(dataMin, dataMax, has_nulls);
73  }
74 
75  // Only called from the executor for synthesized meta-information.
76  ChunkMetadata getMetadata(const SQLTypeInfo& ti) override {
77  ChunkMetadata chunk_metadata{ti, 0, 0, ChunkStats{}};
78  chunk_metadata.fillChunkStats(dataMin, dataMax, has_nulls);
79  return chunk_metadata;
80  }
81 
82  // Only called from the executor for synthesized meta-information.
83  void updateStats(const int64_t val, const bool is_null) override {
84  if (is_null) {
85  has_nulls = true;
86  } else {
87  const auto data = static_cast<T>(val);
88  dataMin = std::min(dataMin, data);
89  dataMax = std::max(dataMax, data);
90  }
91  }
92 
93  // Only called from the executor for synthesized meta-information.
94  void updateStats(const double 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 reduceStats(const Encoder& that) override {
106  const auto that_typed = static_cast<const DateDaysEncoder<T, V>&>(that);
107  if (that_typed.has_nulls) {
108  has_nulls = true;
109  }
110  dataMin = std::min(dataMin, that_typed.dataMin);
111  dataMax = std::max(dataMax, that_typed.dataMax);
112  }
113 
114  void copyMetadata(const Encoder* copyFromEncoder) override {
115  num_elems_ = copyFromEncoder->getNumElems();
116  auto castedEncoder = reinterpret_cast<const DateDaysEncoder<T, V>*>(copyFromEncoder);
117  dataMin = castedEncoder->dataMin;
118  dataMax = castedEncoder->dataMax;
119  has_nulls = castedEncoder->has_nulls;
120  }
121 
122  void writeMetadata(FILE* f) override {
123  // assumes pointer is already in right place
124  fwrite((int8_t*)&num_elems_, sizeof(size_t), 1, f);
125  fwrite((int8_t*)&dataMin, sizeof(T), 1, f);
126  fwrite((int8_t*)&dataMax, sizeof(T), 1, f);
127  fwrite((int8_t*)&has_nulls, sizeof(bool), 1, f);
128  }
129 
130  void readMetadata(FILE* f) override {
131  // assumes pointer is already in right place
132  fread((int8_t*)&num_elems_, sizeof(size_t), 1, f);
133  fread((int8_t*)&dataMin, 1, sizeof(T), f);
134  fread((int8_t*)&dataMax, 1, sizeof(T), f);
135  fread((int8_t*)&has_nulls, 1, sizeof(bool), f);
136  }
137 
138  bool resetChunkStats(const ChunkStats& stats) override {
139  const auto new_min = DatumFetcher::getDatumVal<T>(stats.min);
140  const auto new_max = DatumFetcher::getDatumVal<T>(stats.max);
141 
142  if (dataMin == new_min && dataMax == new_max && has_nulls == stats.has_nulls) {
143  return false;
144  }
145 
146  dataMin = new_min;
147  dataMax = new_max;
148  has_nulls = stats.has_nulls;
149  return true;
150  }
151 
154  bool has_nulls;
155 
156 }; // DateDaysEncoder
157 
158 #endif // DATE_DAYS_ENCODER_H
size_t num_elems_
Definition: Encoder.h:179
void updateStats(const int64_t val, const bool is_null) override
void fillChunkStats(const T min, const T max, const bool has_nulls)
Definition: ChunkMetadata.h:38
bool has_nulls
Definition: ChunkMetadata.h:28
void readMetadata(FILE *f) override
ChunkMetadata getMetadata(const SQLTypeInfo &ti) override
int64_t get_epoch_seconds_from_days(const int64_t days)
Data_Namespace::AbstractBuffer * buffer_
Definition: Encoder.h:181
virtual void getMetadata(ChunkMetadata &chunkMetadata)
Definition: Encoder.cpp:227
void writeMetadata(FILE *f) override
size_t getNumElems() const
Definition: Encoder.h:175
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.
DateDaysOverflowValidator date_days_overflow_validator_
Definition: Encoder.h:185
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
bool g_enable_debug_timer false
Definition: Execute.cpp:68
#define CHECK(condition)
Definition: Logger.h:187
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)
ChunkMetadata appendData(int8_t *&srcData, const size_t numAppendElems, const SQLTypeInfo &ti, const bool replicating=false) override
void getMetadata(ChunkMetadata &chunkMetadata) override
DateDaysEncoder(Data_Namespace::AbstractBuffer *buffer)