ob_inner_sql_result.cpp 29.0 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
/**
 * 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 SERVER

#include "ob_inner_sql_result.h"

#include "lib/mysqlclient/ob_mysql_result.h"
#include "lib/hash/ob_hashmap.h"
#include "lib/rc/context.h"

namespace oceanbase {
using namespace common;
using namespace common::sqlclient;
using namespace share;
using namespace share::schema;
using namespace sql;

namespace observer {
inline int ObInnerSQLResult::check_extend_value(const common::ObObj& obj)
{
  int ret = OB_SUCCESS;
  if (obj.is_null()) {
    ret = OB_ERR_NULL_VALUE;
  } else if (obj.is_min_value()) {
    ret = OB_ERR_MIN_VALUE;
  } else if (obj.is_max_value()) {
    ret = OB_ERR_MIN_VALUE;
  }
  return ret;
}

ObInnerSQLResult::ObInnerSQLResult(ObSQLSessionInfo& session)
    : column_map_created_(false),
      column_indexed_(false),
      column_map_(),
      mem_context_(nullptr),
      mem_context_destroy_guard_(mem_context_),
      sql_ctx_(),
      opened_(false),
      session_(session),
      result_set_(nullptr),
      row_(NULL),
      execute_start_ts_(0),
      execute_end_ts_(0),
      compat_mode_(
          ORACLE_MODE == session.get_compatibility_mode() ? ObWorker::CompatMode::ORACLE : ObWorker::CompatMode::MYSQL),
      is_inited_(false)
{
  sql_ctx_.exec_type_ = InnerSql;
}

int ObInnerSQLResult::init()
{
  int ret = OB_SUCCESS;
  lib::ContextParam param;
  param.set_mem_attr(session_.get_effective_tenant_id(), ObModIds::OB_RESULT_SET, ObCtxIds::DEFAULT_CTX_ID)
      .set_properties(lib::USE_TL_PAGE_OPTIONAL)
      .set_page_size(OB_MALLOC_MIDDLE_BLOCK_SIZE)
      .set_ablock_size(lib::INTACT_MIDDLE_AOBJECT_SIZE);
  if (OB_FAIL(CURRENT_CONTEXT.CREATE_CONTEXT(mem_context_, param))) {
    LOG_WARN("create memory entity failed", K(ret));
  } else {
    result_set_ = new (buf_) ObResultSet(session_, mem_context_->get_arena_allocator());
    result_set_->set_is_inner_result_set(true);
    is_inited_ = true;
  }
  return ret;
}

ObInnerSQLResult::~ObInnerSQLResult()
{
  close();
  if (result_set_ != nullptr) {
    result_set_->~ObResultSet();
  }
}

int ObInnerSQLResult::open()
{
  int ret = OB_SUCCESS;
  execute_start_ts_ = ObTimeUtility::current_time();
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else {
    share::CompatModeGuard g(compat_mode_);
    WITH_CONTEXT(mem_context_)
    {
      if (opened_) {
        ret = OB_INIT_TWICE;
        LOG_WARN("result set already open", K(ret));
      } else if (OB_FAIL(result_set_->sync_open())) {
L
lck0 已提交
103
        ObResultSet::refresh_location_cache(result_set_->get_exec_context().get_task_exec_ctx(), true, ret);
O
oceanbase-admin 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        LOG_WARN("open result set failed", K(ret));
        // move after precess_retry().
        //        result_set_->close();
      } else {
        opened_ = true;
      }
    }
  }
  execute_end_ts_ = ObTimeUtility::current_time();
  return ret;
}

int ObInnerSQLResult::close()
{
  int ret = OB_SUCCESS;
  // close can be executed too if result set not open.
  if (opened_) {
    // opened=true imply is_inited=true
    if (OB_FAIL(inner_close(false))) {
      LOG_WARN("result set close failed", K(ret));
    }
  }
  column_map_.clear();
  column_indexed_ = false;
  return ret;
}

int ObInnerSQLResult::force_close(bool need_retry)
{
  UNUSED(need_retry);
  int ret = OB_SUCCESS;
  if (OB_FAIL(inner_close(false))) {
    LOG_WARN("result set close failed", K(ret));
  }
  column_map_.clear();
  column_indexed_ = false;
  return ret;
}

int ObInnerSQLResult::inner_close(bool need_retry)
{
  int ret = OB_SUCCESS;
  share::CompatModeGuard g(compat_mode_);
  LOG_DEBUG("compat_mode_", K(ret), K(compat_mode_), K(lbt()));
  WITH_CONTEXT(mem_context_)
  {
    if (OB_FAIL(result_set_->close(need_retry))) {
L
lck0 已提交
151
      ObResultSet::refresh_location_cache(result_set_->get_exec_context().get_task_exec_ctx(), true, ret);
O
oceanbase-admin 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
      LOG_WARN("result set close failed", K(ret), K(need_retry));
    }
  }
  opened_ = false;
  return ret;
}

int ObInnerSQLResult::next()
{
  int ret = OB_SUCCESS;
  LOG_DEBUG("compat_mode_", K(ret), K(compat_mode_), K(lbt()));
  row_ = NULL;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else {
    share::CompatModeGuard g(compat_mode_);
    WITH_CONTEXT(mem_context_)
    {
      if (OB_FAIL(result_set_->get_next_row(row_))) {
        if (OB_ITER_END != ret) {
L
lck0 已提交
173
          ObResultSet::refresh_location_cache(result_set_->get_exec_context().get_task_exec_ctx(), true, ret);
O
oceanbase-admin 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 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 445 446 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 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
          LOG_WARN("get next row failed", K(ret));
        }
      }
    }
  }
  return ret;
}

int ObInnerSQLResult::build_column_map() const
{
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (!column_map_created_) {
    if (OB_FAIL(column_map_.create(
            COLUMN_MAP_BUCKET_NUM, ObModIds::OB_HASH_BUCKET_SQL_COLUMN_MAP, ObModIds::OB_HASH_NODE_SQL_COLUMN_MAP))) {
      LOG_WARN("create hash table failed", K(ret), LITERAL_K(COLUMN_MAP_BUCKET_NUM));
    } else {
      column_map_created_ = true;
    }
  }
  if (OB_SUCC(ret)) {
    column_map_.clear();
    const ColumnsFieldIArray* fields = result_set_->get_field_columns();
    if (OB_ISNULL(fields)) {
      ret = OB_INVALID_ARGUMENT;
      LOG_WARN("invalid argument", K(ret), K(fields));
    }
    for (int64_t i = 0; OB_SUCC(ret) && i < fields->count(); ++i) {
      if (nullptr == fields->at(i).cname_) {
        ret = OB_INVALID_ARGUMENT;
        LOG_WARN("null field name", K(ret), "field", fields->at(i));
      } else {
        if (OB_FAIL(column_map_.set_refactored(fields->at(i).cname_, i))) {
          // ignore duplicate column name
          if (OB_HASH_EXIST == ret) {
            ret = OB_SUCCESS;
          } else {
            LOG_WARN("add column name to column map failed", K(ret));
          }
        }
      }
    }
  }

  if (OB_SUCC(ret)) {
    column_indexed_ = true;
  }

  return ret;
}

int ObInnerSQLResult::find_idx(const char* col_name, int64_t& idx) const
{
  idx = OB_INVALID_INDEX;
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (nullptr == col_name) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), KP(col_name));
  } else {
    if (OB_UNLIKELY(!column_indexed_)) {
      if (OB_FAIL(build_column_map())) {
        LOG_WARN("build column map failed", K(ret));
      }
    }
    if (OB_SUCC(ret)) {
      ret = column_map_.get_refactored(ObString::make_string(col_name), idx);
      if (OB_SUCCESS != ret && OB_HASH_NOT_EXIST != ret) {
        LOG_WARN("get index from hash failed", K(ret), K(col_name));
      } else if (OB_HASH_NOT_EXIST == ret) {
        ret = OB_ENTRY_NOT_EXIST;
      } else if (OB_INVALID_INDEX == idx) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("invalid index", K(ret), K(idx));
      }
    }
  }
  return ret;
}

#define DEF_GET_VALUE_BY_INDEX_IMPL(name, obj_name, type)                                       \
  int ObInnerSQLResult::name(const int64_t col_idx, type& val) const                            \
  {                                                                                             \
    int ret = OB_SUCCESS;                                                                       \
    if (!opened_) {                                                                             \
      ret = OB_NOT_INIT;                                                                        \
      LOG_WARN("not opened", K(ret));                                                           \
    } else if (NULL == row_) {                                                                  \
      ret = OB_ERR_UNEXPECTED;                                                                  \
      LOG_WARN("row is null", K(ret));                                                          \
    } else if (col_idx >= (NULL == row_->projector_ ? row_->count_ : row_->projector_size_)) {  \
      ret = OB_SIZE_OVERFLOW;                                                                   \
      LOG_WARN("column index overflow", K(ret), K(col_idx), "row", *row_);                      \
    } else {                                                                                    \
      const int64_t idx = (NULL != row_->projector_ ? row_->projector_[col_idx] : col_idx);     \
      if (idx < 0 || idx >= row_->count_) {                                                     \
        ret = OB_ERR_UNEXPECTED;                                                                \
        LOG_WARN("invalid index", K(ret), K(idx), "cell count", row_->count_);                  \
      } else {                                                                                  \
        const ObObj& obj = row_->cells_[idx];                                                   \
        if (OB_FAIL(check_extend_value(obj))) {                                                 \
          LOG_DEBUG("check extend value failed", K(ret));                                       \
        } else if (OB_FAIL(obj.obj_name(val))) {                                                \
          LOG_WARN("get " #type " value from obj failed", K(ret), K(obj), K(col_idx), K_(row)); \
        }                                                                                       \
      }                                                                                         \
    }                                                                                           \
    return ret;                                                                                 \
  }

#define DEF_GET_VALUE_BY_INDEX(name, type) DEF_GET_VALUE_BY_INDEX_IMPL(name, name, type)

// DEF_GET_VALUE_BY_INDEX(get_int, int64_t);
DEF_GET_VALUE_BY_INDEX_IMPL(get_uint, get_uint64, uint64_t);
DEF_GET_VALUE_BY_INDEX(get_datetime, int64_t);
DEF_GET_VALUE_BY_INDEX(get_date, int32_t);
DEF_GET_VALUE_BY_INDEX(get_time, int64_t);
DEF_GET_VALUE_BY_INDEX(get_year, uint8_t);

int ObInnerSQLResult::get_timestamp(const int64_t col_idx, const common::ObTimeZoneInfo* tz_info, int64_t& val) const
{
  UNUSED(tz_info);
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(!opened_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (OB_UNLIKELY(NULL == row_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("row is null", K(ret));
  } else if (OB_UNLIKELY(col_idx >= (NULL == row_->projector_ ? row_->count_ : row_->projector_size_))) {
    ret = OB_SIZE_OVERFLOW;
    LOG_WARN("column index overflow", K(ret), K(col_idx), "row", *row_);
  } else {
    const int64_t idx = (NULL != row_->projector_ ? row_->projector_[col_idx] : col_idx);
    if (OB_UNLIKELY(idx < 0) || OB_UNLIKELY(idx >= row_->count_)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("invalid index", K(ret), K(idx), "cell count", row_->count_);
    } else {
      const ObObj& obj = row_->cells_[idx];
      if (OB_FAIL(check_extend_value(obj))) {
        LOG_DEBUG("check extend value failed", K(ret));
      } else {
        val = obj.get_timestamp();
      }
    }
  }
  return ret;
}

int ObInnerSQLResult::get_otimestamp_value(const int64_t col_idx, const common::ObTimeZoneInfo& tz_info,
    const common::ObObjType type, common::ObOTimestampData& otimestamp_val) const
{
  UNUSED(tz_info);
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(!opened_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (OB_UNLIKELY(NULL == row_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("row is null", K(ret));
  } else if (OB_UNLIKELY(col_idx >= (NULL == row_->projector_ ? row_->count_ : row_->projector_size_))) {
    ret = OB_SIZE_OVERFLOW;
    LOG_WARN("column index overflow", K(ret), K(col_idx), "row", *row_);
  } else {
    const int64_t idx = (NULL != row_->projector_ ? row_->projector_[col_idx] : col_idx);
    if (OB_UNLIKELY(idx < 0) || OB_UNLIKELY(idx >= row_->count_)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("invalid index", K(ret), K(idx), "cell count", row_->count_);
    } else {
      const ObObj& obj = row_->cells_[idx];
      if (OB_UNLIKELY(type != obj.get_type())) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("type is mismatch", K(type), K(obj), K(ret));
      } else if (OB_FAIL(check_extend_value(obj))) {
        LOG_DEBUG("check extend value failed", K(ret));
      } else {
        otimestamp_val = obj.get_otimestamp_value();
      }
    }
  }
  return ret;
}

int ObInnerSQLResult::get_timestamp_tz(
    const int64_t col_idx, const common::ObTimeZoneInfo& tz_info, common::ObOTimestampData& otimestamp_val) const
{
  return get_otimestamp_value(col_idx, tz_info, ObTimestampTZType, otimestamp_val);
}

int ObInnerSQLResult::get_timestamp_ltz(
    const int64_t col_idx, const common::ObTimeZoneInfo& tz_info, common::ObOTimestampData& otimestamp_val) const
{
  return get_otimestamp_value(col_idx, tz_info, ObTimestampLTZType, otimestamp_val);
}

int ObInnerSQLResult::get_timestamp_nano(
    const int64_t col_idx, const common::ObTimeZoneInfo& tz_info, common::ObOTimestampData& otimestamp_val) const
{
  return get_otimestamp_value(col_idx, tz_info, ObTimestampNanoType, otimestamp_val);
}

DEF_GET_VALUE_BY_INDEX(get_interval_ym, ObIntervalYMValue);
DEF_GET_VALUE_BY_INDEX(get_interval_ds, ObIntervalDSValue);

int ObInnerSQLResult::get_bool(const int64_t col_idx, bool& bool_val) const
{
  int64_t v = 0;
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (OB_INVALID_INDEX == col_idx) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(col_idx));
  } else if (OB_FAIL(get_int(col_idx, v))) {
    LOG_WARN("get int value failed", K(ret), K(col_idx));
  } else {
    bool_val = v;
  }
  return ret;
}

DEF_GET_VALUE_BY_INDEX(get_varchar, ObString);
DEF_GET_VALUE_BY_INDEX(get_raw, ObString);
DEF_GET_VALUE_BY_INDEX(get_nvarchar2, ObString);
DEF_GET_VALUE_BY_INDEX(get_nchar, ObString);
DEF_GET_VALUE_BY_INDEX(get_float, float);
DEF_GET_VALUE_BY_INDEX(get_double, double);

int ObInnerSQLResult::get_int(const int64_t col_idx, int64_t& int_val) const
{
  int ret = OB_SUCCESS;
  const ObObj* obj = NULL;
  if (OB_FAIL(get_obj(col_idx, obj))) {
    LOG_WARN("get obj error", K(ret));
  } else if (OB_ISNULL(obj)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("get a invalud obj", K(col_idx), K(obj), K(ret));
  } else if (OB_UNLIKELY(ObIntTC != obj->get_type_class())) {
    ret = OB_OBJ_TYPE_ERROR;
    LOG_WARN("invalid input type", K(ret), K(*obj));
  } else {
    int_val = obj->get_int();
  }
  return ret;
}

DEF_GET_VALUE_BY_INDEX_IMPL(get_number_impl, get_number, number::ObNumber);
DEF_GET_VALUE_BY_INDEX_IMPL(get_urowid_impl, get_urowid, ObURowIDData);

#undef DEF_GET_VALUE_BY_INDEX
#undef DEF_GET_VALUE_BY_INDEX_IMPL

int ObInnerSQLResult::get_type(const int64_t col_idx, ObObjMeta& type) const
{
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (NULL == row_) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("row is null", K(ret));
  } else if (col_idx >= (NULL == row_->projector_ ? row_->count_ : row_->projector_size_)) {
    ret = OB_SIZE_OVERFLOW;
    LOG_WARN("column index overflow", K(ret), K(col_idx), "row", *row_);
  } else {
    const int64_t idx = (NULL != row_->projector_ ? row_->projector_[col_idx] : col_idx);
    if (idx < 0 || idx >= row_->count_) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("invalid index", K(ret), K(idx), "cell count", row_->count_);
    } else {
      const ObObj& obj = row_->cells_[idx];
      type = obj.get_meta();
    }
  }
  return ret;
}

int ObInnerSQLResult::get_obj(
    const int64_t col_idx, ObObj& obj, const ObTimeZoneInfo* tz_info, ObIAllocator* allocator) const
{
  UNUSED(tz_info);
  UNUSED(allocator);
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (NULL == row_) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("row is null", K(ret));
  } else if (col_idx >= (NULL == row_->projector_ ? row_->count_ : row_->projector_size_)) {
    ret = OB_SIZE_OVERFLOW;
    LOG_WARN("column index overflow", K(ret), K(col_idx), "row", *row_);
  } else {
    const int64_t idx = (NULL != row_->projector_ ? row_->projector_[col_idx] : col_idx);
    if (idx < 0 || idx >= row_->count_) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("invalid index", K(ret), K(idx), "cell count", row_->count_);
    } else {
      obj = row_->cells_[idx];
    }
  }
  return ret;
}

#define DEF_GET_VALUE_BY_NAME(name, type)                           \
  int ObInnerSQLResult::name(const char* col_name, type& val) const \
  {                                                                 \
    int ret = OB_SUCCESS;                                           \
    int64_t idx = OB_INVALID_INDEX;                                 \
    if (!opened_) {                                                 \
      ret = OB_NOT_INIT;                                            \
      LOG_WARN("not opened", K(ret));                               \
    } else if (NULL == col_name) {                                  \
      ret = OB_INVALID_ARGUMENT;                                    \
      LOG_WARN("column name can not be NULL", K(ret));              \
    } else if (OB_FAIL(find_idx(col_name, idx))) {                  \
      if (OB_ENTRY_NOT_EXIST == ret) {                              \
        ret = OB_ERR_COLUMN_NOT_FOUND;                              \
      } else {                                                      \
        LOG_WARN("find column index failed", K(ret), K(col_name));  \
      }                                                             \
    } else if (OB_INVALID_INDEX == idx) {                           \
      ret = OB_ERR_UNEXPECTED;                                      \
      LOG_WARN("invalid index returned", K(ret), K(idx));           \
    } else if (OB_FAIL(name(idx, val))) {                           \
      if (OB_ERR_NULL_VALUE != ret) {                               \
        LOG_WARN(#name " failed", K(ret), K(idx));                  \
      }                                                             \
    }                                                               \
    return ret;                                                     \
  }

DEF_GET_VALUE_BY_NAME(get_int, int64_t);
DEF_GET_VALUE_BY_NAME(get_uint, uint64_t);
DEF_GET_VALUE_BY_NAME(get_datetime, int64_t);
DEF_GET_VALUE_BY_NAME(get_date, int32_t);
DEF_GET_VALUE_BY_NAME(get_time, int64_t);
DEF_GET_VALUE_BY_NAME(get_year, uint8_t);
DEF_GET_VALUE_BY_NAME(get_bool, bool);
DEF_GET_VALUE_BY_NAME(get_varchar, ObString);
DEF_GET_VALUE_BY_NAME(get_raw, ObString);
DEF_GET_VALUE_BY_NAME(get_float, float);
DEF_GET_VALUE_BY_NAME(get_double, double);
DEF_GET_VALUE_BY_NAME(get_number_impl, number::ObNumber);
DEF_GET_VALUE_BY_NAME(get_type, ObObjMeta);
DEF_GET_VALUE_BY_NAME(get_obj, ObObj);
DEF_GET_VALUE_BY_NAME(get_interval_ym, ObIntervalYMValue);
DEF_GET_VALUE_BY_NAME(get_interval_ds, ObIntervalDSValue);
DEF_GET_VALUE_BY_NAME(get_nvarchar2, ObString);
DEF_GET_VALUE_BY_NAME(get_nchar, ObString);
DEF_GET_VALUE_BY_NAME(get_urowid_impl, ObURowIDData);
#undef DEF_GET_VALUE_BY_NAME

int ObInnerSQLResult::get_timestamp(const char* col_name, const common::ObTimeZoneInfo* tz_info, int64_t& val) const
{
  UNUSED(tz_info);
  int ret = OB_SUCCESS;
  int64_t idx = OB_INVALID_INDEX;
  if (OB_UNLIKELY(!opened_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (OB_UNLIKELY(NULL == col_name)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("column name can not be NULL", K(ret));
  } else if (OB_FAIL(find_idx(col_name, idx))) {
    if (OB_ENTRY_NOT_EXIST == ret) {
      ret = OB_ERR_COLUMN_NOT_FOUND;
    } else {
      LOG_WARN("find column index failed", K(ret), K(col_name));
    }
  } else if (OB_UNLIKELY(OB_INVALID_INDEX == idx)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid index returned", K(ret), K(idx));
  } else if (OB_FAIL(get_timestamp(idx, tz_info, val))) {
    if (OB_ERR_NULL_VALUE != ret) {
      LOG_WARN("get_timestamp failed", K(ret), K(idx));
    }
  }
  return ret;
}

int ObInnerSQLResult::get_otimestamp_value(const char* col_name, const common::ObTimeZoneInfo& tz_info,
    const common::ObObjType type, common::ObOTimestampData& otimestamp_val) const
{
  UNUSED(tz_info);
  int ret = OB_SUCCESS;
  int64_t idx = OB_INVALID_INDEX;
  if (OB_UNLIKELY(!opened_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (OB_UNLIKELY(NULL == col_name)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("column name can not be NULL", K(ret));
  } else if (OB_FAIL(find_idx(col_name, idx))) {
    if (OB_ENTRY_NOT_EXIST == ret) {
      ret = OB_ERR_COLUMN_NOT_FOUND;
    } else {
      LOG_WARN("find column index failed", K(ret), K(col_name));
    }
  } else if (OB_UNLIKELY(OB_INVALID_INDEX == idx)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid index returned", K(ret), K(idx));
  } else if (OB_FAIL(get_otimestamp_value(idx, tz_info, type, otimestamp_val))) {
    if (OB_ERR_NULL_VALUE != ret) {
      LOG_WARN("get_otimestamp failed", K(ret), K(idx));
    }
  }
  return ret;
}

int ObInnerSQLResult::get_timestamp_tz(
    const char* col_name, const common::ObTimeZoneInfo& tz_info, common::ObOTimestampData& otimestamp_val) const
{
  return get_otimestamp_value(col_name, tz_info, ObTimestampTZType, otimestamp_val);
}

int ObInnerSQLResult::get_timestamp_ltz(
    const char* col_name, const common::ObTimeZoneInfo& tz_info, common::ObOTimestampData& otimestamp_val) const
{
  return get_otimestamp_value(col_name, tz_info, ObTimestampLTZType, otimestamp_val);
}

int ObInnerSQLResult::get_timestamp_nano(
    const char* col_name, const common::ObTimeZoneInfo& tz_info, common::ObOTimestampData& otimestamp_val) const
{
  return get_otimestamp_value(col_name, tz_info, ObTimestampNanoType, otimestamp_val);
}

int ObInnerSQLResult::get_number(const int64_t col_idx, common::number::ObNumber& nmb_val) const
{
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (OB_INVALID_INDEX == col_idx) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(col_idx));
  } else if (OB_FAIL(get_number_impl(col_idx, nmb_val))) {
    LOG_WARN("get number impl failed", K(ret), K(col_idx));
  }
  return ret;
}

int ObInnerSQLResult::get_number(const char* col_name, common::number::ObNumber& nmb_val) const
{
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (NULL == col_name) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), KP(col_name));
  } else if (OB_FAIL(get_number_impl(col_name, nmb_val))) {
    LOG_WARN("get number impl failed", K(ret), K(col_name));
  }
  return ret;
}

int ObInnerSQLResult::inner_get_number(const int64_t col_idx, number::ObNumber& nmb_val, IAllocator& allocator) const
{
  UNUSED(allocator);
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (OB_INVALID_INDEX == col_idx) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(col_idx));
  } else if (OB_FAIL(get_number_impl(col_idx, nmb_val))) {
    LOG_WARN("get number impl failed", K(ret), K(col_idx));
  }
  return ret;
}

int ObInnerSQLResult::inner_get_number(const char* col_name, number::ObNumber& nmb_val, IAllocator& allocator) const
{
  UNUSED(allocator);
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (NULL == col_name) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), KP(col_name));
  } else if (OB_FAIL(get_number_impl(col_name, nmb_val))) {
    LOG_WARN("get number impl failed", K(ret), K(col_name));
  }
  return ret;
}

int ObInnerSQLResult::inner_get_urowid(
    const int64_t col_idx, ObURowIDData& urowid_data, ObIAllocator& /* not used */) const
{
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (OB_FAIL(get_urowid_impl(col_idx, urowid_data))) {
    LOG_WARN("failed to get urowid", K(ret));
  }
  return ret;
}

int ObInnerSQLResult::inner_get_urowid(
    const char* col_name, ObURowIDData& urowid_data, ObIAllocator& /* no used */) const
{
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (OB_FAIL(get_urowid_impl(col_name, urowid_data))) {
    LOG_WARN("failed to get urowid data", K(ret));
  }
  return ret;
}

int ObInnerSQLResult::get_lob_locator(const int64_t col_idx, ObLobLocator*& lob_locator) const
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(get_lob_locator_impl(col_idx, lob_locator))) {
    LOG_WARN("get lob locator impl failed", K(ret), K(col_idx));
  }
  return ret;
}

int ObInnerSQLResult::get_lob_locator(const char* col_name, ObLobLocator*& lob_locator) const
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(get_lob_locator_impl(col_name, lob_locator))) {
    LOG_WARN("get lob locator impl failed", K(ret), K(col_name));
  }
  return ret;
}

int ObInnerSQLResult::inner_get_lob_locator(
    const int64_t col_idx, ObLobLocator*& lob_locator, ObIAllocator& allocator) const
{
  UNUSED(allocator);
  int ret = OB_SUCCESS;
  if (OB_FAIL(get_lob_locator_impl(col_idx, lob_locator))) {
    LOG_WARN("get lob locator impl failed", K(ret), K(col_idx));
  }
  return ret;
}

int ObInnerSQLResult::inner_get_lob_locator(
    const char* col_name, ObLobLocator*& lob_locator, ObIAllocator& allocator) const
{
  UNUSED(allocator);
  int ret = OB_SUCCESS;
  if (OB_FAIL(get_lob_locator_impl(col_name, lob_locator))) {
    LOG_WARN("get lob locator impl failed", K(ret), K(col_name));
  }
  return ret;
}

int ObInnerSQLResult::get_lob_locator_impl(const int64_t col_idx, ObLobLocator*& lob_locator) const
{
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (OB_ISNULL(row_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("row is null", K(ret));
  } else if (OB_INVALID_INDEX == col_idx) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(col_idx));
  } else if (col_idx >= (NULL == row_->projector_ ? row_->count_ : row_->projector_size_)) {
    ret = OB_SIZE_OVERFLOW;
    LOG_WARN("column index overflow", K(ret), K(col_idx), "row", *row_);
  } else {
    const int64_t idx = (NULL != row_->projector_ ? row_->projector_[col_idx] : col_idx);
    if (idx < 0 || idx >= row_->count_) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("invalid index", K(ret), K(idx), "cell count", row_->count_);
    } else {
      const ObObj& obj = row_->cells_[idx];
      if (OB_FAIL(check_extend_value(obj))) {
        LOG_DEBUG("check extend value failed", K(ret));
      } else if (OB_FAIL(obj.get_lob_locator(lob_locator))) {
        LOG_WARN("get lob locator value from obj failed", K(ret), K(obj), K(col_idx), K_(row));
      }
    }
  }
  return ret;
}

int ObInnerSQLResult::get_lob_locator_impl(const char* col_name, ObLobLocator*& lob_locator) const
{
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else {
    int64_t idx = OB_INVALID_INDEX;
    if (OB_ISNULL(col_name)) {
      ret = OB_INVALID_ARGUMENT;
      LOG_WARN("column name can not be NULL", K(ret));
    } else if (OB_FAIL(find_idx(col_name, idx))) {
      if (OB_ENTRY_NOT_EXIST == ret) {
        ret = OB_ERR_COLUMN_NOT_FOUND;
      } else {
        LOG_WARN("find column index failed", K(ret), K(col_name));
      }
    } else if (OB_INVALID_INDEX == idx) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("invalid index returned", K(ret), K(idx));
    } else if (OB_FAIL(get_lob_locator_impl(idx, lob_locator))) {
      if (OB_ERR_NULL_VALUE != ret) {
        LOG_WARN("inner get lob locator failed", K(ret), K(idx));
      }
    }
  }
  return ret;
}

int ObInnerSQLResult::get_obj(const int64_t col_idx, const common::ObObj*& result) const
{
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else if (NULL == row_) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("row is null", K(ret));
  } else if (col_idx >= (NULL == row_->projector_ ? row_->count_ : row_->projector_size_)) {
    ret = OB_SIZE_OVERFLOW;
    LOG_WARN("column index overflow", K(ret), K(col_idx), "row", *row_);
  } else {
    const int64_t idx = (NULL != row_->projector_ ? row_->projector_[col_idx] : col_idx);
    if (idx < 0 || idx >= row_->count_) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("invalid index", K(ret), K(idx), "cell count", row_->count_);
    } else {
      const ObObj& obj = row_->cells_[idx];
      if (OB_FAIL(check_extend_value(obj))) {
        LOG_DEBUG("check extend value failed", K(ret));
      } else {
        result = &obj;
      }
    }
  }
  return ret;
}

int ObInnerSQLResult::print_info() const
{
  int ret = OB_SUCCESS;
  if (!opened_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not opened", K(ret));
  } else {
    LOG_INFO("result", K_(result_set));
  }
  return ret;
}
}  // end of namespace observer
}  // end of namespace oceanbase