1#pragma once
2
3#include <Common/Stopwatch.h>
4#include <Core/Block.h>
5#include <Formats/FormatSettings.h>
6#include <Processors/Formats/IOutputFormat.h>
7#include <Formats/ParsedTemplateFormatString.h>
8
9
10namespace DB
11{
12
13class TemplateBlockOutputFormat : public IOutputFormat
14{
15 using ColumnFormat = ParsedTemplateFormatString::ColumnFormat;
16public:
17 TemplateBlockOutputFormat(const Block & header_, WriteBuffer & out_, const FormatSettings & settings_,
18 ParsedTemplateFormatString format_, ParsedTemplateFormatString row_format_,
19 std::string row_between_delimiter_);
20
21 String getName() const override { return "TemplateBlockOutputFormat"; }
22
23 void doWritePrefix() override;
24
25 void setRowsBeforeLimit(size_t rows_before_limit_) override { rows_before_limit = rows_before_limit_; rows_before_limit_set = true; }
26 void onProgress(const Progress & progress_) override { progress.incrementPiecewiseAtomically(progress_); }
27
28 enum class ResultsetPart : size_t
29 {
30 Data,
31 Totals,
32 ExtremesMin,
33 ExtremesMax,
34 Rows,
35 RowsBeforeLimit,
36 TimeElapsed,
37 RowsRead,
38 BytesRead
39 };
40
41 static ResultsetPart stringToResultsetPart(const String & part);
42
43protected:
44 void consume(Chunk chunk) override;
45 void consumeTotals(Chunk chunk) override { totals = std::move(chunk); }
46 void consumeExtremes(Chunk chunk) override { extremes = std::move(chunk); }
47 void finalize() override;
48
49 void writeRow(const Chunk & chunk, size_t row_num);
50 void serializeField(const IColumn & column, const IDataType & type, size_t row_num, ColumnFormat format);
51 template <typename U, typename V> void writeValue(U value, ColumnFormat col_format);
52
53protected:
54 const FormatSettings settings;
55 DataTypes types;
56
57 ParsedTemplateFormatString format;
58 ParsedTemplateFormatString row_format;
59
60 size_t rows_before_limit = 0;
61 bool rows_before_limit_set = false;
62 Chunk totals;
63 Chunk extremes;
64 Progress progress;
65 Stopwatch watch;
66
67 size_t row_count = 0;
68 bool need_write_prefix = true;
69
70 std::string row_between_delimiter;
71};
72
73}
74