OmniSciDB  b24e664e58
 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  ChunkMetadata appendData(int8_t*& srcData,
41  const size_t numAppendElems,
42  const SQLTypeInfo&,
43  const bool replicating = false) override {
44  T* unencodedData = reinterpret_cast<T*>(srcData);
45  std::vector<T> encoded_data;
46  if (replicating) {
47  encoded_data.resize(numAppendElems);
48  }
49  for (size_t i = 0; i < numAppendElems; ++i) {
50  size_t ri = replicating ? 0 : i;
51  T data = unencodedData[ri];
52  if (replicating) {
53  encoded_data[i] = data;
54  }
55  if (data == none_encoded_null_value<T>()) {
56  has_nulls = true;
57  } else {
59  dataMin = std::min(dataMin, data);
60  dataMax = std::max(dataMax, data);
61  }
62  }
63  num_elems_ += numAppendElems;
64  buffer_->append(
65  replicating ? reinterpret_cast<int8_t*>(encoded_data.data()) : srcData,
66  numAppendElems * sizeof(T));
67  ChunkMetadata chunkMetadata;
68  getMetadata(chunkMetadata);
69  if (!replicating) {
70  srcData += numAppendElems * sizeof(T);
71  }
72  return chunkMetadata;
73  }
74 
75  void getMetadata(ChunkMetadata& chunkMetadata) override {
76  Encoder::getMetadata(chunkMetadata); // call on parent class
77  chunkMetadata.fillChunkStats(dataMin, dataMax, has_nulls);
78  }
79 
80  // Only called from the executor for synthesized meta-information.
81  ChunkMetadata getMetadata(const SQLTypeInfo& ti) override {
82  ChunkMetadata chunk_metadata{ti, 0, 0, ChunkStats{}};
83  chunk_metadata.fillChunkStats(dataMin, dataMax, has_nulls);
84  return chunk_metadata;
85  }
86 
87  // Only called from the executor for synthesized meta-information.
88  void updateStats(const int64_t val, const bool is_null) override {
89  if (is_null) {
90  has_nulls = true;
91  } else {
92  const auto data = static_cast<T>(val);
93  dataMin = std::min(dataMin, data);
94  dataMax = std::max(dataMax, data);
95  }
96  }
97 
98  // Only called from the executor for synthesized meta-information.
99  void updateStats(const double val, const bool is_null) override {
100  if (is_null) {
101  has_nulls = true;
102  } else {
103  const auto data = static_cast<T>(val);
104  dataMin = std::min(dataMin, data);
105  dataMax = std::max(dataMax, data);
106  }
107  }
108 
109  // Only called from the executor for synthesized meta-information.
110  void reduceStats(const Encoder& that) override {
111  const auto that_typed = static_cast<const NoneEncoder&>(that);
112  if (that_typed.has_nulls) {
113  has_nulls = true;
114  }
115  dataMin = std::min(dataMin, that_typed.dataMin);
116  dataMax = std::max(dataMax, that_typed.dataMax);
117  }
118 
119  void writeMetadata(FILE* f) override {
120  // assumes pointer is already in right place
121  fwrite((int8_t*)&num_elems_, sizeof(size_t), 1, f);
122  fwrite((int8_t*)&dataMin, sizeof(T), 1, f);
123  fwrite((int8_t*)&dataMax, sizeof(T), 1, f);
124  fwrite((int8_t*)&has_nulls, sizeof(bool), 1, f);
125  }
126 
127  void readMetadata(FILE* f) override {
128  // assumes pointer is already in right place
129  fread((int8_t*)&num_elems_, sizeof(size_t), 1, f);
130  fread((int8_t*)&dataMin, sizeof(T), 1, f);
131  fread((int8_t*)&dataMax, sizeof(T), 1, f);
132  fread((int8_t*)&has_nulls, sizeof(bool), 1, f);
133  }
134 
135  bool resetChunkStats(const ChunkStats& stats) override {
136  const auto new_min = DatumFetcher::getDatumVal<T>(stats.min);
137  const auto new_max = DatumFetcher::getDatumVal<T>(stats.max);
138 
139  if (dataMin == new_min && dataMax == new_max && has_nulls == stats.has_nulls) {
140  return false;
141  }
142 
143  dataMin = new_min;
144  dataMax = new_max;
145  has_nulls = stats.has_nulls;
146  return true;
147  }
148 
149  void copyMetadata(const Encoder* copyFromEncoder) override {
150  num_elems_ = copyFromEncoder->getNumElems();
151  auto castedEncoder = reinterpret_cast<const NoneEncoder<T>*>(copyFromEncoder);
152  dataMin = castedEncoder->dataMin;
153  dataMax = castedEncoder->dataMax;
154  has_nulls = castedEncoder->has_nulls;
155  }
156 
159  bool has_nulls;
160 
161 }; // class NoneEncoder
162 
163 #endif // NONE_ENCODER_H
ChunkMetadata getMetadata(const SQLTypeInfo &ti) override
Definition: NoneEncoder.h:81
size_t num_elems_
Definition: Encoder.h:179
void writeMetadata(FILE *f) override
Definition: NoneEncoder.h:119
DecimalOverflowValidator decimal_overflow_validator_
Definition: Encoder.h:184
ChunkMetadata appendData(int8_t *&srcData, const size_t numAppendElems, const SQLTypeInfo &, const bool replicating=false) override
Definition: NoneEncoder.h:40
void fillChunkStats(const T min, const T max, const bool has_nulls)
Definition: ChunkMetadata.h:38
bool has_nulls
Definition: ChunkMetadata.h:28
void updateStats(const int64_t val, const bool is_null) override
Definition: NoneEncoder.h:88
Data_Namespace::AbstractBuffer * buffer_
Definition: Encoder.h:181
virtual void getMetadata(ChunkMetadata &chunkMetadata)
Definition: Encoder.cpp:227
void getMetadata(ChunkMetadata &chunkMetadata) override
Definition: NoneEncoder.h:75
size_t getNumElems() const
Definition: Encoder.h:175
An AbstractBuffer is a unit of data management for a data manager.
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:135
bool is_null(const T &v, const SQLTypeInfo &t)
bool has_nulls
Definition: NoneEncoder.h:159
void updateStats(const double val, const bool is_null) override
Definition: NoneEncoder.h:99
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:71
NoneEncoder(Data_Namespace::AbstractBuffer *buffer)
Definition: NoneEncoder.h:34
void reduceStats(const Encoder &that) override
Definition: NoneEncoder.h:110
void copyMetadata(const Encoder *copyFromEncoder) override
Definition: NoneEncoder.h:149
void readMetadata(FILE *f) override
Definition: NoneEncoder.h:127