-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathvcollect_iterator.h
More file actions
365 lines (277 loc) · 12.1 KB
/
vcollect_iterator.h
File metadata and controls
365 lines (277 loc) · 12.1 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <list>
#include <memory>
#include <vector>
#include "common/status.h"
#include "storage/iterators.h"
#include "storage/rowset/rowset_reader_context.h"
#include "storage/utils.h"
#ifdef USE_LIBCPP
#include <queue>
#else
#include <ext/pb_ds/priority_queue.hpp>
#endif
#include "core/block/block.h"
#include "storage/rowset/rowset_reader.h"
#include "storage/tablet/tablet_reader.h"
namespace __gnu_pbds {
struct pairing_heap_tag;
} // namespace __gnu_pbds
namespace doris {
class TabletSchema;
class RuntimeProfile;
class VCollectIterator {
public:
// Hold reader point to get reader params
~VCollectIterator();
void init(TabletReader* reader, bool ori_data_overlapping, bool force_merge, bool is_reverse);
Status add_child(const RowSetSplits& rs_splits);
Status build_heap(std::vector<RowsetReaderSharedPtr>& rs_readers);
// Get top row of the heap, nullptr if reach end.
Status current_row(IteratorRowRef* ref) const;
// Read nest order row in Block.
// Returns
// OK when read successfully.
// Status::Error<END_OF_FILE>("") and set *row to nullptr when EOF is reached.
// Others when error happens
Status next(IteratorRowRef* ref);
Status next(Block* block);
bool is_merge() const { return _merge; }
RowLocation current_row_location() { return _inner_iter->current_row_location(); }
Status current_block_row_locations(std::vector<RowLocation>* block_row_locations) {
return _inner_iter->current_block_row_locations(block_row_locations);
}
void update_profile(RuntimeProfile* profile) {
if (_inner_iter != nullptr) {
_inner_iter->update_profile(profile);
}
}
inline bool use_topn_next() const { return _topn_limit > 0; }
private:
// next for topn query
Status _topn_next(Block* block);
class BlockRowPosComparator {
public:
BlockRowPosComparator(MutableBlock* mutable_block,
const std::vector<uint32_t>* compare_columns, bool is_reverse)
: _mutable_block(mutable_block),
_compare_columns(compare_columns),
_is_reverse(is_reverse) {}
bool operator()(const size_t& lpos, const size_t& rpos) const;
private:
const MutableBlock* _mutable_block = nullptr;
const std::vector<uint32_t>* _compare_columns;
// reverse the compare order
const bool _is_reverse = false;
};
// This interface is the actual implementation of the new version of iterator.
// It currently contains two implementations, one is Level0Iterator,
// which only reads data from the rowset reader, and the other is Level1Iterator,
// which can read merged data from multiple LevelIterators through MergeHeap.
// By using Level1Iterator, some rowset readers can be merged in advance and
// then merged with other rowset readers.
class LevelIterator {
public:
LevelIterator(TabletReader* reader)
: _schema(reader->tablet_schema()),
_compare_columns(reader->_reader_context.read_orderby_key_columns) {}
virtual Status init(bool get_data_by_ref = false) = 0;
virtual void init_for_union(bool get_data_by_ref) {}
virtual int64_t version() const = 0;
virtual const IteratorRowRef* current_row_ref() const { return &_ref; }
virtual Status next(IteratorRowRef* ref) = 0;
virtual Status next(Block* block) = 0;
void set_same(bool same) { _ref.is_same = same; }
bool is_same() const { return _ref.is_same; }
virtual ~LevelIterator() = default;
const TabletSchema& tablet_schema() const { return _schema; }
const inline std::vector<uint32_t>* compare_columns() const { return _compare_columns; }
virtual RowLocation current_row_location() = 0;
virtual Status current_block_row_locations(std::vector<RowLocation>* row_location) = 0;
[[nodiscard]] virtual Status ensure_first_row_ref() = 0;
virtual void update_profile(RuntimeProfile* profile) = 0;
protected:
const TabletSchema& _schema;
IteratorRowRef _ref;
std::vector<uint32_t>* _compare_columns = nullptr;
};
// Compare row cursors between multiple merge elements,
// if row cursors equal, compare data version.
class LevelIteratorComparator {
public:
LevelIteratorComparator(int sequence, bool is_reverse)
: _sequence(sequence), _is_reverse(is_reverse) {}
bool operator()(LevelIterator* lhs, LevelIterator* rhs);
private:
int _sequence;
// reverse the compare order
bool _is_reverse = false;
};
#ifdef USE_LIBCPP
using MergeHeap = std::priority_queue<LevelIterator*, std::vector<LevelIterator*>,
LevelIteratorComparator>;
#else
using MergeHeap = __gnu_pbds::priority_queue<LevelIterator*, LevelIteratorComparator,
__gnu_pbds::pairing_heap_tag>;
#endif
// Iterate from rowset reader. This Iterator usually like a leaf node
class Level0Iterator : public LevelIterator {
public:
Level0Iterator(RowsetReaderSharedPtr rs_reader, TabletReader* reader);
~Level0Iterator() override = default;
Status init(bool get_data_by_ref = false) override;
void init_for_union(bool get_data_by_ref) override;
/* For unique and agg, rows is aggregated in block_reader, which access
* first row so we need prepare the first row ref while duplicated
* key does not need it.
*
* Here, we organize a lot state here, e.g. data model, order, data
* overlapping, we should split the iterators in the furure to make the
* logic simple and much more understandable.
*/
[[nodiscard]] Status ensure_first_row_ref() override;
int64_t version() const override;
Status next(IteratorRowRef* ref) override;
Status next(Block* block) override;
RowLocation current_row_location() override;
Status current_block_row_locations(std::vector<RowLocation>* block_row_locations) override;
void update_profile(RuntimeProfile* profile) override {
if (_rs_reader != nullptr) {
_rs_reader->update_profile(profile);
}
}
Status refresh_current_row();
private:
Status _next_by_ref(IteratorRowRef* ref);
bool _is_empty() {
if (_get_data_by_ref) {
return _block_view.empty();
} else {
return _block->rows() == 0;
}
}
bool _current_valid() {
if (_get_data_by_ref) {
return _current < _block_view.size();
} else {
return _ref.row_pos < _block->rows();
}
}
void _reset() {
if (_get_data_by_ref) {
_block_view.clear();
_ref.reset();
_current = 0;
} else {
_ref.is_same = false;
_ref.row_pos = 0;
_block->clear_column_data();
}
}
Status _refresh() {
if (_get_data_by_ref) {
return _rs_reader->next_batch(&_block_view);
} else {
if (_is_merge_iterator) {
_row_is_same.clear();
BlockWithSameBit block_with_same_bit {.block = _block.get(),
.same_bit = _row_is_same};
return _rs_reader->next_batch(&block_with_same_bit);
} else {
return _rs_reader->next_batch(_block.get());
}
}
}
RowsetReaderSharedPtr _rs_reader;
TabletReader* _reader = nullptr;
std::shared_ptr<Block> _block;
int _current;
BlockView _block_view;
std::vector<RowLocation> _block_row_locations;
std::vector<bool> _row_is_same;
bool _is_merge_iterator = false;
bool _get_data_by_ref = false;
};
// Iterate from LevelIterators (maybe Level0Iterators or Level1Iterator or mixed)
class Level1Iterator : public LevelIterator {
public:
Level1Iterator(std::list<std::unique_ptr<LevelIterator>> children, TabletReader* reader,
bool merge, bool is_reverse, bool skip_same);
Status init(bool get_data_by_ref = false) override;
int64_t version() const override;
Status next(IteratorRowRef* ref) override;
Status next(Block* block) override;
RowLocation current_row_location() override { return _cur_child->current_row_location(); }
Status current_block_row_locations(std::vector<RowLocation>* block_row_locations) override;
[[nodiscard]] Status ensure_first_row_ref() override;
~Level1Iterator() override;
void update_profile(RuntimeProfile* profile) override {
if (_cur_child != nullptr) {
_cur_child->update_profile(profile);
}
}
void init_level0_iterators_for_union();
private:
Status _merge_next(IteratorRowRef* ref);
Status _normal_next(IteratorRowRef* ref);
Status _normal_next(Block* block);
Status _merge_next(Block* block);
// Each LevelIterator corresponds to a rowset reader,
// it will be cleared after '_heap' has been initialized when '_merge == true'.
std::list<std::unique_ptr<LevelIterator>> _children;
// point to the Level0Iterator containing the next output row.
// null when VCollectIterator hasn't been initialized or reaches EOF.
std::unique_ptr<LevelIterator> _cur_child;
TabletReader* _reader = nullptr;
// when `_merge == true`, rowset reader returns ordered rows and VCollectIterator uses a priority queue to merge
// sort them. The output of VCollectIterator is also ordered.
// When `_merge == false`, rowset reader returns *partial* ordered rows. VCollectIterator simply returns all rows
// from the first rowset, the second rowset, .., the last rowset. The output of CollectorIterator is also
// *partially* ordered.
bool _merge = true;
// reverse the compare order
bool _is_reverse = false;
bool _skip_same;
// used when `_merge == true`
std::unique_ptr<MergeHeap> _heap;
std::vector<RowLocation> _block_row_locations;
};
std::unique_ptr<LevelIterator> _inner_iter;
// Each LevelIterator corresponds to a rowset reader,
// it will be cleared after '_inner_iter' has been initialized.
std::list<std::unique_ptr<LevelIterator>> _children;
bool _merge = true;
// reverse the compare order
bool _is_reverse = false;
// for topn next
size_t _topn_limit = 0;
bool _topn_eof = false;
std::vector<RowSetSplits> _rs_splits;
// General limit pushdown for DUP_KEYS and UNIQUE_KEYS with MOW (non-merge path).
// When > 0, VCollectIterator will stop reading after returning this many rows.
int64_t _general_read_limit = -1;
// Number of rows already returned to the caller.
int64_t _general_rows_returned = 0;
// Hold reader point to access read params, such as fetch conditions.
TabletReader* _reader = nullptr;
bool _skip_same;
};
} // namespace doris