aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/protobuf_udf/value_builder.cpp
blob: 3696308f2324c3c27bd7b6bd9cdbef1d3b12261a (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
#include "type_builder.h"
#include "value_builder.h"

#include <yql/essentials/public/udf/udf_value.h>
#include <yql/essentials/public/udf/udf_value_builder.h>
#include <yql/essentials/public/udf/udf_terminator.h>
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/minikql/mkql_node.h>

namespace NYql {
namespace NUdf {

using namespace NProtoBuf;

TProtobufValue::TProtobufValue(const TProtoInfo& info)
    : Info_(info)
{
}

TProtobufValue::~TProtobufValue()
{ }

TUnboxedValue TProtobufValue::Run(
        const IValueBuilder* valueBuilder,
        const TUnboxedValuePod* args) const
{
    auto blob = args[0].AsStringRef();

    try {
        auto result = this->Parse(TStringBuf(blob.Data(), blob.Size()));
        if (result == nullptr) {
            return TUnboxedValue();
        }
        auto proto(result);
        return FillValueFromProto(*proto.Get(), valueBuilder, Info_);
    } catch (const std::exception& e) {
        UdfTerminate(e.what());
    }
}

TProtobufSerialize::TProtobufSerialize(const TProtoInfo& info)
    : Info_(info)
{
}

TProtobufSerialize::~TProtobufSerialize()
{ }

TUnboxedValue TProtobufSerialize::Run(
        const IValueBuilder* valueBuilder,
        const TUnboxedValuePod* args) const
{
    try {
        TAutoPtr<Message> proto = MakeProto();
        FillProtoFromValue(args[0], *proto, Info_);
        TMaybe<TString> result = this->Serialize(*proto);
        if (!result) {
            return TUnboxedValue();
        }
        return valueBuilder->NewString(*result);
    } catch (const std::exception& e) {
        UdfTerminate(e.what());
    }
}

namespace {

static TUnboxedValuePod CreateEnumValue(
        const IValueBuilder* valueBuilder,
        const NProtoBuf::EnumValueDescriptor* desc,
        const EEnumFormat format,
        TFlags<EFieldFlag> fieldFlags)
{
    if (fieldFlags.HasFlags(EFieldFlag::EnumInt)) {
        return TUnboxedValuePod((i64)desc->number());
    } else if (fieldFlags.HasFlags(EFieldFlag::EnumString)) {
        return valueBuilder->NewString(desc->name()).Release();
    }
    switch (format) {
    case EEnumFormat::Number:
        return TUnboxedValuePod((i32)desc->number());
    case EEnumFormat::Name:
        return valueBuilder->NewString(desc->name()).Release();
    case EEnumFormat::FullName:
        return valueBuilder->NewString(desc->full_name()).Release();
    }

    Y_UNREACHABLE();
}

static TUnboxedValuePod CreateSingleField(
    const IValueBuilder* valueBuilder,
    const Message& proto,
    const FieldDescriptor* fd,
    const TProtoInfo& info,
    TFlags<EFieldFlag> fieldFlags)
{
    auto r = proto.GetReflection();

#define FIELD_TO_VALUE(EProtoCppType, ProtoGet) \
case FieldDescriptor::EProtoCppType: { \
    return TUnboxedValuePod(r->ProtoGet(proto, fd)); \
}

    switch (fd->cpp_type()) {
        FIELD_TO_VALUE(CPPTYPE_INT32,  GetInt32);
        FIELD_TO_VALUE(CPPTYPE_INT64,  GetInt64);
        FIELD_TO_VALUE(CPPTYPE_UINT32, GetUInt32);
        FIELD_TO_VALUE(CPPTYPE_UINT64, GetUInt64);
        FIELD_TO_VALUE(CPPTYPE_DOUBLE, GetDouble);
        FIELD_TO_VALUE(CPPTYPE_BOOL,   GetBool);

        case FieldDescriptor::CPPTYPE_FLOAT: {
            const auto f = r->GetFloat(proto, fd);
            return info.YtMode ? TUnboxedValuePod(double(f)) : TUnboxedValuePod(f);
        }
        case FieldDescriptor::CPPTYPE_ENUM: {
            return CreateEnumValue(valueBuilder, r->GetEnum(proto, fd), info.EnumFormat, fieldFlags);
        }
        case FieldDescriptor::CPPTYPE_STRING: {
            return valueBuilder->NewString(r->GetString(proto, fd)).Release();
        }
        case FieldDescriptor::CPPTYPE_MESSAGE: {
            const auto& protoField = r->GetMessage(proto, fd);
            if (fieldFlags.HasFlags(EFieldFlag::Binary)) {
                return valueBuilder->NewString(protoField.SerializeAsString()).Release();
            } else {
                auto msg = FillValueFromProto(protoField, valueBuilder, info);
                return fd->is_optional() ? msg.Release().MakeOptional() : msg.Release();
            }
        }
    }
#undef FIELD_TO_VALUE

    return TUnboxedValuePod();
}

static TUnboxedValuePod CreateDefaultValue(
        const IValueBuilder* valueBuilder,
        const FieldDescriptor* fd,
        const TProtoInfo& info,
        TFlags<EFieldFlag> fieldFlags)
{
#define DEFAULT_TO_VALUE(EProtoCppType, ValueGet) \
case FieldDescriptor::EProtoCppType: { \
    return TUnboxedValuePod(fd->ValueGet()); \
    break; \
}

    switch (fd->cpp_type()) {
        DEFAULT_TO_VALUE(CPPTYPE_INT32,  default_value_int32);
        DEFAULT_TO_VALUE(CPPTYPE_INT64,  default_value_int64);
        DEFAULT_TO_VALUE(CPPTYPE_UINT32, default_value_uint32);
        DEFAULT_TO_VALUE(CPPTYPE_UINT64, default_value_uint64);
        DEFAULT_TO_VALUE(CPPTYPE_DOUBLE, default_value_double);
        DEFAULT_TO_VALUE(CPPTYPE_BOOL,   default_value_bool);

        case FieldDescriptor::CPPTYPE_FLOAT: {
            const auto f = fd->default_value_float();
            return info.YtMode ? TUnboxedValuePod(double(f)) : TUnboxedValuePod(f);
        }
        case FieldDescriptor::CPPTYPE_ENUM:
            return CreateEnumValue(valueBuilder, fd->default_value_enum(), info.EnumFormat, fieldFlags);

        case FieldDescriptor::CPPTYPE_STRING:
            return valueBuilder->NewString(fd->default_value_string()).Release();
        default:
            return TUnboxedValuePod();
}
#undef DEFAULT_TO_VALUE
}

static TUnboxedValuePod CreateRepeatedField(
        const IValueBuilder* valueBuilder,
        const Message& proto,
        const FieldDescriptor* fd,
        const TProtoInfo& info,
        TFlags<EFieldFlag> fieldFlags)
{
    auto r = proto.GetReflection();

#define REPEATED_FIELD_TO_VALUE(EProtoCppType, ProtoGet) \
case FieldDescriptor::EProtoCppType: { \
    for (int i = 0; i < endI; ++i) { \
        *items++ = TUnboxedValuePod(r->ProtoGet(proto, fd, i)); \
    } \
    break; \
}

    const auto endI = r->FieldSize(proto, fd);
    NUdf::TUnboxedValue *items = nullptr;
    auto list = valueBuilder->NewArray(endI, items);
    switch (fd->cpp_type()) {
        REPEATED_FIELD_TO_VALUE(CPPTYPE_INT32,  GetRepeatedInt32);
        REPEATED_FIELD_TO_VALUE(CPPTYPE_INT64,  GetRepeatedInt64);
        REPEATED_FIELD_TO_VALUE(CPPTYPE_UINT32, GetRepeatedUInt32);
        REPEATED_FIELD_TO_VALUE(CPPTYPE_UINT64, GetRepeatedUInt64);
        REPEATED_FIELD_TO_VALUE(CPPTYPE_DOUBLE, GetRepeatedDouble);
        REPEATED_FIELD_TO_VALUE(CPPTYPE_BOOL,   GetRepeatedBool);

        case FieldDescriptor::CPPTYPE_FLOAT:
            for (int i = 0; i < endI; ++i) {
                const auto f = r->GetRepeatedFloat(proto, fd, i);
                *items++ = info.YtMode ? TUnboxedValuePod(double(f)) : TUnboxedValuePod(f);
            }
            break;
        case FieldDescriptor::CPPTYPE_ENUM:
            for (int i = 0; i < endI; ++i) {
                *items++ = CreateEnumValue(valueBuilder, r->GetRepeatedEnum(proto, fd, i), info.EnumFormat, fieldFlags);
            }
            break;
        case FieldDescriptor::CPPTYPE_STRING:
            for (int i = 0; i < endI; ++i) {
                *items++ = valueBuilder->NewString(r->GetRepeatedString(proto, fd, i));
            }
            break;
        case FieldDescriptor::CPPTYPE_MESSAGE:
            for (int i = 0; i < endI; ++i) {
                const auto& protoFieldElement = r->GetRepeatedMessage(proto, fd, i);
                if (fieldFlags.HasFlags(EFieldFlag::Binary)) {
                    *items++ = valueBuilder->NewString(protoFieldElement.SerializeAsString());
                } else {
                    *items++ = FillValueFromProto(protoFieldElement, valueBuilder, info);
                }
            }
            break;
    }
#undef REPEATED_FIELD_TO_VALUE

    return list.Release();
}

static TUnboxedValuePod CreateMapField(
    const IValueBuilder* valueBuilder,
    const Message& proto,
    const FieldDescriptor* fd,
    const TProtoInfo& info,
    const TMessageInfo& msgInfo,
    TFlags<EFieldFlag> fieldFlags)
{
    auto r = proto.GetReflection();

    auto dictType = msgInfo.DictTypes.Value(fd->number(), nullptr);
    Y_ENSURE(dictType);
    auto dictBuilder = valueBuilder->NewDict(dictType, TDictFlags::Hashed);

    const auto noBinaryFlags = TFlags<EFieldFlag>(fieldFlags).RemoveFlags(EFieldFlag::Binary);
    for (int i = 0, end = r->FieldSize(proto, fd); i < end; ++i) {
        const auto& protoDictElement = r->GetRepeatedMessage(proto, fd, i);
        dictBuilder->Add(
            TUnboxedValue(CreateSingleField(valueBuilder, protoDictElement, fd->message_type()->map_key(), info, noBinaryFlags)),
            TUnboxedValue(CreateSingleField(valueBuilder, protoDictElement, fd->message_type()->map_value(), info, fieldFlags))
        );
    }

    return dictBuilder->Build().Release();
}

}

TUnboxedValue FillValueFromProto(
        const Message& proto,
        const IValueBuilder* valueBuilder,
        const TProtoInfo& info)
{
    const auto d  = proto.GetDescriptor();
    const auto r  = proto.GetReflection();
    const auto mi = info.Messages.find(d->full_name());

    if (mi == info.Messages.end()) {
        ythrow yexception() << "unknown message " << d->full_name();
    }

    const auto msgInfo = mi->second;
    TUnboxedValue* items = nullptr;
    auto value = valueBuilder->NewArray(msgInfo->FieldsCount, items);

    auto makeValue = [&](const FieldDescriptor* fd, const TMessageInfo::TFieldInfo& fInfo) -> TUnboxedValuePod {
        if (fInfo.Flags.HasFlags(EFieldFlag::Void)) {
            return TUnboxedValuePod::Void();
        }

        if (fd->is_map() && fInfo.Flags.HasFlags(EFieldFlag::Dict)) {
            if (r->FieldSize(proto, fd) == 0 && fInfo.Flags.HasFlags(EFieldFlag::OptionalContainer)) {
                return TUnboxedValuePod();
            } else {
                return CreateMapField(valueBuilder, proto, fd, info, *msgInfo, fInfo.Flags);
            }
        } else if (fd->is_optional()) {
            if (r->HasField(proto, fd)) {
                return CreateSingleField(valueBuilder, proto, fd, info, fInfo.Flags);
            } else if (fd->has_default_value() || AvoidOptionalScalars(info.SyntaxAware, fd)) {
                return CreateDefaultValue(valueBuilder, fd, info, fInfo.Flags);
            } else {
                return TUnboxedValuePod();
            }
        } else if (fd->is_repeated()) {
            if (r->FieldSize(proto, fd) > 0) {
                return CreateRepeatedField(valueBuilder, proto, fd, info, fInfo.Flags);
            } else {
                if (info.OptionalLists || fInfo.Flags.HasFlags(EFieldFlag::OptionalContainer)) {
                    return TUnboxedValuePod();
                } else {
                    return valueBuilder->NewEmptyList().Release();
                }
            }
        } else if (fd->is_required()) {
            if (r->HasField(proto, fd)) {
                return CreateSingleField(valueBuilder, proto, fd, info, fInfo.Flags);
            } else {
                ythrow yexception() << "required field " << fd->name() << " has no value";
            }
        }
        return TUnboxedValuePod();
    };

    THashSet<const OneofDescriptor*> visitedOneofs;
    for (int i = 0, end = d->field_count(); i < end; ++i) {
        const FieldDescriptor* fd = d->field(i);
        const auto& fInfo = msgInfo->Fields[fd->number()];

        if (auto oneofDescriptor = fd->containing_oneof(); info.YtMode && oneofDescriptor && fInfo.Flags.HasFlags(EFieldFlag::Variant)) {
            if (visitedOneofs.insert(oneofDescriptor).second) {
                items[fInfo.Pos] = TUnboxedValuePod();
                if (auto ofd = r->GetOneofFieldDescriptor(proto, oneofDescriptor)) {
                    const auto& ofInfo = msgInfo->Fields[ofd->number()];
                    if (fInfo.Pos != ofInfo.Pos) {
                        ythrow yexception() << "mismatch of oneof field " << ofd->name() << " position";
                    }
                    const ui32* varIndex = msgInfo->VariantIndicies.FindPtr(ofd->number());
                    if (!varIndex) {
                        ythrow yexception() << "missing oneof field " << ofd->name() << " index";
                    }
                    items[ofInfo.Pos] = valueBuilder->NewVariant(*varIndex, TUnboxedValue(makeValue(ofd, ofInfo))).Release().MakeOptional();
                }
            }
        } else {
            items[fInfo.Pos] = makeValue(fd, fInfo);
        }
    }

    return value;
}

} // namespace NUdf
} // namespace NYql