forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_log_limit.cpp
More file actions
177 lines (170 loc) · 6.89 KB
/
Copy pathob_log_limit.cpp
File metadata and controls
177 lines (170 loc) · 6.89 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
/*
* Copyright (c) 2025 OceanBase.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define USING_LOG_PREFIX SQL_OPT
#include "ob_log_limit.h"
#include "ob_log_table_scan.h"
using namespace oceanbase::sql;
using namespace oceanbase::common;
using namespace oceanbase::sql::log_op_def;
int ObLogLimit::get_op_exprs(ObIArray<ObRawExpr*> &all_exprs)
{
int ret = OB_SUCCESS;
// Support with ties functionality, if there exists order by item, need to add the corresponding order by item
ObSEArray<ObOrderDirection, 6> order_directions;
if (is_fetch_with_ties() && OB_FAIL(ObOptimizerUtil::split_expr_direction(
order_items_,
all_exprs,
order_directions))) {
LOG_WARN("failed to split expr and direction", K(ret));
} else if (NULL != limit_expr_ && OB_FAIL(all_exprs.push_back(limit_expr_))) {
LOG_WARN("failed to push back expr", K(ret));
} else if (NULL != offset_expr_ && OB_FAIL(all_exprs.push_back(offset_expr_))) {
LOG_WARN("failed to push back expr", K(ret));
} else if (NULL != percent_expr_ && OB_FAIL(all_exprs.push_back(percent_expr_))) {
LOG_WARN("failed to push back expr", K(ret));
} else if (OB_FAIL(ObLogicalOperator::get_op_exprs(all_exprs))) {
LOG_WARN("failed to push back expr", K(ret));
} else {/*do nothing*/ }
return ret;
}
int ObLogLimit::est_cost()
{
int ret = OB_SUCCESS;
double card = 0.0;
double op_cost = 0.0;
double cost = 0.0;
EstimateCostInfo param;
param.need_parallel_ = get_parallel();
if (OB_FAIL(do_re_est_cost(param, card_, op_cost_, cost_))) {
LOG_WARN("failed to est cost", K(ret));
} else {
LOG_TRACE("succeed to estimate limit-k cost", K(card_), K(op_cost_), K(cost_));
}
return ret;
}
int ObLogLimit::do_re_est_cost(EstimateCostInfo ¶m, double &card, double &op_cost, double &cost)
{
int ret = OB_SUCCESS;
card = 0.0;
op_cost = 0.0;
cost = 0.0;
int64_t limit_count = -1;
int64_t offset_count = 0;
double limit_percent = -1;
ObLogicalOperator *child = NULL;
const int64_t parallel = param.need_parallel_;
if (OB_ISNULL(get_plan()) || OB_ISNULL(child = get_child(first_child))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(get_plan()), K(child), K(ret));
} else if (OB_UNLIKELY((parallel > 1 && (NULL != percent_expr_ || NULL != offset_expr_))
|| parallel < 1)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected parallel degree", K(ret), K(param), K(percent_expr_), K(offset_expr_));
} else if (OB_FAIL(get_limit_offset_value(percent_expr_, limit_expr_, offset_expr_,
limit_percent, limit_count, offset_count))) {
LOG_WARN("failed to get limit offset value", K(ret));
} else {
card = 0 > param.need_row_count_ ? child->get_card() : param.need_row_count_;
if (0 <= limit_count) {
if (parallel > 1) {
card = std::min(card, child->get_card());
card = std::min(card, static_cast<double>(limit_count * parallel));
} else {
card = std::min(card, child->get_card() - offset_count);
card = std::min(card, static_cast<double>(limit_count));
}
card = std::max(0.0, card);
param.need_row_count_ = std::min(card + offset_count, child->get_card());
} else if (0 <= limit_percent) {
// reset need_row_count_ when exists limit_percent
card = std::min(card, child->get_card());
param.need_row_count_ = -1;
} else {
// 1. exists dynamic param in limit/offset or is_calc_found_rows_
// 2. has offset without limit / percent
card = std::min(card, child->get_card() - offset_count);
card = std::max(0.0, card);
param.need_row_count_ = std::min(card + offset_count, child->get_card());
}
if (OB_SUCC(ret)) {
double child_card = 0.0;
double child_cost = 0.0;
ObOptimizerContext &opt_ctx = get_plan()->get_optimizer_context();
if (is_calc_found_rows_) {
param.need_row_count_ = -1;
}
if (OB_FAIL(child->re_est_cost(param, child_card, child_cost))) {
LOG_WARN("failed to re-est cost", K(ret));
} else {
op_cost = ObOptEstCost::cost_get_rows(child_card, opt_ctx);
cost = op_cost + child_cost;
child_card = 0 <= limit_percent
? std::max(child_card * limit_percent / 100 - offset_count, 0.0)
: std::max(child_card - offset_count, 0.0);
child_card = std::max(0.0, child_card);
card = std::min(card, child_card);
}
}
}
return ret;
}
int ObLogLimit::get_plan_item_info(PlanText &plan_text,
ObSqlPlanItem &plan_item)
{
int ret = OB_SUCCESS;
if (OB_FAIL(ObLogicalOperator::get_plan_item_info(plan_text, plan_item))) {
LOG_WARN("failed to get plan item info", K(ret));
} else if (NULL != limit_expr_ || NULL != offset_expr_ || NULL != percent_expr_) {
BEGIN_BUF_PRINT;
ObRawExpr *limit = limit_expr_;
ObRawExpr *offset = offset_expr_;
ObRawExpr *percent = percent_expr_;
EXPLAIN_PRINT_EXPR(limit, type);
ret = BUF_PRINTF(", ");
EXPLAIN_PRINT_EXPR(offset, type);
if (percent_expr_ != NULL) {
BUF_PRINTF(", ");
EXPLAIN_PRINT_EXPR(percent, type);
}
if (is_fetch_with_ties_) {
BUF_PRINTF(", ");
BUF_PRINTF("with_ties(%s)", "true");
}
END_BUF_PRINT(plan_item.special_predicates_,
plan_item.special_predicates_len_);
}
return ret;
}
int ObLogLimit::inner_replace_op_exprs(ObRawExprReplacer &replacer)
{
int ret = OB_SUCCESS;
if (NULL != limit_expr_ && OB_FAIL(replace_expr_action(replacer, limit_expr_))) {
LOG_WARN("failed to replace limit expr", K(ret));
} else if (NULL != offset_expr_ && OB_FAIL(replace_expr_action(replacer, offset_expr_))) {
LOG_WARN("failed to replace offset expr", K(ret));
} else if (NULL != percent_expr_ && OB_FAIL(replace_expr_action(replacer, percent_expr_))) {
LOG_WARN("failed to replace percent expr", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && i < order_items_.count(); ++i) {
if (OB_ISNULL(order_items_.at(i).expr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null ", K(ret));
} else if (OB_FAIL(replace_expr_action(replacer, order_items_.at(i).expr_))) {
LOG_WARN("failed to adjust order expr with onetime", K(ret));
}
}
return ret;
}