From 73ec60d0d0ae05df845d965be7cc04b50a8919eb Mon Sep 17 00:00:00 2001 From: Larry955 <1412857955@qq.com> Date: Mon, 5 Jun 2023 11:42:12 +0000 Subject: [PATCH] fix failed case --- src/share/stat/ob_opt_stat_sql_service.cpp | 5 +++-- src/sql/optimizer/ob_join_order.cpp | 8 ++++++-- src/sql/optimizer/ob_opt_selectivity.cpp | 11 ++++++++--- src/sql/optimizer/ob_optimizer_util.cpp | 18 ++++++++++++++++++ src/sql/optimizer/ob_optimizer_util.h | 1 + src/sql/optimizer/ob_select_log_plan.cpp | 8 ++++++-- src/sql/optimizer/ob_table_location.cpp | 9 +++++++-- src/sql/resolver/dml/ob_hint.cpp | 5 +++++ src/sql/resolver/dml/ob_hint.h | 1 + 9 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/share/stat/ob_opt_stat_sql_service.cpp b/src/share/stat/ob_opt_stat_sql_service.cpp index ddbaee91f0..a6a7511b25 100644 --- a/src/share/stat/ob_opt_stat_sql_service.cpp +++ b/src/share/stat/ob_opt_stat_sql_service.cpp @@ -118,7 +118,7 @@ "b_endpoint_value," \ "endpoint_repeat_cnt) VALUES " -#define DELETE_HISTOGRAM_STAT_SQL "DELETE FROM __all_histogram_stat WHERE " +#define DELETE_HISTOGRAM_STAT_SQL "DELETE /*+opt_param('enable_in_range_optimization','true')*/ FROM __all_histogram_stat WHERE " #define DELETE_COL_STAT_SQL "DELETE FROM __all_column_stat WHERE " #define DELETE_TAB_STAT_SQL "DELETE FROM __all_table_stat WHERE " #define UPDATE_HISTOGRAM_TYPE_SQL "UPDATE __all_column_stat SET histogram_type = 0, bucket_cnt = 0 WHERE" @@ -2054,7 +2054,8 @@ int ObOptStatSqlService::fetch_table_rowcnt(const uint64_t tenant_id, ObSchemaUtils::get_real_table_mappings_tid(table_id) : table_id; if (OB_FAIL(gen_tablet_list_str(all_tablet_ids, all_ls_ids, tablet_list_str, tablet_ls_list_str))) { LOG_WARN("failed to gen tablet list str", K(ret)); - } else if (OB_FAIL(raw_sql.append_fmt("select tablet_id, max(row_count) from (select cast(tablet_id as unsigned) as tablet_id, cast(inserts - deletes as signed) as row_count "\ + } else if (OB_FAIL(raw_sql.append_fmt("select /*+opt_param('enable_in_range_optimization','true')*/ tablet_id, max(row_count) from "\ + "(select cast(tablet_id as unsigned) as tablet_id, cast(inserts - deletes as signed) as row_count "\ "from %s where tenant_id = %lu and table_id = %lu and tablet_id in %s union all "\ "select cast(tablet_id as unsigned) as tablet_id, cast(row_count as signed) as row_count from %s, "\ "(select frozen_scn from %s order by frozen_scn desc limit 1) where "\ diff --git a/src/sql/optimizer/ob_join_order.cpp b/src/sql/optimizer/ob_join_order.cpp index 3a63d9a949..23f275a7fc 100644 --- a/src/sql/optimizer/ob_join_order.cpp +++ b/src/sql/optimizer/ob_join_order.cpp @@ -3184,8 +3184,12 @@ int ObJoinOrder::extract_preliminary_query_range(const ObIArray &ran tmp_qr = new(tmp_ptr)ObQueryRange(*allocator_); const ObDataTypeCastParams dtc_params = ObBasicSessionInfo::create_dtc_params(session_info); - bool is_in_range_optimization_enabled = session_info->is_in_range_optimization_enabled(); - if (OB_FAIL(tmp_qr->preliminary_extract_query_range(range_columns, predicates, + bool is_in_range_optimization_enabled = false; + if (OB_FAIL(ObOptimizerUtil::is_in_range_optimization_enabled(opt_ctx->get_global_hint(), + session_info, + is_in_range_optimization_enabled))) { + LOG_WARN("failed to check in range optimization enabled", K(ret)); + } else if (OB_FAIL(tmp_qr->preliminary_extract_query_range(range_columns, predicates, dtc_params, opt_ctx->get_exec_ctx(), &expr_constraints, params, false, true, diff --git a/src/sql/optimizer/ob_opt_selectivity.cpp b/src/sql/optimizer/ob_opt_selectivity.cpp index b0335d3013..072a0e6b71 100644 --- a/src/sql/optimizer/ob_opt_selectivity.cpp +++ b/src/sql/optimizer/ob_opt_selectivity.cpp @@ -3821,13 +3821,18 @@ int ObOptSelectivity::get_column_query_range(const OptSelectivityCtx &ctx, ObDataTypeCastParams dtc_params = ObBasicSessionInfo::create_dtc_params(ctx.get_session_info()); const ColumnItem* column_item = NULL; bool dummy_all_single_value_ranges = true; - + bool is_in_range_optimization_enabled = false; if (OB_ISNULL(log_plan) || OB_ISNULL(exec_ctx) || - OB_ISNULL(column_item = log_plan->get_column_item_by_id(table_id, column_id))) { + OB_ISNULL(column_item = log_plan->get_column_item_by_id(table_id, column_id)) || + OB_ISNULL(ctx.get_stmt()) || OB_ISNULL(ctx.get_stmt()->get_query_ctx())) { ret = OB_ERR_UNEXPECTED; LOG_WARN("get unexpected null", K(ret), K(log_plan), K(exec_ctx), K(column_item)); } else if (OB_FAIL(column_items.push_back(*column_item))) { LOG_WARN("failed to push back column item", K(ret)); + } else if (OB_FAIL(ObOptimizerUtil::is_in_range_optimization_enabled(ctx.get_stmt()->get_query_ctx()->get_global_hint(), + ctx.get_session_info(), + is_in_range_optimization_enabled))) { + LOG_WARN("failed to check in range optimization enabled", K(ret)); } else if (OB_FAIL(query_range.preliminary_extract_query_range(column_items, quals, dtc_params, @@ -3836,7 +3841,7 @@ int ObOptSelectivity::get_column_query_range(const OptSelectivityCtx &ctx, params, false, true, - ctx.get_session_info()->is_in_range_optimization_enabled()))) { + is_in_range_optimization_enabled))) { LOG_WARN("failed to preliminary extract query range", K(ret)); } else if (!query_range.need_deep_copy()) { if (OB_FAIL(query_range.direct_get_tablet_ranges(allocator, *exec_ctx, ranges, diff --git a/src/sql/optimizer/ob_optimizer_util.cpp b/src/sql/optimizer/ob_optimizer_util.cpp index 3e39c7b64b..b5608ee547 100644 --- a/src/sql/optimizer/ob_optimizer_util.cpp +++ b/src/sql/optimizer/ob_optimizer_util.cpp @@ -8678,3 +8678,21 @@ int ObOptimizerUtil::generate_pseudo_trans_info_expr(ObOptimizerContext &opt_ctx } return ret; } + +int ObOptimizerUtil::is_in_range_optimization_enabled(const ObGlobalHint &global_hint, ObSQLSessionInfo *session_info, bool &is_enabled) +{ + int ret = OB_SUCCESS; + bool has_hint = false; + bool is_hint_enabled = false; + if (OB_ISNULL(session_info)) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("get unexpected null", K(ret)); + } else if (OB_FAIL(global_hint.opt_params_.get_bool_opt_param(ObOptParamHint::ENABLE_IN_RANGE_OPTIMIZATION, is_hint_enabled, has_hint))) { + LOG_WARN("failed to check has opt param", K(ret)); + } else if (has_hint) { + is_enabled = is_hint_enabled; + } else { + is_enabled = session_info->is_in_range_optimization_enabled(); + } + return ret; +} diff --git a/src/sql/optimizer/ob_optimizer_util.h b/src/sql/optimizer/ob_optimizer_util.h index 2f729fc002..9ae057884f 100644 --- a/src/sql/optimizer/ob_optimizer_util.h +++ b/src/sql/optimizer/ob_optimizer_util.h @@ -1445,6 +1445,7 @@ public: const common::ObString &table_name, ObOpPseudoColumnRawExpr *&expr); + static int is_in_range_optimization_enabled(const ObGlobalHint &global_hint, ObSQLSessionInfo *session_info, bool &is_enabled); private: //disallow construct ObOptimizerUtil(); diff --git a/src/sql/optimizer/ob_select_log_plan.cpp b/src/sql/optimizer/ob_select_log_plan.cpp index 4153f3eb1d..7565bd5b1d 100644 --- a/src/sql/optimizer/ob_select_log_plan.cpp +++ b/src/sql/optimizer/ob_select_log_plan.cpp @@ -6402,7 +6402,6 @@ int ObSelectLogPlan::adjust_late_materialization_plan_structure(ObLogicalOperato if (OB_SUCC(ret)) { const ObDataTypeCastParams dtc_params = ObBasicSessionInfo::create_dtc_params(optimizer_context_.get_session_info()); - bool is_in_range_optimization_enabled = optimizer_context_.get_session_info()->is_in_range_optimization_enabled(); ObQueryRange *query_range = static_cast(get_allocator().alloc(sizeof(ObQueryRange))); const ParamStore *params = get_optimizer_context().get_params(); if (OB_ISNULL(query_range)) { @@ -6413,7 +6412,12 @@ int ObSelectLogPlan::adjust_late_materialization_plan_structure(ObLogicalOperato LOG_WARN("get unexpected null", K(ret)); } else { query_range = new(query_range)ObQueryRange(get_allocator()); - if (OB_FAIL(query_range->preliminary_extract_query_range(range_columns, + bool is_in_range_optimization_enabled = false; + if (OB_FAIL(ObOptimizerUtil::is_in_range_optimization_enabled(optimizer_context_.get_global_hint(), + optimizer_context_.get_session_info(), + is_in_range_optimization_enabled))) { + LOG_WARN("failed to check in range optimization enabled", K(ret)); + } else if (OB_FAIL(query_range->preliminary_extract_query_range(range_columns, join_conditions, dtc_params, optimizer_context_.get_exec_ctx(), diff --git a/src/sql/optimizer/ob_table_location.cpp b/src/sql/optimizer/ob_table_location.cpp index 691f1f810b..9f7bd0915a 100644 --- a/src/sql/optimizer/ob_table_location.cpp +++ b/src/sql/optimizer/ob_table_location.cpp @@ -1291,8 +1291,13 @@ int ObTableLocation::init( if (OB_FAIL(record_in_dml_partition_info(stmt, exec_ctx, filter_exprs, is_in_hit_, table_schema))) { //这是一个特殊路径,针对in filter条件 LOG_WARN("fail to record_in_dml_partition_info", K(ret)); } else if (!is_in_hit_) { - if (OB_FAIL(record_not_insert_dml_partition_info(stmt, exec_ctx, table_schema, filter_exprs, dtc_params, - session_info->is_in_range_optimization_enabled()))) { + bool is_in_range_optimization_enabled = false; + if (OB_FAIL(ObOptimizerUtil::is_in_range_optimization_enabled(stmt.get_query_ctx()->get_global_hint(), + session_info, + is_in_range_optimization_enabled))) { + LOG_WARN("failed to check in range optimization enabled", K(ret)); + } else if (OB_FAIL(record_not_insert_dml_partition_info(stmt, exec_ctx, table_schema, filter_exprs, dtc_params, + is_in_range_optimization_enabled))) { LOG_WARN("Fail to record select or update partition info", K(stmt_type_), K(ret)); } else if (OB_FAIL(get_not_insert_dml_part_sort_expr(stmt, sort_exprs))) { LOG_WARN("Failed to get not insert dml sort key with parts", K(ret)); diff --git a/src/sql/resolver/dml/ob_hint.cpp b/src/sql/resolver/dml/ob_hint.cpp index d2213ce1ab..2abf904d91 100644 --- a/src/sql/resolver/dml/ob_hint.cpp +++ b/src/sql/resolver/dml/ob_hint.cpp @@ -734,6 +734,11 @@ bool ObOptParamHint::is_param_val_valid(const OptParamType param_type, const ObO || 0 == val.get_varchar().case_compare("false")); break; } + case ENABLE_IN_RANGE_OPTIMIZATION: { + is_valid = val.is_varchar() && (0 == val.get_varchar().case_compare("true") + || 0 == val.get_varchar().case_compare("false")); + break; + } default: LOG_TRACE("invalid opt param val", K(param_type), K(val)); break; diff --git a/src/sql/resolver/dml/ob_hint.h b/src/sql/resolver/dml/ob_hint.h index 23b43da4d9..ad5cc6cdf1 100644 --- a/src/sql/resolver/dml/ob_hint.h +++ b/src/sql/resolver/dml/ob_hint.h @@ -91,6 +91,7 @@ struct ObOptParamHint DEF(USE_PART_SORT_MGB,) \ DEF(USE_DEFAULT_OPT_STAT,) \ DEF(USE_FORCE_BLOCK_SAMPLE,) \ + DEF(ENABLE_IN_RANGE_OPTIMIZATION,) \ DECLARE_ENUM(OptParamType, opt_param, OPT_PARAM_TYPE_DEF, static);