diff --git a/src/sql/resolver/dml/ob_group_by_checker.cpp b/src/sql/resolver/dml/ob_group_by_checker.cpp index 536d27527eda6de9120cf48204a1f3ca1892f477..a1cbf97975a66a5fc50d11112b1ad277f5743658 100644 --- a/src/sql/resolver/dml/ob_group_by_checker.cpp +++ b/src/sql/resolver/dml/ob_group_by_checker.cpp @@ -209,7 +209,7 @@ int ObGroupByChecker::check_groupby_valid(ObRawExpr *expr) // eg: // select c1 +1 +2 from t1 having c1+1 >0; // it need check, report error // select c1 from t1 having 1>0; // it don't need check, it will success -int ObGroupByChecker::check_group_by(ObSelectStmt *ref_stmt, bool has_having_self_column/*default false*/, bool has_group_by_clause) +int ObGroupByChecker::check_group_by(ObSelectStmt *ref_stmt, bool has_having_self_column, bool has_group_by_clause, bool only_need_constraints) { int ret = OB_SUCCESS; // group by checker @@ -217,10 +217,11 @@ int ObGroupByChecker::check_group_by(ObSelectStmt *ref_stmt, bool has_having_sel ret = OB_ERR_UNEXPECTED; LOG_WARN("stmt is null", K(ret)); } else if (ref_stmt->has_group_by() || - ref_stmt->has_rollup() || - ref_stmt->has_grouping_sets() || - has_having_self_column || - has_group_by_clause) { + ref_stmt->has_rollup() || + (is_oracle_mode() && + (ref_stmt->has_grouping_sets() || + has_having_self_column || + has_group_by_clause))) { ObSEArray all_grouping_sets_exprs; ObSEArray all_rollup_exprs; for (int64_t i = 0; OB_SUCC(ret) && i < ref_stmt->get_grouping_sets_items_size(); ++i) { @@ -257,9 +258,10 @@ int ObGroupByChecker::check_group_by(ObSelectStmt *ref_stmt, bool has_having_sel &all_grouping_sets_exprs); checker.set_query_ctx(ref_stmt->get_query_ctx()); checker.set_nested_aggr(ref_stmt->contain_nested_aggr()); + checker.only_need_contraints_ = only_need_constraints; if (OB_FAIL(checker.check_select_stmt(ref_stmt))) { LOG_WARN("failed to check group by", K(ret)); - } else { + } else if (!only_need_constraints) { for (int64_t i = 0; OB_SUCC(ret) && i < ref_stmt->get_group_expr_size(); i++) { if (OB_FAIL(check_groupby_valid(ref_stmt->get_group_exprs().at(i)))) { LOG_WARN("failed to check groupby valid.", K(ret)); @@ -341,9 +343,24 @@ bool ObGroupByChecker::find_in_rollup(ObRawExpr &expr) LOG_DEBUG("found in rollup exprs", K(expr)); } } + if (OB_SUCCESS == check_ctx.err_code_ && !found && is_top_select_stmt()) { + for (int64_t nth_rollup = 0; !found && nth_rollup < rollup_cnt; ++nth_rollup) { + //in oracle mode, only non static const expr will be replaced later in replace_group_by_exprs + if (is_mysql_mode() || !rollup_exprs_->at(nth_rollup)->is_static_const_expr()) { + check_ctx.reset(); + check_ctx.ignore_param_ = true; + check_ctx.override_const_compare_ = true; + check_ctx.override_query_compare_ = true; + if (expr.same_as(*rollup_exprs_->at(nth_rollup), &check_ctx)) { + found = true; + LOG_DEBUG("found same structure in rollup exprs", K(expr)); + } + } + } + } } if (OB_FAIL(ret)) { - } else if (found && OB_SUCCESS == check_ctx.err_code_ && lib::is_oracle_mode()) { + } else if (found && OB_SUCCESS == check_ctx.err_code_) { if (OB_FAIL(append(query_ctx_->all_equal_param_constraints_, check_ctx.equal_param_info_))) { LOG_WARN("failed to append equal params constraints", K(ret)); @@ -376,7 +393,7 @@ bool ObGroupByChecker::find_in_group_by(ObRawExpr &expr) } } if (OB_FAIL(ret)) { - } else if (found && OB_SUCCESS == check_ctx.err_code_ && lib::is_oracle_mode()) { + } else if (found && OB_SUCCESS == check_ctx.err_code_) { // 这里抽取了两种约束: // 1. select a+1 from t1 group by a+1 --> select a+? from t1 group by a+? // 抽取一种等值约束,使得只要是两个问号值相同就可以匹配,比如: @@ -479,7 +496,15 @@ int ObGroupByChecker::add_abs_equal_constraint_in_grouping_sets(ObConstRawExpr & } // add precalc_constraint in transformer: expr > 0; } else { - //do nothing. + // same stucture, add pc const param constraint + ObStmtCompareContext check_ctx; + if (OB_FAIL(check_ctx.add_param_pair(expr.get_value().get_unknown(), NULL))) { + LOG_WARN("fail to add param pair", K(ret)); + } else if (OB_FAIL(check_ctx.add_param_pair(c_expr->get_value().get_unknown(), NULL))) { + LOG_WARN("fail to add param pair", K(ret)); + } else if (OB_FAIL(add_pc_const_param_info(check_ctx))) { + LOG_WARN("failed to append pc constraints", K(ret)); + } } } } @@ -574,7 +599,11 @@ int ObGroupByChecker::colref_belongs_to_check_stmt(ObRawExpr &expr, bool &belong int ObGroupByChecker::visit(ObConstRawExpr &expr) { int ret = OB_SUCCESS; - if (find_in_rollup(expr) || find_in_grouping_sets(expr)) { + if (is_mysql_mode()) { + if (find_in_group_by(expr) || find_in_rollup(expr) || find_in_grouping_sets(expr)) { + set_skip_expr(&expr); + } + } else if (find_in_rollup(expr) || find_in_grouping_sets(expr)) { set_skip_expr(&expr); } else if (OB_FAIL(add_abs_equal_constraint_in_grouping_sets(expr))) { LOG_WARN("fail to add abs_equal constraintd", K(ret)); @@ -585,7 +614,9 @@ int ObGroupByChecker::visit(ObConstRawExpr &expr) int ObGroupByChecker::visit(ObExecParamRawExpr &expr) { int ret = OB_SUCCESS; - if (OB_ISNULL(expr.get_ref_expr())) { + if (only_need_contraints_) { + // do nothing + } else if (OB_ISNULL(expr.get_ref_expr())) { ret = OB_ERR_UNEXPECTED; LOG_WARN("ref expr is invalid", K(ret)); } else if (OB_FAIL(expr.get_ref_expr()->preorder_accept(*this))) { @@ -627,7 +658,7 @@ int ObGroupByChecker::check_select_stmt(const ObSelectStmt *ref_stmt) // non ref query } else { int32_t ignore_scope = 0; - if (is_top_select_stmt()) { + if (is_top_select_stmt() || only_need_contraints_) { // 当前select stmt,则仅仅check having, select item, order // eg: // select c1,c2,(select d2 from t2 where t1.c1=t2.d1) as c3 from t1 group by c1,c2; @@ -676,7 +707,7 @@ int ObGroupByChecker::check_select_stmt(const ObSelectStmt *ref_stmt) ret = tmp_ret; LOG_WARN("failed to pop back stmt", K(ret)); } - if (OB_SUCC(ret)) { + if (OB_SUCC(ret) && !only_need_contraints_) { const ObIArray &child_stmts = ref_stmt->get_set_query(); for (int64_t i = 0; OB_SUCC(ret) && i < child_stmts.count(); ++i) { ret = check_select_stmt(child_stmts.at(i)); @@ -698,7 +729,7 @@ int ObGroupByChecker::visit(ObQueryRefRawExpr &expr) int ret = OB_SUCCESS; if (find_in_group_by(expr) || find_in_rollup(expr) || find_in_grouping_sets(expr)) { set_skip_expr(&expr); - } else { + } else if (!only_need_contraints_) { const ObSelectStmt *ref_stmt = expr.get_ref_stmt(); if (OB_FAIL(check_select_stmt(ref_stmt))) { LOG_WARN("failed to check select stmt", K(ret)); @@ -716,7 +747,11 @@ int ObGroupByChecker::visit(ObColumnRefRawExpr &expr) { int ret = OB_SUCCESS; bool belongs_to = true; - if (OB_FAIL(colref_belongs_to_check_stmt(expr, belongs_to))) { + if (only_need_contraints_) { + if (find_in_group_by(expr) || find_in_rollup(expr) || find_in_grouping_sets(expr)) { + set_skip_expr(&expr); + } + } else if (OB_FAIL(colref_belongs_to_check_stmt(expr, belongs_to))) { LOG_WARN("failed to get belongs to stmt", K(ret)); } else if (belongs_to) { // expr is in the current stmt @@ -763,6 +798,8 @@ int ObGroupByChecker::visit(ObOpRawExpr &expr) int ret = OB_SUCCESS; if (find_in_group_by(expr) || find_in_rollup(expr) || find_in_grouping_sets(expr)) { set_skip_expr(&expr); + } else if (only_need_contraints_) { + // do nothing } else { switch(expr.get_expr_type()) { case T_OP_CONNECT_BY_ROOT: @@ -803,6 +840,8 @@ int ObGroupByChecker::visit(ObAggFunRawExpr &expr) // then a.d2 that is in max(a.d2) is not in group by d1, it will report error if (find_in_group_by(expr) || find_in_rollup(expr) || find_in_grouping_sets(expr)) { set_skip_expr(&expr); + } else if (only_need_contraints_) { + // do nothing } else if (OB_FAIL(belongs_to_check_stmt(expr, belongs_to))) { LOG_WARN("failed to get belongs to stmt", K(ret)); } else if (belongs_to) { @@ -825,7 +864,7 @@ int ObGroupByChecker::visit(ObSysFunRawExpr &expr) int ret = OB_SUCCESS; if (find_in_group_by(expr) || find_in_rollup(expr) || find_in_grouping_sets(expr)) { set_skip_expr(&expr); - } else { + } else if (!only_need_contraints_) { if (T_FUN_SYS_ROWNUM == expr.get_expr_type()) { bool belongs_to = true; if (OB_FAIL(belongs_to_check_stmt(expr, belongs_to))) { @@ -875,7 +914,11 @@ int ObGroupByChecker::visit(ObPseudoColumnRawExpr &expr) { int ret = OB_SUCCESS; bool belongs_to = true; - if (OB_FAIL(belongs_to_check_stmt(expr, belongs_to))) { + if (only_need_contraints_) { + if (find_in_group_by(expr) || find_in_rollup(expr) || find_in_grouping_sets(expr)) { + set_skip_expr(&expr); + } + } else if (OB_FAIL(belongs_to_check_stmt(expr, belongs_to))) { LOG_WARN("failed to get belongs to stmt", K(ret)); } else if (belongs_to) { if (find_in_group_by(expr) || find_in_rollup(expr) || find_in_grouping_sets(expr)) { diff --git a/src/sql/resolver/dml/ob_group_by_checker.h b/src/sql/resolver/dml/ob_group_by_checker.h index 7b241cdc42dab66ff77180341f34887995c2daab..31f3715b11b7a10722fc2284fd6b3af03b99f1d4 100644 --- a/src/sql/resolver/dml/ob_group_by_checker.h +++ b/src/sql/resolver/dml/ob_group_by_checker.h @@ -37,7 +37,8 @@ public: query_ctx_(nullptr), has_nested_aggr_(false), is_check_order_by_(false), - dblink_groupby_expr_(NULL) + dblink_groupby_expr_(NULL), + only_need_contraints_(false) {} virtual ~ObGroupByChecker() {} @@ -74,6 +75,8 @@ private: bool has_nested_aggr_; bool is_check_order_by_; common::ObIArray *dblink_groupby_expr_; + // if true, only add constraints for shared exprs which will be replaced in replace_stmt_expr_with_groupby_exprs + bool only_need_contraints_; private: // Top select stmt 是指当前调用group by checker的select_stmt,不是select_stmt的level // 其他select_stmt会每进入一层,递增一层,同时检查结束退出,会递减一层 @@ -100,7 +103,8 @@ public: // all functions below should only called in resolver static int check_group_by(ObSelectStmt *ref_stmt, bool has_having_self_column, - bool has_group_by_clause); + bool has_group_by_clause, + bool only_need_constraints); static int check_groupby_valid(ObRawExpr *expr); static int check_by_expr( const ObSelectStmt *ref_stmt, diff --git a/src/sql/resolver/dml/ob_select_resolver.cpp b/src/sql/resolver/dml/ob_select_resolver.cpp index 995d83f511993328bd6eb1587b24fc5eb32c9825..8e01d1e0222a65a106544bbf6a485e7b4601f785 100644 --- a/src/sql/resolver/dml/ob_select_resolver.cpp +++ b/src/sql/resolver/dml/ob_select_resolver.cpp @@ -740,9 +740,11 @@ int ObSelectResolver::check_group_by() ObSelectStmt *select_stmt = get_select_stmt(); CK(OB_NOT_NULL(select_stmt), OB_NOT_NULL(session_info_)); CK(OB_NOT_NULL(select_stmt->get_query_ctx())); + bool only_need_constraints = true; if (is_only_full_group_by_on(session_info_->get_sql_mode()) && !select_stmt->get_query_ctx()->is_prepare_stmt()) { if (is_oracle_mode()) { + only_need_constraints = false; for (int64_t i = 0; OB_SUCC(ret) && i < select_stmt->get_group_expr_size(); i++) { ObRawExpr *group_by_expr = NULL; if (OB_ISNULL(group_by_expr = select_stmt->get_group_exprs().at(i))) { @@ -795,54 +797,6 @@ int ObSelectResolver::check_group_by() LOG_WARN("failed to check multi rollup items valid", K(ret)); } else {/*do nothing*/} } - - if (OB_FAIL(ret)) { - } else if (OB_FAIL(ObGroupByChecker::check_group_by(select_stmt, having_has_self_column_, has_group_by_clause()))) { - LOG_WARN("failed to check group by in oracle mode"); - } else if (OB_ISNULL(params_.param_list_) || OB_ISNULL(params_.query_ctx_)) { - // do nothing - LOG_DEBUG("null obj", K(params_.param_list_), K(params_.query_ctx_)); - } else if (NULL != params_.secondary_namespace_) { - //处于PL的prepare阶段,跳过即可 - } else { - // group by checker只是把表达式中常量的index存在了常量约束里,需要在这里回填obj - // 填充all_plan_const_param_constraints_中的obj - for (int64_t i = 0; OB_SUCC(ret) && i < params_.query_ctx_->all_plan_const_param_constraints_.count(); - i++) { - ObPCConstParamInfo &const_param_info = params_.query_ctx_->all_plan_const_param_constraints_.at(i); - const_param_info.const_params_.reset(); - for (int64_t j = 0; OB_SUCC(ret) && j < const_param_info.const_idx_.count(); j++) { - int64_t idx = const_param_info.const_idx_.at(j); - if (idx < 0 || idx >= params_.param_list_->count()) { - ret = OB_INVALID_ARGUMENT; - LOG_WARN("invalid index", K(ret), K(idx), K(i), K(j)); - } else if (OB_FAIL(const_param_info.const_params_.push_back(params_.param_list_->at(idx)))) { - LOG_WARN("failed to push back element", K(ret)); - } else { - // do nothing - } - } - } // for end - - // 填充all_possible_const_param_constraints_中的obj - for (int64_t i = 0; - OB_SUCC(ret) && i < params_.query_ctx_->all_possible_const_param_constraints_.count(); - i++) { - ObPCConstParamInfo &const_param_info = params_.query_ctx_->all_possible_const_param_constraints_.at(i); - const_param_info.const_params_.reset(); - for (int64_t j = 0; OB_SUCC(ret) && j < const_param_info.const_idx_.count(); j++) { - int64_t idx = const_param_info.const_idx_.at(j); - if (idx < 0 || idx >= params_.param_list_->count()) { - ret = OB_INVALID_ARGUMENT; - LOG_WARN("invalid index", K(ret), K(idx), K(i), K(j)); - } else if (OB_FAIL(const_param_info.const_params_.push_back(params_.param_list_->at(idx)))) { - LOG_WARN("failed to push back element", K(ret)); - } else { - // do nothing - } - } - } // for end - } } else { //在解析过程中,standard group checker会记录需要检查的column和expr,在所有语句都解析完成后 if (OB_FAIL(standard_group_checker_.check_only_full_group_by())) { @@ -851,39 +805,66 @@ int ObSelectResolver::check_group_by() } } + if (OB_FAIL(ret)) { + } else if (OB_FAIL(ObGroupByChecker::check_group_by(select_stmt, having_has_self_column_, has_group_by_clause(), only_need_constraints))) { + LOG_WARN("failed to check group by in oracle mode"); + } else if (OB_ISNULL(params_.param_list_) || OB_ISNULL(params_.query_ctx_)) { + // do nothing + LOG_DEBUG("null obj", K(params_.param_list_), K(params_.query_ctx_)); + } else if (NULL != params_.secondary_namespace_) { + //处于PL的prepare阶段,跳过即可 + } else { + // group by checker只是把表达式中常量的index存在了常量约束里,需要在这里回填obj + // 填充all_plan_const_param_constraints_中的obj + for (int64_t i = 0; OB_SUCC(ret) && i < params_.query_ctx_->all_plan_const_param_constraints_.count(); + i++) { + ObPCConstParamInfo &const_param_info = params_.query_ctx_->all_plan_const_param_constraints_.at(i); + const_param_info.const_params_.reset(); + for (int64_t j = 0; OB_SUCC(ret) && j < const_param_info.const_idx_.count(); j++) { + int64_t idx = const_param_info.const_idx_.at(j); + if (idx < 0 || idx >= params_.param_list_->count()) { + ret = OB_INVALID_ARGUMENT; + LOG_WARN("invalid index", K(ret), K(idx), K(i), K(j)); + } else if (OB_FAIL(const_param_info.const_params_.push_back(params_.param_list_->at(idx)))) { + LOG_WARN("failed to push back element", K(ret)); + } else { + // do nothing + } + } + } // for end + + // 填充all_possible_const_param_constraints_中的obj + for (int64_t i = 0; + OB_SUCC(ret) && i < params_.query_ctx_->all_possible_const_param_constraints_.count(); + i++) { + ObPCConstParamInfo &const_param_info = params_.query_ctx_->all_possible_const_param_constraints_.at(i); + const_param_info.const_params_.reset(); + for (int64_t j = 0; OB_SUCC(ret) && j < const_param_info.const_idx_.count(); j++) { + int64_t idx = const_param_info.const_idx_.at(j); + if (idx < 0 || idx >= params_.param_list_->count()) { + ret = OB_INVALID_ARGUMENT; + LOG_WARN("invalid index", K(ret), K(idx), K(i), K(j)); + } else if (OB_FAIL(const_param_info.const_params_.push_back(params_.param_list_->at(idx)))) { + LOG_WARN("failed to push back element", K(ret)); + } else { + // do nothing + } + } + } // for end + } + // replace with same group by columns. // groupby之上的计算我们放在这里统一处理: // 1. select item/having/order item中的表达式树(子树)需要每个都在group by列中找到 // 2. 递归查找是否在groupby列中,将在groupby的列的指针替换。 if (OB_SUCC(ret)) { - if (OB_FAIL(replace_stmt_expr_with_groupby_exprs(select_stmt, params_.query_ctx_))) { + if (OB_FAIL(ObTransformUtils::replace_stmt_expr_with_groupby_exprs(select_stmt, NULL))) { LOG_WARN("failed to replace stmt expr with groupby columns", K(ret)); } } return ret; } -int ObSelectResolver::replace_stmt_expr_with_groupby_exprs(ObSelectStmt *select_stmt, - ObQueryCtx *query_ctx) -{ - int ret = OB_SUCCESS; - ObSEArray pc_constraints; - ObSEArray eq_constraints; - if (OB_ISNULL(query_ctx)) { - ret = OB_ERR_UNEXPECTED; - LOG_WARN("unexpected null", K(ret), K(query_ctx)); - } else if (OB_FAIL(ObTransformUtils::inner_replace_stmt_expr_with_groupby_exprs(select_stmt, params_.param_list_, pc_constraints, eq_constraints))) { - LOG_WARN("failed to replace stmt expr with groupby columns", K(ret)); - } else if (OB_FAIL(append_array_no_dup(query_ctx->all_plan_const_param_constraints_, pc_constraints))) { - LOG_WARN("failed to append const param info", K(ret)); - } else if (OB_FAIL(append_array_no_dup(query_ctx->all_possible_const_param_constraints_, pc_constraints))) { - LOG_WARN("failed to append const param info", K(ret)); - } else if (OB_FAIL(append_array_no_dup(query_ctx->all_equal_param_constraints_, eq_constraints))) { - LOG_WARN("failed to append const param info", K(ret)); - } - return ret; -} - // 1. lob type can't be ordered // 2. the order item should be exists in select items if has distinct int ObSelectResolver::check_order_by() diff --git a/src/sql/resolver/dml/ob_select_resolver.h b/src/sql/resolver/dml/ob_select_resolver.h index adf8f8d3e4c63dc92da28ce8cdda98384ed67fed..5e549e661502daf442730e3e62130e1c82986a24 100644 --- a/src/sql/resolver/dml/ob_select_resolver.h +++ b/src/sql/resolver/dml/ob_select_resolver.h @@ -428,8 +428,6 @@ private: int add_name_for_anonymous_view(); int add_name_for_anonymous_view_recursive(TableItem *table_item); - int replace_stmt_expr_with_groupby_exprs(ObSelectStmt *select_stmt, ObQueryCtx *query_ctx); - int is_need_check_col_dup(const ObRawExpr *expr, bool &need_check); protected: // data members diff --git a/src/sql/resolver/expr/ob_raw_expr.cpp b/src/sql/resolver/expr/ob_raw_expr.cpp index f5a8e805b63250c6c52be48f13a6e7d66554400e..e0d7291e45d93a4dfeb1e333d375c3061de92917 100644 --- a/src/sql/resolver/expr/ob_raw_expr.cpp +++ b/src/sql/resolver/expr/ob_raw_expr.cpp @@ -805,11 +805,15 @@ bool ObExprEqualCheckContext::compare_const(const ObConstRawExpr &left, int &ret = err_code_; bool result = false; if (left.get_result_type() == right.get_result_type()) { - const ObObj &this_value = left.get_value().is_unknown() ? + if (ignore_param_ && (left.get_value().is_unknown() || right.get_value().is_unknown())) { + result = true; + } else { + const ObObj &this_value = left.get_value().is_unknown() ? left.get_result_type().get_param() : left.get_value(); - const ObObj &other_value = right.get_value().is_unknown() ? + const ObObj &other_value = right.get_value().is_unknown() ? right.get_result_type().get_param() : right.get_value(); - result = this_value.is_equal(other_value, CS_TYPE_BINARY); + result = this_value.is_equal(other_value, CS_TYPE_BINARY); + } } if (OB_SUCC(ret) && result && left.get_value().is_unknown()) { if (OB_FAIL(add_param_pair(left.get_value().get_unknown(), NULL))) { diff --git a/src/sql/resolver/expr/ob_raw_expr.h b/src/sql/resolver/expr/ob_raw_expr.h index aa32d4aad56863d626965795c1e75255845526e9..fcaa715f3001dd4046c66b1483f2e95f4dbbd188 100644 --- a/src/sql/resolver/expr/ob_raw_expr.h +++ b/src/sql/resolver/expr/ob_raw_expr.h @@ -1191,7 +1191,8 @@ struct ObExprEqualCheckContext override_set_op_compare_(false), err_code_(common::OB_SUCCESS), param_expr_(), - need_check_deterministic_(false) + need_check_deterministic_(false), + ignore_param_(false) { } ObExprEqualCheckContext(bool need_check_deterministic) : override_const_compare_(false), @@ -1202,7 +1203,8 @@ struct ObExprEqualCheckContext override_set_op_compare_(false), err_code_(common::OB_SUCCESS), param_expr_(), - need_check_deterministic_(need_check_deterministic) + need_check_deterministic_(need_check_deterministic), + ignore_param_(false) { } virtual ~ObExprEqualCheckContext() {} struct ParamExprPair @@ -1248,6 +1250,7 @@ struct ObExprEqualCheckContext err_code_ = OB_SUCCESS; param_expr_.reset(); need_check_deterministic_ = false; + ignore_param_ = false; } bool override_const_compare_; bool override_column_compare_; @@ -1259,6 +1262,7 @@ struct ObExprEqualCheckContext //when compare with T_QUESTIONMARK, as T_QUESTIONMARK is unkown, record this first. common::ObSEArray param_expr_; bool need_check_deterministic_; + bool ignore_param_; // only compare structure of expr }; struct ObExprParamCheckContext : ObExprEqualCheckContext diff --git a/src/sql/rewrite/ob_stmt_comparer.cpp b/src/sql/rewrite/ob_stmt_comparer.cpp index 0222d69637dc5eb525881d0cd45089df0ea6f8f2..6dadf17606c33bfe5b5554356bd852416092d63b 100644 --- a/src/sql/rewrite/ob_stmt_comparer.cpp +++ b/src/sql/rewrite/ob_stmt_comparer.cpp @@ -189,6 +189,8 @@ bool ObStmtCompareContext::compare_const(const ObConstRawExpr &left, const ObCon } } else if (is_left_calc_item || is_right_calc_item) { bret = false; + } else if (ignore_param_) { + bret = ObExprEqualCheckContext::compare_const(left, right); } else if (left.get_result_type().get_param().is_equal( right.get_result_type().get_param(), CS_TYPE_BINARY)) { ObPCParamEqualInfo info; diff --git a/src/sql/rewrite/ob_transform_pre_process.cpp b/src/sql/rewrite/ob_transform_pre_process.cpp index e6dc2313fe30ff8f7c07d97f9b7dd677a356c22f..9ffaba958ff97dbb1c37799a43790bf3c46918b1 100644 --- a/src/sql/rewrite/ob_transform_pre_process.cpp +++ b/src/sql/rewrite/ob_transform_pre_process.cpp @@ -1237,7 +1237,7 @@ int ObTransformPreProcess::create_set_view_stmt(ObSelectStmt *origin_stmt, 1 so that 1 in "select c1-1 from t1 group by grouping sets(c1,1);" share same expr. */ - if (OB_FAIL(ObTransformUtils::replace_stmt_expr_with_groupby_exprs(origin_stmt, ctx_, true))) { + if (OB_FAIL(ObTransformUtils::replace_stmt_expr_with_groupby_exprs(origin_stmt, ctx_))) { LOG_WARN("failed to replace stmt expr with groupby columns", K(ret)); } for (int64_t i = 0; OB_SUCC(ret) && i < count; i++) { @@ -1277,7 +1277,7 @@ int ObTransformPreProcess::create_set_view_stmt(ObSelectStmt *origin_stmt, groupby_stmt->get_window_func_exprs().reset(); ObSEArray old_exprs; ObSEArray new_exprs; - if (OB_FAIL(ObTransformUtils::replace_stmt_expr_with_groupby_exprs(groupby_stmt, ctx_))) { + if (OB_FAIL(ObTransformUtils::replace_stmt_expr_with_groupby_exprs(groupby_stmt, NULL))) { LOG_WARN("failed to replace stmt expr with groupby columns", K(ret)); } else if (OB_FAIL(create_select_list_from_grouping_sets(groupby_stmt, groupby_exprs_list, diff --git a/src/sql/rewrite/ob_transform_utils.cpp b/src/sql/rewrite/ob_transform_utils.cpp index 4b2fbfe8f1e6a0be5055668a0a127c47bab4978c..f32a557822e5635ec1c8893f0c57e0e9dcec86d3 100644 --- a/src/sql/rewrite/ob_transform_utils.cpp +++ b/src/sql/rewrite/ob_transform_utils.cpp @@ -9168,32 +9168,9 @@ int ObTransformUtils::add_const_param_constraints(ObRawExpr *expr, return ret; } -int ObTransformUtils::replace_stmt_expr_with_groupby_exprs(ObSelectStmt *select_stmt, - ObTransformerCtx *trans_ctx, - bool need_check_add_expr) { - int ret = OB_SUCCESS; - ObPhysicalPlanCtx *plan_ctx = NULL; - ObSEArray pc_constraints; - ObSEArray eq_constraints; - if (OB_ISNULL(trans_ctx) || OB_ISNULL(trans_ctx->exec_ctx_) || OB_ISNULL(plan_ctx = trans_ctx->exec_ctx_->get_physical_plan_ctx())) { - ret = OB_ERR_UNEXPECTED; - LOG_WARN("get unexpected null", K(ret), K(trans_ctx), K(plan_ctx)); - } else if (OB_FAIL(inner_replace_stmt_expr_with_groupby_exprs(select_stmt, &plan_ctx->get_param_store(), pc_constraints, eq_constraints, need_check_add_expr ? trans_ctx : NULL))) { - LOG_WARN("fail to replace stmt expr with group by exprs", K(ret)); - } else if (OB_FAIL(append_array_no_dup(trans_ctx->plan_const_param_constraints_, pc_constraints))) { - LOG_WARN("failed to append const param info", K(ret)); - } else if (OB_FAIL(append_array_no_dup(trans_ctx->equal_param_constraints_, eq_constraints))) { - LOG_WARN("failed to append const param info", K(ret)); - } - return ret; -} - //pc_constraints are for non-paramlized groupby exprs while eq_constraints are for paramlized groupby exprs -int ObTransformUtils::inner_replace_stmt_expr_with_groupby_exprs(ObSelectStmt *select_stmt, - const ParamStore *param_store, - ObIArray& pc_constraints, - ObIArray & eq_constraints, - ObTransformerCtx *trans_ctx/*default null*/) +int ObTransformUtils::replace_stmt_expr_with_groupby_exprs(ObSelectStmt *select_stmt, + ObTransformerCtx *trans_ctx) { int ret = OB_SUCCESS; if (OB_ISNULL(select_stmt)) { @@ -9206,9 +9183,8 @@ int ObTransformUtils::inner_replace_stmt_expr_with_groupby_exprs(ObSelectStmt *s if (OB_FAIL(replace_with_groupby_exprs(select_stmt, select_items.at(i).expr_, true, - param_expr_idxs, - eq_constraints, - trans_ctx))) { + trans_ctx, + false))) { LOG_WARN("failed to replace with groupby columns.", K(ret)); } else { /*do nothing.*/ } } @@ -9217,9 +9193,8 @@ int ObTransformUtils::inner_replace_stmt_expr_with_groupby_exprs(ObSelectStmt *s if (OB_FAIL(replace_with_groupby_exprs(select_stmt, having_exprs.at(i), true, - param_expr_idxs, - eq_constraints, - trans_ctx))) { + trans_ctx, + false))) { LOG_WARN("failed to replace with groupby columns.", K(ret)); } else { /*do nothing.*/ } } @@ -9228,35 +9203,11 @@ int ObTransformUtils::inner_replace_stmt_expr_with_groupby_exprs(ObSelectStmt *s if (OB_FAIL(replace_with_groupby_exprs(select_stmt, order_items.at(i).expr_, false, - param_expr_idxs, - eq_constraints, - trans_ctx))) { + trans_ctx, + false))) { LOG_WARN("failed to replace with groupby columns.", K(ret)); } else { /*do nothing.*/ } } - if (OB_SUCC(ret) && param_expr_idxs.count() > 0) { - //build pc_constraints - if (OB_ISNULL(param_store)) { - ret = OB_ERR_UNEXPECTED; - LOG_WARN("get unexpected null", K(ret), K(param_store)); - } else { - for (int64_t i = 0; OB_SUCC(ret) && i < param_expr_idxs.count(); i++) { - ObPCConstParamInfo const_param_info; - int64_t param_idx = param_expr_idxs.at(i); - if (OB_UNLIKELY(param_idx < 0 || param_idx >= param_store->count())) { - ret = OB_ERR_UNEXPECTED; - LOG_WARN("get unexpected error", K(ret), K(param_idx), - K(param_store->count())); - } else if (OB_FAIL(const_param_info.const_idx_.push_back(param_idx))) { - LOG_WARN("failed to push back param idx", K(ret)); - } else if (OB_FAIL(const_param_info.const_params_.push_back(param_store->at(param_idx)))) { - LOG_WARN("failed to push back value", K(ret)); - } else if (OB_FAIL(pc_constraints.push_back(const_param_info))) { - LOG_WARN("failed to push back param info", K(ret)); - } else {/*do nothing*/} - } - } - } } return ret; } @@ -9264,10 +9215,8 @@ int ObTransformUtils::inner_replace_stmt_expr_with_groupby_exprs(ObSelectStmt *s int ObTransformUtils::replace_with_groupby_exprs(ObSelectStmt *select_stmt, ObRawExpr *&expr, bool need_query_compare, - ObIArray & param_expr_idxs, - ObIArray & eq_constraints, ObTransformerCtx *trans_ctx, - bool in_add_expr/*default false*/) + bool in_add_expr) { int ret = OB_SUCCESS; ObStmtCompareContext check_context; @@ -9293,8 +9242,6 @@ int ObTransformUtils::replace_with_groupby_exprs(ObSelectStmt *select_stmt, if (OB_FAIL(SMART_CALL(replace_with_groupby_exprs(select_stmt, expr->get_param_expr(i), need_query_compare, - param_expr_idxs, - eq_constraints, trans_ctx, T_OP_ADD == expr->get_expr_type())))) { LOG_WARN("failed to replace with groupby columns.", K(ret)); @@ -9306,41 +9253,22 @@ int ObTransformUtils::replace_with_groupby_exprs(ObSelectStmt *select_stmt, ObIArray &grouping_sets_items = select_stmt->get_grouping_sets_items(); ObIArray &multi_rollup_items = select_stmt->get_multi_rollup_items(); for (int64_t i = 0; OB_SUCC(ret) && !is_existed && i < groupby_exprs.count(); i++) { - check_context.param_expr_.reset(); - check_context.equal_param_info_.reset(); if (OB_ISNULL(groupby_exprs.at(i))) { ret = OB_ERR_UNEXPECTED; LOG_WARN("got an unexpected null", K(ret)); } else if (groupby_exprs.at(i)->same_as(*expr, &check_context)) { - // add_var_to_array_no_dup - if (OB_FAIL(append(param_exprs_local, check_context.param_expr_))) { - LOG_WARN("fail to append param expr", K(ret)); - } else if (OB_FAIL(append(equal_params_local, check_context.equal_param_info_))) { - LOG_WARN("fail to append equal param info", K(ret)); - } else { - expr = groupby_exprs.at(i); - is_existed = true; - } + expr = groupby_exprs.at(i); + is_existed = true; } else { /*do nothing.*/ } - } for (int64_t i = 0; OB_SUCC(ret) && !is_existed && i < rollup_exprs.count(); i++) { - check_context.param_expr_.reset(); - check_context.equal_param_info_.reset(); if (OB_ISNULL(rollup_exprs.at(i))) { ret = OB_ERR_UNEXPECTED; LOG_WARN("got an unexpected null", K(ret)); - } else if (((lib::is_mysql_mode() && !(expr->has_flag(IS_CONST) && ob_is_integer_type(expr->get_result_type().get_type())))|| - (lib::is_oracle_mode() && !expr->is_static_const_expr())) + } else if ((lib::is_mysql_mode()|| !expr->is_static_const_expr()) && rollup_exprs.at(i)->same_as(*expr, &check_context)) { - if (OB_FAIL(append(param_exprs_local, check_context.param_expr_))) { - LOG_WARN("fail to append param expr", K(ret)); - } else if (OB_FAIL(append(equal_params_local, check_context.equal_param_info_))) { - LOG_WARN("fail to append equal param info", K(ret)); - } else { - expr = rollup_exprs.at(i); - is_existed = true; - } + expr = rollup_exprs.at(i); + is_existed = true; } else { /*do nothing.*/ } } for (int64_t i = 0; OB_SUCC(ret) && !is_existed && i < grouping_sets_items.count(); i++) { @@ -9354,14 +9282,8 @@ int ObTransformUtils::replace_with_groupby_exprs(ObSelectStmt *select_stmt, ret = OB_ERR_UNEXPECTED; LOG_WARN("got an unexpected null", K(ret)); } else if (groupby_exprs.at(k)->same_as(*expr, &check_context)) { - if (OB_FAIL(append(param_exprs_local, check_context.param_expr_))) { - LOG_WARN("fail to append param expr", K(ret)); - } else if (OB_FAIL(append(equal_params_local, check_context.equal_param_info_))) { - LOG_WARN("fail to append equal param info", K(ret)); - } else { - expr = groupby_exprs.at(k); - is_existed = true; - } + expr = groupby_exprs.at(k); + is_existed = true; } else if (trans_ctx != NULL && in_add_expr && expr->get_expr_type() == T_QUESTIONMARK && groupby_exprs.at(k)->get_expr_type() == T_QUESTIONMARK && @@ -9377,7 +9299,6 @@ int ObTransformUtils::replace_with_groupby_exprs(ObSelectStmt *select_stmt, if (OB_FAIL(replace_add_exprs_with_groupby_exprs(expr, groupby_exprs.at(k), trans_ctx, - equal_params_local, is_existed))) { LOG_WARN("replace exprs in add failed", K(ret)); } @@ -9391,20 +9312,12 @@ int ObTransformUtils::replace_with_groupby_exprs(ObSelectStmt *select_stmt, for (int64_t k = 0; OB_SUCC(ret) && !is_existed && k < rollup_list_exprs.count(); k++) { ObIArray &groupby_exprs = rollup_list_exprs.at(k).groupby_exprs_; for (int64_t m = 0; OB_SUCC(ret) && !is_existed && m < groupby_exprs.count(); m++) { - check_context.param_expr_.reset(); - check_context.equal_param_info_.reset(); if (OB_ISNULL(groupby_exprs.at(m))) { ret = OB_ERR_UNEXPECTED; LOG_WARN("got an unexpected null", K(ret)); } else if (groupby_exprs.at(m)->same_as(*expr, &check_context)) { - if (OB_FAIL(append(param_exprs_local, check_context.param_expr_))) { - LOG_WARN("fail to append param expr", K(ret)); - } else if (OB_FAIL(append(equal_params_local, check_context.equal_param_info_))) { - LOG_WARN("fail to append equal param info", K(ret)); - } else { - expr = groupby_exprs.at(m); - is_existed = true; - } + expr = groupby_exprs.at(m); + is_existed = true; } else { /*do nothing.*/ } } } @@ -9417,36 +9330,17 @@ int ObTransformUtils::replace_with_groupby_exprs(ObSelectStmt *select_stmt, for (int64_t j = 0; OB_SUCC(ret) && !is_existed && j < rollup_list_exprs.count(); j++) { ObIArray &groupby_exprs = rollup_list_exprs.at(j).groupby_exprs_; for (int64_t k = 0; OB_SUCC(ret) && !is_existed && k < groupby_exprs.count(); k++) { - check_context.param_expr_.reset(); - check_context.equal_param_info_.reset(); if (OB_ISNULL(groupby_exprs.at(k))) { ret = OB_ERR_UNEXPECTED; LOG_WARN("got an unexpected null", K(ret)); } else if (groupby_exprs.at(k)->same_as(*expr, &check_context)) { - if (OB_FAIL(append(param_exprs_local, check_context.param_expr_))) { - LOG_WARN("fail to append param expr", K(ret)); - } else if (OB_FAIL(append(equal_params_local, check_context.equal_param_info_))) { - LOG_WARN("fail to append equal param info", K(ret)); - } else { - expr = groupby_exprs.at(k); - is_existed = true; - } + expr = groupby_exprs.at(k); + is_existed = true; } else { /*do nothing.*/ } } } } } - if (OB_SUCC(ret)) { - if (OB_FAIL(append_array_no_dup(eq_constraints, equal_params_local))) { - LOG_WARN("fail to append equal param info", K(ret)); - } else { - for (int64_t i = 0; OB_SUCC(ret) && i < param_exprs_local.count(); ++i) { - if (OB_FAIL(add_var_to_array_no_dup(param_expr_idxs, param_exprs_local.at(i).param_idx_))) { - LOG_WARN("fail to append equal param constraints", K(ret)); - } - } - } - } } return ret; } @@ -9454,7 +9348,6 @@ int ObTransformUtils::replace_with_groupby_exprs(ObSelectStmt *select_stmt, int ObTransformUtils::replace_add_exprs_with_groupby_exprs(ObRawExpr *&expr_l, ObRawExpr *expr_r, ObTransformerCtx *trans_ctx, - ObIArray & eq_constraints, bool &is_existed) { int ret = OB_SUCCESS; @@ -9487,14 +9380,8 @@ int ObTransformUtils::replace_add_exprs_with_groupby_exprs(ObRawExpr *&expr_l, } else if (check_objparam_negative(left_param) && !check_objparam_negative(right_param)) { // replace the expr with a T_OP_NEG expr who's child is groupby_exprs.at(k); ObOpRawExpr *neg = NULL; - ObPCParamEqualInfo eq_info; - eq_info.first_param_idx_ = idx_left; - eq_info.second_param_idx_ = idx_right; - eq_info.use_abs_cmp_ = true; if (OB_FAIL(expr_factory->create_raw_expr(T_OP_NEG, neg))) { LOG_WARN("failed to create neg expr", K(ret)); - } else if (OB_FAIL(eq_constraints.push_back(eq_info))) { - LOG_WARN("failed to push back equal param", K(ret)); } else if (OB_ISNULL(expr_l = neg)) { ret = OB_ERR_UNEXPECTED; LOG_WARN("neg expr is NULL", K(ret)); diff --git a/src/sql/rewrite/ob_transform_utils.h b/src/sql/rewrite/ob_transform_utils.h index 5d70c98823cd62bf53e99415424024b15d2006ea..9fec8f53371e23f4ec553c9fd10cc59d2d93114d 100644 --- a/src/sql/rewrite/ob_transform_utils.h +++ b/src/sql/rewrite/ob_transform_utils.h @@ -1337,20 +1337,12 @@ public: static int add_const_param_constraints(ObRawExpr *expr, ObTransformerCtx *ctx); - static int inner_replace_stmt_expr_with_groupby_exprs(ObSelectStmt *select_stmt, - const ParamStore *param_store, - ObIArray& pc_constrants, - ObIArray & eq_constraints, - ObTransformerCtx *trans_ctx = NULL); - static int replace_stmt_expr_with_groupby_exprs(ObSelectStmt *select_stmt, - ObTransformerCtx *trans_ctx, - bool need_check_add_expr = false); + ObTransformerCtx *trans_ctx); static int replace_add_exprs_with_groupby_exprs(ObRawExpr *&expr_l, ObRawExpr *expr_r, ObTransformerCtx *trans_ctx, - ObIArray & eq_constraints, bool &is_existed); static bool check_objparam_abs_equal(const ObObjParam &obj1, const ObObjParam &obj2); @@ -1364,10 +1356,8 @@ public: static int replace_with_groupby_exprs(ObSelectStmt *select_stmt, ObRawExpr *&expr, bool need_query_compare, - ObIArray& param_expr_idxs, - ObIArray & eq_constraints, - ObTransformerCtx *tran_ctx = NULL, - bool in_add_expr = false); + ObTransformerCtx *tran_ctx, + bool in_add_expr); static int add_constraint_for_groupby_expr(ObTransformerCtx *trans_ctx, ObSelectStmt *select_stmt, ObRawExpr* groupby_expr, ObRawExpr* old_expr); static int add_param_not_null_constraint(ObTransformerCtx &ctx,