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

@ -19,56 +19,24 @@
#include "lib/ob_date_unit_type.h"
#include "sql/engine/expr/ob_expr_util.h"
namespace oceanbase {
using namespace common;
namespace sql {
ObExprDateFormat::ObExprDateFormat(ObIAllocator& alloc)
: ObStringExprOperator(alloc, T_FUN_SYS_DATE_FORMAT, N_DATE_FORMAT, 2)
{}
ObExprDateFormat::~ObExprDateFormat()
{}
int ObExprDateFormat::calc_result2(ObObj& result, const ObObj& date, const ObObj& format, ObExprCtx& expr_ctx) const
namespace oceanbase
{
using namespace common;
namespace sql
{
ObExprDateFormat::ObExprDateFormat(ObIAllocator &alloc)
: ObStringExprOperator(alloc, T_FUN_SYS_DATE_FORMAT, N_DATE_FORMAT, 2)
{
int ret = OB_SUCCESS;
ObTime ob_time;
char* buf = NULL;
int64_t buf_len = OB_MAX_DATE_FORMAT_BUF_LEN;
int64_t pos = 0;
bool res_null = false;
if (OB_UNLIKELY(ObNullType == date.get_type() || ObNullType == format.get_type())) {
result.set_null();
} else if (OB_UNLIKELY(ObStringTC != format.get_type_class())) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("DATE_FORMAT() expected a string as format argument");
check_reset_status(expr_ctx, ret, result);
} else if (OB_ISNULL(expr_ctx.calc_buf_) || OB_ISNULL(expr_ctx.my_session_)) {
ret = OB_NOT_INIT;
LOG_WARN("the pointer is null");
} else if (OB_ISNULL(buf = static_cast<char*>(expr_ctx.calc_buf_->alloc(buf_len)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("no more memory to alloc for buf");
} else if (OB_FAIL(ob_obj_to_ob_time_with_date(date, get_timezone_info(expr_ctx.my_session_), ob_time,
get_cur_time(expr_ctx.exec_ctx_->get_physical_plan_ctx())))) {
LOG_WARN("failed to convert obj to ob time");
check_reset_status(expr_ctx, ret, result);
} else if (OB_UNLIKELY(format.get_string().empty())) {
result.set_null();
} else if (OB_FAIL(
ObTimeConverter::ob_time_to_str_format(ob_time, format.get_string(), buf, buf_len, pos, res_null))) {
LOG_WARN("failed to convert ob time to str with format");
} else if (res_null) {
result.set_null();
} else {
result.set_varchar(buf, static_cast<int32_t>(pos));
result.set_collation(result_type_);
}
return ret;
}
void ObExprDateFormat::check_reset_status(ObExprCtx& expr_ctx, int& ret, ObObj& result)
ObExprDateFormat::~ObExprDateFormat()
{
}
void ObExprDateFormat::check_reset_status(ObExprCtx &expr_ctx,
int &ret,
ObObj &result)
{
if (CM_IS_WARN_ON_FAIL(expr_ctx.cast_mode_)) {
ret = OB_SUCCESS;
@ -76,7 +44,9 @@ void ObExprDateFormat::check_reset_status(ObExprCtx& expr_ctx, int& ret, ObObj&
}
}
int ObExprDateFormat::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprDateFormat::cg_expr(ObExprCGCtx &op_cg_ctx,
const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
@ -84,11 +54,12 @@ int ObExprDateFormat::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr,
if (rt_expr.arg_cnt_ != 2) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("date_format expr should have two params", K(ret), K(rt_expr.arg_cnt_));
} else if (OB_ISNULL(rt_expr.args_) || OB_ISNULL(rt_expr.args_[0]) || OB_ISNULL(rt_expr.args_[1])) {
} else if (OB_ISNULL(rt_expr.args_) || OB_ISNULL(rt_expr.args_[0])
|| OB_ISNULL(rt_expr.args_[1])) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of date_format expr is null", K(ret), K(rt_expr.args_));
} else if (ObStringTC != ob_obj_type_class(rt_expr.args_[1]->datum_meta_.type_) &&
ObNullType != rt_expr.args_[1]->datum_meta_.type_) {
} else if (ObStringTC != ob_obj_type_class(rt_expr.args_[1]->datum_meta_.type_)
&& ObNullType != rt_expr.args_[1]->datum_meta_.type_) {
rt_expr.eval_func_ = ObExprDateFormat::calc_date_format_invalid;
} else {
rt_expr.eval_func_ = ObExprDateFormat::calc_date_format;
@ -96,22 +67,24 @@ int ObExprDateFormat::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr,
return ret;
}
int ObExprDateFormat::calc_date_format(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprDateFormat::calc_date_format(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
int ret = OB_SUCCESS;
ObTime ob_time;
char* buf = NULL;
char *buf = NULL;
int64_t buf_len = OB_MAX_DATE_FORMAT_BUF_LEN;
int64_t pos = 0;
const ObSQLSessionInfo* session = NULL;
ObDatum* date = NULL;
ObDatum* format = NULL;
const ObSQLSessionInfo *session = NULL;
ObDatum *date = NULL;
ObDatum *format = NULL;
uint64_t cast_mode = 0;
bool res_null = false;
ObDateSqlMode date_sql_mode;
if (OB_ISNULL(session = ctx.exec_ctx_.get_my_session())) {
ret = OB_NOT_INIT;
LOG_WARN("session is null", K(ret), K(session));
} else if (OB_FAIL(ObSQLUtils::get_default_cast_mode(session->get_stmt_type(), session, cast_mode))) {
} else if (OB_FAIL(ObSQLUtils::get_default_cast_mode(session->get_stmt_type(),
session, cast_mode))) {
LOG_WARN("get default cast mode failed", K(ret));
} else if (OB_FAIL(expr.eval_param_value(ctx, date, format))) {
LOG_WARN("calc param failed", K(ret));
@ -120,11 +93,14 @@ int ObExprDateFormat::calc_date_format(const ObExpr& expr, ObEvalCtx& ctx, ObDat
} else if (OB_ISNULL(buf = expr.get_str_res_mem(ctx, buf_len))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("no more memory to alloc for buf");
} else if (FALSE_IT(date_sql_mode.init(session->get_sql_mode()))) {
} else if (OB_FAIL(ob_datum_to_ob_time_with_date(*date,
expr.args_[0]->datum_meta_.type_,
get_timezone_info(session),
ob_time,
get_cur_time(ctx.exec_ctx_.get_physical_plan_ctx())))) {
expr.args_[0]->datum_meta_.type_,
get_timezone_info(session),
ob_time,
get_cur_time(ctx.exec_ctx_.get_physical_plan_ctx()),
false,
date_sql_mode))) {
LOG_WARN("failed to convert datum to ob time");
if (CM_IS_WARN_ON_FAIL(cast_mode) && OB_ALLOCATE_MEMORY_FAILED != ret) {
ret = OB_SUCCESS;
@ -132,8 +108,12 @@ int ObExprDateFormat::calc_date_format(const ObExpr& expr, ObEvalCtx& ctx, ObDat
}
} else if (OB_UNLIKELY(format->get_string().empty())) {
expr_datum.set_null();
} else if (OB_FAIL(
ObTimeConverter::ob_time_to_str_format(ob_time, format->get_string(), buf, buf_len, pos, res_null))) {
} else if (OB_FAIL(ObTimeConverter::ob_time_to_str_format(ob_time,
format->get_string(),
buf,
buf_len,
pos,
res_null))) {
LOG_WARN("failed to convert ob time to str with format");
} else if (res_null) {
expr_datum.set_null();
@ -143,20 +123,21 @@ int ObExprDateFormat::calc_date_format(const ObExpr& expr, ObEvalCtx& ctx, ObDat
return ret;
}
int ObExprDateFormat::calc_date_format_invalid(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprDateFormat::calc_date_format_invalid(const ObExpr &expr, ObEvalCtx &ctx,
ObDatum &expr_datum)
{
UNUSED(expr);
int ret = OB_SUCCESS;
expr_datum.set_null();
uint64_t cast_mode = 0;
const ObSQLSessionInfo* session = NULL;
const ObSQLSessionInfo *session = NULL;
if (OB_ISNULL(session = ctx.exec_ctx_.get_my_session())) {
ret = OB_NOT_INIT;
LOG_WARN("session is null", K(ret), K(session));
} else if (OB_FAIL(ObSQLUtils::get_default_cast_mode(session->get_stmt_type(), session, cast_mode))) {
LOG_WARN("get default cast mode failed", K(ret));
} else if (!CM_IS_WARN_ON_FAIL(cast_mode)) {
ret = OB_INVALID_ARGUMENT;
ret =OB_INVALID_ARGUMENT;
}
return ret;
}
@ -229,54 +210,6 @@ inline int ObExprGetFormat::calc_result_type2(ObExprResType &type,
return ret;
}
int ObExprGetFormat::calc_result2(ObObj &result,
const ObObj &unit,
const ObObj &format,
ObExprCtx &expr_ctx) const
{
UNUSED(expr_ctx);
int ret = OB_SUCCESS;
if (OB_UNLIKELY(ObIntType != unit.get_type()
|| unit.get_int() < 0 || unit.get_int() >= GET_FORMAT_MAX)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("unexpected unit date type", K(ret), K(unit));
} else if (format.is_null()) {
result.set_null();
} else if (OB_UNLIKELY(ObVarcharType != format.get_type())) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("GET_FORMAT() expected a string as format argument");
} else {
Format fm = FORMAT_MAX;
ObGetFormatUnitType type = static_cast<ObGetFormatUnitType>(unit.get_int());
const ObString str = format.get_string();
for (int64_t i = 0; i < FORMAT_MAX; i++) {
if (0 == str.case_compare(FORMAT_STR[i])) {
fm = static_cast<Format>(i);
break;
}
}
if (OB_UNLIKELY(FORMAT_MAX == fm)) {
result.set_null();
} else {
const char *res_str = NULL;
if (GET_FORMAT_DATE == type) {
result.set_varchar(DATE_FORMAT[fm]);
} else if (GET_FORMAT_TIME == type) {
result.set_varchar(TIME_FORMAT[fm]);
} else if (GET_FORMAT_DATETIME == type) {
result.set_varchar(DATETIME_FORMAT[fm]);
}
if (OB_UNLIKELY(ObCharset::is_cs_nonascii(result_type_.get_collation_type()))
&& OB_FAIL(convert_result_collation(result_type_, result, expr_ctx.calc_buf_))) {
LOG_WARN("fail to convert result collation", K(ret));
} else {
result.set_collation(result_type_);
}
}
}
return ret;
}
int ObExprGetFormat::cg_expr(ObExprCGCtx &op_cg_ctx,
const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
@ -358,5 +291,5 @@ int ObExprGetFormat::calc_get_format(const ObExpr &expr, ObEvalCtx &ctx, ObDatum
return ret;
}
} // namespace sql
} // namespace oceanbase
}
}