1#include <IO/WriteBuffer.h>
2#include <IO/WriteHelpers.h>
3#include <Columns/IColumn.h>
4#include <DataTypes/IDataType.h>
5#include <Processors/Formats/Impl/BinaryRowOutputFormat.h>
6#include <Formats/FormatFactory.h>
7
8
9namespace DB
10{
11
12BinaryRowOutputFormat::BinaryRowOutputFormat(WriteBuffer & out_, const Block & header, bool with_names_, bool with_types_, FormatFactory::WriteCallback callback)
13 : IRowOutputFormat(header, out_, callback), with_names(with_names_), with_types(with_types_)
14{
15}
16
17void BinaryRowOutputFormat::writePrefix()
18{
19 auto & header = getPort(PortKind::Main).getHeader();
20 size_t columns = header.columns();
21
22 if (with_names || with_types)
23 {
24 writeVarUInt(columns, out);
25 }
26
27 if (with_names)
28 {
29 for (size_t i = 0; i < columns; ++i)
30 {
31 writeStringBinary(header.safeGetByPosition(i).name, out);
32 }
33 }
34
35 if (with_types)
36 {
37 for (size_t i = 0; i < columns; ++i)
38 {
39 writeStringBinary(header.safeGetByPosition(i).type->getName(), out);
40 }
41 }
42}
43
44void BinaryRowOutputFormat::writeField(const IColumn & column, const IDataType & type, size_t row_num)
45{
46 type.serializeBinary(column, row_num, out);
47}
48
49
50void registerOutputFormatProcessorRowBinary(FormatFactory & factory)
51{
52 factory.registerOutputFormatProcessor("RowBinary", [](
53 WriteBuffer & buf,
54 const Block & sample,
55 FormatFactory::WriteCallback callback,
56 const FormatSettings &)
57 {
58 return std::make_shared<BinaryRowOutputFormat>(buf, sample, false, false, callback);
59 });
60
61 factory.registerOutputFormatProcessor("RowBinaryWithNamesAndTypes", [](
62 WriteBuffer & buf,
63 const Block & sample,
64 FormatFactory::WriteCallback callback,
65 const FormatSettings &)
66 {
67 return std::make_shared<BinaryRowOutputFormat>(buf, sample, true, true, callback);
68 });
69}
70
71}
72