HappenLee commented on code in PR #12700: URL: https://github.com/apache/doris/pull/12700#discussion_r973839470
########## be/src/vec/columns/column.h: ########## @@ -402,6 +402,10 @@ class IColumn : public COW<IColumn> { virtual int compare_at(size_t n, size_t m, const IColumn& rhs, int nan_direction_hint) const = 0; + virtual void compare_internal(size_t rhs_row_id, const IColumn& rhs, int nan_direction_hint, Review Comment: Add a commen for the function `compare_internal` ########## be/src/vec/columns/column_decimal.cpp: ########## @@ -381,6 +381,32 @@ void ColumnDecimal<T>::sort_column(const ColumnSorter* sorter, EqualFlags& flags sorter->template sort_column(static_cast<const Self&>(*this), flags, perms, range, last_column); } +template <typename T> +void ColumnDecimal<T>::compare_internal(size_t rhs_row_id, const IColumn& rhs, + int nan_direction_hint, int direction, + std::vector<uint8>& cmp_res, + uint8* __restrict filter) const { + auto sz = this->size(); + DCHECK(cmp_res.size() == sz); + const auto& cmp_base = assert_cast<const ColumnDecimal<T>&>(rhs).get_data()[rhs_row_id]; + + size_t begin = simd::find_zero(cmp_res, 0); + while (begin < sz) { + size_t end = simd::find_nonzero(cmp_res, begin + 1); Review Comment: we maybe need a better name for `find_nonzero`, find_one ? ########## be/src/vec/core/sort_cursor.h: ########## @@ -21,43 +21,123 @@ #pragma once #include "vec/columns/column.h" -#include "vec/columns/column_string.h" -#include "vec/common/assert_cast.h" -#include "vec/common/typeid_cast.h" #include "vec/core/block.h" -#include "vec/core/column_numbers.h" #include "vec/core/sort_description.h" #include "vec/exprs/vexpr_context.h" -#include "vec/runtime/vdata_stream_recvr.h" namespace doris::vectorized { +struct HeapSortCursorBlockView { +public: + Block block; + ColumnRawPtrs sort_columns; + SortDescription& desc; + + HeapSortCursorBlockView(Block&& cur_block, SortDescription& sort_desc) + : block(cur_block), desc(sort_desc) { + _reset(); + } + + void filter_block(IColumn::Filter& filter) { + Block::filter_block_internal(&block, filter, block.columns()); + _reset(); + } + +private: + void _reset() { + sort_columns.clear(); + auto columns = block.get_columns(); + for (size_t j = 0, size = desc.size(); j < size; ++j) { + auto& column_desc = desc[j]; + size_t column_number = !column_desc.column_name.empty() + ? block.get_position_by_name(column_desc.column_name) + : column_desc.column_number; + sort_columns.push_back(columns[column_number].get()); + } + } +}; + +struct HeapSortCursorImpl { +public: + HeapSortCursorImpl() : _row_id(0), _block_view(nullptr) {} + HeapSortCursorImpl(int row_id, std::shared_ptr<HeapSortCursorBlockView> block_view) + : _row_id(row_id), _block_view(block_view) {} + + HeapSortCursorImpl(const HeapSortCursorImpl& other) { + _row_id = other._row_id; + _block_view = other._block_view; + } + + HeapSortCursorImpl(HeapSortCursorImpl&& other) { + _row_id = other._row_id; + _block_view = std::move(other._block_view); + } + + HeapSortCursorImpl& operator=(HeapSortCursorImpl&& other) { + std::swap(_row_id, other._row_id); + std::swap(_block_view, other._block_view); + return *this; + } + + ~HeapSortCursorImpl() = default; + const size_t row_id() const { return _row_id; } + + const ColumnRawPtrs& sort_columns() const { return _block_view->sort_columns; } + + const Block* block() const { return &_block_view->block; } + + const SortDescription& sort_desc() const { return _block_view->desc; } + + bool operator<(const HeapSortCursorImpl& rhs) const { + for (size_t i = 0; i < sort_desc().size(); ++i) { + int direction = sort_desc()[i].direction; + int nulls_direction = sort_desc()[i].nulls_direction; + int res = direction * sort_columns()[i]->compare_at(row_id(), rhs.row_id(), + *(rhs.sort_columns()[i]), + nulls_direction); + // ASC: direction == 1. If bigger, res > 0. So we return true. + if (res < 0) { + return true; + } + if (res > 0) { + return false; + } + } + return false; + } + +private: + size_t _row_id; + std::shared_ptr<HeapSortCursorBlockView> _block_view; Review Comment: Smart pointers introduce additional memory and performance overhead, consider whether we can circumvent its use with other mechanisms to gain performance gains ########## be/src/vec/common/sort/topn_sorter.cpp: ########## @@ -0,0 +1,156 @@ +// 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/common/sort/topn_sorter.h" + +namespace doris::vectorized { + +TopNSorter::TopNSorter(VSortExecExprs& vsort_exec_exprs, int limit, int64_t offset, + ObjectPool* pool, std::vector<bool>& is_asc_order, + std::vector<bool>& nulls_first, const RowDescriptor& row_desc) + : Sorter(vsort_exec_exprs, limit, offset, pool, is_asc_order, nulls_first), + _heap_size(limit + offset), + _heap(std::make_unique<SortingHeap>()), + _topn_filter_rows(0) {} + +Status TopNSorter::append_block(Block* block, bool* mem_reuse) { + DCHECK(block->rows() > 0); + { + if (_vsort_exec_exprs.need_materialize_tuple()) { + auto output_tuple_expr_ctxs = _vsort_exec_exprs.sort_tuple_slot_expr_ctxs(); + std::vector<int> valid_column_ids(output_tuple_expr_ctxs.size()); + for (int i = 0; i < output_tuple_expr_ctxs.size(); ++i) { + RETURN_IF_ERROR(output_tuple_expr_ctxs[i]->execute(block, &valid_column_ids[i])); + } + + Block new_block; + for (auto column_id : valid_column_ids) { + new_block.insert(block->get_by_position(column_id)); + } + block->swap(new_block); + } + + // TODO: could we init `_sort_description` only once? + _sort_description.resize(_vsort_exec_exprs.lhs_ordering_expr_ctxs().size()); Review Comment: maybe we just need keep `column_number`, other value` _is_asc_order` and `null_firs`t can be reuse ########## be/src/vec/common/sort/topn_sorter.cpp: ########## @@ -0,0 +1,156 @@ +// 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/common/sort/topn_sorter.h" + +namespace doris::vectorized { + +TopNSorter::TopNSorter(VSortExecExprs& vsort_exec_exprs, int limit, int64_t offset, + ObjectPool* pool, std::vector<bool>& is_asc_order, + std::vector<bool>& nulls_first, const RowDescriptor& row_desc) + : Sorter(vsort_exec_exprs, limit, offset, pool, is_asc_order, nulls_first), + _heap_size(limit + offset), + _heap(std::make_unique<SortingHeap>()), + _topn_filter_rows(0) {} + +Status TopNSorter::append_block(Block* block, bool* mem_reuse) { + DCHECK(block->rows() > 0); + { + if (_vsort_exec_exprs.need_materialize_tuple()) { + auto output_tuple_expr_ctxs = _vsort_exec_exprs.sort_tuple_slot_expr_ctxs(); + std::vector<int> valid_column_ids(output_tuple_expr_ctxs.size()); + for (int i = 0; i < output_tuple_expr_ctxs.size(); ++i) { + RETURN_IF_ERROR(output_tuple_expr_ctxs[i]->execute(block, &valid_column_ids[i])); + } + + Block new_block; + for (auto column_id : valid_column_ids) { + new_block.insert(block->get_by_position(column_id)); + } + block->swap(new_block); + } + + // TODO: could we init `_sort_description` only once? + _sort_description.resize(_vsort_exec_exprs.lhs_ordering_expr_ctxs().size()); + for (int i = 0; i < _sort_description.size(); i++) { + const auto& ordering_expr = _vsort_exec_exprs.lhs_ordering_expr_ctxs()[i]; + RETURN_IF_ERROR(ordering_expr->execute(block, &_sort_description[i].column_number)); + + _sort_description[i].direction = _is_asc_order[i] ? 1 : -1; + _sort_description[i].nulls_direction = _nulls_first[i] ? -_sort_description[i].direction + : _sort_description[i].direction; + } + } + Block tmp_block = block->clone_empty(); + tmp_block.swap(*block); + std::shared_ptr<HeapSortCursorBlockView> block_view( + new HeapSortCursorBlockView(std::move(tmp_block), _sort_description)); + size_t num_rows = tmp_block.rows(); + if (_heap_size == _heap->size()) { + { + SCOPED_TIMER(_topn_filter_timer); + _do_filter(block_view.get(), num_rows); + } + size_t remain_rows = block_view->block.rows(); + _topn_filter_rows += (num_rows - remain_rows); + COUNTER_SET(_topn_filter_rows_counter, _topn_filter_rows); + for (size_t i = 0; i < remain_rows; ++i) { + HeapSortCursorImpl cursor(i, block_view); + _heap->replace_top_if_less(std::move(cursor)); + } + } else { + size_t free_slots = std::min<size_t>(_heap_size - _heap->size(), num_rows); + + size_t i = 0; + for (; i < free_slots; ++i) { + HeapSortCursorImpl cursor(i, block_view); + _heap->push(std::move(cursor)); + } + + for (; i < num_rows; ++i) { + HeapSortCursorImpl cursor(i, block_view); + _heap->replace_top_if_less(std::move(cursor)); + } + } + return Status::OK(); +} + +Status TopNSorter::prepare_for_read() { + if (!_heap->empty() && _heap->size() > _offset) { + auto top = _heap->top(); Review Comment: const auto & ########## be/src/vec/common/sort/topn_sorter.cpp: ########## @@ -0,0 +1,156 @@ +// 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/common/sort/topn_sorter.h" + +namespace doris::vectorized { + +TopNSorter::TopNSorter(VSortExecExprs& vsort_exec_exprs, int limit, int64_t offset, + ObjectPool* pool, std::vector<bool>& is_asc_order, + std::vector<bool>& nulls_first, const RowDescriptor& row_desc) + : Sorter(vsort_exec_exprs, limit, offset, pool, is_asc_order, nulls_first), + _heap_size(limit + offset), + _heap(std::make_unique<SortingHeap>()), + _topn_filter_rows(0) {} + +Status TopNSorter::append_block(Block* block, bool* mem_reuse) { + DCHECK(block->rows() > 0); + { + if (_vsort_exec_exprs.need_materialize_tuple()) { + auto output_tuple_expr_ctxs = _vsort_exec_exprs.sort_tuple_slot_expr_ctxs(); + std::vector<int> valid_column_ids(output_tuple_expr_ctxs.size()); + for (int i = 0; i < output_tuple_expr_ctxs.size(); ++i) { + RETURN_IF_ERROR(output_tuple_expr_ctxs[i]->execute(block, &valid_column_ids[i])); + } + + Block new_block; + for (auto column_id : valid_column_ids) { + new_block.insert(block->get_by_position(column_id)); + } + block->swap(new_block); + } + + // TODO: could we init `_sort_description` only once? + _sort_description.resize(_vsort_exec_exprs.lhs_ordering_expr_ctxs().size()); + for (int i = 0; i < _sort_description.size(); i++) { + const auto& ordering_expr = _vsort_exec_exprs.lhs_ordering_expr_ctxs()[i]; + RETURN_IF_ERROR(ordering_expr->execute(block, &_sort_description[i].column_number)); + + _sort_description[i].direction = _is_asc_order[i] ? 1 : -1; + _sort_description[i].nulls_direction = _nulls_first[i] ? -_sort_description[i].direction + : _sort_description[i].direction; + } + } + Block tmp_block = block->clone_empty(); + tmp_block.swap(*block); + std::shared_ptr<HeapSortCursorBlockView> block_view( + new HeapSortCursorBlockView(std::move(tmp_block), _sort_description)); + size_t num_rows = tmp_block.rows(); + if (_heap_size == _heap->size()) { + { + SCOPED_TIMER(_topn_filter_timer); + _do_filter(block_view.get(), num_rows); Review Comment: rethink we really need to do `filter_block_internal`. why not just keep origin block mem? ########## be/src/vec/common/sort/topn_sorter.cpp: ########## @@ -0,0 +1,156 @@ +// 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/common/sort/topn_sorter.h" + +namespace doris::vectorized { + +TopNSorter::TopNSorter(VSortExecExprs& vsort_exec_exprs, int limit, int64_t offset, + ObjectPool* pool, std::vector<bool>& is_asc_order, + std::vector<bool>& nulls_first, const RowDescriptor& row_desc) + : Sorter(vsort_exec_exprs, limit, offset, pool, is_asc_order, nulls_first), + _heap_size(limit + offset), + _heap(std::make_unique<SortingHeap>()), + _topn_filter_rows(0) {} + +Status TopNSorter::append_block(Block* block, bool* mem_reuse) { + DCHECK(block->rows() > 0); + { + if (_vsort_exec_exprs.need_materialize_tuple()) { + auto output_tuple_expr_ctxs = _vsort_exec_exprs.sort_tuple_slot_expr_ctxs(); + std::vector<int> valid_column_ids(output_tuple_expr_ctxs.size()); + for (int i = 0; i < output_tuple_expr_ctxs.size(); ++i) { + RETURN_IF_ERROR(output_tuple_expr_ctxs[i]->execute(block, &valid_column_ids[i])); + } + + Block new_block; + for (auto column_id : valid_column_ids) { + new_block.insert(block->get_by_position(column_id)); + } + block->swap(new_block); Review Comment: do the `swap` may cause loss the not order by column ? ########## be/src/vec/core/sort_block.h: ########## @@ -220,6 +220,7 @@ class ColumnSorter { EqualRange& range, bool last_column) const { if (!column.has_null()) { column.get_nested_column().sort_column(this, flags, perms, range, last_column); + return; Review Comment: better move ` column.get_nested_column().sort_column(this, flags, perms, range, last_column); ` into `else` not just return ########## be/src/vec/common/sort/topn_sorter.cpp: ########## @@ -0,0 +1,156 @@ +// 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/common/sort/topn_sorter.h" + +namespace doris::vectorized { + +TopNSorter::TopNSorter(VSortExecExprs& vsort_exec_exprs, int limit, int64_t offset, + ObjectPool* pool, std::vector<bool>& is_asc_order, + std::vector<bool>& nulls_first, const RowDescriptor& row_desc) + : Sorter(vsort_exec_exprs, limit, offset, pool, is_asc_order, nulls_first), + _heap_size(limit + offset), + _heap(std::make_unique<SortingHeap>()), + _topn_filter_rows(0) {} + +Status TopNSorter::append_block(Block* block, bool* mem_reuse) { + DCHECK(block->rows() > 0); + { + if (_vsort_exec_exprs.need_materialize_tuple()) { + auto output_tuple_expr_ctxs = _vsort_exec_exprs.sort_tuple_slot_expr_ctxs(); + std::vector<int> valid_column_ids(output_tuple_expr_ctxs.size()); + for (int i = 0; i < output_tuple_expr_ctxs.size(); ++i) { + RETURN_IF_ERROR(output_tuple_expr_ctxs[i]->execute(block, &valid_column_ids[i])); + } + + Block new_block; + for (auto column_id : valid_column_ids) { + new_block.insert(block->get_by_position(column_id)); + } + block->swap(new_block); + } + + // TODO: could we init `_sort_description` only once? + _sort_description.resize(_vsort_exec_exprs.lhs_ordering_expr_ctxs().size()); + for (int i = 0; i < _sort_description.size(); i++) { + const auto& ordering_expr = _vsort_exec_exprs.lhs_ordering_expr_ctxs()[i]; + RETURN_IF_ERROR(ordering_expr->execute(block, &_sort_description[i].column_number)); + + _sort_description[i].direction = _is_asc_order[i] ? 1 : -1; + _sort_description[i].nulls_direction = _nulls_first[i] ? -_sort_description[i].direction + : _sort_description[i].direction; + } + } + Block tmp_block = block->clone_empty(); + tmp_block.swap(*block); + std::shared_ptr<HeapSortCursorBlockView> block_view( + new HeapSortCursorBlockView(std::move(tmp_block), _sort_description)); + size_t num_rows = tmp_block.rows(); + if (_heap_size == _heap->size()) { + { + SCOPED_TIMER(_topn_filter_timer); + _do_filter(block_view.get(), num_rows); + } + size_t remain_rows = block_view->block.rows(); + _topn_filter_rows += (num_rows - remain_rows); + COUNTER_SET(_topn_filter_rows_counter, _topn_filter_rows); + for (size_t i = 0; i < remain_rows; ++i) { + HeapSortCursorImpl cursor(i, block_view); + _heap->replace_top_if_less(std::move(cursor)); + } + } else { + size_t free_slots = std::min<size_t>(_heap_size - _heap->size(), num_rows); + + size_t i = 0; + for (; i < free_slots; ++i) { + HeapSortCursorImpl cursor(i, block_view); + _heap->push(std::move(cursor)); + } + + for (; i < num_rows; ++i) { + HeapSortCursorImpl cursor(i, block_view); + _heap->replace_top_if_less(std::move(cursor)); + } + } + return Status::OK(); +} + +Status TopNSorter::prepare_for_read() { + if (!_heap->empty() && _heap->size() > _offset) { + auto top = _heap->top(); + size_t num_columns = top.block()->columns(); + MutableColumns result_columns = top.block()->clone_empty_columns(); + + size_t init_size = std::min((size_t)_limit, _heap->size()); + result_columns.reserve(init_size); + + DCHECK(_heap->size() <= _heap_size); + // Use a vector to reverse elements in heap + std::vector<HeapSortCursorImpl> vector_to_reverse(init_size); + size_t capacity = 0; + while (!_heap->empty()) { + auto current = _heap->top(); + _heap->pop(); + vector_to_reverse[capacity++] = std::move(current); + if (_offset != 0 && _heap->size() == _offset) { + break; + } + } + for (size_t i = capacity - 1; i > 0; i--) { Review Comment: why `int i` and `i >= 0` and remove unless 114 to 118 line. ########## be/src/vec/common/sort/topn_sorter.cpp: ########## @@ -0,0 +1,156 @@ +// 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/common/sort/topn_sorter.h" + +namespace doris::vectorized { + +TopNSorter::TopNSorter(VSortExecExprs& vsort_exec_exprs, int limit, int64_t offset, + ObjectPool* pool, std::vector<bool>& is_asc_order, + std::vector<bool>& nulls_first, const RowDescriptor& row_desc) + : Sorter(vsort_exec_exprs, limit, offset, pool, is_asc_order, nulls_first), + _heap_size(limit + offset), + _heap(std::make_unique<SortingHeap>()), + _topn_filter_rows(0) {} + +Status TopNSorter::append_block(Block* block, bool* mem_reuse) { + DCHECK(block->rows() > 0); + { + if (_vsort_exec_exprs.need_materialize_tuple()) { + auto output_tuple_expr_ctxs = _vsort_exec_exprs.sort_tuple_slot_expr_ctxs(); + std::vector<int> valid_column_ids(output_tuple_expr_ctxs.size()); + for (int i = 0; i < output_tuple_expr_ctxs.size(); ++i) { + RETURN_IF_ERROR(output_tuple_expr_ctxs[i]->execute(block, &valid_column_ids[i])); + } + + Block new_block; + for (auto column_id : valid_column_ids) { + new_block.insert(block->get_by_position(column_id)); + } + block->swap(new_block); + } + + // TODO: could we init `_sort_description` only once? + _sort_description.resize(_vsort_exec_exprs.lhs_ordering_expr_ctxs().size()); + for (int i = 0; i < _sort_description.size(); i++) { + const auto& ordering_expr = _vsort_exec_exprs.lhs_ordering_expr_ctxs()[i]; + RETURN_IF_ERROR(ordering_expr->execute(block, &_sort_description[i].column_number)); + + _sort_description[i].direction = _is_asc_order[i] ? 1 : -1; + _sort_description[i].nulls_direction = _nulls_first[i] ? -_sort_description[i].direction + : _sort_description[i].direction; + } + } + Block tmp_block = block->clone_empty(); + tmp_block.swap(*block); + std::shared_ptr<HeapSortCursorBlockView> block_view( + new HeapSortCursorBlockView(std::move(tmp_block), _sort_description)); + size_t num_rows = tmp_block.rows(); + if (_heap_size == _heap->size()) { + { + SCOPED_TIMER(_topn_filter_timer); + _do_filter(block_view.get(), num_rows); + } + size_t remain_rows = block_view->block.rows(); + _topn_filter_rows += (num_rows - remain_rows); + COUNTER_SET(_topn_filter_rows_counter, _topn_filter_rows); + for (size_t i = 0; i < remain_rows; ++i) { + HeapSortCursorImpl cursor(i, block_view); + _heap->replace_top_if_less(std::move(cursor)); + } + } else { + size_t free_slots = std::min<size_t>(_heap_size - _heap->size(), num_rows); + + size_t i = 0; + for (; i < free_slots; ++i) { + HeapSortCursorImpl cursor(i, block_view); + _heap->push(std::move(cursor)); + } + + for (; i < num_rows; ++i) { + HeapSortCursorImpl cursor(i, block_view); + _heap->replace_top_if_less(std::move(cursor)); + } + } + return Status::OK(); +} + +Status TopNSorter::prepare_for_read() { + if (!_heap->empty() && _heap->size() > _offset) { + auto top = _heap->top(); + size_t num_columns = top.block()->columns(); + MutableColumns result_columns = top.block()->clone_empty_columns(); + + size_t init_size = std::min((size_t)_limit, _heap->size()); + result_columns.reserve(init_size); + + DCHECK(_heap->size() <= _heap_size); + // Use a vector to reverse elements in heap + std::vector<HeapSortCursorImpl> vector_to_reverse(init_size); + size_t capacity = 0; + while (!_heap->empty()) { + auto current = _heap->top(); + _heap->pop(); + vector_to_reverse[capacity++] = std::move(current); + if (_offset != 0 && _heap->size() == _offset) { + break; + } + } + for (size_t i = capacity - 1; i > 0; i--) { + auto rid = vector_to_reverse[i].row_id(); + const auto cur_block = vector_to_reverse[i].block(); + for (size_t j = 0; j < num_columns; ++j) { + result_columns[j]->insert_from(*(cur_block->get_columns()[j]), rid); + } + } + auto rid = vector_to_reverse[0].row_id(); + const auto cur_block = vector_to_reverse[0].block(); + for (size_t j = 0; j < num_columns; ++j) { + result_columns[j]->insert_from(*(cur_block->get_columns()[j]), rid); + } + _return_block = top.block()->clone_with_columns(std::move(result_columns)); Review Comment: use `vector_to_reverse` to do `clone_with_columns` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org