aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Processors/Formats/Impl/JSONColumnsBlockOutputFormatBase.cpp
blob: 490516b7eb4620f4fce7ce014eea141f326ecdaf (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
#include <Processors/Formats/Impl/JSONColumnsBlockOutputFormatBase.h>
#include <IO/WriteHelpers.h>
#include <IO/WriteBufferValidUTF8.h>
#include <Formats/JSONUtils.h>


namespace DB
{

JSONColumnsBlockOutputFormatBase::JSONColumnsBlockOutputFormatBase(
    WriteBuffer & out_, const Block & header_, const FormatSettings & format_settings_, bool validate_utf8)
    : OutputFormatWithUTF8ValidationAdaptor(validate_utf8, header_, out_)
    , format_settings(format_settings_)
    , serializations(header_.getSerializations())
{
}

void JSONColumnsBlockOutputFormatBase::consume(Chunk chunk)
{
    if (!mono_chunk)
    {
        mono_chunk = std::move(chunk);
        return;
    }

    mono_chunk.append(chunk);
}

void JSONColumnsBlockOutputFormatBase::writeSuffix()
{
    writeChunk(mono_chunk);
    mono_chunk.clear();
}

void JSONColumnsBlockOutputFormatBase::writeChunk(Chunk & chunk)
{
    writeChunkStart();
    const auto & columns = chunk.getColumns();
    for (size_t i = 0; i != columns.size(); ++i)
    {
        writeColumnStart(i);
        writeColumn(*columns[i], *serializations[i]);
        writeColumnEnd(i == columns.size() - 1);
    }
    written_rows += chunk.getNumRows();
    writeChunkEnd();
}

void JSONColumnsBlockOutputFormatBase::writeColumnEnd(bool is_last)
{
    JSONUtils::writeCompactArrayEnd(*ostr);
    if (!is_last)
        JSONUtils::writeFieldDelimiter(*ostr);
}

void JSONColumnsBlockOutputFormatBase::writeColumn(const IColumn & column, const ISerialization & serialization)
{
    for (size_t i = 0; i != column.size(); ++i)
    {
        if (i != 0)
            JSONUtils::writeFieldCompactDelimiter(*ostr);
        serialization.serializeTextJSON(column, i, *ostr, format_settings);
    }
}

}