patch 4.0

This commit is contained in:
wangzelin.wzl
2022-10-24 10:34:53 +08:00
parent 4ad6e00ec3
commit 93a1074b0c
10533 changed files with 2588271 additions and 2299373 deletions

View File

@ -10,7 +10,7 @@
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL_ENG
#define USING_LOG_PREFIX SQL_ENG
#include "sql/engine/expr/ob_expr_date_add.h"
#include "lib/ob_date_unit_type.h"
@ -23,33 +23,41 @@
using namespace oceanbase::common;
using namespace oceanbase::sql;
ObExprDateAdjust::ObExprDateAdjust(
ObIAllocator& alloc, ObExprOperatorType type, const char* name, int32_t param_num, int32_t dimension)
ObExprDateAdjust::ObExprDateAdjust(ObIAllocator &alloc,
ObExprOperatorType type,
const char *name,
int32_t param_num,
int32_t dimension)
: ObFuncExprOperator(alloc, type, name, param_num, dimension)
{}
{
}
ObExprDateAdjust::~ObExprDateAdjust()
{}
{
}
int ObExprDateAdjust::calc_result_type3(ObExprResType& type, ObExprResType& date, ObExprResType& interval,
ObExprResType& unit, ObExprTypeCtx& type_ctx) const
int ObExprDateAdjust::calc_result_type3(ObExprResType &type,
ObExprResType &date,
ObExprResType &interval,
ObExprResType &unit,
ObExprTypeCtx &type_ctx) const
{
UNUSED(type_ctx);
UNUSED(interval);
// if date is ObDateType, and date_uint contains no hour / minute / second / microsecond,
// the result type should be date, not datetime or timestamp.
//if date is ObDateType, and date_uint contains no hour / minute / second / microsecond,
//the result type should be date, not datetime or timestamp.
// date_add('2012-2-29', interval '-1000000' microsecond) => 2012-02-28 23:59:59 .
// date_add('2012-2-29', interval '-100000' microsecond) => 2012-02-28 23:59:59.900000 .
// date_add('2012-2-29 00:00:00.100', interval '-100000' microsecond) => 2012-02-29 00:00:00 .
// so scale of literal datetime or date should be -1.
// literal -> cast to string -> is date or datetime format?
//literal -> cast to string -> is date or datetime format?
int ret = OB_SUCCESS;
ObDateUnitType unit_type = static_cast<ObDateUnitType>(unit.get_param().get_int());
ObExprResType res_date = date;
const ObSQLSessionInfo* session = static_cast<const ObSQLSessionInfo*>(type_ctx.get_session());
const ObSQLSessionInfo *session = static_cast<const ObSQLSessionInfo*>(type_ctx.get_session());
if (OB_ISNULL(session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is null", K(ret));
@ -57,15 +65,13 @@ int ObExprDateAdjust::calc_result_type3(ObExprResType& type, ObExprResType& date
type.set_null();
} else {
unit.set_calc_type(ObIntType);
bool use_static_engine = session->use_static_typing_engine();
if (ObDateTimeType == date.get_type() || ObTimestampType == date.get_type()) {
type.set_datetime();
if (use_static_engine) {
date.set_calc_type(ObDateTimeType);
}
date.set_calc_type(ObDateTimeType);
} else if (ObDateType == date.get_type()) {
if (DATE_UNIT_YEAR == unit_type || DATE_UNIT_MONTH == unit_type || DATE_UNIT_DAY == unit_type ||
DATE_UNIT_YEAR_MONTH == unit_type || DATE_UNIT_WEEK == unit_type || DATE_UNIT_QUARTER == unit_type) {
if (DATE_UNIT_YEAR == unit_type || DATE_UNIT_MONTH == unit_type
|| DATE_UNIT_DAY == unit_type || DATE_UNIT_YEAR_MONTH == unit_type
|| DATE_UNIT_WEEK == unit_type || DATE_UNIT_QUARTER == unit_type) {
type.set_date();
} else {
type.set_datetime();
@ -73,28 +79,25 @@ int ObExprDateAdjust::calc_result_type3(ObExprResType& type, ObExprResType& date
} else if (ObTimeType == date.get_type()) {
type.set_time();
} else {
if (use_static_engine) {
date.set_calc_type(ObVarcharType);
}
date.set_calc_type(ObVarcharType);
type.set_varchar();
type.set_length(DATETIME_MAX_LENGTH);
ret = aggregate_charsets_for_string_result(type, &date, 1, type_ctx.get_coll_type());
}
if (use_static_engine) {
interval.set_calc_type(ObVarcharType);
}
interval.set_calc_type(ObVarcharType);
if (OB_SUCC(ret)) {
if (share::is_oracle_mode()) {
if (lib::is_oracle_mode()) {
type.set_scale(static_cast<ObScale>(OB_MAX_DATE_PRECISION));
} else { // mysql mode
} else { // mysql mode
ObScale scale = 0;
if (DATE_UNIT_MICROSECOND == unit_type || DATE_UNIT_SECOND_MICROSECOND == unit_type ||
DATE_UNIT_MINUTE_MICROSECOND == unit_type || DATE_UNIT_HOUR_MICROSECOND == unit_type ||
DATE_UNIT_DAY_MICROSECOND == unit_type) {
if (DATE_UNIT_MICROSECOND == unit_type
|| DATE_UNIT_SECOND_MICROSECOND == unit_type || DATE_UNIT_MINUTE_MICROSECOND == unit_type
|| DATE_UNIT_HOUR_MICROSECOND == unit_type || DATE_UNIT_DAY_MICROSECOND == unit_type) {
scale = OB_MAX_DATETIME_PRECISION;
} else if (DATE_UNIT_SECOND == unit_type) {
if (0 <= interval.get_scale()) {
scale = std::min(static_cast<ObScale>(OB_MAX_DATETIME_PRECISION), interval.get_scale());
scale = std::min(static_cast<ObScale>(OB_MAX_DATETIME_PRECISION),
interval.get_scale());
} else {
scale = OB_MAX_DATETIME_PRECISION;
}
@ -102,7 +105,8 @@ int ObExprDateAdjust::calc_result_type3(ObExprResType& type, ObExprResType& date
if (date.is_datetime() || date.is_timestamp() || date.is_time()) {
scale = std::max(date.get_scale(), scale);
} else if (date.is_date()) {
if ((DATE_UNIT_DAY <= unit_type && unit_type <= DATE_UNIT_YEAR) || DATE_UNIT_YEAR_MONTH == unit_type) {
if ((DATE_UNIT_DAY <= unit_type && unit_type <= DATE_UNIT_YEAR)
|| DATE_UNIT_YEAR_MONTH == unit_type) {
scale = 0;
} else {
// do nothing
@ -115,191 +119,31 @@ int ObExprDateAdjust::calc_result_type3(ObExprResType& type, ObExprResType& date
}
}
}
// todo:
// todo(jiuren)/(yeti):
// for column :
// date_unit not has microsecond and no default scale but the result has mircrosecond, so the scale should be
// result_scale such as: select date_add(a, INTERVAL 0.1 second);
// date_unit not has microsecond and no default scale but the result has mircrosecond, so the scale should be result_scale
// such as: select date_add(a, INTERVAL 0.1 second);
return ret;
}
int ObExprDateAdjust::calc_result3(ObObj& result, const ObObj& date, const ObObj& interval, const ObObj& unit,
ObExprCtx& expr_ctx, bool is_add, const ObExprResType& res_type) const
{
/**
* TODO
* the performance of this function is not so good in some cases, like date is varchar and
* unit is year_month, because the calculation is based on ob_time. we will do three cast
* operations for date: varchar -> ob_time -> datetime -> ob_time, in which we actually need
* only one: varchar -> ob_time.
* the optimization should expose ob_time and related functions to date add operation,
* especially those with an ob_time struct as input, like ob_time_to_datetime, which is private
* now to avoid extra ob_time validation.
* so how to resolve this problem without exposing this private functions?
* remember we have another time library named ObTimeUtility? we can expose these functions
* to ObTimeUtility using inheritance and protected function, and keeps private to other classes.
* BUT!!! this optimization will take lots of work, so I have to do it in future, not NOW.
*/
int ret = OB_SUCCESS;
if (OB_ISNULL(expr_ctx.calc_buf_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("allocator is null", K(ret));
} else if (OB_UNLIKELY(date.is_null() || interval.is_null()) || ObNullType == res_type.get_type()) {
result.set_null();
} else {
int64_t dt_val = 0;
ObString interval_val;
int64_t unit_value = 0;
bool is_neg = false;
bool is_json = (ObJsonType == date.get_type());
number::ObNumber interval_num;
if (OB_SUCC(unit.get_int(unit_value))) {
ObDateUnitType unit_val = static_cast<ObDateUnitType>(unit_value);
int64_t res_dt_val = 0;
EXPR_DEFINE_CAST_CTX(expr_ctx, CM_NONE);
EXPR_GET_DATETIME_V2(date, dt_val);
EXPR_GET_VARCHAR_V2(interval, interval_val);
if (OB_SUCC(ret) && share::is_oracle_mode()) {
if (OB_FAIL(interval_num.from(interval_val.ptr(), interval_val.length(), *expr_ctx.calc_buf_))) {
ret = OB_ERR_INTERVAL_INVALID;
LOG_WARN("the interval is invalid in oracle mode", K(ret));
} else if (!interval_num.is_integer()) {
is_neg = interval_num.is_negative();
if (DATE_UNIT_SECOND != unit_val) {
ret = OB_ERR_INTERVAL_INVALID;
LOG_WARN("the interval is invalid in oracle mode", K(ret));
} else {
uint64_t sub_num = 1;
number::ObNumber tmp_sub_num;
// add a positive or minus a negative number, truncate decimal part.
if (OB_FAIL(interval_num.trunc(0))) {
LOG_WARN("trunc decimal failed", K(ret));
} else if ((is_add && is_neg)) {
if (OB_FAIL(tmp_sub_num.from(sub_num, *expr_ctx.calc_buf_))) {
LOG_WARN("fail to trans sub_num to tmp_sub_num", K(ret));
} else if (OB_FAIL(interval_num.sub(tmp_sub_num, interval_num, *expr_ctx.calc_buf_))) {
LOG_WARN("fail to sub tmp_sub_num", K(ret));
}
} else if (!is_add && !is_neg) {
if (OB_FAIL(tmp_sub_num.from(sub_num, *expr_ctx.calc_buf_))) {
LOG_WARN("fail to trans sub_num to tmp_sub_num", K(ret));
} else if (OB_FAIL(interval_num.add(tmp_sub_num, interval_num, *expr_ctx.calc_buf_))) {
LOG_WARN("fail to add tmp_sub_num", K(ret));
}
}
if (OB_SUCC(ret)) {
ObObj tmp_obj;
tmp_obj.set_number(interval_num);
EXPR_GET_VARCHAR_V2(tmp_obj, interval_val);
}
}
}
}
if (OB_SUCC(ret)) {
if (OB_UNLIKELY(ObTimeConverter::ZERO_DATETIME == dt_val)) {
result.set_null();
} else if (OB_FAIL(ObTimeConverter::date_adjust(dt_val, interval_val, unit_val, res_dt_val, is_add))) {
if (OB_UNLIKELY(OB_INVALID_DATE_VALUE == ret)) {
result.set_null();
ret = OB_SUCCESS;
}
} else if (res_type.is_date()) {
int32_t d_val = 0;
if (OB_FAIL(ObTimeConverter::datetime_to_date(res_dt_val, NULL, d_val))) {
LOG_WARN("failed to cast datetime to date ", K(res_dt_val), K(ret));
} else {
result.set_date(d_val);
}
} else if (res_type.is_time()) {
int64_t cur_date = 0;
cur_date = (dt_val / USECS_PER_DAY) * USECS_PER_DAY;
if (res_dt_val - cur_date > OB_MAX_TIME || res_dt_val - cur_date < -OB_MAX_TIME) {
result.set_null();
} else if (((unit_val >= DATE_UNIT_MONTH && unit_val <= DATE_UNIT_YEAR) ||
DATE_UNIT_YEAR_MONTH == unit_val) &&
res_dt_val != dt_val) {
result.set_null();
} else {
result.set_time(res_dt_val - cur_date);
}
} else if (res_type.is_datetime()) {
result.set_datetime(res_dt_val);
} else { // RETURN STRING TYPE
ObObj date_obj = date;
ObObj res_date;
bool has_set_value = false;
// cast in place
if (OB_FAIL(ObObjCaster::to_type(ObVarcharType, cast_ctx, date_obj, date_obj))) {
LOG_WARN("failed to cast to string", K(ret), K(date_obj));
} else {
if (is_json) { // json to string will add quote automatically, here should take off quote
ObString str = date_obj.get_string();
if (str.length() >= 3) { // 2 quote and content length >= 1
date_obj.set_string(date_obj.get_type(), str.ptr() + 1, str.length() - 1);
}
}
bool dt_flag = false;
int tmp_ret = OB_SUCCESS;
if (OB_SUCCESS != (tmp_ret = ObTimeConverter::str_is_date_format(date_obj.get_string(), dt_flag))) {
if (CM_IS_WARN_ON_FAIL(expr_ctx.cast_mode_) && OB_INVALID_DATE_FORMAT == tmp_ret) {
result.set_null();
has_set_value = true;
} else {
ret = tmp_ret;
LOG_WARN("failed to cast str to datetime format", K(ret));
}
} else {
if (dt_flag) {
res_date.set_type(ObDateType);
} else {
res_date.set_type(ObDateTimeType);
}
}
}
if (OB_SUCC(ret) && !has_set_value) {
if (res_date.is_date() &&
(DATE_UNIT_YEAR == unit_val || DATE_UNIT_MONTH == unit_val || DATE_UNIT_DAY == unit_val ||
DATE_UNIT_YEAR_MONTH == unit_val || DATE_UNIT_WEEK == unit_val || DATE_UNIT_QUARTER == unit_val)) {
int32_t d_val = 0;
if (OB_FAIL(ObTimeConverter::datetime_to_date(res_dt_val, NULL, d_val))) {
LOG_WARN("failed to cast datetime to date ", K(res_dt_val), K(ret));
} else {
result.set_date(d_val);
if (OB_FAIL(ObObjCaster::to_type(ObVarcharType, cast_ctx, result, result))) {
LOG_WARN("failed to cast object to ObVarcharType ", K(result), K(ret));
}
}
} else {
result.set_datetime(res_dt_val);
if (OB_FAIL(ObObjCaster::to_type(ObVarcharType, cast_ctx, result, result))) {
LOG_WARN("failed to cast object to ObVarcharType ", K(result), K(ret));
}
}
}
}
}
}
}
return ret;
}
int ObExprDateAdjust::calc_date_adjust(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum, bool is_add)
int ObExprDateAdjust::calc_date_adjust(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum,
bool is_add)
{
int ret = OB_SUCCESS;
const ObSQLSessionInfo* session = NULL;
const ObSQLSessionInfo *session = NULL;
const ObObjType res_type = expr.datum_meta_.type_;
const ObObjType date_type = expr.args_[0]->datum_meta_.type_;
ObIAllocator& tmp_alloc = ctx.get_reset_tmp_alloc();
ObDatum* date = NULL;
ObDatum* interval = NULL;
ObDatum* unit = NULL;
ObDatum *date = NULL;
ObDatum *interval = NULL;
ObDatum *unit = NULL;
bool is_json = (expr.args_[0]->args_ != NULL) && (expr.args_[0]->args_[0]->datum_meta_.type_ == ObJsonType);
if (OB_ISNULL(session = ctx.exec_ctx_.get_my_session())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is null", K(ret));
} else if (OB_FAIL(expr.eval_param_value(ctx, date, interval, unit))) {
LOG_WARN("eval param value failed");
} else if (OB_UNLIKELY(date->is_null() || interval->is_null()) || ObNullType == res_type) {
} else if (OB_UNLIKELY(date->is_null() || interval->is_null())
|| ObNullType == res_type) {
expr_datum.set_null();
} else {
int64_t dt_val = 0;
@ -312,18 +156,20 @@ int ObExprDateAdjust::calc_date_adjust(const ObExpr& expr, ObEvalCtx& ctx, ObDat
dt_val = date->get_datetime();
} else {
ObTime ob_time;
ObDateSqlMode date_sql_mode;
ObTimeConvertCtx cvrt_ctx(get_timezone_info(session), false);
date_sql_mode.init(session->get_sql_mode());
if (is_json) { // json to string will add quote automatically, here should take off quote
ObString str = date->get_string();
if (str.length() >= 3) { // 2 quote and content length >= 1
date->set_string(str.ptr() + 1, str.length() - 1);
}
}
if (OB_FAIL(ob_datum_to_ob_time_with_date(*date,
date_type,
get_timezone_info(session),
ob_time,
get_cur_time(ctx.exec_ctx_.get_physical_plan_ctx())))) {
if (OB_FAIL(ob_datum_to_ob_time_with_date(*date, date_type,
get_timezone_info(session),
ob_time,
get_cur_time(ctx.exec_ctx_.get_physical_plan_ctx()),
false, date_sql_mode))) {
uint64_t cast_mode = 0;
ObSQLUtils::get_default_cast_mode(session->get_stmt_type(), session, cast_mode);
if (CM_IS_WARN_ON_FAIL(cast_mode)) {
@ -339,9 +185,12 @@ int ObExprDateAdjust::calc_date_adjust(const ObExpr& expr, ObEvalCtx& ctx, ObDat
}
if (OB_SUCC(ret) && !has_set_value) {
ObDateSqlMode date_sql_mode;
date_sql_mode.init(session->get_sql_mode());
if (OB_UNLIKELY(ObTimeConverter::ZERO_DATETIME == dt_val)) {
expr_datum.set_null();
} else if (OB_FAIL(ObTimeConverter::date_adjust(dt_val, interval_val, unit_val, res_dt_val, is_add))) {
} else if (OB_FAIL(ObTimeConverter::date_adjust(dt_val, interval_val, unit_val, res_dt_val,
is_add, date_sql_mode))) {
if (OB_UNLIKELY(OB_INVALID_DATE_VALUE == ret)) {
expr_datum.set_null();
ret = OB_SUCCESS;
@ -358,19 +207,20 @@ int ObExprDateAdjust::calc_date_adjust(const ObExpr& expr, ObEvalCtx& ctx, ObDat
cur_date = (dt_val / USECS_PER_DAY) * USECS_PER_DAY;
if (res_dt_val - cur_date > OB_MAX_TIME || res_dt_val - cur_date < -OB_MAX_TIME) {
expr_datum.set_null();
} else if (((unit_val >= DATE_UNIT_MONTH && unit_val <= DATE_UNIT_YEAR) || DATE_UNIT_YEAR_MONTH == unit_val) &&
res_dt_val != dt_val) {
} else if (((unit_val >= DATE_UNIT_MONTH && unit_val <= DATE_UNIT_YEAR)
|| DATE_UNIT_YEAR_MONTH == unit_val) && res_dt_val != dt_val) {
expr_datum.set_null();
} else {
expr_datum.set_time(res_dt_val - cur_date);
}
} else if (ObDateTimeType == res_type) {
expr_datum.set_datetime(res_dt_val);
} else { // RETURN STRING TYPE
} else { //RETURN STRING TYPE
ObObjType res_date;
bool dt_flag = false;
int tmp_ret = OB_SUCCESS;
if (OB_SUCCESS != (tmp_ret = ObTimeConverter::str_is_date_format(date->get_string(), dt_flag))) {
if (OB_SUCCESS != (tmp_ret =
ObTimeConverter::str_is_date_format(date->get_string(), dt_flag))) {
uint64_t cast_mode = 0;
ObSQLUtils::get_default_cast_mode(session->get_stmt_type(), session, cast_mode);
if (CM_IS_WARN_ON_FAIL(cast_mode) && OB_INVALID_DATE_FORMAT == tmp_ret) {
@ -389,24 +239,25 @@ int ObExprDateAdjust::calc_date_adjust(const ObExpr& expr, ObEvalCtx& ctx, ObDat
}
if (OB_SUCC(ret) && !has_set_value) {
if (ObDateType == res_date &&
(DATE_UNIT_YEAR == unit_val || DATE_UNIT_MONTH == unit_val || DATE_UNIT_DAY == unit_val ||
DATE_UNIT_YEAR_MONTH == unit_val || DATE_UNIT_WEEK == unit_val || DATE_UNIT_QUARTER == unit_val)) {
if (ObDateType == res_date && (DATE_UNIT_YEAR == unit_val
|| DATE_UNIT_MONTH == unit_val
|| DATE_UNIT_DAY == unit_val || DATE_UNIT_YEAR_MONTH == unit_val
|| DATE_UNIT_WEEK == unit_val || DATE_UNIT_QUARTER == unit_val)) {
int32_t d_val = 0;
if (OB_FAIL(ObTimeConverter::datetime_to_date(res_dt_val, NULL, d_val))) {
LOG_WARN("failed to cast datetime to date ", K(res_dt_val), K(ret));
} else {
ObTime ob_time;
const int64_t date_buf_len = DATETIME_MAX_LENGTH + 1;
char* buf = expr.get_str_res_mem(ctx, date_buf_len);
char *buf = expr.get_str_res_mem(ctx, date_buf_len);
int64_t pos = 0;
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory failed", K(ret));
} else if (OB_FAIL(ObTimeConverter::date_to_ob_time(d_val, ob_time))) {
LOG_WARN("failed to convert days to ob time", K(ret));
} else if (OB_FAIL(
ObTimeConverter::ob_time_to_str(ob_time, DT_TYPE_DATE, 0, buf, date_buf_len, pos, true))) {
} else if (OB_FAIL(ObTimeConverter::ob_time_to_str(ob_time, DT_TYPE_DATE, 0, buf,
date_buf_len, pos, true))) {
LOG_WARN("failed to convert ob time to string", K(ret));
} else {
expr_datum.ptr_ = buf;
@ -415,14 +266,15 @@ int ObExprDateAdjust::calc_date_adjust(const ObExpr& expr, ObEvalCtx& ctx, ObDat
}
} else {
const int64_t datetime_buf_len = DATETIME_MAX_LENGTH + 1;
char* buf = expr.get_str_res_mem(ctx, datetime_buf_len);
char *buf = expr.get_str_res_mem(ctx, datetime_buf_len);
int64_t pos = 0;
ObString format;
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory failed", K(ret));
} else if (OB_FAIL(ObTimeConverter::datetime_to_str(
res_dt_val, NULL, format, -1, buf, datetime_buf_len, pos, true))) {
} else if (OB_FAIL(ObTimeConverter::datetime_to_str(res_dt_val, NULL,
format, -1, buf,
datetime_buf_len, pos, true))) {
LOG_WARN("failed to cast object to ObVarcharType ", K(ret));
} else {
expr_datum.ptr_ = buf;
@ -436,17 +288,11 @@ int ObExprDateAdjust::calc_date_adjust(const ObExpr& expr, ObEvalCtx& ctx, ObDat
return ret;
}
ObExprDateAdd::ObExprDateAdd(ObIAllocator& alloc)
ObExprDateAdd::ObExprDateAdd(ObIAllocator &alloc)
: ObExprDateAdjust(alloc, T_FUN_SYS_DATE_ADD, N_DATE_ADD, 3, NOT_ROW_DIMENSION)
{}
int ObExprDateAdd::calc_result3(
ObObj& result, const ObObj& date, const ObObj& interval, const ObObj& unit, ObExprCtx& expr_ctx) const
{
return ObExprDateAdjust::calc_result3(result, date, interval, unit, expr_ctx, true, result_type_);
}
int ObExprDateAdd::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprDateAdd::cg_expr(ObExprCGCtx &op_cg_ctx, const ObRawExpr &raw_expr, ObExpr &rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
@ -457,30 +303,28 @@ int ObExprDateAdd::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, Ob
} else if (OB_ISNULL(rt_expr.args_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of date_add expr is null", K(ret), K(rt_expr.args_));
} else if (OB_ISNULL(rt_expr.args_[0]) || OB_ISNULL(rt_expr.args_[1]) || OB_ISNULL(rt_expr.args_[2])) {
} else if (OB_ISNULL(rt_expr.args_[0]) ||
OB_ISNULL(rt_expr.args_[1]) ||
OB_ISNULL(rt_expr.args_[2])) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("child of date_add expr is null", K(ret), K(rt_expr.args_[0]), K(rt_expr.args_[1]), K(rt_expr.args_[2]));
LOG_WARN("child of date_add expr is null", K(ret), K(rt_expr.args_[0]),
K(rt_expr.args_[1]), K(rt_expr.args_[2]));
} else {
rt_expr.eval_func_ = ObExprDateAdd::calc_date_add;
}
return ret;
}
int ObExprDateAdd::calc_date_add(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprDateAdd::calc_date_add(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
return ObExprDateAdjust::calc_date_adjust(expr, ctx, expr_datum, true /* is_add */);
}
ObExprDateSub::ObExprDateSub(ObIAllocator& alloc)
ObExprDateSub::ObExprDateSub(ObIAllocator &alloc)
: ObExprDateAdjust(alloc, T_FUN_SYS_DATE_SUB, N_DATE_SUB, 3, NOT_ROW_DIMENSION)
{}
int ObExprDateSub::calc_result3(
ObObj& result, const ObObj& date, const ObObj& interval, const ObObj& unit, ObExprCtx& expr_ctx) const
{
return ObExprDateAdjust::calc_result3(result, date, interval, unit, expr_ctx, false, result_type_);
}
int ObExprDateSub::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprDateSub::cg_expr(ObExprCGCtx &op_cg_ctx, const ObRawExpr &raw_expr, ObExpr &rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
@ -491,26 +335,31 @@ int ObExprDateSub::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, Ob
} else if (OB_ISNULL(rt_expr.args_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of date_sub expr is null", K(ret), K(rt_expr.args_));
} else if (OB_ISNULL(rt_expr.args_[0]) || OB_ISNULL(rt_expr.args_[1]) || OB_ISNULL(rt_expr.args_[2])) {
} else if (OB_ISNULL(rt_expr.args_[0]) ||
OB_ISNULL(rt_expr.args_[1]) ||
OB_ISNULL(rt_expr.args_[2])) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("child of date_sub expr is null", K(ret), K(rt_expr.args_[0]), K(rt_expr.args_[1]), K(rt_expr.args_[2]));
LOG_WARN("child of date_sub expr is null", K(ret), K(rt_expr.args_[0]),
K(rt_expr.args_[1]), K(rt_expr.args_[2]));
} else {
rt_expr.eval_func_ = ObExprDateSub::calc_date_sub;
}
return ret;
}
int ObExprDateSub::calc_date_sub(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprDateSub::calc_date_sub(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
return ObExprDateAdjust::calc_date_adjust(expr, ctx, expr_datum, false /* is_add */);
}
ObExprAddMonths::ObExprAddMonths(ObIAllocator& alloc)
ObExprAddMonths::ObExprAddMonths(ObIAllocator &alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_ADD_MONTHS, N_ADD_MONTHS, 2, NOT_ROW_DIMENSION)
{}
int ObExprAddMonths::calc_result_type2(
ObExprResType& type, ObExprResType& type1, ObExprResType& type2, common::ObExprTypeCtx& type_ctx) const
int ObExprAddMonths::calc_result_type2(ObExprResType &type,
ObExprResType &type1,
ObExprResType &type2,
common::ObExprTypeCtx &type_ctx) const
{
int ret = OB_SUCCESS;
UNUSED(type_ctx);
@ -522,34 +371,9 @@ int ObExprAddMonths::calc_result_type2(
return ret;
}
int ObExprAddMonths::calc_result2(
common::ObObj& result, const common::ObObj& obj1, const common::ObObj& obj2, common::ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
UNUSED(expr_ctx);
int64_t ori_date_utc = 0;
number::ObNumber months;
int64_t months_int = 0;
int64_t res_date_utc = 0;
if (obj1.is_null_oracle() || obj2.is_null_oracle()) {
result.set_null();
} else if (OB_FAIL(obj1.get_datetime(ori_date_utc))) {
LOG_WARN("fail to get datetime", K(ret));
} else if (OB_FAIL(obj2.get_number(months))) {
LOG_WARN("fail to get number", K(ret));
} else if (OB_FAIL(months.extract_valid_int64_with_trunc(months_int))) {
LOG_WARN("fail to do round", K(ret));
} else if (OB_FAIL(ObTimeConverter::date_add_nmonth(ori_date_utc, months_int, res_date_utc, true))) {
LOG_WARN("fail to date add nmonth", K(ret));
} else {
result.set_datetime(res_date_utc);
result.set_scale(OB_MAX_DATE_PRECISION);
}
return ret;
}
int ObExprAddMonths::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprAddMonths::cg_expr(ObExprCGCtx &op_cg_ctx,
const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
@ -560,7 +384,8 @@ int ObExprAddMonths::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr,
} else if (OB_ISNULL(rt_expr.args_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of add_months expr is null", K(ret), K(rt_expr.args_));
} else if (OB_ISNULL(rt_expr.args_[0]) || OB_ISNULL(rt_expr.args_[1])) {
} else if (OB_ISNULL(rt_expr.args_[0]) ||
OB_ISNULL(rt_expr.args_[1])) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("child of add_months expr is null", K(ret), K(rt_expr.args_[0]), K(rt_expr.args_[1]));
} else {
@ -569,14 +394,14 @@ int ObExprAddMonths::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr,
return ret;
}
int ObExprAddMonths::calc_add_months(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprAddMonths::calc_add_months(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
int ret = OB_SUCCESS;
ObDatum* param1 = NULL;
ObDatum* param2 = NULL;
ObDatum *param1 = NULL;
ObDatum *param2 = NULL;
if (OB_FAIL(expr.args_[0]->eval(ctx, param1))) {
LOG_WARN("eval first param value failed");
} else if (param1->is_null()) {
} else if (param1->is_null()) { //短路计算
expr_datum.set_null();
} else if (OB_FAIL(expr.args_[1]->eval(ctx, param2))) {
LOG_WARN("eval second param value failed");
@ -590,7 +415,8 @@ int ObExprAddMonths::calc_add_months(const ObExpr& expr, ObEvalCtx& ctx, ObDatum
if (OB_FAIL(months.extract_valid_int64_with_trunc(months_int))) {
LOG_WARN("fail to do round", K(ret));
} else if (OB_FAIL(ObTimeConverter::date_add_nmonth(ori_date_utc, months_int, res_date_utc, true))) {
} else if (OB_FAIL(ObTimeConverter::date_add_nmonth(ori_date_utc, months_int,
res_date_utc, true))) {
LOG_WARN("fail to date add nmonth", K(ret));
} else {
expr_datum.set_datetime(res_date_utc);
@ -599,63 +425,38 @@ int ObExprAddMonths::calc_add_months(const ObExpr& expr, ObEvalCtx& ctx, ObDatum
return ret;
}
ObExprLastDay::ObExprLastDay(ObIAllocator& alloc)
ObExprLastDay::ObExprLastDay(ObIAllocator &alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_LAST_DAY, N_LAST_DAY, 1, NOT_ROW_DIMENSION)
{}
int ObExprLastDay::calc_result_type1(ObExprResType& type, ObExprResType& type1, common::ObExprTypeCtx& type_ctx) const
int ObExprLastDay::calc_result_type1(ObExprResType &type,
ObExprResType &type1,
common::ObExprTypeCtx &type_ctx) const
{
int ret = OB_SUCCESS;
UNUSED(type_ctx);
if (is_oracle_mode()) {
type.set_datetime();
} else {
type.set_date();
}
type.set_scale(OB_MAX_DATE_PRECISION);
type1.set_calc_type(ObDateTimeType);
type1.set_calc_scale(OB_MAX_DATETIME_PRECISION);
return ret;
}
int ObExprLastDay::calc_result1(common::ObObj& result, const common::ObObj& obj, common::ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
UNUSED(expr_ctx);
int64_t ori_date_utc = 0;
int64_t res_date_utc = 0;
const ObObjType res_type = is_oracle_mode() ? ObDateTimeType : ObDateType;
ObSQLSessionInfo *session = NULL;
if (OB_ISNULL(session = expr_ctx.my_session_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is null", K(ret));
} else if (obj.is_null()) {
result.set_null();
} else if (OB_FAIL(obj.get_datetime(ori_date_utc))) {
LOG_WARN("fail to get datetime", K(ret));
} else if (OB_FAIL(ObTimeConverter::calc_last_date_of_the_month(ori_date_utc, res_date_utc, res_type, false))) {
LOG_WARN("fail to calc last mday", K(ret), K(ori_date_utc), K(res_type));
if (!is_oracle_mode()) {
uint64_t cast_mode = 0;
ObSQLUtils::get_default_cast_mode(session->get_stmt_type(), session, cast_mode);
if (CM_IS_WARN_ON_FAIL(cast_mode)) {
result.set_null();
ret = OB_SUCCESS;
}
}
bool accept_input_type = type1.is_null()
|| ob_is_string_tc(type1.get_type())
|| type1.is_datetime()
|| type1.is_otimestamp_type()
|| is_mysql_mode();
if (OB_UNLIKELY(!accept_input_type)) {
ret = OB_ERR_INVALID_TYPE_FOR_OP;
LOG_WARN("inconsistent type", K(ret), K(type1));
} else {
if (is_oracle_mode()) {
result.set_datetime(res_date_utc);
type.set_datetime();
} else {
result.set_date(static_cast<int32_t>(res_date_utc));
type.set_date();
}
result.set_scale(OB_MAX_DATE_PRECISION);
type.set_scale(OB_MAX_DATE_PRECISION);
type1.set_calc_type(ObDateTimeType);
type1.set_calc_scale(OB_MAX_DATETIME_PRECISION);
}
return ret;
}
int ObExprLastDay::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprLastDay::cg_expr(ObExprCGCtx &op_cg_ctx, const ObRawExpr &raw_expr, ObExpr &rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
@ -672,7 +473,7 @@ int ObExprLastDay::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, Ob
return ret;
}
int ObExprLastDay::calc_last_day(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprLastDay::calc_last_day(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
int ret = OB_SUCCESS;
ObDatum *param1 = NULL;
@ -688,8 +489,10 @@ int ObExprLastDay::calc_last_day(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& ex
const ObObjType res_type = is_oracle_mode() ? ObDateTimeType : ObDateType;
int64_t ori_date_utc = param1->get_datetime();
int64_t res_date_utc = 0;
if (OB_FAIL(ObTimeConverter::calc_last_date_of_the_month(
ori_date_utc, res_date_utc, res_type, false))) {
ObDateSqlMode date_sql_mode;
date_sql_mode.init(session->get_sql_mode());
if (OB_FAIL(ObTimeConverter::calc_last_date_of_the_month(ori_date_utc, res_date_utc,
res_type, !is_no_zero_in_date(session->get_sql_mode()), date_sql_mode))) {
LOG_WARN("fail to calc last mday", K(ret), K(ori_date_utc), K(res_date_utc));
if (!is_oracle_mode()) {
uint64_t cast_mode = 0;
@ -711,47 +514,35 @@ int ObExprLastDay::calc_last_day(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& ex
return ret;
}
ObExprNextDay::ObExprNextDay(ObIAllocator& alloc)
ObExprNextDay::ObExprNextDay(ObIAllocator &alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_NEXT_DAY, N_NEXT_DAY, 2, NOT_ROW_DIMENSION)
{}
int ObExprNextDay::calc_result_type2(
ObExprResType& type, ObExprResType& type1, ObExprResType& type2, common::ObExprTypeCtx& type_ctx) const
int ObExprNextDay::calc_result_type2(ObExprResType &type,
ObExprResType &type1,
ObExprResType &type2,
common::ObExprTypeCtx &type_ctx) const
{
int ret = OB_SUCCESS;
UNUSED(type_ctx);
type.set_datetime();
type.set_scale(OB_MAX_DATE_PRECISION);
type1.set_calc_type(ObDateTimeType);
type1.set_calc_scale(OB_MAX_DATETIME_PRECISION);
type2.set_calc_type(ObVarcharType);
return ret;
}
int ObExprNextDay::calc_result2(
common::ObObj& result, const common::ObObj& obj1, const common::ObObj& obj2, common::ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
UNUSED(expr_ctx);
int64_t ori_date_utc = 0;
ObString wday_name;
int64_t res_date_utc = 0;
if (obj1.is_null_oracle() || obj2.is_null_oracle()) {
result.set_null();
} else if (OB_FAIL(obj1.get_datetime(ori_date_utc))) {
LOG_WARN("fail to get datetime", K(ret));
} else if (OB_FAIL(obj2.get_varchar(wday_name))) {
LOG_WARN("fail to get varchar", K(ret));
} else if (OB_FAIL(ObTimeConverter::calc_next_date_of_the_wday(ori_date_utc, wday_name.trim(), res_date_utc))) {
LOG_WARN("fail to calc next mday", K(ret));
bool accept_input_type = type1.is_null()
|| ob_is_string_tc(type1.get_type())
|| type1.is_datetime()
|| type1.is_otimestamp_type();
if (OB_UNLIKELY(!accept_input_type)) {
ret = OB_ERR_INVALID_TYPE_FOR_OP;
LOG_WARN("inconsistent type", K(ret), K(type1));
} else {
result.set_datetime(res_date_utc);
result.set_scale(OB_MAX_DATE_PRECISION);
type.set_datetime();
type.set_scale(OB_MAX_DATE_PRECISION);
type1.set_calc_type(ObDateTimeType);
type1.set_calc_scale(OB_MAX_DATETIME_PRECISION);
type2.set_calc_type(ObVarcharType);
}
return ret;
}
int ObExprNextDay::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprNextDay::cg_expr(ObExprCGCtx &op_cg_ctx, const ObRawExpr &raw_expr, ObExpr &rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
@ -762,7 +553,8 @@ int ObExprNextDay::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, Ob
} else if (OB_ISNULL(rt_expr.args_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of next_day expr is null", K(ret), K(rt_expr.args_));
} else if (OB_ISNULL(rt_expr.args_[0]) || OB_ISNULL(rt_expr.args_[1])) {
} else if (OB_ISNULL(rt_expr.args_[0]) ||
OB_ISNULL(rt_expr.args_[1])) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("child of next_day expr is null", K(ret), K(rt_expr.args_[0]), K(rt_expr.args_[1]));
} else {
@ -771,14 +563,14 @@ int ObExprNextDay::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, Ob
return ret;
}
int ObExprNextDay::calc_next_day(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
int ObExprNextDay::calc_next_day(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
int ret = OB_SUCCESS;
ObDatum* param1 = NULL;
ObDatum* param2 = NULL;
ObDatum *param1 = NULL;
ObDatum *param2 = NULL;
if (OB_FAIL(expr.args_[0]->eval(ctx, param1))) {
LOG_WARN("eval first param value failed");
} else if (param1->is_null()) {
} else if (param1->is_null()) { //短路计算
expr_datum.set_null();
} else if (OB_FAIL(expr.args_[1]->eval(ctx, param2))) {
LOG_WARN("eval second param value failed");
@ -789,7 +581,8 @@ int ObExprNextDay::calc_next_day(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& ex
ObString wday_name = param2->get_string();
int64_t res_date_utc = 0;
if (OB_FAIL(ObTimeConverter::calc_next_date_of_the_wday(ori_date_utc, wday_name.trim(), res_date_utc))) {
if (OB_FAIL(ObTimeConverter::calc_next_date_of_the_wday(ori_date_utc,
wday_name.trim(), res_date_utc))) {
LOG_WARN("fail to calc next mday", K(ret));
} else {
expr_datum.set_datetime(res_date_utc);