OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Encoder.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 ENCODER_H
18 #define ENCODER_H
19 
20 #include "../Shared/DateConverters.h"
21 #include "../Shared/sqltypes.h"
22 #include "../Shared/types.h"
23 #include "ChunkMetadata.h"
24 
25 #include <cmath>
26 #include <iostream>
27 #include <limits>
28 #include <stdexcept>
29 #include <vector>
30 
31 namespace Data_Namespace {
32 class AbstractBuffer;
33 }
34 
35 // default max input buffer size to 1MB
36 #define MAX_INPUT_BUF_SIZE 1048576
37 
39  public:
41  if (type.is_array()) {
42  type = type.get_elem_type();
43  }
44 
45  do_check_ = type.is_decimal();
46  int precision = type.get_precision();
47  int scale = type.get_scale();
48  max_ = (int64_t)std::pow((double)10.0, precision);
49  min_ = -max_;
50  pow10_ = precision - scale;
51  }
52 
53  template <typename T>
54  void validate(T value) {
55  if (std::is_integral<T>::value) {
56  do_validate(static_cast<int64_t>(value));
57  }
58  }
59 
60  void do_validate(int64_t value) {
61  if (!do_check_) {
62  return;
63  }
64 
65  if (value >= max_) {
66  throw std::runtime_error("Decimal overflow: value is greater than 10^" +
68  " value " + std::to_string(value));
69  }
70 
71  if (value <= min_) {
72  throw std::runtime_error("Decimal overflow: value is less than -10^" +
74  " value " + std::to_string(value));
75  }
76  }
77 
78  private:
79  bool do_check_;
80  int64_t max_;
81  int64_t min_;
82  int pow10_;
83 };
84 
85 template <typename INNER_VALIDATOR>
87  public:
88  NullAwareValidator(SQLTypeInfo type, INNER_VALIDATOR* inner_validator) {
89  if (type.is_array()) {
90  type = type.get_elem_type();
91  }
92 
94  inner_validator_ = inner_validator;
95  }
96 
97  template <typename T>
98  void validate(T value) {
99  if (skip_null_check_ || value != inline_int_null_value<T>()) {
100  inner_validator_->template validate<T>(value);
101  }
102  }
103 
104  private:
106  INNER_VALIDATOR* inner_validator_;
107 };
108 
110  public:
113  type.is_array() ? type.get_elem_type().is_date_in_days() : type.is_date_in_days();
114  const bool is_date_16_ = is_date_in_days_ ? type.get_comp_param() == 16 : false;
115  max_ = is_date_16_ ? static_cast<int64_t>(std::numeric_limits<int16_t>::max())
116  : static_cast<int64_t>(std::numeric_limits<int32_t>::max());
117  min_ = is_date_16_ ? static_cast<int64_t>(std::numeric_limits<int16_t>::min())
118  : static_cast<int64_t>(std::numeric_limits<int32_t>::min());
119  }
120 
121  template <typename T>
122  void validate(T value) {
123  if (!is_date_in_days_ || !std::is_integral<T>::value) {
124  return;
125  }
126  const int64_t days =
127  DateConverters::get_epoch_days_from_seconds(static_cast<int64_t>(value));
128  if (days > max_) {
129  throw std::runtime_error("Date encoding overflow: Epoch days " +
130  std::to_string(days) + " greater than maximum capacity " +
132  }
133  if (days < min_) {
134  throw std::runtime_error("Date encoding underflow: Epoch days " +
135  std::to_string(days) + " less than minumum capacity " +
137  }
138  }
139 
140  private:
142  int64_t max_;
143  int64_t min_;
144 };
145 
146 class Encoder {
147  public:
149  const SQLTypeInfo sqlType);
151  virtual ~Encoder() {}
152 
153  virtual ChunkMetadata appendData(int8_t*& srcData,
154  const size_t numAppendElems,
155  const SQLTypeInfo&,
156  const bool replicating = false) = 0;
157  virtual void getMetadata(ChunkMetadata& chunkMetadata);
158  // Only called from the executor for synthesized meta-information.
159  virtual ChunkMetadata getMetadata(const SQLTypeInfo& ti) = 0;
160  virtual void updateStats(const int64_t val, const bool is_null) = 0;
161  virtual void updateStats(const double val, const bool is_null) = 0;
162  virtual void reduceStats(const Encoder&) = 0;
163  virtual void copyMetadata(const Encoder* copyFromEncoder) = 0;
164  virtual void writeMetadata(FILE* f /*, const size_t offset*/) = 0;
165  virtual void readMetadata(FILE* f /*, const size_t offset*/) = 0;
166 
173  virtual bool resetChunkStats(const ChunkStats&) { return false; }
174 
175  size_t getNumElems() const { return num_elems_; }
176  void setNumElems(const size_t num_elems) { num_elems_ = num_elems; }
177 
178  protected:
179  size_t num_elems_;
180 
182  // ChunkMetadata metadataTemplate_;
183 
186 };
187 
188 #endif // Encoder_h
void do_validate(int64_t value)
Definition: Encoder.h:60
size_t num_elems_
Definition: Encoder.h:179
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:335
DateDaysOverflowValidator(SQLTypeInfo type)
Definition: Encoder.h:111
DecimalOverflowValidator decimal_overflow_validator_
Definition: Encoder.h:184
static Encoder * Create(Data_Namespace::AbstractBuffer *buffer, const SQLTypeInfo sqlType)
Definition: Encoder.cpp:26
HOST DEVICE int get_scale() const
Definition: sqltypes.h:331
INNER_VALIDATOR * inner_validator_
Definition: Encoder.h:106
void setNumElems(const size_t num_elems)
Definition: Encoder.h:176
std::string to_string(char const *&&v)
virtual bool resetChunkStats(const ChunkStats &)
: Reset chunk level stats (min, max, nulls) using new values from the argument.
Definition: Encoder.h:173
virtual void reduceStats(const Encoder &)=0
bool skip_null_check_
Definition: Encoder.h:105
bool is_array() const
Definition: sqltypes.h:485
Data_Namespace::AbstractBuffer * buffer_
Definition: Encoder.h:181
virtual void getMetadata(ChunkMetadata &chunkMetadata)
Definition: Encoder.cpp:227
size_t getNumElems() const
Definition: Encoder.h:175
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:333
void validate(T value)
Definition: Encoder.h:122
An AbstractBuffer is a unit of data management for a data manager.
virtual ~Encoder()
Definition: Encoder.h:151
void validate(T value)
Definition: Encoder.h:98
Encoder(Data_Namespace::AbstractBuffer *buffer)
Definition: Encoder.cpp:221
DateDaysOverflowValidator date_days_overflow_validator_
Definition: Encoder.h:185
void validate(T value)
Definition: Encoder.h:54
bool is_null(const T &v, const SQLTypeInfo &t)
virtual ChunkMetadata appendData(int8_t *&srcData, const size_t numAppendElems, const SQLTypeInfo &, const bool replicating=false)=0
NullAwareValidator(SQLTypeInfo type, INNER_VALIDATOR *inner_validator)
Definition: Encoder.h:88
int64_t get_epoch_days_from_seconds(const int64_t seconds)
int get_precision() const
Definition: sqltypes.h:329
SQLTypeInfoCore get_elem_type() const
Definition: sqltypes.h:659
virtual void writeMetadata(FILE *f)=0
bool is_decimal() const
Definition: sqltypes.h:480
virtual void updateStats(const int64_t val, const bool is_null)=0
virtual void copyMetadata(const Encoder *copyFromEncoder)=0
virtual void readMetadata(FILE *f)=0
DecimalOverflowValidator(SQLTypeInfo type)
Definition: Encoder.h:40