patch 4.0
This commit is contained in:
@ -19,20 +19,26 @@
|
||||
#include "sql/engine/ob_exec_context.h"
|
||||
#include "lib/timezone/ob_time_convert.h"
|
||||
|
||||
namespace oceanbase {
|
||||
namespace oceanbase
|
||||
{
|
||||
using namespace common;
|
||||
namespace sql {
|
||||
namespace sql
|
||||
{
|
||||
|
||||
const int64_t ORACLE_TZ_OFFSET_MAX_LENGTH = 7; //[+|-]TZH:TZM
|
||||
const int64_t ORACLE_TZ_OFFSET_MAX_LENGTH = 7;//[+|-]TZH:TZM
|
||||
|
||||
ObExprTzOffset::ObExprTzOffset(ObIAllocator& alloc)
|
||||
ObExprTzOffset::ObExprTzOffset(ObIAllocator &alloc)
|
||||
: ObFuncExprOperator(alloc, T_FUN_SYS_TZ_OFFSET, N_TZ_OFFSET, 1, NOT_ROW_DIMENSION)
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
int ObExprTzOffset::calc_result_type1(ObExprResType& type, ObExprResType& input, common::ObExprTypeCtx& type_ctx) const
|
||||
int ObExprTzOffset::calc_result_type1(ObExprResType &type,
|
||||
ObExprResType &input,
|
||||
common::ObExprTypeCtx &type_ctx) const
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
if (OB_UNLIKELY(!input.is_varchar_or_char() && !input.is_null())) {
|
||||
if (OB_UNLIKELY(!input.is_varchar_or_char()
|
||||
&& !input.is_null())) {
|
||||
ret = common::OB_ERR_INVALID_TYPE_FOR_ARGUMENT;
|
||||
|
||||
LOG_WARN("invalid type", K(input.get_type()));
|
||||
@ -53,81 +59,10 @@ int ObExprTzOffset::calc_result_type1(ObExprResType& type, ObExprResType& input,
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprTzOffset::calc_result1(common::ObObj& result, const common::ObObj& input, ObExprCtx& expr_ctx) const
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
if (OB_ISNULL(expr_ctx.my_session_) || OB_ISNULL(expr_ctx.calc_buf_)) {
|
||||
ret = OB_INVALID_ARGUMENT;
|
||||
LOG_WARN("argument is invalid_", K(expr_ctx.my_session_), K(ret));
|
||||
} else if (OB_UNLIKELY(input.is_null())) {
|
||||
result.set_null();
|
||||
} else if (OB_UNLIKELY(!input.is_varchar())) {
|
||||
ret = OB_ERR_UNEXPECTED;
|
||||
LOG_WARN("input is not varchar", K(input), K(ret));
|
||||
} else if (input.get_varchar().empty()) {
|
||||
result.set_null();
|
||||
} else {
|
||||
ObString input_string = input.get_varchar().trim();
|
||||
int32_t offset_sec = 0;
|
||||
int32_t offset_min = 0;
|
||||
int ret_more = OB_SUCCESS;
|
||||
|
||||
if (OB_FAIL(ObTimeConverter::str_to_offset(input_string, offset_sec, ret_more, is_oracle_mode(), true))) {
|
||||
if (ret != OB_ERR_UNKNOWN_TIME_ZONE) {
|
||||
LOG_WARN("fail to convert str_to_offset", K(input_string), K(ret));
|
||||
} else if (ret_more != OB_SUCCESS) {
|
||||
ret = ret_more;
|
||||
LOG_WARN("invalid time zone hour or minute", K(input_string), K(ret));
|
||||
}
|
||||
} else {
|
||||
offset_min = offset_sec / SECS_PER_MIN;
|
||||
}
|
||||
|
||||
if (OB_ERR_UNKNOWN_TIME_ZONE == ret) {
|
||||
ObTime ob_time(DT_TYPE_ORACLE_TTZ);
|
||||
MEMCPY(ob_time.tz_name_, input_string.ptr(), input_string.length());
|
||||
ob_time.tz_name_[input_string.length()] = '\0';
|
||||
ob_time.is_tz_name_valid_ = true;
|
||||
int64_t ts_value = ObTimeUtility::current_time();
|
||||
|
||||
ObTimeConvertCtx cvrt_ctx(TZ_INFO(expr_ctx.my_session_), true);
|
||||
ObOTimestampData tmp_ot_data;
|
||||
tmp_ot_data.time_ctx_.tz_desc_ = 0;
|
||||
tmp_ot_data.time_ctx_.store_tz_id_ = 0;
|
||||
tmp_ot_data.time_us_ = ObTimeUtility::current_time();
|
||||
|
||||
if (OB_FAIL(ObTimeConverter::otimestamp_to_ob_time(ObTimestampLTZType, tmp_ot_data, NULL, ob_time))) {
|
||||
LOG_WARN("failed to convert otimestamp_to_ob_time", K(ret));
|
||||
} else if (OB_FAIL(ObTimeConverter::str_to_tz_offset(cvrt_ctx, ob_time))) {
|
||||
LOG_WARN("failed to convert string to tz_offset", K(ret));
|
||||
} else {
|
||||
offset_min = ob_time.parts_[DT_OFFSET_MIN];
|
||||
LOG_DEBUG("finish str_to_tz_offset", K(ob_time), K(tmp_ot_data));
|
||||
}
|
||||
}
|
||||
|
||||
if (OB_SUCC(ret)) {
|
||||
char const_dbtimezone_str[ORACLE_TZ_OFFSET_MAX_LENGTH] = {0};
|
||||
char* res_ptr = NULL;
|
||||
int64_t pos = 0;
|
||||
const char* fmt_str = (offset_min < 0 ? "-%02d:%02d" : "+%02d:%02d");
|
||||
if (OB_ISNULL(res_ptr = static_cast<char*>(expr_ctx.calc_buf_->alloc(ORACLE_TZ_OFFSET_MAX_LENGTH)))) {
|
||||
ret = OB_ALLOCATE_MEMORY_FAILED;
|
||||
LOG_WARN("Failed to alloc", "size", ORACLE_TZ_OFFSET_MAX_LENGTH, K(ret));
|
||||
} else if (OB_FAIL(databuff_printf(
|
||||
res_ptr, ORACLE_TZ_OFFSET_MAX_LENGTH, pos, fmt_str, abs(offset_min) / 60, abs(offset_min) % 60))) {
|
||||
LOG_WARN("Failed to databuff_printf", K(offset_min), K(ret));
|
||||
} else {
|
||||
result.set_varchar(res_ptr, pos);
|
||||
result.set_collation(result_type_);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprTzOffset::eval_tz_offset_val(
|
||||
const ObString& in_tz_str, ObSQLSessionInfo& my_session, ObIAllocator& alloc, ObString& res_str)
|
||||
int ObExprTzOffset::eval_tz_offset_val(const ObString &in_tz_str,
|
||||
ObSQLSessionInfo &my_session,
|
||||
ObIAllocator &alloc,
|
||||
ObString &res_str)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
ObString trimed_tz_str = in_tz_str;
|
||||
@ -136,7 +71,8 @@ int ObExprTzOffset::eval_tz_offset_val(
|
||||
int32_t offset_min = 0;
|
||||
int ret_more = OB_SUCCESS;
|
||||
|
||||
if (OB_FAIL(ObTimeConverter::str_to_offset(trimed_tz_str, offset_sec, ret_more, is_oracle_mode(), true))) {
|
||||
if (OB_FAIL(ObTimeConverter::str_to_offset(trimed_tz_str, offset_sec, ret_more,
|
||||
is_oracle_mode(), true))) {
|
||||
if (ret != OB_ERR_UNKNOWN_TIME_ZONE) {
|
||||
LOG_WARN("fail to convert str_to_offset", K(trimed_tz_str), K(ret));
|
||||
} else if (ret_more != OB_SUCCESS) {
|
||||
@ -152,15 +88,14 @@ int ObExprTzOffset::eval_tz_offset_val(
|
||||
MEMCPY(ob_time.tz_name_, trimed_tz_str.ptr(), trimed_tz_str.length());
|
||||
ob_time.tz_name_[trimed_tz_str.length()] = '\0';
|
||||
ob_time.is_tz_name_valid_ = true;
|
||||
int64_t ts_value = ObTimeUtility::current_time();
|
||||
|
||||
ObTimeConvertCtx cvrt_ctx(TZ_INFO(&my_session), true);
|
||||
ObOTimestampData tmp_ot_data;
|
||||
tmp_ot_data.time_ctx_.tz_desc_ = 0;
|
||||
tmp_ot_data.time_ctx_.store_tz_id_ = 0;
|
||||
tmp_ot_data.time_us_ = ObTimeUtility::current_time();
|
||||
|
||||
if (OB_FAIL(ObTimeConverter::otimestamp_to_ob_time(ObTimestampLTZType, tmp_ot_data, NULL, ob_time))) {
|
||||
if (OB_FAIL(ObTimeConverter::otimestamp_to_ob_time(ObTimestampLTZType, tmp_ot_data,
|
||||
NULL, ob_time))) {
|
||||
LOG_WARN("failed to convert otimestamp_to_ob_time", K(ret));
|
||||
} else if (OB_FAIL(ObTimeConverter::str_to_tz_offset(cvrt_ctx, ob_time))) {
|
||||
LOG_WARN("failed to convert string to tz_offset", K(ret));
|
||||
@ -170,16 +105,18 @@ int ObExprTzOffset::eval_tz_offset_val(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (OB_SUCC(ret)) {
|
||||
char const_dbtimezone_str[ORACLE_TZ_OFFSET_MAX_LENGTH] = {0};
|
||||
char* res_ptr = NULL;
|
||||
char *res_ptr = NULL;
|
||||
int64_t pos = 0;
|
||||
const char* fmt_str = (offset_min < 0 ? "-%02d:%02d" : "+%02d:%02d");
|
||||
const char *fmt_str = (offset_min < 0 ? "-%02d:%02d" : "+%02d:%02d");
|
||||
// 即使alloc分配的空间比实际需要的多应该也没关系
|
||||
// ORACLE_TZ_OFFSET_MAX_LENGTH为7, 很小的值
|
||||
if (OB_ISNULL(res_ptr = static_cast<char*>(alloc.alloc(ORACLE_TZ_OFFSET_MAX_LENGTH)))) {
|
||||
ret = OB_ALLOCATE_MEMORY_FAILED;
|
||||
LOG_WARN("Failed to alloc", "size", ORACLE_TZ_OFFSET_MAX_LENGTH, K(ret));
|
||||
} else if (OB_FAIL(databuff_printf(
|
||||
res_ptr, ORACLE_TZ_OFFSET_MAX_LENGTH, pos, fmt_str, abs(offset_min) / 60, abs(offset_min) % 60))) {
|
||||
LOG_WARN("Failed to alloc", "size", ORACLE_TZ_OFFSET_MAX_LENGTH , K(ret));
|
||||
} else if (OB_FAIL(databuff_printf(res_ptr, ORACLE_TZ_OFFSET_MAX_LENGTH, pos, fmt_str,
|
||||
abs(offset_min) / 60, abs(offset_min) % 60))) {
|
||||
LOG_WARN("Failed to databuff_printf", K(offset_min), K(ret));
|
||||
} else {
|
||||
res_str.assign_ptr(res_ptr, pos);
|
||||
@ -188,27 +125,30 @@ int ObExprTzOffset::eval_tz_offset_val(
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprTzOffset::eval_tz_offset(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res)
|
||||
int ObExprTzOffset::eval_tz_offset(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
ObDatum* tz = NULL;
|
||||
ObDatum *tz = NULL;
|
||||
if (OB_FAIL(expr.eval_param_value(ctx, tz))) {
|
||||
LOG_WARN("eval param_value failed", K(ret));
|
||||
} else if (tz->is_null()) {
|
||||
res.set_null();
|
||||
} else {
|
||||
const ObString& tz_str = tz->get_string();
|
||||
const ObString &tz_str = tz->get_string();
|
||||
ObString res_str_utf8;
|
||||
ObString res_str;
|
||||
ObIAllocator& calc_alloc = ctx.get_reset_tmp_alloc();
|
||||
ObEvalCtx::TempAllocGuard alloc_guard(ctx);
|
||||
ObIAllocator &calc_alloc = alloc_guard.get_allocator();
|
||||
ObExprStrResAlloc res_alloc(expr, ctx);
|
||||
const ObCollationType out_cs_type = expr.datum_meta_.cs_type_;
|
||||
CK(OB_NOT_NULL(ctx.exec_ctx_.get_my_session()));
|
||||
if (OB_FAIL(ret)) {
|
||||
} else if (OB_FAIL(eval_tz_offset_val(tz_str, *ctx.exec_ctx_.get_my_session(), calc_alloc, res_str_utf8))) {
|
||||
} else if (OB_FAIL(eval_tz_offset_val(tz_str, *ctx.exec_ctx_.get_my_session(),
|
||||
calc_alloc, res_str_utf8))) {
|
||||
LOG_WARN("eval_tz_offset_val failed", K(ret), K(tz_str));
|
||||
} else if (OB_FAIL(ObExprUtil::convert_string_collation(
|
||||
res_str_utf8, ObCharset::get_system_collation(), res_str, out_cs_type, calc_alloc))) {
|
||||
res_str_utf8, ObCharset::get_system_collation(),
|
||||
res_str, out_cs_type, calc_alloc))) {
|
||||
LOG_WARN("convert string coll failed", K(ret), K(res_str_utf8), K(out_cs_type));
|
||||
} else if (ObExprUtil::deep_copy_str(res_str, res_str, res_alloc)) {
|
||||
LOG_WARN("copy str failed", K(ret));
|
||||
@ -219,17 +159,20 @@ int ObExprTzOffset::eval_tz_offset(const ObExpr& expr, ObEvalCtx& ctx, ObDatum&
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprTzOffset::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr, ObExpr& expr) const
|
||||
int ObExprTzOffset::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
|
||||
ObExpr &expr) const
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
UNUSED(expr_cg_ctx);
|
||||
UNUSED(raw_expr);
|
||||
CK(1 == expr.arg_cnt_);
|
||||
CK(OB_NOT_NULL(expr.args_) && OB_NOT_NULL(expr.args_[0]));
|
||||
CK(ob_is_varchar_or_char(expr.args_[0]->datum_meta_.type_, expr.args_[0]->datum_meta_.cs_type_));
|
||||
CK(ob_is_varchar_or_char(expr.datum_meta_.type_, expr.datum_meta_.cs_type_));
|
||||
CK(ob_is_varchar_or_char(expr.args_[0]->datum_meta_.type_,
|
||||
expr.args_[0]->datum_meta_.cs_type_));
|
||||
CK(ob_is_varchar_or_char(expr.datum_meta_.type_,
|
||||
expr.datum_meta_.cs_type_));
|
||||
OX(expr.eval_func_ = eval_tz_offset);
|
||||
return ret;
|
||||
}
|
||||
} // namespace sql
|
||||
} // namespace oceanbase
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user