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


##########
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:
   This may bring pressure on memory if too much blocks produced here. For now 
I delete this `filter_block_internal ` and add a TODO 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