From 5eef4de86dd88203f69c404daff59908d060a58f Mon Sep 17 00:00:00 2001 From: chimyue Date: Wed, 10 May 2023 05:19:22 +0000 Subject: [PATCH] fix some optimizer bugs --- src/sql/engine/px/ob_px_util.cpp | 2 +- src/sql/monitor/ob_sql_plan.cpp | 6 +++--- src/sql/ob_dml_stmt_printer.cpp | 7 ++++--- src/sql/optimizer/ob_join_order.cpp | 9 +++++---- src/sql/optimizer/ob_join_order.h | 14 +++++++++++++- src/sql/optimizer/ob_optimizer.cpp | 2 +- src/sql/resolver/dml/ob_hint.cpp | 11 +++++++---- src/sql/resolver/dml/ob_hint.h | 2 +- src/sql/resolver/dml/ob_sql_hint.cpp | 11 ++++++----- src/sql/resolver/dml/ob_sql_hint.h | 5 ++--- 10 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/sql/engine/px/ob_px_util.cpp b/src/sql/engine/px/ob_px_util.cpp index 47616bf8b4..7821384523 100644 --- a/src/sql/engine/px/ob_px_util.cpp +++ b/src/sql/engine/px/ob_px_util.cpp @@ -718,7 +718,7 @@ int ObPXServerAddrUtil::alloc_by_random_distribution(ObExecContext &exec_ctx, } else if (OB_FAIL(get_location_addrs(locations, addrs))) { LOG_WARN("fail get location addrs", K(ret)); } else { - int64_t parallel = child.get_assigned_worker_count(); + int64_t parallel = parent.get_assigned_worker_count(); if (0 >= parallel) { parallel = 1; } diff --git a/src/sql/monitor/ob_sql_plan.cpp b/src/sql/monitor/ob_sql_plan.cpp index 62af0bd32a..01c90adc48 100644 --- a/src/sql/monitor/ob_sql_plan.cpp +++ b/src/sql/monitor/ob_sql_plan.cpp @@ -203,7 +203,7 @@ int ObSqlPlan::get_plan_outline_info_one_line(PlanText &plan_text, LOG_WARN("failed to get plan tree outline", K(ret)); } else if (OB_FAIL(query_hint.print_transform_hints(plan_text))) { LOG_WARN("failed to print all transform hints", K(ret)); - } else if (OB_FAIL(query_hint.get_global_hint().print_global_hint(plan_text, /*ignore_parallel*/false))) { + } else if (OB_FAIL(query_hint.get_global_hint().print_global_hint(plan_text))) { LOG_WARN("failed to print global hint", K(ret)); } else { BUF_PRINT_CONST_STR(" END_OUTLINE_DATA*/", plan_text); @@ -606,7 +606,7 @@ int ObSqlPlan::get_plan_used_hint_info(PlanText &plan_text, LOG_WARN("failed to print qb name hints", K(ret)); } else if (OB_FAIL(query_hint.print_transform_hints(temp_text))) { LOG_WARN("failed to print all transform hints", K(ret)); - } else if (OB_FAIL(query_hint.get_global_hint().print_global_hint(temp_text, false))) { + } else if (OB_FAIL(query_hint.get_global_hint().print_global_hint(temp_text))) { LOG_WARN("failed to print global hint", K(ret)); } else { BUF_PRINT_CONST_STR(NEW_LINE, temp_text); @@ -702,7 +702,7 @@ int ObSqlPlan::get_plan_outline_info(PlanText &plan_text, LOG_WARN("failed to get plan tree outline", K(ret)); } else if (OB_FAIL(query_hint.print_transform_hints(temp_text))) { LOG_WARN("failed to print all transform hints", K(ret)); - } else if (OB_FAIL(query_hint.get_global_hint().print_global_hint(temp_text, false))) { + } else if (OB_FAIL(query_hint.get_global_hint().print_global_hint(temp_text))) { LOG_WARN("failed to print global hint", K(ret)); } else { BUF_PRINT_CONST_STR(NEW_LINE, temp_text); diff --git a/src/sql/ob_dml_stmt_printer.cpp b/src/sql/ob_dml_stmt_printer.cpp index d668d7b813..390c403441 100644 --- a/src/sql/ob_dml_stmt_printer.cpp +++ b/src/sql/ob_dml_stmt_printer.cpp @@ -67,14 +67,15 @@ int ObDMLStmtPrinter::print_hint() DATA_PRINTF("%s", hint_begin); if (OB_SUCC(ret)) { const ObQueryHint &query_hint = stmt_->get_query_ctx()->get_query_hint(); - // just for print hint, ExplainType set as invalid type PlanText plan_text; plan_text.buf_ = buf_; plan_text.buf_len_ = buf_len_; plan_text.pos_ = *pos_; plan_text.is_oneline_ = true; - bool ignore_parallel = print_params_.for_dblink_; - if (OB_FAIL(query_hint.print_stmt_hint(plan_text, *stmt_, is_root_, ignore_parallel))) { + plan_text.type_ = print_params_.for_dblink_ + ? EXPLAIN_DBLINK_STMT + : EXPLAIN_UNINITIALIZED; // just for print hint, ExplainType set as invalid type + if (OB_FAIL(query_hint.print_stmt_hint(plan_text, *stmt_, is_root_))) { LOG_WARN("failed to print stmt hint", K(ret)); } else if (plan_text.pos_ == *pos_) { // no hint, roolback buffer! diff --git a/src/sql/optimizer/ob_join_order.cpp b/src/sql/optimizer/ob_join_order.cpp index 3eb2bc6103..80a84c72f6 100644 --- a/src/sql/optimizer/ob_join_order.cpp +++ b/src/sql/optimizer/ob_join_order.cpp @@ -5496,14 +5496,15 @@ int JoinPath::compute_join_path_parallel_and_server_info(const common::ObAddr &l ? left_path : right_path; parallel = inherit_child->parallel_; server_cnt = inherit_child->server_cnt_; - const ObShardingInfo *inherit_sharding = NULL; - if (OB_ISNULL(inherit_sharding = inherit_child->get_sharding())) { + const ObShardingInfo *sharding = NULL; + int64_t part_cnt = 0; + if (OB_ISNULL(sharding = inherit_child->try_get_sharding_with_table_location()) + || OB_UNLIKELY((part_cnt = sharding->get_part_cnt()) <= 0)) { ret = OB_ERR_UNEXPECTED; - LOG_WARN("get unexpected partition wise", K(ret), K(parallel), KPC(inherit_sharding)); + LOG_WARN("get unexpected partition wise", K(ret), K(parallel), KPC(sharding), K(part_cnt)); } else if (OB_FAIL(server_list.assign(inherit_child->server_list_))) { LOG_WARN("failed to assign server list", K(ret)); } else { - int64_t part_cnt = inherit_sharding->get_part_cnt(); parallel = parallel > part_cnt ? part_cnt : parallel; } } else if (DistAlgo::DIST_EXT_PARTITION_WISE == join_dist_algo) { diff --git a/src/sql/optimizer/ob_join_order.h b/src/sql/optimizer/ob_join_order.h index 238ef2f9fc..7908f857b4 100644 --- a/src/sql/optimizer/ob_join_order.h +++ b/src/sql/optimizer/ob_join_order.h @@ -417,6 +417,17 @@ struct EstimateCostInfo { } return ret_sharding; } + inline ObShardingInfo* try_get_sharding_with_table_location() const + { + ObShardingInfo *ret_sharding = NULL; + if (NULL != strong_sharding_ && NULL != strong_sharding_->get_phy_table_location_info()) { + ret_sharding = strong_sharding_; + } else if (!weak_sharding_.empty() && NULL != weak_sharding_.at(0) + && NULL != weak_sharding_.at(0)->get_phy_table_location_info()) { + ret_sharding = weak_sharding_.at(0); + } + return ret_sharding; + } inline bool is_local() const { return (NULL != strong_sharding_ && strong_sharding_->is_local()); @@ -472,7 +483,8 @@ struct EstimateCostInfo { } inline bool parallel_more_than_part_cnt() const { - return NULL != get_sharding() && parallel_ > get_sharding()->get_part_cnt(); + const ObShardingInfo *sharding = try_get_sharding_with_table_location(); + return NULL != sharding && parallel_ > sharding->get_part_cnt(); } int compute_path_property_from_log_op(); int set_parallel_and_server_info_for_match_all(); diff --git a/src/sql/optimizer/ob_optimizer.cpp b/src/sql/optimizer/ob_optimizer.cpp index 0134142aca..9bcf4cf736 100644 --- a/src/sql/optimizer/ob_optimizer.cpp +++ b/src/sql/optimizer/ob_optimizer.cpp @@ -606,7 +606,7 @@ int ObOptimizer::init_parallel_policy(ObDMLStmt &stmt, const ObSQLSessionInfo &s } else if (ObGlobalHint::UNSET_PARALLEL != session_force_parallel_dop) { ctx_.set_parallel(session_force_parallel_dop); ctx_.set_parallel_rule(PXParallelRule::SESSION_FORCE_PARALLEL); - } else if (session_enable_manual_dop) { + } else if (session_enable_manual_dop || ctx_.get_global_hint().enable_manual_dop()) { ctx_.set_parallel_rule(PXParallelRule::MANUAL_TABLE_DOP); } else { ctx_.set_parallel_rule(PXParallelRule::USE_PX_DEFAULT); diff --git a/src/sql/resolver/dml/ob_hint.cpp b/src/sql/resolver/dml/ob_hint.cpp index fd0323d6f0..1b484ad09c 100644 --- a/src/sql/resolver/dml/ob_hint.cpp +++ b/src/sql/resolver/dml/ob_hint.cpp @@ -378,13 +378,14 @@ int ObGlobalHint::assign(const ObGlobalHint &other) // hints below not print // MAX_CONCURRENT // ObDDLSchemaVersionHint -int ObGlobalHint::print_global_hint(PlanText &plan_text, const bool ignore_parallel) const +int ObGlobalHint::print_global_hint(PlanText &plan_text) const { int ret = OB_SUCCESS; char *buf = plan_text.buf_; int64_t &buf_len = plan_text.buf_len_; int64_t &pos = plan_text.pos_; const char* outline_indent = ObQueryHint::get_outline_indent(plan_text.is_oneline_); + const bool ignore_parallel_for_dblink = EXPLAIN_DBLINK_STMT == plan_text.type_; #define PRINT_GLOBAL_HINT_STR(hint_str) \ if (OB_FAIL(BUF_PRINTF("%s%s", outline_indent, hint_str))) { \ @@ -410,7 +411,7 @@ int ObGlobalHint::print_global_hint(PlanText &plan_text, const bool ignore_paral } //DOP - if (OB_SUCC(ret) && !dops_.empty() && !ignore_parallel) { + if (OB_SUCC(ret) && !dops_.empty() && !ignore_parallel_for_dblink) { for (int64_t i = 0; OB_SUCC(ret) && i < dops_.count(); ++i) { if (OB_FAIL(BUF_PRINTF("%sDOP(%lu, %lu)", outline_indent, dops_.at(i).dfo_, dops_.at(i).dop_))) { LOG_WARN("failed to print dop hint", K(ret)); @@ -466,9 +467,11 @@ int ObGlobalHint::print_global_hint(PlanText &plan_text, const bool ignore_paral LOG_WARN("failed to print log level hint", K(ret)); } } - if (OB_SUCC(ret) && has_parallel_hint() && !ignore_parallel) { //PARALLEL + if (OB_SUCC(ret) && has_parallel_hint() && !ignore_parallel_for_dblink) { //PARALLEL if (has_parallel_degree()) { PRINT_GLOBAL_HINT_NUM("PARALLEL", parallel_); + } else if (plan_text.is_outline_data_) { + /* do not print parallel policy for outline data */ } else if (enable_auto_dop()) { PRINT_GLOBAL_HINT_STR("PARALLEL( AUTO )"); } else if (enable_manual_dop()) { @@ -480,7 +483,7 @@ int ObGlobalHint::print_global_hint(PlanText &plan_text, const bool ignore_paral } if (OB_SUCC(ret) && ObPDMLOption::NOT_SPECIFIED != pdml_option_) { //PDML if (ObPDMLOption::ENABLE == pdml_option_) { - if (!ignore_parallel) { + if (!ignore_parallel_for_dblink) { PRINT_GLOBAL_HINT_STR("ENABLE_PARALLEL_DML"); } } else if (ObPDMLOption::DISABLE == pdml_option_) { diff --git a/src/sql/resolver/dml/ob_hint.h b/src/sql/resolver/dml/ob_hint.h index f5f9bc5a76..23b43da4d9 100644 --- a/src/sql/resolver/dml/ob_hint.h +++ b/src/sql/resolver/dml/ob_hint.h @@ -154,7 +154,7 @@ struct ObGlobalHint { void merge_dynamic_sampling_hint(int64_t dynamic_sampling); bool has_hint_exclude_concurrent() const; - int print_global_hint(PlanText &plan_text, const bool ignore_parallel) const; + int print_global_hint(PlanText &plan_text) const; int print_monitoring_hints(PlanText &plan_text) const; ObPDMLOption get_pdml_option() const { return pdml_option_; } diff --git a/src/sql/resolver/dml/ob_sql_hint.cpp b/src/sql/resolver/dml/ob_sql_hint.cpp index cedd33b377..8b6003eefd 100644 --- a/src/sql/resolver/dml/ob_sql_hint.cpp +++ b/src/sql/resolver/dml/ob_sql_hint.cpp @@ -657,7 +657,7 @@ int ObQueryHint::print_qb_name_hints(PlanText &plan_text) const // If outline_stmt_id_ is invalid stmt id and has_outline_data(), do not print hint. // This may happened for outline data from SPM. int ObQueryHint::print_stmt_hint(PlanText &plan_text, const ObDMLStmt &stmt, - const bool is_root_stmt, const bool ignore_parallel) const + const bool is_root_stmt) const { int ret = OB_SUCCESS; const int64_t stmt_id = stmt.get_stmt_id(); @@ -673,9 +673,9 @@ int ObQueryHint::print_stmt_hint(PlanText &plan_text, const ObDMLStmt &stmt, // If stmt is the first stmt can add hint, print global hint and hint with qb name. const bool is_first_stmt = is_root_stmt; if ((is_first_stmt || OB_INVALID_ID != stmt.get_dblink_id()) && - OB_FAIL(get_global_hint().print_global_hint(plan_text, ignore_parallel))) { + OB_FAIL(get_global_hint().print_global_hint(plan_text))) { LOG_WARN("failed to print global hint", K(ret)); - } else if (OB_FAIL(stmt.get_stmt_hint().print_stmt_hint(plan_text, ignore_parallel))) { + } else if (OB_FAIL(stmt.get_stmt_hint().print_stmt_hint(plan_text))) { LOG_WARN("failed to print stmt hint", K(ret)); } else if (is_first_stmt) { int tmp = OB_SUCCESS; @@ -725,7 +725,7 @@ int ObQueryHint::print_outline_data(PlanText &plan_text) const } } if (OB_FAIL(ret)) { - } else if (OB_FAIL(get_global_hint().print_global_hint(plan_text, /*ignore_parallel*/false))) { + } else if (OB_FAIL(get_global_hint().print_global_hint(plan_text))) { LOG_WARN("failed to print global hint", K(ret)); } else if (OB_FAIL(BUF_PRINTF("%sEND_OUTLINE_DATA", ObQueryHint::get_outline_indent(is_oneline)))) { LOG_WARN("fail to print buf", K(ret)); @@ -1064,11 +1064,12 @@ DEF_TO_STRING(ObStmtHint) return pos; } -int ObStmtHint::print_stmt_hint(PlanText &plan_text, const bool ignore_parallel) const +int ObStmtHint::print_stmt_hint(PlanText &plan_text) const { int ret = OB_SUCCESS; const ObHint *hint = NULL; const int64_t N = get_hint_count(); + const bool ignore_parallel = EXPLAIN_DBLINK_STMT == plan_text.type_; for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) { if (OB_ISNULL(hint = get_hint_by_idx(i))) { ret = OB_ERR_UNEXPECTED; diff --git a/src/sql/resolver/dml/ob_sql_hint.h b/src/sql/resolver/dml/ob_sql_hint.h index 0cabd1c5ee..94cb67fc76 100644 --- a/src/sql/resolver/dml/ob_sql_hint.h +++ b/src/sql/resolver/dml/ob_sql_hint.h @@ -137,8 +137,7 @@ struct ObQueryHint { || global_hint_.has_hint_exclude_concurrent(); } // print hint - int print_stmt_hint(PlanText &plan_text, const ObDMLStmt &stmt, - const bool is_root_stmt, const bool ignore_parallel) const; + int print_stmt_hint(PlanText &plan_text, const ObDMLStmt &stmt, const bool is_root_stmt) const; int print_outline_data(PlanText &plan_text) const; int print_qb_name_hints(PlanText &plan_text) const; int print_qb_name_hint(PlanText &plan_text, int64_t stmt_id) const; @@ -206,7 +205,7 @@ struct ObStmtHint int init_stmt_hint(const ObDMLStmt &stmt, const ObQueryHint &query_hint, bool use_stmt_id_hints); - int print_stmt_hint(PlanText &plan_text, const bool ignore_parallel) const; + int print_stmt_hint(PlanText &plan_text) const; const ObHint *get_normal_hint(ObItemType hint_type, int64_t *idx = NULL) const; ObHint *get_normal_hint(ObItemType hint_type, int64_t *idx = NULL); void set_query_hint(const ObQueryHint *query_hint) { query_hint_ = query_hint; }