patch 4.0

This commit is contained in:
wangzelin.wzl
2022-10-24 10:34:53 +08:00
parent 4ad6e00ec3
commit 93a1074b0c
10533 changed files with 2588271 additions and 2299373 deletions

View File

@ -10,7 +10,7 @@
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL_ENG
#define USING_LOG_PREFIX SQL_ENG
//#include "common/sql_mode/ob_sql_mode_utils.h"
#include "sql/ob_sql_utils.h"
@ -21,19 +21,22 @@
using namespace oceanbase::common;
namespace oceanbase {
namespace sql {
namespace oceanbase
{
namespace sql
{
ObExprInterval::ObExprInterval(common::ObIAllocator& alloc)
: ObExprOperator(alloc, T_FUN_SYS_INTERVAL, "interval", MORE_THAN_TWO, NOT_ROW_DIMENSION), use_binary_search_(true)
ObExprInterval::ObExprInterval(common::ObIAllocator &alloc) :
ObExprOperator(alloc, T_FUN_SYS_INTERVAL, "interval", MORE_THAN_TWO, NOT_ROW_DIMENSION),
use_binary_search_(true)
{}
OB_SERIALIZE_MEMBER((ObExprInterval, ObExprOperator), use_binary_search_);
int ObExprInterval::assign(const ObExprOperator& other)
int ObExprInterval::assign(const ObExprOperator &other)
{
int ret = OB_SUCCESS;
const ObExprInterval* tmp_other = static_cast<const ObExprInterval*>(&other);
const ObExprInterval *tmp_other = static_cast<const ObExprInterval *>(&other);
if (other.get_type() != T_FUN_SYS_INTERVAL) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument. wrong type for other", K(ret), K(other));
@ -47,8 +50,10 @@ int ObExprInterval::assign(const ObExprOperator& other)
return ret;
}
int ObExprInterval::calc_result_typeN(
ObExprResType& type, ObExprResType* types, int64_t param_num, ObExprTypeCtx& type_ctx) const
int ObExprInterval::calc_result_typeN(ObExprResType &type,
ObExprResType *types,
int64_t param_num,
ObExprTypeCtx &type_ctx) const
{
UNUSED(type_ctx);
int ret = OB_SUCCESS;
@ -63,7 +68,7 @@ int ObExprInterval::calc_result_typeN(
type.set_int();
type.set_precision(ObAccuracy::DDL_DEFAULT_ACCURACY[ObIntType].precision_);
type.set_scale(ObAccuracy::DDL_DEFAULT_ACCURACY[ObIntType].scale_);
// set calc type
//set calc type
common::ObObjType calc_type = types[0].get_type();
if (calc_type != ObNumberType && calc_type != ObUNumberType)
@ -79,68 +84,13 @@ int ObExprInterval::calc_result_typeN(
return ret;
}
int ObExprInterval::calc_resultN(ObObj& result, const ObObj* obj_array, int64_t param_num, ObExprCtx& expr_ctx) const
{
UNUSED(expr_ctx);
int ret = OB_SUCCESS;
if (param_num < 2) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("Incorrect parameter number", K(param_num), K(ret));
} else if (OB_ISNULL(obj_array)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("obj_array is NULL", K(ret));
} else if (obj_array[0].is_null()) {
result.set_int(-1);
} else {
const bool is_num = (ObNumberType == obj_array[0].get_type() || ObUNumberType == obj_array[0].get_type());
LOG_DEBUG("ObExprInterval", K(is_num), K(use_binary_search_), K(obj_array[0].get_collation_type()));
if (!use_binary_search_) {
int64_t i = 1;
for (; i < param_num; ++i) {
if (obj_array[i].is_null()) {
continue;
} else if ((is_num ? ObObjCmpFuncs::compare_oper_nullsafe(
obj_array[i], obj_array[0], obj_array[i].get_collation_type(), CO_GT)
: (obj_array[i].get_double() > obj_array[0].get_double()))) {
break;
}
}
result.set_int(i - 1);
;
} else {
int64_t high = param_num - 1;
int64_t low = 1;
int64_t mid = 0;
while (low <= high) {
mid = (low + high + 1) / 2;
LOG_DEBUG("obj_array[mid]", K(obj_array[mid].is_null()), K(obj_array[mid].get_double()), K(obj_array[mid]));
if (is_num ? ObObjCmpFuncs::compare_oper_nullsafe(
obj_array[mid], obj_array[0], obj_array[mid].get_collation_type(), CO_GT)
: (obj_array[mid].get_double() > obj_array[0].get_double())) {
high = mid - 1;
mid -= 1;
} else if (low == high) {
break;
} else {
low = mid;
}
} // while
result.set_int(mid);
} // else
}
return ret;
}
int ObExprInterval::calc_interval_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res)
int ObExprInterval::calc_interval_expr(const ObExpr &expr, ObEvalCtx &ctx,
ObDatum &res)
{
int ret = OB_SUCCESS;
ObDatum* arg0 = NULL;
if (OB_UNLIKELY(2 > expr.arg_cnt_ || 1 != expr.inner_func_cnt_) || OB_ISNULL(expr.inner_functions_) ||
OB_ISNULL(expr.inner_functions_[0])) {
ObDatum *arg0 = NULL;
if (OB_UNLIKELY(2 > expr.arg_cnt_ || 1 != expr.inner_func_cnt_) ||
OB_ISNULL(expr.inner_functions_) || OB_ISNULL(expr.inner_functions_[0])) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid arg cnt", K(ret), K(expr.arg_cnt_));
} else if (OB_FAIL(expr.eval_param_value(ctx, arg0))) {
@ -148,12 +98,13 @@ int ObExprInterval::calc_interval_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDat
} else if (arg0->is_null()) {
res.set_int(-1);
} else {
ObDatumCmpFuncType cmp_func = reinterpret_cast<ObDatumCmpFuncType>(expr.inner_functions_[0]);
ObDatumCmpFuncType cmp_func = reinterpret_cast<ObDatumCmpFuncType>(
expr.inner_functions_[0]);
bool use_binary_search = static_cast<bool>(expr.extra_);
if (!use_binary_search) {
int64_t i = 1;
for (; i < expr.arg_cnt_; ++i) {
const ObDatum& arg_i = expr.locate_param_datum(ctx, static_cast<int>(i));
const ObDatum &arg_i = expr.locate_param_datum(ctx, static_cast<int>(i));
if (arg_i.is_null()) {
continue;
} else if (cmp_func(arg_i, *arg0) > 0) {
@ -161,14 +112,13 @@ int ObExprInterval::calc_interval_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDat
break;
}
}
res.set_int(i - 1);
;
res.set_int(i - 1);;
} else {
int64_t high = expr.arg_cnt_ - 1;
int64_t low = 1;
int64_t mid = 0;
while (low <= high) {
const ObDatum& arg_i = expr.locate_param_datum(ctx, static_cast<int>(mid));
const ObDatum &arg_i = expr.locate_param_datum(ctx, static_cast<int>(mid));
mid = (low + high + 1) / 2;
if (cmp_func(arg_i, *arg0) > 0) {
high = mid - 1;
@ -178,29 +128,32 @@ int ObExprInterval::calc_interval_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDat
} else {
low = mid;
}
} // while
} // while
res.set_int(mid);
}
}
return ret;
}
int ObExprInterval::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprInterval::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
int ret = OB_SUCCESS;
UNUSED(raw_expr);
if (OB_UNLIKELY(2 > rt_expr.arg_cnt_) || OB_ISNULL(expr_cg_ctx.allocator_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected arg cnt or allocator is NULL", K(ret), K(rt_expr.arg_cnt_), KP(expr_cg_ctx.allocator_));
} else if (OB_ISNULL(
rt_expr.inner_functions_ = reinterpret_cast<void**>(expr_cg_ctx.allocator_->alloc(sizeof(void*))))) {
LOG_WARN("unexpected arg cnt or allocator is NULL", K(ret), K(rt_expr.arg_cnt_),
KP(expr_cg_ctx.allocator_));
} else if (OB_ISNULL(rt_expr.inner_functions_ =
reinterpret_cast<void**>(expr_cg_ctx.allocator_->alloc(sizeof(void*))))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc mem for inner func failed", K(ret));
} else {
// make sure all arg type is same
for (int64_t i = 0; OB_SUCC(ret) && i < rt_expr.arg_cnt_; ++i) {
const ObObjType& arg_type = rt_expr.args_[i]->datum_meta_.type_;
if (!(ObNumberType == arg_type || ObUNumberType == arg_type || ObDoubleType == arg_type)) {
const ObObjType &arg_type = rt_expr.args_[i]->datum_meta_.type_;
if (!(ObNumberType == arg_type || ObUNumberType == arg_type ||
ObDoubleType == arg_type)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("all type must be number type or double type", K(ret), K(arg_type), K(i));
}
@ -214,15 +167,15 @@ int ObExprInterval::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr,
// otherwise, use sequential search
static const int64_t MYSQL_BINARY_SEARCH_BOUND = 8;
bool use_binary_search = true;
const ObSysFunRawExpr* sys_fun_expr = dynamic_cast<const ObSysFunRawExpr*>(&raw_expr);
const ObSysFunRawExpr *sys_fun_expr = dynamic_cast<const ObSysFunRawExpr*>(&raw_expr);
CK(OB_NOT_NULL(sys_fun_expr));
if (OB_SUCC(ret) && rt_expr.arg_cnt_ > MYSQL_BINARY_SEARCH_BOUND) {
for (int64_t i = 0; OB_SUCC(ret) && i < rt_expr.arg_cnt_; ++i) {
if (OB_ISNULL(sys_fun_expr->get_param_expr(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("param expr is NULL", K(ret), K(i), K(*sys_fun_expr));
} else if (!sys_fun_expr->get_param_expr(i)->has_const_or_const_expr_flag() ||
!sys_fun_expr->get_param_expr(i)->is_not_null()) {
} else if (!sys_fun_expr->get_param_expr(i)->is_const_expr() ||
!sys_fun_expr->get_param_expr(i)->is_not_null_for_read()) {
use_binary_search = false;
break;
}
@ -232,10 +185,12 @@ int ObExprInterval::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr,
}
if (OB_SUCC(ret)) {
const ObObjType& arg_type = rt_expr.args_[0]->datum_meta_.type_;
const ObObjType &arg_type = rt_expr.args_[0]->datum_meta_.type_;
// 数字比较不用考虑collation,
// do not care NULL_FIRST or NULL_LAST, will ignore null in calc_interval_expr()
rt_expr.inner_functions_[0] = reinterpret_cast<void*>(
ObDatumFuncs::get_nullsafe_cmp_func(arg_type, arg_type, default_null_pos(), CS_TYPE_BINARY, false));
ObDatumFuncs::get_nullsafe_cmp_func(arg_type, arg_type, default_null_pos(),
CS_TYPE_BINARY, false));
if (OB_ISNULL(rt_expr.inner_functions_[0])) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("cmp_func is NULL", K(ret), K(arg_type));
@ -249,5 +204,6 @@ int ObExprInterval::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr,
return ret;
}
} // namespace sql
} // namespace oceanbase
} // namespace sql
} // namespace oceanbase