forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_fd_item.cpp
More file actions
458 lines (431 loc) · 16.6 KB
/
ob_fd_item.cpp
File metadata and controls
458 lines (431 loc) · 16.6 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/**
* 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_JO
#include "sql/optimizer/ob_fd_item.h"
#include "sql/optimizer/ob_optimizer_util.h"
using namespace oceanbase;
using namespace sql;
using namespace oceanbase::common;
int ObFdItem::assign(const ObFdItem& other)
{
parent_exprs_ = other.parent_exprs_;
is_unique_ = other.is_unique_;
stmt_level_ = other.stmt_level_;
return OB_SUCCESS;
}
int ObFdItem::check_expr_in_child(const ObRawExpr* expr, const EqualSets& equal_sets, bool& is_in_child) const
{
int ret = OB_SUCCESS;
UNUSED(expr);
UNUSED(equal_sets);
is_in_child = false;
return ret;
}
int ObFdItem::check_exprs_in_child(
const ObIArray<ObRawExpr*>& exprs, const EqualSets& equal_sets, bool& is_in_child) const
{
int ret = OB_SUCCESS;
is_in_child = true;
for (int64_t i = 0; OB_SUCC(ret) && is_in_child && i < exprs.count(); ++i) {
if (OB_FAIL(check_expr_in_child(exprs.at(i), equal_sets, is_in_child))) {
LOG_WARN("failed to check expr in child", K(ret));
}
}
return ret;
}
int ObFdItem::check_exprs_in_child(
const ObIArray<ObRawExpr*>& exprs, const EqualSets& equal_sets, const int64_t pos_start, ObSqlBitSet<> exprs_set)
{
int ret = OB_SUCCESS;
bool in_child = false;
if (OB_UNLIKELY(pos_start < 0)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid array pos", K(pos_start), K(ret));
}
for (int64_t i = pos_start; OB_SUCC(ret) && i < exprs.count(); ++i) {
if (exprs_set.has_member(i)) {
// do nothing
} else if (OB_FAIL(check_expr_in_child(exprs.at(i), equal_sets, in_child))) {
LOG_WARN("failed to check expr in child", K(ret));
} else if (in_child && OB_FAIL(exprs_set.add_member(i))) {
LOG_WARN("failed to add member to set", K(ret));
}
}
return ret;
}
int ObFdItem::check_exprs_in_child(const common::ObIArray<OrderItem>& order_items, const EqualSets& equal_sets,
const int64_t pos_start, ObSqlBitSet<> exprs_set)
{
int ret = OB_SUCCESS;
bool in_child = false;
if (OB_UNLIKELY(pos_start < 0)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid array pos", K(pos_start), K(ret));
}
for (int64_t i = pos_start; OB_SUCC(ret) && i < order_items.count(); ++i) {
if (exprs_set.has_member(i)) {
// do nothing
} else if (OB_FAIL(check_expr_in_child(order_items.at(i).expr_, equal_sets, in_child))) {
LOG_WARN("failed to check expr in child", K(ret));
} else if (in_child && OB_FAIL(exprs_set.add_member(i))) {
LOG_WARN("failed to add member to set", K(ret));
}
}
return ret;
}
int ObTableFdItem::assign(const ObTableFdItem& other)
{
int ret = OB_SUCCESS;
if (OB_LIKELY(this != &other)) {
if (OB_FAIL(ObFdItem::assign(other))) {
LOG_WARN("failed to assign ObFdItem", K(ret));
} else {
child_tables_ = other.child_tables_;
}
}
return ret;
}
int ObTableFdItem::check_expr_in_child(const ObRawExpr* expr, const EqualSets& equal_sets, bool& is_in_child) const
{
int ret = OB_SUCCESS;
const ObRawExpr* cur_expr = NULL;
bool is_consistent = false;
is_in_child = false;
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get null expr", K(ret));
} else if (expr->get_expr_levels().has_member(stmt_level_)) {
if (child_tables_.is_superset(expr->get_relation_ids())) {
is_in_child = true;
}
for (int64_t i = 0; OB_SUCC(ret) && !is_in_child && i < equal_sets.count(); ++i) {
const EqualSet* equal_set = equal_sets.at(i);
if (OB_ISNULL(equal_set)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get null equal set", K(ret));
} else if (!ObOptimizerUtil::find_equal_expr(*equal_set, expr)) {
continue;
}
for (int64_t j = 0; OB_SUCC(ret) && !is_in_child && j < equal_set->count(); ++j) {
if (OB_ISNULL(cur_expr = equal_set->at(j))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get null expr", K(ret));
} else if (!child_tables_.is_superset(cur_expr->get_relation_ids())) {
// do nothing
} else if (OB_FAIL(ObRawExprUtils::expr_is_order_consistent(cur_expr, expr, is_consistent))) {
LOG_WARN("failed to check expr is order consistent", K(ret));
} else if (is_consistent) {
is_in_child = true;
}
}
}
}
return ret;
}
int ObExprFdItem::assign(const ObExprFdItem& other)
{
int ret = OB_SUCCESS;
if (OB_LIKELY(this != &other)) {
if (OB_FAIL(ObFdItem::assign(other))) {
LOG_WARN("failed to assign ObFdItem", K(ret));
} else if (OB_FAIL(child_exprs_.assign(other.child_exprs_))) {
LOG_WARN("failed to assign child tables", K(ret));
}
}
return ret;
}
int ObExprFdItem::check_expr_in_child(const ObRawExpr* expr, const EqualSets& equal_sets, bool& is_in_child) const
{
int ret = OB_SUCCESS;
is_in_child = ObOptimizerUtil::find_equal_expr(child_exprs_, expr, equal_sets);
return ret;
}
int ObFdItemFactory::create_fd_item_set(ObFdItemSet*& fd_item_set)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(fd_item_set = (ObFdItemSet*)allocator_.alloc(sizeof(ObFdItemSet)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to alloc fd item sets", K(ret));
} else {
fd_item_set = new (fd_item_set) ObFdItemSet();
if (OB_FAIL(item_set_store_.store_obj(fd_item_set))) {
LOG_WARN("failed to store obj", K(ret));
fd_item_set->~ObFdItemSet();
fd_item_set = NULL;
}
}
return ret;
}
int ObFdItemFactory::get_parent_exprs_ptr(ObRawExpr* parent_expr, ObRawExprSet*& parent_exprs_ptr)
{
int ret = OB_SUCCESS;
ObSEArray<ObRawExpr*, 1> parent_exprs;
if (OB_FAIL(parent_exprs.push_back(parent_expr))) {
LOG_WARN("failed to push back expr", K(ret));
} else {
ret = get_parent_exprs_ptr(parent_exprs, parent_exprs_ptr);
}
return ret;
}
int ObFdItemFactory::get_parent_exprs_ptr(const ObIArray<ObRawExpr*>& parent_exprs, ObRawExprSet*& parent_exprs_ptr)
{
int ret = OB_SUCCESS;
parent_exprs_ptr = NULL;
bool find = false;
if (OB_UNLIKELY(parent_exprs.empty())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get empty parent exprs", K(ret));
} else {
// check parent exprs exists
for (int64_t i = 0; OB_SUCC(ret) && !find && i < parent_sets_.count(); ++i) {
if (OB_ISNULL(parent_sets_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get null fd parent set", K(ret));
} else if (parent_exprs.count() == parent_sets_.at(i)->count() &&
ObOptimizerUtil::subset_exprs(*parent_sets_.at(i), parent_exprs)) {
parent_exprs_ptr = parent_sets_.at(i);
find = true;
}
}
// if not exists, create new one
if (find) {
// do nothing
} else if (OB_FAIL(ObRawExprSetUtils::add_expr_set(&allocator_, parent_exprs, parent_sets_))) {
LOG_WARN("failed to add expr set", K(ret));
} else {
parent_exprs_ptr = parent_sets_.at(parent_sets_.count() - 1);
}
}
return ret;
}
int ObFdItemFactory::create_table_fd_item(
ObTableFdItem*& fd_item, const bool is_unique, ObRawExprSet* parent_exprs, const int32_t stmt_level)
{
int ret = OB_SUCCESS;
fd_item = NULL;
void* ptr = allocator_.alloc(sizeof(ObTableFdItem));
if (OB_ISNULL(ptr)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("no memory to create ObTableFdItem", K(ret));
} else {
fd_item = new (ptr) ObTableFdItem(is_unique, parent_exprs, stmt_level);
if (OB_FAIL(item_store_.store_obj(fd_item))) {
LOG_WARN("failed to store obj", K(ret));
fd_item->~ObTableFdItem();
fd_item = NULL;
}
}
return ret;
}
int ObFdItemFactory::create_table_fd_item(ObTableFdItem*& fd_item, const bool is_unique,
const ObIArray<ObRawExpr*>& parent_exprs, const int32_t stmt_level, const ObRelIds& table_set)
{
int ret = OB_SUCCESS;
fd_item = NULL;
ObRawExprSet* parent_exprs_ptr = NULL;
if (OB_FAIL(get_parent_exprs_ptr(parent_exprs, parent_exprs_ptr))) {
LOG_WARN("failed to get parent exprs ptr", K(ret));
} else if (OB_FAIL(create_table_fd_item(fd_item, is_unique, parent_exprs_ptr, stmt_level))) {
LOG_WARN("failed to create table fd item", K(ret));
} else if (OB_FAIL(fd_item->get_child_tables().add_members(table_set))) {
LOG_WARN("failed to add members to child tables", K(ret));
}
return ret;
}
int ObFdItemFactory::create_table_fd_item(ObTableFdItem*& fd_item, const ObTableFdItem& other)
{
int ret = OB_SUCCESS;
fd_item = NULL;
if (OB_FAIL(create_table_fd_item(fd_item, other.is_unique(), other.get_parent_exprs(), other.get_stmt_level()))) {
LOG_WARN("failed to create table fd item", K(ret));
} else if (OB_FAIL(fd_item->get_child_tables().add_members(other.get_child_tables()))) {
LOG_WARN("failed to add member to child tables", K(ret));
}
return ret;
}
int ObFdItemFactory::create_expr_fd_item(
ObExprFdItem*& fd_item, const bool is_unique, ObRawExprSet* parent_exprs, const int32_t stmt_level)
{
int ret = OB_SUCCESS;
fd_item = NULL;
void* ptr = allocator_.alloc(sizeof(ObExprFdItem));
if (OB_ISNULL(ptr)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("no memory to create ObExprFdItem", K(ret));
} else {
fd_item = new (ptr) ObExprFdItem(is_unique, parent_exprs, stmt_level);
if (OB_FAIL(item_store_.store_obj(fd_item))) {
LOG_WARN("failed to store obj", K(ret));
fd_item->~ObExprFdItem();
fd_item = NULL;
}
}
return ret;
}
int ObFdItemFactory::create_expr_fd_item(ObExprFdItem*& fd_item, const bool is_unique,
const ObIArray<ObRawExpr*>& parent_exprs, const int32_t stmt_level, const ObIArray<ObRawExpr*>& child_exprs)
{
int ret = OB_SUCCESS;
fd_item = NULL;
ObRawExprSet* parent_exprs_ptr = NULL;
if (OB_FAIL(get_parent_exprs_ptr(parent_exprs, parent_exprs_ptr))) {
LOG_WARN("failed to get parent exprs ptr", K(ret));
} else if (OB_FAIL(create_expr_fd_item(fd_item, is_unique, parent_exprs_ptr, stmt_level))) {
LOG_WARN("failed to create table fd item", K(ret));
} else if (OB_FAIL(ObRawExprSetUtils::to_expr_set(&allocator_, child_exprs, fd_item->get_child_exprs()))) {
LOG_WARN("failed to expr set", K(ret));
}
return ret;
}
int ObFdItemFactory::create_expr_fd_item(ObExprFdItem*& fd_item, const ObExprFdItem& other)
{
int ret = OB_SUCCESS;
fd_item = NULL;
if (OB_FAIL(create_expr_fd_item(fd_item, other.is_unique(), other.get_parent_exprs(), other.get_stmt_level()))) {
LOG_WARN("failed to create table fd item", K(ret));
} else if (OB_FAIL(fd_item->get_child_exprs().assign(other.get_child_exprs()))) {
LOG_WARN("failed to assign child exprs", K(ret));
}
return ret;
}
int ObFdItemFactory::copy_fd_item(ObFdItem*& fd_item, const ObFdItem& other)
{
int ret = OB_SUCCESS;
if (other.is_table_fd_item()) {
ObTableFdItem* table_fd_item = NULL;
if (OB_FAIL(create_table_fd_item(table_fd_item, static_cast<const ObTableFdItem&>(other)))) {
LOG_WARN("failed to create table fd item", K(ret));
} else {
fd_item = table_fd_item;
}
} else if (other.is_expr_fd_item()) {
ObExprFdItem* expr_fd_item = NULL;
if (OB_FAIL(create_expr_fd_item(expr_fd_item, static_cast<const ObExprFdItem&>(other)))) {
LOG_WARN("failed to create expr fd item", K(ret));
} else {
fd_item = expr_fd_item;
}
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected fd item type", K(ret));
}
return ret;
}
int ObFdItemFactory::deduce_fd_item_set(const EqualSets& equal_sets, ObIArray<ObRawExpr*>& column_exprs,
ObIArray<ObRawExpr*>& const_exprs, ObFdItemSet& fd_item_set)
{
int ret = OB_SUCCESS;
bool is_happend = true;
ObSqlBitSet<> deduced_fd;
ObSEArray<ObFdItem*, 8> new_fd_items;
int64_t const_exprs_count = -1;
if (OB_FAIL(ObOptimizerUtil::remove_equal_exprs(equal_sets, const_exprs, column_exprs))) {
LOG_WARN("failed to remove equal exprs", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && const_exprs.count() > const_exprs_count && i < 10; i++) {
const_exprs_count = const_exprs.count();
if (OB_FAIL(do_deduce_fd_item_set(
equal_sets, column_exprs, const_exprs, fd_item_set, false, deduced_fd, new_fd_items))) {
LOG_WARN("failed to simplify fd item", K(ret));
}
}
if (OB_FAIL(ret)) {
/*do nothing*/
} else if (OB_FAIL(do_deduce_fd_item_set(
equal_sets, column_exprs, const_exprs, fd_item_set, true, deduced_fd, new_fd_items))) {
LOG_WARN("failed to simplify fd item", K(ret));
} else if (OB_FAIL(append(fd_item_set, new_fd_items))) {
LOG_WARN("failed to append fd items", K(ret));
}
return ret;
}
int ObFdItemFactory::do_deduce_fd_item_set(const EqualSets& equal_sets, ObIArray<ObRawExpr*>& column_exprs,
ObIArray<ObRawExpr*>& const_exprs, ObFdItemSet& fd_item_set, const bool final_deduce, ObSqlBitSet<>& deduced_fd,
ObIArray<ObFdItem*>& new_fd_items)
{
int ret = OB_SUCCESS;
for (int64_t i = 0; OB_SUCC(ret) && i < fd_item_set.count(); i++) {
ObFdItem* fd_item = NULL;
ObRawExprSet* parent_exprs = NULL;
if (OB_ISNULL(fd_item = fd_item_set.at(i)) || OB_ISNULL(parent_exprs = fd_item->get_parent_exprs())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (deduced_fd.has_member(i)) { // has deduced
/*do nothing*/
} else {
ObSEArray<ObRawExpr*, 8> const_parent_exprs;
ObSEArray<ObRawExpr*, 8> other_parent_exprs;
bool is_expr_const = false;
for (int64_t j = 0; OB_SUCC(ret) && j < parent_exprs->count(); j++) {
if (OB_FAIL(ObOptimizerUtil::is_const_expr(parent_exprs->at(j), equal_sets, const_exprs, is_expr_const))) {
LOG_WARN("failed to check is const expr", K(ret));
} else if (is_expr_const) {
ret = const_parent_exprs.push_back(parent_exprs->at(j));
} else {
ret = other_parent_exprs.push_back(parent_exprs->at(j));
}
}
if (const_parent_exprs.empty() && other_parent_exprs.empty()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected fd", K(ret), K(*fd_item));
} else if (other_parent_exprs.empty()) {
bool is_in_child = false;
ObSEArray<ObRawExpr*, 8> other_column_exprs;
for (int64_t j = 0; OB_SUCC(ret) && j < column_exprs.count(); j++) {
if (OB_FAIL(fd_item->check_expr_in_child(column_exprs.at(j), equal_sets, is_in_child))) {
LOG_WARN("failed to check expr in child", K(ret));
} else if (is_in_child) {
ret = const_exprs.push_back(column_exprs.at(j));
} else {
ret = other_column_exprs.push_back(column_exprs.at(j));
}
}
if (OB_SUCC(ret) && OB_FAIL(column_exprs.assign(other_column_exprs))) {
LOG_WARN("failed to assign exprs", K(ret));
}
ObFdItem* new_fd_item = NULL;
ObRawExprSet* parent_exprs_ptr = NULL;
if (OB_FAIL(ret) || 1 == const_parent_exprs.count()) {
/*do nothing*/
} else if (OB_FAIL(get_parent_exprs_ptr(const_parent_exprs.at(0), parent_exprs_ptr))) {
LOG_WARN("failed to get parent exprs ptr", K(ret));
} else if (OB_FAIL(fd_item->get_parent_exprs()->assign(*parent_exprs_ptr))) {
LOG_WARN("failed to assign expr set", K(ret));
} else {
for (int64_t j = 1; OB_SUCC(ret) && j < const_parent_exprs.count(); j++) {
if (OB_FAIL(get_parent_exprs_ptr(const_parent_exprs.at(j), parent_exprs_ptr))) {
LOG_WARN("failed to get parent exprs ptr", K(ret));
} else if (OB_FAIL(copy_fd_item(new_fd_item, *fd_item))) {
LOG_WARN("failed to copy fd item", K(ret));
} else if (OB_FAIL(new_fd_item->get_parent_exprs()->assign(*parent_exprs_ptr))) {
LOG_WARN("failed to assign expr set", K(ret));
} else if (new_fd_items.push_back(new_fd_item)) {
LOG_WARN("failed to push back fd item", K(ret));
}
}
}
if (OB_SUCC(ret) && OB_FAIL(deduced_fd.add_member(i))) {
LOG_WARN("failed to assign exprs", K(ret));
}
} else if (final_deduce && !const_parent_exprs.empty()) {
ObRawExprSet* parent_exprs_ptr = NULL;
if (OB_FAIL(get_parent_exprs_ptr(other_parent_exprs, parent_exprs_ptr))) {
LOG_WARN("failed to get parent exprs ptr", K(ret));
} else if (OB_FAIL(fd_item->get_parent_exprs()->assign(*parent_exprs_ptr))) {
LOG_WARN("failed to assign expr set", K(ret));
}
} else { /*do nothing*/
}
}
}
return ret;
}