Co-authored-by: zzg19950727 <1071026277@qq.com> Co-authored-by: xianyu-w <707512433@qq.com>
170 lines
6.5 KiB
C++
170 lines
6.5 KiB
C++
/**
|
|
* 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 (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::re_est_cost(EstimateCostInfo ¶m, double &card, double &cost)
|
|
{
|
|
int ret = OB_SUCCESS;
|
|
ObLogicalOperator *child = NULL;
|
|
int parallel = 1.0;
|
|
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_UNLIKELY((parallel = get_parallel()) < 1)) {
|
|
ret = OB_ERR_UNEXPECTED;
|
|
LOG_WARN("get unexpected parallel degree", K(parallel), K(ret));
|
|
} 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();
|
|
double op_cost = ObOptEstCost::cost_filter_rows(child_card / parallel,
|
|
get_filter_exprs(),
|
|
opt_ctx.get_cost_model_type());
|
|
cost = child_cost + op_cost;
|
|
card = child_card * selectivity;
|
|
if (param.override_) {
|
|
set_op_cost(op_cost);
|
|
set_cost(cost);
|
|
set_card(card);
|
|
}
|
|
}
|
|
}
|
|
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;
|
|
}
|