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
|
#include <Processors/Transforms/CreatingSetsTransform.h>
#include <Processors/Executors/PushingPipelineExecutor.h>
#include <Processors/Sinks/SinkToStorage.h>
#include <Interpreters/Set.h>
#include <Interpreters/IJoin.h>
#include <Interpreters/Context.h>
#include <Storages/IStorage.h>
#include <Common/logger_useful.h>
#include <iomanip>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
extern const int SET_SIZE_LIMIT_EXCEEDED;
}
CreatingSetsTransform::~CreatingSetsTransform() = default;
CreatingSetsTransform::CreatingSetsTransform(
Block in_header_,
Block out_header_,
SetAndKeyPtr set_and_key_,
StoragePtr external_table_,
SizeLimits network_transfer_limits_,
PreparedSetsCachePtr prepared_sets_cache_)
: IAccumulatingTransform(std::move(in_header_), std::move(out_header_))
, set_and_key(std::move(set_and_key_))
, external_table(std::move(external_table_))
, network_transfer_limits(std::move(network_transfer_limits_))
, prepared_sets_cache(std::move(prepared_sets_cache_))
{
}
void CreatingSetsTransform::work()
{
if (!is_initialized)
init();
if (done_with_set && done_with_table)
{
finishConsume();
input.close();
}
IAccumulatingTransform::work();
}
void CreatingSetsTransform::startSubquery()
{
/// Lookup the set in the cache if we don't need to build table.
if (prepared_sets_cache && !external_table)
{
/// Try to find the set in the cache and wait for it to be built.
/// Retry if the set from cache fails to be built.
while (true)
{
auto from_cache = prepared_sets_cache->findOrPromiseToBuild(set_and_key->key);
if (from_cache.index() == 0)
{
LOG_TRACE(log, "Building set, key: {}", set_and_key->key);
promise_to_build = std::move(std::get<0>(from_cache));
}
else
{
LOG_TRACE(log, "Waiting for set to be build by another thread, key: {}", set_and_key->key);
SharedSet set_built_by_another_thread = std::move(std::get<1>(from_cache));
const SetPtr & ready_set = set_built_by_another_thread.get();
if (!ready_set)
{
LOG_TRACE(log, "Failed to use set from cache, key: {}", set_and_key->key);
continue;
}
set_and_key->set = ready_set;
done_with_set = true;
set_from_cache = true;
}
break;
}
}
if (set_and_key->set && !set_from_cache)
LOG_TRACE(log, "Creating set, key: {}", set_and_key->key);
if (external_table)
LOG_TRACE(log, "Filling temporary table.");
if (external_table)
/// TODO: make via port
table_out = QueryPipeline(external_table->write({}, external_table->getInMemoryMetadataPtr(), nullptr, /*async_insert=*/false));
done_with_set = !set_and_key->set || set_from_cache;
done_with_table = !external_table;
if ((done_with_set && !set_from_cache) && done_with_table)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Logical error: nothing to do with subquery");
if (table_out.initialized())
{
executor = std::make_unique<PushingPipelineExecutor>(table_out);
executor->start();
}
}
void CreatingSetsTransform::finishSubquery()
{
auto seconds = watch.elapsedNanoseconds() / 1e9;
if (set_from_cache)
{
LOG_DEBUG(log, "Got set from cache in {} sec.", seconds);
}
else if (read_rows != 0)
{
if (set_and_key->set)
LOG_DEBUG(log, "Created Set with {} entries from {} rows in {} sec.", set_and_key->set->getTotalRowCount(), read_rows, seconds);
if (external_table)
LOG_DEBUG(log, "Created Table with {} rows in {} sec.", read_rows, seconds);
}
else
{
LOG_DEBUG(log, "Subquery has empty result.");
}
}
void CreatingSetsTransform::init()
{
is_initialized = true;
watch.restart();
startSubquery();
}
void CreatingSetsTransform::consume(Chunk chunk)
{
read_rows += chunk.getNumRows();
auto block = getInputPort().getHeader().cloneWithColumns(chunk.detachColumns());
if (!done_with_set)
{
if (!set_and_key->set->insertFromBlock(block.getColumnsWithTypeAndName()))
done_with_set = true;
}
if (!done_with_table)
{
block = materializeBlock(block);
executor->push(block);
rows_to_transfer += block.rows();
bytes_to_transfer += block.bytes();
if (!network_transfer_limits.check(rows_to_transfer, bytes_to_transfer, "IN/JOIN external table",
ErrorCodes::SET_SIZE_LIMIT_EXCEEDED))
done_with_table = true;
}
if (done_with_set && done_with_table)
finishConsume();
}
Chunk CreatingSetsTransform::generate()
{
if (set_and_key->set && !set_from_cache)
{
set_and_key->set->finishInsert();
if (promise_to_build)
promise_to_build->set_value(set_and_key->set);
}
if (table_out.initialized())
{
executor->finish();
executor.reset();
table_out.reset();
}
finishSubquery();
return {};
}
}
|