diff --git a/dbms/src/Formats/BinaryRowInputStream.cpp b/dbms/src/Formats/BinaryRowInputStream.cpp index 7c059782e6d..6b378f4b572 100644 --- a/dbms/src/Formats/BinaryRowInputStream.cpp +++ b/dbms/src/Formats/BinaryRowInputStream.cpp @@ -8,8 +8,8 @@ namespace DB { -BinaryRowInputStream::BinaryRowInputStream(ReadBuffer & istr_, const Block & header_) - : istr(istr_), header(header_) +BinaryRowInputStream::BinaryRowInputStream(ReadBuffer & istr_, const Block & header_, bool with_names_, bool with_types_) + : istr(istr_), header(header_), with_names(with_names_), with_types(with_types_) { } @@ -27,6 +27,34 @@ bool BinaryRowInputStream::read(MutableColumns & columns, RowReadExtension &) } +void BinaryRowInputStream::readPrefix() +{ + UInt64 columns; + String tmp; + + if (with_names || with_types) + { + readVarUInt(columns, istr) + } + + if (with_names) + { + for (size_t i = 0; i < columns; ++i) + { + readStringBinary(tmp, istr); + } + } + + if (with_types) + { + for (size_t i = 0; i < columns; ++i) + { + readStringBinary(tmp, istr); + } + } +} + + void registerInputFormatRowBinary(FormatFactory & factory) { factory.registerInputFormat("RowBinary", []( @@ -37,7 +65,18 @@ void registerInputFormatRowBinary(FormatFactory & factory) const FormatSettings & settings) { return std::make_shared( - std::make_shared(buf, sample), + std::make_shared(buf, sample, false, false), + sample, max_block_size, settings); + }); + + factory.registerInputFormat("RowBinaryWithNamesAndTypes", []( + WriteBuffer & buf, + const Block & sample, + const Context &, + const FormatSettings &) + { + return std::make_shared( + std::make_shared(buf, sample, true, true), sample, max_block_size, settings); }); } diff --git a/dbms/src/Formats/BinaryRowInputStream.h b/dbms/src/Formats/BinaryRowInputStream.h index f70e081f097..43af4076f9c 100644 --- a/dbms/src/Formats/BinaryRowInputStream.h +++ b/dbms/src/Formats/BinaryRowInputStream.h @@ -15,13 +15,16 @@ class ReadBuffer; class BinaryRowInputStream : public IRowInputStream { public: - BinaryRowInputStream(ReadBuffer & istr_, const Block & header_); + BinaryRowInputStream(ReadBuffer & istr_, const Block & sample_, bool with_names_, bool with_types_); bool read(MutableColumns & columns, RowReadExtension &) override; + void readPrefix() override; private: ReadBuffer & istr; Block header; + bool with_names; + bool with_types; }; } diff --git a/dbms/src/Formats/BinaryRowOutputStream.cpp b/dbms/src/Formats/BinaryRowOutputStream.cpp index 7f1d4e7a4c0..63417cac273 100644 --- a/dbms/src/Formats/BinaryRowOutputStream.cpp +++ b/dbms/src/Formats/BinaryRowOutputStream.cpp @@ -20,14 +20,14 @@ void BinaryRowOutputStream::writePrefix() if (with_names || with_types) { - writeVarUInt(columns) + writeVarUInt(columns, ostr) } if (with_names) { for (size_t i = 0; i < columns; ++i) { - writeBinary(sample.safeGetByPosition(i).name, ostr); + writeStringBinary(sample.safeGetByPosition(i).name, ostr); } } @@ -35,7 +35,7 @@ void BinaryRowOutputStream::writePrefix() { for (size_t i = 0; i < columns; ++i) { - writeBinary(sample.safeGetByPosition(i).type->getName(), ostr); + writeStringBinary(sample.safeGetByPosition(i).type->getName(), ostr); } } } diff --git a/dbms/src/Formats/BinaryRowOutputStream.h b/dbms/src/Formats/BinaryRowOutputStream.h index ff908ba4a7d..d16bcfa6c03 100644 --- a/dbms/src/Formats/BinaryRowOutputStream.h +++ b/dbms/src/Formats/BinaryRowOutputStream.h @@ -1,6 +1,7 @@ #pragma once #include +#include namespace DB @@ -16,7 +17,7 @@ class WriteBuffer; class BinaryRowOutputStream : public IRowOutputStream { public: - BinaryRowOutputStream(WriteBuffer & ostr_); + BinaryRowOutputStream(WriteBuffer & ostr_, const Block & sample_, bool with_names_, bool with_types_); void writeField(const IColumn & column, const IDataType & type, size_t row_num) override; void writePrefix() override; diff --git a/dbms/tests/queries/0_stateless/00309_formats.sql b/dbms/tests/queries/0_stateless/00309_formats.sql index 470e4327715..87a1ea454d0 100644 --- a/dbms/tests/queries/0_stateless/00309_formats.sql +++ b/dbms/tests/queries/0_stateless/00309_formats.sql @@ -1,5 +1,6 @@ SET output_format_write_statistics = 0; SELECT number * 246 + 10 AS n, toDate('2000-01-01') + n AS d, range(n) AS arr, arrayStringConcat(arrayMap(x -> reinterpretAsString(x), arr)) AS s, (n, d) AS tuple FROM system.numbers LIMIT 2 FORMAT RowBinary; +SELECT number * 246 + 10 AS n, toDate('2000-01-01') + n AS d, range(n) AS arr, arrayStringConcat(arrayMap(x -> reinterpretAsString(x), arr)) AS s, (n, d) AS tuple FROM system.numbers LIMIT 2 FORMAT RowBinaryWithNamesAndTypes; SELECT number * 246 + 10 AS n, toDate('2000-01-01') + n AS d, range(n) AS arr, arrayStringConcat(arrayMap(x -> reinterpretAsString(x), arr)) AS s, (n, d) AS tuple FROM system.numbers LIMIT 2 FORMAT TabSeparatedWithNamesAndTypes; SELECT number * 246 + 10 AS n, toDate('2000-01-01') + n AS d, range(n) AS arr, arrayStringConcat(arrayMap(x -> reinterpretAsString(x), arr)) AS s, (n, d) AS tuple FROM system.numbers LIMIT 2 FORMAT TabSeparatedRaw; SELECT number * 246 + 10 AS n, toDate('2000-01-01') + n AS d, range(n) AS arr, arrayStringConcat(arrayMap(x -> reinterpretAsString(x), arr)) AS s, (n, d) AS tuple FROM system.numbers LIMIT 2 FORMAT CSV;