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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
#pragma once
#include <yql/essentials/minikql/defs.h>
#include <yql/essentials/minikql/mkql_node.h>
#include <yql/essentials/minikql/pack_num.h>
#include <yql/essentials/public/decimal/yql_decimal_serialize.h>
#include <yql/essentials/public/udf/udf_value.h>
#include <yql/essentials/utils/chunked_buffer.h>
#include <library/cpp/packedtypes/zigzag.h>
#include <util/generic/buffer.h>
#include <util/generic/strbuf.h>
namespace NKikimr {
namespace NMiniKQL {
namespace NDetails {
template<typename TBuf>
inline void PackUInt64(ui64 val, TBuf& buf) {
buf.Advance(MAX_PACKED64_SIZE);
char* dst = buf.Pos() - MAX_PACKED64_SIZE;
buf.EraseBack(MAX_PACKED64_SIZE - Pack64(val, dst));
}
template<typename TBuf>
inline void PackInt64(i64 val, TBuf& buf) {
PackUInt64(ZigZagEncode(val), buf);
}
template<typename TBuf>
inline void PackUInt32(ui32 val, TBuf& buf) {
buf.Advance(MAX_PACKED32_SIZE);
char* dst = buf.Pos() - MAX_PACKED32_SIZE;
buf.EraseBack(MAX_PACKED32_SIZE - Pack32(val, dst));
}
template<typename TBuf>
inline void PackInt32(i32 val, TBuf& buf) {
PackUInt32(ZigZagEncode(val), buf);
}
template<typename TBuf>
inline void PackUInt16(ui16 val, TBuf& buf) {
buf.Advance(MAX_PACKED32_SIZE);
char* dst = buf.Pos() - MAX_PACKED32_SIZE;
buf.EraseBack(MAX_PACKED32_SIZE - Pack32(val, dst));
}
template<typename TBuf>
inline void PackInt16(i16 val, TBuf& buf) {
PackUInt16(ZigZagEncode(val), buf);
}
template <typename T, typename TBuf>
void PutRawData(T val, TBuf& buf) {
buf.Advance(sizeof(T));
std::memcpy(buf.Pos() - sizeof(T), &val, sizeof(T));
}
constexpr size_t MAX_PACKED_DECIMAL_SIZE = sizeof(NYql::NDecimal::TInt128);
template<typename TBuf>
void PackDecimal(NYql::NDecimal::TInt128 val, TBuf& buf) {
buf.Advance(MAX_PACKED_DECIMAL_SIZE);
char* dst = buf.Pos() - MAX_PACKED_DECIMAL_SIZE;
buf.EraseBack(MAX_PACKED_DECIMAL_SIZE - NYql::NDecimal::Serialize(val, dst));
}
class TChunkedInputBuffer : private TNonCopyable {
public:
explicit TChunkedInputBuffer(NYql::TChunkedBuffer&& rope)
: Rope_(std::move(rope))
{
Next();
}
explicit TChunkedInputBuffer(TStringBuf input)
: Rope_(NYql::TChunkedBuffer{})
, Data_(input.data())
, Len_(input.size())
{
}
inline const char* data() const {
return Data_;
}
inline size_t length() const {
return Len_;
}
inline size_t size() const {
return Len_;
}
inline void Skip(size_t size) {
Y_DEBUG_ABORT_UNLESS(size <= Len_);
Data_ += size;
Len_ -= size;
}
bool IsEmpty() {
if (size()) {
return false;
}
Next();
return size() == 0;
}
inline void CopyTo(char* dst, size_t toCopy) {
if (Y_LIKELY(toCopy <= size())) {
std::memcpy(dst, data(), toCopy);
Skip(toCopy);
} else {
CopyToChunked(dst, toCopy);
}
}
inline NYql::TChunkedBuffer ReleaseRope() {
Y_DEBUG_ABORT_UNLESS(OriginalLen_ >= Len_);
Rope_.Erase(OriginalLen_ - Len_);
NYql::TChunkedBuffer result = std::move(Rope_);
Data_ = nullptr;
Len_ = OriginalLen_ = 0;
Rope_.Clear();
return result;
}
void Next() {
Y_DEBUG_ABORT_UNLESS(Len_ == 0);
Rope_.Erase(OriginalLen_);
if (!Rope_.Empty()) {
Len_ = OriginalLen_ = Rope_.Front().Buf.size();
Data_ = Rope_.Front().Buf.data();
Y_DEBUG_ABORT_UNLESS(Len_ > 0);
} else {
Len_ = OriginalLen_ = 0;
Data_ = nullptr;
}
}
private:
void CopyToChunked(char* dst, size_t toCopy) {
while (toCopy) {
size_t chunkSize = std::min(size(), toCopy);
std::memcpy(dst, data(), chunkSize);
Skip(chunkSize);
dst += chunkSize;
toCopy -= chunkSize;
if (toCopy) {
Next();
MKQL_ENSURE(size(), "Unexpected end of buffer");
}
}
}
NYql::TChunkedBuffer Rope_;
const char* Data_ = nullptr;
size_t Len_ = 0;
size_t OriginalLen_ = 0;
};
template <typename T>
T GetRawData(TChunkedInputBuffer& buf) {
T val;
buf.CopyTo(reinterpret_cast<char*>(&val), sizeof(val));
return val;
}
template<typename T>
T UnpackInteger(TChunkedInputBuffer& buf) {
T res;
size_t read;
if constexpr (std::is_same_v<T, NYql::NDecimal::TInt128>) {
std::tie(res, read) = NYql::NDecimal::Deserialize(buf.data(), buf.size());
Y_DEBUG_ABORT_UNLESS((read != 0) xor (NYql::NDecimal::IsError(res)));
} else if constexpr (std::is_same_v<T, ui64>) {
read = Unpack64(buf.data(), buf.size(), res);
} else {
static_assert(std::is_same_v<T, ui32>, "Only ui32/ui64/TInt128 are supported");
read = Unpack32(buf.data(), buf.size(), res);
}
if (Y_LIKELY(read > 0)) {
buf.Skip(read);
return res;
}
static_assert(MAX_PACKED_DECIMAL_SIZE > MAX_PACKED64_SIZE);
char tmpBuf[MAX_PACKED_DECIMAL_SIZE];
Y_DEBUG_ABORT_UNLESS(buf.size() < MAX_PACKED_DECIMAL_SIZE);
std::memcpy(tmpBuf, buf.data(), buf.size());
size_t pos = buf.size();
buf.Skip(buf.size());
for (;;) {
if (buf.size() == 0) {
buf.Next();
MKQL_ENSURE(buf.size() > 0, (std::is_same_v<T, NYql::NDecimal::TInt128> ? "Bad decimal packed data" : "Bad uint packed data"));
}
Y_DEBUG_ABORT_UNLESS(pos < MAX_PACKED_DECIMAL_SIZE);
tmpBuf[pos++] = *buf.data();
buf.Skip(1);
if constexpr (std::is_same_v<T, NYql::NDecimal::TInt128>) {
std::tie(res, read) = NYql::NDecimal::Deserialize(tmpBuf, pos);
Y_DEBUG_ABORT_UNLESS((read != 0) xor (NYql::NDecimal::IsError(res)));
} else if constexpr (std::is_same_v<T, ui64>) {
read = Unpack64(tmpBuf, pos, res);
} else {
read = Unpack32(tmpBuf, pos, res);
}
if (read) {
break;
}
}
return res;
}
inline NYql::NDecimal::TInt128 UnpackDecimal(TChunkedInputBuffer& buf) {
return UnpackInteger<NYql::NDecimal::TInt128>(buf);
}
inline ui64 UnpackUInt64(TChunkedInputBuffer& buf) {
return UnpackInteger<ui64>(buf);
}
inline i64 UnpackInt64(TChunkedInputBuffer& buf) {
return ZigZagDecode(UnpackUInt64(buf));
}
inline ui32 UnpackUInt32(TChunkedInputBuffer& buf) {
return UnpackInteger<ui32>(buf);
}
inline i32 UnpackInt32(TChunkedInputBuffer& buf) {
return ZigZagDecode(UnpackUInt32(buf));
}
inline ui16 UnpackUInt16(TChunkedInputBuffer& buf) {
ui32 res = UnpackUInt32(buf);
MKQL_ENSURE(res <= Max<ui16>(), "Corrupted data");
return res;
}
inline i16 UnpackInt16(TChunkedInputBuffer& buf) {
return ZigZagDecode(UnpackUInt16(buf));
}
} // NDetails
}
}
|