fix optimizer trace bug and fix format sql plan bug

This commit is contained in:
zzg19950727
2023-02-07 11:37:22 +08:00
committed by ob-robot
parent 4f31f2a715
commit 9bbe05b4b4
5 changed files with 92 additions and 29 deletions

View File

@ -140,6 +140,40 @@ int ObSqlPlan::store_sql_plan_for_explain(ObLogPlan* plan,
return ret; return ret;
} }
int ObSqlPlan::print_sql_plan(ObLogPlan* plan,
ExplainType type,
const ObExplainDisplayOpt& option,
ObIArray<common::ObString> &plan_strs)
{
int ret = OB_SUCCESS;
PlanText plan_text;
PlanText out_plan_text;
plan_text.type_ = type;
ObSEArray<ObSqlPlanItem*, 16> sql_plan_infos;
ObSEArray<ObPlanRealInfo, 2> dummy_plan_infos;
if (OB_FAIL(init_buffer(plan_text))) {
LOG_WARN("failed to init buffer", K(ret));
} else if (OB_FAIL(get_sql_plan_infos(plan_text,
plan,
sql_plan_infos))) {
LOG_WARN("failed to get sql plan infos", K(ret));
} else if (OB_FAIL(format_sql_plan(sql_plan_infos,
type,
option,
dummy_plan_infos,
out_plan_text))) {
LOG_WARN("failed to format sql plan", K(ret));
} else if (OB_FAIL(plan_text_to_strings(out_plan_text, plan_strs))) {
LOG_WARN("failed to convert plan text to strings", K(ret));
}
if (OB_FAIL(ret)) {
LOG_WARN("failed to store sql plan", K(ret));
ret = OB_SUCCESS;
}
destroy_buffer(plan_text);
return ret;
}
int ObSqlPlan::get_sql_plan(const ObString &sql_id, int ObSqlPlan::get_sql_plan(const ObString &sql_id,
int64_t plan_id, int64_t plan_id,
ExplainType type, ExplainType type,
@ -253,7 +287,9 @@ int ObSqlPlan::get_plan_outline_info_one_line(PlanText &plan_text,
plan_text.is_outline_data_ = true; plan_text.is_outline_data_ = true;
BUF_PRINT_CONST_STR("/*+BEGIN_OUTLINE_DATA", plan_text); BUF_PRINT_CONST_STR("/*+BEGIN_OUTLINE_DATA", plan_text);
const ObQueryHint &query_hint = query_ctx->get_query_hint(); const ObQueryHint &query_hint = query_ctx->get_query_hint();
if (OB_FAIL(get_plan_tree_outline(plan_text, plan->get_plan_root()))) { if (OB_FAIL(reset_plan_tree_outline_flag(plan->get_plan_root()))) {
LOG_WARN("failed to reset plan tree outline flag", K(ret));
} else if (OB_FAIL(get_plan_tree_outline(plan_text, plan->get_plan_root()))) {
LOG_WARN("failed to get plan tree outline", K(ret)); LOG_WARN("failed to get plan tree outline", K(ret));
} else if (OB_FAIL(query_hint.print_transform_hints(plan_text))) { } else if (OB_FAIL(query_hint.print_transform_hints(plan_text))) {
LOG_WARN("failed to print all transform hints", K(ret)); LOG_WARN("failed to print all transform hints", K(ret));
@ -528,7 +564,9 @@ int ObSqlPlan::get_plan_outline_info(PlanText &plan_text,
temp_text.pos_ = 0; temp_text.pos_ = 0;
BUF_PRINT_CONST_STR("/*+\n BEGIN_OUTLINE_DATA", temp_text); BUF_PRINT_CONST_STR("/*+\n BEGIN_OUTLINE_DATA", temp_text);
const ObQueryHint &query_hint = query_ctx->get_query_hint(); const ObQueryHint &query_hint = query_ctx->get_query_hint();
if (OB_FAIL(get_plan_tree_outline(temp_text, plan->get_plan_root()))) { if (OB_FAIL(reset_plan_tree_outline_flag(plan->get_plan_root()))) {
LOG_WARN("failed to reset plan tree outline flag", K(ret));
} else if (OB_FAIL(get_plan_tree_outline(temp_text, plan->get_plan_root()))) {
LOG_WARN("failed to get plan tree outline", K(ret)); LOG_WARN("failed to get plan tree outline", K(ret));
} else if (OB_FAIL(query_hint.print_transform_hints(temp_text))) { } else if (OB_FAIL(query_hint.print_transform_hints(temp_text))) {
LOG_WARN("failed to print all transform hints", K(ret)); LOG_WARN("failed to print all transform hints", K(ret));
@ -544,6 +582,23 @@ int ObSqlPlan::get_plan_outline_info(PlanText &plan_text,
return ret; return ret;
} }
int ObSqlPlan::reset_plan_tree_outline_flag(ObLogicalOperator* op)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(op) || OB_ISNULL(op->get_plan())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null op", K(ret));
} else {
op->get_plan()->reset_outline_print_flags();
}
for (int i = 0; OB_SUCC(ret) && i < op->get_num_of_child(); ++i) {
if (OB_FAIL(SMART_CALL(reset_plan_tree_outline_flag(op->get_child(i))))) {
LOG_WARN("failed to reset child plan tree outline flag", K(ret));
}
}
return ret;
}
int ObSqlPlan::get_plan_tree_outline(PlanText &plan_text, int ObSqlPlan::get_plan_tree_outline(PlanText &plan_text,
ObLogicalOperator* op) ObLogicalOperator* op)
{ {

View File

@ -126,6 +126,11 @@ public:
const ObExplainDisplayOpt& option, const ObExplainDisplayOpt& option,
ObIArray<common::ObString> &plan_strs); ObIArray<common::ObString> &plan_strs);
int print_sql_plan(ObLogPlan* plan,
ExplainType type,
const ObExplainDisplayOpt& option,
ObIArray<common::ObString> &plan_strs);
int get_sql_plan(const ObString &sql_id, int get_sql_plan(const ObString &sql_id,
int64_t plan_id, int64_t plan_id,
ExplainType type, ExplainType type,
@ -190,6 +195,8 @@ private:
ObLogPlan* plan, ObLogPlan* plan,
ObSqlPlanItem* sql_plan_item); ObSqlPlanItem* sql_plan_item);
static int reset_plan_tree_outline_flag(ObLogicalOperator* op);
static int get_plan_tree_outline(PlanText &plan_text, static int get_plan_tree_outline(PlanText &plan_text,
ObLogicalOperator* op); ObLogicalOperator* op);

View File

@ -408,15 +408,20 @@ int ObOptimizerTraceImpl::append(const ObLogPlan *log_plan)
target_plan = op->get_explain_plan(); target_plan = op->get_explain_plan();
} }
if (OB_NOT_NULL(target_plan)) { if (OB_NOT_NULL(target_plan)) {
if (OB_ISNULL(buf = static_cast<char*>(allocator_.alloc(buf_len)))) { ObSqlPlan sql_plan(target_plan->get_allocator());
ret = OB_ALLOCATE_MEMORY_FAILED; sql_plan.set_session_info(target_plan->get_optimizer_context().get_session_info());
LOG_ERROR("Failed to allocate buffer", "buffer size", buf_len, K(ret)); ObSEArray<common::ObString, 64> plan_strs;
} else { if (OB_FAIL(sql_plan.store_sql_plan_for_explain(const_cast<ObLogPlan*>(target_plan),
EXPLAIN_EXTENDED,
option,
plan_strs))) {
LOG_WARN("failed to store sql plan", K(ret));
}
OPT_TRACE_TITLE("Query Plan"); OPT_TRACE_TITLE("Query Plan");
buf_len = target_plan->to_string(buf, buf_len, EXPLAIN_EXTENDED, option); for (int64_t i = 0; OB_SUCC(ret) && i < plan_strs.count(); ++i) {
if (OB_FAIL(new_line())) { if (OB_FAIL(new_line())) {
LOG_WARN("failed to append msg", K(ret)); LOG_WARN("failed to append msg", K(ret));
} else if (OB_FAIL(append(ObString(buf_len, buf)))) { } else if (OB_FAIL(append(plan_strs.at(i)))) {
LOG_WARN("failed to append plan", K(ret)); LOG_WARN("failed to append plan", K(ret));
} }
} }

View File

@ -9863,25 +9863,11 @@ int ObJoinOrder::get_valid_path_info(const ObJoinOrder &left_tree,
} }
OPT_TRACE("candi distribute methods:"); OPT_TRACE("candi distribute methods:");
int64_t distributed_methods = path_info.distributed_methods_; int64_t distributed_methods = path_info.distributed_methods_;
const ObString dist_algo_str[] = for (int64_t k = 1; k < DistAlgo::DIST_MAX_JOIN_METHOD; k = k << 1) {
{ if (distributed_methods & k) {
"BASIC", DistAlgo dist_algo = get_dist_algo(k);
"PULL TO LOCAL", OPT_TRACE(ob_dist_algo_str(dist_algo));
"HASH HASH",
"BROADCAST NONE",
"NONE BROADCAST",
"BC2HOST NONE",
"PARTITION NONE",
"NONE PARTITION",
"PARTITION WISE",
"UNKNOWN ALGO",
"RANDOM"
};
for (int idx = 0; idx < sizeof(dist_algo_str) / sizeof(ObString); ++idx) {
if (distributed_methods & 1) {
OPT_TRACE(dist_algo_str[idx]);
} }
distributed_methods >>= 1;
} }
} }
} }

View File

@ -102,6 +102,10 @@ enum SetAlgo
HASH_SET HASH_SET
}; };
/********
* When modifying DistAlgo, the function ob_dist_algo_str
* needs to be modified synchronously !!!
************/
enum DistAlgo enum DistAlgo
{ {
DIST_INVALID_METHOD = 0, DIST_INVALID_METHOD = 0,
@ -132,15 +136,21 @@ inline const ObString &ob_dist_algo_str(DistAlgo algo)
"UNKNOWN ALGO", "UNKNOWN ALGO",
"BASIC", "BASIC",
"PULL TO LOCAL", "PULL TO LOCAL",
"HASH NONE",
"NONE HASH",
"HASH HASH", "HASH HASH",
"BROADCAST NONE", "BROADCAST NONE",
"NONE BROADCAST", "NONE BROADCAST",
"BC2HOST NONE", "BC2HOST NONE",
"PARTITION NONE", "PARTITION NONE",
"NONE PARTITION", "NONE PARTITION",
"NONE ALL",
"ALL NONE",
"PARTITION WISE", "PARTITION WISE",
"EXTEND PARTITION WISE",
"UNKNOWN ALGO", "UNKNOWN ALGO",
"RANDOM" "SET RANDOM",
"SET PARTITION WISE"
}; };
int64_t idx = 0; int64_t idx = 0;
int64_t value = algo; int64_t value = algo;
@ -148,7 +158,7 @@ inline const ObString &ob_dist_algo_str(DistAlgo algo)
value >>= 1; value >>= 1;
++idx; ++idx;
} }
if (OB_LIKELY(idx >= 0) && OB_LIKELY(idx <= 11)) { if (OB_LIKELY(idx >= 0) && OB_LIKELY(idx <= sizeof(dist_algo_str) / sizeof(ObString))) {
return dist_algo_str[idx]; return dist_algo_str[idx];
} else { } else {
return dist_algo_str[0]; return dist_algo_str[0];