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

@ -20,131 +20,77 @@
#include "sql/session/ob_sql_session_info.h"
using namespace oceanbase::common;
using namespace oceanbase::sql;
ObArithFunc ObExprIntDiv::int_div_funcs_[ObMaxTC] = {
NULL,
ObExprIntDiv::intdiv_int, // int
ObExprIntDiv::intdiv_uint, // uint
ObExprIntDiv::intdiv_number, // float
ObExprIntDiv::intdiv_number, // double
ObExprIntDiv::intdiv_number, // number
NULL, // datetime
NULL, // date
NULL, // time
NULL, // year
NULL, // string
NULL, // extend
NULL, // unknown
NULL, // text
NULL, // bit
NULL, // enumset
NULL, // enumsetinner
ObArithFunc ObExprIntDiv::int_div_funcs_[ObMaxTC] =
{
NULL,
ObExprIntDiv::intdiv_int,//int
ObExprIntDiv::intdiv_uint,//uint
ObExprIntDiv::intdiv_number,//float
ObExprIntDiv::intdiv_number,//double
ObExprIntDiv::intdiv_number,//number
NULL,//datetime
NULL,//date
NULL,//time
NULL,//year
NULL,//string
NULL,//extend
NULL,//unknown
NULL,//text
NULL,//bit
NULL,//enumset
NULL,//enumsetinner
};
#define REPORT_OUT_OF_RANGE_ERROR(format, meta, left, right) \
do { \
ret = OB_OPERATE_OVERFLOW; \
pos = 0; \
int tmp_ret = OB_SUCCESS; \
if (OB_UNLIKELY((tmp_ret = databuff_printf( \
expr_str, OB_MAX_TWO_OPERATOR_EXPR_LENGTH, pos, format, left, right)) != OB_SUCCESS)) { \
ret = tmp_ret; \
LOG_WARN("fail to print databuff", K(ret), K(OB_MAX_TWO_OPERATOR_EXPR_LENGTH), K(pos)); \
} else { \
LOG_USER_ERROR(OB_OPERATE_OVERFLOW, meta, expr_str); \
} \
} while (0)
#define REPORT_OUT_OF_RANGE_ERROR(format, meta, left, right) \
do \
{\
ret = OB_OPERATE_OVERFLOW;\
pos = 0;\
int tmp_ret = OB_SUCCESS;\
if (OB_UNLIKELY((tmp_ret = databuff_printf(expr_str, \
OB_MAX_TWO_OPERATOR_EXPR_LENGTH, \
pos, \
format, \
left, \
right)) != OB_SUCCESS)) {\
ret = tmp_ret;\
LOG_WARN("fail to print databuff", K(ret), K(OB_MAX_TWO_OPERATOR_EXPR_LENGTH), K(pos));\
} else {\
LOG_USER_ERROR(OB_OPERATE_OVERFLOW, meta, expr_str);\
}\
}while(0)
ObExprIntDiv::ObExprIntDiv(ObIAllocator& alloc)
: ObArithExprOperator(alloc, T_OP_INT_DIV, N_INT_DIV, 2, NOT_ROW_DIMENSION,
ObExprResultTypeUtil::get_int_div_result_type, ObExprResultTypeUtil::get_int_div_calc_type, int_div_funcs_)
ObExprIntDiv::ObExprIntDiv(ObIAllocator &alloc)
: ObArithExprOperator(alloc,
T_OP_INT_DIV,
N_INT_DIV,
2,
NOT_ROW_DIMENSION,
ObExprResultTypeUtil::get_int_div_result_type,
ObExprResultTypeUtil::get_int_div_calc_type,
int_div_funcs_)
{}
int ObExprIntDiv::calc_result_type2(
ObExprResType& type, ObExprResType& type1, ObExprResType& type2, ObExprTypeCtx& type_ctx) const
int ObExprIntDiv::calc_result_type2(ObExprResType &type, ObExprResType &type1, ObExprResType &type2, ObExprTypeCtx &type_ctx) const
{
int ret = OB_SUCCESS;
if (OB_FAIL(ObArithExprOperator::calc_result_type2(type, type1, type2, type_ctx))) {
LOG_WARN("fail to calc result type", K(ret), K(type), K(type1), K(type2));
} else {
type.set_precision(static_cast<ObPrecision>(MIN(type1.get_precision() - type2.get_precision() + 1, 0)));
type.set_precision(static_cast<ObPrecision>(MIN(type1.get_precision() - type2.get_precision() + 1, 0)));
type.set_scale(0);
}
return ret;
}
int ObExprIntDiv::calc_result2(ObObj& res, const ObObj& left, const ObObj& right, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(ObNullType == left.get_type() || ObNullType == right.get_type())) {
res.set_null();
} else {
ObObjTypeClass tc1 = left.get_type_class();
ObObjTypeClass tc2 = right.get_type_class();
ObScale calc_scale = result_type_.get_calc_scale();
ObObjType calc_type = result_type_.get_calc_type();
if (ObIntTC == tc1) {
if (ObIntTC == tc2 || ObUIntTC == tc2) {
ret = intdiv_int(res, left, right, expr_ctx.calc_buf_, calc_scale);
} else {
ret = ObArithExprOperator::calc_(res, left, right, expr_ctx, calc_scale, calc_type, int_div_funcs_);
}
} else if (ObUIntTC == tc1) {
if (ObIntTC == tc2 || ObUIntTC == tc2) {
ret = intdiv_uint(res, left, right, expr_ctx.calc_buf_, calc_scale);
} else {
ret = ObArithExprOperator::calc_(res, left, right, expr_ctx, calc_scale, calc_type, int_div_funcs_);
}
} else {
ret = ObArithExprOperator::calc_(res, left, right, expr_ctx, calc_scale, calc_type, int_div_funcs_);
}
}
if (OB_SUCC(ret) && result_type_.get_type() != res.get_type() && ObNullType != res.get_type()) {
if (ObNumberTC == res.get_type_class()) {
number::ObNumber num;
if (OB_ISNULL(expr_ctx.calc_buf_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("calc_buf_ is null", K(ret));
} else if (OB_FAIL(num.from(res.get_number(), *expr_ctx.calc_buf_))) {
LOG_WARN("deep copy failed", K(ret), K(res.get_number()));
} else if (OB_FAIL(num.trunc(0))) {
LOG_WARN("fail to trunc number", K(ret), K(num));
} else {
res.set_number(num);
}
}
if (OB_SUCC(ret)) {
const ObObj* res_obj = NULL;
EXPR_DEFINE_CAST_CTX(expr_ctx, CM_NONE);
cast_ctx.cast_mode_ &= ~(CM_WARN_ON_FAIL);
EXPR_CAST_OBJ_V2(result_type_.get_type(), res, res_obj);
if (OB_SUCC(ret)) {
if (OB_UNLIKELY(NULL == res_obj)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("cast obj result is NULL", K(ret));
} else {
res = *res_obj;
}
} else if (OB_DATA_OUT_OF_RANGE == ret) {
ret = OB_OPERATE_OVERFLOW;
LOG_WARN("operate overflow", K(ret), K(left), K(right));
} else {
LOG_WARN("fail to cast obj", K(ret), K(result_type_.get_type()), K(res));
}
}
} else { /* do nothing */
}
return ret;
}
int ObExprIntDiv::calc(ObObj& res, const ObObj& left, const ObObj& right, ObIAllocator* allocator, ObScale scale)
int ObExprIntDiv::calc(ObObj &res, const ObObj &left, const ObObj &right, ObIAllocator *allocator, ObScale scale)
{
int ret = OB_SUCCESS;
ObObjType res_type;
common::ObObjType result_ob1_type;
common::ObObjType result_ob2_type;
ObCalcTypeFunc calc_type_func = ObExprResultTypeUtil::get_int_div_calc_type;
if (OB_FAIL(ObExprResultTypeUtil::get_int_div_result_type(
res_type, result_ob1_type, result_ob2_type, left.get_type(), right.get_type()))) {
if (OB_FAIL(ObExprResultTypeUtil::get_int_div_result_type(res_type, result_ob1_type, result_ob2_type, left.get_type(), right.get_type()))) {
LOG_WARN("fail to get int div result type", K(ret), K(left), K(right));
} else if (OB_FAIL(ObArithExprOperator::calc(res, left, right, allocator, scale, calc_type_func, int_div_funcs_))) {
LOG_WARN("fail to calc int div", K(ret), K(left), K(right));
@ -160,7 +106,7 @@ int ObExprIntDiv::calc(ObObj& res, const ObObj& left, const ObObj& right, ObIAll
}
}
if (OB_SUCC(ret)) {
const ObObj* res_obj = NULL;
const ObObj *res_obj = NULL;
ObCastCtx cast_ctx(allocator, NULL, CM_WARN_ON_FAIL, CS_TYPE_INVALID);
EXPR_CAST_OBJ_V2(res_type, res, res_obj);
if (OB_SUCC(ret)) {
@ -174,12 +120,11 @@ int ObExprIntDiv::calc(ObObj& res, const ObObj& left, const ObObj& right, ObIAll
LOG_WARN("fail to cast obj", K(ret), K(res_type), K(res));
}
}
} else { /* do nothing */
}
} else { /* do nothing */ }
return ret;
}
int ObExprIntDiv::intdiv_int(ObObj& res, const ObObj& left, const ObObj& right, ObIAllocator* allocator, ObScale scale)
int ObExprIntDiv::intdiv_int(ObObj &res, const ObObj &left, const ObObj &right, ObIAllocator *allocator, ObScale scale)
{
UNUSED(allocator);
UNUSED(scale);
@ -197,8 +142,8 @@ int ObExprIntDiv::intdiv_int(ObObj& res, const ObObj& left, const ObObj& right,
if (OB_UNLIKELY(0 == right_i)) {
res.set_null();
} else if (OB_UNLIKELY(INT64_MIN == left_i && (-1) == right_i)) {
// INT64_MIN / -1 is undefined behavior in C language.
// report an error no matter in dml or ddl stmt to be compatible with mysql
//INT64_MIN / -1 is undefined behavior in C language.
//report an error no matter in dml or ddl stmt to be compatible with mysql
REPORT_OUT_OF_RANGE_ERROR("'(%ld div %ld)'", "BIGINT", left_i, right_i);
} else {
int64_t res_i = left_i / right_i;
@ -215,9 +160,9 @@ int ObExprIntDiv::intdiv_int(ObObj& res, const ObObj& left, const ObObj& right,
res.set_null();
} else {
uint64_t res_ui = left_ui / right_ui;
if (OB_UNLIKELY(left_i < 0 && res_ui > 0)) {
if (OB_UNLIKELY(left_i < 0 && res_ui > 0)){
REPORT_OUT_OF_RANGE_ERROR("'(%ld div %lu)'", "BIGINT UNSIGNED", left_i, right_ui);
} else {
} else {
res.set_uint64(res_ui);
}
}
@ -229,7 +174,7 @@ int ObExprIntDiv::intdiv_int(ObObj& res, const ObObj& left, const ObObj& right,
return ret;
}
int ObExprIntDiv::intdiv_uint(ObObj& res, const ObObj& left, const ObObj& right, ObIAllocator* allocator, ObScale scale)
int ObExprIntDiv::intdiv_uint(ObObj &res, const ObObj &left, const ObObj &right, ObIAllocator *allocator, ObScale scale)
{
UNUSED(allocator);
UNUSED(scale);
@ -238,7 +183,7 @@ int ObExprIntDiv::intdiv_uint(ObObj& res, const ObObj& left, const ObObj& right,
uint64_t right_ui = 0;
char expr_str[OB_MAX_TWO_OPERATOR_EXPR_LENGTH];
int64_t pos = 0;
if (OB_UNLIKELY(left.get_type_class() != ObUIntTC)) {
if (OB_UNLIKELY(left.get_type_class() != ObUIntTC)){
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected operator type", "left type", left.get_type_class(), "right type", right.get_type_class());
} else {
@ -250,16 +195,16 @@ int ObExprIntDiv::intdiv_uint(ObObj& res, const ObObj& left, const ObObj& right,
uint64_t res_ui = left_ui / right_ui;
res.set_uint64(res_ui);
}
} else if (ObIntTC == right.get_type_class()) {
} else if (ObIntTC == right.get_type_class()){
int64_t right_i = right.get_int();
right_ui = right_i < 0 ? -right_i : right_i;
if (OB_UNLIKELY(0 == right_ui)) {
res.set_null();
} else {
uint64_t res_ui = left_ui / right_ui;
if (OB_UNLIKELY(right_i < 0 && res_ui > 0)) { // eg: 7 div -6
if (OB_UNLIKELY(right_i < 0 && res_ui > 0)) { // eg: 7 div -6
REPORT_OUT_OF_RANGE_ERROR("'(%lu div %ld)'", "BIGINT UNSIGNED", left_ui, right_i);
} else {
} else {
res.set_uint64(res_ui);
}
}
@ -271,30 +216,29 @@ int ObExprIntDiv::intdiv_uint(ObObj& res, const ObObj& left, const ObObj& right,
return ret;
}
int ObExprIntDiv::intdiv_number(
ObObj& res, const ObObj& left, const ObObj& right, ObIAllocator* allocator, ObScale scale)
int ObExprIntDiv::intdiv_number(ObObj &res, const ObObj &left, const ObObj &right, ObIAllocator *allocator, ObScale scale)
{
int ret = OB_SUCCESS;
int ret=OB_SUCCESS;
if (OB_UNLIKELY(left.get_type_class() != right.get_type_class())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected operator type", "left type", left.get_type_class(), "right type", right.get_type_class());
} else if (OB_FAIL(ObExprDiv::calc(res, left, right, allocator, scale))) {
LOG_WARN("failed to div numbers", K(ret), K(left), K(right));
} else { /* do nothing */
}
} else {/* do nothing */ }
return ret;
}
int ObExprIntDiv::div_int_int(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& datum)
int ObExprIntDiv::div_int_int(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &datum)
{
int ret = OB_SUCCESS;
ObDatum* left = NULL;
ObDatum* right = NULL;
ObDatum *left = NULL;
ObDatum *right = NULL;
bool is_finish = false;
if (OB_FAIL(ObArithExprOperator::get_arith_operand(expr, ctx, left, right, datum, is_finish))) {
LOG_WARN("get_arith_operand failed", K(ret));
} else if (is_finish) {
// do nothing
//do nothing
} else if (right->get_int() == 0) {
datum.set_null();
} else {
@ -303,8 +247,8 @@ int ObExprIntDiv::div_int_int(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& datum
int64_t left_i = left->get_int();
int64_t right_i = right->get_int();
if (OB_UNLIKELY(INT64_MIN == left_i && (-1) == right_i)) {
// INT64_MIN / -1 is undefined behavior in C language.
// report an error no matter in dml or ddl stmt to be compatible with mysql
//INT64_MIN / -1 is undefined behavior in C language.
//report an error no matter in dml or ddl stmt to be compatible with mysql
REPORT_OUT_OF_RANGE_ERROR("'(%ld div %ld)'", "BIGINT", left_i, right_i);
} else {
int64_t res_i = left_i / right_i;
@ -318,16 +262,17 @@ int ObExprIntDiv::div_int_int(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& datum
return ret;
}
int ObExprIntDiv::div_int_uint(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& datum)
int ObExprIntDiv::div_int_uint(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &datum)
{
int ret = OB_SUCCESS;
ObDatum* left = NULL;
ObDatum* right = NULL;
ObDatum *left = NULL;
ObDatum *right = NULL;
bool is_finish = false;
if (OB_FAIL(ObArithExprOperator::get_arith_operand(expr, ctx, left, right, datum, is_finish))) {
LOG_WARN("get_arith_operand failed", K(ret));
} else if (is_finish) {
// do nothing
//do nothing
} else if (right->get_uint() == 0) {
datum.set_null();
} else {
@ -335,27 +280,27 @@ int ObExprIntDiv::div_int_uint(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& datu
uint64_t left_ui = left_i < 0 ? -left_i : left_i;
uint64_t right_ui = right->get_uint();
uint64_t res_ui = left_ui / right_ui;
if (OB_UNLIKELY(left_i < 0 && res_ui > 0)) {
if (OB_UNLIKELY(left_i < 0 && res_ui > 0)){
char expr_str[OB_MAX_TWO_OPERATOR_EXPR_LENGTH];
int64_t pos = 0;
REPORT_OUT_OF_RANGE_ERROR("'(%ld div %lu)'", "BIGINT UNSIGNED", left_i, right_ui);
} else {
} else {
datum.set_uint(res_ui);
}
}
return ret;
}
int ObExprIntDiv::div_uint_uint(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& datum)
int ObExprIntDiv::div_uint_uint(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &datum)
{
int ret = OB_SUCCESS;
ObDatum* left = NULL;
ObDatum* right = NULL;
ObDatum *left = NULL;
ObDatum *right = NULL;
bool is_finish = false;
if (OB_FAIL(ObArithExprOperator::get_arith_operand(expr, ctx, left, right, datum, is_finish))) {
LOG_WARN("get_arith_operand failed", K(ret));
} else if (is_finish) {
// do nothing
//do nothing
} else if (right->get_uint() == 0) {
datum.set_null();
} else {
@ -363,20 +308,21 @@ int ObExprIntDiv::div_uint_uint(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& dat
uint64_t right_ui = right->get_uint();
uint64_t res_ui = left_ui / right_ui;
datum.set_uint(res_ui);
}
return ret;
}
int ObExprIntDiv::div_uint_int(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& datum)
int ObExprIntDiv::div_uint_int(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &datum)
{
int ret = OB_SUCCESS;
ObDatum* left = NULL;
ObDatum* right = NULL;
ObDatum *left = NULL;
ObDatum *right = NULL;
bool is_finish = false;
if (OB_FAIL(ObArithExprOperator::get_arith_operand(expr, ctx, left, right, datum, is_finish))) {
LOG_WARN("get_arith_operand failed", K(ret));
} else if (is_finish) {
// do nothing
//do nothing
} else if (right->get_int() == 0) {
datum.set_null();
} else {
@ -384,27 +330,27 @@ int ObExprIntDiv::div_uint_int(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& datu
int64_t right_i = right->get_int();
uint64_t right_ui = right_i < 0 ? -right_i : right_i;
uint64_t res_ui = left_ui / right_ui;
if (OB_UNLIKELY(right_i < 0 && res_ui > 0)) { // eg: 7 div -6
if (OB_UNLIKELY(right_i < 0 && res_ui > 0)) { // eg: 7 div -6
char expr_str[OB_MAX_TWO_OPERATOR_EXPR_LENGTH];
int64_t pos = 0;
REPORT_OUT_OF_RANGE_ERROR("'(%lu div %ld)'", "BIGINT UNSIGNED", left_ui, right_i);
} else {
} else {
datum.set_uint(res_ui);
}
}
return ret;
}
int ObExprIntDiv::div_number(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& datum)
int ObExprIntDiv::div_number(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &datum)
{
int ret = OB_SUCCESS;
ObDatum* left = NULL;
ObDatum* right = NULL;
ObDatum *left = NULL;
ObDatum *right = NULL;
bool is_finish = false;
if (OB_FAIL(get_arith_operand(expr, ctx, left, right, datum, is_finish))) {
LOG_WARN("get_arith_operand failed", K(ret));
} else if (is_finish) {
// do nothing
//do nothing
} else if (right->get_number().is_zero()) {
datum.set_null();
} else {
@ -443,7 +389,9 @@ int ObExprIntDiv::div_number(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& datum)
return ret;
}
int ObExprIntDiv::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprIntDiv::cg_expr(ObExprCGCtx &op_cg_ctx,
const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
int ret = OB_SUCCESS;
UNUSED(raw_expr);