Repository navigation
Expand file tree
/
Copy pathIOutputFormat.cpp
More file actions
243 lines (195 loc) · 5.64 KB
/
Copy pathIOutputFormat.cpp
File metadata and controls
243 lines (195 loc) · 5.64 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <Columns/IColumn.h>
#include <Core/Block.h>
#include <IO/WriteBuffer.h>
#include <IO/WriteBufferDecorator.h>
#include <Processors/Formats/IOutputFormat.h>
#include <Processors/Port.h>
#include <Common/FailPoint.h>
#include <base/sleep.h>
namespace DB
{
namespace FailPoints
{
extern const char output_format_sleep_on_progress[];
}
IOutputFormat::IOutputFormat(SharedHeader header_, WriteBuffer & out_)
: IProcessor({header_, header_, header_}, {}), out(out_)
{
}
IOutputFormat::Status IOutputFormat::prepare()
{
if (has_input)
return Status::Ready;
for (auto kind : {Main, Totals, Extremes})
{
auto & input = getPort(kind);
if (kind != Main && !input.isConnected())
continue;
if (input.isFinished())
continue;
input.setNeeded();
if (!input.hasData())
return Status::NeedData;
current_chunk = input.pull(true);
current_block_kind = kind;
has_input = true;
return Status::Ready;
}
finished = true;
if (!finalized)
return Status::Ready;
return Status::Finished;
}
static Chunk prepareTotals(Chunk chunk)
{
if (!chunk.hasRows())
return {};
if (chunk.getNumRows() > 1)
{
/// This may happen if something like ARRAY JOIN was executed on totals.
/// Skip rows except the first one.
auto columns = chunk.detachColumns();
for (auto & column : columns)
column = column->cut(0, 1);
chunk.setColumns(std::move(columns), 1);
}
return chunk;
}
void IOutputFormat::work()
{
std::lock_guard lock(writing_mutex);
if (has_progress_update_to_write)
{
writeProgress(statistics.progress);
has_progress_update_to_write = false;
}
writePrefixIfNeeded();
if (finished && !finalized)
{
if (rows_before_limit_counter && rows_before_limit_counter->hasAppliedStep())
setRowsBeforeLimit(rows_before_limit_counter->get());
if (rows_before_aggregation_counter && rows_before_aggregation_counter->hasAppliedStep())
setRowsBeforeAggregation(rows_before_aggregation_counter->get());
finalizeUnlocked();
return;
}
switch (current_block_kind)
{
case Main:
result_rows += current_chunk.getNumRows();
result_bytes += current_chunk.allocatedBytes();
consume(std::move(current_chunk));
break;
case Totals:
writeSuffixIfNeeded();
if (auto totals = prepareTotals(std::move(current_chunk)))
{
consumeTotals(std::move(totals));
are_totals_written = true;
}
break;
case Extremes:
writeSuffixIfNeeded();
consumeExtremes(std::move(current_chunk));
break;
}
if (auto_flush)
flushImpl();
has_input = false;
}
void IOutputFormat::flushImpl()
{
out.next();
/// If output is a compressed buffer, we will flush the compressed chunk as well.
if (auto * out_with_nested = dynamic_cast<WriteBufferWithOwnMemoryDecorator *>(&out))
out_with_nested->getNestedBuffer()->next();
}
void IOutputFormat::flush()
{
std::lock_guard lock(writing_mutex);
flushImpl();
}
void IOutputFormat::write(const Block & block)
{
std::lock_guard lock(writing_mutex);
if (has_progress_update_to_write)
{
writeProgress(statistics.progress);
has_progress_update_to_write = false;
}
writePrefixIfNeeded();
consume(Chunk(block.getColumns(), block.rows()));
if (auto_flush)
flushImpl();
}
void IOutputFormat::finalizeUnlocked()
{
if (finalized)
return;
writePrefixIfNeeded();
if (has_progress_update_to_write)
{
writeProgress(statistics.progress);
has_progress_update_to_write = false;
}
writeSuffixIfNeeded();
finalizeImpl();
if (auto_flush)
flushImpl();
finalizeBuffers();
finalized = true;
}
void IOutputFormat::finalize()
{
std::lock_guard lock(writing_mutex);
finalizeUnlocked();
}
void IOutputFormat::setTotals(const Block & totals)
{
std::lock_guard lock(writing_mutex);
writeSuffixIfNeeded();
consumeTotals(Chunk(totals.getColumns(), totals.rows()));
are_totals_written = true;
}
void IOutputFormat::setExtremes(const Block & extremes)
{
std::lock_guard lock(writing_mutex);
writeSuffixIfNeeded();
consumeExtremes(Chunk(extremes.getColumns(), extremes.rows()));
}
void IOutputFormat::onProgress(const Progress & progress)
{
fiu_do_on(
FailPoints::output_format_sleep_on_progress,
{
sleepForMilliseconds(100);
});
statistics.progress.incrementPiecewiseAtomically(progress);
UInt64 elapsed_ns = statistics.watch.elapsedNanoseconds();
statistics.progress.elapsed_ns = elapsed_ns;
if (writesProgressConcurrently())
{
has_progress_update_to_write = true;
/// Do not write progress too frequently.
if (elapsed_ns >= prev_progress_write_ns + 1000 * progress_write_frequency_us)
{
std::unique_lock lock(writing_mutex, std::try_to_lock);
if (lock && has_progress_update_to_write && !finalized)
{
writeProgress(statistics.progress);
flushImpl();
prev_progress_write_ns = elapsed_ns;
has_progress_update_to_write = false;
}
}
}
}
void IOutputFormat::setProgress(Progress progress)
{
statistics.progress = std::move(progress);
}
InputPort & IOutputFormat::getPort(PortKind kind)
{
return *std::next(inputs.begin(), kind);
}
}