From 049f8ad2f98182ffc48447358aaeed55aec443a5 Mon Sep 17 00:00:00 2001 From: AlexYue Date: Fri, 13 Jan 2023 18:33:40 +0800 Subject: [PATCH] [Bug](sort)fix merge sorter might div zero when block bytes less than block rows (#15859) If block bytes are bigger than the corresponding block's rows, then the avg_size_per_row would be zero. Which would end up diving zero in the following logic. --- be/src/vec/common/sort/sorter.cpp | 2 +- .../data/demo_p0/streamLoad_action.out | 8 ++++++ .../suites/demo_p0/streamLoad_action.groovy | 26 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 regression-test/data/demo_p0/streamLoad_action.out diff --git a/be/src/vec/common/sort/sorter.cpp b/be/src/vec/common/sort/sorter.cpp index 778ad27fe2..9bc0a1034c 100644 --- a/be/src/vec/common/sort/sorter.cpp +++ b/be/src/vec/common/sort/sorter.cpp @@ -43,7 +43,7 @@ Status MergeSorterState::add_sorted_block(Block& block) { return Status::OK(); } if (0 == avg_row_bytes_) { - avg_row_bytes_ = block.bytes() / rows; + avg_row_bytes_ = std::max((std::size_t)1, block.bytes() / rows); spill_block_batch_size_ = (BLOCK_SPILL_BATCH_BYTES + avg_row_bytes_ - 1) / avg_row_bytes_; } diff --git a/regression-test/data/demo_p0/streamLoad_action.out b/regression-test/data/demo_p0/streamLoad_action.out new file mode 100644 index 0000000000..445a11c6dc --- /dev/null +++ b/regression-test/data/demo_p0/streamLoad_action.out @@ -0,0 +1,8 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !sql -- +0 +2 +2 +3 +3 + diff --git a/regression-test/suites/demo_p0/streamLoad_action.groovy b/regression-test/suites/demo_p0/streamLoad_action.groovy index 49df370d11..87affd32c4 100644 --- a/regression-test/suites/demo_p0/streamLoad_action.groovy +++ b/regression-test/suites/demo_p0/streamLoad_action.groovy @@ -78,4 +78,30 @@ suite("streamLoad_action") { assertTrue(json.NumberLoadedRows > 0 && json.LoadBytes > 0) } } + + // to test merge sort + sql """ DROP TABLE IF EXISTS B """ + sql """ + CREATE TABLE IF NOT EXISTS B + ( + b_id int + ) + DISTRIBUTED BY HASH(b_id) BUCKETS 1 + PROPERTIES("replication_num" = "1"); + """ + + sql " INSERT INTO B values (1);" + + qt_sql """ + SELECT subq_0.`c1` AS c1 + FROM + (SELECT version() AS c0, + ref_0.`id` AS c1 + FROM test_streamload_action1 AS ref_0 + LEFT JOIN B AS ref_9 ON (ref_0.`id` = ref_9.`b_id`) + WHERE ref_9.`b_id` IS NULL) AS subq_0 + WHERE subq_0.`c0` IS NOT NULL + ORDER BY subq_0.`c1`, subq_0.`c0` DESC + LIMIT 5; + """ }