fix some replace expr bugs in optimizer
This commit is contained in:
@ -27,6 +27,16 @@ ObRawExprReplacer::ObRawExprReplacer()
|
||||
ObRawExprReplacer::~ObRawExprReplacer()
|
||||
{}
|
||||
|
||||
void ObRawExprReplacer::destroy()
|
||||
{
|
||||
if (to_exprs_.created()) {
|
||||
to_exprs_.destroy();
|
||||
}
|
||||
if (expr_replace_map_.created()) {
|
||||
expr_replace_map_.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
int ObRawExprReplacer::replace(ObRawExpr *&expr)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
@ -310,27 +320,23 @@ bool ObRawExprReplacer::skip_child(ObRawExpr &expr)
|
||||
}
|
||||
|
||||
int ObRawExprReplacer::add_replace_expr(ObRawExpr *from_expr,
|
||||
ObRawExpr *to_expr)
|
||||
ObRawExpr *to_expr,
|
||||
bool overwrite /*false*/)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
bool is_existed = false;
|
||||
if (OB_ISNULL(from_expr) || OB_ISNULL(to_expr)) {
|
||||
ret = OB_ERR_UNEXPECTED;
|
||||
LOG_WARN("unexpected null expr", KP(from_expr), KP(to_expr), K(ret));
|
||||
} else if (OB_UNLIKELY(!expr_replace_map_.created())) {
|
||||
if (OB_FAIL(expr_replace_map_.create(64, ObModIds::OB_SQL_COMPILE))) {
|
||||
LOG_WARN("failed to create expr map", K(ret));
|
||||
} else if (OB_FAIL(to_exprs_.create(64))) {
|
||||
LOG_WARN("failed to create expr set", K(ret));
|
||||
}
|
||||
}
|
||||
if (OB_FAIL(ret)) {
|
||||
} else if (OB_FAIL(check_from_expr_existed(from_expr, to_expr, is_existed))) {
|
||||
} else if (OB_FAIL(try_init_expr_map(DEFAULT_BUCKET_SIZE))) {
|
||||
LOG_WARN("failed to init expr map", K(ret));
|
||||
} else if (OB_FAIL(check_from_expr_existed(from_expr, to_expr, overwrite, is_existed))) {
|
||||
LOG_WARN("failed to check from expr existed", K(ret));
|
||||
} else if (is_existed) {
|
||||
// do not add duplicated replace expr
|
||||
} else if (OB_FAIL(expr_replace_map_.set_refactored(reinterpret_cast<uint64_t>(from_expr),
|
||||
reinterpret_cast<uint64_t>(to_expr)))) {
|
||||
reinterpret_cast<uint64_t>(to_expr),
|
||||
overwrite))) {
|
||||
LOG_WARN("failed to add replace expr into map", K(ret));
|
||||
} else if (OB_FAIL(to_exprs_.set_refactored(reinterpret_cast<uint64_t>(to_expr)))) {
|
||||
LOG_WARN("failed to add replace expr into set", K(ret));
|
||||
@ -345,6 +351,8 @@ int ObRawExprReplacer::add_replace_exprs(const ObIArray<ObRawExpr *> &from_exprs
|
||||
if (OB_UNLIKELY(from_exprs.count() != to_exprs.count())) {
|
||||
ret = OB_ERR_UNEXPECTED;
|
||||
LOG_WARN("expr size mismatch", K(from_exprs.count()), K(to_exprs.count()), K(ret));
|
||||
} else if (OB_FAIL(try_init_expr_map(from_exprs.count()))) {
|
||||
LOG_WARN("failed to init expr map", K(ret));
|
||||
}
|
||||
for (int64_t i = 0; OB_SUCC(ret) && i < from_exprs.count(); ++i) {
|
||||
if (OB_FAIL(add_replace_expr(from_exprs.at(i), to_exprs.at(i)))) {
|
||||
@ -358,6 +366,9 @@ int ObRawExprReplacer::add_replace_exprs(
|
||||
const ObIArray<std::pair<ObRawExpr *, ObRawExpr *>> &to_replace_exprs)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
if (OB_FAIL(try_init_expr_map(to_replace_exprs.count()))) {
|
||||
LOG_WARN("failed to init expr map", K(ret));
|
||||
}
|
||||
for (int64_t i = 0; OB_SUCC(ret) && i < to_replace_exprs.count(); ++i) {
|
||||
if (OB_FAIL(add_replace_expr(to_replace_exprs.at(i).first, to_replace_exprs.at(i).second))) {
|
||||
LOG_WARN("failed to add replace expr", K(ret));
|
||||
@ -366,8 +377,39 @@ int ObRawExprReplacer::add_replace_exprs(
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObRawExprReplacer::append_replace_exprs(const ObRawExprReplacer &other)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
if (OB_FAIL(try_init_expr_map(other.expr_replace_map_.size()))) {
|
||||
LOG_WARN("failed to init expr map", K(ret));
|
||||
}
|
||||
for (auto it = other.expr_replace_map_.begin();
|
||||
OB_SUCC(ret) && it != other.expr_replace_map_.end(); ++it) {
|
||||
if (OB_FAIL(add_replace_expr(reinterpret_cast<ObRawExpr *>(it->first),
|
||||
reinterpret_cast<ObRawExpr *>(it->second)))) {
|
||||
LOG_WARN("failed to push back from to expr", K(ret));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObRawExprReplacer::try_init_expr_map(int64_t bucket_size)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
if (OB_UNLIKELY(!expr_replace_map_.created())) {
|
||||
bucket_size = MAX(bucket_size, DEFAULT_BUCKET_SIZE);
|
||||
if (OB_FAIL(expr_replace_map_.create(bucket_size, ObModIds::OB_SQL_COMPILE))) {
|
||||
LOG_WARN("failed to create expr map", K(ret));
|
||||
} else if (OB_FAIL(to_exprs_.create(bucket_size))) {
|
||||
LOG_WARN("failed to create expr set", K(ret));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObRawExprReplacer::check_from_expr_existed(const ObRawExpr *from_expr,
|
||||
const ObRawExpr *to_expr,
|
||||
const bool overwrite,
|
||||
bool &is_existed)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
@ -377,8 +419,12 @@ int ObRawExprReplacer::check_from_expr_existed(const ObRawExpr *from_expr,
|
||||
} else if (!is_existed) {
|
||||
// do nothing
|
||||
} else if (OB_UNLIKELY(old_expr != to_expr)) {
|
||||
ret = OB_ERR_UNEXPECTED;
|
||||
LOG_WARN("conflict expr replace rules", K(ret), KPC(from_expr), KPC(to_expr), KPC(old_expr));
|
||||
if (overwrite) {
|
||||
is_existed = false;
|
||||
} else {
|
||||
ret = OB_ERR_UNEXPECTED;
|
||||
LOG_WARN("conflict expr replace rules", K(ret), KPC(from_expr), KPC(to_expr), KPC(old_expr));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -35,6 +35,7 @@ public:
|
||||
ObRawExprReplacer();
|
||||
virtual ~ObRawExprReplacer();
|
||||
|
||||
void destroy();
|
||||
int replace(ObRawExpr *&expr);
|
||||
virtual int visit(ObConstRawExpr &expr) override;
|
||||
virtual int visit(ObExecParamRawExpr &expr) override;
|
||||
@ -56,17 +57,23 @@ public:
|
||||
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_; }
|
||||
bool empty() const { return !expr_replace_map_.created(); }
|
||||
int add_replace_expr(ObRawExpr *from_expr,
|
||||
ObRawExpr *to_expr);
|
||||
ObRawExpr *to_expr,
|
||||
bool overwrite = false);
|
||||
int add_replace_exprs(const ObIArray<ObRawExpr *> &from_exprs,
|
||||
const ObIArray<ObRawExpr *> &to_exprs);
|
||||
int add_replace_exprs(const ObIArray<std::pair<ObRawExpr *, ObRawExpr *>> &to_replace_exprs);
|
||||
int append_replace_exprs(const ObRawExprReplacer &other);
|
||||
|
||||
private:
|
||||
// types and constants
|
||||
static const int64_t DEFAULT_BUCKET_SIZE = 64;
|
||||
private:
|
||||
int try_init_expr_map(int64_t bucket_size);
|
||||
int check_from_expr_existed(const ObRawExpr *from_expr,
|
||||
const ObRawExpr *to_expr,
|
||||
const bool overwrite,
|
||||
bool &is_existed);
|
||||
int check_skip_expr(const ObRawExpr &expr, bool &skip_expr);
|
||||
int check_need_replace(const ObRawExpr *old_expr,
|
||||
|
||||
Reference in New Issue
Block a user