fix print type scale bug

This commit is contained in:
st0
2021-09-13 17:45:22 +08:00
committed by wangzelin.wzl
parent 65e00203c5
commit c19f7ea941
25 changed files with 22507 additions and 22248 deletions

View File

@ -77,14 +77,18 @@ int ObExprToSeconds::calc_result1(ObObj& result, const ObObj& obj, ObExprCtx& ex
const ObObj* p_obj = NULL;
EXPR_CAST_OBJ_V2(ObDateType, obj, p_obj);
if (OB_SUCC(ret)) {
if (OB_ISNULL(p_obj)) {
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))) {
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;
@ -392,88 +396,109 @@ int ObExprSubtime::calc_result_type2(
int ObExprSubtime::calc_result2(common::ObObj& result, const common::ObObj& date_arg, const common::ObObj& time_arg,
common::ObExprCtx& expr_ctx) const
{
ObTime ot;
ObTime ot2(DT_TYPE_TIME);
int ret = OB_SUCCESS;
int ret1 = OB_SUCCESS;
int ret2 = OB_SUCCESS;
int64_t t_val1 = 0;
int64_t t_val2 = 0;
bool is_string_with_time_fmt = false;
ObTime ot1(DT_TYPE_TIME);
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 (date_arg.get_type() == ObVarcharType) {
ret1 = ob_obj_to_ob_time_with_date(date_arg, get_timezone_info(expr_ctx.my_session_), ot);
if (OB_SUCCESS != ret1) {
ret2 = ob_obj_to_ob_time_without_date(date_arg, get_timezone_info(expr_ctx.my_session_), ot2);
is_string_with_time_fmt = true;
} else if (0 == ot.parts_[DT_YEAR] && 0 == ot.parts_[DT_MON] && 0 == ot.parts_[DT_MDAY]) {
is_string_with_time_fmt = true;
}
} else { /* do nothing */
}
if (OB_SUCCESS != ret1 && OB_SUCCESS != ret2) {
if (OB_UNLIKELY(date_arg.is_null() || time_arg.is_null())) {
result.set_null();
} else if (OB_ISNULL(expr_ctx.calc_buf_)) {
} else if (OB_ISNULL(expr_ctx.calc_buf_) || OB_ISNULL(session = expr_ctx.my_session_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("allocator is null", K(ret));
} else if (OB_UNLIKELY(date_arg.is_null() || time_arg.is_null())) {
result.set_null();
LOG_WARN("allocator or session is null", K(ret), K(expr_ctx.calc_buf_));
} else {
if (ObVarcharType == time_arg.get_type()) {
EXPR_GET_TIME_V2(time_arg, t_val2);
} else {
TYPE_CHECK(time_arg, ObTimeType);
t_val2 = time_arg.get_time();
if (ObVarcharType == result_type) {
if (OB_FAIL(ob_obj_to_ob_time_without_date(date_arg,
get_timezone_info(session), ot1))) {
LOG_WARN("obj to ob time without date failed", K(ret), K(date_arg));
} else {
if (0 == ot1.parts_[DT_YEAR] && 0 == ot1.parts_[DT_MON] && 0 == ot1.parts_[DT_MDAY]) {
param_with_date = false;
}
}
}
if (ObTimeType == date_arg.get_type() || is_string_with_time_fmt) { // a#, subtime(t1, t2)
if (ObVarcharType == date_arg.get_type()) {
EXPR_GET_TIME_V2(date_arg, t_val1);
if (OB_SUCC(ret)) {
if (ObTimeType == time_arg.get_type()) {
t_val2 = time_arg.get_time();
} else {
TYPE_CHECK(date_arg, ObTimeType);
t_val1 = date_arg.get_time();
ObTime ot2(DT_TYPE_TIME);
if (OB_FAIL(ob_obj_to_ob_time_without_date(time_arg, get_timezone_info(session), ot2))) {
LOG_WARN("cast the second param failed", K(ret));
} else {
t_val2 = ObTimeConverter::ob_time_to_time(ot2);
}
}
if (OB_SUCC(ret)) {
int64_t int_usec = t_val1 - t_val2;
// LOG_WARN("@songxin, ObExprSubtime#time-time", K(t_val1), K(t_val2), K(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_FAIL(ret)) {
} else if (ObTimeType == result_type || !param_with_date) { // a#, subtime(时间1, 时间2)
if (ObVarcharType == result_type) {
t_val1 = ObTimeConverter::ob_time_to_time(ot1);
if (IS_NEG_TIME(ot1.mode_)) {
t_val1 = -t_val1;
}
} else {
result.set_time(int_usec);
TYPE_CHECK(date_arg, ObTimeType);
t_val1 = date_arg.get_time();
}
}
} else { // b#, subtime(t1, t2)
if (ObVarcharType == date_arg.get_type()) {
EXPR_GET_DATETIME_V2(date_arg, t_val1);
} else {
TYPE_CHECK(date_arg, ObDateTimeType);
t_val1 = date_arg.get_datetime();
}
if (OB_SUCC(ret)) {
int64_t int_usec = t_val1 - t_val2;
// LOG_WARN("@songxin, ObExprSubtime#datetime-time", K(t_val1), K(t_val2), K(int_usec));
if (ObTimeConverter::is_valid_datetime(int_usec)) {
result.set_datetime(int_usec);
if (OB_SUCC(ret)) {
int64_t int_usec = 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;
} else {
LOG_WARN("time value is out of range", K(ret), K(int_usec));
}
} else {
result.set_time(int_usec);
}
}
} else { // b#, subtime(日期时间1, 时间2)
const ObTimeZoneInfo *tz_info = NULL;
if (OB_ISNULL(tz_info = get_timezone_info(expr_ctx.my_session_))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("tz info is null", K(ret));
} else if (ObVarcharType == result_type) {
ObTimeConvertCtx cvrt_ctx(tz_info, false);
if (OB_FAIL(ObTimeConverter::ob_time_to_datetime(ot1, cvrt_ctx, t_val1))) {
LOG_WARN("ob_time_to_datetime failed", K(ret));
}
} else {
result.set_null();
if (ObDateTimeType != date_arg.get_type() && ObTimestampType != date_arg.get_type()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid date arg type", K(ret));
} else {
int64_t offset = ObTimestampType == date_arg.get_type() ? tz_info->get_offset() : 0;
t_val1 = date_arg.get_datetime() + offset * USECS_PER_SEC;
}
}
if (OB_SUCC(ret)) {
int64_t int_usec = t_val1 - t_val2;
if (ObTimeConverter::is_valid_datetime(int_usec)) {
result.set_datetime(int_usec);
} else {
result.set_null();
}
}
}
if (OB_SUCC(ret) && ObVarcharType == result_type) {
if (OB_FAIL(ObObjCaster::to_type(ObVarcharType, cast_ctx, result, result))) {
LOG_WARN("failed to cast object to ObVarcharType ", K(result), K(ret));
}
}
}
if (OB_SUCC(ret) && date_arg.get_type() == ObVarcharType) {
if (OB_FAIL(ObObjCaster::to_type(ObVarcharType, cast_ctx, result, result))) {
LOG_WARN("failed to cast object to ObVarcharType ", K(result), K(ret));
if (OB_FAIL(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) && OB_ALLOCATE_MEMORY_FAILED != ret) {
ret = OB_SUCCESS;
result.set_null();
}
}
}
if (OB_FAIL(ret)) {
result.set_null();
ret = OB_SUCCESS;
}
return ret;
}