diff --git a/src/Formats/registerFormats.cpp b/src/Formats/registerFormats.cpp index 768ba4a6edbb..20f4cb6523df 100644 --- a/src/Formats/registerFormats.cpp +++ b/src/Formats/registerFormats.cpp @@ -94,6 +94,7 @@ void registerOutputFormatMarkdown(FormatFactory & factory); void registerOutputFormatPostgreSQLWire(FormatFactory & factory); void registerOutputFormatPrometheus(FormatFactory & factory); void registerOutputFormatSQLInsert(FormatFactory & factory); +void registerOutputFormatHash(FormatFactory & factory); /// Input only formats. @@ -240,6 +241,7 @@ void registerFormats() registerOutputFormatCapnProto(factory); registerOutputFormatPrometheus(factory); registerOutputFormatSQLInsert(factory); + registerOutputFormatHash(factory); registerInputFormatRegexp(factory); registerInputFormatJSONAsString(factory); diff --git a/src/Processors/Formats/Impl/HashOutputFormat.cpp b/src/Processors/Formats/Impl/HashOutputFormat.cpp new file mode 100644 index 000000000000..b12dfa0f07a2 --- /dev/null +++ b/src/Processors/Formats/Impl/HashOutputFormat.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include + + +namespace DB +{ + +HashOutputFormat::HashOutputFormat(WriteBuffer & out_, SharedHeader header_) + : IOutputFormat(header_, out_) +{ +} + +String HashOutputFormat::getName() const +{ + return "HashOutputFormat"; +} + +void HashOutputFormat::consume(Chunk chunk) +{ + for (const auto & column : chunk.getColumns()) + { + for (size_t i = 0; i < column->size(); ++i) + column->updateHashWithValue(i, hash); + } +} + +void HashOutputFormat::finalizeImpl() +{ + std::string hashString = getSipHash128AsHexString(hash); + out.write(hashString.data(), hashString.size()); + out.write("\n", 1); + out.next(); +} + +void registerOutputFormatHash(FormatFactory & factory) +{ + factory.registerOutputFormat("Hash", + [](WriteBuffer & buf, const Block & header, const FormatSettings &) + { + return std::make_shared(buf, std::make_shared(header)); + }); +} + +} diff --git a/src/Processors/Formats/Impl/HashOutputFormat.h b/src/Processors/Formats/Impl/HashOutputFormat.h new file mode 100644 index 000000000000..322d1f811ecf --- /dev/null +++ b/src/Processors/Formats/Impl/HashOutputFormat.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +namespace DB +{ + +class HashOutputFormat final : public IOutputFormat +{ +public: + HashOutputFormat(WriteBuffer & out_, SharedHeader header_); + String getName() const override; + +private: + void consume(Chunk chunk) override; + void finalizeImpl() override; + + SipHash hash; +}; + +} diff --git a/tests/queries/0_stateless/03577_hash_format.reference b/tests/queries/0_stateless/03577_hash_format.reference new file mode 100644 index 000000000000..7f2336a0083a --- /dev/null +++ b/tests/queries/0_stateless/03577_hash_format.reference @@ -0,0 +1,3 @@ +75b419a3aa739a211291e7cc119bd3c9 +d3b90098d049660862d6dc53ac7505e5 +92d47ccfb2950a8e10ac9ddf4314f1bf diff --git a/tests/queries/0_stateless/03577_hash_format.sql b/tests/queries/0_stateless/03577_hash_format.sql new file mode 100644 index 000000000000..1b21ac2ad3a9 --- /dev/null +++ b/tests/queries/0_stateless/03577_hash_format.sql @@ -0,0 +1,3 @@ +SELECT number FROM system.numbers LIMIT 1 FORMAT Hash; +SELECT number FROM system.numbers LIMIT 20 FORMAT Hash; +SELECT number AS hello, toString(number) AS world, (hello, world) AS tuple, nullIf(hello % 3, 0) AS sometimes_nulls FROM system.numbers LIMIT 20 FORMAT Hash;