From eac33e2d8e7ffd62d20b4b8950982bd4cf6f6ebc Mon Sep 17 00:00:00 2001 From: xianyu-w <707512433@qq.com> Date: Fri, 24 Feb 2023 14:57:22 +0000 Subject: [PATCH] Fix or expansion bugs --- src/sql/rewrite/ob_transform_or_expansion.cpp | 38 ++++++++++++------- src/sql/rewrite/ob_transform_or_expansion.h | 5 ++- src/sql/rewrite/ob_transform_utils.cpp | 12 +++++- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/sql/rewrite/ob_transform_or_expansion.cpp b/src/sql/rewrite/ob_transform_or_expansion.cpp index cdeb975194..8bd6c89c1b 100644 --- a/src/sql/rewrite/ob_transform_or_expansion.cpp +++ b/src/sql/rewrite/ob_transform_or_expansion.cpp @@ -164,7 +164,7 @@ int ObTransformOrExpansion::transform_in_where_conditon(ObIArray &pa } else if (INVALID_OR_EXPAND_TYPE == ctx.or_expand_type_) { /*do nothing*/ } else if (OB_FAIL(transform_or_expansion(spj_stmt, semi_info->semi_id_, i, - true, ctx, transformed_union_stmt))) { + false, ctx, transformed_union_stmt))) { LOG_WARN("failed to do transformation", K(ret)); } else if (OB_FAIL(merge_stmt(trans_stmt, spj_stmt, transformed_union_stmt))) { LOG_WARN("failed to merge stmt", K(ret)); @@ -423,7 +423,7 @@ int ObTransformOrExpansion::try_do_transform_inner_join(ObIArray /*do nothing*/ OPT_TRACE("retry count reached max times:", try_times_); } else if (OB_FAIL(transform_or_expansion(ref_query, joined_table->table_id_, - trans_infos.at(i).pos_, true, ctx, + trans_infos.at(i).pos_, false, ctx, trans_ref_query))) { LOG_WARN("failed to do transformation", K(ret)); } else if (OB_FAIL(do_transform_for_left_join(trans_ref_query, left_unique_pos, @@ -2042,7 +2042,7 @@ int ObTransformOrExpansion::is_expand_anti_or_cond(const ObRawExpr &expr, int ObTransformOrExpansion::transform_or_expansion(ObSelectStmt *stmt, const uint64_t trans_id, const int64_t expr_pos, - bool do_classify, + bool is_topk, ObCostBasedRewriteCtx &ctx, ObSelectStmt *&trans_stmt) { @@ -2075,22 +2075,23 @@ int ObTransformOrExpansion::transform_or_expansion(ObSelectStmt *stmt, LOG_WARN("unexpect null stmt", K(ret), K(copy_stmt)); } else if (OB_FAIL(copy_stmt->deep_copy(*stmt_factory, *expr_factory, *stmt))) { LOG_WARN("failed to deep copy child statement", K(ret)); - } else if (!ctx.is_set_distinct_ && OB_FAIL(preprocess_or_condition(*copy_stmt, trans_id, expr_pos))) { - LOG_WARN("failed to preprocess or condition", K(ret)); - } else if (ctx.is_set_distinct_ && !ctx.is_unique_ && - OB_FAIL(ObTransformUtils::recursive_set_stmt_unique(copy_stmt, ctx_, true))) { - LOG_WARN("failed to set stmt unique", K(ret)); } else if (OB_FAIL(copy_stmt->get_stmt_hint().reset_explicit_trans_hint(T_USE_CONCAT))) { LOG_WARN("failed to reset explicit trans hint", K(ret)); } else if (OB_FAIL(get_expand_conds(*copy_stmt, trans_id, conds_exprs))) { LOG_WARN("failed to get expand conds", K(ret)); } else if (FALSE_IT(view_stmt = copy_stmt)) { // never reach - } else if (OB_INVALID_ID == trans_id && + } else if (OB_INVALID_ID == trans_id && !is_topk && OB_FAIL(get_condition_related_view(copy_stmt, view_stmt, view_table, - view_expr_pos, conds_exprs))) { + view_expr_pos, conds_exprs, + ctx.is_set_distinct_))) { LOG_WARN("failed to create view for tables", K(ret)); - } else if (do_classify && + } else if (!ctx.is_set_distinct_ && OB_FAIL(preprocess_or_condition(*view_stmt, trans_id, view_expr_pos))) { + LOG_WARN("failed to preprocess or condition", K(ret)); + } else if (ctx.is_set_distinct_ && !ctx.is_unique_ && + OB_FAIL(ObTransformUtils::recursive_set_stmt_unique(view_stmt, ctx_, true))) { + LOG_WARN("failed to set stmt unique", K(ret)); + } else if (!is_topk && OB_FAIL(classify_or_expr(*view_stmt, conds_exprs->at(view_expr_pos)))) { LOG_WARN("failed to classify or expr", K(ret), KPC(conds_exprs->at(view_expr_pos))); } else { @@ -3062,7 +3063,8 @@ int ObTransformOrExpansion::get_condition_related_view(ObSelectStmt *stmt, ObSelectStmt *&view_stmt, TableItem *&view_table, int64_t& expr_pos, - ObIArray *&conds_exprs) + ObIArray *&conds_exprs, + bool &is_set_distinct) { int ret = OB_SUCCESS; ObStmtFactory *stmt_factory = NULL; @@ -3137,6 +3139,14 @@ int ObTransformOrExpansion::get_condition_related_view(ObSelectStmt *stmt, } else { expr_pos = new_expr_pos; conds_exprs = &view_stmt->get_condition_exprs(); + if (is_set_distinct) { + bool has_lob = false; + if (OB_FAIL(check_select_expr_has_lob(*view_stmt, has_lob))) { + LOG_WARN("failed to check lob", K(ret)); + } else { + is_set_distinct = !has_lob; + } + } } } return ret; diff --git a/src/sql/rewrite/ob_transform_or_expansion.h b/src/sql/rewrite/ob_transform_or_expansion.h index 6c85630861..849d85b001 100644 --- a/src/sql/rewrite/ob_transform_or_expansion.h +++ b/src/sql/rewrite/ob_transform_or_expansion.h @@ -297,7 +297,7 @@ private: int transform_or_expansion(ObSelectStmt *stmt, const uint64_t trans_id, const int64_t expr_pos, - bool do_classify, + bool is_topk, ObCostBasedRewriteCtx &ctx, ObSelectStmt *&trans_stmt); int adjust_or_expansion_stmt(ObIArray *conds_exprs, @@ -372,7 +372,8 @@ private: ObSelectStmt *&view_stmt, TableItem *&view_table, int64_t &expr_pos, - ObIArray *&conds_exprs); + ObIArray *&conds_exprs, + bool &is_set_distinct); int check_delay_expr(ObRawExpr* expr, bool &delay); int check_valid_rel_table(ObSelectStmt &stmt, ObRelIds &rel_ids, diff --git a/src/sql/rewrite/ob_transform_utils.cpp b/src/sql/rewrite/ob_transform_utils.cpp index d0755f1f2f..92d82b1da3 100644 --- a/src/sql/rewrite/ob_transform_utils.cpp +++ b/src/sql/rewrite/ob_transform_utils.cpp @@ -6208,6 +6208,15 @@ int ObTransformUtils::create_set_stmt(ObTransformerCtx *ctx, } else { if (ObSelectStmt::UNION != set_type || is_distinct) { temp_stmt->assign_set_distinct(); + for (int64_t i = 0; OB_SUCC(ret) && i < child_stmts.count(); i++) { + if (OB_ISNULL(child_stmts.at(i))) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("unexpected null stmt", K(ret)); + } else if (child_stmts.at(i)->get_select_items().empty() && + OB_FAIL(create_dummy_select_item(*child_stmts.at(i), ctx))) { + LOG_WARN("failed to create dummy select", K(ret)); + } + } } else { temp_stmt->assign_set_all(); } @@ -6215,7 +6224,8 @@ int ObTransformUtils::create_set_stmt(ObTransformerCtx *ctx, temp_stmt->assign_set_op(set_type); temp_stmt->set_calc_found_rows(child_stmt->is_calc_found_rows()); // adjust statement id after set query_ctx and set_type - if (OB_FAIL(temp_stmt->adjust_statement_id(ctx->allocator_, + if (OB_FAIL(ret)) { + } else if (OB_FAIL(temp_stmt->adjust_statement_id(ctx->allocator_, ctx->src_qb_name_, ctx->src_hash_val_))) { LOG_WARN("failed to adjust statement id", K(ret), K(child_stmt));