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 "sql/engine/expr/ob_expr_func_ceil.h"
@ -18,24 +18,33 @@
#include "share/object/ob_obj_cast.h"
#include "sql/parser/ob_item_type.h"
#include "objit/common/ob_item_type.h"
#include "sql/engine/expr/ob_expr_result_type_util.h"
#include "sql/engine/expr/ob_expr_util.h"
#include "sql/session/ob_sql_session_info.h"
namespace oceanbase {
namespace oceanbase
{
using namespace common;
using namespace share;
namespace sql {
ObExprCeilFloor::ObExprCeilFloor(
ObIAllocator& alloc, ObExprOperatorType type, const char* name, int32_t param_num, int32_t dimension)
namespace sql
{
ObExprCeilFloor::ObExprCeilFloor(ObIAllocator &alloc,
ObExprOperatorType type,
const char *name,
int32_t param_num,
int32_t dimension)
: ObFuncExprOperator(alloc, type, name, param_num, dimension)
{}
{
}
ObExprCeilFloor::~ObExprCeilFloor()
{}
{
}
int ObExprCeilFloor::calc_result_type1(ObExprResType& type, ObExprResType& type1, ObExprTypeCtx& type_ctx) const
int ObExprCeilFloor::calc_result_type1(ObExprResType &type,
ObExprResType &type1,
ObExprTypeCtx &type_ctx) const
{
int ret = OB_SUCCESS;
ObObjType res_type = ObMaxType;
@ -62,10 +71,10 @@ int ObExprCeilFloor::calc_result_type1(ObExprResType& type, ObExprResType& type1
int16_t prec = type1.get_precision();
int16_t scale = type1.get_scale();
type.set_number();
// to be compatible with mysql.
//to be compatible with mysql.
if (scale > 0) {
if (prec - scale <= MAX_LIMIT_WITH_SCALE)
type.set_int();
type.set_int();
} else if (prec <= MAX_LIMIT_WITHOUT_SCALE) {
type.set_int();
}
@ -77,7 +86,8 @@ int ObExprCeilFloor::calc_result_type1(ObExprResType& type, ObExprResType& type1
LOG_WARN("unexpected result type", K(ret), K(type1), K(res_type));
} else {
type.set_type(res_type);
const ObSQLSessionInfo* session = dynamic_cast<const ObSQLSessionInfo*>(type_ctx.get_session());
const ObSQLSessionInfo *session =
dynamic_cast<const ObSQLSessionInfo*>(type_ctx.get_session());
if (OB_ISNULL(session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("cast basic session to sql session info failed", K(ret));
@ -86,7 +96,7 @@ int ObExprCeilFloor::calc_result_type1(ObExprResType& type, ObExprResType& type1
}
}
}
// no need to test ret here
//no need to test ret here
// scale
type.set_scale(0);
type.set_precision(type1.get_precision());
@ -96,96 +106,10 @@ int ObExprCeilFloor::calc_result_type1(ObExprResType& type, ObExprResType& type1
return ret;
}
int ObExprCeilFloor::calc_result1(ObObj& result, const ObObj& input, ObExprCtx& expr_ctx, bool is_ceil) const
int calc_ceil_floor(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res_datum)
{
int ret = OB_SUCCESS;
if (NULL == expr_ctx.calc_buf_) {
ret = OB_NOT_INIT;
LOG_WARN("varchar buffer not init", K(ret));
} else if (input.is_null()) {
result.set_null();
} else {
ObObjType type = input.get_type();
if (is_oracle_mode()) {
if (input.is_float()) {
if (is_ceil) {
result.set_float(ceilf(input.get_float()));
} else {
result.set_float(floorf(input.get_float()));
}
} else if (input.is_double()) {
if (is_ceil) {
result.set_double(ceil(input.get_double()));
} else {
result.set_double(floor(input.get_double()));
}
} else if (input.is_number()) {
number::ObNumber tmp_number;
int64_t scale = 0;
if (OB_FAIL(tmp_number.from(input.get_number(), *expr_ctx.calc_buf_))) {
LOG_WARN("copy number fail", K(ret));
} else if (OB_FAIL(is_ceil ? tmp_number.ceil(scale) : tmp_number.floor(scale))) {
LOG_WARN("get ceil/floor(number) failed", K(is_ceil), K(ret));
} else {
result.set_number(tmp_number);
}
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected type", K(input), K(ret));
}
LOG_DEBUG("finish ceil/floor", K(is_ceil), K(input), K(result), K(ret));
} else {
ObObj input_copy = input;
if (type != result_type_.get_type() && !(ObNumberType == type && ObIntType == result_type_.get_type())) {
ObCastMode cast_mode = get_cast_mode();
EXPR_DEFINE_CAST_CTX(expr_ctx, cast_mode);
cast_ctx.expect_obj_collation_ = cast_ctx.dest_collation_;
if (OB_FAIL(ObObjCaster::to_type(result_type_.get_type(), cast_ctx, input, input_copy))) {
LOG_WARN("fail to convert type", K(ret), K(result_type_), K(input));
}
}
type = input_copy.get_type();
if (OB_SUCC(ret)) {
if (ob_is_int_tc(type)) {
result.set_int(input_copy.get_int());
} else if (ob_is_uint_tc(type)) {
result.set_uint64(input_copy.get_uint64());
} else if (ob_is_double_tc(type)) {
result.set_double(is_ceil ? ceil(input_copy.get_double()) : floor(input_copy.get_double()));
} else if (ob_is_number_tc(type)) {
number::ObNumber num = input_copy.get_number();
number::ObNumber tmp_number;
int64_t scale = 0;
if (OB_FAIL(tmp_number.from(num, *expr_ctx.calc_buf_))) {
LOG_WARN("copy number fail", K(ret));
} else if (OB_FAIL(is_ceil ? tmp_number.ceil(scale) : tmp_number.floor(scale))) {
LOG_WARN("get ceil/floor(number) failed", K(is_ceil), K(ret));
} else if (ob_is_number_tc(result_type_.get_type())) {
result.set_type(result_type_.get_type());
result.set_number_value(tmp_number);
} else {
int64_t res_int = 0;
if (tmp_number.is_valid_int64(res_int)) {
result.set_int(res_int);
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected input number value", K(ret), K(input_copy), K(result_type_));
}
}
} else {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid input type", K(ret), K(type));
}
}
}
}
return ret;
}
int calc_ceil_floor(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
ObDatum* arg_datum = NULL;
ObDatum *arg_datum = NULL;
if (OB_FAIL(expr.args_[0]->eval(ctx, arg_datum))) {
LOG_WARN("eval arg 0 failed", K(ret), K(expr));
} else if (arg_datum->is_null()) {
@ -246,69 +170,174 @@ int calc_ceil_floor(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
return ret;
}
int ObExprCeilFloor::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
// TYPE value
// ObNumberTC 0
// ObIntegerType 1
// ObFloatType 2
// ObDoubleType 3
template <int TYPE, bool IS_FLOOR>
int do_eval_batch_ceil_floor(const ObExpr &expr,
ObEvalCtx &ctx,
const ObBitVector &skip,
const int64_t batch_size)
{
int ret = OB_SUCCESS;
static_assert(TYPE >= 0 && TYPE <= 3, "TYPE value out of range");
ObBitVector &eval_flag = expr.get_evaluated_flags(ctx);
ObDatumVector arg_datums = expr.args_[0]->locate_expr_datumvector(ctx);
ObDatum *res_datums = expr.locate_batch_datums(ctx);
for (int64_t i = 0; OB_SUCC(ret) && i < batch_size; i++) {
if (!eval_flag.contain(i) && !skip.contain(i)) {
if (0 == TYPE) {
if (arg_datums.at(i)->is_null()) {
res_datums[i].set_null();
} else {
number::ObNumber arg_nmb(arg_datums.at(i)->get_number());
ObNumStackOnceAlloc tmp_alloc;
number::ObNumber res_nmb;
if (OB_FAIL(res_nmb.from(arg_nmb, tmp_alloc))) {
LOG_WARN("get number from arg failed", K(ret), K(arg_nmb));
} else {
if (IS_FLOOR) {
if (OB_FAIL(res_nmb.floor(0))) {
LOG_WARN("calc floor for number failed", K(ret), K(res_nmb));
}
} else {
if (OB_FAIL(res_nmb.ceil(0))) {
LOG_WARN("calc ceil for number failed", K(ret), K(res_nmb));
}
}
const ObObjType res_type = expr.datum_meta_.type_;
if (ObNumberTC == ob_obj_type_class(res_type)) {
res_datums[i].set_number(res_nmb);
} else if (ob_is_integer_type(res_type)) {
int64_t res_int = 0;
if (res_nmb.is_valid_int64(res_int)) {
res_datums[i].set_int(res_int);
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected res type", K(ret), K(res_type));
}
}
}
}
} else if (1 == TYPE) {
if (arg_datums.at(i)->is_null()) {
res_datums[i].set_null();
} else {
res_datums[i].set_int(arg_datums.at(i)->get_int());
}
} else if (2 == TYPE) {
if (arg_datums.at(i)->is_null()) {
res_datums[i].set_null();
} else if (IS_FLOOR) {
res_datums[i].set_float(floorf(arg_datums.at(i)->get_float()));
} else {
res_datums[i].set_float(ceilf(arg_datums.at(i)->get_float()));
}
} else if (3 == TYPE) {
if (arg_datums.at(i)->is_null()) {
res_datums[i].set_null();
} else if (IS_FLOOR) {
res_datums[i].set_double(floor(arg_datums.at(i)->get_double()));
} else {
res_datums[i].set_double(ceil(arg_datums.at(i)->get_double()));
}
}
}
eval_flag.set(i);
}
return ret;
}
int eval_batch_ceil_floor(const ObExpr &expr,
ObEvalCtx &ctx,
const ObBitVector &skip,
const int64_t batch_size)
{
int ret = OB_SUCCESS;
if (OB_FAIL(expr.args_[0]->eval_batch(ctx, skip, batch_size))) {
LOG_WARN("vectorized evaluate failed", K(ret), K(expr));
} else {
const ObObjType arg_type = expr.args_[0]->datum_meta_.type_;
const bool is_floor = (T_FUN_SYS_FLOOR == expr.type_) ? true : false;
if (ObNumberTC == ob_obj_type_class(arg_type)) {
ret = is_floor
? do_eval_batch_ceil_floor<0,true>(expr, ctx, skip, batch_size)
: do_eval_batch_ceil_floor<0,false>(expr, ctx, skip, batch_size);
} else if (ob_is_integer_type(arg_type)) {
ret = do_eval_batch_ceil_floor<1,true>(expr, ctx, skip, batch_size);
} else if (ObFloatType == arg_type) {
ret = is_floor
? do_eval_batch_ceil_floor<2,true>(expr, ctx, skip, batch_size)
: do_eval_batch_ceil_floor<2,false>(expr, ctx, skip, batch_size);
} else if (ObDoubleType == arg_type) {
ret = is_floor
? do_eval_batch_ceil_floor<3,true>(expr, ctx, skip, batch_size)
: do_eval_batch_ceil_floor<3,false>(expr, ctx, skip, batch_size);
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected arg type", K(ret), K(arg_type));
}
}
return ret;
}
int ObExprCeilFloor::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
int ret = OB_SUCCESS;
UNUSED(expr_cg_ctx);
UNUSED(raw_expr);
rt_expr.eval_func_ = calc_ceil_floor;
rt_expr.eval_batch_func_ = eval_batch_ceil_floor;
return ret;
}
// func ceil()
ObExprFuncCeil::ObExprFuncCeil(ObIAllocator& alloc)
//func ceil()
ObExprFuncCeil::ObExprFuncCeil(ObIAllocator &alloc)
: ObExprCeilFloor(alloc, T_FUN_SYS_CEIL, "ceil", 1, NOT_ROW_DIMENSION)
{}
ObExprFuncCeil::~ObExprFuncCeil()
{}
int ObExprFuncCeil::calc_result_type1(ObExprResType& type, ObExprResType& type1, common::ObExprTypeCtx& type_ctx) const
{
return ObExprCeilFloor::calc_result_type1(type, type1, type_ctx);
}
int ObExprFuncCeil::calc_result1(ObObj& result, const ObObj& input, ObExprCtx& expr_ctx) const
ObExprFuncCeil::~ObExprFuncCeil()
{
return ObExprCeilFloor::calc_result1(result, input, expr_ctx, true);
}
int ObExprFuncCeil::calc_result_type1(ObExprResType &type, ObExprResType &type1, common::ObExprTypeCtx &type_ctx) const
{
return ObExprCeilFloor::calc_result_type1(type, type1, type_ctx);
}
// func ceiling()
ObExprFuncCeiling::ObExprFuncCeiling(ObIAllocator& alloc)
ObExprFuncCeiling::ObExprFuncCeiling(ObIAllocator &alloc)
: ObExprCeilFloor(alloc, T_FUN_SYS_CEILING, "ceiling", 1, NOT_ROW_DIMENSION)
{}
{
}
ObExprFuncCeiling::~ObExprFuncCeiling()
{}
{
}
int ObExprFuncCeiling::calc_result_type1(
ObExprResType& type, ObExprResType& type1, common::ObExprTypeCtx& type_ctx) const
int ObExprFuncCeiling::calc_result_type1(ObExprResType &type, ObExprResType &type1, common::ObExprTypeCtx &type_ctx) const
{
return ObExprCeilFloor::calc_result_type1(type, type1, type_ctx);
}
int ObExprFuncCeiling::calc_result1(ObObj& result, const ObObj& input, ObExprCtx& expr_ctx) const
{
return ObExprCeilFloor::calc_result1(result, input, expr_ctx, true);
}
ObExprFuncFloor::ObExprFuncFloor(ObIAllocator& alloc)
ObExprFuncFloor::ObExprFuncFloor(ObIAllocator &alloc)
: ObExprCeilFloor(alloc, T_FUN_SYS_FLOOR, "floor", 1, NOT_ROW_DIMENSION)
{}
{
}
// func floor()
ObExprFuncFloor::~ObExprFuncFloor()
{}
{
}
int ObExprFuncFloor::calc_result_type1(ObExprResType& type, ObExprResType& type1, common::ObExprTypeCtx& type_ctx) const
int ObExprFuncFloor::calc_result_type1(ObExprResType &type, ObExprResType &type1, common::ObExprTypeCtx &type_ctx) const
{
return ObExprCeilFloor::calc_result_type1(type, type1, type_ctx);
}
int ObExprFuncFloor::calc_result1(ObObj& result, const ObObj& input, ObExprCtx& expr_ctx) const
{
return ObExprCeilFloor::calc_result1(result, input, expr_ctx, false);
}
} // namespace sql
} // namespace oceanbase
} // namespace sql
} // namespace oceanbase