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

@ -22,33 +22,32 @@
using namespace oceanbase::share;
using namespace oceanbase::sql;
namespace oceanbase {
namespace sql {
namespace oceanbase
{
namespace sql
{
ObExprDayOfMonth::ObExprDayOfMonth(ObIAllocator& alloc)
: ObExprTimeBase(alloc, DT_MDAY, T_FUN_SYS_DAY_OF_MONTH, N_DAY_OF_MONTH){};
ObExprDayOfMonth::ObExprDayOfMonth(ObIAllocator &alloc)
: ObExprTimeBase(alloc, DT_MDAY, T_FUN_SYS_DAY_OF_MONTH, N_DAY_OF_MONTH) {};
ObExprDayOfMonth::~ObExprDayOfMonth()
{}
ObExprDayOfMonth::~ObExprDayOfMonth() {}
int ObExprDayOfMonth::calc_dayofmonth(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprDayOfMonth::calc_dayofmonth(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
return ObExprTimeBase::calc(expr, ctx, expr_datum, DT_MDAY, true, true);
}
ObExprDay::ObExprDay(ObIAllocator &alloc)
: ObExprTimeBase(alloc, DT_MDAY, T_FUN_SYS_DAY, N_DAY){};
: ObExprTimeBase(alloc, DT_MDAY, T_FUN_SYS_DAY, N_DAY) {};
ObExprDay::~ObExprDay()
{}
ObExprDay::~ObExprDay() {}
ObExprDayOfWeek::ObExprDayOfWeek(ObIAllocator& alloc)
: ObExprTimeBase(alloc, DT_WDAY, T_FUN_SYS_DAY_OF_WEEK, N_DAY_OF_WEEK){};
ObExprDayOfWeek::ObExprDayOfWeek(ObIAllocator &alloc)
: ObExprTimeBase(alloc, DT_WDAY, T_FUN_SYS_DAY_OF_WEEK, N_DAY_OF_WEEK) {};
ObExprDayOfWeek::~ObExprDayOfWeek()
{}
ObExprDayOfWeek::~ObExprDayOfWeek() {}
int ObExprDayOfWeek::calc_dayofweek(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprDayOfWeek::calc_dayofweek(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
int ret = OB_SUCCESS;
if (ObExprTimeBase::calc(expr, ctx, expr_datum, DT_WDAY, true)) {
@ -59,66 +58,24 @@ int ObExprDayOfWeek::calc_dayofweek(const ObExpr& expr, ObEvalCtx& ctx, ObDatum&
return ret;
}
ObExprDayOfYear::ObExprDayOfYear(ObIAllocator& alloc)
: ObExprTimeBase(alloc, DT_YDAY, T_FUN_SYS_DAY_OF_YEAR, N_DAY_OF_YEAR){};
ObExprDayOfYear::ObExprDayOfYear(ObIAllocator &alloc)
: ObExprTimeBase(alloc, DT_YDAY, T_FUN_SYS_DAY_OF_YEAR, N_DAY_OF_YEAR) {};
ObExprDayOfYear::~ObExprDayOfYear()
{}
ObExprDayOfYear::~ObExprDayOfYear() { }
int ObExprDayOfYear::calc_dayofyear(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprDayOfYear::calc_dayofyear(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
return ObExprTimeBase::calc(expr, ctx, expr_datum, DT_YDAY, true);
}
ObExprToSeconds::ObExprToSeconds(ObIAllocator& alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_TO_SECONDS, N_TO_SECONDS, 1, NOT_ROW_DIMENSION){};
ObExprToSeconds::ObExprToSeconds(ObIAllocator &alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_TO_SECONDS, N_TO_SECONDS, 1, NOT_ROW_DIMENSION) {};
ObExprToSeconds::~ObExprToSeconds()
{}
ObExprToSeconds::~ObExprToSeconds() {}
int ObExprToSeconds::calc_result1(ObObj& result, const ObObj& obj, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
EXPR_DEFINE_CAST_CTX(expr_ctx, CM_NONE);
const ObObj* p_obj = NULL;
EXPR_CAST_OBJ_V2(ObDateType, obj, p_obj);
if (OB_SUCC(ret)) {
if (OB_ISNULL(expr_ctx.my_session_) || OB_ISNULL(expr_ctx.exec_ctx_)) {
ret = OB_NOT_INIT;
LOG_WARN("session ptr or exec ctx is null", K(ret), K(expr_ctx.my_session_));
} else if (OB_ISNULL(p_obj)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("p_obj is null", K(ret));
} else if (OB_UNLIKELY(p_obj->is_null() || OB_INVALID_DATE_VALUE == cast_ctx.warning_)) {
result.set_null();
} else {
ObTime ot;
if (OB_FAIL(ob_obj_to_ob_time_with_date(obj, get_timezone_info(expr_ctx.my_session_), ot,
get_cur_time(expr_ctx.exec_ctx_->get_physical_plan_ctx())))) {
LOG_WARN("cast to ob time failed", K(ret), K(obj), K(expr_ctx.cast_mode_));
if (CM_IS_WARN_ON_FAIL(expr_ctx.cast_mode_)) {
ret = OB_SUCCESS;
result.set_null();
} else { /* do nothing */
}
} else {
int64_t days = p_obj->get_date() + DAYS_FROM_ZERO_TO_BASE;
int64_t seconds =
days * 60 * 60 * 24L + ot.parts_[DT_HOUR] * 60 * 60L + ot.parts_[DT_MIN] * 60L + ot.parts_[DT_SEC];
if (seconds < 0) {
result.set_null();
} else {
result.set_int(seconds);
}
}
}
} else {
LOG_WARN("cast obj failed", K(ret));
}
return ret;
}
int ObExprToSeconds::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprToSeconds::cg_expr(ObExprCGCtx &op_cg_ctx,
const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
@ -130,16 +87,16 @@ int ObExprToSeconds::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr,
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of to_seconds expr is null", K(ret), K(rt_expr.args_));
} else {
rt_expr.eval_func_ = ObExprToSeconds::calc_toseconds;
rt_expr.eval_func_ = ObExprToSeconds::calc_toseconds;
}
return ret;
}
int ObExprToSeconds::calc_toseconds(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprToSeconds::calc_toseconds(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
int ret = OB_SUCCESS;
ObDatum* param_datum = NULL;
const ObSQLSessionInfo* session = NULL;
ObDatum *param_datum = NULL;
const ObSQLSessionInfo *session = NULL;
if (OB_ISNULL(session = ctx.exec_ctx_.get_my_session())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is null", K(ret));
@ -149,23 +106,23 @@ int ObExprToSeconds::calc_toseconds(const ObExpr& expr, ObEvalCtx& ctx, ObDatum&
expr_datum.set_null();
} else {
ObTime ot;
if (OB_FAIL(ob_datum_to_ob_time_with_date(*param_datum,
expr.args_[0]->datum_meta_.type_,
get_timezone_info(session),
ot,
get_cur_time(ctx.exec_ctx_.get_physical_plan_ctx())))) {
ObDateSqlMode date_sql_mode;
date_sql_mode.init(session->get_sql_mode());
if (OB_FAIL(ob_datum_to_ob_time_with_date(*param_datum, expr.args_[0]->datum_meta_.type_,
get_timezone_info(session), ot,
get_cur_time(ctx.exec_ctx_.get_physical_plan_ctx()),
false, date_sql_mode))) {
LOG_WARN("cast to ob time failed", K(ret));
uint64_t cast_mode = 0;
ObSQLUtils::get_default_cast_mode(session->get_stmt_type(), session, cast_mode);
if (CM_IS_WARN_ON_FAIL(cast_mode)) {
ret = OB_SUCCESS;
expr_datum.set_null();
} else { /* do nothing */
}
} else { /* do nothing */ }
} else {
int64_t days = ObTimeConverter::ob_time_to_date(ot) + DAYS_FROM_ZERO_TO_BASE;
int64_t seconds =
days * 60 * 60 * 24L + ot.parts_[DT_HOUR] * 60 * 60L + ot.parts_[DT_MIN] * 60L + ot.parts_[DT_SEC];
int64_t seconds = days * 60 * 60 * 24L + ot.parts_[DT_HOUR] * 60 * 60L
+ ot.parts_[DT_MIN] * 60L + ot.parts_[DT_SEC];
if (seconds < 0) {
expr_datum.set_null();
} else {
@ -176,61 +133,14 @@ int ObExprToSeconds::calc_toseconds(const ObExpr& expr, ObEvalCtx& ctx, ObDatum&
return ret;
}
ObExprSecToTime::ObExprSecToTime(ObIAllocator& alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_SEC_TO_TIME, N_SEC_TO_TIME, 1, NOT_ROW_DIMENSION){};
ObExprSecToTime::ObExprSecToTime(ObIAllocator &alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_SEC_TO_TIME, N_SEC_TO_TIME, 1, NOT_ROW_DIMENSION) {};
ObExprSecToTime::~ObExprSecToTime()
{}
ObExprSecToTime::~ObExprSecToTime() {}
int ObExprSecToTime::calc_result1(ObObj& result, const ObObj& sec, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(sec.is_null())) {
result.set_null();
} else if (OB_ISNULL(expr_ctx.calc_buf_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("calc buf is NULL", K(ret));
} else {
TYPE_CHECK(sec, ObNumberType);
EXPR_DEFINE_CAST_CTX(expr_ctx, CM_NONE);
number::ObNumber num_usec;
number::ObNumber num_sec;
number::ObNumber million;
int64_t int_usec = 0;
if (OB_FAIL(num_sec.from(sec.get_number(), *expr_ctx.calc_buf_))) {
LOG_WARN("failed to create number", K(ret), K(sec.get_number()));
} else if (OB_FAIL(million.from(static_cast<int64_t>(1000000), *expr_ctx.calc_buf_))) {
LOG_WARN("failed to create number million", K(ret));
} else if (OB_FAIL(num_sec.mul(million, num_usec, *expr_ctx.calc_buf_))) {
LOG_WARN("failed to mul number", K(ret), K(num_sec), K(million));
} else if (OB_FAIL(num_usec.round(0))) {
LOG_WARN("failed to round number", K(ret), K(num_usec));
} else if (!num_usec.is_valid_int64(int_usec)) {
int_usec = num_usec.is_negative() ? -INT64_MAX : INT64_MAX;
}
if (OB_SUCC(ret)) {
if (num_usec.is_negative()) {
int_usec = -int_usec;
}
if (OB_FAIL(ObTimeConverter::time_overflow_trunc(int_usec))) {
if (CM_IS_WARN_ON_FAIL(cast_ctx.cast_mode_)) {
ret = OB_SUCCESS;
} else {
LOG_WARN("time value is out of range", K(ret), K(int_usec));
}
}
}
if (OB_SUCC(ret)) {
if (num_usec.is_negative()) {
int_usec = -int_usec;
}
result.set_time(int_usec);
}
}
return ret;
}
int ObExprSecToTime::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprSecToTime::cg_expr(ObExprCGCtx &op_cg_ctx,
const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
@ -242,17 +152,17 @@ int ObExprSecToTime::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr,
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of sectotime expr is null", K(ret), K(rt_expr.args_));
} else {
CK(ObNumberType == rt_expr.args_[0]->datum_meta_.type_);
rt_expr.eval_func_ = ObExprSecToTime::calc_sectotime;
CK(ObNumberType == rt_expr.args_[0]->datum_meta_.type_);
rt_expr.eval_func_ = ObExprSecToTime::calc_sectotime;
}
return ret;
}
int ObExprSecToTime::calc_sectotime(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprSecToTime::calc_sectotime(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
int ret = OB_SUCCESS;
ObDatum* param_datum = NULL;
const ObSQLSessionInfo* session = NULL;
ObDatum *param_datum = NULL;
const ObSQLSessionInfo *session = NULL;
if (OB_ISNULL(session = ctx.exec_ctx_.get_my_session())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is null", K(ret));
@ -302,26 +212,13 @@ int ObExprSecToTime::calc_sectotime(const ObExpr& expr, ObEvalCtx& ctx, ObDatum&
return ret;
}
ObExprTimeToSec::ObExprTimeToSec(ObIAllocator& alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_TIME_TO_SEC, N_TIME_TO_SEC, 1, NOT_ROW_DIMENSION){};
ObExprTimeToSec::~ObExprTimeToSec()
{}
ObExprTimeToSec::ObExprTimeToSec(ObIAllocator &alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_TIME_TO_SEC, N_TIME_TO_SEC, 1, NOT_ROW_DIMENSION) {};
ObExprTimeToSec::~ObExprTimeToSec() {}
int ObExprTimeToSec::calc_result1(ObObj& result, const ObObj& obj, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(obj.is_null())) {
result.set_null();
} else {
TYPE_CHECK(obj, ObTimeType);
int64_t t_val = obj.get_time();
result.set_int(USEC_TO_SEC(t_val));
}
UNUSED(expr_ctx);
return ret;
}
int ObExprTimeToSec::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprTimeToSec::cg_expr(ObExprCGCtx &op_cg_ctx,
const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
@ -333,17 +230,17 @@ int ObExprTimeToSec::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr,
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of timetosec expr is null", K(ret), K(rt_expr.args_));
} else {
CK(ObTimeType == rt_expr.args_[0]->datum_meta_.type_);
rt_expr.eval_func_ = ObExprTimeToSec::calc_timetosec;
CK(ObTimeType == rt_expr.args_[0]->datum_meta_.type_);
rt_expr.eval_func_ = ObExprTimeToSec::calc_timetosec;
}
return ret;
}
int ObExprTimeToSec::calc_timetosec(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprTimeToSec::calc_timetosec(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
int ret = OB_SUCCESS;
ObDatum* param_datum = NULL;
const ObSQLSessionInfo* session = NULL;
ObDatum *param_datum = NULL;
const ObSQLSessionInfo *session = NULL;
if (OB_ISNULL(session = ctx.exec_ctx_.get_my_session())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is null", K(ret));
@ -358,11 +255,22 @@ int ObExprTimeToSec::calc_timetosec(const ObExpr& expr, ObEvalCtx& ctx, ObDatum&
return ret;
}
ObExprSubtime::ObExprSubtime(ObIAllocator& alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_SUBTIME, N_SUB_TIME, 2, NOT_ROW_DIMENSION)
ObExprSubAddtime::ObExprSubAddtime(ObIAllocator &alloc, ObExprOperatorType type, const char *name, int32_t param_num, int32_t dimension)
: ObFuncExprOperator(alloc, type, name, 2, NOT_ROW_DIMENSION)
{}
int ObExprSubtime::calc_result_type2(
ObExprResType& type, ObExprResType& date_arg, ObExprResType& time_arg, ObExprTypeCtx& type_ctx) const
ObExprSubtime::ObExprSubtime(ObIAllocator &alloc)
: ObExprSubAddtime(alloc, T_FUN_SYS_SUBTIME, N_SUB_TIME, 2, NOT_ROW_DIMENSION)
{}
ObExprAddtime::ObExprAddtime(ObIAllocator &alloc)
: ObExprSubAddtime(alloc, T_FUN_SYS_ADDTIME, N_ADD_TIME, 2, NOT_ROW_DIMENSION)
{}
int ObExprSubAddtime::calc_result_type2(ObExprResType &type,
ObExprResType &date_arg,
ObExprResType &time_arg,
ObExprTypeCtx &type_ctx) const
{
int ret = OB_SUCCESS;
ObScale scale1 = date_arg.get_scale();
@ -386,6 +294,10 @@ int ObExprSubtime::calc_result_type2(
ret = aggregate_charsets_for_string_result(type, &date_arg, 1, type_ctx.get_coll_type());
}
if (OB_SUCC(ret)) {
// date_arg无法设置calc_type的原因是,date_arg类型是varchar时,设置calc_type为time和datetime都不合适
// 如果设置为time,那么当字符串中包含日期时,转换后日期信息直接丢失了
// 如果设置为datetime,虽然保留了日期信息,但是当字符串不包含日期时,mysql会将字符串最右边视为秒,
// 即'123'的含义是1分23秒,但是OB cast成datetime类型会把字符串右边视为日,即‘123’的含义是1月23日
if (ob_is_enumset_tc(date_arg.get_type())) {
date_arg.set_calc_type(ObVarcharType);
}
@ -399,8 +311,10 @@ int ObExprSubtime::calc_result_type2(
return ret;
}
int ObExprSubtime::calc_result2(common::ObObj& result, const common::ObObj& date_arg, const common::ObObj& time_arg,
common::ObExprCtx& expr_ctx) const
int ObExprSubAddtime::calc_result2(common::ObObj &result,
const common::ObObj &date_arg,
const common::ObObj &time_arg,
common::ObExprCtx &expr_ctx) const
{
int ret = OB_SUCCESS;
int64_t t_val1 = 0;
@ -409,7 +323,6 @@ int ObExprSubtime::calc_result2(common::ObObj& result, const common::ObObj& date
bool param_with_date = true;
ObObjType result_type = get_result_type().get_type();
const ObSQLSessionInfo *session = NULL;
EXPR_DEFINE_CAST_CTX(expr_ctx, CM_NONE);
if (OB_UNLIKELY(date_arg.is_null() || time_arg.is_null())) {
result.set_null();
@ -440,7 +353,7 @@ int ObExprSubtime::calc_result2(common::ObObj& result, const common::ObObj& date
}
}
if (OB_FAIL(ret)) {
} else if (ObTimeType == result_type || !param_with_date) { // a#, subtime(时间1, 时间2)
} else if (ObTimeType == result_type || !param_with_date) { // a#, subaddtime(时间1, 时间2)
if (ObVarcharType == result_type) {
t_val1 = ObTimeConverter::ob_time_to_time(ot1);
if (IS_NEG_TIME(ot1.mode_)) {
@ -451,7 +364,7 @@ int ObExprSubtime::calc_result2(common::ObObj& result, const common::ObObj& date
t_val1 = date_arg.get_time();
}
if (OB_SUCC(ret)) {
int64_t int_usec = t_val1 - t_val2;
int64_t int_usec = (get_type() == T_FUN_SYS_SUBTIME) ? t_val1 - t_val2 : t_val1 + t_val2;
if (OB_FAIL(ObTimeConverter::time_overflow_trunc(int_usec))) {
if (CM_IS_WARN_ON_FAIL(cast_ctx.cast_mode_)) {
ret = OB_SUCCESS;
@ -462,7 +375,7 @@ int ObExprSubtime::calc_result2(common::ObObj& result, const common::ObObj& date
result.set_time(int_usec);
}
}
} else { // b#, subtime(日期时间1, 时间2)
} else { // b#, subaddtime(日期时间1, 时间2)
const ObTimeZoneInfo *tz_info = NULL;
if (OB_ISNULL(tz_info = get_timezone_info(expr_ctx.my_session_))) {
ret = OB_ERR_UNEXPECTED;
@ -482,7 +395,7 @@ int ObExprSubtime::calc_result2(common::ObObj& result, const common::ObObj& date
}
}
if (OB_SUCC(ret)) {
int64_t int_usec = t_val1 - t_val2;
int64_t int_usec = (get_type() == T_FUN_SYS_SUBTIME) ? t_val1 - t_val2 : t_val1 + t_val2;
if (ObTimeConverter::is_valid_datetime(int_usec)) {
result.set_datetime(int_usec);
} else {
@ -508,46 +421,52 @@ int ObExprSubtime::calc_result2(common::ObObj& result, const common::ObObj& date
return ret;
}
int ObExprSubtime::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprSubAddtime::cg_expr(ObExprCGCtx &op_cg_ctx,
const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
int ret = OB_SUCCESS;
if (rt_expr.arg_cnt_ != 2) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("subtime 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])) {
LOG_WARN("subaddtime 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])) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of subtime expr is null", K(ret), K(rt_expr.args_));
LOG_WARN("children of subaddtime expr is null", K(ret), K(rt_expr.args_));
} else {
const ObObjType result_type = rt_expr.datum_meta_.type_;
const ObObjType param1_type = rt_expr.args_[0]->datum_meta_.type_;
const ObObjType param2_type = rt_expr.args_[1]->datum_meta_.type_;
if (ObTimeType != param2_type && ObVarcharType != param2_type) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("second param of subtime expr is not time type", K(ret), K(param2_type));
LOG_WARN("second param of subaddtime expr is not time type", K(ret), K(param2_type));
} else {
switch (result_type) {
case ObDateTimeType:
if (ObNullType != param1_type && ObDateTimeType != param1_type && ObTimestampType != param1_type) {
case ObDateTimeType :
if (ObNullType != param1_type && ObDateTimeType != param1_type
&& ObTimestampType != param1_type) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid type of first argument", K(ret), K(result_type), K(param1_type));
} else {
rt_expr.eval_func_ = ObExprSubtime::subtime_datetime;
rt_expr.eval_func_ = ObExprSubAddtime::subaddtime_datetime;
}
break;
case ObTimeType:
case ObTimeType :
if (ObTimeType != param1_type) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid type of first argument", K(ret), K(result_type), K(param1_type));
} else {
rt_expr.eval_func_ = ObExprSubtime::subtime_datetime;
//这里虽然参数类型、结果类型是TimeType,和上面不同,但是get/set_time/datetime的含义是相同的,
//两个计算函数的本质都是取第一个参数都int64_t值,与第二个参数相减,然后把结果放入expr_datum中
rt_expr.eval_func_ = ObExprSubAddtime::subaddtime_datetime;
}
break;
case ObVarcharType:
rt_expr.eval_func_ = ObExprSubtime::subtime_varchar;
case ObVarcharType :
rt_expr.eval_func_ = ObExprSubAddtime::subaddtime_varchar;
break;
default:
default :
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid expr type", K(rt_expr.datum_meta_.type_));
}
@ -556,12 +475,17 @@ int ObExprSubtime::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, Ob
return ret;
}
int ObExprSubtime::subtime_common(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum, bool& null_res,
ObDatum*& date_arg, ObDatum*& time_arg, int64_t& time_val)
int ObExprSubAddtime::subaddtime_common(const ObExpr &expr,
ObEvalCtx &ctx,
ObDatum &expr_datum,
bool &null_res,
ObDatum *&date_arg,
ObDatum *&time_arg,
int64_t &time_val)
{
int ret = OB_SUCCESS;
null_res = false;
const ObSQLSessionInfo* session = NULL;
const ObSQLSessionInfo *session = NULL;
if (OB_ISNULL(session = ctx.exec_ctx_.get_my_session())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is null", K(ret));
@ -576,10 +500,8 @@ int ObExprSubtime::subtime_common(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& e
ObTime ot2(DT_TYPE_TIME);
if (ObTimeType == expr.args_[1]->datum_meta_.type_) {
time_val = time_arg->get_time();
} else if (OB_FAIL(ob_datum_to_ob_time_without_date(*time_arg,
expr.args_[1]->datum_meta_.type_,
get_timezone_info(ctx.exec_ctx_.get_my_session()),
ot2))) {
} else if (OB_FAIL(ob_datum_to_ob_time_without_date(*time_arg, expr.args_[1]->datum_meta_.type_,
get_timezone_info(ctx.exec_ctx_.get_my_session()), ot2))) {
LOG_WARN("cast the second param failed", K(ret));
expr_datum.set_null();
null_res = true;
@ -595,25 +517,26 @@ int ObExprSubtime::subtime_common(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& e
return ret;
}
int ObExprSubtime::subtime_datetime(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprSubAddtime::subaddtime_datetime(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
int ret = OB_SUCCESS;
ObDatum* date_arg = NULL;
ObDatum* time_arg = NULL;
ObDatum *date_arg = NULL;
ObDatum *time_arg = NULL;
int64_t t_val1 = 0;
int64_t t_val2 = 0;
bool null_res = false;
const ObSQLSessionInfo* session = ctx.exec_ctx_.get_my_session();
const ObTimeZoneInfo* tz_info = NULL;
if (OB_FAIL(subtime_common(expr, ctx, expr_datum, null_res, date_arg, time_arg, t_val2))) {
LOG_WARN("calc subtime failed", K(ret));
const ObSQLSessionInfo *session = ctx.exec_ctx_.get_my_session();
const ObTimeZoneInfo *tz_info = NULL;
if (OB_FAIL(subaddtime_common(expr, ctx, expr_datum, null_res, date_arg, time_arg, t_val2))) {
LOG_WARN("calc subaddtime failed", K(ret));
} else if (OB_ISNULL(tz_info = get_timezone_info(session))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("timezone info of session is null", K(ret));
} else if (!null_res) {
int64_t offset = ObTimestampType == expr.args_[0]->datum_meta_.type_ ? tz_info->get_offset() : 0;
int64_t offset = ObTimestampType == expr.args_[0]->datum_meta_.type_
? tz_info->get_offset() : 0;
t_val1 = date_arg->get_datetime() + offset * USECS_PER_SEC;
int64_t int_usec = t_val1 - t_val2;
int64_t int_usec = (expr.type_ == T_FUN_SYS_SUBTIME) ? t_val1 - t_val2 : t_val1 + t_val2;
if (ObTimeConverter::is_valid_datetime(int_usec)) {
expr_datum.set_datetime(int_usec);
} else {
@ -623,24 +546,25 @@ int ObExprSubtime::subtime_datetime(const ObExpr& expr, ObEvalCtx& ctx, ObDatum&
return ret;
}
int ObExprSubtime::subtime_varchar(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprSubAddtime::subaddtime_varchar(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
int ret = OB_SUCCESS;
ObDatum* date_arg = NULL;
ObDatum* time_arg = NULL;
ObDatum *date_arg = NULL;
ObDatum *time_arg = NULL;
int64_t t_val1 = 0;
int64_t t_val2 = 0;
bool null_res = false;
const ObSQLSessionInfo* session = ctx.exec_ctx_.get_my_session();
const ObTimeZoneInfo* tz_info = NULL;
if (OB_FAIL(subtime_common(expr, ctx, expr_datum, null_res, date_arg, time_arg, t_val2))) {
LOG_WARN("calc subtime failed", K(ret));
const ObSQLSessionInfo *session = ctx.exec_ctx_.get_my_session();
const ObTimeZoneInfo *tz_info = NULL;
if (OB_FAIL(subaddtime_common(expr, ctx, expr_datum, null_res, date_arg, time_arg, t_val2))) {
LOG_WARN("calc subaddtime failed", K(ret));
} else if (OB_ISNULL(tz_info = get_timezone_info(session))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("timezone info of session is null", K(ret));
} else if (!null_res) {
ObTime ot1(DT_TYPE_TIME);
if (OB_FAIL(ob_datum_to_ob_time_without_date(*date_arg, expr.args_[0]->datum_meta_.type_, tz_info, ot1))) {
if (OB_FAIL(ob_datum_to_ob_time_without_date(*date_arg, expr.args_[0]->datum_meta_.type_,
tz_info, ot1))) {
LOG_WARN("cast the first param failed", K(ret));
expr_datum.set_null();
} else {
@ -661,20 +585,21 @@ int ObExprSubtime::subtime_varchar(const ObExpr& expr, ObEvalCtx& ctx, ObDatum&
}
}
if (OB_SUCC(ret)) {
int64_t int_usec = t_val1 - t_val2;
int64_t int_usec = (expr.type_ == T_FUN_SYS_SUBTIME) ? t_val1 - t_val2 : t_val1 + t_val2;
const int64_t datetime_buf_len = DATETIME_MAX_LENGTH + 1;
char* buf = expr.get_str_res_mem(ctx, datetime_buf_len);
char *buf = expr.get_str_res_mem(ctx, datetime_buf_len);
int64_t pos = 0;
// compatiable with mysql. display last 6 digits if we have mill
//兼容mysql行为,有毫秒则显示6位小数,否则不显示
ObString format;
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory failed", K(ret));
} else if (param_with_date && OB_FAIL(ObTimeConverter::datetime_to_str(
int_usec, NULL, format, -1, buf, datetime_buf_len, pos, true))) {
} else if (param_with_date && OB_FAIL(ObTimeConverter::datetime_to_str(int_usec, NULL,
format, -1, buf,
datetime_buf_len, pos, true))) {
LOG_WARN("datetime to str failed", K(ret));
} else if (!param_with_date &&
OB_FAIL(ObTimeConverter::time_to_str(int_usec, -1, buf, datetime_buf_len, pos, true))) {
} else if (!param_with_date && OB_FAIL(ObTimeConverter::time_to_str(int_usec, -1, buf,
datetime_buf_len, pos, true))) {
LOG_WARN("time to str failed", K(ret));
} else {
expr_datum.ptr_ = buf;
@ -691,5 +616,35 @@ int ObExprSubtime::subtime_varchar(const ObExpr& expr, ObEvalCtx& ctx, ObDatum&
return ret;
}
} // namespace sql
} // namespace oceanbase
ObExprDayName::ObExprDayName(ObIAllocator &alloc)
: ObExprTimeBase(alloc, DT_WDAY, T_FUN_SYS_DAY_NAME, N_DAY_NAME) {};
ObExprDayName::~ObExprDayName() {}
int ObExprDayName::calc_dayname(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
int ret = OB_SUCCESS;
if (ObExprTimeBase::calc(expr, ctx, expr_datum, DT_WDAY, true)) {
LOG_WARN("dayname calc day of dayweek failed", K(ret));
}
return ret;
}
int ObExprDayName::calc_result_type1(ObExprResType &type,
ObExprResType &type1,
common::ObExprTypeCtx &type_ctx) const
{
ObCollationType cs_type = type_ctx.get_coll_type();
type.set_varchar();
type.set_collation_type(cs_type);
type.set_collation_level(CS_LEVEL_IMPLICIT);
common::ObObjTypeClass tc1 = ob_obj_type_class(type1.get_type());
if (ob_is_enumset_tc(type1.get_type())) {
type1.set_calc_type(common::ObVarcharType);
} else if ((common::ObFloatTC == tc1) || (common::ObDoubleTC == tc1)) {
type1.set_calc_type(common::ObIntType);
}
return OB_SUCCESS;
}
} //namespace sql
} //namespace oceanbase