aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/comp_nodes/mkql_frombytes.cpp
blob: 175be5bcdb6d1b096207a307d759ef115c01ce85 (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
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
#include "mkql_frombytes.h"
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/minikql/mkql_node_builder.h>
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/public/udf/tz/udf_tz.h>

#include <yql/essentials/utils/swap_bytes.h>

#include <yql/essentials/types/binary_json/read.h>

#include <util/system/unaligned_mem.h>

namespace NKikimr {
namespace NMiniKQL {

namespace {

using NYql::SwapBytes;

template <bool IsOptional>
class TFromBytesWrapper : public TMutableComputationNode<TFromBytesWrapper<IsOptional>> {
    typedef TMutableComputationNode<TFromBytesWrapper<IsOptional>> TBaseComputation;
public:
    TFromBytesWrapper(TComputationMutables& mutables, IComputationNode* data, NUdf::TDataTypeId schemeType, ui32 param1, ui32 param2)
        : TBaseComputation(mutables)
        , Data(data)
        , SchemeType(NUdf::GetDataSlot(schemeType))
        , Param1(param1)
        , Param2(param2)
    {
        if (SchemeType == NUdf::EDataSlot::Decimal) {
            DecimalBound = NYql::NDecimal::TInt128(1);
            NYql::NDecimal::TInt128 ten(10U);
            for (ui32 i = 0; i < Param1; ++i) {
                DecimalBound *= ten;
            }

            NegDecimalBound = -DecimalBound;
        }
    }

    NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
        auto data = Data->GetValue(ctx);
        if (IsOptional && !data) {
            return NUdf::TUnboxedValuePod();
        }

        switch (SchemeType) {
        case NUdf::EDataSlot::TzDate: {
            const auto& ref = data.AsStringRef();
            if (ref.Size() != 4) {
                return NUdf::TUnboxedValuePod();
            }

            auto tzId = SwapBytes(ReadUnaligned<ui16>(ref.Data() + ref.Size() - sizeof(ui16)));
            auto value = SwapBytes(data.Get<ui16>());
            if (value < NUdf::MAX_DATE && tzId < NUdf::GetTimezones().size()) {
                auto ret = NUdf::TUnboxedValuePod(value);
                ret.SetTimezoneId(tzId);
                return ret;
            }

            return NUdf::TUnboxedValuePod();
        }

        case NUdf::EDataSlot::TzDatetime: {
            const auto& ref = data.AsStringRef();
            if (ref.Size() != 6) {
                return NUdf::TUnboxedValuePod();
            }

            auto tzId = SwapBytes(ReadUnaligned<ui16>(ref.Data() + ref.Size() - sizeof(ui16)));
            auto value = SwapBytes(data.Get<ui32>());
            if (value < NUdf::MAX_DATETIME && tzId < NUdf::GetTimezones().size()) {
                auto ret = NUdf::TUnboxedValuePod(value);
                ret.SetTimezoneId(tzId);
                return ret;
            }

            return NUdf::TUnboxedValuePod();
        }

        case NUdf::EDataSlot::TzTimestamp: {
            const auto& ref = data.AsStringRef();
            if (ref.Size() != 10) {
                return NUdf::TUnboxedValuePod();
            }

            auto tzId = SwapBytes(ReadUnaligned<ui16>(ref.Data() + ref.Size() - sizeof(ui16)));
            auto value = SwapBytes(data.Get<ui64>());
            if (value < NUdf::MAX_TIMESTAMP && tzId < NUdf::GetTimezones().size()) {
                auto ret = NUdf::TUnboxedValuePod(value);
                ret.SetTimezoneId(tzId);
                return ret;
            }

            return NUdf::TUnboxedValuePod();
        }

        case NUdf::EDataSlot::TzDate32: {
            const auto& ref = data.AsStringRef();
            if (ref.Size() != 6) {
                return NUdf::TUnboxedValuePod();
            }

            auto tzId = SwapBytes(ReadUnaligned<ui16>(ref.Data() + ref.Size() - sizeof(ui16)));
            auto value = SwapBytes(data.Get<i32>());
            if (value >= NUdf::MIN_DATE32 && value <= NUdf::MAX_DATE32 && tzId < NUdf::GetTimezones().size()) {
                auto ret = NUdf::TUnboxedValuePod(value);
                ret.SetTimezoneId(tzId);
                return ret;
            }

            return NUdf::TUnboxedValuePod();
        }

        case NUdf::EDataSlot::TzDatetime64: {
            const auto& ref = data.AsStringRef();
            if (ref.Size() != 10) {
                return NUdf::TUnboxedValuePod();
            }

            auto tzId = SwapBytes(ReadUnaligned<ui16>(ref.Data() + ref.Size() - sizeof(ui16)));
            auto value = SwapBytes(data.Get<i64>());
            if (value >= NUdf::MIN_DATETIME64 && value <= NUdf::MAX_DATETIME64 && tzId < NUdf::GetTimezones().size()) {
                auto ret = NUdf::TUnboxedValuePod(value);
                ret.SetTimezoneId(tzId);
                return ret;
            }

            return NUdf::TUnboxedValuePod();
        }

        case NUdf::EDataSlot::TzTimestamp64: {
            const auto& ref = data.AsStringRef();
            if (ref.Size() != 10) {
                return NUdf::TUnboxedValuePod();
            }

            auto tzId = SwapBytes(ReadUnaligned<ui16>(ref.Data() + ref.Size() - sizeof(ui16)));
            auto value = SwapBytes(data.Get<i64>());
            if (value >= NUdf::MIN_TIMESTAMP64 && value <= NUdf::MAX_TIMESTAMP64 && tzId < NUdf::GetTimezones().size()) {
                auto ret = NUdf::TUnboxedValuePod(value);
                ret.SetTimezoneId(tzId);
                return ret;
            }

            return NUdf::TUnboxedValuePod();
        }

        case NUdf::EDataSlot::JsonDocument: {
            if (!NBinaryJson::IsValidBinaryJson(TStringBuf(data.AsStringRef()))) {
                return NUdf::TUnboxedValuePod();
            }
            return data.Release();
        }

        case NUdf::EDataSlot::Decimal: {
            const auto& ref = data.AsStringRef();
            if (ref.Size() != 15) {
                return NUdf::TUnboxedValuePod();
            }

            NYql::NDecimal::TInt128 v = 0;
            ui8* p = (ui8*)&v;
            memcpy(p, ref.Data(), 15);
            p[0xF] = (p[0xE] & 0x80) ? 0xFF : 0x00;
            if (NYql::NDecimal::IsError(v)) {
                return NUdf::TUnboxedValuePod();
            }

            if (!NYql::NDecimal::IsNormal(v)) {
                return NUdf::TUnboxedValuePod(v);
            }

            if (v >= DecimalBound) {
                return NUdf::TUnboxedValuePod(NYql::NDecimal::Inf());
            }

            if (v <= NegDecimalBound) {
                return NUdf::TUnboxedValuePod(-NYql::NDecimal::Inf());
            }

            return NUdf::TUnboxedValuePod(v);
        }

        default:
            if (IsValidValue(SchemeType, data)) {
                return data.Release();
            } else {
                return NUdf::TUnboxedValuePod();
            }
        }
    }

private:
    void RegisterDependencies() const final {
        this->DependsOn(Data);
    }

    IComputationNode* const Data;
    const NUdf::EDataSlot SchemeType;
    const ui32 Param1;
    const ui32 Param2;
    NYql::NDecimal::TInt128 DecimalBound, NegDecimalBound;
};

}

IComputationNode* WrapFromBytes(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
    MKQL_ENSURE(callable.GetInputsCount() == 2 || callable.GetInputsCount() == 4, "Expected 2 or 4 args");

    bool isOptional;
    const auto dataType = UnpackOptionalData(callable.GetInput(0), isOptional);
    MKQL_ENSURE(dataType->GetSchemeType() == NUdf::TDataType<char*>::Id, "Expected String");

    const auto schemeTypeData = AS_VALUE(TDataLiteral, callable.GetInput(1));
    const auto schemeType = schemeTypeData->AsValue().Get<ui32>();
    ui32 param1 = 0;
    ui32 param2 = 0;
    if (schemeType == NUdf::TDataType<NUdf::TDecimal>::Id) {
        MKQL_ENSURE(callable.GetInputsCount() == 4, "Expected 4 args");
        const auto param1Data = AS_VALUE(TDataLiteral, callable.GetInput(2));
        param1 = param1Data->AsValue().Get<ui32>();
        const auto param2Data = AS_VALUE(TDataLiteral, callable.GetInput(3));
        param2 = param2Data->AsValue().Get<ui32>();
    } else {
        MKQL_ENSURE(callable.GetInputsCount() == 2, "Expected 2 args");
    }

    const auto data = LocateNode(ctx.NodeLocator, callable, 0);
    if (isOptional) {
        return new TFromBytesWrapper<true>(ctx.Mutables, data, static_cast<NUdf::TDataTypeId>(schemeType), param1, param2);
    } else {
        return new TFromBytesWrapper<false>(ctx.Mutables, data, static_cast<NUdf::TDataTypeId>(schemeType), param1, param2);
    }
}

}
}