Skip to content

Commit

Permalink
[fix](order by) fix bug of order by desc when rowsets is no overlapping
Browse files Browse the repository at this point in the history
  • Loading branch information
jacktengg committed Mar 26, 2023
1 parent 5463ba6 commit 1c3354c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
42 changes: 25 additions & 17 deletions be/src/vec/olap/vcollect_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,10 @@ 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);
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
Expand Down Expand Up @@ -639,6 +626,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);
Expand Down
2 changes: 2 additions & 0 deletions be/src/vec/olap/vcollect_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ class VCollectIterator {
return false;
}

Status init_level0_iterators_for_union();

private:
Status _merge_next(IteratorRowRef* ref);

Expand Down
6 changes: 6 additions & 0 deletions regression-test/data/query_p0/sort/sort.out
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ z

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

32 changes: 32 additions & 0 deletions regression-test/suites/query_p0/sort/sort.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,36 @@ suite("sort") {
);"""
sql """insert into test_convert values("b"),("z"),("a"), ("c"), ("睿"), ("多"), ("丝");"""
qt_sql """select * from test_convert order by convert(a using gbk);"""

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(`time_period`, `area_name`, `province`, `res_name`, `dev`) BUCKETS 4
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
);
"""

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; """
}

0 comments on commit 1c3354c

Please sign in to comment.