Fix bit type casting inconsistent with mysql

This commit is contained in:
br0
2021-10-13 19:48:33 +08:00
committed by wangzelin.wzl
parent 8dddff226c
commit 614d2970e8
4 changed files with 227 additions and 132 deletions

View File

@ -3359,28 +3359,41 @@ CAST_FUNC_NAME(bit, datetime)
{
GET_SESSION()
{
const int32_t BUF_LEN = (OB_MAX_BIT_LENGTH + 7) / 8;
int64_t pos = 0;
char buf[BUF_LEN] = {0};
uint64_t in_val = child_res->get_uint();
ObLengthSemantics length = expr.args_[0]->datum_meta_.length_semantics_;
if (OB_FAIL(bit_to_char_array(in_val, length, buf, BUF_LEN, pos))) {
LOG_WARN("fail to store val", K(buf), K(BUF_LEN), K(in_val), K(pos));
} else if (OB_ISNULL(session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
} else {
int warning = OB_SUCCESS;
ObObjType out_type = expr.datum_meta_.type_;
ObString str(pos, buf);
ObTimeConvertCtx cvrt_ctx(session->get_timezone_info(), ObTimestampType == out_type);
ObScale res_scale;
int64_t out_val = 0;
if (CAST_FAIL(ObTimeConverter::str_to_datetime(str, cvrt_ctx, out_val, &res_scale))) {
LOG_WARN("str_to_datetime failed", K(ret));
DEF_IN_OUT_VAL(uint64_t, int64_t, 0);
if (CM_IS_COLUMN_CONVERT(expr.extra_)) {
// if cast mode is column convert, using bit as int64 to do cast.
int64_t int64 = 0;
if (OB_FAIL(common_uint_int(expr, ObIntType, in_val, ctx, int64))) {
LOG_WARN("common_uint_int failed", K(ret));
} else if (OB_UNLIKELY(0 > int64)) {
ret = OB_INVALID_DATE_FORMAT;
LOG_WARN("invalid date", K(ret), K(int64));
} else {
SET_RES_DATETIME(out_val);
ObTimeConvertCtx cvrt_ctx(session->get_timezone_info(), ObTimestampType == expr.datum_meta_.type_);
if (CAST_FAIL(ObTimeConverter::int_to_datetime(int64, 0, cvrt_ctx, out_val))) {
LOG_WARN("int_datetime failed", K(ret), K(int64));
}
}
} else {
// using bit as char array to do cast.
const int32_t BUF_LEN = (OB_MAX_BIT_LENGTH + 7) / 8;
int64_t pos = 0;
char buf[BUF_LEN] = {0};
ObLengthSemantics length = expr.args_[0]->datum_meta_.length_semantics_;
if (OB_FAIL(bit_to_char_array(in_val, length, buf, BUF_LEN, pos))) {
LOG_WARN("fail to store val", K(buf), K(BUF_LEN), K(in_val), K(pos));
} else {
ObObjType out_type = expr.datum_meta_.type_;
ObString str(pos, buf);
ObTimeConvertCtx cvrt_ctx(session->get_timezone_info(), ObTimestampType == out_type);
ObScale res_scale;
if (CAST_FAIL(ObTimeConverter::str_to_datetime(str, cvrt_ctx, out_val, &res_scale))) {
LOG_WARN("str_to_datetime failed", K(ret));
}
}
}
if (OB_SUCC(ret)) {
SET_RES_DATETIME(out_val);
}
}
}
@ -3391,20 +3404,32 @@ CAST_FUNC_NAME(bit, date)
{
EVAL_ARG()
{
const int32_t BUF_LEN = (OB_MAX_BIT_LENGTH + 7) / 8;
int64_t pos = 0;
char buf[BUF_LEN] = {0};
DEF_IN_OUT_VAL(uint64_t, int32_t, 0);
ObLengthSemantics length = expr.args_[0]->datum_meta_.length_semantics_;
if (OB_FAIL(bit_to_char_array(in_val, length, buf, BUF_LEN, pos))) {
LOG_WARN("fail to store val", K(buf), K(BUF_LEN), K(in_val), K(pos));
} else {
ObString str(pos, buf);
if (CAST_FAIL(ObTimeConverter::str_to_date(str, out_val))) {
LOG_WARN("str_to_datetime failed", K(ret));
} else {
SET_RES_DATE(out_val);
if (CM_IS_COLUMN_CONVERT(expr.extra_)) {
// if cast mode is column convert, using bit as int64 to do cast.
int64_t int64 = 0;
if (OB_FAIL(common_uint_int(expr, ObIntType, in_val, ctx, int64))) {
LOG_WARN("common_uint_int failed", K(ret));
} else if (CAST_FAIL(ObTimeConverter::int_to_date(int64, out_val))) {
LOG_WARN("int_to_date failed", K(ret), K(int64), K(out_val));
}
} else {
// using bit as char array to do cast.
const int32_t BUF_LEN = (OB_MAX_BIT_LENGTH + 7) / 8;
int64_t pos = 0;
char buf[BUF_LEN] = {0};
ObLengthSemantics length = expr.args_[0]->datum_meta_.length_semantics_;
if (OB_FAIL(bit_to_char_array(in_val, length, buf, BUF_LEN, pos))) {
LOG_WARN("fail to store val", K(buf), K(BUF_LEN), K(in_val), K(pos));
} else {
ObString str(pos, buf);
if (CAST_FAIL(ObTimeConverter::str_to_date(str, out_val))) {
LOG_WARN("str_to_datetime failed", K(ret));
}
}
}
if (OB_SUCC(ret)) {
SET_RES_DATE(out_val);
}
}
return ret;
@ -3414,20 +3439,31 @@ CAST_FUNC_NAME(bit, time)
{
EVAL_ARG()
{
const int32_t BUF_LEN = (OB_MAX_BIT_LENGTH + 7) / 8;
int64_t pos = 0;
char buf[BUF_LEN] = {0};
DEF_IN_OUT_VAL(uint64_t, int64_t, 0);
ObLengthSemantics length = expr.args_[0]->datum_meta_.length_semantics_;
if (OB_FAIL(bit_to_char_array(in_val, length, buf, BUF_LEN, pos))) {
LOG_WARN("fail to store val", K(buf), K(BUF_LEN), K(in_val), K(pos));
if (CM_IS_COLUMN_CONVERT(expr.extra_)) {
// if cast mode is column convert, using bit as int64 to do cast.
int64_t int64 = 0;
if (OB_FAIL(common_uint_int(expr, ObIntType, in_val, ctx, int64))) {
LOG_WARN("common_uint_int failed", K(ret), K(in_val));
} else if (OB_FAIL(common_int_time(expr, int64, res_datum))) {
LOG_WARN("common_int_time failed", K(ret), K(out_val));
}
} else {
ObString str(pos, buf);
ObScale res_scale;
if (CAST_FAIL(ObTimeConverter::str_to_time(str, out_val, &res_scale))) {
LOG_WARN("str_to_datetime failed", K(ret));
// using bit as char array to do cast.
const int32_t BUF_LEN = (OB_MAX_BIT_LENGTH + 7) / 8;
int64_t pos = 0;
char buf[BUF_LEN] = {0};
ObLengthSemantics length = expr.args_[0]->datum_meta_.length_semantics_;
if (OB_FAIL(bit_to_char_array(in_val, length, buf, BUF_LEN, pos))) {
LOG_WARN("fail to store val", K(buf), K(BUF_LEN), K(in_val), K(pos));
} else {
SET_RES_TIME(out_val);
ObString str(pos, buf);
ObScale res_scale;
if (CAST_FAIL(ObTimeConverter::str_to_time(str, out_val, &res_scale))) {
LOG_WARN("str_to_datetime failed", K(ret));
} else {
SET_RES_TIME(out_val);
}
}
}
}
@ -3438,20 +3474,13 @@ CAST_FUNC_NAME(bit, year)
{
EVAL_ARG()
{
DEF_IN_OUT_VAL(uint64_t, uint8_t, 0);
const int32_t BUF_LEN = (OB_MAX_BIT_LENGTH + 7) / 8;
int64_t pos = 0;
char buf[BUF_LEN] = {0};
ObLengthSemantics length = expr.args_[0]->datum_meta_.length_semantics_;
if (OB_FAIL(bit_to_char_array(in_val, length, buf, BUF_LEN, pos))) {
LOG_WARN("fail to store val", K(buf), K(BUF_LEN), K(in_val), K(pos));
} else {
ObString str(pos, buf);
if (CAST_FAIL(ObTimeConverter::str_to_year(str, out_val))) {
LOG_WARN("str_to_datetime failed", K(ret));
} else {
SET_RES_YEAR(out_val);
}
// same as uint to year
uint64_t in_val = child_res->get_uint();
int64_t out_val = 0;
if (OB_FAIL(common_uint_int(expr, ObIntType, in_val, ctx, out_val))) {
LOG_WARN("common_uint_int failed", K(ret), K(in_val));
} else if (OB_FAIL(common_int_year(expr, out_val, res_datum))) {
LOG_WARN("common_int_year failed", K(ret), K(out_val));
}
}
return ret;
@ -3461,17 +3490,26 @@ CAST_FUNC_NAME(bit, string)
{
EVAL_ARG()
{
const int32_t BUF_LEN = (OB_MAX_BIT_LENGTH + 7) / 8;
int64_t pos = 0;
char buf[BUF_LEN] = {0};
uint64_t in_val = child_res->get_uint();
ObLengthSemantics length = expr.args_[0]->datum_meta_.length_semantics_;
if (OB_FAIL(bit_to_char_array(in_val, length, buf, BUF_LEN, pos))) {
LOG_WARN("fail to store val", K(buf), K(BUF_LEN), K(in_val), K(pos));
if (CM_IS_COLUMN_CONVERT(expr.extra_)) {
// if cast mode is column convert, using bit as int64 to do cast.
ObFastFormatInt ffi(in_val);
if (OB_FAIL(common_copy_string_zf(expr, ObString(ffi.length(), ffi.ptr()), ctx, res_datum))) {
LOG_WARN("common_copy_string_zf failed", K(ret), K(ObString(ffi.length(), ffi.ptr())));
}
} else {
ObString str(pos, buf);
if (OB_FAIL(common_copy_string(expr, str, ctx, res_datum))) {
LOG_WARN("common_copy_string failed", K(ret));
// using bit as char array to do cast.
const int32_t BUF_LEN = (OB_MAX_BIT_LENGTH + 7) / 8;
int64_t pos = 0;
char buf[BUF_LEN] = {0};
ObLengthSemantics length = expr.args_[0]->datum_meta_.length_semantics_;
if (OB_FAIL(bit_to_char_array(in_val, length, buf, BUF_LEN, pos))) {
LOG_WARN("fail to store val", K(ret), K(in_val), K(length), K(buf), K(BUF_LEN), K(pos));
} else {
ObString str(pos, buf);
if (OB_FAIL(common_copy_string(expr, str, ctx, res_datum))) {
LOG_WARN("common_copy_string failed", K(ret));
}
}
}
}

View File

@ -202,6 +202,14 @@ int OB_INLINE ObExprOperator::cast_operand_type(
LOG_DEBUG(
"need cast operand", K(res_type), K(res_type.get_calc_meta().get_scale()), K(res_obj), K(res_obj.get_scale()));
ObCastMode cast_mode = get_cast_mode();
// In PAD expression, we need add COLUMN_CONVERT to cast mode when cast is
// from bit to binary and column convert is set in column_conv_ctx_. The COLUMN_CONVERT
// cast mode is used in bit_to_string to decide which cast way is appropriate.
if (OB_UNLIKELY(T_FUN_PAD == get_type() && ob_is_bit_tc(param_type) &&
ob_is_varbinary_type(calc_type, calc_collation_type) &&
CM_IS_COLUMN_CONVERT(expr_ctx.column_conv_ctx_.cast_mode_))) {
cast_mode |= CM_COLUMN_CONVERT;
}
if (ob_is_string_or_lob_type(res_type.get_calc_type()) && res_type.is_zerofill()) {
// For zerofilled string

View File

@ -390,7 +390,7 @@ int ObInfixExpression::eval(
const int64_t idx = item.get_column();
if (OB_LIKELY(idx >= 0 && OB_LIKELY(idx < row.count_) && OB_LIKELY(NULL != row.cells_))) {
stack[pos] = row.cells_[idx];
if (OB_UNLIKELY(ObBitType == stack[pos].get_type())) {
if (OB_UNLIKELY(ObBitType == stack[pos].get_type() && -1 != item.get_accuracy().get_precision())) {
// use object scale to store bit length
stack[pos].set_scale(item.get_accuracy().get_precision());
} else if (OB_UNLIKELY(ob_is_text_tc(stack[pos].get_type()))) {
@ -401,7 +401,7 @@ int ObInfixExpression::eval(
} else if (NULL != ctx.row2_ && NULL != ctx.row2_->cells_ && idx >= row.count_ &&
idx - row.count_ < ctx.row2_->count_) {
stack[pos] = ctx.row2_->cells_[idx - row.count_];
if (OB_UNLIKELY(ObBitType == stack[pos].get_type())) {
if (OB_UNLIKELY(ObBitType == stack[pos].get_type() && -1 != item.get_accuracy().get_precision())) {
// use object scale to store bit length
stack[pos].set_scale(item.get_accuracy().get_precision());
} else if (-1 != item.get_accuracy().get_scale()) {
@ -435,6 +435,12 @@ int ObInfixExpression::eval(
item.get_item_type(),
"type_name",
get_type_name(item.get_item_type()));
} else {
// For expression op, we need set scale info after it has been evaluated.
if (OB_UNLIKELY(ObBitType == stack[pos].get_type() && -1 != item.get_accuracy().get_precision())) {
// use object scale to store bit length
stack[pos].set_scale(item.get_accuracy().get_precision());
}
}
} else {
ret = OB_ERR_UNEXPECTED;