From f6c64ffeb21b1f23bea1c435826b74180ffaeb75 Mon Sep 17 00:00:00 2001 From: xianyu-w <707512433@qq.com> Date: Tue, 4 Apr 2023 00:44:54 +0000 Subject: [PATCH] Fix print window function bug --- src/sql/resolver/expr/ob_raw_expr.cpp | 2 - src/sql/resolver/expr/ob_raw_expr.h | 7 -- src/sql/resolver/expr/ob_raw_expr_printer.cpp | 86 +++---------------- 3 files changed, 14 insertions(+), 81 deletions(-) diff --git a/src/sql/resolver/expr/ob_raw_expr.cpp b/src/sql/resolver/expr/ob_raw_expr.cpp index dc054336e..c9e49b49b 100644 --- a/src/sql/resolver/expr/ob_raw_expr.cpp +++ b/src/sql/resolver/expr/ob_raw_expr.cpp @@ -5029,7 +5029,6 @@ int ObWindow::assign(const ObWindow &other) void ObWinFunRawExpr::clear_child() { func_type_ = T_MAX; - is_distinct_ = false; func_params_.reset(); partition_exprs_.reset(); order_items_.reset(); @@ -5058,7 +5057,6 @@ int ObWinFunRawExpr::assign(const ObRawExpr &other) LOG_WARN("failed to assign lower bound", K(ret)); } else { func_type_ = tmp.func_type_; - is_distinct_ = tmp.is_distinct_; is_ignore_null_ = tmp.is_ignore_null_; is_from_first_ = tmp.is_from_first_; agg_expr_ = tmp.agg_expr_; diff --git a/src/sql/resolver/expr/ob_raw_expr.h b/src/sql/resolver/expr/ob_raw_expr.h index e8c6ca9d4..cd88aa0a8 100644 --- a/src/sql/resolver/expr/ob_raw_expr.h +++ b/src/sql/resolver/expr/ob_raw_expr.h @@ -4283,7 +4283,6 @@ public: : ObRawExpr(), ObWindow(), func_type_(T_MAX), - is_distinct_(false), is_ignore_null_(false), is_from_first_(false), agg_expr_(NULL), @@ -4295,7 +4294,6 @@ public: : ObRawExpr(alloc), ObWindow(), func_type_(T_MAX), - is_distinct_(false), is_ignore_null_(false), is_from_first_(false), agg_expr_(NULL), @@ -4312,8 +4310,6 @@ public: const common::ObIArray &new_exprs); inline void set_func_type(ObItemType func_type) { func_type_ = func_type; } - inline void set_is_distinct(bool is_distinct) - { is_distinct_ = is_distinct; } inline void set_is_ignore_null(bool is_ignore_null) { is_ignore_null_ = is_ignore_null; } inline void set_is_from_first(bool is_from_first) @@ -4323,7 +4319,6 @@ public: inline void set_agg_expr(ObAggFunRawExpr *agg_expr) { agg_expr_ = agg_expr; } inline ObItemType get_func_type() const { return func_type_; } - inline bool is_distinct() { return is_distinct_; } inline bool is_ignore_null() { return is_ignore_null_; } inline bool is_from_first() { return is_from_first_; } inline const common::ObIArray &get_func_params() const { return func_params_; } @@ -4369,7 +4364,6 @@ public: N_EXPR_INFO, info_, N_REL_ID, rel_ids_, K_(func_type), - K_(is_distinct), K_(func_params), K_(partition_exprs), K_(order_items), @@ -4384,7 +4378,6 @@ public: private: DISALLOW_COPY_AND_ASSIGN(ObWinFunRawExpr); ObItemType func_type_; - bool is_distinct_; bool is_ignore_null_; bool is_from_first_; common::ObArray func_params_; diff --git a/src/sql/resolver/expr/ob_raw_expr_printer.cpp b/src/sql/resolver/expr/ob_raw_expr_printer.cpp index fab268be7..84a89cdad 100644 --- a/src/sql/resolver/expr/ob_raw_expr_printer.cpp +++ b/src/sql/resolver/expr/ob_raw_expr_printer.cpp @@ -3266,13 +3266,17 @@ int ObRawExprPrinter::print(ObWinFunRawExpr *expr) case T_FUN_JSON_OBJECTAGG: SET_SYMBOL_IF_EMPTY("json_objectagg"); case T_FUN_PL_AGG_UDF: { - if (type == T_FUN_PL_AGG_UDF) { - if (OB_ISNULL(expr->get_pl_agg_udf_expr()) || - OB_UNLIKELY(!expr->get_pl_agg_udf_expr()->is_udf_expr())) { + if (OB_ISNULL(expr->get_agg_expr())) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("get unexpected null", K(ret), KPC(expr)); + } else if (type == T_FUN_PL_AGG_UDF) { + ObRawExpr *udf_expr = expr->get_agg_expr()->get_pl_agg_udf_expr(); + if (OB_ISNULL(udf_expr) || + OB_UNLIKELY(!udf_expr->is_udf_expr())) { ret = OB_ERR_UNEXPECTED; - LOG_WARN("get unexpected null", K(ret)); + LOG_WARN("get unexpected null", K(ret), KPC(expr)); } else { - symbol = static_cast(expr->get_pl_agg_udf_expr())->get_func_name(); + symbol = static_cast(udf_expr)->get_func_name(); } } if (OB_SUCC(ret)) { @@ -3280,7 +3284,7 @@ int ObRawExprPrinter::print(ObWinFunRawExpr *expr) } // distinct, default 'all', not print if (OB_SUCC(ret)) { - if (expr->is_distinct()) { + if (expr->get_agg_expr()->is_param_distinct()) { DATA_PRINTF("distinct "); } } @@ -3305,7 +3309,8 @@ int ObRawExprPrinter::print(ObWinFunRawExpr *expr) } DATA_PRINTF(")"); DATA_PRINTF(" over("); - if (OB_FAIL(print_partition_exprs(expr))) { + if (OB_FAIL(ret)) { + } else if (OB_FAIL(print_partition_exprs(expr))) { LOG_WARN("failed to print partition exprs.", K(ret)); } else if (OB_FAIL(print_order_items(expr))) { LOG_WARN("failed to print order items.", K(ret)); @@ -3462,72 +3467,9 @@ int ObRawExprPrinter::print(ObWinFunRawExpr *expr) break; } case T_FUN_GROUP_CONCAT: { - // mysql: group_concat(distinct c1,c2+1 order by c1 desc separator ',') - // oracle: listagg(c1,',') within group(order by c1); - if (lib::is_oracle_mode()) { - SET_SYMBOL_IF_EMPTY("listagg"); - } else { - SET_SYMBOL_IF_EMPTY("group_concat"); + if (OB_FAIL(print(expr->get_agg_expr()))) { + LOG_WARN("failed to print agg expr", K(ret)); } - DATA_PRINTF("%.*s(", LEN_AND_PTR(symbol)); - // distinct - if (OB_SUCC(ret)) { - if (expr->is_distinct()) { - DATA_PRINTF("distinct "); - } - } - // expr list - for (int64_t i = 0; OB_SUCC(ret) && i < expr->get_agg_expr()->get_real_param_count(); ++i) { - PRINT_EXPR(expr->get_agg_expr()->get_real_param_exprs().at(i)); - DATA_PRINTF(","); - } - if (OB_SUCC(ret)) { - --*pos_; - } - if (lib::is_oracle_mode() && 0 == expr->get_order_items().count()) {// oracle 模式 listagg 支持无 within group - /* do nothing */ - } else if (lib::is_oracle_mode()) { - DATA_PRINTF(") within group ("); - // order by - if (OB_SUCC(ret)) { - const ObIArray &order_items = expr->get_agg_expr()->get_order_items(); - int64_t order_item_size = order_items.count(); - if (order_item_size > 0) { - DATA_PRINTF(" order by "); - for (int64_t i = 0; OB_SUCC(ret) && i < order_item_size; ++i) { - const OrderItem &order_item = order_items.at(i); - PRINT_EXPR(order_item.expr_); - if (OB_SUCC(ret)) { - if (lib::is_mysql_mode()) { - if (is_descending_direction(order_item.order_type_)) { - DATA_PRINTF(" desc "); - } - } else if (order_item.order_type_ == NULLS_FIRST_ASC) { - DATA_PRINTF(" asc nulls first "); - } else if (order_item.order_type_ == NULLS_LAST_ASC) {//use default value - /*do nothing*/ - } else if (order_item.order_type_ == NULLS_FIRST_DESC) {//use default value - DATA_PRINTF(" desc "); - } else if (order_item.order_type_ == NULLS_LAST_DESC) { - DATA_PRINTF(" desc nulls last "); - } else {/*do nothing*/} - } - DATA_PRINTF(","); - } - if (OB_SUCC(ret)) { - --*pos_; - } - } - } - } - // separator - if (OB_SUCC(ret)) { - if (expr->get_agg_expr()->get_separator_param_expr()) { - DATA_PRINTF(" separator "); - PRINT_EXPR(expr->get_agg_expr()->get_separator_param_expr()); - } - } - DATA_PRINTF(")"); if (OB_SUCC(ret)) { DATA_PRINTF(" over("); if (OB_FAIL(print_partition_exprs(expr))) {