forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_log_merge.cpp
More file actions
369 lines (344 loc) · 13.5 KB
/
ob_log_merge.cpp
File metadata and controls
369 lines (344 loc) · 13.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
* 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 "sql/resolver/dml/ob_merge_stmt.h"
#include "sql/optimizer/ob_log_merge.h"
#include "lib/container/ob_se_array.h"
#include "lib/container/ob_se_array_iterator.h"
#include "share/partition_table/ob_partition_location_cache.h"
#include "sql/ob_phy_table_location.h"
#include "sql/code_generator/ob_expr_generator_impl.h"
#include "sql/ob_sql_utils.h"
#include "sql/optimizer/ob_log_plan.h"
using namespace oceanbase;
using namespace sql;
using namespace oceanbase::common;
using namespace share;
using namespace oceanbase::share::schema;
int ObLogMerge::print_my_plan_annotation(char* buf, int64_t& buf_len, int64_t& pos, ExplainType type)
{
int ret = OB_SUCCESS;
if (OB_FAIL(ObLogInsert::print_my_plan_annotation(buf, buf_len, pos, type))) {
LOG_WARN("fail to print plan annotaqqtion", K(ret));
} else if (OB_FAIL(BUF_PRINTF(", "))) {
LOG_WARN("BUF_PRINTF fails", K(ret));
} else if (OB_FAIL(BUF_PRINTF("\n "))) {
LOG_WARN("BUF_PRINTF fails", K(ret));
} else if (OB_ISNULL(match_condition_exprs_) || OB_ISNULL(update_condition_exprs_) ||
OB_ISNULL(insert_condition_exprs_) || OB_ISNULL(delete_condition_exprs_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("some condition exprs is NULL",
KPC(match_condition_exprs_),
KPC(update_condition_exprs_),
KPC(insert_condition_exprs_),
KPC(delete_condition_exprs_),
K(ret));
} else {
const ObIArray<ObRawExpr*>& match_conds = *match_condition_exprs_;
const ObIArray<ObRawExpr*>& update_conds = *update_condition_exprs_;
const ObIArray<ObRawExpr*>& delete_conds = *delete_condition_exprs_;
const ObIArray<ObRawExpr*>& insert_conds = *insert_condition_exprs_;
EXPLAIN_PRINT_EXPRS(match_conds, type);
if (OB_SUCC(ret) && OB_FAIL(BUF_PRINTF(", "))) {
LOG_WARN("BUF_PRINTF fails", K(ret));
}
EXPLAIN_PRINT_EXPRS(insert_conds, type);
if (OB_FAIL(ret)) {
} else if (OB_FAIL(BUF_PRINTF(", "))) {
LOG_WARN("BUF_PRINTF fails", K(ret));
} else if (OB_FAIL(BUF_PRINTF("\n "))) {
LOG_WARN("BUF_PRINTF fails", K(ret));
}
EXPLAIN_PRINT_EXPRS(update_conds, type);
if (OB_SUCC(ret) && OB_FAIL(BUF_PRINTF(", "))) {
LOG_WARN("BUF_PRINTF fails", K(ret));
}
EXPLAIN_PRINT_EXPRS(delete_conds, type);
}
return ret;
}
int ObLogMerge::add_all_table_assignments_to_ctx(ObAllocExprContext& ctx)
{
int ret = OB_SUCCESS;
int64_t table_cnt = tables_assignments_->count();
if (table_cnt == 0) {
// do nothing
} else if (table_cnt == 1) {
ObSEArray<ObRawExpr*, 8> exprs;
const ObAssignments& assigns = tables_assignments_->at(0).assignments_;
for (int64_t i = 0; OB_SUCC(ret) && i < assigns.count(); i++) {
if (OB_ISNULL(assigns.at(i).expr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(exprs.push_back(assigns.at(i).expr_))) {
LOG_WARN("failed to push back exprs", K(ret));
} else { /*do nothing*/
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(add_merge_exprs_to_ctx(ctx, exprs))) {
LOG_WARN("failed to add exprs to ctx", K(ret));
} else { /*do nothing*/
}
}
} else {
LOG_WARN("unexpected assignment table count", K(table_cnt));
}
return ret;
}
int ObLogMerge::allocate_expr_pre(ObAllocExprContext& ctx)
{
int ret = OB_SUCCESS;
if (OB_FAIL(add_all_table_assignments_to_ctx(ctx))) {
LOG_WARN("fail to add all table assignment", K(ret));
} else if (NULL != match_condition_exprs_ && OB_FAIL(add_merge_exprs_to_ctx(ctx, *match_condition_exprs_))) {
LOG_WARN("fail to add expr to ctx", K(ret));
} else if (NULL != insert_condition_exprs_ && OB_FAIL(add_merge_exprs_to_ctx(ctx, *insert_condition_exprs_))) {
LOG_WARN("fail to add expr to ctx", K(ret));
} else if (NULL != update_condition_exprs_ && OB_FAIL(add_merge_exprs_to_ctx(ctx, *update_condition_exprs_))) {
LOG_WARN("fail to add expr to ctx", K(ret));
} else if (OB_FAIL(add_delete_exprs_to_ctx(ctx))) {
LOG_WARN("fail to add expr to ctx", K(ret));
} else if (nullptr != value_vector_ && OB_FAIL(add_merge_exprs_to_ctx(ctx, *value_vector_))) {
LOG_WARN("fail to add expr to ctx", K(ret));
} else if (OB_FAIL(add_all_source_table_columns_to_ctx(ctx))) {
LOG_WARN("fail to add source table columns to ctx", K(ret));
} else if (OB_FAIL(ObLogicalOperator::allocate_expr_pre(ctx))) {
LOG_WARN("failed to add parent need expr", K(ret));
}
return ret;
}
int ObLogMerge::add_merge_exprs_to_ctx(ObAllocExprContext& ctx, const ObIArray<ObRawExpr*>& exprs)
{
int ret = OB_SUCCESS;
ObLogicalOperator* child = NULL;
uint64_t producer_id = OB_INVALID_ID;
ObSEArray<ObRawExpr*, 8> subquery_exprs;
ObSEArray<ObRawExpr*, 8> non_subquery_exprs;
if (OB_FAIL(classify_merge_subquery_expr(exprs, subquery_exprs, non_subquery_exprs))) {
LOG_WARN("failed to classify merge subquery exprs", K(ret));
} else if (!non_subquery_exprs.empty() && OB_FAIL(add_exprs_to_ctx(ctx, non_subquery_exprs))) {
LOG_WARN("failed to add exprs to ctx", K(ret));
} else if (subquery_exprs.empty()) {
/*do nothing*/
} else if (OB_ISNULL(child = get_child(ObLogicalOperator::first_child))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(get_next_producer_id(child, producer_id))) {
LOG_WARN("failed to get next producer id", K(ret));
} else if (OB_FAIL(add_exprs_to_ctx(ctx, subquery_exprs, producer_id))) {
LOG_WARN("failed to add exprs to ctx", K(ret));
} else { /*do nothing*/
}
return ret;
}
int ObLogMerge::classify_merge_subquery_expr(
const ObIArray<ObRawExpr*>& exprs, ObIArray<ObRawExpr*>& subquery_exprs, ObIArray<ObRawExpr*>& non_subquery_exprs)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(get_plan())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < exprs.count(); i++) {
bool has_subquery = false;
if (OB_ISNULL(exprs.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(ObOptimizerUtil::check_expr_contain_subquery(
exprs.at(i), &get_plan()->get_onetime_exprs(), has_subquery))) {
LOG_WARN("failed to check whether contain subquery", K(ret));
} else if (has_subquery) {
ret = subquery_exprs.push_back(exprs.at(i));
} else {
ret = non_subquery_exprs.push_back(exprs.at(i));
}
}
}
return ret;
}
int ObLogMerge::add_delete_exprs_to_ctx(ObAllocExprContext& ctx)
{
int ret = OB_SUCCESS;
ObRawExpr* expr = NULL;
ObSEArray<ObRawExpr*, 8> subquery_exprs;
ObSEArray<ObRawExpr*, 8> non_subquery_exprs;
if (OB_ISNULL(delete_condition_exprs_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(classify_merge_subquery_expr(*delete_condition_exprs_, subquery_exprs, non_subquery_exprs))) {
LOG_WARN("failed to classify merge subquery exprs", K(ret));
} else {
if (!non_subquery_exprs.empty()) {
ObSEArray<ObRawExpr*, 8> delete_column_exprs;
if (OB_FAIL(ObRawExprUtils::extract_column_exprs(non_subquery_exprs, delete_column_exprs))) {
LOG_WARN("fail to extract column exprs", K(ret));
} else if (OB_FAIL(add_exprs_to_ctx(ctx, delete_column_exprs))) {
LOG_WARN("fail to add exprs to ctx", K(ret));
} else { /*do nothing*/
}
}
if (OB_SUCC(ret) && !subquery_exprs.empty()) {
ObLogicalOperator* child = NULL;
uint64_t producer_id = OB_INVALID_ID;
if (OB_ISNULL(child = get_child(ObLogicalOperator::first_child))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(get_next_producer_id(child, producer_id))) {
LOG_WARN("failed to get next producer id", K(ret));
} else if (OB_FAIL(add_exprs_to_ctx(ctx, subquery_exprs, producer_id))) {
LOG_WARN("failed to add exprs to ctx", K(ret));
} else { /*do nothing*/
}
}
}
return ret;
}
int ObLogMerge::check_output_dep_specific(ObRawExprCheckDep& checker)
{
int ret = OB_SUCCESS;
if (OB_FAIL(ObLogInsert::check_output_dep_specific(checker))) {
LOG_WARN("ObLogDelUpd::check_output_dep_specific fails", K(ret));
} else {
if (NULL != match_condition_exprs_) {
for (int64_t i = 0; OB_SUCC(ret) && i < match_condition_exprs_->count(); i++) {
if (OB_ISNULL(match_condition_exprs_->at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(checker.check(*match_condition_exprs_->at(i)))) {
LOG_WARN("failed to check expr", K(ret));
} else { /*do nothing*/
}
}
}
if (OB_SUCC(ret) && NULL != insert_condition_exprs_) {
for (int64_t i = 0; OB_SUCC(ret) && i < insert_condition_exprs_->count(); i++) {
if (OB_ISNULL(insert_condition_exprs_->at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(checker.check(*insert_condition_exprs_->at(i)))) {
LOG_WARN("failed to check expr", K(ret));
} else { /*do nothing*/
}
}
}
if (OB_SUCC(ret) && NULL != update_condition_exprs_) {
for (int64_t i = 0; OB_SUCC(ret) && i < update_condition_exprs_->count(); i++) {
if (OB_ISNULL(update_condition_exprs_->at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(checker.check(*update_condition_exprs_->at(i)))) {
LOG_WARN("failed to check expr", K(ret));
} else { /*do nothing*/
}
}
}
if (OB_SUCC(ret) && NULL != delete_condition_exprs_) {
for (int64_t i = 0; OB_SUCC(ret) && i < delete_condition_exprs_->count(); i++) {
if (OB_ISNULL(delete_condition_exprs_->at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(checker.check(*delete_condition_exprs_->at(i)))) {
LOG_WARN("failed to check expr", K(ret));
} else { /*do nothing*/
}
}
}
if (OB_SUCC(ret) && NULL != value_vector_) {
for (int64_t i = 0; OB_SUCC(ret) && i < value_vector_->count(); i++) {
if (OB_ISNULL(value_vector_->at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(checker.check(*value_vector_->at(i)))) {
LOG_WARN("failed to check expr", K(ret));
} else { /*do nothing*/
}
}
}
if (OB_SUCC(ret) && NULL != rowkey_exprs_) {
for (int64_t i = 0; OB_SUCC(ret) && i < rowkey_exprs_->count(); i++) {
if (OB_ISNULL(rowkey_exprs_->at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(checker.check(*rowkey_exprs_->at(i)))) {
LOG_WARN("failed to check expr", K(ret));
} else { /*do nothing*/
}
}
}
}
return ret;
}
uint64_t ObLogMerge::hash(uint64_t seed) const
{
if (NULL != match_condition_exprs_) {
HASH_PTR_ARRAY(*match_condition_exprs_, seed);
}
if (NULL != insert_condition_exprs_) {
HASH_PTR_ARRAY(*insert_condition_exprs_, seed);
}
if (NULL != update_condition_exprs_) {
HASH_PTR_ARRAY(*update_condition_exprs_, seed);
}
if (NULL != delete_condition_exprs_) {
HASH_PTR_ARRAY(*delete_condition_exprs_, seed);
}
if (NULL != value_vector_) {
HASH_PTR_ARRAY(*value_vector_, seed);
}
if (NULL != rowkey_exprs_) {
HASH_PTR_ARRAY(*rowkey_exprs_, seed);
}
seed = ObLogicalOperator::hash(seed);
return seed;
}
int ObLogMerge::add_all_source_table_columns_to_ctx(ObAllocExprContext& ctx)
{
int ret = OB_SUCCESS;
CK(OB_NOT_NULL(table_columns_), !table_columns_->empty());
OZ(add_exprs_to_ctx(ctx, *table_columns_));
return ret;
}
int ObLogMerge::inner_append_not_produced_exprs(ObRawExprUniqueSet& raw_exprs) const
{
int ret = OB_SUCCESS;
OZ(ObLogDelUpd::inner_append_not_produced_exprs(raw_exprs));
if (OB_SUCC(ret)) {
if (nullptr != match_condition_exprs_ && OB_FAIL(raw_exprs.append(*match_condition_exprs_))) {
LOG_WARN("append_array_no_dup failed", K(ret));
}
if (nullptr != insert_condition_exprs_ && OB_FAIL(raw_exprs.append(*insert_condition_exprs_))) {
LOG_WARN("append_array_no_dup failed", K(ret));
}
if (nullptr != update_condition_exprs_ && OB_FAIL(raw_exprs.append(*update_condition_exprs_))) {
LOG_WARN("append_array_no_dup failed", K(ret));
}
if (nullptr != delete_condition_exprs_ && OB_FAIL(raw_exprs.append(*delete_condition_exprs_))) {
LOG_WARN("append_array_no_dup failed", K(ret));
}
if (nullptr != rowkey_exprs_ && OB_FAIL(raw_exprs.append(*rowkey_exprs_))) {
LOG_WARN("append_array_no_dup failed", K(ret));
}
}
OZ(ObLogInsert::inner_append_not_produced_exprs(raw_exprs));
return ret;
}
const char* ObLogMerge::get_name() const
{
const char* ret = "NOT SET";
if (is_multi_part_dml()) {
ret = "MULTI PARTITION MERGE";
} else {
ret = "MERGE";
}
return ret;
}