diff --git a/src/sql/rewrite/ob_transform_const_propagate.cpp b/src/sql/rewrite/ob_transform_const_propagate.cpp index cb24ce008c..c2cc529ed4 100644 --- a/src/sql/rewrite/ob_transform_const_propagate.cpp +++ b/src/sql/rewrite/ob_transform_const_propagate.cpp @@ -2443,7 +2443,7 @@ int ObTransformConstPropagate::acquire_pullup_infos(ObDMLStmt *stmt, PullupConstInfos *new_infos = NULL; index = stmt_pullup_const_infos_.count(); if (OB_ISNULL(new_infos = (PullupConstInfos *) allocator_.alloc(sizeof(PullupConstInfos)))) { - ret = OB_ERR_UNEXPECTED; + ret = OB_ALLOCATE_MEMORY_FAILED; LOG_WARN("failed to allocate pullup predicates array", K(ret)); } else { new_infos = new (new_infos) PullupConstInfos(); diff --git a/src/sql/rewrite/ob_transform_pre_process.cpp b/src/sql/rewrite/ob_transform_pre_process.cpp index bb86164174..d85d45f1d0 100644 --- a/src/sql/rewrite/ob_transform_pre_process.cpp +++ b/src/sql/rewrite/ob_transform_pre_process.cpp @@ -8055,6 +8055,35 @@ int ObTransformPreProcess::transform_full_outer_join(ObDMLStmt *&stmt, bool &tra return ret; } +int ObTransformPreProcess::convert_join_preds_vector_to_scalar(JoinedTable &joined_table, + bool &trans_happened) +{ + int ret = OB_SUCCESS; + ObSEArray new_join_cond; + bool is_happened = false; + if (OB_ISNULL(ctx_)) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("get NULL ptr", K(ret) , KP(ctx_)); + } else { + for (int64_t i = 0; OB_SUCC(ret) && i < joined_table.get_join_conditions().count(); i++) { + if (OB_FAIL(ObTransformUtils::convert_preds_vector_to_scalar(*ctx_, + joined_table.get_join_conditions().at(i), + new_join_cond, + is_happened))) { + LOG_WARN("failed to convert predicate vector to scalar", K(ret)); + } + } + if (OB_SUCC(ret) && is_happened) { + trans_happened = true; + joined_table.get_join_conditions().reset(); + if (OB_FAIL(append(joined_table.get_join_conditions(), new_join_cond))) { + LOG_WARN("failed to append join conditions", K(ret)); + } + } + } + return ret; +} + int ObTransformPreProcess::recursively_eliminate_full_join(ObDMLStmt &stmt, TableItem *table_item, bool &trans_happened) @@ -8065,9 +8094,9 @@ int ObTransformPreProcess::recursively_eliminate_full_join(ObDMLStmt &stmt, JoinedTable *joined_table = static_cast(table_item); TableItem *view_table = NULL; TableItem *from_table = table_item; - if (OB_ISNULL(table_item)) { + if (OB_ISNULL(table_item) || OB_ISNULL(ctx_)) { ret = OB_ERR_UNEXPECTED; - LOG_WARN("table item is null", K(ret), K(table_item)); + LOG_WARN("table item is null", K(ret), K(table_item), K(ctx_)); } else if (!table_item->is_joined_table()) { /* do nothing */ } else if (OB_FAIL(recursively_eliminate_full_join(stmt, @@ -8080,6 +8109,8 @@ int ObTransformPreProcess::recursively_eliminate_full_join(ObDMLStmt &stmt, LOG_WARN("failed to transform full nl join.", K(ret)); } else if (!joined_table->is_full_join()) { /* do nothing */ + } else if (OB_FAIL(convert_join_preds_vector_to_scalar(*joined_table, trans_happened))) { + LOG_WARN("failed to convert join preds vector to scalar", K(ret)); } else if (OB_FAIL(check_join_condition(&stmt, joined_table, has_euqal, has_subquery))) { LOG_WARN("failed to check join condition", K(ret)); } else if (has_euqal || has_subquery) { diff --git a/src/sql/rewrite/ob_transform_pre_process.h b/src/sql/rewrite/ob_transform_pre_process.h index 02843169fa..a990dcc3d6 100644 --- a/src/sql/rewrite/ob_transform_pre_process.h +++ b/src/sql/rewrite/ob_transform_pre_process.h @@ -599,6 +599,7 @@ struct DistinctObjMeta int expand_last_insert_id_for_join(ObDMLStmt &stmt, JoinedTable *join_table, bool &is_happened); int remove_last_insert_id(ObRawExpr *&expr); int check_last_insert_id_removable(const ObRawExpr *expr, bool &is_removable); + int convert_join_preds_vector_to_scalar(JoinedTable &joined_table, bool &trans_happened); private: DISALLOW_COPY_AND_ASSIGN(ObTransformPreProcess); }; diff --git a/src/sql/rewrite/ob_transform_predicate_move_around.cpp b/src/sql/rewrite/ob_transform_predicate_move_around.cpp index ebcbc7b1a8..675d14ef3e 100644 --- a/src/sql/rewrite/ob_transform_predicate_move_around.cpp +++ b/src/sql/rewrite/ob_transform_predicate_move_around.cpp @@ -2995,7 +2995,7 @@ int ObTransformPredicateMoveAround::acquire_transform_params(ObDMLStmt *stmt, PullupPreds *new_preds = NULL; index = stmt_pullup_preds_.count(); if (OB_ISNULL(new_preds = (PullupPreds *) allocator_.alloc(sizeof(PullupPreds)))) { - ret = OB_ERR_UNEXPECTED; + ret = OB_ALLOCATE_MEMORY_FAILED; LOG_WARN("failed to allocate pullup predicates array", K(ret)); } else { new_preds = new (new_preds) PullupPreds(); diff --git a/src/sql/rewrite/ob_transform_simplify_expr.cpp b/src/sql/rewrite/ob_transform_simplify_expr.cpp index f367222e0c..fd3f93e38f 100644 --- a/src/sql/rewrite/ob_transform_simplify_expr.cpp +++ b/src/sql/rewrite/ob_transform_simplify_expr.cpp @@ -591,7 +591,7 @@ int ObTransformSimplifyExpr::convert_preds_vector_to_scalar(ObDMLStmt *stmt, boo int ret = OB_SUCCESS; ObSEArray new_cond; bool is_happened = false; - if (OB_ISNULL(stmt)) { + if (OB_ISNULL(stmt) || OB_ISNULL(ctx_)) { ret = OB_ERR_UNEXPECTED; LOG_WARN("NULL stmt", K(ret)); } else if (!stmt->is_sel_del_upd()) { @@ -600,8 +600,8 @@ int ObTransformSimplifyExpr::convert_preds_vector_to_scalar(ObDMLStmt *stmt, boo /// 需要转换的谓词有: where, join condition. /// having 中能下降的都下降过了. for (int64_t i = 0; OB_SUCC(ret) && i < stmt->get_condition_size(); i++) { - if (OB_FAIL(inner_convert_preds_vector_to_scalar( - stmt->get_condition_expr(i), new_cond, is_happened))) { + if (OB_FAIL(ObTransformUtils::convert_preds_vector_to_scalar(*ctx_, + stmt->get_condition_expr(i), new_cond, is_happened))) { LOG_WARN("failed to convert predicate vector to scalar", K(ret)); } } @@ -614,9 +614,9 @@ int ObTransformSimplifyExpr::convert_preds_vector_to_scalar(ObDMLStmt *stmt, boo } if (OB_SUCC(ret)) { for (int64_t i = 0; OB_SUCC(ret) && i < stmt->get_joined_tables().count(); i++) { - if (OB_FAIL(convert_join_preds_vector_to_scalar( - stmt->get_joined_tables().at(i), trans_happened))) { - LOG_WARN("failed to convert join preds vector to scalar", K(ret)); + if (OB_FAIL(recursively_convert_join_preds_vector_to_scalar(stmt->get_joined_tables().at(i), + trans_happened))) { + LOG_WARN("failed to convert join vector condition to scalar", K(ret)); } } } @@ -624,19 +624,30 @@ int ObTransformSimplifyExpr::convert_preds_vector_to_scalar(ObDMLStmt *stmt, boo return ret; } -int ObTransformSimplifyExpr::convert_join_preds_vector_to_scalar(TableItem *table_item, bool &trans_happened) +int ObTransformSimplifyExpr::recursively_convert_join_preds_vector_to_scalar(TableItem *table_item, + bool &trans_happened) { int ret = OB_SUCCESS; - if (OB_ISNULL(table_item)) { + JoinedTable *joined_table = NULL; + if (OB_ISNULL(table_item) || OB_ISNULL(ctx_)) { ret = OB_ERR_UNEXPECTED; LOG_WARN("NULL table item", K(ret)); - } else if (table_item->is_joined_table()) { - JoinedTable *joined_table = reinterpret_cast(table_item); + } else if (!table_item->is_joined_table()) { + } else if (FALSE_IT(joined_table = reinterpret_cast(table_item))){ + } else if (OB_FAIL(recursively_convert_join_preds_vector_to_scalar(joined_table->left_table_, + trans_happened))) { + LOG_WARN("failed to convert join preds_vector to scalar", K(ret)); + } else if (OB_FAIL(recursively_convert_join_preds_vector_to_scalar(joined_table->right_table_, + trans_happened))) { + LOG_WARN("failed to convert join preds_vector to scalar", K(ret)); + } else { ObSEArray new_join_cond; bool is_happened = false; for (int64_t i = 0; OB_SUCC(ret) && i < joined_table->get_join_conditions().count(); i++) { - if (OB_FAIL(inner_convert_preds_vector_to_scalar( - joined_table->get_join_conditions().at(i), new_join_cond, is_happened))) { + if (OB_FAIL(ObTransformUtils::convert_preds_vector_to_scalar(*ctx_, + joined_table->get_join_conditions().at(i), + new_join_cond, + is_happened))) { LOG_WARN("failed to convert predicate vector to scalar", K(ret)); } } @@ -651,85 +662,6 @@ int ObTransformSimplifyExpr::convert_join_preds_vector_to_scalar(TableItem *tabl return ret; } -int ObTransformSimplifyExpr::inner_convert_preds_vector_to_scalar(ObRawExpr *expr, - ObIArray &exprs, - bool &trans_happened) -{ - int ret = OB_SUCCESS; - bool need_push = true; - ObRawExprFactory *factory = NULL; - ObSQLSessionInfo *session = NULL; - bool is_stack_overflow = false; - if (OB_ISNULL(expr) || OB_ISNULL(ctx_) || OB_ISNULL(factory = ctx_->expr_factory_) - || OB_ISNULL(session = ctx_->session_info_)) { - ret = OB_ERR_UNEXPECTED; - LOG_WARN("NULL param", K(ret), K(expr), K_(ctx), - K(factory), K(session)); - } else if (OB_FAIL(check_stack_overflow(is_stack_overflow))) { - LOG_WARN("failed to check stack overflow", K(ret)); - } else if (is_stack_overflow) { - ret = OB_SIZE_OVERFLOW; - LOG_WARN("too deep recursive", K(ret), K(is_stack_overflow)); - } else if ((expr->get_expr_type() == T_OP_EQ) || (expr->get_expr_type() == T_OP_NSEQ)) { - // 条件1: 是等号 - ObOpRawExpr *op_expr = reinterpret_cast(expr); - ObRawExpr *param_expr1 = expr->get_param_expr(0); - ObRawExpr *param_expr2 = expr->get_param_expr(1); - - if (OB_UNLIKELY(2 != op_expr->get_param_count()) - || OB_ISNULL(param_expr1) || OB_ISNULL(param_expr2)) { - ret = OB_ERR_UNEXPECTED; - LOG_WARN("param expr is wrong", K(op_expr->get_param_count()), - K(param_expr1), K(param_expr2)); - } else if (T_OP_ROW == param_expr1->get_expr_type() - && T_OP_ROW == param_expr2->get_expr_type()) { - // 条件2: 两边都是 ROW - need_push = false; - trans_happened = true; - if (OB_UNLIKELY(!is_oracle_mode() && param_expr1->get_param_count() != param_expr2->get_param_count())) { - ret = OB_ERR_UNEXPECTED; - LOG_WARN("param number not equal", K(ret), K(param_expr1->get_param_count()), - K(param_expr2->get_param_count())); - - } else if (OB_UNLIKELY(is_oracle_mode() - && 1 > param_expr2->get_param_count() - && param_expr1->get_param_count() != param_expr2->get_param_expr(0)->get_param_count() - && param_expr1->get_param_count() != param_expr2->get_param_count())) { - ret = OB_ERR_INVALID_COLUMN_NUM; - LOG_WARN("invalid relational operator on oracle mode", K(ret), - K(param_expr2->get_param_count())); - } else { - if (is_oracle_mode() && T_OP_ROW == param_expr2->get_param_expr(0)->get_expr_type()) { - param_expr2 = param_expr2->get_param_expr(0); - } - for (int64_t i = 0; OB_SUCC(ret) && i < param_expr1->get_param_count(); i++) { - ObOpRawExpr *new_op_expr = NULL; - if (OB_FAIL(factory->create_raw_expr(expr->get_expr_type(), new_op_expr))) { - LOG_WARN("failed to create raw expr", K(ret)); - } else if (OB_ISNULL(new_op_expr)) { - ret = OB_ERR_UNEXPECTED; - LOG_WARN("NULL new op expr", K(ret)); - } else if (OB_FAIL(new_op_expr->set_param_exprs( - param_expr1->get_param_expr(i), param_expr2->get_param_expr(i)))) { - LOG_WARN("failed to set param expr", K(ret)); - } else if (OB_FAIL(new_op_expr->formalize(session))) { - LOG_WARN("failed to formalize expr", K(ret)); - } else if (OB_FAIL(SMART_CALL(inner_convert_preds_vector_to_scalar( - reinterpret_cast(new_op_expr), exprs, trans_happened)))) { - /// 对拆分后 expr 继续递归 - LOG_WARN("failed to call inner convert recursive", K(ret)); - } - } - } - } - } - if (OB_SUCC(ret) && need_push && OB_FAIL(exprs.push_back(expr))) { - /// 不满足上述两个条件, 将该 expr 添加至输出. - LOG_WARN("failed to push back expr", K(ret)); - } - return ret; -} - /* check if like condition need to be replaced and create new equal exprs */ int ObTransformSimplifyExpr::check_like_condition(ObRawExpr *&expr, ObIArray &old_exprs, diff --git a/src/sql/rewrite/ob_transform_simplify_expr.h b/src/sql/rewrite/ob_transform_simplify_expr.h index 664bd6bf25..08155d2ae3 100644 --- a/src/sql/rewrite/ob_transform_simplify_expr.h +++ b/src/sql/rewrite/ob_transform_simplify_expr.h @@ -53,8 +53,7 @@ private: const ParamStore ¶m_store, ObIArray &null_expr_lists); int convert_preds_vector_to_scalar(ObDMLStmt *stmt, bool &trans_happened); - int convert_join_preds_vector_to_scalar(TableItem *table_item, bool &trans_happened); - int inner_convert_preds_vector_to_scalar(ObRawExpr *expr, ObIArray &exprs, bool &trans_happened); + int recursively_convert_join_preds_vector_to_scalar(TableItem *table_item, bool &trans_happened); int remove_dummy_exprs(ObDMLStmt *stmt, bool &trans_happened); int remove_dummy_filter_exprs(common::ObIArray &exprs, ObIArray &constraints); diff --git a/src/sql/rewrite/ob_transform_utils.cpp b/src/sql/rewrite/ob_transform_utils.cpp index a6cf80cd3d..a063cbc493 100644 --- a/src/sql/rewrite/ob_transform_utils.cpp +++ b/src/sql/rewrite/ob_transform_utils.cpp @@ -13266,5 +13266,86 @@ int ObTransformUtils::check_is_index_part_key(ObTransformerCtx &ctx, return ret; } +int ObTransformUtils::convert_preds_vector_to_scalar(ObTransformerCtx &ctx, + ObRawExpr *expr, + ObIArray &exprs, + bool &trans_happened) +{ + int ret = OB_SUCCESS; + bool need_push = true; + ObRawExprFactory *factory = NULL; + ObSQLSessionInfo *session = NULL; + bool is_stack_overflow = false; + if (OB_ISNULL(expr) || OB_ISNULL(factory = ctx.expr_factory_) || + OB_ISNULL(session = ctx.session_info_)) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("NULL param", K(ret), KP(expr), KP(factory), KP(session)); + } else if (OB_FAIL(check_stack_overflow(is_stack_overflow))) { + LOG_WARN("failed to check stack overflow", K(ret)); + } else if (is_stack_overflow) { + ret = OB_SIZE_OVERFLOW; + LOG_WARN("too deep recursive", K(ret), K(is_stack_overflow)); + } else if (expr->get_expr_type() == T_OP_EQ || expr->get_expr_type() == T_OP_NSEQ) { + // 条件1: 是等号 + ObOpRawExpr *op_expr = reinterpret_cast(expr); + ObRawExpr *param_expr1 = expr->get_param_expr(0); + ObRawExpr *param_expr2 = expr->get_param_expr(1); + if (OB_UNLIKELY(2 != op_expr->get_param_count()) || + OB_ISNULL(param_expr1) || OB_ISNULL(param_expr2)) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("param expr is wrong", K(op_expr->get_param_count()), + K(param_expr1), K(param_expr2)); + } else if (T_OP_ROW == param_expr1->get_expr_type() && + T_OP_ROW == param_expr2->get_expr_type()) { + // 条件2: 两边都是 ROW + need_push = false; + trans_happened = true; + if (OB_UNLIKELY(!is_oracle_mode() && param_expr1->get_param_count() != param_expr2->get_param_count())) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("param number not equal", K(ret), K(param_expr1->get_param_count()), + K(param_expr2->get_param_count())); + } else if (OB_UNLIKELY(is_oracle_mode() + && 1 > param_expr2->get_param_count() + && param_expr1->get_param_count() != param_expr2->get_param_expr(0)->get_param_count() + && param_expr1->get_param_count() != param_expr2->get_param_count())) { + ret = OB_ERR_INVALID_COLUMN_NUM; + LOG_WARN("invalid relational operator on oracle mode", K(ret), + K(param_expr2->get_param_count())); + } else { + if (is_oracle_mode() && T_OP_ROW == param_expr2->get_param_expr(0)->get_expr_type()) { + param_expr2 = param_expr2->get_param_expr(0); + } + for (int64_t i = 0; OB_SUCC(ret) && i < param_expr1->get_param_count(); i++) { + ObOpRawExpr *new_op_expr = NULL; + if (OB_FAIL(factory->create_raw_expr(expr->get_expr_type(), new_op_expr))) { + LOG_WARN("failed to create raw expr", K(ret)); + } else if (OB_ISNULL(new_op_expr)) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("NULL new op expr", K(ret)); + } else if (OB_FAIL(new_op_expr->set_param_exprs( + param_expr1->get_param_expr(i), param_expr2->get_param_expr(i)))) { + LOG_WARN("failed to set param expr", K(ret)); + } else if (OB_FAIL(new_op_expr->formalize(session))) { + LOG_WARN("failed to formalize expr", K(ret)); + } else if (OB_FAIL(new_op_expr->pull_relation_id())) { + LOG_WARN("failed to pull relation id and levels", K(ret)); + } else if (OB_FAIL(SMART_CALL(convert_preds_vector_to_scalar(ctx, + reinterpret_cast(new_op_expr), + exprs, + trans_happened)))) { + /// 对拆分后 expr 继续递归 + LOG_WARN("failed to call inner convert recursive", K(ret)); + } + } + } + } + } + if (OB_SUCC(ret) && need_push && OB_FAIL(exprs.push_back(expr))) { + /// 不满足上述两个条件, 将该 expr 添加至输出. + LOG_WARN("failed to push back expr", K(ret)); + } + return ret; +} + } // namespace sql } // namespace oceanbase diff --git a/src/sql/rewrite/ob_transform_utils.h b/src/sql/rewrite/ob_transform_utils.h index 600cb7dc49..e9ed48f2c2 100644 --- a/src/sql/rewrite/ob_transform_utils.h +++ b/src/sql/rewrite/ob_transform_utils.h @@ -1773,6 +1773,10 @@ public: ObIArray &relation_exprs, ObIArray &common_exprs); static int check_is_index_part_key(ObTransformerCtx &ctx, ObDMLStmt &stmt, ObRawExpr *check_expr, bool &is_valid); + static int convert_preds_vector_to_scalar(ObTransformerCtx &ctx, + ObRawExpr *expr, + ObIArray &exprs, + bool &trans_happened); private: static int inner_get_lazy_left_join(ObDMLStmt *stmt, TableItem *table,