forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Sort] Support merge sort in ExchangeNode (apache#19)
Change-Id: I5f15771153fe8e982c78c581c42d5b1e25ab543f Co-authored-by: lihaopeng <[email protected]>
- Loading branch information
Showing
9 changed files
with
342 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// 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. | ||
|
||
#include "vec/runtime/vsorted_run_merger.h" | ||
|
||
#include <vector> | ||
|
||
#include "exprs/expr.h" | ||
#include "runtime/descriptors.h" | ||
#include "runtime/row_batch.h" | ||
#include "runtime/sorter.h" | ||
#include "runtime/tuple_row.h" | ||
#include "util/debug_util.h" | ||
#include "util/defer_op.h" | ||
#include "util/runtime_profile.h" | ||
|
||
using std::vector; | ||
|
||
namespace doris::vectorized { | ||
|
||
VSortedRunMerger::VSortedRunMerger(const std::vector<VExprContext *>& ordering_expr, const std::vector<bool>& is_asc_order, | ||
const std::vector<bool>& nulls_first, const size_t batch_size, int64_t limit, size_t offset, RuntimeProfile* profile) | ||
:_ordering_expr(ordering_expr), _is_asc_order(is_asc_order), _nulls_first(nulls_first), _batch_size(batch_size), | ||
_limit(limit), _offset(offset){ | ||
_get_next_timer = ADD_TIMER(profile, "MergeGetNext"); | ||
_get_next_batch_timer = ADD_TIMER(profile, "MergeGetNextBatch"); | ||
} | ||
|
||
Status VSortedRunMerger::prepare(const vector<BlockSupplier>& input_runs, bool parallel) { | ||
for (const auto &supplier : input_runs) { | ||
_cursors.emplace_back(supplier, _ordering_expr, _is_asc_order, _nulls_first); | ||
} | ||
|
||
for (size_t i = 0; i < _cursors.size(); ++i) { | ||
_priority_queue.push(SortCursor(&_cursors[i])); | ||
} | ||
|
||
for (const auto& cursor : _cursors) { | ||
if (!cursor._is_eof) { | ||
_empty_block = cursor.create_empty_blocks(); | ||
break; | ||
} | ||
} | ||
|
||
return Status::OK(); | ||
} | ||
|
||
Status VSortedRunMerger::get_next(Block* output_block, bool* eos) { | ||
// Only have one receive data queue of data, no need to do merge and | ||
// copy the data of block. | ||
// return the data in receive data directly | ||
if (_priority_queue.size() == 1) { | ||
auto current = _priority_queue.top(); | ||
while (_offset != 0 && current->block_ptr() != nullptr) { | ||
if (_offset >= current->rows - current->pos) { | ||
_offset -= (current->rows - current->pos); | ||
current->has_next_block(); | ||
} else { | ||
current->pos += _offset; | ||
_offset = 0; | ||
} | ||
} | ||
|
||
if (current->isFirst()) { | ||
if (current->block_ptr() != nullptr) { | ||
current->block_ptr()->swap(*output_block); | ||
*eos = !current->has_next_block(); | ||
} else { | ||
*eos = true; | ||
} | ||
} else { | ||
if (current->block_ptr() != nullptr) { | ||
for (int i = 0; i < current->all_columns.size(); i++) { | ||
auto& column_with_type = current->block_ptr()->getByPosition(i); | ||
column_with_type.column = column_with_type.column->cut(current->pos, | ||
current->rows - current->pos); | ||
} | ||
current->block_ptr()->swap(*output_block); | ||
*eos = !current->has_next_block(); | ||
} else { | ||
*eos = true; | ||
} | ||
} | ||
} else { | ||
size_t num_columns = _empty_block.columns(); | ||
MutableColumns merged_columns = _empty_block.cloneEmptyColumns(); | ||
/// TODO: reserve (in each column) | ||
|
||
/// Take rows from queue in right order and push to 'merged'. | ||
size_t merged_rows = 0; | ||
while (!_priority_queue.empty()) { | ||
auto current = _priority_queue.top(); | ||
_priority_queue.pop(); | ||
|
||
if (_offset > 0) { | ||
_offset--; | ||
} else { | ||
for (size_t i = 0; i < num_columns; ++i) | ||
merged_columns[i]->insertFrom(*current->all_columns[i], current->pos); | ||
++merged_rows; | ||
} | ||
next_heap(current); | ||
if (merged_rows == _batch_size) | ||
break; | ||
} | ||
|
||
if (merged_rows == 0) { | ||
*eos = true; | ||
return Status::OK(); | ||
} | ||
|
||
Block merge_block = _empty_block.cloneWithColumns(std::move(merged_columns)); | ||
merge_block.swap(*output_block); | ||
} | ||
|
||
_num_rows_returned += output_block->rows(); | ||
if (_limit != -1 && _num_rows_returned >= _limit) { | ||
output_block->set_num_rows(output_block->rows() - (_num_rows_returned - _limit)); | ||
*eos = true; | ||
} | ||
return Status::OK(); | ||
} | ||
|
||
void VSortedRunMerger::next_heap(SortCursor& current) { | ||
if (!current->isLast()) { | ||
current->next(); | ||
_priority_queue.push(current); | ||
} else if (current->has_next_block()) { | ||
_priority_queue.push(current); | ||
} | ||
} | ||
|
||
} // namespace doris |
Oops, something went wrong.