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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
#include <Processors/Transforms/IntersectOrExceptTransform.h>
namespace DB
{
/// After visitor is applied, ASTSelectIntersectExcept always has two child nodes.
IntersectOrExceptTransform::IntersectOrExceptTransform(const Block & header_, Operator operator_)
: IProcessor(InputPorts(2, header_), {header_})
, current_operator(operator_)
{
const Names & columns = header_.getNames();
size_t num_columns = columns.empty() ? header_.columns() : columns.size();
key_columns_pos.reserve(columns.size());
for (size_t i = 0; i < num_columns; ++i)
{
auto pos = columns.empty() ? i : header_.getPositionByName(columns[i]);
key_columns_pos.emplace_back(pos);
}
}
IntersectOrExceptTransform::Status IntersectOrExceptTransform::prepare()
{
auto & output = outputs.front();
if (output.isFinished())
{
for (auto & in : inputs)
in.close();
return Status::Finished;
}
if (!output.canPush())
{
for (auto & input : inputs)
input.setNotNeeded();
return Status::PortFull;
}
if (current_output_chunk)
{
output.push(std::move(current_output_chunk));
}
if (finished_second_input)
{
if (inputs.front().isFinished())
{
output.finish();
return Status::Finished;
}
}
else if (inputs.back().isFinished())
{
finished_second_input = true;
}
if (!has_input)
{
InputPort & input = finished_second_input ? inputs.front() : inputs.back();
input.setNeeded();
if (!input.hasData())
return Status::NeedData;
current_input_chunk = input.pull();
has_input = true;
}
return Status::Ready;
}
void IntersectOrExceptTransform::work()
{
if (!finished_second_input)
{
accumulate(std::move(current_input_chunk));
}
else
{
filter(current_input_chunk);
current_output_chunk = std::move(current_input_chunk);
}
has_input = false;
}
template <typename Method>
void IntersectOrExceptTransform::addToSet(Method & method, const ColumnRawPtrs & columns, size_t rows, SetVariants & variants) const
{
typename Method::State state(columns, key_sizes, nullptr);
for (size_t i = 0; i < rows; ++i)
state.emplaceKey(method.data, i, variants.string_pool);
}
template <typename Method>
size_t IntersectOrExceptTransform::buildFilter(
Method & method, const ColumnRawPtrs & columns, IColumn::Filter & filter, size_t rows, SetVariants & variants) const
{
typename Method::State state(columns, key_sizes, nullptr);
size_t new_rows_num = 0;
for (size_t i = 0; i < rows; ++i)
{
auto find_result = state.findKey(method.data, i, variants.string_pool);
filter[i] = (current_operator == ASTSelectIntersectExceptQuery::Operator::EXCEPT_ALL
|| current_operator == ASTSelectIntersectExceptQuery::Operator::EXCEPT_DISTINCT)
? !find_result.isFound()
: find_result.isFound();
if (filter[i])
++new_rows_num;
}
return new_rows_num;
}
void IntersectOrExceptTransform::accumulate(Chunk chunk)
{
convertToFullIfSparse(chunk);
auto num_rows = chunk.getNumRows();
auto columns = chunk.detachColumns();
ColumnRawPtrs column_ptrs;
column_ptrs.reserve(key_columns_pos.size());
for (auto pos : key_columns_pos)
{
/// Hash methods expect non-const column
columns[pos] = columns[pos]->convertToFullColumnIfConst();
column_ptrs.emplace_back(columns[pos].get());
}
if (!data)
data.emplace();
if (data->empty())
data->init(SetVariants::chooseMethod(column_ptrs, key_sizes));
auto & data_set = *data;
switch (data->type)
{
case SetVariants::Type::EMPTY:
break;
#define M(NAME) \
case SetVariants::Type::NAME: \
addToSet(*data_set.NAME, column_ptrs, num_rows, data_set); \
break;
APPLY_FOR_SET_VARIANTS(M)
#undef M
}
}
void IntersectOrExceptTransform::filter(Chunk & chunk)
{
convertToFullIfSparse(chunk);
auto num_rows = chunk.getNumRows();
auto columns = chunk.detachColumns();
ColumnRawPtrs column_ptrs;
column_ptrs.reserve(key_columns_pos.size());
for (auto pos : key_columns_pos)
{
/// Hash methods expect non-const column
columns[pos] = columns[pos]->convertToFullColumnIfConst();
column_ptrs.emplace_back(columns[pos].get());
}
if (!data)
data.emplace();
if (data->empty())
data->init(SetVariants::chooseMethod(column_ptrs, key_sizes));
size_t new_rows_num = 0;
IColumn::Filter filter(num_rows);
auto & data_set = *data;
switch (data->type)
{
case SetVariants::Type::EMPTY:
break;
#define M(NAME) \
case SetVariants::Type::NAME: \
new_rows_num = buildFilter(*data_set.NAME, column_ptrs, filter, num_rows, data_set); \
break;
APPLY_FOR_SET_VARIANTS(M)
#undef M
}
if (!new_rows_num)
return;
for (auto & column : columns)
column = column->filter(filter, -1);
chunk.setColumns(std::move(columns), new_rows_num);
}
}
|