This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch branch-1.2-lts
in repository https://gitbox.apache.org/repos/asf/doris.git

commit 1aa640b04722f5ea9a01ed96f4487fdae7d238f3
Author: TengJianPing <[email protected]>
AuthorDate: Tue Mar 28 09:31:37 2023 +0800

    [fix](order by) fix bug of order by desc when rowsets is no overlapping 
(#18100)
    
    In the case of rowets non-overlap and desc sorting, the logic of 
VCollectIterator::Level0Iterator::init_for_union will be followed. In this 
function, the row ref pos of the first level0 iterator is set to 0, and the row 
pos of other level0 iterators are all Set to -1.
    
    But in the level1iterator, when rowets are non-overlapping and is ordering 
by desc, the list of rowset iterators will be reversed, causing the row ref pos 
of the first level0 iterator in the list to be -1, causing the block reader to 
think that the entire tablet has no data.
---
 be/src/vec/olap/vcollect_iterator.cpp            | 43 ++++++++++++++----------
 be/src/vec/olap/vcollect_iterator.h              |  2 ++
 regression-test/data/query_p0/sort/sort.out      |  6 ++++
 regression-test/suites/query_p0/sort/sort.groovy | 34 +++++++++++++++++++
 4 files changed, 68 insertions(+), 17 deletions(-)

diff --git a/be/src/vec/olap/vcollect_iterator.cpp 
b/be/src/vec/olap/vcollect_iterator.cpp
index 7c51a1be2b..e5874f8597 100644
--- a/be/src/vec/olap/vcollect_iterator.cpp
+++ b/be/src/vec/olap/vcollect_iterator.cpp
@@ -132,23 +132,11 @@ Status 
VCollectIterator::build_heap(std::vector<RowsetReaderSharedPtr>& rs_reade
                     new Level1Iterator(_children, _reader, _merge, 
_is_reverse, _skip_same));
         }
     } else {
-        bool have_multiple_child = false;
-        bool is_first_child = true;
-        for (auto iter = _children.begin(); iter != _children.end();) {
-            auto s = (*iter)->init_for_union(is_first_child, 
have_multiple_child);
-            if (!s.ok()) {
-                delete (*iter);
-                iter = _children.erase(iter);
-                if (!s.is<END_OF_FILE>()) {
-                    return s;
-                }
-            } else {
-                have_multiple_child = true;
-                is_first_child = false;
-                ++iter;
-            }
-        }
-        _inner_iter.reset(new Level1Iterator(_children, _reader, _merge, 
_is_reverse, _skip_same));
+        auto level1_iter = std::make_unique<Level1Iterator>(_children, 
_reader, _merge, _is_reverse,
+                                                            _skip_same);
+        _children.clear();
+        RETURN_IF_ERROR(level1_iter->init_level0_iterators_for_union());
+        _inner_iter.reset(level1_iter.release());
     }
     RETURN_IF_NOT_EOF_AND_OK(_inner_iter->init());
     // Clear _children earlier to release any related references
@@ -454,6 +442,27 @@ Status VCollectIterator::Level1Iterator::init(bool 
get_data_by_ref) {
     return Status::OK();
 }
 
+Status VCollectIterator::Level1Iterator::init_level0_iterators_for_union() {
+    bool have_multiple_child = false;
+    bool is_first_child = true;
+    for (auto iter = _children.begin(); iter != _children.end();) {
+        auto s = (*iter)->init_for_union(is_first_child, have_multiple_child);
+        if (!s.ok()) {
+            delete (*iter);
+            iter = _children.erase(iter);
+            if (!s.is<END_OF_FILE>()) {
+                return s;
+            }
+        } else {
+            have_multiple_child = true;
+            is_first_child = false;
+            ++iter;
+        }
+    }
+
+    return Status::OK();
+}
+
 Status VCollectIterator::Level1Iterator::_merge_next(IteratorRowRef* ref) {
     _heap->pop();
     auto res = _cur_child->next(ref);
diff --git a/be/src/vec/olap/vcollect_iterator.h 
b/be/src/vec/olap/vcollect_iterator.h
index 14785b5984..604bfaeba4 100644
--- a/be/src/vec/olap/vcollect_iterator.h
+++ b/be/src/vec/olap/vcollect_iterator.h
@@ -246,6 +246,8 @@ private:
             return false;
         }
 
+        Status init_level0_iterators_for_union();
+
     private:
         Status _merge_next(IteratorRowRef* ref);
 
diff --git a/regression-test/data/query_p0/sort/sort.out 
b/regression-test/data/query_p0/sort/sort.out
index 4e898a3f62..b9ce854f6e 100644
--- a/regression-test/data/query_p0/sort/sort.out
+++ b/regression-test/data/query_p0/sort/sort.out
@@ -10,3 +10,9 @@
 -- !sort_string_on_fe --
 true
 
+-- !sql_orderby_non_overlap_desc --
+2023-03-21T09:00       area1   p0      aaaaa   ddddd6  100.000 100.000 100.000 
100.000 2023-03-21T17:00
+2023-03-21T08:00       area1   p0      aaaaa   ddddd5  100.000 100.000 100.000 
100.000 2023-03-21T17:00
+2023-03-21T07:00       area1   p0      aaaaa   ddddd2  100.000 100.000 100.000 
100.000 2023-03-21T17:00
+2023-03-21T06:00       area1   p0      aaaaa   ddddd1  100.000 100.000 100.000 
100.000 2023-03-21T17:00
+
diff --git a/regression-test/suites/query_p0/sort/sort.groovy 
b/regression-test/suites/query_p0/sort/sort.groovy
index 2d41adf192..3c9aab3fad 100644
--- a/regression-test/suites/query_p0/sort/sort.groovy
+++ b/regression-test/suites/query_p0/sort/sort.groovy
@@ -23,4 +23,38 @@ suite("sort") {
     qt_sort_string_single_column """ select * from ( select '汇总' as a union 
all select '2022-01-01' as a ) a order by 1 """
     qt_sort_string_multiple_columns """ select * from ( select '汇总' as a,1 as 
b union all select '2022-01-01' as a,1 as b ) a order by 1,2 """
     qt_sort_string_on_fe """ select '汇总' > '2022-01-01' """
+
+    sql """ DROP TABLE if exists `sort_non_overlap`; """
+    sql """ CREATE TABLE `sort_non_overlap` (
+      `time_period` datetime NOT NULL,
+      `area_name` varchar(255) NOT NULL,
+      `province` varchar(255) NOT NULL,
+      `res_name` varchar(255) NOT NULL,
+      `dev` varchar(255) NOT NULL,
+      `dec0` decimal(10, 3) REPLACE_IF_NOT_NULL NULL,
+      `dec1` decimal(10, 3) REPLACE_IF_NOT_NULL NULL,
+      `dec2` decimal(10, 3) REPLACE_IF_NOT_NULL NULL,
+      `dec3` decimal(10, 3) REPLACE_IF_NOT_NULL NULL,
+      `update_time` datetime REPLACE NULL
+    ) ENGINE=OLAP
+    AGGREGATE KEY(`time_period`, `area_name`, `province`, `res_name`, `dev`)
+    DISTRIBUTED BY HASH(`area_name`) BUCKETS 1
+    PROPERTIES (
+    "replication_allocation" = "tag.location.default: 1",
+    "disable_auto_compaction" = "true"
+    );
+    """
+
+    sql """ insert into sort_non_overlap values
+        ('2023-03-21 06:00:00', 'area1', 'p0', 'aaaaa', 'ddddd1', 100, 100, 
100, 100, '2023-03-21 17:00:00'),
+        ('2023-03-21 07:00:00', 'area1', 'p0', 'aaaaa', 'ddddd2', 100, 100, 
100, 100, '2023-03-21 17:00:00');
+    """
+
+    sql """ insert into sort_non_overlap values
+                ('2023-03-21 08:00:00', 'area1', 'p0', 'aaaaa', 'ddddd5', 100, 
100, 100, 100, '2023-03-21 17:00:00'),
+                ('2023-03-21 09:00:00', 'area1', 'p0', 'aaaaa', 'ddddd6', 100, 
100, 100, 100, '2023-03-21 17:00:00');
+    """
+
+    qt_sql_orderby_non_overlap_desc """ select * from sort_non_overlap order 
by time_period desc limit 4; """
 }
+


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to