forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISource.cpp
More file actions
160 lines (127 loc) · 3.44 KB
/
Copy pathISource.cpp
File metadata and controls
160 lines (127 loc) · 3.44 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
#include <Processors/ISource.h>
#include <QueryPipeline/StreamLocalLimits.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NOT_IMPLEMENTED;
}
ISource::~ISource() = default;
ISource::ISource(SharedHeader header, bool enable_auto_progress)
: IProcessor({}, {std::move(header)})
, auto_progress(enable_auto_progress)
, output(outputs.front())
{
}
void ISource::cancel(CancelReason) noexcept
{
bool already_cancelled = is_cancelled.exchange(true, std::memory_order_acq_rel);
if (already_cancelled)
return;
onCancel();
}
ISource::Status ISource::prepare()
{
if (finished)
{
output.finish();
return Status::Finished;
}
/// Check can output.
if (output.isFinished())
return Status::Finished;
if (!output.canPush())
return Status::PortFull;
if (!has_input)
return Status::Ready;
output.pushData(std::move(current_chunk));
has_input = false;
if (isCancelled())
{
output.finish();
return Status::Finished;
}
if (got_exception)
{
output.finish();
return Status::Finished;
}
/// Now, we pushed to output, and it must be full.
return Status::PortFull;
}
void ISource::setStorageLimits(const std::shared_ptr<const StorageLimitsList> & storage_limits_)
{
storage_limits = storage_limits_;
}
void ISource::progress(size_t read_rows, size_t read_bytes)
{
//std::cerr << "========= Progress " << read_rows << " from " << getName() << std::endl << StackTrace().toString() << std::endl;
read_progress_was_set = true;
std::lock_guard lock(read_progress_mutex);
read_progress.read_rows += read_rows;
read_progress.read_bytes += read_bytes;
}
std::optional<ISource::ReadProgress> ISource::getReadProgress()
{
std::lock_guard lock(read_progress_mutex);
if (finished && read_progress.read_bytes == 0 && read_progress.total_rows_approx == 0)
return {};
ReadProgressCounters res_progress;
std::swap(read_progress, res_progress);
if (storage_limits)
return ReadProgress{res_progress, *storage_limits};
static StorageLimitsList empty_limits;
return ReadProgress{res_progress, empty_limits};
}
void ISource::addTotalRowsApprox(size_t value)
{
std::lock_guard lock(read_progress_mutex);
read_progress.total_rows_approx += value;
}
void ISource::addTotalBytes(size_t value)
{
std::lock_guard lock(read_progress_mutex);
read_progress.total_bytes += value;
}
void ISource::work()
{
try
{
if (finished)
return;
read_progress_was_set = false;
if (auto chunk = tryGenerate())
{
current_chunk.chunk = std::move(*chunk);
if (current_chunk.chunk)
{
has_input = true;
if (auto_progress && !read_progress_was_set)
progress(current_chunk.chunk.getNumRows(), current_chunk.chunk.bytes());
}
}
else
finished = true;
if (finished)
onFinish();
}
catch (...)
{
got_exception = true;
if (!std::exchange(finished, true))
onFinish();
throw;
}
}
Chunk ISource::generate()
{
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "generate is not implemented for {}", getName());
}
std::optional<Chunk> ISource::tryGenerate()
{
auto chunk = generate();
if (!chunk)
return std::nullopt;
return chunk;
}
}