aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/computation/mkql_pack_bc.cpp
blob: 1998d68f74e8a730de8ee6a30bf7d1fda6b042a5 (plain) (blame)
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
#include <util/generic/buffer.h>

#include <yql/essentials/public/udf/udf_value.h>
#include <yql/essentials/minikql/pack_num.h>
#include <library/cpp/packedtypes/zigzag.h>

#include <yql/essentials/minikql/defs.h>
#include "mkql_optional_usage_mask.h"

using namespace NKikimr;

namespace NDetails {

void PackUInt64(ui64 pos, ui64 val, TBuffer& buf) {
    const auto actual = Pack64(val, buf.Data() + pos);
    buf.Chop(pos + actual, MAX_PACKED64_SIZE - actual);
}

void PackUInt64(ui64 val, TBuffer& buf) {
    size_t off = buf.Size();
    buf.Advance(MAX_PACKED64_SIZE);
    buf.EraseBack(MAX_PACKED64_SIZE - Pack64(val, buf.Data() + off));
}

void PackInt64(i64 val, TBuffer& buf) {
    PackUInt64(ZigZagEncode(val), buf);
}

void PackUInt32(ui32 val, TBuffer& buf) {
    size_t off = buf.Size();
    buf.Advance(MAX_PACKED32_SIZE);
    buf.EraseBack(MAX_PACKED32_SIZE - Pack32(val, buf.Data() + off));
}

void PackInt32(i32 val, TBuffer& buf) {
    PackUInt32(ZigZagEncode(val), buf);
}

ui64 UnpackUInt64(TStringBuf& buf) {
    ui64 res = 0;
    size_t read = Unpack64(buf.data(), buf.length(), res);
    MKQL_ENSURE(read, "Bad ui64 packed data");
    buf.Skip(read);
    return res;
}

i64 UnpackInt64(TStringBuf& buf) {
    return ZigZagDecode(UnpackUInt64(buf));
}

ui32 UnpackUInt32(TStringBuf& buf) {
    ui32 res = 0;
    size_t read = Unpack32(buf.data(), buf.length(), res);
    MKQL_ENSURE(read, "Bad ui32 packed data");
    buf.Skip(read);
    return res;
}

i32 UnpackInt32(TStringBuf& buf) {
    return ZigZagDecode(UnpackUInt32(buf));
}

template <typename T>
void PutRawData(T val, TBuffer& buf) {
    buf.Append(reinterpret_cast<const char*>(&val), sizeof(T));
}

template <typename T>
T GetRawData(TStringBuf& buf) {
    MKQL_ENSURE(sizeof(T) <= buf.size(), "Bad packed data. Buffer too small");
    T val = 0;
    std::memcpy(&val, buf.data(), sizeof(T));
    buf.Skip(sizeof(T));
    return val;
}

}

extern "C" void GetElement(const TRawUV* value, ui32 index, TRawUV* item) {
    const auto result(reinterpret_cast<const NUdf::TUnboxedValuePod*>(value)->GetElement(index));
    *item = reinterpret_cast<const TRawUV&>(result);
}

extern "C" bool FetchNextItem(const TRawUV* value, TRawUV* item) {
    NUdf::TUnboxedValue fetch;
    if (NUdf::EFetchStatus::Finish == reinterpret_cast<const NUdf::TUnboxedValuePod*>(value)->Fetch(fetch))
        return false;
    *item = reinterpret_cast<TRawUV&>(fetch);
    return true;
}

extern "C" bool NextListItem(TRawUV* value, TRawUV* item) {
    NUdf::TUnboxedValue next;
    const auto v = reinterpret_cast<NUdf::TUnboxedValuePod*>(value);
    if (!v->Next(next)) {
        v->DeleteUnreferenced();
        return false;
    }

    *item = reinterpret_cast<TRawUV&>(next);
    return true;
}

extern "C" bool NextDictItem(TRawUV* value, TRawUV* first, TRawUV* second) {
    NUdf::TUnboxedValue key, payload;
    const auto v = reinterpret_cast<NUdf::TUnboxedValuePod*>(value);
    if (!v->NextPair(key, payload)) {
        v->DeleteUnreferenced();
        return false;
    }

    *first = reinterpret_cast<TRawUV&>(key);
    *second = reinterpret_cast<TRawUV&>(payload);
    return true;
}

extern "C" void PackString(const TRawUV* value, ui64* buffer) {
    const auto& stringRef = reinterpret_cast<const NUdf::TUnboxedValue*>(value)->AsStringRef();
    NDetails::PackUInt32(stringRef.Size(), *reinterpret_cast<TBuffer*>(buffer));
    reinterpret_cast<TBuffer*>(buffer)->Append(stringRef.Data(), stringRef.Size());
}

extern "C" void PackStringData(const TRawUV* value, ui64* buffer) {
    const auto& stringRef = reinterpret_cast<const NUdf::TUnboxedValue*>(value)->AsStringRef();
    reinterpret_cast<TBuffer*>(buffer)->Append(stringRef.Data(), stringRef.Size());
}

extern "C" void PackBool(const TRawUV* value, ui64* buffer) {
    NDetails::PutRawData(reinterpret_cast<const NUdf::TUnboxedValue*>(value)->Get<bool>(), *reinterpret_cast<TBuffer*>(buffer));
}

extern "C" void PackByte(const TRawUV* value, ui64* buffer) {
    NDetails::PutRawData(reinterpret_cast<const NUdf::TUnboxedValue*>(value)->Get<ui8>(), *reinterpret_cast<TBuffer*>(buffer));
}

extern "C" void PackInt32(const TRawUV* value, ui64* buffer) {
    NDetails::PackInt32(reinterpret_cast<const NUdf::TUnboxedValue*>(value)->Get<i32>(), *reinterpret_cast<TBuffer*>(buffer));
}

extern "C" void PackUInt32(const TRawUV* value, ui64* buffer) {
    NDetails::PackUInt32(reinterpret_cast<const NUdf::TUnboxedValue*>(value)->Get<ui32>(), *reinterpret_cast<TBuffer*>(buffer));
}

extern "C" void PackInt64(const TRawUV* value, ui64* buffer) {
    NDetails::PackInt64(reinterpret_cast<const NUdf::TUnboxedValue*>(value)->Get<i64>(), *reinterpret_cast<TBuffer*>(buffer));
}

extern "C" void PackUInt64(const TRawUV* value, ui64* buffer) {
    NDetails::PackUInt64(reinterpret_cast<const NUdf::TUnboxedValue*>(value)->Get<ui64>(), *reinterpret_cast<TBuffer*>(buffer));
}

extern "C" void PackFloat(const TRawUV* value, ui64* buffer) {
    NDetails::PutRawData(reinterpret_cast<const NUdf::TUnboxedValue*>(value)->Get<float>(), *reinterpret_cast<TBuffer*>(buffer));
}

extern "C" void PackDouble(const TRawUV* value, ui64* buffer) {
    NDetails::PutRawData(reinterpret_cast<const NUdf::TUnboxedValue*>(value)->Get<double>(), *reinterpret_cast<TBuffer*>(buffer));
}

extern "C" ui32 GetVariantItem(const TRawUV* value, TRawUV* item, ui64* buffer) {
    const auto v = reinterpret_cast<const NUdf::TUnboxedValuePod*>(value);
    const ui32 index = v->GetVariantIndex();
    NDetails::PackUInt32(index, *reinterpret_cast<TBuffer*>(buffer));
    const auto result(v->GetVariantItem());
    *item = reinterpret_cast<const TRawUV&>(result);
    return index;
}

extern "C" bool GetListIterator(const TRawUV* value, TRawUV* item, ui64* buffer) {
    const auto v = reinterpret_cast<const NUdf::TUnboxedValuePod*>(value);
    const auto size = v->GetListLength();
    NDetails::PackUInt64(size, *reinterpret_cast<TBuffer*>(buffer));
    if (!size)
        return false;
    const auto result(v->GetListIterator().Release());
    *item = reinterpret_cast<const TRawUV&>(result);
    return true;
}

extern "C" bool GetDictIterator(const TRawUV* value, TRawUV* item, ui64* buffer) {
    const auto v = reinterpret_cast<const NUdf::TUnboxedValuePod*>(value);
    const auto size = v->GetDictLength();
    NDetails::PackUInt64(size, *reinterpret_cast<TBuffer*>(buffer));
    if (!size)
        return false;
    const auto result(v->GetDictIterator().Release());
    *item = reinterpret_cast<const TRawUV&>(result);
    return true;
}

extern "C" bool GetOptionalValue(const TRawUV* value, TRawUV* item, ui64* mask) {
    const auto v = reinterpret_cast<const NUdf::TUnboxedValuePod*>(value);
    const bool has = bool(*v);
    reinterpret_cast<NMiniKQL::NDetails::TOptionalUsageMask*>(mask)->SetNextEmptyOptional(!*v);
    if (has) {
        const auto result(v->GetOptionalValue());
        *item = reinterpret_cast<const TRawUV&>(result);
    }
    return has;
}