1+ // Licensed to the Apache Software Foundation (ASF) under one
2+ // or more contributor license agreements. See the NOTICE file
3+ // distributed with this work for additional information
4+ // regarding copyright ownership. The ASF licenses this file
5+ // to you under the Apache License, Version 2.0 (the
6+ // "License"); you may not use this file except in compliance
7+ // with the License. You may obtain a copy of the License at
8+ //
9+ // http://www.apache.org/licenses/LICENSE-2.0
10+ //
11+ // Unless required by applicable law or agreed to in writing,
12+ // software distributed under the License is distributed on an
13+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+ // KIND, either express or implied. See the License for the
15+ // specific language governing permissions and limitations
16+ // under the License.
17+
18+ #include " vec/runtime/vsorted_run_merger.h"
19+
20+ #include < vector>
21+
22+ #include " exprs/expr.h"
23+ #include " runtime/descriptors.h"
24+ #include " runtime/row_batch.h"
25+ #include " runtime/sorter.h"
26+ #include " runtime/tuple_row.h"
27+ #include " util/debug_util.h"
28+ #include " util/defer_op.h"
29+ #include " util/runtime_profile.h"
30+
31+ using std::vector;
32+
33+ namespace doris ::vectorized {
34+
35+ VSortedRunMerger::VSortedRunMerger (const std::vector<VExprContext *>& ordering_expr, const std::vector<bool >& is_asc_order,
36+ const std::vector<bool >& nulls_first, const size_t batch_size, int64_t limit, size_t offset, RuntimeProfile* profile)
37+ :_ordering_expr(ordering_expr), _is_asc_order(is_asc_order), _nulls_first(nulls_first), _batch_size(batch_size),
38+ _limit (limit), _offset(offset){
39+ _get_next_timer = ADD_TIMER (profile, " MergeGetNext" );
40+ _get_next_batch_timer = ADD_TIMER (profile, " MergeGetNextBatch" );
41+ }
42+
43+ Status VSortedRunMerger::prepare (const vector<BlockSupplier>& input_runs, bool parallel) {
44+ for (const auto &supplier : input_runs) {
45+ _cursors.emplace_back (supplier, _ordering_expr, _is_asc_order, _nulls_first);
46+ }
47+
48+ for (size_t i = 0 ; i < _cursors.size (); ++i) {
49+ _priority_queue.push (SortCursor (&_cursors[i]));
50+ }
51+
52+ for (const auto & cursor : _cursors) {
53+ if (!cursor._is_eof ) {
54+ _empty_block = cursor.create_empty_blocks ();
55+ break ;
56+ }
57+ }
58+
59+ return Status::OK ();
60+ }
61+
62+ Status VSortedRunMerger::get_next (Block* output_block, bool * eos) {
63+ // Only have one receive data queue of data, no need to do merge and
64+ // copy the data of block.
65+ // return the data in receive data directly
66+ if (_priority_queue.size () == 1 ) {
67+ auto current = _priority_queue.top ();
68+ while (_offset != 0 && current->block_ptr () != nullptr ) {
69+ if (_offset >= current->rows - current->pos ) {
70+ _offset -= (current->rows - current->pos );
71+ current->has_next_block ();
72+ } else {
73+ current->pos += _offset;
74+ _offset = 0 ;
75+ }
76+ }
77+
78+ if (current->isFirst ()) {
79+ if (current->block_ptr () != nullptr ) {
80+ current->block_ptr ()->swap (*output_block);
81+ *eos = !current->has_next_block ();
82+ } else {
83+ *eos = true ;
84+ }
85+ } else {
86+ if (current->block_ptr () != nullptr ) {
87+ for (int i = 0 ; i < current->all_columns .size (); i++) {
88+ auto & column_with_type = current->block_ptr ()->getByPosition (i);
89+ column_with_type.column = column_with_type.column ->cut (current->pos ,
90+ current->rows - current->pos );
91+ }
92+ current->block_ptr ()->swap (*output_block);
93+ *eos = !current->has_next_block ();
94+ } else {
95+ *eos = true ;
96+ }
97+ }
98+ } else {
99+ size_t num_columns = _empty_block.columns ();
100+ MutableColumns merged_columns = _empty_block.cloneEmptyColumns ();
101+ // / TODO: reserve (in each column)
102+
103+ // / Take rows from queue in right order and push to 'merged'.
104+ size_t merged_rows = 0 ;
105+ while (!_priority_queue.empty ()) {
106+ auto current = _priority_queue.top ();
107+ _priority_queue.pop ();
108+
109+ if (_offset > 0 ) {
110+ _offset--;
111+ } else {
112+ for (size_t i = 0 ; i < num_columns; ++i)
113+ merged_columns[i]->insertFrom (*current->all_columns [i], current->pos );
114+ ++merged_rows;
115+ }
116+ next_heap (current);
117+ if (merged_rows == _batch_size)
118+ break ;
119+ }
120+
121+ if (merged_rows == 0 ) {
122+ *eos = true ;
123+ return Status::OK ();
124+ }
125+
126+ Block merge_block = _empty_block.cloneWithColumns (std::move (merged_columns));
127+ merge_block.swap (*output_block);
128+ }
129+
130+ _num_rows_returned += output_block->rows ();
131+ if (_limit != -1 && _num_rows_returned >= _limit) {
132+ output_block->set_num_rows (output_block->rows () - (_num_rows_returned - _limit));
133+ *eos = true ;
134+ }
135+ return Status::OK ();
136+ }
137+
138+ void VSortedRunMerger::next_heap (SortCursor& current) {
139+ if (!current->isLast ()) {
140+ current->next ();
141+ _priority_queue.push (current);
142+ } else if (current->has_next_block ()) {
143+ _priority_queue.push (current);
144+ }
145+ }
146+
147+ } // namespace doris
0 commit comments