This is an automated email from the ASF dual-hosted git repository.
zhangstar333 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 1c4356afc93 [test](beut) add all sorter type beut (#50384)
1c4356afc93 is described below
commit 1c4356afc9374e9f8c3104ffe8cac1aa16f767f1
Author: Mryange <[email protected]>
AuthorDate: Tue May 6 10:32:59 2025 +0800
[test](beut) add all sorter type beut (#50384)
### What problem does this PR solve?
add all sorter type beut
---
be/test/vec/exec/sort/full_sort_test.cpp | 125 +++++++++++++++
be/test/vec/exec/sort/heap_sorter_test.cpp | 131 ++++++++++++++++
be/test/vec/exec/sort/merge_sorter_state.cpp | 105 +++++++++++++
be/test/vec/exec/sort/partition_sorter_test.cpp | 197 ++++++++++++++++++++++++
be/test/vec/exec/sort/sort_test.cpp | 36 +++++
be/test/vec/exec/sort/topn_sort_test.cpp | 108 +++++++++++++
6 files changed, 702 insertions(+)
diff --git a/be/test/vec/exec/sort/full_sort_test.cpp
b/be/test/vec/exec/sort/full_sort_test.cpp
new file mode 100644
index 00000000000..5646db5e49a
--- /dev/null
+++ b/be/test/vec/exec/sort/full_sort_test.cpp
@@ -0,0 +1,125 @@
+// 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 <gen_cpp/olap_file.pb.h>
+#include <gen_cpp/types.pb.h>
+#include <glog/logging.h>
+#include <gtest/gtest.h>
+
+#include <algorithm>
+#include <cstdint>
+#include <memory>
+#include <random>
+#include <utility>
+
+#include "common/object_pool.h"
+#include "runtime/runtime_state.h"
+#include "testutil/column_helper.h"
+#include "testutil/mock/mock_descriptors.h"
+#include "testutil/mock/mock_runtime_state.h"
+#include "testutil/mock/mock_slot_ref.h"
+#include "vec/columns/columns_number.h"
+#include "vec/common/assert_cast.h"
+#include "vec/common/sort/heap_sorter.h"
+#include "vec/common/sort/sorter.h"
+#include "vec/common/sort/topn_sorter.h"
+#include "vec/common/sort/vsort_exec_exprs.h"
+#include "vec/core/block.h"
+
+namespace doris::vectorized {
+
+struct FullSorterTest : public testing::Test {
+ void SetUp() override {
+ row_desc.reset(new
MockRowDescriptor({std::make_shared<DataTypeInt64>()}, &pool));
+
+ sort_exec_exprs._sort_tuple_slot_expr_ctxs =
+ MockSlotRef::create_mock_contexts(0,
std::make_shared<DataTypeInt64>());
+
+ sort_exec_exprs._materialize_tuple = false;
+
+ sort_exec_exprs._ordering_expr_ctxs =
+ MockSlotRef::create_mock_contexts(0,
std::make_shared<DataTypeInt64>());
+
+ sort_exec_exprs._sort_tuple_slot_expr_ctxs =
+ MockSlotRef::create_mock_contexts(0,
std::make_shared<DataTypeInt64>());
+ }
+ MockRuntimeState _state;
+ RuntimeProfile _profile {"test"};
+
+ std::unique_ptr<FullSorter> sorter;
+
+ std::unique_ptr<MockRowDescriptor> row_desc;
+
+ ObjectPool pool;
+
+ VSortExecExprs sort_exec_exprs;
+
+ std::vector<bool> is_asc_order {true};
+ std::vector<bool> nulls_first {false};
+};
+
+TEST_F(FullSorterTest, test_full_sorter1) {
+ sorter = FullSorter::create_unique(sort_exec_exprs, -1, 0, &pool,
is_asc_order, nulls_first,
+ *row_desc, nullptr, nullptr);
+
+ Block block1 = ColumnHelper::create_block<DataTypeInt64>({1, 2, 3, 4, 5,
6, 7, 8, 9, 10});
+ Block block2 = ColumnHelper::create_block<DataTypeInt64>({10, 9, 8, 7, 6,
5, 4, 3, 2, 1});
+
+ EXPECT_TRUE(sorter->has_enough_capacity(&block1, &block2));
+ EXPECT_TRUE(block1.get_by_position(0).column->has_enough_capacity(
+ *block2.get_by_position(0).column));
+}
+
+TEST_F(FullSorterTest, test_full_sorter2) {
+ sorter = FullSorter::create_unique(sort_exec_exprs, -1, 0, &pool,
is_asc_order, nulls_first,
+ *row_desc, nullptr, nullptr);
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({1, 2, 3, 4,
5, 6, 7, 8, 9, 10});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ }
+
+ {
+ auto col_const =
ColumnConst::create(ColumnHelper::create_column<DataTypeInt64>({1}), 10);
+ Block block = {ColumnWithTypeAndName(std::move(col_const),
+
std::make_shared<DataTypeInt64>(), "col")};
+
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ }
+
+ std::cout << sorter->get_reserve_mem_size(&_state, false) << std::endl;
+}
+
+TEST_F(FullSorterTest, test_full_sorter3) {
+ sorter = FullSorter::create_unique(sort_exec_exprs, 3, 3, &pool,
is_asc_order, nulls_first,
+ *row_desc, nullptr, nullptr);
+ sorter->init_profile(&_profile);
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({1, 2, 3, 4,
5, 6, 7, 8, 9, 10});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ EXPECT_TRUE(sorter->_do_sort());
+ }
+
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({4, 5, 6, 7});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ EXPECT_TRUE(sorter->_do_sort());
+ }
+ EXPECT_EQ(sorter->_state->get_sorted_block()[0]->rows(), 6);
+ EXPECT_EQ(sorter->_state->get_sorted_block()[1]->rows(), 4);
+}
+
+} // namespace doris::vectorized
\ No newline at end of file
diff --git a/be/test/vec/exec/sort/heap_sorter_test.cpp
b/be/test/vec/exec/sort/heap_sorter_test.cpp
new file mode 100644
index 00000000000..f886d70369b
--- /dev/null
+++ b/be/test/vec/exec/sort/heap_sorter_test.cpp
@@ -0,0 +1,131 @@
+// 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/heap_sorter.h"
+
+#include <gen_cpp/olap_file.pb.h>
+#include <gen_cpp/types.pb.h>
+#include <glog/logging.h>
+#include <gtest/gtest.h>
+
+#include <algorithm>
+#include <cstdint>
+#include <memory>
+#include <random>
+#include <utility>
+
+#include "common/object_pool.h"
+#include "runtime/runtime_state.h"
+#include "testutil/column_helper.h"
+#include "testutil/mock/mock_descriptors.h"
+#include "testutil/mock/mock_runtime_state.h"
+#include "testutil/mock/mock_slot_ref.h"
+#include "vec/columns/columns_number.h"
+#include "vec/common/assert_cast.h"
+#include "vec/common/sort/sorter.h"
+#include "vec/common/sort/topn_sorter.h"
+#include "vec/common/sort/vsort_exec_exprs.h"
+#include "vec/core/block.h"
+
+namespace doris::vectorized {
+
+struct HeapSorterTest : public testing::Test {
+ void SetUp() override {
+ row_desc.reset(new
MockRowDescriptor({std::make_shared<DataTypeInt64>()}, &pool));
+
+ sort_exec_exprs._sort_tuple_slot_expr_ctxs =
+ MockSlotRef::create_mock_contexts(0,
std::make_shared<DataTypeInt64>());
+
+ sort_exec_exprs._materialize_tuple = false;
+
+ sort_exec_exprs._ordering_expr_ctxs =
+ MockSlotRef::create_mock_contexts(0,
std::make_shared<DataTypeInt64>());
+
+ sort_exec_exprs._sort_tuple_slot_expr_ctxs =
+ MockSlotRef::create_mock_contexts(0,
std::make_shared<DataTypeInt64>());
+ }
+ MockRuntimeState _state;
+ RuntimeProfile _profile {"test"};
+
+ std::unique_ptr<HeapSorter> sorter;
+
+ std::unique_ptr<MockRowDescriptor> row_desc;
+
+ ObjectPool pool;
+
+ VSortExecExprs sort_exec_exprs;
+
+ std::vector<bool> is_asc_order {true};
+ std::vector<bool> nulls_first {false};
+};
+
+TEST_F(HeapSorterTest, test_topn_sorter1) {
+ DataTypes data_types {std::make_shared<DataTypeInt64>(),
std::make_shared<DataTypeInt64>()};
+ row_desc.reset(new MockRowDescriptor(data_types, &pool));
+
+ sort_exec_exprs._sort_tuple_slot_expr_ctxs =
MockSlotRef::create_mock_contexts(data_types);
+
+ sort_exec_exprs._materialize_tuple = true;
+
+ sort_exec_exprs._ordering_expr_ctxs =
MockSlotRef::create_mock_contexts(data_types);
+
+ sort_exec_exprs._sort_tuple_slot_expr_ctxs =
MockSlotRef::create_mock_contexts(data_types);
+
+ sort_exec_exprs._need_convert_to_nullable_flags = {true, false};
+
+ sorter = HeapSorter::create_unique(sort_exec_exprs, 6, 0, &pool,
is_asc_order, nulls_first,
+ *row_desc);
+
+ sorter->init_profile(&_profile);
+
+ {
+ Block block =
+ ColumnHelper::create_block<DataTypeInt64>({7, 5, 4, 3, 2, 1},
{7, 5, 4, 3, 2, 1});
+ auto st = sorter->append_block(&block);
+ EXPECT_TRUE(st.ok());
+ }
+
+ EXPECT_EQ(sorter->_heap->size(), 6);
+
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({6}, {6});
+ auto st = sorter->append_block(&block);
+ EXPECT_TRUE(st.ok());
+ }
+
+ EXPECT_EQ(sorter->_heap->size(), 6);
+
+ static_cast<void>(sorter->get_top_value());
+
+ EXPECT_TRUE(sorter->prepare_for_read());
+
+ {
+ Block block;
+ bool eos;
+ EXPECT_TRUE(sorter->get_next(&_state, &block, &eos));
+ std::cout << block.dump_data() << std::endl;
+ EXPECT_EQ(block.rows(), 6);
+
+ EXPECT_TRUE(ColumnHelper::block_equal(
+ block,
+ Block
{ColumnHelper::create_nullable_column_with_name<DataTypeInt64>(
+ {1, 2, 3, 4, 5, 6}, {false, false, false,
false, false, false}),
+
ColumnHelper::create_column_with_name<DataTypeInt64>({1, 2, 3, 4, 5, 6})}));
+ }
+}
+
+} // namespace doris::vectorized
\ No newline at end of file
diff --git a/be/test/vec/exec/sort/merge_sorter_state.cpp
b/be/test/vec/exec/sort/merge_sorter_state.cpp
new file mode 100644
index 00000000000..e98dfcc751e
--- /dev/null
+++ b/be/test/vec/exec/sort/merge_sorter_state.cpp
@@ -0,0 +1,105 @@
+// 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 <gen_cpp/olap_file.pb.h>
+#include <gen_cpp/types.pb.h>
+#include <glog/logging.h>
+#include <gtest/gtest.h>
+
+#include <algorithm>
+#include <cstdint>
+#include <memory>
+#include <random>
+#include <utility>
+
+#include "common/object_pool.h"
+#include "runtime/runtime_state.h"
+#include "testutil/column_helper.h"
+#include "testutil/mock/mock_descriptors.h"
+#include "testutil/mock/mock_runtime_state.h"
+#include "testutil/mock/mock_slot_ref.h"
+#include "vec/columns/columns_number.h"
+#include "vec/common/assert_cast.h"
+#include "vec/common/sort/heap_sorter.h"
+#include "vec/common/sort/sorter.h"
+#include "vec/common/sort/topn_sorter.h"
+#include "vec/common/sort/vsort_exec_exprs.h"
+#include "vec/core/block.h"
+
+namespace doris::vectorized {
+
+struct MergeSorterStateTest : public testing::Test {
+ void SetUp() override {
+ row_desc.reset(new
MockRowDescriptor({std::make_shared<DataTypeInt64>()}, &pool));
+ }
+ MockRuntimeState _state;
+ RuntimeProfile _profile {"test"};
+
+ std::shared_ptr<MergeSorterState> state;
+
+ std::unique_ptr<MockRowDescriptor> row_desc;
+
+ ObjectPool pool;
+};
+
+std::shared_ptr<Block> create_block(std::vector<int64_t> data) {
+ auto block = std::make_shared<Block>();
+ *block = ColumnHelper::create_block<DataTypeInt64>(data);
+ return block;
+}
+
+TEST_F(MergeSorterStateTest, test1) {
+ state.reset(new MergeSorterState(*row_desc, 0, -1, &_state, &_profile));
+ state->add_sorted_block(create_block({1, 2, 3}));
+ state->add_sorted_block(create_block({4, 5, 6}));
+ state->add_sorted_block(create_block({}));
+ EXPECT_EQ(state->num_rows(), 6);
+ EXPECT_EQ(state->data_size(), 48);
+ EXPECT_EQ(state->get_queue().size(), 0);
+
+ SortDescription desc {SortColumnDescription {0, 1, -1}};
+ EXPECT_TRUE(state->build_merge_tree(desc));
+ EXPECT_EQ(state->get_queue().size(), 2);
+
+ {
+ Block block;
+ bool eos = false;
+ Status status = state->merge_sort_read(&block, 2, &eos);
+ EXPECT_TRUE(status.ok());
+ EXPECT_TRUE(ColumnHelper::block_equal(block,
+
ColumnHelper::create_block<DataTypeInt64>({1, 2})));
+ }
+
+ {
+ Block block;
+ bool eos = false;
+ Status status = state->merge_sort_read(&block, 2, &eos);
+ EXPECT_TRUE(status.ok());
+ EXPECT_TRUE(ColumnHelper::block_equal(block,
+
ColumnHelper::create_block<DataTypeInt64>({3, 4})));
+ }
+
+ {
+ Block block;
+ bool eos = false;
+ Status status = state->merge_sort_read(&block, 2, &eos);
+ EXPECT_TRUE(status.ok());
+ EXPECT_TRUE(ColumnHelper::block_equal(block,
+
ColumnHelper::create_block<DataTypeInt64>({5, 6})));
+ }
+}
+} // namespace doris::vectorized
\ No newline at end of file
diff --git a/be/test/vec/exec/sort/partition_sorter_test.cpp
b/be/test/vec/exec/sort/partition_sorter_test.cpp
new file mode 100644
index 00000000000..8a27d6e8a62
--- /dev/null
+++ b/be/test/vec/exec/sort/partition_sorter_test.cpp
@@ -0,0 +1,197 @@
+// 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 <gen_cpp/olap_file.pb.h>
+#include <gen_cpp/types.pb.h>
+#include <glog/logging.h>
+#include <gtest/gtest.h>
+
+#include <algorithm>
+#include <cstdint>
+#include <memory>
+#include <random>
+#include <utility>
+
+#include "common/object_pool.h"
+#include "runtime/runtime_state.h"
+#include "testutil/column_helper.h"
+#include "testutil/mock/mock_descriptors.h"
+#include "testutil/mock/mock_runtime_state.h"
+#include "testutil/mock/mock_slot_ref.h"
+#include "vec/columns/columns_number.h"
+#include "vec/common/assert_cast.h"
+#include "vec/common/sort/heap_sorter.h"
+#include "vec/common/sort/sorter.h"
+#include "vec/common/sort/topn_sorter.h"
+#include "vec/common/sort/vsort_exec_exprs.h"
+#include "vec/core/block.h"
+
+namespace doris::vectorized {
+
+struct PartitionSorterTest : public testing::Test {
+ void SetUp() override {
+ row_desc.reset(new
MockRowDescriptor({std::make_shared<DataTypeInt64>()}, &pool));
+
+ sort_exec_exprs._sort_tuple_slot_expr_ctxs =
+ MockSlotRef::create_mock_contexts(0,
std::make_shared<DataTypeInt64>());
+
+ sort_exec_exprs._materialize_tuple = false;
+
+ sort_exec_exprs._ordering_expr_ctxs =
+ MockSlotRef::create_mock_contexts(0,
std::make_shared<DataTypeInt64>());
+
+ sort_exec_exprs._sort_tuple_slot_expr_ctxs =
+ MockSlotRef::create_mock_contexts(0,
std::make_shared<DataTypeInt64>());
+ }
+ MockRuntimeState _state;
+ RuntimeProfile _profile {"test"};
+
+ std::unique_ptr<PartitionSorter> sorter;
+
+ std::unique_ptr<MockRowDescriptor> row_desc;
+
+ ObjectPool pool;
+
+ VSortExecExprs sort_exec_exprs;
+
+ std::vector<bool> is_asc_order {true};
+ std::vector<bool> nulls_first {false};
+};
+
+TEST_F(PartitionSorterTest, test_partition_sorter_read_row_num) {
+ sorter = PartitionSorter::create_unique(sort_exec_exprs, -1, 0, &pool,
is_asc_order,
+ nulls_first, *row_desc, nullptr,
nullptr, false, 20,
+ TopNAlgorithm::ROW_NUMBER,
nullptr);
+ sorter->init_profile(&_profile);
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({10, 9, 8, 7,
6, 5, 4, 3, 2, 1});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ }
+
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({4, 5, 6, 7});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ }
+
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({100, 111});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ }
+
+ {
+ auto st = sorter->prepare_for_read();
+ EXPECT_TRUE(st.ok()) << st.msg();
+ }
+ {
+ bool eos = false;
+ Block block;
+ EXPECT_TRUE(sorter->get_next(&_state, &block, &eos).ok());
+ std::cout << block.dump_data() << std::endl;
+ EXPECT_TRUE(ColumnHelper::block_equal(
+ block, ColumnHelper::create_block<DataTypeInt64>(
+ {1, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 10})));
+ }
+
+ {
+ bool eos = false;
+ Block block;
+ EXPECT_TRUE(sorter->get_next(&_state, &block, &eos).ok());
+ std::cout << block.dump_data() << std::endl;
+ EXPECT_TRUE(ColumnHelper::block_equal(
+ block, ColumnHelper::create_block<DataTypeInt64>({100, 111})));
+ }
+}
+
+TEST_F(PartitionSorterTest, test_partition_sorter_DENSE_RANK) {
+ SortCursorCmp previous_row;
+
+ sorter = PartitionSorter::create_unique(sort_exec_exprs, -1, 0, &pool,
is_asc_order,
+ nulls_first, *row_desc, nullptr,
nullptr, false, 20,
+ TopNAlgorithm::DENSE_RANK,
&previous_row);
+ sorter->init_profile(&_profile);
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({10, 9, 8, 7,
6, 5, 4, 3, 2, 1});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ }
+
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({4, 5, 6, 7});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ }
+
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({100, 111});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ }
+
+ {
+ auto st = sorter->prepare_for_read();
+ EXPECT_TRUE(st.ok()) << st.msg();
+ }
+ {
+ bool eos = false;
+ Block block;
+ EXPECT_TRUE(sorter->get_next(&_state, &block, &eos).ok());
+ std::cout << block.dump_data() << std::endl;
+ EXPECT_TRUE(ColumnHelper::block_equal(
+ block, ColumnHelper::create_block<DataTypeInt64>(
+ {1, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 10,
100, 111})));
+ }
+
+ sorter->reset_sorter_state(&_state);
+}
+
+TEST_F(PartitionSorterTest, test_partition_sorter_RANK) {
+ SortCursorCmp previous_row;
+
+ sorter = PartitionSorter::create_unique(sort_exec_exprs, -1, 0, &pool,
is_asc_order,
+ nulls_first, *row_desc, nullptr,
nullptr, false, 20,
+ TopNAlgorithm::RANK,
&previous_row);
+ sorter->init_profile(&_profile);
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({10, 9, 8, 7,
6, 5, 4, 3, 2, 1});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ }
+
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({4, 5, 6, 7});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ }
+
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({100, 111});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ }
+
+ {
+ auto st = sorter->prepare_for_read();
+ EXPECT_TRUE(st.ok()) << st.msg();
+ }
+ {
+ bool eos = false;
+ Block block;
+ EXPECT_TRUE(sorter->get_next(&_state, &block, &eos).ok());
+ std::cout << block.dump_data() << std::endl;
+ EXPECT_TRUE(ColumnHelper::block_equal(
+ block, ColumnHelper::create_block<DataTypeInt64>(
+ {1, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 10,
100, 111})));
+ }
+
+ sorter->reset_sorter_state(&_state);
+}
+
+} // namespace doris::vectorized
\ No newline at end of file
diff --git a/be/test/vec/exec/sort/sort_test.cpp
b/be/test/vec/exec/sort/sort_test.cpp
index e774bcdb037..15d9bc22761 100644
--- a/be/test/vec/exec/sort/sort_test.cpp
+++ b/be/test/vec/exec/sort/sort_test.cpp
@@ -27,6 +27,7 @@
#include "common/object_pool.h"
#include "runtime/runtime_state.h"
+#include "testutil/column_helper.h"
#include "testutil/mock/mock_descriptors.h"
#include "testutil/mock/mock_runtime_state.h"
#include "testutil/mock/mock_slot_ref.h"
@@ -175,4 +176,39 @@ TEST_F(SortTest, test_heap_sort) {
test_sort(SortType::HEAP_SORT, 1000, 10, 100);
}
+TEST_F(SortTest, test_sorter) {
+ VSortExecExprs sort_exec_exprs;
+ ObjectPool pool;
+ std::unique_ptr<MockRowDescriptor> row_desc;
+ std::unique_ptr<RuntimeProfile> profile =
std::make_unique<RuntimeProfile>("");
+
+ std::vector<bool> is_asc_order {true};
+ std::vector<bool> nulls_first {false};
+
+ std::unique_ptr<vectorized::Sorter> sorter;
+ DataTypes data_types {std::make_shared<DataTypeInt64>(),
std::make_shared<DataTypeInt64>()};
+ row_desc.reset(new MockRowDescriptor(data_types, &pool));
+
+ sort_exec_exprs._sort_tuple_slot_expr_ctxs =
MockSlotRef::create_mock_contexts(data_types);
+
+ sort_exec_exprs._materialize_tuple = true;
+
+ sort_exec_exprs._ordering_expr_ctxs =
MockSlotRef::create_mock_contexts(data_types);
+
+ sort_exec_exprs._sort_tuple_slot_expr_ctxs =
MockSlotRef::create_mock_contexts(data_types);
+
+ sort_exec_exprs._need_convert_to_nullable_flags = {true, false};
+
+ sorter = FullSorter::create_unique(sort_exec_exprs, -1, 0, &pool,
is_asc_order, nulls_first,
+ *row_desc, nullptr, nullptr);
+
+ {
+ Block src_block = ColumnHelper::create_block<DataTypeInt64>({4, 1, 2},
{10, 1, 3});
+ Block dest_block;
+ auto st = sorter->partial_sort(src_block, dest_block);
+ EXPECT_TRUE(st.ok()) << st.msg();
+ std::cout << dest_block.dump_data() << std::endl;
+ }
+}
+
} // namespace doris::vectorized
diff --git a/be/test/vec/exec/sort/topn_sort_test.cpp
b/be/test/vec/exec/sort/topn_sort_test.cpp
new file mode 100644
index 00000000000..923f4b17945
--- /dev/null
+++ b/be/test/vec/exec/sort/topn_sort_test.cpp
@@ -0,0 +1,108 @@
+// 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 <gen_cpp/olap_file.pb.h>
+#include <gen_cpp/types.pb.h>
+#include <glog/logging.h>
+#include <gtest/gtest.h>
+
+#include <algorithm>
+#include <cstdint>
+#include <memory>
+#include <random>
+#include <utility>
+
+#include "common/object_pool.h"
+#include "runtime/runtime_state.h"
+#include "testutil/column_helper.h"
+#include "testutil/mock/mock_descriptors.h"
+#include "testutil/mock/mock_runtime_state.h"
+#include "testutil/mock/mock_slot_ref.h"
+#include "vec/columns/columns_number.h"
+#include "vec/common/assert_cast.h"
+#include "vec/common/sort/heap_sorter.h"
+#include "vec/common/sort/sorter.h"
+#include "vec/common/sort/topn_sorter.h"
+#include "vec/common/sort/vsort_exec_exprs.h"
+#include "vec/core/block.h"
+
+namespace doris::vectorized {
+
+struct TopNSorterTest : public testing::Test {
+ void SetUp() override {
+ row_desc.reset(new
MockRowDescriptor({std::make_shared<DataTypeInt64>()}, &pool));
+
+ sort_exec_exprs._sort_tuple_slot_expr_ctxs =
+ MockSlotRef::create_mock_contexts(0,
std::make_shared<DataTypeInt64>());
+
+ sort_exec_exprs._materialize_tuple = false;
+
+ sort_exec_exprs._ordering_expr_ctxs =
+ MockSlotRef::create_mock_contexts(0,
std::make_shared<DataTypeInt64>());
+
+ sort_exec_exprs._sort_tuple_slot_expr_ctxs =
+ MockSlotRef::create_mock_contexts(0,
std::make_shared<DataTypeInt64>());
+ }
+ MockRuntimeState _state;
+ RuntimeProfile _profile {"test"};
+
+ std::unique_ptr<TopNSorter> sorter;
+
+ std::unique_ptr<MockRowDescriptor> row_desc;
+
+ ObjectPool pool;
+
+ VSortExecExprs sort_exec_exprs;
+
+ std::vector<bool> is_asc_order {true};
+ std::vector<bool> nulls_first {false};
+};
+
+TEST_F(TopNSorterTest, test_topn_sorter1) {
+ sorter = TopNSorter::create_unique(sort_exec_exprs, 3, 3, &pool,
is_asc_order, nulls_first,
+ *row_desc, nullptr, nullptr);
+ sorter->init_profile(&_profile);
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({10, 9, 8, 7,
6, 5, 4, 3, 2, 1});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ }
+
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({4, 5, 6, 7});
+ EXPECT_TRUE(sorter->append_block(&block).ok());
+ }
+ EXPECT_EQ(sorter->_state->get_sorted_block()[0]->rows(), 6);
+
+ EXPECT_TRUE(ColumnHelper::block_equal(
+ *sorter->_state->get_sorted_block()[0],
+ ColumnHelper::create_block<DataTypeInt64>({1, 2, 3, 4, 5, 6})));
+ EXPECT_EQ(sorter->_state->get_sorted_block()[1]->rows(), 4);
+}
+
+TEST_F(TopNSorterTest, test_topn_sorter2) {
+ sorter = TopNSorter::create_unique(sort_exec_exprs, -1, 3, &pool,
is_asc_order, nulls_first,
+ *row_desc, nullptr, nullptr);
+ sorter->init_profile(&_profile);
+ {
+ Block block = ColumnHelper::create_block<DataTypeInt64>({10, 9, 8, 7,
6, 5, 4, 3, 2, 1});
+ auto st = sorter->append_block(&block);
+ EXPECT_TRUE(!st.ok());
+ std::cout << st.msg() << std::endl;
+ }
+}
+
+} // namespace doris::vectorized
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]