forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_log_for_update.cpp
More file actions
292 lines (274 loc) · 10.5 KB
/
Copy pathob_log_for_update.cpp
File metadata and controls
292 lines (274 loc) · 10.5 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
/*
* 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 "sql/optimizer/ob_log_for_update.h"
#include "sql/optimizer/ob_log_table_scan.h"
using namespace oceanbase;
using namespace sql;
using namespace oceanbase::common;
ObLogForUpdate::ObLogForUpdate(ObLogPlan &plan)
: ObLogicalOperator(plan),
skip_locked_(false),
is_multi_part_dml_(false),
gi_charged_(false),
wait_ts_(-1),
lock_rownum_(NULL),
index_dml_info_()
{}
const char* ObLogForUpdate::get_name() const
{
const char *ret = "NOT SET";
if (is_multi_part_dml()) {
ret = "DISTRIBUTED FOR UPDATE";
} else {
ret = "FOR UPDATE";
}
return ret;
}
int ObLogForUpdate::get_plan_item_info(PlanText &plan_text,
ObSqlPlanItem &plan_item)
{
int ret = OB_SUCCESS;
const ObDMLStmt *stmt = NULL;
if (OB_ISNULL(get_plan()) || OB_ISNULL(stmt = get_plan()->get_stmt())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt is null", K(ret));
} else if (OB_FAIL(ObLogicalOperator::get_plan_item_info(plan_text, plan_item))) {
LOG_WARN("failed to get plan item info", K(ret));
} else {
BEGIN_BUF_PRINT;
if (OB_FAIL(BUF_PRINTF("lock tables"))) {
LOG_WARN("BUF_PRINTF fails", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && i < index_dml_info_.count(); ++i) {
TableItem *table = NULL;
if (OB_ISNULL(index_dml_info_.at(i)) ||
OB_ISNULL(table = stmt->get_table_item_by_id(index_dml_info_.at(i)->table_id_))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("index dml info is null", K(ret), K(index_dml_info_.at(i)), K(table));
} else if (OB_FAIL(BUF_PRINTF("%c%.*s%c",
i == 0 ? '(' : ' ',
table->get_table_name().length(),
table->get_table_name().ptr(),
i == index_dml_info_.count() - 1 ? ')' : ','))) {
LOG_WARN("failed to print lock table name", K(ret));
}
}
END_BUF_PRINT(plan_item.special_predicates_,
plan_item.special_predicates_len_);
}
return ret;
}
int ObLogForUpdate::compute_sharding_info()
{
int ret = OB_SUCCESS;
ObLogicalOperator *child = NULL;
if (OB_ISNULL(child = get_child(ObLogicalOperator::first_child))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(child), K(ret));
} else if (OB_FAIL(ObLogicalOperator::compute_sharding_info())) {
LOG_WARN("failed to compute sharding info", K(ret));
} else if (OB_ISNULL(get_sharding())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else {
is_partition_wise_ = !is_multi_part_dml_ && !child->is_exchange_allocated() &&
get_sharding()->is_distributed() &&
NULL != get_sharding()->get_phy_table_location_info();
}
return ret;
}
int ObLogForUpdate::allocate_granule_pre(AllocGIContext &ctx)
{
return pw_allocate_granule_pre(ctx);
}
int ObLogForUpdate::allocate_granule_post(AllocGIContext &ctx)
{
int ret = OB_SUCCESS;
bool is_partition_wise_state = ctx.is_in_partition_wise_state();
if (OB_ISNULL(get_plan())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(pw_allocate_granule_post(ctx))) { // After allocating GI, the status of ctx will be cleaned up
LOG_WARN("failed to allocate pw gi post", K(ret));
} else {
if (is_partition_wise_state && ctx.is_op_set_pw(this)) {
ObSEArray<ObLogicalOperator *, 2> tsc_ops;
if (OB_FAIL(find_all_tsc(tsc_ops, this))) {
LOG_WARN("failed to find all tsc", K(ret));
} else if (tsc_ops.count() < 1){
// do nothing
set_gi_above(true);
} else {
// tsc op and current dml operator both need to set gi above
ARRAY_FOREACH(tsc_ops, idx) {
ObLogTableScan *tsc_op = static_cast<ObLogTableScan*>(tsc_ops.at(idx));
tsc_op->set_gi_above(true);
}
set_gi_above(true);
}
} else {
LOG_TRACE("not allocate dml gi for px",
K(ret), K(ctx), K(ctx.is_op_set_pw(this)));
}
}
return ret;
}
int ObLogForUpdate::get_op_exprs(ObIArray<ObRawExpr*> &all_exprs)
{
int ret = OB_SUCCESS;
if (is_multi_part_dml() && OB_FAIL(generate_multi_part_partition_id_expr())) {
LOG_WARN("failed to generate update expr", K(ret));
} else if (OB_FAIL(get_for_update_dependant_exprs(all_exprs))) {
LOG_WARN("failed to get for update dependant exprs", K(ret));
} else if (NULL != lock_rownum_ && OB_FAIL(all_exprs.push_back(lock_rownum_))) {
LOG_WARN("failed to push back exprs", K(ret));
} else if (OB_FAIL(ObLogicalOperator::get_op_exprs(all_exprs))) {
LOG_WARN("failed to get op exprs", K(ret));
} else { /*do nothing*/ }
return ret;
}
int ObLogForUpdate::generate_multi_part_partition_id_expr()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(get_stmt()) || OB_ISNULL(get_plan())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(get_stmt()), K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && i < index_dml_info_.count(); ++i) {
ObRawExpr *part_expr = NULL;
if (OB_ISNULL(index_dml_info_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(get_plan()->gen_calc_part_id_expr(index_dml_info_.at(i)->loc_table_id_,
index_dml_info_.at(i)->ref_table_id_,
CALC_PARTITION_TABLET_ID,
part_expr))) {
LOG_WARN("failed to gen calc part id expr", K(ret));
} else if (OB_ISNULL(part_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else {
index_dml_info_.at(i)->old_part_id_expr_ = part_expr;
}
}
return ret;
}
int ObLogForUpdate::get_for_update_dependant_exprs(ObIArray<ObRawExpr*> &dep_exprs)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(get_stmt())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else {
const TableItem *table_item = NULL;
for (int64_t i = 0; OB_SUCC(ret) && i < index_dml_info_.count(); i++) {
if (OB_ISNULL(index_dml_info_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("index dml info is null", K(ret));
} else if (OB_FAIL(append(dep_exprs, index_dml_info_.at(i)->column_exprs_))) {
LOG_WARN("failed to append rowkey expr", K(ret));
} else if (!is_multi_part_dml()) {
/*do nothing*/
} else if (NULL != index_dml_info_.at(i)->old_part_id_expr_ &&
OB_FAIL(dep_exprs.push_back(index_dml_info_.at(i)->old_part_id_expr_))) {
LOG_WARN("failed to push back old partition id expr", K(ret));
} else { /*do nothing*/ }
}
// mark expr reference
for (int64_t i = 0; OB_SUCC(ret) && i < dep_exprs.count(); i++) {
if (OB_ISNULL(dep_exprs.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else {
dep_exprs.at(i)->set_explicited_reference();
}
}
}
return ret;
}
int ObLogForUpdate::est_cost()
{
int ret = OB_SUCCESS;
ObLogicalOperator *first_child = NULL;
if (OB_ISNULL(get_plan()) ||
OB_ISNULL(first_child = get_child(ObLogicalOperator::first_child))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("first child is null", K(ret));
} else {
// todo: refine for update cost
ObOptimizerContext &opt_ctx = get_plan()->get_optimizer_context();
set_op_cost(ObOptEstCost::cost_get_rows(first_child->get_card(), opt_ctx));
set_cost(first_child->get_cost() + op_cost_);
set_card(first_child->get_card());
}
return ret;
}
int ObLogForUpdate::compute_op_ordering()
{
int ret = OB_SUCCESS;
ObLogicalOperator *child = NULL;
if (OB_ISNULL(child = get_child(first_child))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Child is null", K(ret));
} else if (OB_FAIL(set_op_ordering(child->get_op_ordering()))) {
LOG_WARN("Failed to set op ordering", K(ret));
}
return ret;
}
bool ObLogForUpdate::is_multi_table_skip_locked()
{
bool is_multi_table_skip_locked = false;
if (index_dml_info_.count() > 1 && is_skip_locked()) {
is_multi_table_skip_locked = true;
}
return is_multi_table_skip_locked;
}
int ObLogForUpdate::inner_replace_op_exprs(ObRawExprReplacer &replacer)
{
int ret = OB_SUCCESS;
for (int64_t i = 0; OB_SUCC(ret) && i < index_dml_info_.count(); ++i) {
IndexDMLInfo *dml_info = index_dml_info_.at(i);
if (OB_ISNULL(dml_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("dml info is null", K(ret));
} else if (OB_FAIL(replace_exprs_action(replacer, dml_info->ck_cst_exprs_))) {
LOG_WARN("failed to replace exprs", K(ret));
} else if (NULL != dml_info->new_part_id_expr_ &&
OB_FAIL(replace_expr_action(replacer, dml_info->new_part_id_expr_))) {
LOG_WARN("failed to replace new parititon id expr", K(ret));
} else if (NULL != dml_info->old_part_id_expr_ &&
OB_FAIL(replace_expr_action(replacer, dml_info->old_part_id_expr_))) {
LOG_WARN("failed to replace old parititon id expr", K(ret));
} else if (NULL != dml_info->old_rowid_expr_ &&
OB_FAIL(replace_expr_action(replacer, dml_info->old_rowid_expr_))) {
LOG_WARN("failed to replace old rowid expr", K(ret));
} else if (NULL != dml_info->new_rowid_expr_ &&
OB_FAIL(replace_expr_action(replacer, dml_info->new_rowid_expr_))) {
LOG_WARN("failed to replace new rowid expr", K(ret));
} else if (OB_FAIL(replace_exprs_action(replacer, dml_info->column_convert_exprs_))) {
LOG_WARN("failed to replace exprs", K(ret));
} else if (OB_FAIL(replace_exprs_action(replacer, dml_info->column_old_values_exprs_))) {
LOG_WARN("failed to replace column old values exprs ", K(ret));
}
for (int64_t j = 0; OB_SUCC(ret) && j < dml_info->assignments_.count(); ++j) {
if (OB_FAIL(replace_expr_action(replacer, dml_info->assignments_.at(j).expr_))) {
LOG_WARN("failed to replace expr", K(ret));
}
}
}
return ret;
}