forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_log_subplan_scan.cpp
More file actions
168 lines (159 loc) · 6.56 KB
/
Copy pathob_log_subplan_scan.cpp
File metadata and controls
168 lines (159 loc) · 6.56 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
/**
* 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/optimizer/ob_log_subplan_scan.h"
#include "sql/optimizer/ob_log_plan.h"
#include "sql/optimizer/ob_opt_est_cost.h"
#include "sql/optimizer/ob_join_order.h"
#include "common/ob_smart_call.h"
using namespace oceanbase::sql;
using namespace oceanbase::common;
int ObLogSubPlanScan::generate_access_exprs()
{
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("get unexpected null", K(get_plan()), K(stmt), K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < stmt->get_column_size(); i++) {
const ColumnItem *col_item = stmt->get_column_item(i);
if (OB_ISNULL(col_item) || OB_ISNULL(col_item->expr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(col_item), K(ret));
} else if (col_item->table_id_ == subquery_id_ &&
col_item->expr_->is_explicited_reference() &&
!col_item->expr_->is_unpivot_mocked_column() &&
OB_FAIL(access_exprs_.push_back(col_item->expr_))) {
LOG_WARN("failed to push back expr", K(ret));
} else { /*do nothing*/ }
}
}
return ret;
}
int ObLogSubPlanScan::get_op_exprs(ObIArray<ObRawExpr*> &all_exprs)
{
int ret = OB_SUCCESS;
if (OB_FAIL(generate_access_exprs())) {
LOG_WARN("failed to generate access exprs", K(ret));
} else if (OB_FAIL(append(all_exprs, access_exprs_))) {
LOG_WARN("failed to append exprs", K(ret));
} else if (OB_FAIL(ObLogicalOperator::get_op_exprs(all_exprs))) {
LOG_WARN("failed to append op exprs", K(ret));
} else { /*do nothing*/ }
return ret;
}
int ObLogSubPlanScan::allocate_expr_post(ObAllocExprContext &ctx)
{
int ret = OB_SUCCESS;
for (int64_t i = 0; OB_SUCC(ret) && i < access_exprs_.count(); i++) {
ObRawExpr *expr = NULL;
if (OB_ISNULL(expr = access_exprs_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(mark_expr_produced(expr, branch_id_, id_, ctx))) {
LOG_WARN("failed to mark expr as produced", K(ret));
} else if (!is_plan_root() && OB_FAIL(output_exprs_.push_back(expr))) {
LOG_WARN("failed to push back expr", K(ret));
} else { /*do nothing*/ }
}
// check if we can produce some more exprs, such as 1 + 'c1' after we have produced 'c1'
if(OB_SUCC(ret)) {
if (OB_FAIL(ObLogicalOperator::allocate_expr_post(ctx))) {
LOG_WARN("failed to allocate expr pre", K(ret));
} else { /*do nothing*/ }
}
return ret;
}
int ObLogSubPlanScan::get_plan_item_info(PlanText &plan_text,
ObSqlPlanItem &plan_item)
{
int ret = OB_SUCCESS;
// print access
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;
const ObIArray<ObRawExpr*> &access = get_access_exprs();
EXPLAIN_PRINT_EXPRS(access, type);
END_BUF_PRINT(plan_item.access_predicates_,
plan_item.access_predicates_len_);
}
if (OB_SUCC(ret)) {
const ObString &name = get_subquery_name();
BUF_PRINT_OB_STR(name.ptr(),
name.length(),
plan_item.object_alias_,
plan_item.object_alias_len_);
}
return ret;
}
int ObLogSubPlanScan::do_re_est_cost(EstimateCostInfo ¶m, double &card, double &op_cost, double &cost)
{
int ret = OB_SUCCESS;
ObLogicalOperator *child = NULL;
const int64_t parallel = param.need_parallel_;
double selectivity = 1.0;
if (OB_ISNULL(child = get_child(ObLogicalOperator::first_child)) ||
OB_ISNULL(get_plan())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(child), K(ret));
} else if (OB_FALSE_IT(get_plan()->get_selectivity_ctx().init_op_ctx(&child->get_output_equal_sets(), child->get_card()))) {
} else if (OB_FAIL(ObOptSelectivity::calculate_selectivity(get_plan()->get_basic_table_metas(),
get_plan()->get_selectivity_ctx(),
get_filter_exprs(),
selectivity,
get_plan()->get_predicate_selectivities()))) {
LOG_WARN("failed to calculate selectivity", K(ret));
} else {
double child_card = child->get_card();
double child_cost = child->get_cost();
if (param.need_row_count_ >= 0 && param.need_row_count_ < get_card() &&
selectivity > 0) {
param.need_row_count_ /= selectivity;
}
if (OB_FAIL(SMART_CALL(child->re_est_cost(param, child_card, child_cost)))) {
LOG_WARN("failed to re est exchange cost", K(ret));
} else {
ObOptimizerContext &opt_ctx = get_plan()->get_optimizer_context();
op_cost = ObOptEstCost::cost_filter_rows(child_card / parallel,
get_filter_exprs(),
opt_ctx);
cost = child_cost + op_cost;
card = child_card * selectivity;
}
}
return ret;
}
int ObLogSubPlanScan::check_output_dependance(ObIArray<ObRawExpr *> &child_output,
PPDeps &deps)
{
int ret = OB_SUCCESS;
ObSEArray<ObRawExpr*, 8> exprs;
LOG_TRACE("start to check output exprs", K(type_), K(child_output), K(deps));
ObRawExprCheckDep dep_checker(child_output, deps, false);
if (OB_FAIL(append(exprs, filter_exprs_))) {
LOG_WARN("failed to append exprs", K(ret));
} else if (OB_FAIL(append_array_no_dup(exprs, output_exprs_))) {
LOG_WARN("failed to append array no dup", K(ret));
} else if (OB_FAIL(dep_checker.check(exprs))) {
LOG_WARN("failed to check op_exprs", K(ret));
} else {
LOG_TRACE("succeed to check output exprs", K(exprs), K(type_), K(deps));
}
return ret;
}
int ObLogSubPlanScan::is_my_fixed_expr(const ObRawExpr *expr, bool &is_fixed)
{
is_fixed = ObOptimizerUtil::find_item(access_exprs_, expr);
return OB_SUCCESS;
}