Fix remove_const bug

This commit is contained in:
2149
2023-05-23 10:41:34 +00:00
committed by ob-robot
parent 795f695156
commit 03fdde5756
4 changed files with 42 additions and 34 deletions

View File

@ -181,6 +181,8 @@ public:
int add_replace_exprs(const ObIArray<ObRawExpr *> &from_exprs,
const ObIArray<ObRawExpr *> &to_exprs,
const ObIArray<ObRawExpr *> *skip_exprs = NULL);
void set_skip_bool_param_mysql(bool skip) { replacer_.set_skip_bool_param_mysql(skip); }
bool is_skip_bool_param_mysql() { return replacer_.is_skip_bool_param_mysql(); }
private:
int add_skip_expr(const ObRawExpr *skip_expr);
int check_expr_need_skip(const ObRawExpr *skip_expr, bool &need_skip);

View File

@ -21,7 +21,7 @@ namespace oceanbase
namespace sql
{
ObRawExprReplacer::ObRawExprReplacer()
: replace_happened_(false)
: replace_happened_(false), skip_bool_param_mysql_(false)
{}
ObRawExprReplacer::~ObRawExprReplacer()
@ -117,6 +117,15 @@ int ObRawExprReplacer::visit(ObOpRawExpr &expr)
for (int64_t i = 0; OB_SUCC(ret) && i < count; ++i) {
if (OB_FAIL(check_need_replace(expr.get_param_expr(i), new_expr, need_replace))) {
LOG_WARN("failed to check need replace", K(ret));
} else if (is_skip_bool_param_mysql()) {
if (T_OP_IS == expr.get_expr_type() && i == 1) {
//do nothing
} else if (T_OP_IS_NOT == expr.get_expr_type() && i == 1) {
//do nothing
} else if (need_replace) {
ret = expr.replace_param_expr(i, new_expr);
replace_happened_ = true;
}
} else if (need_replace) {
ret = expr.replace_param_expr(i, new_expr);
replace_happened_ = true;

View File

@ -54,6 +54,8 @@ public:
virtual bool skip_child(ObRawExpr &expr) override;
bool get_replace_happened() const { return replace_happened_; }
void set_skip_bool_param_mysql(bool skip) { skip_bool_param_mysql_ = skip; }
bool is_skip_bool_param_mysql() { return skip_bool_param_mysql_; }
int add_replace_expr(ObRawExpr *from_expr,
ObRawExpr *to_expr);
int add_replace_exprs(const ObIArray<ObRawExpr *> &from_exprs,
@ -78,6 +80,9 @@ private:
hash::ObHashMap<uint64_t, uint64_t> expr_replace_map_;
bool replace_happened_;
//If true, skip param epxrs which are resolved as flags instead of exprs in mysql.
//(e.g. the second param of IS expr)
bool skip_bool_param_mysql_;
};
} // end namespace sql

View File

@ -8524,41 +8524,33 @@ int ObTransformPreProcess::replace_remove_const_exprs(ObSelectStmt *stmt,
LOG_WARN("failed to init compare context", K(ret));
} else {
if (is_mysql_mode()) {
if (OB_FAIL(ObTransformUtils::replace_exprs(static_const_exprs, static_remove_const_exprs, stmt->get_having_exprs()))) {
LOG_WARN("failed to replace exec_params in having expr", K(ret));
} else if (OB_FAIL(ObTransformUtils::replace_exprs(column_ref_exprs, column_ref_remove_const_exprs, stmt->get_having_exprs()))) {
LOG_WARN("failed to replace exec_params in having expr", K(ret));
ObStmtExprReplacer replacer;
replacer.remove_all();
replacer.add_scope(SCOPE_HAVING);
replacer.add_scope(SCOPE_SELECT);
replacer.add_scope(SCOPE_ORDERBY);
replacer.set_recursive(false);
replacer.set_skip_bool_param_mysql(true);
if (OB_FAIL(replacer.add_replace_exprs(static_const_exprs, static_remove_const_exprs))) {
LOG_WARN("failed to add replace exprs", K(ret));
} else if (OB_FAIL(replacer.add_replace_exprs(column_ref_exprs, column_ref_remove_const_exprs))) {
LOG_WARN("failed to add replace exprs", K(ret));
} else if (OB_FAIL(stmt->iterate_stmt_expr(replacer))) {
LOG_WARN("failed to iterate stmt expr", K(ret));
}
} else if (is_oracle_mode() && OB_FAIL(ObTransformUtils::replace_exprs(exec_params, exec_params_remove_const_exprs, stmt->get_having_exprs()))) {
LOG_WARN("failed to replace exec_params in having expr", K(ret));
}
}
for (int i = 0; OB_SUCC(ret) && i < stmt->get_select_item_size(); ++i) {
if (OB_ISNULL(stmt->get_select_item(i).expr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null", K(ret));
} else if (is_mysql_mode()) {
if (OB_FAIL(ObTransformUtils::replace_expr(static_const_exprs, static_remove_const_exprs, stmt->get_select_item(i).expr_))) {
LOG_WARN("failed to replace exec_params in having expr", K(ret));
} else if (OB_FAIL(ObTransformUtils::replace_expr(column_ref_exprs, column_ref_remove_const_exprs, stmt->get_select_item(i).expr_))) {
LOG_WARN("failed to replace exec_params in having expr", K(ret));
} else {
ObStmtExprReplacer replacer;
replacer.remove_all();
replacer.add_scope(SCOPE_HAVING);
replacer.add_scope(SCOPE_SELECT);
replacer.add_scope(SCOPE_ORDERBY);
replacer.set_recursive(false);
replacer.set_skip_bool_param_mysql(false);
if (OB_FAIL(replacer.add_replace_exprs(exec_params, exec_params_remove_const_exprs))) {
LOG_WARN("failed to add replace exprs", K(ret));
} else if (OB_FAIL(stmt->iterate_stmt_expr(replacer))) {
LOG_WARN("failed to iterate stmt expr", K(ret));
}
} else if (OB_FAIL(ObTransformUtils::replace_expr(exec_params, exec_params_remove_const_exprs, stmt->get_select_item(i).expr_))) {
LOG_WARN("failed to replace exec_params in having expr", K(ret));
}
}
for (int i = 0; OB_SUCC(ret) && i < stmt->get_order_item_size(); ++i) {
if (OB_ISNULL(stmt->get_order_item(i).expr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null", K(ret));
} else if (is_mysql_mode()) {
if (OB_FAIL(ObTransformUtils::replace_expr(static_const_exprs, static_remove_const_exprs, stmt->get_order_item(i).expr_))) {
LOG_WARN("failed to replace exec_params in having expr", K(ret));
} else if (OB_FAIL(ObTransformUtils::replace_expr(column_ref_exprs, column_ref_remove_const_exprs, stmt->get_order_item(i).expr_))) {
LOG_WARN("failed to replace exec_params in having expr", K(ret));
}
} else if (OB_FAIL(ObTransformUtils::replace_expr(exec_params, exec_params_remove_const_exprs, stmt->get_order_item(i).expr_))) {
LOG_WARN("failed to replace exec_params in having expr", K(ret));
}
}