aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Processors/Formats/Impl/CapnProtoRowInputFormat.h
blob: d19b655e23d2210000b335fff480ae6ca83d2ab6 (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
#pragma once

#include "clickhouse_config.h"
#if USE_CAPNP

#include <Core/Block.h>
#include <Formats/CapnProtoSchema.h>
#include <Formats/CapnProtoSerializer.h>
#include <Processors/Formats/IRowInputFormat.h>
#include <Processors/Formats/ISchemaReader.h>

namespace DB
{

class FormatSchemaInfo;
class ReadBuffer;

/** A stream for reading messages in Cap'n Proto format in given schema.
  * Like Protocol Buffers and Thrift (but unlike JSON or MessagePack),
  * Cap'n Proto messages are strongly-typed and not self-describing.
  * The schema in this case cannot be compiled in, so it uses a runtime schema parser.
  * See https://capnproto.org/cxx.html
  */
class CapnProtoRowInputFormat final : public IRowInputFormat
{
public:
    CapnProtoRowInputFormat(ReadBuffer & in_, Block header, Params params_, const CapnProtoSchemaInfo & info, const FormatSettings & format_settings);

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

private:
    bool readRow(MutableColumns & columns, RowReadExtension &) override;

    bool supportsCountRows() const override { return true; }
    size_t countRows(size_t max_block_size) override;

    std::pair<kj::Array<capnp::word>, size_t> readMessagePrefix();
    kj::Array<capnp::word> readMessage();
    void skipMessage();

    std::shared_ptr<CapnProtoSchemaParser> parser;
    capnp::StructSchema schema;
    std::unique_ptr<CapnProtoSerializer> serializer;
};

class CapnProtoSchemaReader : public IExternalSchemaReader
{
public:
    explicit CapnProtoSchemaReader(const FormatSettings & format_settings_);

    NamesAndTypesList readSchema() override;

private:
    const FormatSettings format_settings;
};

}

#endif // USE_CAPNP