forked from StarRocks/starrocks
-
Notifications
You must be signed in to change notification settings - Fork 2
/
local_partition_topn_context.cpp
160 lines (140 loc) · 6.82 KB
/
local_partition_topn_context.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// This file is licensed under the Elastic License 2.0. Copyright 2021-present, StarRocks Limited.
#include "exec/pipeline/sort/local_partition_topn_context.h"
#include <exec/vectorized/partition/chunks_partitioner.h>
#include "exec/vectorized/chunks_sorter_topn.h"
namespace starrocks::pipeline {
LocalPartitionTopnContext::LocalPartitionTopnContext(
const std::vector<TExpr>& t_partition_exprs, SortExecExprs& sort_exec_exprs, std::vector<bool> is_asc_order,
std::vector<bool> is_null_first, const std::string& sort_keys, int64_t offset, int64_t partition_limit,
const TTopNType::type topn_type, const std::vector<OrderByType>& order_by_types,
TupleDescriptor* materialized_tuple_desc, const RowDescriptor& parent_node_row_desc,
const RowDescriptor& parent_node_child_row_desc)
: _t_partition_exprs(t_partition_exprs),
_sort_exec_exprs(sort_exec_exprs),
_is_asc_order(is_asc_order),
_is_null_first(is_null_first),
_sort_keys(sort_keys),
_offset(offset),
_partition_limit(partition_limit),
_topn_type(topn_type),
_order_by_types(order_by_types),
_materialized_tuple_desc(materialized_tuple_desc),
_parent_node_row_desc(parent_node_row_desc),
_parent_node_child_row_desc(parent_node_child_row_desc) {}
Status LocalPartitionTopnContext::prepare(RuntimeState* state) {
RETURN_IF_ERROR(Expr::create_expr_trees(state->obj_pool(), _t_partition_exprs, &_partition_exprs));
for (auto& expr : _partition_exprs) {
auto& type_desc = expr->root()->type();
if (!type_desc.support_groupby()) {
return Status::NotSupported(fmt::format("partition by type {} is not supported", type_desc.debug_string()));
}
}
auto partition_size = _t_partition_exprs.size();
_partition_types.resize(partition_size);
for (auto i = 0; i < partition_size; ++i) {
TExprNode expr = _t_partition_exprs[i].nodes[0];
_partition_types[i].result_type = TypeDescriptor::from_thrift(expr.type);
_partition_types[i].is_nullable = expr.is_nullable;
_has_nullable_key = _has_nullable_key || _partition_types[i].is_nullable;
}
_chunks_partitioner =
std::make_unique<vectorized::ChunksPartitioner>(_has_nullable_key, _partition_exprs, _partition_types);
return _chunks_partitioner->prepare(state);
}
Status LocalPartitionTopnContext::push_one_chunk_to_partitioner(RuntimeState* state,
const vectorized::ChunkPtr& chunk) {
auto st = _chunks_partitioner->offer(chunk);
if (_chunks_partitioner->is_downgrade()) {
transfer_all_chunks_from_partitioner_to_sorters(state);
}
return st;
}
void LocalPartitionTopnContext::sink_complete() {
_is_sink_complete = true;
}
Status LocalPartitionTopnContext::transfer_all_chunks_from_partitioner_to_sorters(RuntimeState* state) {
if (_is_transfered) {
return Status::OK();
}
const auto num_partitions = _chunks_partitioner->num_partitions();
_chunks_sorters.resize(num_partitions);
for (int i = 0; i < num_partitions; ++i) {
_chunks_sorters[i] = std::make_shared<vectorized::ChunksSorterTopn>(
state, &_sort_exec_exprs.lhs_ordering_expr_ctxs(), &_is_asc_order, &_is_null_first, _sort_keys, _offset,
_partition_limit, _topn_type, vectorized::ChunksSorterTopn::tunning_buffered_chunks(_partition_limit));
}
RETURN_IF_ERROR(_chunks_partitioner->consume_from_hash_map(
[this, state](int32_t partition_idx, const vectorized::ChunkPtr& chunk) {
_chunks_sorters[partition_idx]->update(state, chunk);
return true;
}));
for (auto& chunks_sorter : _chunks_sorters) {
RETURN_IF_ERROR(chunks_sorter->done(state));
}
_is_transfered = true;
return Status::OK();
}
bool LocalPartitionTopnContext::has_output() {
if (_chunks_partitioner->is_downgrade() && _is_transfered) {
return _sorter_index < _chunks_sorters.size() || !_chunks_partitioner->is_downgrade_buffer_empty();
}
return _is_sink_complete && _sorter_index < _chunks_sorters.size();
}
bool LocalPartitionTopnContext::is_finished() {
if (!_is_sink_complete) {
return false;
}
return !has_output();
}
StatusOr<vectorized::ChunkPtr> LocalPartitionTopnContext::pull_one_chunk() {
vectorized::ChunkPtr chunk = nullptr;
if (_sorter_index < _chunks_sorters.size()) {
ASSIGN_OR_RETURN(chunk, pull_one_chunk_from_sorters());
if (chunk != nullptr) {
return chunk;
}
}
chunk = _chunks_partitioner->consume_from_downgrade_buffer();
return chunk;
}
StatusOr<vectorized::ChunkPtr> LocalPartitionTopnContext::pull_one_chunk_from_sorters() {
auto& chunks_sorter = _chunks_sorters[_sorter_index];
vectorized::ChunkPtr chunk = nullptr;
bool eos = false;
RETURN_IF_ERROR(chunks_sorter->get_next(&chunk, &eos));
if (eos) {
// Current sorter has no output, try to get chunk from next sorter
_sorter_index++;
}
return chunk;
}
LocalPartitionTopnContextFactory::LocalPartitionTopnContextFactory(
const int32_t degree_of_parallelism, const std::vector<TExpr>& t_partition_exprs,
SortExecExprs& sort_exec_exprs, std::vector<bool> is_asc_order, std::vector<bool> is_null_first,
const std::string& sort_keys, int64_t offset, int64_t partition_limit, const TTopNType::type topn_type,
const std::vector<OrderByType>& order_by_types, TupleDescriptor* materialized_tuple_desc,
const RowDescriptor& parent_node_row_desc, const RowDescriptor& parent_node_child_row_desc)
: _ctxs(degree_of_parallelism),
_t_partition_exprs(t_partition_exprs),
_sort_exec_exprs(sort_exec_exprs),
_is_asc_order(is_asc_order),
_is_null_first(is_null_first),
_sort_keys(sort_keys),
_offset(offset),
_partition_limit(partition_limit),
_topn_type(topn_type),
_order_by_types(order_by_types),
_materialized_tuple_desc(materialized_tuple_desc),
_parent_node_row_desc(parent_node_row_desc),
_parent_node_child_row_desc(parent_node_child_row_desc) {}
LocalPartitionTopnContext* LocalPartitionTopnContextFactory::create(int32_t driver_sequence) {
DCHECK_LT(driver_sequence, _ctxs.size());
if (_ctxs[driver_sequence] == nullptr) {
_ctxs[driver_sequence] = std::make_shared<LocalPartitionTopnContext>(
_t_partition_exprs, _sort_exec_exprs, _is_asc_order, _is_null_first, _sort_keys, _offset,
_partition_limit, _topn_type, _order_by_types, _materialized_tuple_desc, _parent_node_row_desc,
_parent_node_child_row_desc);
}
return _ctxs[driver_sequence].get();
}
} // namespace starrocks::pipeline