From 62a736875f5eeea9a03574c389bb5fe3fd7db393 Mon Sep 17 00:00:00 2001 From: yb0 Date: Tue, 21 Dec 2021 15:09:54 +0800 Subject: [PATCH] refine plan generate strategy for remote sql --- src/sql/optimizer/ob_join_order.cpp | 39 ++--------------------------- src/sql/optimizer/ob_optimizer.cpp | 21 ++++++++++++++-- 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/src/sql/optimizer/ob_join_order.cpp b/src/sql/optimizer/ob_join_order.cpp index 2355e1b371..6a306b0c73 100644 --- a/src/sql/optimizer/ob_join_order.cpp +++ b/src/sql/optimizer/ob_join_order.cpp @@ -2288,21 +2288,15 @@ int ObJoinOrder::add_cte_table(const uint64_t table_id, const uint64_t ref_id) int ObJoinOrder::compute_cost_and_prune_access_path(PathHelper& helper, ObIArray& access_paths) { int ret = OB_SUCCESS; - ObSqlCtx* sql_ctx = NULL; - ObTaskExecutorCtx* task_exec_ctx = NULL; - if (OB_ISNULL(get_plan()) || OB_ISNULL(get_plan()->get_optimizer_context().get_exec_ctx()) || - OB_ISNULL(task_exec_ctx = get_plan()->get_optimizer_context().get_task_exec_ctx()) || - OB_ISNULL(sql_ctx = get_plan()->get_optimizer_context().get_exec_ctx()->get_sql_ctx())) { + if (OB_ISNULL(get_plan())) { ret = OB_ERR_UNEXPECTED; - LOG_WARN("get unexpected null", K(get_plan()), K(sql_ctx), K(ret)); + LOG_WARN("get unexpected null", K(get_plan()), K(ret)); } else { for (int64_t i = 0; OB_SUCC(ret) && i < access_paths.count(); ++i) { AccessPath* ap = access_paths.at(i); if (OB_ISNULL(ap) || OB_ISNULL(ap->get_sharding_info())) { ret = OB_ERR_UNEXPECTED; LOG_WARN("get unexpected null", K(ap), K(ret)); - } else if (sql_ctx->is_remote_sql_ && ap->get_sharding_info()->is_remote_or_distribute()) { - /*do nothing*/ } else if (OB_FAIL(ap->estimate_cost())) { LOG_WARN("failed to get index access info", K(*ap), K(ret)); } else if (!ap->is_inner_path()) { @@ -2322,35 +2316,6 @@ int ObJoinOrder::compute_cost_and_prune_access_path(PathHelper& helper, ObIArray } } } // add path end - if (OB_SUCC(ret) && sql_ctx->is_remote_sql_ && interesting_paths_.empty() && !helper.is_inner_path_) { - // for the purpose to refresh location cache - ObSEArray table_partitions; - for (int64_t i = 0; OB_SUCC(ret) && i < access_paths.count(); i++) { - AccessPath* path = NULL; - if (OB_ISNULL(path = access_paths.at(i)) || OB_ISNULL(path->table_partition_info_)) { - ret = OB_ERR_UNEXPECTED; - LOG_WARN("get unexpected null", K(ret), K(path)); - } else if (1 != path->table_partition_info_->get_phy_tbl_location_info().get_phy_part_loc_info_list().count()) { - // do nothing - } else if (is_contain(table_partitions, path->table_partition_info_)) { - /*do nothing*/ - } else if (OB_FAIL(path->table_partition_info_->set_log_op_infos(path->index_id_, path->order_direction_))) { - LOG_WARN("failed to set log op infos", K(ret)); - } else if (OB_FAIL(table_partitions.push_back(path->table_partition_info_))) { - LOG_WARN("failed to push back table partition", K(ret)); - } else { /*do nothing*/ - } - } - if (OB_FAIL(ret)) { - /*do nothing*/ - } else if (OB_FAIL(task_exec_ctx->append_table_locations_no_dup(table_partitions))) { - LOG_WARN("failed to append table locations no dup", K(ret)); - } else { - ret = (get_plan()->get_optimizer_context().is_cost_evaluation() ? OB_SQL_OPT_GEN_PLAN_FALIED - : OB_LOCATION_NOT_EXIST); - LOG_WARN("no available local path for remote sql", K(ret)); - } - } } return ret; } diff --git a/src/sql/optimizer/ob_optimizer.cpp b/src/sql/optimizer/ob_optimizer.cpp index 864729fc2f..caab8fbb5a 100644 --- a/src/sql/optimizer/ob_optimizer.cpp +++ b/src/sql/optimizer/ob_optimizer.cpp @@ -29,9 +29,10 @@ int ObOptimizer::optimize(ObDMLStmt& stmt, ObLogPlan*& logical_plan) const ObSQLSessionInfo* session = ctx_.get_session_info(); int64_t last_mem_usage = ctx_.get_allocator().total(); int64_t optimizer_mem_usage = 0; - if (OB_ISNULL(query_ctx) || OB_ISNULL(session)) { + ObTaskExecutorCtx* task_exec_ctx = ctx_.get_task_exec_ctx(); + if (OB_ISNULL(query_ctx) || OB_ISNULL(session) || OB_ISNULL(task_exec_ctx)) { ret = OB_INVALID_ARGUMENT; - LOG_WARN("invalid arguments", K(ret), K(query_ctx), K(session)); + LOG_WARN("invalid arguments", K(ret), K(query_ctx), K(session), K(task_exec_ctx)); } else if (OB_FAIL(init_env_info(stmt))) { LOG_WARN("failed to init px info", K(ret)); } else if (OB_FAIL(generate_plan_for_temp_table(stmt))) { @@ -62,6 +63,22 @@ int ObOptimizer::optimize(ObDMLStmt& stmt, ObLogPlan*& logical_plan) LOG_USER_ERROR(OB_OP_NOT_ALLOW, "Access tables from multiple tenants"); } } + if (OB_SUCC(ret)) { + if (ctx_.get_exec_ctx()->get_sql_ctx()->is_remote_sql_ && plan->get_phy_plan_type() != OB_PHY_PLAN_LOCAL) { + // set table location to refresh location cache + ObSEArray table_partitions; + if (OB_FAIL(plan->get_global_table_partition_info(table_partitions))) { + LOG_WARN("failed to get global table partition info", K(ret)); + } else if (OB_FAIL(task_exec_ctx->set_table_locations(table_partitions))) { + LOG_WARN("failed to set table locations", K(ret)); + } + + if (OB_SUCC(ret)) { + ret = OB_LOCATION_NOT_EXIST; + LOG_WARN("best plan for remote sql is not local", K(ret), K(plan->get_phy_plan_type())); + } + } + } if (OB_SUCC(ret)) { logical_plan = plan; LOG_TRACE("succ to optimize statement", "stmt", stmt.get_sql_stmt(), K(logical_plan->get_optimization_cost())); -- GitLab