aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/dom/yson.cpp
blob: f3ab30f22c7182bdb3a3b7e146840a1e5e549884 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "node.h"
#include "yson.h"

#include <library/cpp/containers/stack_vector/stack_vec.h>

#include <library/cpp/yson_pull/exceptions.h>
#include <library/cpp/yson_pull/reader.h>
#include <library/cpp/yson_pull/writer.h>

#include <util/string/builder.h>

namespace NYql::NDom {

using namespace NUdf;
using namespace NYsonPull;

namespace {

[[noreturn]] Y_NO_INLINE void UnexpectedEvent(EEventType ev) {
    UdfTerminate((::TStringBuilder() << "Unexpected event: " << ev).c_str());
}

TUnboxedValuePod ParseScalar(const TScalar& scalar, const IValueBuilder* valueBuilder) {
    switch (scalar.Type()) {
        case EScalarType::Entity:
            return MakeEntity();

        case EScalarType::Boolean:
            return MakeBool(scalar.AsBoolean());

        case EScalarType::Int64:
            return MakeInt64(scalar.AsInt64());

        case EScalarType::UInt64:
            return MakeUint64(scalar.AsUInt64());

        case EScalarType::Float64:
            return MakeDouble(scalar.AsFloat64());

        case EScalarType::String:
            return MakeString(scalar.AsString(), valueBuilder);
    }
}

TUnboxedValue ParseAttributes(TReader& reader, const IValueBuilder* valueBuilder);
TUnboxedValue ParseDict(TReader& reader, const IValueBuilder* valueBuilder);

TUnboxedValue ParseList(TReader& reader, const IValueBuilder* valueBuilder) {
    TSmallVec<TUnboxedValue, TStdAllocatorForUdf<TUnboxedValue>> items;
    for (;;) {
        const auto& ev = reader.NextEvent();
        switch (ev.Type()) {
            case EEventType::BeginList:
                items.emplace_back(ParseList(reader, valueBuilder));
                break;
            case EEventType::EndList:
                return MakeList(items.data(), items.size(), valueBuilder);
            case EEventType::BeginMap:
                items.emplace_back(ParseDict(reader, valueBuilder));
                break;
            case EEventType::BeginAttributes:
                items.emplace_back(ParseAttributes(reader, valueBuilder));
                break;
            case EEventType::Scalar:
                items.emplace_back(ParseScalar(ev.AsScalar(), valueBuilder));
                break;
            default:
                UnexpectedEvent(ev.Type());
        }
    }
}

TUnboxedValue ParseDict(TReader& reader, const IValueBuilder* valueBuilder) {
    TSmallVec<TPair, TStdAllocatorForUdf<TPair>> items;
    for (;;) {
        const auto& evKey = reader.NextEvent();
        if (evKey.Type() == EEventType::EndMap) {
            return MakeDict(items.data(), items.size());
        }

        Y_ASSERT(evKey.Type() == EEventType::Key);
        auto key = valueBuilder->NewString(evKey.AsString());
        const auto& ev = reader.NextEvent();
        switch (ev.Type()) {
            case EEventType::BeginList:
                items.emplace_back(std::make_pair(std::move(key), ParseList(reader, valueBuilder)));
                break;
            case EEventType::BeginMap:
                items.emplace_back(std::make_pair(std::move(key), ParseDict(reader, valueBuilder)));
                break;
            case EEventType::BeginAttributes:
                items.emplace_back(std::make_pair(std::move(key), ParseAttributes(reader, valueBuilder)));
                break;
            case EEventType::Scalar:
                items.emplace_back(std::make_pair(std::move(key), ParseScalar(ev.AsScalar(), valueBuilder)));
                break;
            default:
                UnexpectedEvent(ev.Type());
        }
    }
}

TUnboxedValue ParseValue(TReader& reader, const IValueBuilder* valueBuilder);

TUnboxedValue ParseAttributes(TReader& reader, const IValueBuilder* valueBuilder) {
    TSmallVec<TPair, TStdAllocatorForUdf<TPair>> items;
    for (;;) {
        const auto& evKey = reader.NextEvent();
        if (evKey.Type() == EEventType::EndAttributes) {
            break;
        }

        Y_ASSERT(evKey.Type() == EEventType::Key);
        auto key = valueBuilder->NewString(evKey.AsString());
        const auto& ev = reader.NextEvent();
        switch (ev.Type()) {
            case EEventType::BeginList:
                items.emplace_back(std::make_pair(std::move(key), ParseList(reader, valueBuilder)));
                break;
            case EEventType::BeginMap:
                items.emplace_back(std::make_pair(std::move(key), ParseDict(reader, valueBuilder)));
                break;
            case EEventType::BeginAttributes:
                items.emplace_back(std::make_pair(std::move(key), ParseAttributes(reader, valueBuilder)));
                break;
            case EEventType::Scalar:
                items.emplace_back(std::make_pair(std::move(key), ParseScalar(ev.AsScalar(), valueBuilder)));
                break;
            default:
                UnexpectedEvent(ev.Type());
        }
    }

    return MakeAttr(ParseValue(reader, valueBuilder), items.data(), items.size());
}

TUnboxedValue ParseValue(TReader& reader, const IValueBuilder* valueBuilder) {
    const auto& ev = reader.NextEvent();
    switch (ev.Type()) {
        case EEventType::BeginList:
            return ParseList(reader, valueBuilder);
        case EEventType::BeginMap:
            return ParseDict(reader, valueBuilder);
        case EEventType::BeginAttributes:
            return ParseAttributes(reader, valueBuilder);
        case EEventType::Scalar:
            return ParseScalar(ev.AsScalar(), valueBuilder);
        default:
            UnexpectedEvent(ev.Type());
    }
}

/////////////////////////////////////

bool CheckValue(TReader& reader);

bool CheckDict(TReader& reader) {
    for (;;) {
        const auto& evKey = reader.NextEvent();
        if (evKey.Type() == EEventType::EndMap)
            return true;

        if (evKey.Type() != EEventType::Key)
            return false;

        if (CheckValue(reader))
            continue;
        else
            return false;
    }
}

bool CheckAttributes(TReader& reader) {
    for (;;) {
        const auto& evKey = reader.NextEvent();
        if (evKey.Type() == EEventType::EndAttributes)
            break;

        if (evKey.Type() != EEventType::Key)
            return false;

        if (CheckValue(reader))
            continue;
        else
            return false;
    }

    return CheckValue(reader);
}

bool CheckList(TReader& reader) {
    for (;;) {
        const auto& ev = reader.NextEvent();
        switch (ev.Type()) {
            case EEventType::BeginList:
                if (CheckList(reader))
                    break;
                else
                    return false;
            case EEventType::BeginMap:
                if (CheckDict(reader))
                    break;
                else
                    return false;
            case EEventType::BeginAttributes:
                if (CheckAttributes(reader))
                    break;
                else
                    return false;
            case EEventType::Scalar:
                break;
            case EEventType::EndList:
                return true;
            default:
                return false;
        }
    }
}

bool CheckValue(TReader& reader) {
    const auto& ev = reader.NextEvent();
    switch (ev.Type()) {
        case EEventType::BeginList:
            if (CheckList(reader))
                break;
            else
                return false;
        case EEventType::BeginMap:
            if (CheckDict(reader))
                break;
            else
                return false;
        case EEventType::BeginAttributes:
            if (CheckAttributes(reader))
                break;
            else
                return false;
        case EEventType::Scalar:
            break;
        default:
            return false;
    }
    return true;
}

void WriteValue(TWriter& writer, const TUnboxedValue& x) {
    switch (GetNodeType(x)) {
        case ENodeType::String:
            writer.String(x.AsStringRef());
            break;
        case ENodeType::Bool:
            writer.Boolean(x.Get<bool>());
            break;
        case ENodeType::Int64:
            writer.Int64(x.Get<i64>());
            break;
        case ENodeType::Uint64:
            writer.UInt64(x.Get<ui64>());
            break;
        case ENodeType::Double:
            writer.Float64(x.Get<double>());
            break;
        case ENodeType::Entity:
            writer.Entity();
            break;
        case ENodeType::List:
            writer.BeginList();
            if (x.IsBoxed()) {
                if (const auto elements = x.GetElements()) {
                    const auto size = x.GetListLength();
                    for (ui64 i = 0; i < size; ++i) {
                        WriteValue(writer, elements[i]);
                    }
                } else {
                    const auto it = x.GetListIterator();
                    for (TUnboxedValue v; it.Next(v); WriteValue(writer, v))
                        continue;
                }
            }
            writer.EndList();
            break;
        case ENodeType::Dict:
            writer.BeginMap();
            if (x.IsBoxed()) {
                TUnboxedValue key, payload;
                for (const auto it = x.GetDictIterator(); it.NextPair(key, payload);) {
                    writer.Key(key.AsStringRef());
                    WriteValue(writer, payload);
                }
            }
            writer.EndMap();
            break;
        case ENodeType::Attr: {
            writer.BeginAttributes();
            TUnboxedValue key, payload;
            for (const auto it = x.GetDictIterator(); it.NextPair(key, payload);) {
                writer.Key(key.AsStringRef());
                WriteValue(writer, payload);
            }

            writer.EndAttributes();
            WriteValue(writer, x.GetVariantItem());
        }
        break;
    }
}

void SerializeYsonDomImpl(const NUdf::TUnboxedValue& dom, TWriter& writer) {
    writer.BeginStream();
    WriteValue(writer, dom);
    writer.EndStream();
}

}

NUdf::TUnboxedValue TryParseYsonDom(const TStringBuf yson, const NUdf::IValueBuilder* valueBuilder) {
    auto reader = TReader(NInput::FromMemory(yson), EStreamType::Node);
    const auto& begin = reader.NextEvent();
    Y_ASSERT(begin.Type() == EEventType::BeginStream);
    auto value = ParseValue(reader, valueBuilder);
    const auto& end = reader.NextEvent();
    Y_ASSERT(end.Type() == EEventType::EndStream);
    return value;
}

bool IsValidYson(const TStringBuf yson) try {
    auto reader = TReader(NInput::FromMemory(yson), EStreamType::Node);
    const auto& begin = reader.NextEvent();
    if (begin.Type() != EEventType::BeginStream)
        return false;
    if (!CheckValue(reader))
        return false;
    const auto& end = reader.NextEvent();
    return end.Type() == EEventType::EndStream;
} catch (const NException::TBadStream&) {
    return false;
}

TString SerializeYsonDomToBinary(const NUdf::TUnboxedValue& dom) {
    TString result;
    TWriter writer = MakeBinaryWriter(NOutput::FromString(&result), EStreamType::Node);
    SerializeYsonDomImpl(dom, writer);
    return result;
}

TString SerializeYsonDomToText(const NUdf::TUnboxedValue& dom) {
    TString result;
    TWriter writer = MakeTextWriter(NOutput::FromString(&result), EStreamType::Node);
    SerializeYsonDomImpl(dom, writer);
    return result;
}

TString SerializeYsonDomToPrettyText(const NUdf::TUnboxedValue& dom) {
    TString result;
    TWriter writer = MakePrettyTextWriter(NOutput::FromString(&result), EStreamType::Node);
    SerializeYsonDomImpl(dom, writer);
    return result;
}

}