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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#pragma once
#include "Algorithms.h"
#include "ArraySourceVisitor.h"
#include "ArraySinkVisitor.h"
#include "ValueSourceVisitor.h"
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
namespace GatherUtils
{
/// Base classes which selects template function implementation with concrete ArraySource or ArraySink
/// Derived classes should implement selectImpl for ArraySourceSelector and ArraySinkSelector,
/// selectSourceSink for ArraySinkSourceSelector and selectSourcePair for ArraySourcePairSelector
template <typename Base, typename Tuple, int index, typename ... Args>
void callSelectMemberFunctionWithTupleArgument(Tuple & tuple, Args && ... args)
{
if constexpr (index == std::tuple_size<Tuple>::value)
Base::selectImpl(args ...);
else
callSelectMemberFunctionWithTupleArgument<Base, Tuple, index + 1>(tuple, args ..., std::get<index>(tuple));
}
template <typename Base, typename Tuple, int index, typename ... Args>
void callSelectSource(bool is_const, bool is_nullable, Tuple & tuple, Args && ... args)
{
if constexpr (index == std::tuple_size<Tuple>::value)
Base::selectSource(is_const, is_nullable, args ...);
else
callSelectSource<Base, Tuple, index + 1>(is_const, is_nullable, tuple, args ..., std::get<index>(tuple));
}
template <typename Base, typename ... Args>
struct ArraySourceSelectorVisitor final : public ArraySourceVisitorImpl<ArraySourceSelectorVisitor<Base, Args ...>>
{
explicit ArraySourceSelectorVisitor(IArraySource & source, Args && ... args) : packed_args(args ...), array_source(source) {}
using Tuple = std::tuple<Args && ...>;
template <typename Source>
void visitImpl(Source & source)
{
callSelectSource<Base, Tuple, 0>(array_source.isConst(), array_source.isNullable(), packed_args, source);
}
Tuple packed_args;
IArraySource & array_source;
};
template <typename Base>
struct ArraySourceSelector
{
template <typename ... Args>
static void select(IArraySource & source, Args && ... args)
{
ArraySourceSelectorVisitor<Base, Args ...> visitor(source, args ...);
source.accept(visitor);
}
};
template <typename Base, typename ... Args>
struct ArraySinkSelectorVisitor final : public ArraySinkVisitorImpl<ArraySinkSelectorVisitor<Base, Args ...>>
{
explicit ArraySinkSelectorVisitor(Args && ... args) : packed_args(args ...) {}
using Tuple = std::tuple<Args && ...>;
template <typename Sink>
void visitImpl(Sink & sink)
{
callSelectMemberFunctionWithTupleArgument<Base, Tuple, 0>(packed_args, sink);
}
Tuple packed_args;
};
template <typename Base>
struct ArraySinkSelector
{
template <typename ... Args>
static void select(IArraySink & sink, Args && ... args)
{
ArraySinkSelectorVisitor<Base, Args ...> visitor(args ...);
sink.accept(visitor);
}
};
template <typename Base>
struct ArraySourcePairSelector
{
template <typename ... Args>
static void select(IArraySource & first, IArraySource & second, Args && ... args)
{
ArraySourceSelector<Base>::select(first, second, args ...);
}
template <typename FirstSource, typename ... Args>
static void selectSource(bool is_const, bool is_nullable, FirstSource && first, IArraySource & second, Args && ... args)
{
ArraySourceSelector<Base>::select(second, is_const, is_nullable, first, args ...);
}
template <typename SecondSource, typename FirstSource, typename ... Args>
static void selectSource(bool is_second_const, bool is_second_nullable, SecondSource && second,
bool is_first_const, bool is_first_nullable, FirstSource && first, Args && ... args)
{
Base::selectSourcePair(is_first_const, is_first_nullable, first,
is_second_const, is_second_nullable, second, args ...);
}
};
template <typename Base>
struct ArrayAndValueSourceSelectorBySink : public ArraySinkSelector<ArrayAndValueSourceSelectorBySink<Base>>
{
template <typename Sink, typename ... Args>
static void selectImpl(Sink && sink, IArraySource & array_source, IValueSource & value_source, Args && ... args)
{
using SynkType = typename std::decay<Sink>::type;
using ArraySource = typename SynkType::CompatibleArraySource;
using ValueSource = typename SynkType::CompatibleValueSource;
auto check_type = [] (auto source_ptr)
{
if (source_ptr == nullptr)
throw Exception(ErrorCodes::LOGICAL_ERROR, "{} expected {} or {} or {} or {} but got {}",
demangle(typeid(Base).name()),
demangle(typeid(typename SynkType::CompatibleArraySource).name()),
demangle(typeid(ConstSource<typename SynkType::CompatibleArraySource>).name()),
demangle(typeid(typename SynkType::CompatibleValueSource).name()),
demangle(typeid(ConstSource<typename SynkType::CompatibleValueSource>).name()),
demangle(typeid(*source_ptr).name()));
};
auto check_type_and_call_concat = [& sink, & check_type, & args ...] (auto array_source_ptr, auto value_source_ptr)
{
check_type(array_source_ptr);
check_type(value_source_ptr);
Base::selectArrayAndValueSourceBySink(*array_source_ptr, *value_source_ptr, sink, args ...);
};
if (array_source.isConst() && value_source.isConst())
check_type_and_call_concat(typeid_cast<ConstSource<ArraySource> *>(&array_source),
typeid_cast<ConstSource<ValueSource> *>(&value_source));
else if (array_source.isConst())
check_type_and_call_concat(typeid_cast<ConstSource<ArraySource> *>(&array_source),
typeid_cast<ValueSource *>(&value_source));
else if (value_source.isConst())
check_type_and_call_concat(typeid_cast<ArraySource *>(&array_source),
typeid_cast<ConstSource<ValueSource> *>(&value_source));
else
check_type_and_call_concat(typeid_cast<ArraySource *>(&array_source),
typeid_cast<ValueSource *>(&value_source));
}
};
}
}
|