OmniSciDB  f632821e96
FixedLengthEncoder< T, V > Class Template Reference

#include <FixedLengthEncoder.h>

+ Inheritance diagram for FixedLengthEncoder< T, V >:
+ Collaboration diagram for FixedLengthEncoder< T, V >:

Public Member Functions

 FixedLengthEncoder (Data_Namespace::AbstractBuffer *buffer)
 
std::shared_ptr< ChunkMetadataappendData (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 getMetadata (const std::shared_ptr< ChunkMetadata > &chunkMetadata) override
 
std::shared_ptr< ChunkMetadatagetMetadata (const SQLTypeInfo &ti) override
 
void updateStats (const int64_t val, const bool is_null) override
 
void updateStats (const double val, const bool is_null) override
 
void updateStats (const int8_t *const src_data, const size_t num_elements) override
 
void updateStats (const std::vector< std::string > *const src_data, const size_t start_idx, 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
 
void reduceStats (const Encoder &that) override
 
void copyMetadata (const Encoder *copyFromEncoder) override
 
void writeMetadata (FILE *f) override
 
void readMetadata (FILE *f) override
 
bool resetChunkStats (const ChunkStats &stats) override
 : Reset chunk level stats (min, max, nulls) using new values from the argument. More...
 
- Public Member Functions inherited from Encoder
 Encoder (Data_Namespace::AbstractBuffer *buffer)
 
virtual ~Encoder ()
 
size_t getNumElems () const
 
void setNumElems (const size_t num_elems)
 

Public Attributes

dataMin
 
dataMax
 
bool has_nulls
 

Private Member Functions

encodeDataAndUpdateStats (const T &unencoded_data)
 

Additional Inherited Members

- Static Public Member Functions inherited from Encoder
static EncoderCreate (Data_Namespace::AbstractBuffer *buffer, const SQLTypeInfo sqlType)
 
- Protected Attributes inherited from Encoder
size_t num_elems_
 
Data_Namespace::AbstractBufferbuffer_
 
DecimalOverflowValidator decimal_overflow_validator_
 
DateDaysOverflowValidator date_days_overflow_validator_
 

Detailed Description

template<typename T, typename V>
class FixedLengthEncoder< T, V >

Definition at line 33 of file FixedLengthEncoder.h.

Constructor & Destructor Documentation

◆ FixedLengthEncoder()

template<typename T, typename V>
FixedLengthEncoder< T, V >::FixedLengthEncoder ( Data_Namespace::AbstractBuffer buffer)
inline

Definition at line 35 of file FixedLengthEncoder.h.

36  : Encoder(buffer)
37  , dataMin(std::numeric_limits<T>::max())
38  , dataMax(std::numeric_limits<T>::min())
39  , has_nulls(false) {}
Encoder(Data_Namespace::AbstractBuffer *buffer)
Definition: Encoder.cpp:221

Member Function Documentation

◆ appendData()

template<typename T, typename V>
std::shared_ptr<ChunkMetadata> FixedLengthEncoder< T, V >::appendData ( int8_t *&  src_data,
const size_t  num_elems_to_append,
const SQLTypeInfo ti,
const bool  replicating = false,
const int64_t  offset = -1 
)
inlineoverridevirtual

Append data to the chunk buffer backing this encoder.

Parameters
src_dataSource data for the append
num_elems_to_appendNumber of elements to append
tiSQL Type Info for the column TODO(adb): used?
replicatingPass one value and fill the chunk with it
offsetWrite data starting at a given offset. Default is -1 which indicates an append, an offset of 0 rewrites the chunk up to num_elems_to_append.

Implements Encoder.

Definition at line 41 of file FixedLengthEncoder.h.

References Data_Namespace::AbstractBuffer::append(), Encoder::buffer_, CHECK, CHECK_GE, FixedLengthEncoder< T, V >::encodeDataAndUpdateStats(), FixedLengthEncoder< T, V >::getMetadata(), Encoder::num_elems_, and Data_Namespace::AbstractBuffer::write().

45  {
46  T* unencoded_data = reinterpret_cast<T*>(src_data);
47  auto encoded_data = std::make_unique<V[]>(num_elems_to_append);
48  for (size_t i = 0; i < num_elems_to_append; ++i) {
49  size_t ri = replicating ? 0 : i;
50  encoded_data.get()[i] = encodeDataAndUpdateStats(unencoded_data[ri]);
51  }
52 
53  // assume always CPU_BUFFER?
54  if (offset == -1) {
55  num_elems_ += num_elems_to_append;
56  buffer_->append(reinterpret_cast<int8_t*>(encoded_data.get()),
57  num_elems_to_append * sizeof(V));
58  if (!replicating) {
59  src_data += num_elems_to_append * sizeof(T);
60  }
61  } else {
62  num_elems_ = offset + num_elems_to_append;
63  CHECK(!replicating);
64  CHECK_GE(offset, 0);
65  buffer_->write(reinterpret_cast<int8_t*>(encoded_data.get()),
66  num_elems_to_append * sizeof(V),
67  static_cast<size_t>(offset));
68  }
69  auto chunk_metadata = std::make_shared<ChunkMetadata>();
70  getMetadata(chunk_metadata);
71  return chunk_metadata;
72  }
size_t num_elems_
Definition: Encoder.h:218
#define CHECK_GE(x, y)
Definition: Logger.h:210
Data_Namespace::AbstractBuffer * buffer_
Definition: Encoder.h:220
V encodeDataAndUpdateStats(const T &unencoded_data)
void getMetadata(const std::shared_ptr< ChunkMetadata > &chunkMetadata) override
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
virtual void append(int8_t *src, const size_t num_bytes, const MemoryLevel src_buffer_type=CPU_LEVEL, const int device_id=-1)=0
#define CHECK(condition)
Definition: Logger.h:197
+ Here is the call graph for this function:

◆ copyMetadata()

template<typename T, typename V>
void FixedLengthEncoder< T, V >::copyMetadata ( const Encoder copyFromEncoder)
inlineoverridevirtual

Implements Encoder.

Definition at line 137 of file FixedLengthEncoder.h.

References FixedLengthEncoder< T, V >::dataMax, FixedLengthEncoder< T, V >::dataMin, Encoder::getNumElems(), FixedLengthEncoder< T, V >::has_nulls, and Encoder::num_elems_.

137  {
138  num_elems_ = copyFromEncoder->getNumElems();
139  auto castedEncoder =
140  reinterpret_cast<const FixedLengthEncoder<T, V>*>(copyFromEncoder);
141  dataMin = castedEncoder->dataMin;
142  dataMax = castedEncoder->dataMax;
143  has_nulls = castedEncoder->has_nulls;
144  }
size_t num_elems_
Definition: Encoder.h:218
size_t getNumElems() const
Definition: Encoder.h:214
+ Here is the call graph for this function:

◆ encodeDataAndUpdateStats()

template<typename T, typename V>
V FixedLengthEncoder< T, V >::encodeDataAndUpdateStats ( const T &  unencoded_data)
inlineprivate

Definition at line 181 of file FixedLengthEncoder.h.

References Encoder::decimal_overflow_validator_, logger::ERROR, LOG, to_string(), and DecimalOverflowValidator::validate().

Referenced by FixedLengthEncoder< T, V >::appendData(), and FixedLengthEncoder< T, V >::updateStats().

181  {
182  V encoded_data = static_cast<V>(unencoded_data);
183  if (unencoded_data != encoded_data) {
184  decimal_overflow_validator_.validate(unencoded_data);
185  LOG(ERROR) << "Fixed encoding failed, Unencoded: " +
186  std::to_string(unencoded_data) +
187  " encoded: " + std::to_string(encoded_data);
188  } else {
189  T data = unencoded_data;
190  if (data == std::numeric_limits<V>::min()) {
191  has_nulls = true;
192  } else {
194  dataMin = std::min(dataMin, data);
195  dataMax = std::max(dataMax, data);
196  }
197  }
198  return encoded_data;
199  }
DecimalOverflowValidator decimal_overflow_validator_
Definition: Encoder.h:222
#define LOG(tag)
Definition: Logger.h:188
std::string to_string(char const *&&v)
void validate(T value)
Definition: Encoder.h:54
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMetadata() [1/2]

template<typename T, typename V>
void FixedLengthEncoder< T, V >::getMetadata ( const std::shared_ptr< ChunkMetadata > &  chunkMetadata)
inlineoverridevirtual

Reimplemented from Encoder.

Definition at line 74 of file FixedLengthEncoder.h.

References FixedLengthEncoder< T, V >::dataMax, FixedLengthEncoder< T, V >::dataMin, Encoder::getMetadata(), and FixedLengthEncoder< T, V >::has_nulls.

Referenced by FixedLengthEncoder< T, V >::appendData().

74  {
75  Encoder::getMetadata(chunkMetadata); // call on parent class
76  chunkMetadata->fillChunkStats(dataMin, dataMax, has_nulls);
77  }
virtual void getMetadata(const std::shared_ptr< ChunkMetadata > &chunkMetadata)
Definition: Encoder.cpp:227
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMetadata() [2/2]

template<typename T, typename V>
std::shared_ptr<ChunkMetadata> FixedLengthEncoder< T, V >::getMetadata ( const SQLTypeInfo ti)
inlineoverridevirtual

Implements Encoder.

Definition at line 80 of file FixedLengthEncoder.h.

References FixedLengthEncoder< T, V >::dataMax, FixedLengthEncoder< T, V >::dataMin, and FixedLengthEncoder< T, V >::has_nulls.

80  {
81  auto chunk_metadata = std::make_shared<ChunkMetadata>(ti, 0, 0, ChunkStats{});
82  chunk_metadata->fillChunkStats(dataMin, dataMax, has_nulls);
83  return chunk_metadata;
84  }

◆ readMetadata()

template<typename T, typename V>
void FixedLengthEncoder< T, V >::readMetadata ( FILE *  f)
inlineoverridevirtual

Implements Encoder.

Definition at line 154 of file FixedLengthEncoder.h.

References FixedLengthEncoder< T, V >::dataMax, FixedLengthEncoder< T, V >::dataMin, FixedLengthEncoder< T, V >::has_nulls, and Encoder::num_elems_.

154  {
155  // assumes pointer is already in right place
156  fread((int8_t*)&num_elems_, sizeof(size_t), 1, f);
157  fread((int8_t*)&dataMin, 1, sizeof(T), f);
158  fread((int8_t*)&dataMax, 1, sizeof(T), f);
159  fread((int8_t*)&has_nulls, 1, sizeof(bool), f);
160  }
size_t num_elems_
Definition: Encoder.h:218

◆ reduceStats()

template<typename T, typename V>
void FixedLengthEncoder< T, V >::reduceStats ( const Encoder that)
inlineoverridevirtual

Implements Encoder.

Definition at line 128 of file FixedLengthEncoder.h.

References FixedLengthEncoder< T, V >::dataMax, FixedLengthEncoder< T, V >::dataMin, and FixedLengthEncoder< T, V >::has_nulls.

128  {
129  const auto that_typed = static_cast<const FixedLengthEncoder<T, V>&>(that);
130  if (that_typed.has_nulls) {
131  has_nulls = true;
132  }
133  dataMin = std::min(dataMin, that_typed.dataMin);
134  dataMax = std::max(dataMax, that_typed.dataMax);
135  }

◆ resetChunkStats()

template<typename T, typename V>
bool FixedLengthEncoder< T, V >::resetChunkStats ( const ChunkStats )
inlineoverridevirtual

: Reset chunk level stats (min, max, nulls) using new values from the argument.

Returns
: True if an update occurred and the chunk needs to be flushed. False otherwise. Default false if metadata update is unsupported. Only reset chunk stats if the incoming stats differ from the current stats.

Reimplemented from Encoder.

Definition at line 162 of file FixedLengthEncoder.h.

References FixedLengthEncoder< T, V >::dataMax, FixedLengthEncoder< T, V >::dataMin, ChunkStats::has_nulls, FixedLengthEncoder< T, V >::has_nulls, ChunkStats::max, and ChunkStats::min.

162  {
163  const auto new_min = DatumFetcher::getDatumVal<T>(stats.min);
164  const auto new_max = DatumFetcher::getDatumVal<T>(stats.max);
165 
166  if (dataMin == new_min && dataMax == new_max && has_nulls == stats.has_nulls) {
167  return false;
168  }
169 
170  dataMin = new_min;
171  dataMax = new_max;
172  has_nulls = stats.has_nulls;
173  return true;
174  }

◆ updateStats() [1/5]

template<typename T, typename V>
void FixedLengthEncoder< T, V >::updateStats ( const int64_t  val,
const bool  is_null 
)
inlineoverridevirtual

Implements Encoder.

Definition at line 87 of file FixedLengthEncoder.h.

References FixedLengthEncoder< T, V >::dataMax, FixedLengthEncoder< T, V >::dataMin, and FixedLengthEncoder< T, V >::has_nulls.

87  {
88  if (is_null) {
89  has_nulls = true;
90  } else {
91  const auto data = static_cast<T>(val);
92  dataMin = std::min(dataMin, data);
93  dataMax = std::max(dataMax, data);
94  }
95  }
bool is_null(const T &v, const SQLTypeInfo &t)

◆ updateStats() [2/5]

template<typename T, typename V>
void FixedLengthEncoder< T, V >::updateStats ( const double  val,
const bool  is_null 
)
inlineoverridevirtual

Implements Encoder.

Definition at line 98 of file FixedLengthEncoder.h.

References FixedLengthEncoder< T, V >::dataMax, FixedLengthEncoder< T, V >::dataMin, and FixedLengthEncoder< T, V >::has_nulls.

98  {
99  if (is_null) {
100  has_nulls = true;
101  } else {
102  const auto data = static_cast<T>(val);
103  dataMin = std::min(dataMin, data);
104  dataMax = std::max(dataMax, data);
105  }
106  }
bool is_null(const T &v, const SQLTypeInfo &t)

◆ updateStats() [3/5]

template<typename T, typename V>
void FixedLengthEncoder< T, V >::updateStats ( const int8_t *const  src_data,
const size_t  num_elements 
)
inlineoverridevirtual

Update statistics for data without appending.

Parameters
src_data- the data with which to update statistics
num_elements- the number of elements to scan in the data

Implements Encoder.

Definition at line 108 of file FixedLengthEncoder.h.

References FixedLengthEncoder< T, V >::encodeDataAndUpdateStats().

108  {
109  const T* unencoded_data = reinterpret_cast<const T*>(src_data);
110  for (size_t i = 0; i < num_elements; ++i) {
111  encodeDataAndUpdateStats(unencoded_data[i]);
112  }
113  }
V encodeDataAndUpdateStats(const T &unencoded_data)
+ Here is the call graph for this function:

◆ updateStats() [4/5]

template<typename T, typename V>
void FixedLengthEncoder< T, V >::updateStats ( const std::vector< std::string > *const  src_data,
const size_t  start_idx,
const size_t  num_elements 
)
inlineoverridevirtual

Update statistics for string data without appending.

Parameters
src_data- the string data with which to update statistics
start_idx- the offset into src_data to start the update
num_elements- the number of elements to scan in the string data

Implements Encoder.

Definition at line 115 of file FixedLengthEncoder.h.

References UNREACHABLE.

117  {
118  UNREACHABLE();
119  }
#define UNREACHABLE()
Definition: Logger.h:241

◆ updateStats() [5/5]

template<typename T, typename V>
void FixedLengthEncoder< T, V >::updateStats ( const std::vector< ArrayDatum > *const  src_data,
const size_t  start_idx,
const size_t  num_elements 
)
inlineoverridevirtual

Update statistics for array data without appending.

Parameters
src_data- the array data with which to update statistics
start_idx- the offset into src_data to start the update
num_elements- the number of elements to scan in the array data

Implements Encoder.

Definition at line 121 of file FixedLengthEncoder.h.

References UNREACHABLE.

123  {
124  UNREACHABLE();
125  }
#define UNREACHABLE()
Definition: Logger.h:241

◆ writeMetadata()

template<typename T, typename V>
void FixedLengthEncoder< T, V >::writeMetadata ( FILE *  f)
inlineoverridevirtual

Implements Encoder.

Definition at line 146 of file FixedLengthEncoder.h.

References FixedLengthEncoder< T, V >::dataMax, FixedLengthEncoder< T, V >::dataMin, FixedLengthEncoder< T, V >::has_nulls, and Encoder::num_elems_.

146  {
147  // assumes pointer is already in right place
148  fwrite((int8_t*)&num_elems_, sizeof(size_t), 1, f);
149  fwrite((int8_t*)&dataMin, sizeof(T), 1, f);
150  fwrite((int8_t*)&dataMax, sizeof(T), 1, f);
151  fwrite((int8_t*)&has_nulls, sizeof(bool), 1, f);
152  }
size_t num_elems_
Definition: Encoder.h:218

Member Data Documentation

◆ dataMax

◆ dataMin

◆ has_nulls


The documentation for this class was generated from the following file: