aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Processors/Formats/Impl/TemplateBlockOutputFormat.h
blob: 53d98849482fc20c5d831ddc23a25fe861ee95bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once

#include <Common/Stopwatch.h>
#include <Core/Block.h>
#include <Formats/FormatSettings.h>
#include <Processors/Formats/IOutputFormat.h>
#include <Formats/ParsedTemplateFormatString.h>


namespace DB
{

class TemplateBlockOutputFormat : public IOutputFormat
{
    using EscapingRule = FormatSettings::EscapingRule;
public:
    TemplateBlockOutputFormat(const Block & header_, WriteBuffer & out_, const FormatSettings & settings_,
                              ParsedTemplateFormatString format_, ParsedTemplateFormatString row_format_,
                              std::string row_between_delimiter_);

    String getName() const override { return "TemplateBlockOutputFormat"; }

    void setRowsBeforeLimit(size_t rows_before_limit_) override { statistics.rows_before_limit = rows_before_limit_; statistics.applied_limit = true; }
    void onProgress(const Progress & progress_) override { statistics.progress.incrementPiecewiseAtomically(progress_); }

    enum class ResultsetPart : size_t
    {
        Data,
        Totals,
        ExtremesMin,
        ExtremesMax,
        Rows,
        RowsBeforeLimit,
        TimeElapsed,
        RowsRead,
        BytesRead
    };

    static ResultsetPart stringToResultsetPart(const String & part);

private:
    void writePrefix() override;
    void consume(Chunk chunk) override;
    void consumeTotals(Chunk chunk) override { statistics.totals = std::move(chunk); }
    void consumeExtremes(Chunk chunk) override { statistics.extremes = std::move(chunk); }
    void finalizeImpl() override;
    void resetFormatterImpl() override;

    void writeRow(const Chunk & chunk, size_t row_num);
    template <typename U, typename V> void writeValue(U value, EscapingRule escaping_rule);

    void onRowsReadBeforeUpdate() override { row_count = getRowsReadBefore(); }
    bool areTotalsAndExtremesUsedInFinalize() const override { return true; }

    const FormatSettings settings;
    Serializations serializations;

    ParsedTemplateFormatString format;
    ParsedTemplateFormatString row_format;

    size_t row_count = 0;

    std::string row_between_delimiter;
};

}