ob_expr_nvl.cpp 18.7 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#define USING_LOG_PREFIX SQL_ENG
#include "sql/engine/expr/ob_expr_nvl.h"
#include "share/object/ob_obj_cast.h"
#include "sql/engine/expr/ob_expr_operator.h"
#include "sql/engine/expr/ob_expr_promotion_util.h"
#include "sql/session/ob_sql_session_info.h"
#include "sql/engine/expr/ob_expr_result_type_util.h"
W
wangzelin.wzl 已提交
20 21
#include "sql/engine/expr/ob_expr_is.h"
#include <math.h>
O
oceanbase-admin 已提交
22

W
wangzelin.wzl 已提交
23 24
namespace oceanbase
{
O
oceanbase-admin 已提交
25
using namespace common;
W
wangzelin.wzl 已提交
26 27
namespace sql
{
O
oceanbase-admin 已提交
28

W
wangzelin.wzl 已提交
29
int ObExprNvlUtil::calc_result_type(ObExprResType &type,
O
oceanbase-admin 已提交
30

W
wangzelin.wzl 已提交
31 32 33
                                     ObExprResType &type1,
                                     ObExprResType &type2,
                                     ObExprTypeCtx &type_ctx)
O
oceanbase-admin 已提交
34 35 36 37 38 39 40
{
  UNUSED(type_ctx);
  int ret = OB_SUCCESS;
  if (OB_FAIL(ObExprPromotionUtil::get_nvl_type(type, type1, type2))) {
    LOG_WARN("get nvl type failed", K(ret), K(type1), K(type2));
  } else if (OB_UNLIKELY(type.is_invalid())) {
    ret = OB_ERR_INVALID_TYPE_FOR_OP;
O
obdev 已提交
41
  } else if (ob_is_string_type(type.get_type())) {
O
oceanbase-admin 已提交
42 43
    ObCollationLevel res_cs_level = CS_LEVEL_INVALID;
    ObCollationType res_cs_type = CS_TYPE_INVALID;
W
wangzelin.wzl 已提交
44
    if (lib::is_oracle_mode()) {
O
oceanbase-admin 已提交
45 46 47 48 49 50 51
      res_cs_level = CS_LEVEL_IMPLICIT;
      if (type1.is_string_type()) {
        res_cs_type = type1.get_collation_type();
      } else {
        res_cs_type = type_ctx.get_session()->get_dtc_params().nls_collation_;
      }
    } else {
W
wangzelin.wzl 已提交
52 53 54
      if (OB_FAIL(ObCharset::aggregate_collation(type1.get_collation_level(), type1.get_collation_type(),
                                            type2.get_collation_level(), type2.get_collation_type(),
                                            res_cs_level, res_cs_type))) {
O
oceanbase-admin 已提交
55 56 57 58 59 60 61
        LOG_WARN("aggregate collation failed", K(ret), K(type1), K(type2));
      }
    }
    if (OB_SUCC(ret)) {
      type.set_collation_level(res_cs_level);
      type.set_collation_type(res_cs_type);
    }
O
obdev 已提交
62
  } else if (ob_is_raw(type.get_type())) {
O
oceanbase-admin 已提交
63 64
    type.set_collation_level(CS_LEVEL_NUMERIC);
    type.set_collation_type(CS_TYPE_BINARY);
O
obdev 已提交
65 66
  } else if (ob_is_json(type.get_type())) {
    type.set_collation_level(CS_LEVEL_IMPLICIT);
O
oceanbase-admin 已提交
67 68 69 70
  }
  if (OB_SUCC(ret)) {
    type.set_length(MAX(type1.get_length(), type2.get_length()));
    // flag. if both type1 and type2 are NULL, type is null
W
wangzelin.wzl 已提交
71 72
    if (type1.has_result_flag(NOT_NULL_FLAG) || type2.has_result_flag(NOT_NULL_FLAG)) {
      type.set_result_flag(NOT_NULL_FLAG);
O
oceanbase-admin 已提交
73 74 75 76 77
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
78 79
ObExprNvl::ObExprNvl(ObIAllocator &alloc)
 : ObFuncExprOperator(alloc, T_FUN_SYS_NVL, N_NVL, 2, NOT_ROW_DIMENSION)
O
oceanbase-admin 已提交
80 81 82 83 84
{}

ObExprNvl::~ObExprNvl()
{}

W
wangzelin.wzl 已提交
85
void set_calc_type(const ObExprResType &in_type, ObExprResType &out_type)
O
oceanbase-admin 已提交
86 87 88 89 90 91 92
{
  out_type.set_calc_meta(in_type.get_obj_meta());
  out_type.set_calc_accuracy(in_type.get_accuracy());
  out_type.set_calc_collation_type(in_type.get_collation_type());
  out_type.set_calc_collation_level(in_type.get_collation_level());
}

W
wangzelin.wzl 已提交
93 94 95 96
int ObExprNvl::calc_result_type2(ObExprResType &type,
                                 ObExprResType &type1,
                                 ObExprResType &type2,
                                 ObExprTypeCtx &type_ctx) const
O
oceanbase-admin 已提交
97 98 99 100 101 102 103 104 105 106 107
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(ObExprNvlUtil::calc_result_type(type, type1, type2, type_ctx))) {
    LOG_WARN("calc_result_type failed", K(ret), K(type1), K(type2));
  } else {
    // accuracy.
    if (type.get_type() == type1.get_type()) {
      type.set_accuracy(type1.get_accuracy());
    } else {
      type.set_accuracy(type2.get_accuracy());
    }
S
st0 已提交
108 109
    ObScale scale1 = type1.is_null() ? 0 : type1.get_scale();
    ObScale scale2 = type2.is_null() ? 0 : type2.get_scale();
O
oceanbase-admin 已提交
110 111 112 113 114
    if (-1 != scale1 && -1 != scale2) {
      type.set_scale(static_cast<ObScale>(max(scale1, scale2)));
    } else {
      type.set_scale(-1);
    }
W
wangzelin.wzl 已提交
115 116 117 118 119
    type.set_length(MAX(type1.get_length(), type2.get_length()));
    //对于 int 和uint64的混合类型,需要提升类型至decimal
    if (lib::is_mysql_mode()
        && (ObUInt64Type == type1.get_type() || ObUInt64Type == type2.get_type())
        && ObIntType == type.get_type()) {
O
oceanbase-admin 已提交
120 121 122 123 124 125
      type.set_type(ObNumberType);
      type.set_accuracy(ObAccuracy::DDL_DEFAULT_ACCURACY[ObIntType].get_accuracy());
    }
    // enumset.
    const bool type1_is_enumset = ob_is_enumset_tc(type1.get_type());
    const bool type2_is_enumset = ob_is_enumset_tc(type2.get_type());
W
wangzelin.wzl 已提交
126 127
    const ObSQLSessionInfo *session =
      dynamic_cast<const ObSQLSessionInfo*>(type_ctx.get_session());
O
oceanbase-admin 已提交
128 129 130 131 132 133 134 135 136 137 138 139
    if (OB_ISNULL(session)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("cast basic session to sql session failed", K(ret));
    } else if (type1_is_enumset || type2_is_enumset) {
      ObObjType calc_type = enumset_calc_types_[OBJ_TYPE_TO_CLASS[type.get_type()]];
      if (OB_UNLIKELY(ObMaxType == calc_type)) {
        ret = OB_ERR_UNEXPECTED;
        SQL_ENG_LOG(WARN, "invalid type of parameter ", K(type1), K(type2), K(ret));
      } else if (ObVarcharType == calc_type) {
        if (type1_is_enumset) {
          type1.set_calc_type(calc_type);
          type1.set_calc_collation_type(ObCharset::get_system_collation());
W
wangzelin.wzl 已提交
140
        } else {
O
oceanbase-admin 已提交
141 142 143 144
          set_calc_type(type, type1);
        }
        if (type2_is_enumset) {
          type2.set_calc_type(calc_type);
W
wangzelin.wzl 已提交
145
        } else {
O
oceanbase-admin 已提交
146 147
          set_calc_type(type, type2);
        }
W
wangzelin.wzl 已提交
148
      } else {
O
oceanbase-admin 已提交
149 150 151
        set_calc_type(type, type1);
        set_calc_type(type, type2);
      }
W
wangzelin.wzl 已提交
152
    } else {
O
oceanbase-admin 已提交
153 154 155 156 157 158 159 160
      set_calc_type(type, type1);
      set_calc_type(type, type2);
    }
  }
  return ret;
}


W
wangzelin.wzl 已提交
161 162
ObExprOracleNvl::ObExprOracleNvl(ObIAllocator &alloc)
 : ObFuncExprOperator(alloc, T_FUN_SYS_NVL, N_NVL, 2, NOT_ROW_DIMENSION)
O
oceanbase-admin 已提交
163 164 165 166 167
{}

ObExprOracleNvl::~ObExprOracleNvl()
{}

W
wangzelin.wzl 已提交
168 169 170 171
int ObExprOracleNvl::calc_result_type2(ObExprResType &type,
                                       ObExprResType &type1,
                                       ObExprResType &type2,
                                       ObExprTypeCtx &type_ctx) const
O
oceanbase-admin 已提交
172 173 174 175
{
  return calc_nvl_oralce_result_type(type, type1, type2, type_ctx);
}

W
wangzelin.wzl 已提交
176 177 178 179
int ObExprOracleNvl::calc_nvl_oralce_result_type(ObExprResType &type,
                                                 ObExprResType &type1,
                                                 ObExprResType &type2,
                                                 ObExprTypeCtx &type_ctx)
O
oceanbase-admin 已提交
180
{
W
wangzelin.wzl 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
 /*
  create table t1 (col_num1 decimal(10, 2), col_num2 decimal(10, 8),
                   col_str1 varchar(20 char), col_str2 varchar2(60 byte),
                   col_tz timestamp(3) with time zone,
                   col_ltz timestamp(6) with local time zone);
  create view tmp as
  select nvl(col_str1, col_num1) s1_n1,
         nvl(col_str1, col_num2) s1_n2,
         nvl(col_str2, col_num1) s2_n1,
         nvl(col_str2, col_num2) s2_n2,
         nvl(col_str1, col_str2) s1_s2,
         nvl(col_tz, col_ltz) tz_ltz,
         nvl(NULL, col_ltz) null_ltz
    from t1;
  desc tmp;
O
oceanbase-admin 已提交
196

W
wangzelin.wzl 已提交
197 198 199 200 201 202 203 204 205 206 207
  SQL> desc tmp;
   Name            Null?    Type
   --------------- -------- ----------------------------
   S1_N1                    VARCHAR2(40 CHAR)
   S1_N2                    VARCHAR2(40 CHAR)
   S2_N1                    VARCHAR2(60)
   S2_N2                    VARCHAR2(60)
   S1_S2                    VARCHAR2(80)
   TZ_LTZ                   TIMESTAMP(3) WITH TIME ZONE
   NULL_LTZ                 TIMESTAMP(6) WITH LOCAL TIME ZONE
  */
O
oceanbase-admin 已提交
208 209 210 211 212 213 214 215
  int ret = OB_SUCCESS;
  if (OB_FAIL(ObExprNvlUtil::calc_result_type(type, type1, type2, type_ctx))) {
    LOG_WARN("calc_result_type2 failed", K(ret), K(type1), K(type2));
  } else {
    if (type.is_oracle_decimal()) {
      // unknow precision and scale
      type.set_precision(PRECISION_UNKNOWN_YET);
      type.set_scale(ORA_NUMBER_SCALE_UNKNOWN_YET);
W
wangzelin.wzl 已提交
216 217 218
    } else if (type.is_otimestamp_type()
               || type.is_interval_ym()
               || type.is_interval_ds()) {
O
oceanbase-admin 已提交
219 220 221 222 223 224 225 226 227 228 229 230
      /*
       * for otimestamp:
       * if type is otimestamp, it must be one of these situations:
       * 1. type1 is otimestamp, or
       * 2. type1 is const NULL, and type2 is otimestamp.
       * but the index of NVL_TYPE_PROMOTION_ORACLE is type class, so it can't tell us which
       * otimestamp type should be used (timestamp / timestamp with time zone / timestamp with
       * loacl time zone), we must adjust here.
       * see the sql results above.
       *
       * the same case for interval type class
       */
W
wangzelin.wzl 已提交
231
      ObExprResType &res_type = !type1.is_null() ? type1 : type2;
O
oceanbase-admin 已提交
232 233 234
      type.set_type(res_type.get_type());
      type.set_accuracy(res_type.get_accuracy());
      type2.set_calc_accuracy(res_type.get_accuracy());
W
wangzelin.wzl 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247
    } else if (type.is_character_type()) {
      ObSEArray<ObExprResType*, 2, ObNullAllocator> params;
      // the result type and collation is only determined by type1 unless type1 is const NULL.
      if (!type1.is_null()) {
        OZ (params.push_back(&type1));
        OZ (aggregate_string_type_and_charset_oracle(*type_ctx.get_session(), params, type,
                                                    PREFER_VAR_LEN_CHAR));
        OZ (params.push_back(&type2));
      } else {
        OZ (params.push_back(&type2));
        OZ (aggregate_string_type_and_charset_oracle(*type_ctx.get_session(), params, type,
                                                    PREFER_VAR_LEN_CHAR));
        OZ (params.push_back(&type1));
O
oceanbase-admin 已提交
248
      }
W
wangzelin.wzl 已提交
249 250 251 252 253 254 255 256 257
      // the result length semantics is determined by all params
      OZ (aggregate_length_semantics_oracle(*type_ctx.get_session(), params, type));
      OZ (deduce_string_param_calc_type_and_charset(*type_ctx.get_session(), type, params));
      OX (type.set_length(MAX(type1.get_calc_length(), type2.get_calc_length())));
    } else if (lib::is_oracle_mode() && type.is_ext()) {
      CK (type1.get_udt_id() == type2.get_udt_id());
      OX (type.set_udt_id(type1.get_udt_id()));
    } else if (type.is_temporal_type()) {
      type.set_scale(0);
O
oceanbase-admin 已提交
258 259
    }
    /*
W
wangzelin.wzl 已提交
260
     * https://aone.alibaba-inc.com/issue/18763797
O
oceanbase-admin 已提交
261 262 263 264 265 266 267 268 269 270 271
     * select nvl(0, 'hello') from dual;
     * the sql above will return error: ORA-01722: invalid number.
     * we must execute necessary cast operation before we determine which obj should be returned,
     * so we call set_calc_meta() here to tell ObExprOperator::call() "cast all parameters for me".
     */
    type1.set_calc_meta(type);
    type2.set_calc_meta(type);
  }
  return ret;
}

W
wangzelin.wzl 已提交
272 273
ObExprNaNvl::ObExprNaNvl(ObIAllocator &alloc)
  : ObFuncExprOperator(alloc, T_FUN_SYS_NANVL, T_NANVL, 2, NOT_ROW_DIMENSION)
O
oceanbase-admin 已提交
274 275 276 277 278
{}

ObExprNaNvl::~ObExprNaNvl()
{}

W
wangzelin.wzl 已提交
279 280 281 282
int ObExprNaNvl::calc_result_type2(ObExprResType &type,
                                       ObExprResType &type1,
                                       ObExprResType &type2,
                                       ObExprTypeCtx &type_ctx) const
O
oceanbase-admin 已提交
283 284
{
  UNUSED(type_ctx);
W
wangzelin.wzl 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  int ret =  OB_SUCCESS;
  if (ob_is_lob_tc(type1.get_type()) || ob_is_oracle_datetime_tc(type1.get_type())
     || ob_is_rowid_tc(type1.get_type())) {
    ret = OB_ERR_INVALID_TYPE_FOR_OP;
    LOG_USER_ERROR(OB_ERR_INVALID_TYPE_FOR_OP, "NUMBER",
                  ob_obj_type_str(type1.get_type()));
    LOG_WARN("invalid type of parameter", K(ret), K(type1));
  } else if (ob_is_lob_tc(type2.get_type()) || ob_is_oracle_datetime_tc(type2.get_type())
            || ob_is_rowid_tc(type2.get_type())) {
    ret = OB_ERR_INVALID_TYPE_FOR_OP;
    LOG_USER_ERROR(OB_ERR_INVALID_TYPE_FOR_OP, "NUMBER",
                  ob_obj_type_str(type2.get_type()));
    LOG_WARN("invalid type of parameter", K(ret), K(type2));
  }
  if (OB_SUCC(ret)) {
    type.set_type(ObNumberType);
O
oceanbase-admin 已提交
301 302
    type.set_scale(ORA_NUMBER_SCALE_UNKNOWN_YET);
    type.set_precision(PRECISION_UNKNOWN_YET);
W
wangzelin.wzl 已提交
303 304 305 306 307 308 309
    if (ob_is_double_type(type1.get_type()) || ob_is_double_type(type2.get_type())) {
      type.set_type(ObDoubleType);
    } else if (ob_is_float_type(type1.get_type()) || ob_is_float_type(type2.get_type())) {
      type.set_type(ObFloatType);
    }
    type1.set_calc_type(type.get_type());
    type2.set_calc_type(type.get_type());
O
oceanbase-admin 已提交
310 311 312 313
  }
  return ret;
}

W
wangzelin.wzl 已提交
314 315
int ObExprNvlUtil::calc_nvl_expr(const ObExpr &expr, ObEvalCtx &ctx,
                                 ObDatum &res_datum)
O
oceanbase-admin 已提交
316 317 318
{
  int ret = OB_SUCCESS;
  // nvl(arg0, arg1)
W
wangzelin.wzl 已提交
319 320
  ObDatum *arg0 = NULL;
  ObDatum *arg1 = NULL;
O
oceanbase-admin 已提交
321 322 323 324 325 326 327 328 329 330 331

  if (OB_FAIL(expr.eval_param_value(ctx, arg0, arg1))) {
    LOG_WARN("eval args failed", K(ret));
  } else if (!(arg0->is_null())) {
    res_datum.set_datum(*arg0);
  } else {
    res_datum.set_datum(*arg1);
  }
  return ret;
}

W
wangzelin.wzl 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
int ObExprNvlUtil::calc_nvl_expr_batch(const ObExpr &expr,
                                      ObEvalCtx &ctx,
                                      const ObBitVector &skip,
                                      const int64_t batch_size) {
  LOG_DEBUG("eval nvl batch mode", K(batch_size));
  int ret = OB_SUCCESS;
  ObDatum* results = expr.locate_batch_datums(ctx);
  ObBitVector &eval_flags = expr.get_evaluated_flags(ctx);
  ObDatumVector args0;
  ObDatumVector args1;
  if (OB_FAIL(expr.eval_batch_param_value(ctx, skip, batch_size, args0,
                                          args1))) {
    LOG_WARN("eval batch args failed", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < batch_size; ++i) {
      if (skip.at(i) || eval_flags.at(i)) {
        continue;
      }
      eval_flags.set(i);
      ObDatum *arg0 = args0.at(i);
      ObDatum *arg1 = args1.at(i);
      if (!(arg0->is_null())) {
        results[i].set_datum(*arg0);
      } else {
        results[i].set_datum(*arg1);
      }
    }
  }

  return ret;
}

int ObExprNvlUtil::calc_nvl_expr2(const ObExpr &expr, ObEvalCtx &ctx,
                                  ObDatum &res_datum)
O
oceanbase-admin 已提交
366 367 368
{
  int ret = OB_SUCCESS;
  // nvl(arg0, arg1, arg2)
W
wangzelin.wzl 已提交
369 370 371
  ObDatum *arg0 = NULL;
  ObDatum *arg1 = NULL;
  ObDatum *arg2 = NULL;
O
oceanbase-admin 已提交
372 373 374 375 376 377 378 379 380 381 382

  if (OB_FAIL(expr.eval_param_value(ctx, arg0, arg1, arg2))) {
    LOG_WARN("eval args failed", K(ret));
  } else if (!(arg0->is_null())) {
    res_datum.set_datum(*arg1);
  } else {
    res_datum.set_datum(*arg2);
  }
  return ret;
}

W
wangzelin.wzl 已提交
383 384
int ObExprNvl::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
                       ObExpr &rt_expr) const
O
oceanbase-admin 已提交
385 386 387 388 389
{
  int ret = OB_SUCCESS;
  UNUSED(expr_cg_ctx);
  UNUSED(raw_expr);
  rt_expr.eval_func_ = ObExprNvlUtil::calc_nvl_expr;
W
wangzelin.wzl 已提交
390
  rt_expr.eval_batch_func_ = ObExprNvlUtil::calc_nvl_expr_batch;
O
oceanbase-admin 已提交
391 392 393
  return ret;
}

W
wangzelin.wzl 已提交
394 395
int ObExprOracleNvl::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
                       ObExpr &rt_expr) const
O
oceanbase-admin 已提交
396 397 398 399 400
{
  int ret = OB_SUCCESS;
  UNUSED(expr_cg_ctx);
  UNUSED(raw_expr);
  rt_expr.eval_func_ = ObExprNvlUtil::calc_nvl_expr;
W
wangzelin.wzl 已提交
401
  rt_expr.eval_batch_func_ = ObExprNvlUtil::calc_nvl_expr_batch;
O
oceanbase-admin 已提交
402 403 404
  return ret;
}

W
wangzelin.wzl 已提交
405 406
int ObExprNaNvl::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
                       ObExpr &rt_expr) const
O
oceanbase-admin 已提交
407 408 409
{
  int ret = OB_SUCCESS;
  UNUSED(raw_expr);
W
wangzelin.wzl 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
  UNUSED(expr_cg_ctx);
  if (rt_expr.arg_cnt_ != 2 || OB_ISNULL(rt_expr.args_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("count of children is not 2 or children is null", K(ret), K(rt_expr.arg_cnt_),
                                                              K(rt_expr.args_));
  } else if (OB_ISNULL(rt_expr.args_[0]) || OB_ISNULL(rt_expr.args_[1])) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("child is null", K(ret), K(rt_expr.args_[0]), K(rt_expr.args_[1]));
  } else {
    rt_expr.eval_func_ = eval_nanvl;
    rt_expr.eval_batch_func_ = eval_nanvl_batch;
  }
  return ret;
}
// nanvl computational abstraction.
int ObExprNaNvl::eval_nanvl_util(const ObExpr &expr, ObDatum &expr_datum, ObDatum *param1, ObDatum *param2, bool &ret_bool)
{
  int ret = OB_SUCCESS;
  if (param1->is_null() || param2->is_null()) {
    expr_datum.set_null();
  } else if (expr.args_[0]->datum_meta_.type_ != ObFloatType
             && expr.args_[0]->datum_meta_.type_ != ObDoubleType) {
    expr_datum.set_datum(*param1);
  } else {
    if (OB_FAIL(ObExprIs::is_infinite_nan(expr.args_[0]->datum_meta_.type_, param1,
                                ret_bool, ObExprIs::Ieee754::NAN_VALUE))) {
      LOG_WARN("calc_is_infinite unexpect error", K(ret));
    } else {
      if (ret_bool) {
        expr_datum.set_datum(*param2);
      } else {
        expr_datum.set_datum(*param1);
      }
    }
  }
O
oceanbase-admin 已提交
445 446
  return ret;
}
W
wangzelin.wzl 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

int ObExprNaNvl::eval_nanvl(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &expr_datum)
{
  int ret = OB_SUCCESS;
  ObDatum *param1 = NULL;
  ObDatum *param2 = NULL;
  bool ret_bool = false;
  if (OB_FAIL(expr.args_[0]->eval(ctx, param1))) {
    LOG_WARN("eval first param failed", K(ret));
  } else if (param1->is_null()) {
    expr_datum.set_null();
  } else if (OB_FAIL(expr.args_[1]->eval(ctx, param2))) {
    LOG_WARN("eval second param failed", K(ret));
  } else if (param1->is_null() || param2->is_null()) {
    expr_datum.set_null();
  } else {
    if (OB_FAIL(eval_nanvl_util(expr, expr_datum, param1, param2, ret_bool))){
      LOG_WARN("eval_nanvl unexpect error", K(ret));
    } else {
      // do nothing
    }
  }
  return ret;
}

int ObExprNaNvl::eval_nanvl_batch(const ObExpr &expr,
                                  ObEvalCtx &ctx,
                                  const ObBitVector &skip,
                                  const int64_t batch_size) {
  LOG_DEBUG("eval nanvl batch mode", K(batch_size));
  int ret = OB_SUCCESS;
  ObDatum* results = expr.locate_batch_datums(ctx);
  ObBitVector &eval_flags = expr.get_evaluated_flags(ctx);
  if (OB_FAIL(expr.args_[0]->eval_batch(ctx, skip, batch_size))) {
    LOG_WARN("eval args_[0] failed", K(ret));
  } else {
    ObBitVector &my_skip = expr.get_pvt_skip(ctx);
    my_skip.deep_copy(skip, batch_size);
    for (int64_t i = 0; OB_SUCC(ret) && i < batch_size; ++i) {
      if (my_skip.at(i) || eval_flags.at(i)) {
        continue;
      }
      ObDatum *param1 = NULL;
      param1 = &expr.args_[0]->locate_expr_datum(ctx, i);
      if (param1->is_null()) {
        results[i].set_null();
        eval_flags.set(i);
        my_skip.set(i);
      }
    }
    if (OB_FAIL(expr.args_[1]->eval_batch(ctx, my_skip, batch_size))) {
      LOG_WARN("eval args_[1] failed", K(ret));
    } else {
      for (int64_t i = 0; OB_SUCC(ret) && i < batch_size; ++i) {
        if (my_skip.at(i) || eval_flags.at(i)) {
          continue;
        }
        ObDatum *param1 = NULL;
        ObDatum *param2 = NULL;
        bool ret_bool = false;
        param1 = &expr.args_[0]->locate_expr_datum(ctx, i);
        param2 = &expr.args_[1]->locate_expr_datum(ctx, i);
        eval_flags.set(i);
        if (OB_FAIL(eval_nanvl_util(expr, results[i], param1, param2, ret_bool))){
          LOG_WARN("eval_nanvl unexpect error", K(ret));
        } else {
          // do nothing
        }
      }
    }
  }

  return ret;
}

}//namespace sql
}//namespace oceanbase