| 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 | |
| 9 | namespace DB |
| 10 | { |
| 11 | |
| 12 | BinaryRowOutputFormat::BinaryRowOutputFormat(WriteBuffer & out_, const Block & , 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 | |
| 17 | void BinaryRowOutputFormat::writePrefix() |
| 18 | { |
| 19 | auto & = 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 | |
| 44 | void BinaryRowOutputFormat::writeField(const IColumn & column, const IDataType & type, size_t row_num) |
| 45 | { |
| 46 | type.serializeBinary(column, row_num, out); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | void 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 | |