aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Processors/Formats/Impl/JSONColumnsWithMetadataBlockInputFormat.cpp
blob: 23c6114fb39734208fb8929275930521cd4c29bf (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <Processors/Formats/Impl/JSONColumnsWithMetadataBlockInputFormat.h>
#include <IO/ReadHelpers.h>
#include <Formats/FormatFactory.h>
#include <Formats/EscapingRuleUtils.h>
#include <Formats/JSONUtils.h>

namespace DB
{

namespace ErrorCodes
{
    extern const int INCORRECT_DATA;
}

JSONColumnsWithMetadataReader::JSONColumnsWithMetadataReader(ReadBuffer & in_, const Block & header_, const FormatSettings & settings)
    : JSONColumnsReader(in_), header(header_), validate_types_from_metadata(settings.json.validate_types_from_metadata)
{
}

void JSONColumnsWithMetadataReader::readChunkStart()
{
    skipBOMIfExists(*in);
    JSONUtils::skipObjectStart(*in);
    if (validate_types_from_metadata)
        JSONUtils::readMetadataAndValidateHeader(*in, header);
    else
        JSONUtils::readMetadata(*in);

    JSONUtils::skipComma(*in);
    if (!JSONUtils::skipUntilFieldInObject(*in, "data"))
        throw Exception(ErrorCodes::INCORRECT_DATA, "Expected field \"data\" with table content");

    JSONUtils::skipObjectStart(*in);
}


bool JSONColumnsWithMetadataReader::checkChunkEnd()
{
    if (!JSONUtils::checkAndSkipObjectEnd(*in))
        return false;

    JSONUtils::skipTheRestOfObject(*in);
    assertEOF(*in);
    return true;
}

JSONColumnsWithMetadataSchemaReader::JSONColumnsWithMetadataSchemaReader(ReadBuffer & in_) : ISchemaReader(in_)
{
}

NamesAndTypesList JSONColumnsWithMetadataSchemaReader::readSchema()
{
    skipBOMIfExists(in);
    JSONUtils::skipObjectStart(in);
    return JSONUtils::readMetadata(in);
}

void registerInputFormatJSONColumnsWithMetadata(FormatFactory & factory)
{
    factory.registerInputFormat(
        "JSONColumnsWithMetadata",
        [](ReadBuffer & buf,
           const Block &sample,
           const RowInputFormatParams &,
           const FormatSettings & settings)
        {
            return std::make_shared<JSONColumnsBlockInputFormatBase>(buf, sample, settings, std::make_unique<JSONColumnsWithMetadataReader>(buf, sample, settings));
        }
    );
    factory.markFormatSupportsSubsetOfColumns("JSONColumnsWithMetadata");
}

void registerJSONColumnsWithMetadataSchemaReader(FormatFactory & factory)
{
    factory.registerSchemaReader(
        "JSONColumnsWithMetadata",
        [](ReadBuffer & buf, const FormatSettings &)
        {
            return std::make_shared<JSONColumnsWithMetadataSchemaReader>(buf);
        }
    );
}

}