Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Formats/registerFormats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -240,6 +241,7 @@ void registerFormats()
registerOutputFormatCapnProto(factory);
registerOutputFormatPrometheus(factory);
registerOutputFormatSQLInsert(factory);
registerOutputFormatHash(factory);

registerInputFormatRegexp(factory);
registerInputFormatJSONAsString(factory);
Expand Down
47 changes: 47 additions & 0 deletions src/Processors/Formats/Impl/HashOutputFormat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <Processors/Formats/Impl/HashOutputFormat.h>
#include <Columns/IColumn.h>
#include <Formats/FormatFactory.h>
#include <IO/WriteBuffer.h>
#include <Processors/Port.h>


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<HashOutputFormat>(buf, std::make_shared<const Block>(header));
});
}

}
22 changes: 22 additions & 0 deletions src/Processors/Formats/Impl/HashOutputFormat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <Common/SipHash.h>
#include <Processors/Formats/IOutputFormat.h>

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;
};

}
3 changes: 3 additions & 0 deletions tests/queries/0_stateless/03577_hash_format.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
75b419a3aa739a211291e7cc119bd3c9
d3b90098d049660862d6dc53ac7505e5
92d47ccfb2950a8e10ac9ddf4314f1bf
3 changes: 3 additions & 0 deletions tests/queries/0_stateless/03577_hash_format.sql
Original file line number Diff line number Diff line change
@@ -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;