aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Dictionaries/DictionarySource.h
blob: 3fa7ce8bda6647fb48af457bec81ac07975f8174 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#pragma once

#include <boost/noncopyable.hpp>
#include <memory>
#include <Columns/IColumn.h>
#include <Core/Names.h>
#include <Core/Block.h>
#include <Dictionaries/DictionaryStructure.h>
#include <Dictionaries/IDictionary.h>


namespace DB
{

class DictionarySource;

class DictionarySourceCoordinator final : public std::enable_shared_from_this<DictionarySourceCoordinator>
                                        , private boost::noncopyable
{
public:
    using ReadColumnsFunc = std::function<Columns (const Strings &, const DataTypes &, const Columns &, const DataTypes &, const Columns &)>;

    Pipe read(size_t num_streams);

    explicit DictionarySourceCoordinator(
        std::shared_ptr<const IDictionary> dictionary_,
        const Names & column_names,
        ColumnsWithTypeAndName && key_columns_with_type_,
        size_t max_block_size_)
        : dictionary(std::move(dictionary_))
        , key_columns_with_type(std::move(key_columns_with_type_))
        , max_block_size(max_block_size_)
        , read_columns_func([this](
            const Strings & attribute_names,
            const DataTypes & result_types,
            const Columns & key_columns,
            const DataTypes & key_types,
            const Columns & default_values_columns)
        {
            return dictionary->getColumns(attribute_names, result_types, key_columns, key_types, default_values_columns);
        })
    {
        initialize(column_names);
    }

    explicit DictionarySourceCoordinator(
        std::shared_ptr<const IDictionary> dictionary_,
        const Names & column_names,
        ColumnsWithTypeAndName && key_columns_with_type_,
        ColumnsWithTypeAndName && data_columns_with_type_,
        size_t max_block_size_)
        : dictionary(std::move(dictionary_))
        , key_columns_with_type(std::move(key_columns_with_type_))
        , data_columns_with_type(std::move(data_columns_with_type_))
        , max_block_size(max_block_size_)
        , read_columns_func([this](
            const Strings & attribute_names,
            const DataTypes & result_types,
            const Columns & key_columns,
            const DataTypes & key_types,
            const Columns & default_values_columns)
        {
            return dictionary->getColumns(attribute_names, result_types, key_columns, key_types, default_values_columns);
        })
    {
        initialize(column_names);
    }

    explicit DictionarySourceCoordinator(
        std::shared_ptr<const IDictionary> dictionary_,
        const Names & column_names,
        ColumnsWithTypeAndName && key_columns_with_type_,
        ColumnsWithTypeAndName && data_columns_with_type_,
        size_t max_block_size_,
        ReadColumnsFunc read_columns_func_)
        : dictionary(std::move(dictionary_))
        , key_columns_with_type(std::move(key_columns_with_type_))
        , data_columns_with_type(std::move(data_columns_with_type_))
        , max_block_size(max_block_size_)
        , read_columns_func(std::move(read_columns_func_))
    {
        initialize(column_names);
    }

private:

    friend class DictionarySource;

    bool getKeyColumnsNextRangeToRead(ColumnsWithTypeAndName & key_columns, ColumnsWithTypeAndName & data_columns);

    const Block & getHeader() const { return header; }

    const std::vector<std::string> & getAttributesNamesToRead() const { return attributes_names_to_read; }

    const std::vector<DataTypePtr> & getAttributesTypesToRead() const { return attributes_types_to_read; }

    const std::vector<ColumnPtr> & getAttributesDefaultValuesColumns() const { return attributes_default_values_columns; }

    const ReadColumnsFunc & getReadColumnsFunc() const { return read_columns_func; }

    const std::shared_ptr<const IDictionary> & getDictionary() const { return dictionary; }

    void initialize(const Names & column_names);

    static ColumnsWithTypeAndName cutColumns(const ColumnsWithTypeAndName & columns_with_type, size_t start, size_t length);

    std::shared_ptr<const IDictionary> dictionary;

    ColumnsWithTypeAndName key_columns_with_type;
    ColumnsWithTypeAndName data_columns_with_type;

    Block header;

    std::vector<std::string> attributes_names_to_read;
    std::vector<DataTypePtr> attributes_types_to_read;
    std::vector<ColumnPtr> attributes_default_values_columns;

    const size_t max_block_size;
    ReadColumnsFunc read_columns_func;

    std::atomic<size_t> parallel_read_block_index = 0;
};

}