support last_day in mysql mode

This commit is contained in:
jg0
2021-07-27 21:39:15 +08:00
committed by wangzelin.wzl
parent 09edba0ff6
commit 74d0236609
45 changed files with 26893 additions and 25079 deletions

View File

@ -16,6 +16,8 @@
#include "sql/session/ob_sql_session_info.h"
#include "sql/engine/expr/ob_datum_cast.h"
#include "sql/engine/ob_exec_context.h"
#include "lib/ob_date_unit_type.h"
#include "sql/engine/expr/ob_expr_util.h"
namespace oceanbase {
using namespace common;
@ -185,5 +187,202 @@ int ObExprDateFormat::calc_date_format_invalid(const ObExpr& expr, ObEvalCtx& ct
return ret;
}
const char* ObExprGetFormat::FORMAT_STR[FORMAT_MAX] =
{
"EUR",
"INTERNAL",
"ISO",
"JIS",
"USA"
};
const char* ObExprGetFormat::DATE_FORMAT[FORMAT_MAX + 1] =
{
"%d.%m.%Y",
"%Y%m%d",
"%Y-%m-%d",
"%Y-%m-%d",
"%m.%d.%Y",
"invalid"
};
const char* ObExprGetFormat::TIME_FORMAT[FORMAT_MAX + 1] =
{
"%H.%i.%s",
"%H%i%s",
"%H:%i:%s",
"%H:%i:%s",
"%h:%i:%s %p",
"invalid"
};
const char* ObExprGetFormat::DATETIME_FORMAT[FORMAT_MAX + 1] =
{
"%Y-%m-%d %H.%i.%s",
"%Y%m%d%H%i%s",
"%Y-%m-%d %H:%i:%s",
"%Y-%m-%d %H:%i:%s",
"%Y-%m-%d %H.%i.%s",
"invalid"
};
ObExprGetFormat::ObExprGetFormat(ObIAllocator &alloc)
: ObStringExprOperator(alloc, T_FUN_SYS_GET_FORMAT, N_GET_FORMAT, 2)
{
}
ObExprGetFormat::~ObExprGetFormat()
{
}
inline int ObExprGetFormat::calc_result_type2(ObExprResType &type,
ObExprResType &unit,
ObExprResType &format,
common::ObExprTypeCtx &type_ctx) const
{
UNUSED(type_ctx);
UNUSED(unit);
int ret = common::OB_SUCCESS;
// common::ObCollationType collation_connection = common::CS_TYPE_INVALID;
type.set_varchar();
type.set_collation_type(type_ctx.get_coll_type());
type.set_collation_level(common::CS_LEVEL_COERCIBLE);
type.set_length(GET_FORMAT_MAX_LENGTH);
format.set_calc_type(ObVarcharType);
if (ObCharset::is_cs_nonascii(format.get_collation_type())) {
format.set_calc_collation_type(CS_TYPE_UTF8MB4_GENERAL_CI);
}
return ret;
}
int ObExprGetFormat::calc_result2(ObObj &result,
const ObObj &unit,
const ObObj &format,
ObExprCtx &expr_ctx) const
{
UNUSED(expr_ctx);
int ret = OB_SUCCESS;
if (OB_UNLIKELY(ObIntType != unit.get_type()
|| unit.get_int() < 0 || unit.get_int() >= GET_FORMAT_MAX)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("unexpected unit date type", K(ret), K(unit));
} else if (format.is_null()) {
result.set_null();
} else if (OB_UNLIKELY(ObVarcharType != format.get_type())) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("GET_FORMAT() expected a string as format argument");
} else {
Format fm = FORMAT_MAX;
ObGetFormatUnitType type = static_cast<ObGetFormatUnitType>(unit.get_int());
const ObString str = format.get_string();
for (int64_t i = 0; i < FORMAT_MAX; i++) {
if (0 == str.case_compare(FORMAT_STR[i])) {
fm = static_cast<Format>(i);
break;
}
}
if (OB_UNLIKELY(FORMAT_MAX == fm)) {
result.set_null();
} else {
const char *res_str = NULL;
if (GET_FORMAT_DATE == type) {
result.set_varchar(DATE_FORMAT[fm]);
} else if (GET_FORMAT_TIME == type) {
result.set_varchar(TIME_FORMAT[fm]);
} else if (GET_FORMAT_DATETIME == type) {
result.set_varchar(DATETIME_FORMAT[fm]);
}
if (OB_UNLIKELY(ObCharset::is_cs_nonascii(result_type_.get_collation_type()))
&& OB_FAIL(convert_result_collation(result_type_, result, expr_ctx.calc_buf_))) {
LOG_WARN("fail to convert result collation", K(ret));
} else {
result.set_collation(result_type_);
}
}
}
return ret;
}
int ObExprGetFormat::cg_expr(ObExprCGCtx &op_cg_ctx,
const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
int ret = OB_SUCCESS;
if (rt_expr.arg_cnt_ != 2) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("get_format expr should have two params", K(ret), K(rt_expr.arg_cnt_));
} else if (OB_ISNULL(rt_expr.args_) || OB_ISNULL(rt_expr.args_[0])
|| OB_ISNULL(rt_expr.args_[1])) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of get_format expr is null", K(ret), K(rt_expr.args_));
} else if (ObIntType != rt_expr.args_[0]->datum_meta_.type_
|| (ObVarcharType != rt_expr.args_[1]->datum_meta_.type_
&& ObNullType != rt_expr.args_[1]->datum_meta_.type_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument type", K(ret), K(rt_expr.args_[0]->datum_meta_),
K(rt_expr.args_[1]->datum_meta_));
} else {
rt_expr.eval_func_ = ObExprGetFormat::calc_get_format;
}
return ret;
}
int ObExprGetFormat::calc_get_format(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
int ret = OB_SUCCESS;
ObDatum *unit = NULL;
ObDatum *format = NULL;
if (OB_FAIL(expr.eval_param_value(ctx, unit, format))) {
LOG_WARN("calc param failed", K(ret));
} else if (OB_UNLIKELY(unit->is_null() || unit->get_int() < 0
|| unit->get_int() >= GET_FORMAT_MAX)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("unexpected unit unit type", K(ret));
} else if (format->is_null()) {
expr_datum.set_null();
} else {
Format fm = FORMAT_MAX;
ObGetFormatUnitType type = static_cast<ObGetFormatUnitType>(unit->get_int());
const ObString str = format->get_string();
for (int64_t i = 0; i < FORMAT_MAX; i++) {
if (0 == str.case_compare(FORMAT_STR[i])) {
fm = static_cast<Format>(i);
break;
}
}
if (OB_UNLIKELY(FORMAT_MAX == fm)) {
expr_datum.set_null();
} else {
const char *res_str = NULL;
const ObCollationType dest_cs_type = expr.datum_meta_.cs_type_;
if (GET_FORMAT_DATE == type) {
res_str = DATE_FORMAT[fm];
} else if (GET_FORMAT_TIME == type) {
res_str = TIME_FORMAT[fm];
} else if (GET_FORMAT_DATETIME == type) {
res_str = DATETIME_FORMAT[fm];
}
if (OB_LIKELY(!ObCharset::is_cs_nonascii(dest_cs_type))) {
expr_datum.set_string(res_str);
} else {
ObExprStrResAlloc out_alloc(expr, ctx);
ObString out;
if (OB_FAIL(ObExprUtil::convert_string_collation(ObString::make_string(res_str),
CS_TYPE_UTF8MB4_GENERAL_CI,
out,
expr.datum_meta_.cs_type_,
out_alloc))) {
LOG_WARN("convert string collation failed", K(ret));
} else {
expr_datum.set_string(out);
}
}
}
}
return ret;
}
} // namespace sql
} // namespace oceanbase
} // namespace oceanbase