diff --git a/src/share/object/ob_obj_cast.cpp b/src/share/object/ob_obj_cast.cpp index 2efffdcf29..1690f23e59 100644 --- a/src/share/object/ob_obj_cast.cpp +++ b/src/share/object/ob_obj_cast.cpp @@ -4350,6 +4350,28 @@ static int time_date(const ObObjType expect_type, ObObjCastParams ¶ms, return ret; } +static int time_year(const ObObjType expect_type, ObObjCastParams ¶ms, + const ObObj &in, ObObj &out, const ObCastMode cast_mode) +{ + int ret = OB_SUCCESS; + int64_t int_value = 0; + uint8_t value = 0; + if (OB_UNLIKELY(ObTimeTC != in.get_type_class() + || ObYearTC != ob_obj_type_class(expect_type))) { + ret = OB_ERR_UNEXPECTED; + LOG_ERROR("invalid input type", + K(ret), K(in), K(expect_type)); + } else if (OB_FAIL(ObTimeConverter::time_to_int(in.get_time(), int_value))) { + LOG_WARN("time to int failed", K(ret), K(in)); + } else if (CAST_FAIL(ObTimeConverter::int_to_year(int_value, value))) { + LOG_WARN("cast int to year failed", K(ret), K(int_value)); + } else { + SET_RES_YEAR(out); + } + SET_RES_ACCURACY(DEFAULT_SCALE_FOR_YEAR, DEFAULT_SCALE_FOR_INTEGER, DEFAULT_LENGTH_FOR_NUMERIC); + return ret; +} + static int time_string(const ObObjType expect_type, ObObjCastParams ¶ms, const ObObj &in, ObObj &out, const ObCastMode cast_mode) { @@ -4659,6 +4681,30 @@ static int year_date(const ObObjType expect_type, ObObjCastParams ¶ms, return ret; } +static int year_time(const ObObjType expect_type, ObObjCastParams ¶ms, + const ObObj &in, ObObj &out, const ObCastMode cast_mode) +{ + int ret = OB_SUCCESS; + if (OB_UNLIKELY(ObYearTC != in.get_type_class() + || ObTimeTC != ob_obj_type_class(expect_type))) { + ret = OB_ERR_UNEXPECTED; + LOG_ERROR("invalid input type", + K(ret), K(in), K(expect_type)); + } else { + int64_t value = 0; + int64_t year_int = 0; + if (OB_FAIL(ObTimeConverter::year_to_int(in.get_year(), year_int))) { + LOG_WARN("year_to_int failed", K(ret), K(in)); + } else if (CAST_FAIL(ObTimeConverter::int_to_time(year_int, value))) { + LOG_WARN("int to time failed", K(ret)); + } else { + SET_RES_TIME(out); + } + } + SET_RES_ACCURACY(DEFAULT_PRECISION_FOR_TEMPORAL, MIN_SCALE_FOR_TEMPORAL, DEFAULT_LENGTH_FOR_TEMPORAL); + return ret; +} + static int year_string(const ObObjType expect_type, ObObjCastParams ¶ms, const ObObj &in, ObObj &out, const ObCastMode cast_mode) { @@ -9355,7 +9401,7 @@ ObObjCastFunc OB_OBJ_CAST[ObMaxTC][ObMaxTC] = time_datetime,/*datetime*/ time_date,/*date*/ cast_identity,/*time*/ - cast_not_support,/*year*/ + time_year,/*year*/ time_string,/*string*/ cast_not_support,/*extend*/ cast_not_support,/*unknown*/ @@ -9382,7 +9428,7 @@ ObObjCastFunc OB_OBJ_CAST[ObMaxTC][ObMaxTC] = year_number,/*number*/ year_datetime,/*datetime*/ year_date,/*date*/ - cast_not_support,/*time*/ + year_time,/*time*/ cast_identity,/*year*/ year_string,/*string*/ cast_not_support,/*extend*/ diff --git a/src/sql/engine/expr/ob_datum_cast.cpp b/src/sql/engine/expr/ob_datum_cast.cpp index 1013fd9f1e..154d3e4374 100644 --- a/src/sql/engine/expr/ob_datum_cast.cpp +++ b/src/sql/engine/expr/ob_datum_cast.cpp @@ -5148,6 +5148,21 @@ CAST_FUNC_NAME(year, date) return ret; } +CAST_FUNC_NAME(year, time) +{ + EVAL_ARG() + { + int64_t year_int = 0; + uint8_t in_val = child_res->get_year(); + if (OB_FAIL(ObTimeConverter::year_to_int(in_val, year_int))) { + LOG_WARN("year_to_int failed", K(ret), K(in_val)); + } else if (OB_FAIL(common_int_time(expr, year_int, res_datum))) { + LOG_WARN("int to time failed", K(ret)); + } + } + return ret; +} + CAST_FUNC_NAME(year, bit) { EVAL_ARG() @@ -5972,6 +5987,25 @@ CAST_FUNC_NAME(time, date) return ret; } +CAST_FUNC_NAME(time, year) +{ + EVAL_ARG() + { + int warning = OB_SUCCESS; + int64_t in_val = child_res->get_time(); + int64_t int_val = 0; + uint8_t year_val = 0; + if (OB_FAIL(ObTimeConverter::time_to_int(in_val, int_val))) { + LOG_WARN("time_to_int failed", K(ret), K(in_val)); + } else if (CAST_FAIL(ObTimeConverter::int_to_year(int_val, year_val))) { + LOG_WARN("cast int to year failed", K(ret), K(int_val)); + } else { + SET_RES_YEAR(year_val); + } + } + return ret; +} + CAST_FUNC_NAME(time, string) { EVAL_ARG() @@ -11123,7 +11157,7 @@ ObExpr::EvalFunc OB_DATUM_CAST_MYSQL_IMPLICIT[ObMaxTC][ObMaxTC] = time_datetime,/*datetime*/ time_date,/*date*/ cast_eval_arg,/*time*/ - cast_not_support,/*year*/ + time_year,/*year*/ time_string,/*string*/ cast_not_support,/*extend*/ cast_not_support,/*unknown*/ @@ -11150,7 +11184,7 @@ ObExpr::EvalFunc OB_DATUM_CAST_MYSQL_IMPLICIT[ObMaxTC][ObMaxTC] = year_number,/*number*/ year_datetime,/*datetime*/ year_date,/*date*/ - cast_not_support,/*time*/ + year_time,/*time*/ cast_eval_arg,/*year*/ year_string,/*string*/ cast_not_support,/*extend*/