Gabriel39 commented on code in PR #12604:
URL: https://github.com/apache/doris/pull/12604#discussion_r970704416


##########
be/src/vec/exec/vsort_node.cpp:
##########
@@ -182,6 +199,56 @@ Status VSortNode::sort_input(RuntimeState* state) {
     return Status::OK();
 }
 
+Status VSortNode::sort_raw_block(RuntimeState* state) {
+    bool eos = false;
+    do {
+        Block block;
+        
RETURN_IF_ERROR_AND_CHECK_SPAN(child(0)->get_next_after_projects(state, &block, 
&eos),
+                                       child(0)->get_next_span(), eos);
+        auto rows = block.rows();
+
+        if (rows != 0) {
+            RETURN_IF_ERROR(partial_sort(block));
+            size_t mem_usage = block.allocated_bytes();
+
+            // dispose TOP-N logic
+            if (_limit != -1) {
+                // Here is a little opt to reduce the mem uasge, we build a 
max heap
+                // to order the block in _block_priority_queue.
+                // if one block totally greater the heap top of 
_block_priority_queue
+                // we can throw the block data directly.
+                if (_num_rows_in_block < _limit) {
+                    _total_mem_usage += mem_usage;
+                    _sorted_blocks.emplace_back(std::move(block));
+                    _num_rows_in_block += rows;
+                    _block_priority_queue.emplace(_pool->add(
+                            new SortCursorImpl(_sorted_blocks.back(), 
_sort_description)));
+                } else {
+                    SortBlockCursor block_cursor(
+                            _pool->add(new SortCursorImpl(block, 
_sort_description)));
+                    if 
(!block_cursor.totally_greater(_block_priority_queue.top())) {
+                        _sorted_blocks.emplace_back(std::move(block));
+                        _block_priority_queue.push(block_cursor);
+                        _total_mem_usage += mem_usage;
+                    } else {
+                        continue;
+                    }
+                }
+            } else {
+                // dispose normal sort logic
+                _total_mem_usage += mem_usage;

Review Comment:
   `_limit == -1` instead of `row == 0` here 



-- 
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

Reply via email to