diff --git a/deps/oblib/src/lib/json_type/ob_json_base.cpp b/deps/oblib/src/lib/json_type/ob_json_base.cpp index bcd3b31eb9..5dbd8d388c 100644 --- a/deps/oblib/src/lib/json_type/ob_json_base.cpp +++ b/deps/oblib/src/lib/json_type/ob_json_base.cpp @@ -5392,7 +5392,12 @@ int ObIJsonBase::to_datetime(int64_t &value, ObTimeConvertCtx *cvrt_ctx_t) const { INIT_SUCC(ret); int64_t datetime; - + ObTimeConvertCtx cvrt_ctx(NULL, false); + if (OB_NOT_NULL(cvrt_ctx_t) && (lib::is_oracle_mode() || cvrt_ctx_t->is_timestamp_)) { + cvrt_ctx.tz_info_ = cvrt_ctx_t->tz_info_; + cvrt_ctx.oracle_nls_format_ = cvrt_ctx_t->oracle_nls_format_; + cvrt_ctx.is_timestamp_ = cvrt_ctx_t->is_timestamp_; + } switch (json_type()) { case ObJsonNodeType::J_INT: case ObJsonNodeType::J_OINT: { @@ -5421,10 +5426,6 @@ int ObIJsonBase::to_datetime(int64_t &value, ObTimeConvertCtx *cvrt_ctx_t) const case ObJsonNodeType::J_OTIMESTAMP: case ObJsonNodeType::J_OTIMESTAMPTZ: { ObTime t; - ObTimeConvertCtx cvrt_ctx(NULL, false); - if (lib::is_oracle_mode() && !OB_ISNULL(cvrt_ctx_t)) { - ObTimeConvertCtx cvrt_ctx(cvrt_ctx_t->tz_info_, cvrt_ctx_t->oracle_nls_format_, false); - } if (OB_FAIL(get_obtime(t))) { LOG_WARN("fail to get json obtime", K(ret)); } else if (OB_FAIL(ObTimeConverter::ob_time_to_datetime(t, cvrt_ctx, datetime))) { @@ -5444,16 +5445,12 @@ int ObIJsonBase::to_datetime(int64_t &value, ObTimeConvertCtx *cvrt_ctx_t) const LOG_WARN("data is null", K(ret)); } else { ObString str = str_data.string(); - ObTimeConvertCtx cvrt_ctx(NULL, false); if (lib::is_oracle_mode() && OB_NOT_NULL(cvrt_ctx_t)) { - ObTimeConvertCtx cvrt_ctx(cvrt_ctx_t->tz_info_, cvrt_ctx_t->oracle_nls_format_, false); if (OB_FAIL(ObTimeConverter::str_to_date_oracle(str, cvrt_ctx, datetime))) { LOG_WARN("oracle fail to cast string to date", K(ret), K(str)); } - } else { - if (OB_FAIL(ObTimeConverter::str_to_datetime(str, cvrt_ctx, datetime))) { - LOG_WARN("fail to cast string to datetime", K(ret), K(str)); - } + } else if (OB_FAIL(ObTimeConverter::str_to_datetime(str, cvrt_ctx, datetime))) { + LOG_WARN("fail to cast string to datetime", K(ret), K(str)); } } break; @@ -5466,16 +5463,12 @@ int ObIJsonBase::to_datetime(int64_t &value, ObTimeConvertCtx *cvrt_ctx_t) const LOG_WARN("data is null", K(ret)); } else { ObString str(static_cast(length), static_cast(length), data); - ObTimeConvertCtx cvrt_ctx(NULL, false); if (lib::is_oracle_mode() && OB_NOT_NULL(cvrt_ctx_t)) { - ObTimeConvertCtx cvrt_ctx(cvrt_ctx_t->tz_info_, cvrt_ctx_t->oracle_nls_format_, false); if (OB_FAIL(ObTimeConverter::str_to_date_oracle(str, cvrt_ctx, datetime))) { LOG_WARN("oracle fail to cast string to date", K(ret), K(str)); } - } else { - if (OB_FAIL(ObTimeConverter::str_to_datetime(str, cvrt_ctx, datetime))) { - LOG_WARN("fail to cast string to datetime", K(ret), K(str)); - } + } else if (OB_FAIL(ObTimeConverter::str_to_datetime(str, cvrt_ctx, datetime))) { + LOG_WARN("fail to cast string to datetime", K(ret), K(str)); } } break; diff --git a/src/share/object/ob_obj_cast.cpp b/src/share/object/ob_obj_cast.cpp index 5d653614fe..b0aff8d6a3 100644 --- a/src/share/object/ob_obj_cast.cpp +++ b/src/share/object/ob_obj_cast.cpp @@ -8385,6 +8385,7 @@ static int json_datetime(const ObObjType expect_type, ObObjCastParams ¶ms, } else { int64_t value; ObString j_bin_str = in.get_string(); + ObTimeConvertCtx cvrt_ctx(params.dtc_params_.tz_info_, ObTimestampType == expect_type); if (OB_FAIL(sql::ObTextStringHelper::read_real_string_data(params.allocator_v2_, in, j_bin_str))) { LOG_WARN("fail to get real data.", K(ret), K(j_bin_str)); } else { @@ -8392,7 +8393,7 @@ static int json_datetime(const ObObjType expect_type, ObObjCastParams ¶ms, ObIJsonBase *j_base = &j_bin; if (OB_FAIL(j_bin.reset_iter())) { LOG_WARN("failed to reset json bin iter", K(ret), K(j_bin_str)); - } else if (CAST_FAIL(j_base->to_datetime(value))) { + } else if (CAST_FAIL(j_base->to_datetime(value, &cvrt_ctx))) { LOG_WARN("fail to cast json to other type", K(ret), K(j_bin_str), K(expect_type)); ret = OB_ERR_INVALID_JSON_VALUE_FOR_CAST; LOG_USER_ERROR(OB_ERR_INVALID_JSON_VALUE_FOR_CAST); diff --git a/src/sql/engine/basic/ob_json_table_op.cpp b/src/sql/engine/basic/ob_json_table_op.cpp index 22fd0f09c9..d05be914b7 100644 --- a/src/sql/engine/basic/ob_json_table_op.cpp +++ b/src/sql/engine/basic/ob_json_table_op.cpp @@ -591,7 +591,7 @@ int JtFuncHelpler::cast_to_otimstamp(ObIJsonBase *j_base, INIT_SUCC(ret); int64_t val; - oceanbase::common::ObTimeConvertCtx cvrt_ctx(NULL, true); + oceanbase::common::ObTimeConvertCtx cvrt_ctx(NULL, dst_type == ObTimestampType); if (OB_ISNULL(session)) { ret = OB_ERR_UNEXPECTED; LOG_WARN("session is NULL", K(ret)); @@ -613,12 +613,7 @@ int JtFuncHelpler::cast_to_otimstamp(ObIJsonBase *j_base, } else if (OB_FAIL(j_base->to_datetime(val, &cvrt_ctx))) { LOG_WARN("wrapper to datetime failed.", K(ret), K(*j_base)); } else if (dst_type == ObTimestampType) { - int64_t t_out_val = 0; - ret = ObTimeConverter::datetime_to_timestamp(val, - cvrt_ctx.tz_info_, - t_out_val); - ret = OB_ERR_UNEXPECTED_TZ_TRANSITION == ret ? OB_INVALID_DATE_VALUE : ret; - out_val.time_us_ = t_out_val; + out_val.time_us_ = val; out_val.time_ctx_.tail_nsec_ = 0; } else { if (OB_FAIL(ObTimeConverter::odate_to_otimestamp(val, cvrt_ctx.tz_info_, dst_type, out_val))) { diff --git a/src/sql/engine/expr/ob_datum_cast.cpp b/src/sql/engine/expr/ob_datum_cast.cpp index a9e6ed3a2b..77241d5890 100644 --- a/src/sql/engine/expr/ob_datum_cast.cpp +++ b/src/sql/engine/expr/ob_datum_cast.cpp @@ -7881,27 +7881,31 @@ CAST_FUNC_NAME(json, datetime) { EVAL_STRING_ARG() { - int warning = OB_SUCCESS; - int64_t out_val; - ObObjType out_type = expr.datum_meta_.type_; - ObString j_bin_str = child_res->get_string(); - ObEvalCtx::TempAllocGuard tmp_alloc_g(ctx); - common::ObArenaAllocator &temp_allocator = tmp_alloc_g.get_allocator(); - if (OB_FAIL(ObTextStringHelper::read_real_string_data(temp_allocator, *child_res, - expr.args_[0]->datum_meta_, expr.args_[0]->obj_meta_.has_lob_header(), j_bin_str))) { - LOG_WARN("fail to get real data.", K(ret), K(j_bin_str)); - } else { - ObJsonBin j_bin(j_bin_str.ptr(), j_bin_str.length()); - ObIJsonBase *j_base = &j_bin; - - if (OB_FAIL(j_bin.reset_iter())) { - LOG_WARN("failed to reset json bin iter", K(ret), K(j_bin_str)); - } else if (CAST_FAIL(j_base->to_datetime(out_val))) { - LOG_WARN("fail to cast json to datetime type", K(ret), K(j_bin_str)); - ret = OB_ERR_INVALID_JSON_VALUE_FOR_CAST; - LOG_USER_ERROR(OB_ERR_INVALID_JSON_VALUE_FOR_CAST); + GET_SESSION() + { + int warning = OB_SUCCESS; + int64_t out_val; + ObObjType out_type = expr.datum_meta_.type_; + ObString j_bin_str = child_res->get_string(); + ObEvalCtx::TempAllocGuard tmp_alloc_g(ctx); + common::ObArenaAllocator &temp_allocator = tmp_alloc_g.get_allocator(); + ObTimeConvertCtx cvrt_ctx(session->get_timezone_info(), ObTimestampType == out_type); + if (OB_FAIL(ObTextStringHelper::read_real_string_data(temp_allocator, *child_res, + expr.args_[0]->datum_meta_, expr.args_[0]->obj_meta_.has_lob_header(), j_bin_str))) { + LOG_WARN("fail to get real data.", K(ret), K(j_bin_str)); } else { - SET_RES_DATETIME(out_val); + ObJsonBin j_bin(j_bin_str.ptr(), j_bin_str.length()); + ObIJsonBase *j_base = &j_bin; + + if (OB_FAIL(j_bin.reset_iter())) { + LOG_WARN("failed to reset json bin iter", K(ret), K(j_bin_str)); + } else if (CAST_FAIL(j_base->to_datetime(out_val, &cvrt_ctx))) { + LOG_WARN("fail to cast json to datetime type", K(ret), K(j_bin_str)); + ret = OB_ERR_INVALID_JSON_VALUE_FOR_CAST; + LOG_USER_ERROR(OB_ERR_INVALID_JSON_VALUE_FOR_CAST); + } else { + SET_RES_DATETIME(out_val); + } } } } diff --git a/src/sql/engine/expr/ob_expr_json_value.cpp b/src/sql/engine/expr/ob_expr_json_value.cpp index 8600d03cd5..70ecdfff61 100644 --- a/src/sql/engine/expr/ob_expr_json_value.cpp +++ b/src/sql/engine/expr/ob_expr_json_value.cpp @@ -1198,7 +1198,7 @@ int ObExprJsonValue::cast_to_otimstamp(ObIJsonBase *j_base, INIT_SUCC(ret); int64_t val; - oceanbase::common::ObTimeConvertCtx cvrt_ctx(NULL, true); + oceanbase::common::ObTimeConvertCtx cvrt_ctx(NULL, dst_type == ObTimestampType); if (OB_ISNULL(session)) { ret = OB_ERR_UNEXPECTED; LOG_WARN("session is NULL", K(ret));