forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_log_limit.cpp
More file actions
321 lines (304 loc) · 11.7 KB
/
ob_log_limit.cpp
File metadata and controls
321 lines (304 loc) · 11.7 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* Copyright (c) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL_OPT
#include "ob_log_limit.h"
#include "ob_log_group_by.h"
#include "ob_log_operator_factory.h"
#include "ob_log_sort.h"
#include "ob_log_table_scan.h"
#include "ob_optimizer_util.h"
#include "ob_opt_est_cost.h"
#include "ob_log_exchange.h"
#include "sql/rewrite/ob_transform_utils.h"
using namespace oceanbase::sql;
using namespace oceanbase::common;
using namespace oceanbase::sql::log_op_def;
int ObLogLimit::copy_without_child(ObLogicalOperator*& out)
{
int ret = OB_SUCCESS;
out = NULL;
ObLogicalOperator* op = NULL;
ObLogLimit* limit = NULL;
if (OB_FAIL(clone(op))) {
LOG_WARN("failed to clone ObLogLimit", K(ret));
} else if (OB_ISNULL(limit = static_cast<ObLogLimit*>(op))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("failed to cast ObLogicalOperator * to ObLogLimit *", K(ret));
} else {
limit->set_calc_found_rows(is_calc_found_rows_);
limit->set_has_union_child(has_union_child_);
limit->set_fetch_with_ties(is_fetch_with_ties());
limit->set_limit_count(limit_count_);
limit->set_limit_offset(limit_offset_);
limit->set_limit_percent(limit_percent_);
out = limit;
}
return ret;
}
int ObLogLimit::allocate_exchange_post(AllocExchContext* ctx)
{
int ret = OB_SUCCESS;
bool is_basic = false;
bool should_push_limit = (!is_calc_found_rows_ && (limit_percent_ == NULL));
ObLogicalOperator* exchange_point = NULL;
ObExchangeInfo exch_info;
if (OB_ISNULL(ctx)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ctx), K(ret));
} else if (OB_FAIL(compute_basic_sharding_info(ctx, is_basic))) {
LOG_WARN("failed to compute basic sharding info", K(ret));
} else if (is_basic) {
LOG_TRACE("is basic sharding info", K(sharding_info_));
} else if (OB_FAIL(push_down_limit(
ctx, limit_count_, limit_offset_, should_push_limit, is_fetch_with_ties(), exchange_point))) {
LOG_WARN("failed to push down limit", K(ret));
} else if (OB_ISNULL(exchange_point)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(exchange_point), K(ret));
} else if (OB_FAIL(exchange_point->allocate_exchange_nodes_below(first_child, *ctx, exch_info))) {
LOG_WARN("failed to allocate exchange nodes below", K(ret));
} else {
sharding_info_.set_location_type(OB_TBL_LOCATION_LOCAL);
}
return ret;
}
int ObLogLimit::transmit_op_ordering()
{
int ret = OB_SUCCESS;
bool need_sort = false;
if (OB_FAIL(check_need_sort_below_node(0, expected_ordering_, need_sort))) {
LOG_WARN("failed to check need sort", K(ret));
} else if (!need_sort) {
// do nothing
} else if (OB_FAIL(allocate_sort_below(0, expected_ordering_))) {
LOG_WARN("failed to allocate sort", K(ret));
}
if (OB_SUCC(ret)) {
if (OB_FAIL(ObLogicalOperator::transmit_op_ordering())) {
LOG_WARN("failed to transmit op ordering", K(ret));
}
}
return ret;
}
int ObLogLimit::allocate_expr_pre(ObAllocExprContext& ctx)
{
int ret = OB_SUCCESS;
ObSEArray<ObRawExpr*, 6> order_exprs;
ObSEArray<ObOrderDirection, 6> order_directions;
if (is_fetch_with_ties() &&
OB_FAIL(ObOptimizerUtil::split_expr_direction(get_expected_ordering(), order_exprs, order_directions))) {
LOG_WARN("failed to split expr and direction", K(ret));
} else if (OB_FAIL(add_exprs_to_ctx(ctx, order_exprs))) {
LOG_WARN("fail to add expr to ctx", K(ret));
} else if (OB_FAIL(ObLogicalOperator::allocate_expr_pre(ctx))) {
LOG_WARN("failed to add parent need expr", K(ret));
} else { /*do nothing*/
}
return ret;
}
int ObLogLimit::est_cost()
{
int ret = OB_SUCCESS;
int64_t limit_count = -1;
int64_t offset_count = 0;
double percentage = 0.0;
bool is_null_value = false;
if (OB_ISNULL(get_child(first_child)) || OB_ISNULL(get_plan())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Limit's child not expected to be NULL", K(ret));
} else if (limit_count_ != NULL && OB_FAIL(ObTransformUtils::get_limit_value(limit_count_,
get_plan()->get_stmt(),
get_plan()->get_optimizer_context().get_params(),
get_plan()->get_optimizer_context().get_session_info(),
&get_plan()->get_optimizer_context().get_allocator(),
limit_count,
is_null_value))) {
LOG_WARN("Get limit count num error", K(ret));
} else if (!is_null_value && limit_offset_ != NULL &&
OB_FAIL(ObTransformUtils::get_limit_value(limit_offset_,
get_plan()->get_stmt(),
get_plan()->get_optimizer_context().get_params(),
get_plan()->get_optimizer_context().get_session_info(),
&get_plan()->get_optimizer_context().get_allocator(),
offset_count,
is_null_value))) {
LOG_WARN("Get limit offset num error", K(ret));
} else if (!is_null_value && limit_percent_ != NULL &&
OB_FAIL(get_percentage_value(limit_percent_, percentage, is_null_value))) {
LOG_WARN("failed to get percentage value", K(ret));
} else {
double child_card = get_child(first_child)->get_card();
double limit_count_double = 0.0;
double offset_count_double = 0.0;
double scan_count_double = 0.0;
if (limit_count_ != NULL || limit_percent_ != NULL) {
limit_count = is_null_value ? 0 : limit_count;
offset_count = is_null_value ? 0 : offset_count;
if (limit_percent_ != NULL) {
limit_count_double = child_card * percentage / 100.0;
offset_count_double = static_cast<double>(offset_count);
} else {
limit_count_double = static_cast<double>(limit_count);
offset_count_double = static_cast<double>(offset_count);
}
scan_count_double = limit_count_double + offset_count_double;
if (scan_count_double <= child_card) {
set_card(limit_count_double);
} else {
limit_count_double = child_card - offset_count_double;
set_card(limit_count_double >= 0.0 ? limit_count_double : 0.0);
}
if (!is_calc_found_rows_ && NULL == limit_percent_) {
bool re_est = false;
if (OB_FAIL(get_child(first_child)->re_est_cost(this, scan_count_double, re_est))) {
LOG_WARN("Failed to re est cost", K(ret));
}
}
} else {
offset_count_double = static_cast<double>(offset_count);
if (OB_UNLIKELY(offset_count_double < 0)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("offset count double should greater than 0", K(ret), K(offset_count_double));
} else {
limit_count_double = is_null_value ? 0 : child_card - offset_count_double;
set_card(limit_count_double >= 0.0 ? limit_count_double : 0.0);
}
}
if (OB_SUCC(ret)) {
double op_cost = ObOptEstCost::cost_limit(get_child(first_child)->get_card());
set_op_cost(op_cost);
set_cost(get_child(first_child)->get_cost() + op_cost);
set_width(get_child(first_child)->get_width());
}
}
return ret;
}
int ObLogLimit::re_est_cost(const ObLogicalOperator* parent, double need_row_count, bool& re_est)
{
int ret = OB_SUCCESS;
UNUSED(parent);
re_est = false;
if (OB_UNLIKELY(need_row_count < 0.0)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("Need row count should not less than 0", K(ret));
} else if (need_row_count < get_card()) {
card_ = need_row_count;
int64_t offset_count = 0;
ObLogicalOperator* child = NULL;
bool is_null_value = false;
if (OB_ISNULL(child = get_child(first_child)) || OB_ISNULL(get_plan())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(child));
} else if (OB_FAIL(ObTransformUtils::get_limit_value(limit_offset_,
get_plan()->get_stmt(),
get_plan()->get_optimizer_context().get_params(),
get_plan()->get_optimizer_context().get_session_info(),
&get_plan()->get_optimizer_context().get_allocator(),
offset_count,
is_null_value))) {
LOG_WARN("Get limit offset num error", K(ret));
} else if (OB_FAIL(child->re_est_cost(this, static_cast<double>(offset_count) + need_row_count, re_est))) {
LOG_WARN("Failed to re_est child cost", K(ret));
} else {
re_est = true;
double op_cost = ObOptEstCost::cost_limit(child->get_card());
set_op_cost(op_cost);
set_cost(child->get_cost() + op_cost);
}
}
return ret;
}
uint64_t ObLogLimit::hash(uint64_t seed) const
{
uint64_t hash_value = seed;
hash_value = do_hash(is_calc_found_rows_, hash_value);
hash_value = do_hash(has_union_child_, hash_value);
hash_value = do_hash(is_top_limit_, hash_value);
hash_value = ObOptimizerUtil::hash_expr(limit_count_, hash_value);
hash_value = ObOptimizerUtil::hash_expr(limit_offset_, hash_value);
hash_value = ObLogicalOperator::hash(hash_value);
return hash_value;
}
int ObLogLimit::check_output_dep_specific(ObRawExprCheckDep& checker)
{
int ret = OB_SUCCESS;
if (NULL != limit_count_) {
if (OB_FAIL(checker.check(*limit_count_))) {
LOG_WARN("failed to check limit_count_", K(*limit_count_), K(ret));
}
} else { /* Do nothing */
}
if (OB_SUCC(ret)) {
if (NULL != limit_offset_) {
if (OB_FAIL(checker.check(*limit_offset_))) {
LOG_WARN("failed to check limit_offset_", K(limit_offset_), K(ret));
}
} else { /* Do nothing */
}
}
if (OB_SUCC(ret)) {
if (NULL != limit_percent_) {
if (OB_FAIL(checker.check(*limit_percent_))) {
LOG_WARN("failed to check limit_percent_", K(limit_percent_), K(ret));
}
}
}
if (OB_SUCC(ret) && is_fetch_with_ties()) {
ObIArray<OrderItem>& order_items = get_expected_ordering();
for (int64_t i = 0; OB_SUCC(ret) && i < order_items.count(); ++i) {
ObRawExpr* cur_expr = order_items.at(i).expr_;
if (OB_ISNULL(cur_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null ptr", K(cur_expr), K(ret));
} else if (OB_FAIL(checker.check(*cur_expr))) {
LOG_WARN("failed to check expr", K(cur_expr), K(ret));
} else { /*do nothing*/
}
}
}
return ret;
}
int ObLogLimit::print_my_plan_annotation(char* buf, int64_t& buf_len, int64_t& pos, ExplainType type)
{
int ret = OB_SUCCESS;
if (NULL != limit_count_ || NULL != limit_offset_ || NULL != limit_percent_) {
if (OB_FAIL(BUF_PRINTF(", "))) {
LOG_WARN("BUF_PRINTF fails", K(ret));
} else {
ObRawExpr* limit = limit_count_;
ObRawExpr* offset = limit_offset_;
ObRawExpr* percent = limit_percent_;
EXPLAIN_PRINT_EXPR(limit, type);
BUF_PRINTF(", ");
EXPLAIN_PRINT_EXPR(offset, type);
if (limit_percent_ != NULL) {
BUF_PRINTF(", ");
EXPLAIN_PRINT_EXPR(percent, type);
}
if (is_fetch_with_ties_) {
BUF_PRINTF(", ");
BUF_PRINTF("with_ties(%s)", "true");
}
}
}
return ret;
}
int ObLogLimit::inner_append_not_produced_exprs(ObRawExprUniqueSet& raw_exprs) const
{
int ret = OB_SUCCESS;
OZ(raw_exprs.append(limit_count_));
OZ(raw_exprs.append(limit_offset_));
if (NULL != limit_percent_) {
OZ(raw_exprs.append(limit_percent_));
}
return ret;
}