This repository was archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDateDaysEncoder.h
205 lines (178 loc) · 6.75 KB
/
DateDaysEncoder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright 2019 OmniSci, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DATE_DAYS_ENCODER_H
#define DATE_DAYS_ENCODER_H
#include "Logger/Logger.h"
#include <iostream>
#include <memory>
#include "AbstractBuffer.h"
#include "Encoder.h"
#include "Shared/InlineNullValues.h"
#include <IR/DatumFetchers.h>
#include <tbb/parallel_for.h>
#include <tbb/parallel_reduce.h>
template <typename T, typename V>
class DateDaysEncoder : public Encoder {
public:
DateDaysEncoder(Data_Namespace::AbstractBuffer* buffer) : Encoder(buffer) {
resetChunkStats();
}
std::shared_ptr<ChunkMetadata> getMetadata() override {
auto res = Encoder::getMetadata();
res->fillChunkStats(dataMin, dataMax, has_nulls);
return res;
}
// Only called from the executor for synthesized meta-information.
std::shared_ptr<ChunkMetadata> getMetadata(const hdk::ir::Type* type) override {
auto chunk_metadata = std::make_shared<ChunkMetadata>(type, 0, 0, ChunkStats{});
chunk_metadata->fillChunkStats(dataMin, dataMax, has_nulls);
return chunk_metadata;
}
// Only called from the executor for synthesized meta-information.
void updateStats(const int64_t val, const bool is_null) override {
if (is_null) {
has_nulls = true;
} else {
const auto data = static_cast<T>(val);
dataMin = std::min(dataMin, data);
dataMax = std::max(dataMax, data);
}
}
// Only called from the executor for synthesized meta-information.
void updateStats(const double val, const bool is_null) override {
if (is_null) {
has_nulls = true;
} else {
const auto data = static_cast<T>(val);
dataMin = std::min(dataMin, data);
dataMax = std::max(dataMax, data);
}
}
void updateStats(const int8_t* const src_data, const size_t num_elements) override {
const T* unencoded_data = reinterpret_cast<const T*>(src_data);
for (size_t i = 0; i < num_elements; ++i) {
encodeDataAndUpdateStats(unencoded_data[i]);
}
}
void updateStats(const std::vector<std::string>* const src_data,
const size_t start_idx,
const size_t num_elements) override {
UNREACHABLE();
}
void updateStats(const std::vector<ArrayDatum>* const src_data,
const size_t start_idx,
const size_t num_elements) override {
UNREACHABLE();
}
void updateStatsEncoded(const int8_t* const dst_data,
const size_t num_elements,
bool fixlen_array = false) override {
const V* data = reinterpret_cast<const V*>(dst_data);
std::tie(dataMin, dataMax, has_nulls) = tbb::parallel_reduce(
tbb::blocked_range(size_t(0), num_elements),
std::tuple(dataMin, dataMax, has_nulls),
[&](const auto& range, auto init) {
auto [min, max, nulls] = init;
for (size_t i = range.begin(); i < range.end(); i++) {
if (data[i] != inline_null_value<V>()) {
if (!fixlen_array || data[i] != inline_null_array_value<V>()) {
const T val = DateConverters::get_epoch_seconds_from_days(data[i]);
min = std::min(min, val);
max = std::max(max, val);
}
} else {
nulls = true;
}
}
return std::tuple(min, max, nulls);
},
[&](auto lhs, auto rhs) {
const auto [lhs_min, lhs_max, lhs_nulls] = lhs;
const auto [rhs_min, rhs_max, rhs_nulls] = rhs;
return std::tuple(std::min(lhs_min, rhs_min),
std::max(lhs_max, rhs_max),
lhs_nulls || rhs_nulls);
});
}
// Only called from the executor for synthesized meta-information.
void reduceStats(const Encoder& that) override {
const auto that_typed = static_cast<const DateDaysEncoder<T, V>&>(that);
if (that_typed.has_nulls) {
has_nulls = true;
}
dataMin = std::min(dataMin, that_typed.dataMin);
dataMax = std::max(dataMax, that_typed.dataMax);
}
void copyMetadata(const Encoder* copyFromEncoder) override {
num_elems_ = copyFromEncoder->getNumElems();
auto castedEncoder = reinterpret_cast<const DateDaysEncoder<T, V>*>(copyFromEncoder);
dataMin = castedEncoder->dataMin;
dataMax = castedEncoder->dataMax;
has_nulls = castedEncoder->has_nulls;
}
void writeMetadata(FILE* f) override {
// assumes pointer is already in right place
fwrite((int8_t*)&num_elems_, sizeof(size_t), 1, f);
fwrite((int8_t*)&dataMin, sizeof(T), 1, f);
fwrite((int8_t*)&dataMax, sizeof(T), 1, f);
fwrite((int8_t*)&has_nulls, sizeof(bool), 1, f);
}
void readMetadata(FILE* f) override {
// assumes pointer is already in right place
fread((int8_t*)&num_elems_, sizeof(size_t), 1, f);
fread((int8_t*)&dataMin, 1, sizeof(T), f);
fread((int8_t*)&dataMax, 1, sizeof(T), f);
fread((int8_t*)&has_nulls, 1, sizeof(bool), f);
}
bool resetChunkStats(const ChunkStats& stats) override {
const auto new_min = DatumFetcher::getDatumVal<T>(stats.min);
const auto new_max = DatumFetcher::getDatumVal<T>(stats.max);
if (dataMin == new_min && dataMax == new_max && has_nulls == stats.has_nulls) {
return false;
}
dataMin = new_min;
dataMax = new_max;
has_nulls = stats.has_nulls;
return true;
}
void resetChunkStats() override {
dataMin = std::numeric_limits<T>::max();
dataMax = std::numeric_limits<T>::lowest();
has_nulls = false;
}
void fillChunkStats(ChunkStats& stats, const hdk::ir::Type* type) override {
::fillChunkStats(stats, type, dataMin, dataMax, has_nulls);
}
T dataMin;
T dataMax;
bool has_nulls;
private:
V encodeDataAndUpdateStats(const T& unencoded_data) {
V encoded_data;
if (unencoded_data == std::numeric_limits<V>::min()) {
has_nulls = true;
encoded_data = static_cast<V>(unencoded_data);
} else {
date_days_overflow_validator_.validate(unencoded_data);
encoded_data = DateConverters::get_epoch_days_from_seconds(unencoded_data);
const T data = DateConverters::get_epoch_seconds_from_days(encoded_data);
dataMax = std::max(dataMax, data);
dataMin = std::min(dataMin, data);
}
return encoded_data;
}
}; // DateDaysEncoder
#endif // DATE_DAYS_ENCODER_H