aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/io/proto_table_writer.cpp
blob: 62593ff21e1383723f987c7b320d2d9af2615386 (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
#include "proto_table_writer.h"

#include "node_table_writer.h"
#include "proto_helpers.h"

#include <yt/cpp/mapreduce/common/node_builder.h>

#include <yt/cpp/mapreduce/interface/io.h>

#include <yt/cpp/mapreduce/io/job_writer.h>

#include <yt/yt_proto/yt/formats/extension.pb.h>

#include <google/protobuf/unknown_field_set.h>

namespace NYT {

using ::google::protobuf::Descriptor;
using ::google::protobuf::FieldDescriptor;

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

TNode MakeNodeFromMessage(const Message& row)
{
    TNode node;
    TNodeBuilder builder(&node);
    builder.OnBeginMap();

    auto* descriptor = row.GetDescriptor();
    auto* reflection = row.GetReflection();

    int count = descriptor->field_count();
    for (int i = 0; i < count; ++i) {
        auto* fieldDesc = descriptor->field(i);
        if (fieldDesc->is_repeated()) {
            Y_ENSURE(reflection->FieldSize(row, fieldDesc) == 0, "Storing repeated protobuf fields is not supported yet");
            continue;
        } else if (!reflection->HasField(row, fieldDesc)) {
            continue;
        }

        TString columnName = fieldDesc->options().GetExtension(column_name);
        if (columnName.empty()) {
            const auto& keyColumnName = fieldDesc->options().GetExtension(key_column_name);
            columnName = keyColumnName.empty() ? fieldDesc->name() : keyColumnName;
        }

        builder.OnKeyedItem(columnName);

        switch (fieldDesc->type()) {
            case FieldDescriptor::TYPE_STRING:
            case FieldDescriptor::TYPE_BYTES:
                builder.OnStringScalar(reflection->GetString(row, fieldDesc));
                break;
            case FieldDescriptor::TYPE_INT64:
            case FieldDescriptor::TYPE_SINT64:
            case FieldDescriptor::TYPE_SFIXED64:
                builder.OnInt64Scalar(reflection->GetInt64(row, fieldDesc));
                break;
            case FieldDescriptor::TYPE_INT32:
            case FieldDescriptor::TYPE_SINT32:
            case FieldDescriptor::TYPE_SFIXED32:
                builder.OnInt64Scalar(reflection->GetInt32(row, fieldDesc));
                break;
            case FieldDescriptor::TYPE_UINT64:
            case FieldDescriptor::TYPE_FIXED64:
                builder.OnUint64Scalar(reflection->GetUInt64(row, fieldDesc));
                break;
            case FieldDescriptor::TYPE_UINT32:
            case FieldDescriptor::TYPE_FIXED32:
                builder.OnUint64Scalar(reflection->GetUInt32(row, fieldDesc));
                break;
            case FieldDescriptor::TYPE_DOUBLE:
                builder.OnDoubleScalar(reflection->GetDouble(row, fieldDesc));
                break;
            case FieldDescriptor::TYPE_FLOAT:
                builder.OnDoubleScalar(reflection->GetFloat(row, fieldDesc));
                break;
            case FieldDescriptor::TYPE_BOOL:
                builder.OnBooleanScalar(reflection->GetBool(row, fieldDesc));
                break;
            case FieldDescriptor::TYPE_ENUM:
                builder.OnStringScalar(reflection->GetEnum(row, fieldDesc)->name());
                break;
            case FieldDescriptor::TYPE_MESSAGE:
                builder.OnStringScalar(reflection->GetMessage(row, fieldDesc).SerializeAsString());
                break;
            default:
                ythrow yexception() << "Invalid field type for column: " << columnName;
                break;
        }
    }

    builder.OnEndMap();
    return node;
}

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

TProtoTableWriter::TProtoTableWriter(
    THolder<IProxyOutput> output,
    TVector<const Descriptor*>&& descriptors)
    : NodeWriter_(new TNodeTableWriter(std::move(output)))
    , Descriptors_(std::move(descriptors))
{ }

TProtoTableWriter::~TProtoTableWriter()
{ }

size_t TProtoTableWriter::GetBufferMemoryUsage() const
{
    return NodeWriter_->GetBufferMemoryUsage();
}

size_t TProtoTableWriter::GetTableCount() const
{
    return NodeWriter_->GetTableCount();
}

void TProtoTableWriter::FinishTable(size_t tableIndex)
{
    NodeWriter_->FinishTable(tableIndex);
}

void TProtoTableWriter::AddRow(const Message& row, size_t tableIndex)
{
    NodeWriter_->AddRow(MakeNodeFromMessage(row), tableIndex);
}

void TProtoTableWriter::AddRow(Message&& row, size_t tableIndex)
{
    TProtoTableWriter::AddRow(row, tableIndex);
}


void TProtoTableWriter::Abort()
{
    NodeWriter_->Abort();
}

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

TLenvalProtoTableWriter::TLenvalProtoTableWriter(
    THolder<IProxyOutput> output,
    TVector<const Descriptor*>&& descriptors)
    : Output_(std::move(output))
    , Descriptors_(std::move(descriptors))
{ }

TLenvalProtoTableWriter::~TLenvalProtoTableWriter()
{ }

size_t TLenvalProtoTableWriter::GetBufferMemoryUsage() const
{
    return Output_->GetBufferMemoryUsage();
}

size_t TLenvalProtoTableWriter::GetTableCount() const
{
    return Output_->GetStreamCount();
}

void TLenvalProtoTableWriter::FinishTable(size_t tableIndex)
{
    Output_->GetStream(tableIndex)->Finish();
}

void TLenvalProtoTableWriter::AddRow(const Message& row, size_t tableIndex)
{
    ValidateProtoDescriptor(row, tableIndex, Descriptors_, false);

    Y_ABORT_UNLESS(row.GetReflection()->GetUnknownFields(row).empty(),
        "Message has unknown fields. This probably means bug in client code.\n"
        "Message: %s", row.DebugString().data());

    auto* stream = Output_->GetStream(tableIndex);
    i32 size = row.ByteSize();
    stream->Write(&size, sizeof(size));
    bool serializedOk = row.SerializeToArcadiaStream(stream);
    Y_ENSURE(serializedOk, "Failed to serialize protobuf message");
    Output_->OnRowFinished(tableIndex);
}

void TLenvalProtoTableWriter::AddRow(Message&& row, size_t tableIndex)
{
    TLenvalProtoTableWriter::AddRow(row, tableIndex);
}

void TLenvalProtoTableWriter::Abort()
{
    Output_->Abort();
}

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

TLenvalProtoSingleTableWriter::TLenvalProtoSingleTableWriter(
    THolder<IProxyOutput> output,
    const Descriptor* descriptor)
    : TLenvalProtoTableWriter(std::move(output), {descriptor})
{ }

void TLenvalProtoSingleTableWriter::AddRow(const Message& row, size_t tableIndex)
{
    ValidateProtoDescriptor(row, 0, Descriptors_, false);

    Y_ABORT_UNLESS(row.GetReflection()->GetUnknownFields(row).empty(),
        "Message has unknown fields. This probably means bug in client code.\n"
        "Message: %s", row.DebugString().data());

    auto* stream = Output_->GetStream(tableIndex);
    i32 size = row.ByteSize();
    stream->Write(&size, sizeof(size));
    bool serializedOk = row.SerializeToArcadiaStream(stream);
    Y_ENSURE(serializedOk, "Failed to serialize protobuf message");
    Output_->OnRowFinished(tableIndex);
}

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

} // namespace NYT