From 271e8aa047845d8f97f055d90d2a5e6b7c009fb0 Mon Sep 17 00:00:00 2001 From: obdev Date: Thu, 27 Apr 2023 14:41:41 +0000 Subject: [PATCH] Fix some user-defined_rules bug --- src/sql/plan_cache/ob_plan_cache.cpp | 30 ++++++++++++++++++- src/sql/plan_cache/ob_plan_cache.h | 1 + src/sql/plan_cache/ob_plan_cache_struct.h | 11 ------- .../resolver/expr/ob_raw_expr_deduce_type.cpp | 9 +++++- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/sql/plan_cache/ob_plan_cache.cpp b/src/sql/plan_cache/ob_plan_cache.cpp index d7fed79e18..290159a165 100644 --- a/src/sql/plan_cache/ob_plan_cache.cpp +++ b/src/sql/plan_cache/ob_plan_cache.cpp @@ -362,6 +362,31 @@ int ObPlanCache::init(int64_t hash_bucket, uint64_t tenant_id) return ret; } +int ObPlanCache::get_normalized_pattern_digest(const ObPlanCacheCtx &pc_ctx, uint64_t &pattern_digest) +{ + int ret = OB_SUCCESS; + pattern_digest = 0; + if (pc_ctx.mode_ == PC_PS_MODE || pc_ctx.mode_ == PC_PL_MODE || pc_ctx.fp_result_.pc_key_.name_.empty()) { + ObFastParserResult fp_result; + ObSQLMode sql_mode = pc_ctx.sql_ctx_.session_info_->get_sql_mode(); + ObCollationType conn_coll = pc_ctx.sql_ctx_.session_info_->get_local_collation_connection(); + FPContext fp_ctx(conn_coll); + fp_ctx.enable_batched_multi_stmt_ = pc_ctx.sql_ctx_.handle_batched_multi_stmt(); + fp_ctx.sql_mode_ = sql_mode; + if (OB_FAIL(ObSqlParameterization::fast_parser(pc_ctx.allocator_, + fp_ctx, + pc_ctx.raw_sql_, + fp_result))) { + LOG_WARN("failed to fast parser", K(ret), K(sql_mode), K(pc_ctx.raw_sql_)); + } else { + pattern_digest = fp_result.pc_key_.name_.hash(); + } + } else { + pattern_digest = pc_ctx.fp_result_.pc_key_.name_.hash(); + } + return ret; +} + int ObPlanCache::check_after_get_plan(int tmp_ret, ObILibCacheCtx &ctx, ObILibCacheObject *cache_obj) @@ -391,12 +416,15 @@ int ObPlanCache::check_after_get_plan(int tmp_ret, } if (OB_SUCC(ret) && plan != NULL) { bool is_exists = false; + uint64_t pattern_digest = 0; sql::ObUDRMgr *rule_mgr = MTL(sql::ObUDRMgr*); // when the global rule version changes or enable_user_defined_rewrite_rules changes // it is necessary to check whether the physical plan are expired if ((plan->get_rule_version() != rule_mgr->get_rule_version() || plan->is_enable_udr() != enable_udr)) { - if (OB_FAIL(rule_mgr->fuzzy_check_by_pattern_digest(pc_ctx.get_normalized_pattern_digest(), is_exists))) { + if (OB_FAIL(get_normalized_pattern_digest(pc_ctx, pattern_digest))) { + LOG_WARN("failed to calc normalized pattern digest", K(ret)); + } else if (OB_FAIL(rule_mgr->fuzzy_check_by_pattern_digest(pattern_digest, is_exists))) { LOG_WARN("failed to fuzzy check by pattern digest", K(ret)); } else if (is_exists || plan->is_rewrite_sql()) { ret = OB_OLD_SCHEMA_VERSION; diff --git a/src/sql/plan_cache/ob_plan_cache.h b/src/sql/plan_cache/ob_plan_cache.h index 78680a405d..10cedac964 100644 --- a/src/sql/plan_cache/ob_plan_cache.h +++ b/src/sql/plan_cache/ob_plan_cache.h @@ -427,6 +427,7 @@ private: ObPlanCacheCtx &pc_ctx, const ObILibCacheObject &cache_object); int check_after_get_plan(int tmp_ret, ObILibCacheCtx &ctx, ObILibCacheObject *cache_obj); + int get_normalized_pattern_digest(const ObPlanCacheCtx &pc_ctx, uint64_t &pattern_digest); private: enum PlanCacheGCStrategy { INVALID = -1, OFF = 0, REPORT = 1, AUTO = 2}; static int get_plan_cache_gc_strategy(); diff --git a/src/sql/plan_cache/ob_plan_cache_struct.h b/src/sql/plan_cache/ob_plan_cache_struct.h index 0c4f2f054d..aaff54d5f1 100644 --- a/src/sql/plan_cache/ob_plan_cache_struct.h +++ b/src/sql/plan_cache/ob_plan_cache_struct.h @@ -347,17 +347,6 @@ struct ObPlanCacheCtx : public ObILibCacheCtx return ret; } - uint64_t get_normalized_pattern_digest() const - { - common::ObString normalized_pattern; - if (mode_ == PC_PS_MODE || mode_ == PC_PL_MODE || fp_result_.pc_key_.name_.empty()) { - normalized_pattern = raw_sql_; - } else { - normalized_pattern = fp_result_.pc_key_.name_; - } - return normalized_pattern.hash(); - } - int is_retry(bool &v) const; //是否在重试之中 int is_retry_for_dup_tbl(bool &v) const; //仅复制表原因的重试才会设置为true void set_begin_commit_stmt() { begin_commit_stmt_ = true; } diff --git a/src/sql/resolver/expr/ob_raw_expr_deduce_type.cpp b/src/sql/resolver/expr/ob_raw_expr_deduce_type.cpp index bf267740b6..0f65206cae 100644 --- a/src/sql/resolver/expr/ob_raw_expr_deduce_type.cpp +++ b/src/sql/resolver/expr/ob_raw_expr_deduce_type.cpp @@ -449,7 +449,14 @@ int ObRawExprDeduceType::calc_result_type(ObNonTerminalRawExpr &expr, } if (OB_FAIL(ret) && my_session_->is_ps_prepare_stage()) { // the ps prepare stage does not do type deduction, and directly gives a default type. - result_type.set_null(); + result_type.set_type(ObVarcharType); + result_type.set_calc_type(ObVarcharType); + for (int64_t i = 0; i < types.count(); i++) { + types.at(i).set_type(ObVarcharType); + types.at(i).set_calc_type(ObVarcharType); + } + expr.set_result_type(result_type); + expr.set_input_types(types); ret = OB_SUCCESS; } // check parameters can cast to expected type