ob_raw_expr.h 120.6 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
/**
 * 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.
 */

#ifndef OCEANBASE_SQL_RESOLVER_EXPR_RAW_EXPR_
#define OCEANBASE_SQL_RESOLVER_EXPR_RAW_EXPR_
#include "share/ob_define.h"
#include "lib/container/ob_bit_set.h"
#include "lib/string/ob_string.h"
#include "lib/container/ob_se_array.h"
#include "lib/hash_func/ob_hash_func.h"
#include "lib/list/ob_obj_store.h"
#include "lib/rc/context.h"
#include "common/ob_range.h"
#include "common/ob_accuracy.h"
#include "sql/resolver/expr/ob_expr.h"
#include "sql/resolver/expr/ob_const_expr.h"
#include "sql/resolver/expr/ob_var_expr.h"
#include "sql/resolver/expr/ob_op_expr.h"
#include "sql/resolver/expr/ob_column_ref_expr.h"
#include "sql/resolver/expr/ob_case_op_expr.h"
#include "sql/engine/expr/ob_expr_res_type.h"
#include "share/schema/ob_schema_utils.h"
X
xy0 已提交
32
#include "share/schema/ob_schema_struct.h"
O
oceanbase-admin 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "sql/ob_sql_define.h"
#include "sql/resolver/expr/ob_expr_info_flag.h"
#include "share/system_variable/ob_system_variable.h"
#include "share/ob_worker.h"
#include "sql/parser/parse_node.h"
#include "sql/resolver/ob_resolver_define.h"
#include "sql/engine/expr/ob_expr_operator.h"
#include "sql/engine/expr/ob_expr_operator_factory.h"
#include "sql/code_generator/ob_static_engine_expr_cg.h"
namespace oceanbase {
namespace share {
namespace schema {
class ObSchemaGetterGuard;
}
}  // namespace share
namespace sql {
class ObStmt;
class ObSQLSessionInfo;
class ObExprOperator;
class ObRawExprFactory;
class ObSelectStmt;
X
xy0 已提交
54
extern ObRawExpr *USELESS_POINTER;
O
oceanbase-admin 已提交
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
// If is_stack_overflow is true, the printing will not continue
#define DEFINE_VIRTUAL_TO_STRING_CHECK_STACK_OVERFLOW(body)                               \
  DECLARE_VIRTUAL_TO_STRING                                                               \
  {                                                                                       \
    int ret = common::OB_SUCCESS;                                                         \
    int64_t pos = 0;                                                                      \
    bool is_stack_overflow = false;                                                       \
    if (OB_FAIL(check_stack_overflow(is_stack_overflow))) {                               \
      SQL_RESV_LOG(WARN, "failed to check stack overflow", K(ret), K(is_stack_overflow)); \
    } else if (is_stack_overflow) {                                                       \
      SQL_RESV_LOG(DEBUG, "too deep recursive", K(ret), K(is_stack_overflow));            \
    } else {                                                                              \
      J_OBJ_START();                                                                      \
      body;                                                                               \
      J_OBJ_END();                                                                        \
    }                                                                                     \
    return pos;                                                                           \
  }

#define VIRTUAL_TO_STRING_KV_CHECK_STACK_OVERFLOW(args...) DEFINE_VIRTUAL_TO_STRING_CHECK_STACK_OVERFLOW(J_KV(args))

O
oceanbase-admin 已提交
77 78 79 80 81
// ObSqlBitSet is a simple bitset, in order to avoid memory explosure
// ObBitSet is too large just for a simple bitset
const static int64_t DEFAULT_SQL_BITSET_SIZE = 32;
template <int64_t N = DEFAULT_SQL_BITSET_SIZE, typename FlagType = int64_t, bool auto_free = false>
class ObSqlBitSet {
G
gm 已提交
82
public:
O
oceanbase-admin 已提交
83 84 85 86 87 88 89 90 91 92 93 94
  typedef uint32_t BitSetWord;

  ObSqlBitSet() : block_allocator_(NULL), bit_set_word_array_(NULL), desc_()
  {
    int ret = OB_SUCCESS;
    if (OB_FAIL(init_block_allocator())) {
      SQL_RESV_LOG(WARN, "failed to init block allocator", K(ret));
    } else if (OB_ISNULL(block_allocator_)) {
      ret = OB_INVALID_ARGUMENT;
      SQL_RESV_LOG(WARN, "invalid argument", K(ret));
    } else {
      int64_t words_size = sizeof(BitSetWord) * MAX_BITSETWORD;
X
xy0 已提交
95
      if (OB_ISNULL(bit_set_word_array_ = (BitSetWord *)block_allocator_->alloc(words_size))) {
O
oceanbase-admin 已提交
96 97 98 99 100 101 102 103 104 105 106
        ret = OB_ALLOCATE_MEMORY_FAILED;
        SQL_RESV_LOG(WARN, "failed to alloc memory", K(ret));
      } else {
        MEMSET(bit_set_word_array_, 0, words_size);
        desc_.cap_ = static_cast<int16_t>(MAX_BITSETWORD);
        desc_.len_ = 0;
        desc_.inited_ = true;
      }
    }
  }

X
xy0 已提交
107
  ObSqlBitSet(const ObSqlBitSet<N, FlagType, auto_free> &other)
O
oceanbase-admin 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
      : block_allocator_(NULL), bit_set_word_array_(NULL), desc_()
  {
    int ret = OB_SUCCESS;
    desc_.inited_ = false;
    if (!other.is_valid()) {
      ret = OB_NOT_INIT;
      SQL_RESV_LOG(WARN, "not intied", K(ret));
    } else if (OB_FAIL(init_block_allocator())) {
      SQL_RESV_LOG(WARN, "failed to init block allocator", K(ret));
    } else if (OB_ISNULL(block_allocator_)) {
      SQL_RESV_LOG(WARN, "block_allocator_ is null", K(ret));
    } else {
      int64_t cap = other.bitset_word_count() * 2;
      if (cap <= 0) {
        cap = MAX_BITSETWORD;
      }
      int64_t words_size = sizeof(BitSetWord) * cap;
X
xy0 已提交
125
      if (OB_ISNULL(bit_set_word_array_ = (BitSetWord *)block_allocator_->alloc(words_size))) {
O
oceanbase-admin 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        ret = OB_ALLOCATE_MEMORY_FAILED;
        SQL_RESV_LOG(WARN, "failed to alloc memory", K(ret));
      } else {
        MEMSET(bit_set_word_array_, 0, words_size);
        for (int64_t i = 0; i < other.bitset_word_count(); i++) {
          bit_set_word_array_[i] = other.get_bitset_word(i);
        }
        desc_.len_ = static_cast<int16_t>(other.bitset_word_count());
        desc_.cap_ = static_cast<int16_t>(cap);
        desc_.inited_ = true;
      }
    }
  }
  explicit ObSqlBitSet(const int64_t bit_size) : block_allocator_(NULL), bit_set_word_array_(NULL), desc_()
  {
    int ret = OB_SUCCESS;
    if (bit_size < 0) {
      ret = OB_INVALID_ARGUMENT;
      SQL_RESV_LOG(WARN, "invalid argument", K(ret));
    } else if (OB_FAIL(init_block_allocator())) {
      SQL_RESV_LOG(WARN, "failed to init block allocator", K(ret));
    } else {
      int64_t bitset_word_cnt = (bit_size <= N ? MAX_BITSETWORD : ((bit_size - 1) / PER_BITSETWORD_BITS + 1));
      int64_t words_size = sizeof(BitSetWord) * bitset_word_cnt;
X
xy0 已提交
150
      if (OB_ISNULL(bit_set_word_array_ = (BitSetWord *)block_allocator_->alloc(words_size))) {
O
oceanbase-admin 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 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
        ret = OB_ALLOCATE_MEMORY_FAILED;
        SQL_RESV_LOG(WARN, "failed to alloc memory", K(ret));
      } else {
        MEMSET(bit_set_word_array_, 0, words_size);
        desc_.len_ = 0;
        desc_.cap_ = static_cast<int16_t>(bitset_word_cnt);
        desc_.inited_ = true;
      }
    }
  }

  ~ObSqlBitSet()
  {
    destroy();
  }

  void reset()
  {
    reuse();
  }

  void reuse()
  {
    if (is_valid()) {
      if (NULL != bit_set_word_array_ && desc_.len_ > 0) {
        MEMSET(bit_set_word_array_, 0, desc_.len_ * sizeof(BitSetWord));
      }
      desc_.len_ = 0;
    }
  }

  void destroy()
  {
    if (is_valid()) {
      if (auto_free) {
        // auto_free = true, context allocator is used,
        // free memory is just useless
        // do nothing
      } else {
        // free memory
        if (NULL != block_allocator_ && NULL != bit_set_word_array_) {
          block_allocator_->free(bit_set_word_array_);
        }
        if (NULL != block_allocator_) {
          block_allocator_->reset();
          ob_free(block_allocator_);
        }
        block_allocator_ = NULL;
        bit_set_word_array_ = NULL;
      }
      desc_.len_ = 0;
      desc_.cap_ = 0;
      desc_.inited_ = false;
    }
  }

  int64_t bitset_word_count() const
  {
    return static_cast<int64_t>(desc_.len_);
  }
  int64_t bit_count() const
  {
    return static_cast<int64_t>(desc_.len_) * PER_BITSETWORD_BITS;
  }
  bool is_empty() const
  {
    return 0 == num_members();
  }
  bool is_valid() const
  {
    return desc_.inited_;
  }
  void clear_all()
  {
    if (!is_valid()) {
      // do nothing
    } else {
      MEMSET(bit_set_word_array_, 0, desc_.len_ * sizeof(BitSetWord));
    }
  }
  BitSetWord get_bitset_word(int64_t index) const
  {
    BitSetWord word = 0;
    if (!is_valid()) {
      SQL_RESV_LOG(INFO, "not inited");
    } else if (index < 0 || index >= desc_.len_) {
      SQL_RESV_LOG(INFO, "bitmap word index exceeds scope", K(index), K(desc_.len_));
    } else {
      word = bit_set_word_array_[index];
    }
    return word;
  }

  int64_t num_members() const
  {
    int64_t num = 0;
    BitSetWord word = 0;
    if (!is_valid()) {
      // do nothing
    } else {
      for (int64_t i = 0; i < desc_.len_; i++) {
        word = bit_set_word_array_[i];
        if (0 == word) {
          // do nothing
        } else {
          word = (word & UINT32_C(0x55555555)) + ((word >> 1) & UINT32_C(0x55555555));
          word = (word & UINT32_C(0x33333333)) + ((word >> 2) & UINT32_C(0x33333333));
          word = (word & UINT32_C(0x0f0f0f0f)) + ((word >> 4) & UINT32_C(0x0f0f0f0f));
          word = (word & UINT32_C(0x00ff00ff)) + ((word >> 8) & UINT32_C(0x00ff00ff));
          word = (word & UINT32_C(0x0000ffff)) + ((word >> 16) & UINT32_C(0x0000ffff));
          num += (int64_t)word;
        }
      }
    }
    return num;
  }
  bool has_member(int64_t index) const
  {
    bool bool_ret = false;
    if (!is_valid()) {
      // do nothing
    } else if (OB_UNLIKELY(index < 0)) {
      SQL_RESV_LOG(INFO, "negative bitmap member not allowed");
    } else {
      int64_t pos = index >> PER_BITSETWORD_MOD_BITS;
      if (pos >= desc_.len_) {
        // dp nothing
      } else {
        bool_ret = (bit_set_word_array_[pos] & ((BitSetWord)1 << (index & PER_BITSETWORD_MASK))) != 0;
      }
    }
    return bool_ret;
  }
  int add_member(int64_t index)
  {
    int ret = common::OB_SUCCESS;
    if (!is_valid()) {
      ret = OB_NOT_INIT;
      SQL_RESV_LOG(WARN, "not inited", K(ret));
    } else if (OB_UNLIKELY(index < 0)) {
      ret = OB_INVALID_ARGUMENT;
      SQL_RESV_LOG(WARN, "negative bitmap member not allowed", K(ret), K(index));
    } else {
      int64_t pos = index >> PER_BITSETWORD_MOD_BITS;
      if (OB_UNLIKELY(pos >= desc_.cap_)) {
        int64_t new_word_cnt = pos * 2;
        if (OB_FAIL(alloc_new_buf(new_word_cnt))) {
          SQL_RESV_LOG(WARN, "failed to alloc new buf", K(ret));
        }
      }
      if (pos >= desc_.len_) {
        desc_.len_ = static_cast<int16_t>(pos) + 1;
      }
      if (OB_SUCC(ret)) {
        bit_set_word_array_[pos] |= ((BitSetWord)1 << (index & PER_BITSETWORD_MASK));
      }
    }
    return ret;
  }
  int del_member(int64_t index)
  {
    int ret = common::OB_SUCCESS;
    if (!is_valid()) {
      ret = common::OB_NOT_INIT;
      SQL_RESV_LOG(WARN, "not inited", K(ret));
    } else if (OB_UNLIKELY(index < 0)) {
      ret = common::OB_INVALID_ARGUMENT;
      SQL_RESV_LOG(WARN, "negative bitmap member not allowed", K(ret), K(index));
    } else {
      int64_t pos = index >> PER_BITSETWORD_MOD_BITS;
      if (OB_UNLIKELY(pos >= desc_.len_)) {
        // do nothing
      } else {
        bit_set_word_array_[pos] &= ~((BitSetWord)1 << (index & PER_BITSETWORD_MASK));
      }
    }
    return ret;
  }

  int do_mask(int64_t begin_index, int64_t end_index)
  {
    int ret = OB_SUCCESS;
    int64_t max_bit_count = static_cast<int64_t>(desc_.len_) * PER_BITSETWORD_BITS;
    if (!is_valid()) {
      ret = OB_NOT_INIT;
      SQL_RESV_LOG(WARN, "not inited", K(ret));
    } else if (begin_index < 0 || begin_index >= max_bit_count || end_index < 0 || end_index >= max_bit_count) {
      ret = OB_INVALID_ARGUMENT;
      SQL_RESV_LOG(WARN, "invalid argument", K(ret), K(begin_index), K(end_index), K(max_bit_count));
    } else {
      int64_t begin_word = begin_index / PER_BITSETWORD_BITS;
      int64_t end_word = end_index / PER_BITSETWORD_BITS;
      int64_t begin_pos = begin_index % PER_BITSETWORD_BITS;
      int64_t end_pos = end_index % PER_BITSETWORD_BITS;

      for (int64_t i = 0; i < begin_word; i++) {
        bit_set_word_array_[i] = 0;
      }
      for (int64_t i = desc_.len_ - 1; i > end_word; i--) {
        bit_set_word_array_[i] = 0;
      }
      for (int64_t i = 0; i < begin_pos; i++) {
        bit_set_word_array_[begin_word] &= ~((BitSetWord)1 << i);
      }
      for (int64_t i = 1 + end_pos; i < PER_BITSETWORD_BITS; i++) {
        bit_set_word_array_[end_word] &= ~((BitSetWord)1 << i);
      }
    }
    return ret;
  }

  template <int64_t M, typename FlagType2, bool auto_free2>
X
xy0 已提交
363
  int add_members(const ObSqlBitSet<M, FlagType2, auto_free2> &other)
O
oceanbase-admin 已提交
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
  {
    int ret = common::OB_SUCCESS;
    if (!is_valid()) {
      ret = common::OB_NOT_INIT;
      SQL_RESV_LOG(WARN, "not inited", K(ret));
    } else if (OB_ISNULL(bit_set_word_array_)) {
      ret = common::OB_INVALID_ARGUMENT;
      SQL_RESV_LOG(WARN, "invalid argument", K(ret), K(bit_set_word_array_));
    } else {
      int64_t this_count = desc_.len_;
      int64_t that_count = other.bitset_word_count();

      if (this_count < that_count) {
        if (desc_.cap_ >= that_count) {
          // do nothing
        } else {
          int64_t new_word_cnt = that_count * 2;
          if (OB_FAIL(alloc_new_buf(new_word_cnt))) {
            SQL_RESV_LOG(WARN, "failed to alloc new buffer", K(ret));
          }
        }
      }
      if (that_count >= desc_.len_) {
        desc_.len_ = static_cast<int16_t>(that_count);
      }

      if (OB_FAIL(ret)) {
        // do nothing
      } else {
        for (int64_t i = 0; i < that_count; i++) {
          bit_set_word_array_[i] |= other.get_bitset_word(i);
        }
      }
    }
    return ret;
  }

  template <int64_t M, typename FlagType2, bool auto_free2>
X
xy0 已提交
402
  int add_members2(const ObSqlBitSet<M, FlagType2, auto_free2> &other)
O
oceanbase-admin 已提交
403 404 405 406 407
  {
    return add_members(other);
  }

  template <int64_t M, typename FlagType2, bool auto_free2>
X
xy0 已提交
408
  int del_members(const ObSqlBitSet<M, FlagType2, auto_free2> &other)
O
oceanbase-admin 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422
  {
    int ret = common::OB_SUCCESS;
    if (!is_valid()) {
      ret = common::OB_NOT_INIT;
      SQL_RESV_LOG(WARN, "not inited", K(ret));
    } else {
      for (int64_t i = 0; i < desc_.len_; i++) {
        bit_set_word_array_[i] &= ~(other.get_bitset_word(i));
      }
    }
    return ret;
  }

  template <int64_t M, typename FlagType2, bool auto_free2>
X
xy0 已提交
423
  int del_members2(const ObSqlBitSet<M, FlagType2, auto_free2> &other)
O
oceanbase-admin 已提交
424 425 426 427 428
  {
    return del_members(other);
  }

  template <int64_t M, typename FlagType2, bool auto_free2>
X
xy0 已提交
429
  bool is_subset(const ObSqlBitSet<M, FlagType2, auto_free2> &other) const
O
oceanbase-admin 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
  {
    bool bool_ret = true;
    if (!is_valid()) {
      // do nothing
    } else if (is_empty()) {
      bool_ret = true;
    } else if (other.is_empty()) {
      bool_ret = false;
    } else if (desc_.len_ > other.bitset_word_count()) {
      bool_ret = false;
    } else {
      for (int64_t i = 0; bool_ret && i < desc_.len_; i++) {
        if ((bit_set_word_array_[i] & (~(other.get_bitset_word(i)))) != 0) {
          bool_ret = false;
        }
      }
    }
    return bool_ret;
  }

  template <int64_t M, typename FlagType2, bool auto_free2>
X
xy0 已提交
451
  bool is_subset2(const ObSqlBitSet<M, FlagType2, auto_free2> &other) const
O
oceanbase-admin 已提交
452 453 454 455 456
  {
    return is_subset(other);
  }

  template <int64_t M, typename FlagType2, bool auto_free2>
X
xy0 已提交
457
  bool is_superset(const ObSqlBitSet<M, FlagType2, auto_free2> &other) const
O
oceanbase-admin 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
  {
    bool bool_ret = false;
    if (!is_valid()) {
      // do nothing
    } else if (is_empty()) {
      bool_ret = false;
    } else if (other.is_empty()) {
      bool_ret = true;
    } else if (desc_.len_ < other.bitset_word_count()) {
      bool_ret = false;
    } else {
      bool_ret = true;
      for (int64_t i = 0; bool_ret && i < other.bitset_word_count(); i++) {
        if (((~bit_set_word_array_[i]) & other.get_bitset_word(i)) != 0) {
          bool_ret = false;
        }
      }
    }
    return bool_ret;
  }

  template <int64_t M, typename FlagType2, bool auto_free2>
X
xy0 已提交
480
  bool is_superset2(const ObSqlBitSet<M, FlagType2, auto_free2> &other) const
O
oceanbase-admin 已提交
481 482 483 484 485
  {
    return is_superset(other);
  }

  template <int64_t M, typename FlagType2, bool auto_free2>
X
xy0 已提交
486
  bool overlap(const ObSqlBitSet<M, FlagType2, auto_free2> &other) const
O
oceanbase-admin 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
  {
    bool bool_ret = false;
    if (!is_valid()) {
      // do nothing
    } else {
      int64_t min_bitset_word_count = std::min(static_cast<int64_t>(desc_.len_), other.bitset_word_count());
      for (int64_t i = 0; !bool_ret && i < min_bitset_word_count; i++) {
        if ((bit_set_word_array_[i] & (other.get_bitset_word(i))) != 0) {
          bool_ret = true;
        }
      }
    }
    return bool_ret;
  }

  template <int64_t M, typename FlagType2, bool auto_free2>
X
xy0 已提交
503
  bool overlap2(const ObSqlBitSet<M, FlagType2, auto_free2> &other) const
O
oceanbase-admin 已提交
504 505 506 507
  {
    return overlap(other);
  }

X
xy0 已提交
508
  int to_array(ObIArray<int64_t> &arr) const
O
oceanbase-admin 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
  {
    int ret = common::OB_SUCCESS;
    if (!is_valid()) {
      ret = OB_NOT_INIT;
      SQL_RESV_LOG(WARN, "not inited", K(ret));
    } else {
      arr.reuse();
      int64_t num = num_members();
      int64_t count = 0;
      int64_t max_bit_count = desc_.len_ * PER_BITSETWORD_BITS;
      for (int64_t i = 0; OB_SUCC(ret) && count < num && i < max_bit_count; i++) {
        if (has_member(i)) {
          if (OB_FAIL(arr.push_back(i))) {
            SQL_RESV_LOG(WARN, "failed to push back element", K(ret));
          } else {
            count++;
          }
        }
      }  // for end
    }
    return ret;
  }

  template <int64_t M, typename FlagType2, bool auto_free2>
X
xy0 已提交
533
  bool equal(const ObSqlBitSet<M, FlagType2, auto_free2> &other) const
O
oceanbase-admin 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
  {
    bool bool_ret = true;
    if (!is_valid() || !other.is_valid()) {
      bool_ret = false;
    } else if (bitset_word_count() != other.bitset_word_count()) {
      bool_ret = false;
    } else {
      for (int64_t i = 0; bool_ret && i < bitset_word_count(); i++) {
        if (get_bitset_word(i) != other.get_bitset_word(i)) {
          bool_ret = false;
        }
      }
    }
    return bool_ret;
  }

X
xy0 已提交
550
  bool operator==(const ObSqlBitSet<N, FlagType, auto_free> &other) const
O
oceanbase-admin 已提交
551 552 553 554
  {
    return this->equal(other);
  }

X
xy0 已提交
555
  bool operator!=(const ObSqlBitSet<N, FlagType, auto_free> &other) const
O
oceanbase-admin 已提交
556 557 558 559 560
  {
    return !(*this == other);
  }

  template <int64_t M, typename FlagType2, bool auto_free2, int64_t L, typename FlagType3, bool auto_free3>
X
xy0 已提交
561
  int intersect(const ObSqlBitSet<M, FlagType2, auto_free2> &left, const ObSqlBitSet<L, FlagType3, auto_free3> &right)
O
oceanbase-admin 已提交
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
  {
    int ret = common::OB_SUCCESS;
    if (!is_valid() || !left.is_valid() || !right.is_valid()) {
      ret = common::OB_NOT_INIT;
      SQL_RESV_LOG(WARN, "not inited", K(ret));
    } else if (OB_ISNULL(bit_set_word_array_)) {
      ret = common::OB_INVALID_ARGUMENT;
      SQL_RESV_LOG(WARN, "invalid argument", K(ret), K(bit_set_word_array_));
    } else {
      reset();
      int64_t that_count =
          left.bitset_word_count() < right.bitset_word_count() ? left.bitset_word_count() : right.bitset_word_count();
      if (desc_.cap_ < that_count) {
        int64_t new_word_cnt = that_count * 2;
        if (OB_FAIL(alloc_new_buf(new_word_cnt))) {
          SQL_RESV_LOG(WARN, "failed to alloc new buffer", K(ret));
        }
      }
      if (OB_FAIL(ret)) {
        // do nothing
      } else {
        for (int64_t i = 0; i < that_count; i++) {
          bit_set_word_array_[i] = left.get_bitset_word(i) & right.get_bitset_word(i);
        }
        desc_.len_ = static_cast<int16_t>(that_count);
      }
    }
    return ret;
  }

  template <int64_t M, typename FlagType2, bool auto_free2, int64_t L, typename FlagType3, bool auto_free3>
X
xy0 已提交
593
  int except(const ObSqlBitSet<M, FlagType2, auto_free2> &left, const ObSqlBitSet<L, FlagType3, auto_free3> &right)
O
oceanbase-admin 已提交
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
  {
    int ret = common::OB_SUCCESS;
    if (!is_valid() || !left.is_valid() || !right.is_valid()) {
      ret = common::OB_NOT_INIT;
      SQL_RESV_LOG(WARN, "not inited", K(ret));
    } else if (OB_ISNULL(bit_set_word_array_)) {
      ret = common::OB_INVALID_ARGUMENT;
      SQL_RESV_LOG(WARN, "invalid argument", K(ret), K(bit_set_word_array_));
    } else {
      reset();
      int64_t that_count = left.bitset_word_count();
      if (desc_.cap_ < that_count) {
        int64_t new_word_cnt = that_count * 2;
        if (OB_FAIL(alloc_new_buf(new_word_cnt))) {
          SQL_RESV_LOG(WARN, "failed to alloc new buffer", K(ret));
        }
      }
      if (OB_FAIL(ret)) {
        // do nothing
      } else {
        int64_t i = 0;
        for (; i < left.bitset_word_count() && i < right.bitset_word_count(); i++) {
          bit_set_word_array_[i] = left.get_bitset_word(i) ^ (left.get_bitset_word(i) & right.get_bitset_word(i));
        }
        for (; i < left.bitset_word_count(); i++) {
          bit_set_word_array_[i] = left.get_bitset_word(i);
        }
        desc_.len_ = static_cast<int16_t>(that_count);
      }
    }
    return ret;
  }

X
xy0 已提交
627
  static int databuff_print_obj(char *buf, const int64_t buf_len, int64_t &pos, const int64_t &obj)
O
oceanbase-admin 已提交
628 629 630 631
  {
    return common::databuff_print_obj(buf, buf_len, pos, obj);
  }

X
xy0 已提交
632
  static int databuff_print_obj(char *buf, const int64_t buf_len, int64_t &pos, const ObExprInfoFlag &flag)
O
oceanbase-admin 已提交
633 634 635 636
  {
    return common::databuff_printf(buf, buf_len, pos, "\"%s\"", get_expr_info_flag_str(flag));
  }

X
xy0 已提交
637
  int64_t to_string(char *buf, const int64_t buf_len) const
O
oceanbase-admin 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
  {
    int ret = common::OB_SUCCESS;
    int64_t pos = 0;
    J_ARRAY_START();
    int64_t num = num_members();
    int64_t count = 0;
    for (int64_t i = 0; i < bit_count(); i++) {
      if (has_member(i)) {
        if (count != 0 && count < num) {
          J_COMMA();
        }
        FlagType flag = static_cast<FlagType>(i);
        if (OB_FAIL(databuff_print_obj(buf, buf_len, pos, flag))) {
          SQL_RESV_LOG(WARN, "databuff print obj failed", K(ret));
        }
        ++count;
      }
    }
    J_ARRAY_END();
    return pos;
  }

X
xy0 已提交
660
  ObSqlBitSet<N, FlagType, auto_free> &operator=(const ObSqlBitSet<N, FlagType, auto_free> &other)
O
oceanbase-admin 已提交
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
  {
    int ret = OB_SUCCESS;
    if (!other.is_valid() || !is_valid()) {
      ret = OB_NOT_INIT;
      SQL_RESV_LOG(WARN, "invalid bitset", K(is_valid()), K(other.is_valid()));
    } else if (&other == this) {
      // do nothing
    } else {
      if (other.bitset_word_count() > desc_.cap_) {
        int64_t new_word_cnt = other.bitset_word_count() * 2;
        if (OB_FAIL(alloc_new_buf(new_word_cnt))) {
          SQL_RESV_LOG(WARN, "failed to alloc new buf", K(ret));
        }
      }

      for (int64_t i = 0; OB_SUCC(ret) && i < other.bitset_word_count(); i++) {
        bit_set_word_array_[i] = other.get_bitset_word(i);
      }
      desc_.len_ = static_cast<int16_t>(other.bitset_word_count());
    }
    if (OB_FAIL(ret)) {
      // error happened, set inited flag to be false
      desc_.inited_ = false;
    }
    return *this;
  }

G
gm 已提交
688
private:
O
oceanbase-admin 已提交
689 690 691 692
  int alloc_new_buf(int64_t word_cnt)
  {
    int ret = OB_SUCCESS;
    int64_t words_size = sizeof(BitSetWord) * word_cnt;
X
xy0 已提交
693 694
    BitSetWord *new_buf = NULL;
    ObIAllocator *allocator = get_block_allocator();
O
oceanbase-admin 已提交
695 696 697 698 699
    if (!is_valid()) {
      ret = OB_NOT_INIT;
      SQL_RESV_LOG(WARN, "not inited", K(ret));
    } else if (OB_ISNULL(allocator)) {
      SQL_RESV_LOG(WARN, "invalid allocator", K(ret));
X
xy0 已提交
700
    } else if (OB_ISNULL(new_buf = (BitSetWord *)allocator->alloc(words_size))) {
O
oceanbase-admin 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713
      ret = OB_ALLOCATE_MEMORY_FAILED;
      SQL_RESV_LOG(WARN, "failed to alloc memory", K(ret));
    } else {
      MEMSET(new_buf, 0, words_size);
      MEMCPY(new_buf, bit_set_word_array_, desc_.len_ * sizeof(BitSetWord));
      // set word array to new buffer
      allocator->free(bit_set_word_array_);
      bit_set_word_array_ = new_buf;
      desc_.cap_ = static_cast<int16_t>(word_cnt);
    }
    return ret;
  }

X
xy0 已提交
714
  ObIAllocator *get_block_allocator()
O
oceanbase-admin 已提交
715
  {
X
xy0 已提交
716
    ObIAllocator *ret_alloc = NULL;
O
oceanbase-admin 已提交
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
    if (!is_valid()) {
      // do nothing
    } else {
      ret_alloc = block_allocator_;
    }
    return ret_alloc;
  }

  int init_block_allocator()
  {
    int ret = OB_SUCCESS;
    if (is_valid()) {
      ret = OB_INIT_TWICE;
      SQL_RESV_LOG(WARN, "init twice", K(ret));
    } else if (auto_free) {
J
jg0 已提交
732
      block_allocator_ = &CURRENT_CONTEXT->get_arena_allocator();
O
oceanbase-admin 已提交
733
    } else {
X
xy0 已提交
734
      void *alloc_buf = NULL;
O
oceanbase-admin 已提交
735 736 737 738 739 740 741 742 743 744
      if (OB_ISNULL(alloc_buf = ob_malloc(sizeof(ObArenaAllocator), ObModIds::OB_BIT_SET))) {
        ret = OB_ALLOCATE_MEMORY_FAILED;
        SQL_RESV_LOG(WARN, "failed to allocate memory", K(ret));
      } else {
        block_allocator_ = new (alloc_buf) ObArenaAllocator(ObModIds::OB_BIT_SET);
      }
    }
    return ret;
  }

G
gm 已提交
745
private:
O
oceanbase-admin 已提交
746 747 748 749 750 751 752 753 754 755 756 757 758 759
  static const int64_t PER_BITSETWORD_BITS = 32;
  static const int64_t PER_BITSETWORD_MOD_BITS = 5;
  static const int64_t PER_BITSETWORD_MASK = PER_BITSETWORD_BITS - 1;
  static const int64_t MAX_BITSETWORD = ((N <= 0 ? DEFAULT_SQL_BITSET_SIZE : N) - 1) / PER_BITSETWORD_BITS + 1;

  struct SqlBitSetDesc {
    int16_t len_;
    int16_t cap_;
    bool inited_;

    SqlBitSetDesc() : len_(0), cap_(0), inited_(false)
    {}
  };

G
gm 已提交
760
private:
X
xy0 已提交
761 762
  ObIAllocator *block_allocator_;
  BitSetWord *bit_set_word_array_;
O
oceanbase-admin 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
  SqlBitSetDesc desc_;
};

typedef ObSqlBitSet<96, ObExprInfoFlag, true> ObExprInfo;

enum AccessNameType {
  UNKNOWN = -1,
  SYS_FUNC,
  DLL_UDF,
  PL_UDF,
  PL_VAR,
  DB_NS,
  PKG_NS,
  REC_ELEM,
  TYPE_METHOD,
  CURSOR_ATTR,
  UDT_NS,
  LOCAL_TYPE,
  PKG_TYPE,
};

class ObRawExpr;
class ObSysFunRawExpr;
class ObObjAccessIdent {
G
gm 已提交
787
public:
O
oceanbase-admin 已提交
788 789 790
  ObObjAccessIdent()
      : type_(UNKNOWN), access_name_(), access_index_(common::OB_INVALID_INDEX), sys_func_expr_(NULL), params_()
  {}
X
xy0 已提交
791
  ObObjAccessIdent(const common::ObString &name, int64_t index = common::OB_INVALID_INDEX)
O
oceanbase-admin 已提交
792 793 794 795 796
      : type_(UNKNOWN), access_name_(name), access_index_(index), sys_func_expr_(NULL), params_()
  {}
  virtual ~ObObjAccessIdent()
  {}

X
xy0 已提交
797
  int assign(const ObObjAccessIdent &other)
O
oceanbase-admin 已提交
798 799 800 801 802 803 804
  {
    type_ = other.type_;
    access_name_ = other.access_name_;
    access_index_ = other.access_index_;
    sys_func_expr_ = other.sys_func_expr_;
    return params_.assign(other.params_);
  }
X
xy0 已提交
805
  ObObjAccessIdent &operator=(const ObObjAccessIdent &other)
O
oceanbase-admin 已提交
806 807 808 809 810
  {
    assign(other);
    return *this;
  }

G
gm 已提交
811
public:
O
oceanbase-admin 已提交
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 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
  inline void set_type(AccessNameType type)
  {
    type_ = type;
  }
  inline AccessNameType get_type()
  {
    return type_;
  }
  inline void set_sys_func()
  {
    type_ = SYS_FUNC;
  }
  inline void set_dll_udf()
  {
    type_ = DLL_UDF;
  }
  inline void set_db_ns()
  {
    type_ = DB_NS;
  }
  inline void set_pkg_ns()
  {
    type_ = PKG_NS;
  }
  inline void set_rec_elem()
  {
    type_ = REC_ELEM;
  }
  inline void set_type_method()
  {
    type_ = TYPE_METHOD;
  }
  inline bool is_sys_func() const
  {
    return SYS_FUNC == type_;
  }
  inline bool is_dll_udf() const
  {
    return DLL_UDF == type_;
  }
  inline bool is_db_ns() const
  {
    return DB_NS == type_;
  }
  inline bool is_rec_elem() const
  {
    return REC_ELEM == type_;
  }
  inline bool is_type_method() const
  {
    return TYPE_METHOD == type_;
  }
  inline bool is_local_type() const
  {
    return LOCAL_TYPE == type_;
  }
  inline bool is_type() const
  {
    return is_local_type();
  }

X
xy0 已提交
873 874
  int extract_params(int64_t level, common::ObIArray<ObRawExpr *> &params) const;
  int replace_params(ObRawExpr *from, ObRawExpr *to);
O
oceanbase-admin 已提交
875 876 877 878 879 880

  TO_STRING_KV(K_(access_name), K_(access_index), K_(type), K_(params));

  AccessNameType type_;
  common::ObString access_name_;
  int64_t access_index_;
X
xy0 已提交
881
  ObSysFunRawExpr *sys_func_expr_;
O
oceanbase-admin 已提交
882
  // x/y/m/n are parameters of a.f, but the param_level of x/y is 0, and m/n is 1.
X
xy0 已提交
883
  common::ObSEArray<std::pair<ObRawExpr *, int64_t>, 4, common::ModulePageAllocator, true> params_;
O
oceanbase-admin 已提交
884 885 886 887
};

class ObColumnRefRawExpr;
class ObQualifiedName {
G
gm 已提交
888
public:
O
oceanbase-admin 已提交
889 890 891 892 893 894 895 896 897 898 899 900 901 902
  ObQualifiedName()
      : database_name_(),
        tbl_name_(),
        col_name_(),
        is_star_(false),
        ref_expr_(NULL),
        parents_expr_info_(),
        parent_aggr_level_(-1),
        access_idents_(),
        is_access_root_(true)
  {}
  virtual ~ObQualifiedName()
  {}

X
xy0 已提交
903
  int assign(const ObQualifiedName &other)
O
oceanbase-admin 已提交
904 905 906 907 908 909 910 911 912 913 914
  {
    database_name_ = other.database_name_;
    tbl_name_ = other.tbl_name_;
    col_name_ = other.col_name_;
    is_star_ = other.is_star_;
    ref_expr_ = other.ref_expr_;
    parents_expr_info_ = other.parents_expr_info_;
    parent_aggr_level_ = other.parent_aggr_level_;
    is_access_root_ = other.is_access_root_;
    return access_idents_.assign(other.access_idents_);
  }
X
xy0 已提交
915
  ObQualifiedName &operator=(const ObQualifiedName &other)
O
oceanbase-admin 已提交
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
  {
    assign(other);
    return *this;
  }

  void format_qualified_name(common::ObNameCaseMode mode);
  inline bool is_sys_func() const
  {
    return 1 == access_idents_.count() && access_idents_.at(0).is_sys_func();
  }
  inline bool is_dll_udf() const
  {
    return false;
  }
  inline bool is_access_root() const
  {
    return is_access_root_;
  }

X
xy0 已提交
935
  int replace_access_ident_params(ObRawExpr *from, ObRawExpr *to);
O
oceanbase-admin 已提交
936 937 938 939

  TO_STRING_KV(N_DATABASE_NAME, database_name_, N_TABLE_NAME, tbl_name_, N_COLUMN, col_name_, K_(is_star), K_(ref_expr),
      K_(parents_expr_info), K_(parent_aggr_level), K_(access_idents), K_(is_access_root));

G
gm 已提交
940
public:
O
oceanbase-admin 已提交
941 942 943 944
  common::ObString database_name_;
  common::ObString tbl_name_;  // used for package name for UDF
  common::ObString col_name_;  // used for function name for UDF
  bool is_star_;
X
xy0 已提交
945
  ObColumnRefRawExpr *ref_expr_;
O
oceanbase-admin 已提交
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
  ObExprInfo parents_expr_info_;
  int64_t parent_aggr_level_;
  // all identifiers will be stored here, like a/f/c in a.f(x,y).c
  common::ObSEArray<ObObjAccessIdent, 4, common::ModulePageAllocator, true> access_idents_;
  bool is_access_root_;  // qualified name is recursive:
                         // a(b(c)) => b(c) => c, a(b(c)) is root.
};

// bug 6349933: for most of the cases, 8 tables should be more than enough
typedef ObSqlBitSet<8, int64_t, true> ObRelIds;
typedef ObSqlBitSet<common::OB_MAX_SUBQUERY_LAYER_NUM, int64_t, true> ObExprLevels;

/**
 * @brief The ExprCopyPolicy enum
 * Share expr: column, query, aggregation, window function,
               pseudo column, and expr with IS_SHARED_REF
 * COPY_REF_DEFAULT: shallow copy for share expr, deep copy for other expr.
 * COPY_REF_SHARE: deep copy for share expr too.
 * e.g. min(c1),
 *   default mode: return expr pointer directly.
 *   share mode: deep copy min, shallow copy c1, return the copied min expr.
 * every share expr can deep copy themselves, can not deep coy others.
 */
enum ExprCopyPolicy {
  COPY_REF_DEFAULT = 0,
  COPY_REF_SHARED = 1 << 0,
};

struct OrderItem {
  OrderItem()
  {
    reset();
  }
X
xy0 已提交
979
  explicit OrderItem(ObRawExpr *expr) : expr_(expr), order_type_(default_asc_direction())
O
oceanbase-admin 已提交
980
  {}
X
xy0 已提交
981
  OrderItem(ObRawExpr *expr, ObOrderDirection order_type) : expr_(expr), order_type_(order_type)
O
oceanbase-admin 已提交
982 983
  {}

G
gm 已提交
984
public:
O
oceanbase-admin 已提交
985 986 987 988 989 990 991
  virtual ~OrderItem()
  {}
  void reset()
  {
    expr_ = NULL;
    order_type_ = default_asc_direction();
  }
X
xy0 已提交
992
  inline bool operator==(const OrderItem &other) const
O
oceanbase-admin 已提交
993 994 995
  {
    return (expr_ == other.expr_ && order_type_ == other.order_type_);
  }
X
xy0 已提交
996
  inline bool operator!=(const OrderItem &other) const
O
oceanbase-admin 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
  {
    return !(*this == other);
  }
  uint64_t hash(uint64_t seed) const
  {
    if (NULL != expr_) {
      seed = common::do_hash(*expr_, seed);
    }
    seed = common::do_hash(order_type_, seed);

    return seed;
  }

  bool is_null_first() const
  {
    return NULLS_FIRST_ASC == order_type_ || NULLS_FIRST_DESC == order_type_;
  }

  bool is_ascending() const
  {
    return NULLS_FIRST_ASC == order_type_ || NULLS_LAST_ASC == order_type_;
  }

  bool is_descending() const
  {
    return NULLS_FIRST_DESC == order_type_ || NULLS_LAST_DESC == order_type_;
  }

X
xy0 已提交
1025
  int deep_copy(ObRawExprFactory &expr_factory, const OrderItem &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
1026 1027
      bool use_new_allocator = false);

X
xy0 已提交
1028
  static const char *order_type2name(const ObOrderDirection direction)
O
oceanbase-admin 已提交
1029
  {
X
xy0 已提交
1030
    const char *name = NULL;
O
oceanbase-admin 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    switch (direction) {
      case NULLS_FIRST_ASC:
        name = N_NULLS_FIRST_ASC;
        break;
      case NULLS_LAST_ASC:
        name = N_NULLS_LAST_ASC;
        break;
      case NULLS_FIRST_DESC:
        name = N_NULLS_FIRST_DESC;
        break;
      case NULLS_LAST_DESC:
        name = N_NULLS_LAST_DESC;
        break;
      default:
        break;
    }
    return name;
  }

  TO_STRING_KV(N_EXPR, expr_, N_ASCENDING, order_type2name(order_type_));

X
xy0 已提交
1052
  ObRawExpr *expr_;
O
oceanbase-admin 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
  ObOrderDirection order_type_;
};

class ObQueryRefRawExpr;
struct ObExprEqualCheckContext {
  ObExprEqualCheckContext()
      : override_const_compare_(false),
        override_column_compare_(false),
        override_query_compare_(false),
        err_code_(common::OB_SUCCESS),
        param_expr_()
  {}
  virtual ~ObExprEqualCheckContext()
  {}
  struct ParamExprPair {
X
xy0 已提交
1068
    ParamExprPair(int64_t param_idx, const ObRawExpr *expr) : param_idx_(param_idx), expr_(expr)
O
oceanbase-admin 已提交
1069 1070 1071 1072 1073
    {}
    ParamExprPair() : param_idx_(-1), expr_(NULL)
    {}
    TO_STRING_KV(K_(param_idx), K_(expr));
    int64_t param_idx_;
X
xy0 已提交
1074
    const ObRawExpr *expr_;
O
oceanbase-admin 已提交
1075
  };
X
xy0 已提交
1076
  inline int add_param_pair(int64_t param_idx, const ObRawExpr *expr)
O
oceanbase-admin 已提交
1077 1078 1079 1080 1081
  {
    return param_expr_.push_back(ParamExprPair(param_idx, expr));
  }

  // only compare the result type of two columns
X
xy0 已提交
1082
  virtual bool compare_column(const ObColumnRefRawExpr &left, const ObColumnRefRawExpr &right);
O
oceanbase-admin 已提交
1083

X
xy0 已提交
1084
  virtual bool compare_const(const ObConstRawExpr &left, const ObConstRawExpr &right);
O
oceanbase-admin 已提交
1085

X
xy0 已提交
1086
  virtual bool compare_query(const ObQueryRefRawExpr &left, const ObQueryRefRawExpr &right);
O
oceanbase-admin 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112

  void reset()
  {
    override_const_compare_ = false;
    override_column_compare_ = false;
    override_query_compare_ = false;
    err_code_ = OB_SUCCESS;
    param_expr_.reset();
  }
  bool override_const_compare_;
  bool override_column_compare_;
  bool override_query_compare_;
  int err_code_;
  // when compare with T_QUESTIONMARK, as T_QUESTIONMARK is unkown, record this first.
  common::ObSEArray<ParamExprPair, 3, common::ModulePageAllocator, true> param_expr_;
};

struct ObExprParamCheckContext : ObExprEqualCheckContext {
  ObExprParamCheckContext() : ObExprEqualCheckContext(), context_(NULL)
  {
    override_column_compare_ = false;
    override_const_compare_ = true;
    override_query_compare_ = false;
  }
  virtual ~ObExprParamCheckContext()
  {}
X
xy0 已提交
1113 1114
  int init(const ObQueryCtx *context);
  bool compare_const(const ObConstRawExpr &left, const ObConstRawExpr &right) override;
O
oceanbase-admin 已提交
1115

X
xy0 已提交
1116
  int get_calc_expr(const int64_t param_idx, const ObRawExpr *&expr);
O
oceanbase-admin 已提交
1117

X
xy0 已提交
1118
  int is_pre_calc_item(const ObConstRawExpr &const_expr, bool &is_calc);
O
oceanbase-admin 已提交
1119

X
xy0 已提交
1120
  const ObQueryCtx *context_;
O
oceanbase-admin 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
};

enum ObVarType {
  INVALID_VAR = -1,
  SYS_VAR = 0,
  USER_VAR = 1,
};

struct ObVarInfo final {
  OB_UNIS_VERSION(1);

G
gm 已提交
1132
public:
O
oceanbase-admin 已提交
1133 1134
  ObVarInfo() : type_(INVALID_VAR), name_()
  {}
X
xy0 已提交
1135 1136
  int deep_copy(common::ObIAllocator &allocator, ObVarInfo &var_info) const;
  bool operator==(const ObVarInfo &other) const
O
oceanbase-admin 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
  {
    return (type_ == other.type_ && name_ == other.name_);
  }
  TO_STRING_KV(K_(type), K_(name));
  ObVarType type_;
  common::ObString name_;
};

class ObQueryRefRawExpr;
struct ObSubQueryInfo {
  ObSubQueryInfo()
  {
    sub_query_ = NULL;
    ref_expr_ = NULL;
  }
  TO_STRING_KV(K_(ref_expr));

X
xy0 已提交
1154 1155
  const ParseNode *sub_query_;
  ObQueryRefRawExpr *ref_expr_;
O
oceanbase-admin 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
  ObExprInfo parents_expr_info_;
};
class ObAggFunRawExpr;
class ObPseudoColumnRawExpr;
class ObOpRawExpr;
class ObWinFunRawExpr;
class ObUserVarIdentRawExpr;
template <typename ExprFactoryT>
struct ObResolveContext {
  struct ObAggResolveLinkNode : public common::ObDLinkBase<ObAggResolveLinkNode> {
    ObAggResolveLinkNode() : is_win_agg_(false)
    {}
    bool is_win_agg_;
  };
X
xy0 已提交
1170
  ObResolveContext(ExprFactoryT &expr_factory, const common::ObTimeZoneInfo *tz_info, const common::ObNameCaseMode mode)
O
oceanbase-admin 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
      : expr_factory_(expr_factory),
        stmt_(NULL),
        connection_charset_(common::CHARSET_INVALID),
        dest_collation_(common::CS_TYPE_INVALID),
        tz_info_(tz_info),
        case_mode_(mode),
        columns_(NULL),
        sys_vars_(NULL),
        sub_query_info_(NULL),
        aggr_exprs_(NULL),
        win_exprs_(NULL),
        op_exprs_(NULL),
        is_extract_param_type_(true),
        param_list_(NULL),
        prepare_param_count_(0),
        external_param_info_(NULL),
        current_scope_(T_NONE_SCOPE),
        is_win_agg_(false),
        schema_checker_(NULL),
        session_info_(NULL),
        query_ctx_(NULL),
        is_for_pivot_(false),
        is_for_dynamic_sql_(false),
        is_for_dbms_sql_(false)
  {}

X
xy0 已提交
1197 1198
  ExprFactoryT &expr_factory_;
  ObStmt *stmt_;
O
oceanbase-admin 已提交
1199 1200
  common::ObCharsetType connection_charset_;
  common::ObCollationType dest_collation_;
X
xy0 已提交
1201
  const common::ObTimeZoneInfo *tz_info_;
O
oceanbase-admin 已提交
1202 1203
  common::ObNameCaseMode case_mode_;
  ObExprInfo parents_expr_info_;
X
xy0 已提交
1204 1205 1206 1207 1208 1209 1210
  common::ObIArray<ObQualifiedName> *columns_;
  common::ObIArray<ObVarInfo> *sys_vars_;
  common::ObIArray<ObSubQueryInfo> *sub_query_info_;
  common::ObIArray<ObAggFunRawExpr *> *aggr_exprs_;
  common::ObIArray<ObWinFunRawExpr *> *win_exprs_;
  common::ObIArray<ObOpRawExpr *> *op_exprs_;
  common::ObIArray<ObUserVarIdentRawExpr *> *user_var_exprs_;
O
oceanbase-admin 已提交
1211
  bool is_extract_param_type_;
X
xy0 已提交
1212
  const ParamStore *param_list_;
O
oceanbase-admin 已提交
1213
  int64_t prepare_param_count_;
X
xy0 已提交
1214
  common::ObIArray<std::pair<ObRawExpr *, ObConstRawExpr *>> *external_param_info_;  // for anonymous + ps
O
oceanbase-admin 已提交
1215 1216 1217 1218 1219
  ObStmtScope current_scope_;
  common::ObArenaAllocator local_allocator_;
  typedef common::ObDList<ObAggResolveLinkNode> ObAggResolveLink;
  ObAggResolveLink agg_resolve_link_;
  bool is_win_agg_;
X
xy0 已提交
1220 1221 1222 1223
  ObSchemaChecker *schema_checker_;       // we use checker to get udf function name.
  const ObSQLSessionInfo *session_info_;  // we use to get tenant id
  share::schema::ObSchemaGetterGuard *schema_guard_;
  ObQueryCtx *query_ctx_;
O
oceanbase-admin 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
  bool is_for_pivot_;
  bool is_for_dynamic_sql_;
  bool is_for_dbms_sql_;
};

typedef ObResolveContext<ObRawExprFactory> ObExprResolveContext;
class ObRawExprVisitor;
struct ObHiddenColumnItem;

class ObRawExpr : virtual public jit::expr::ObExpr {
G
gm 已提交
1234
public:
X
xy0 已提交
1235 1236
  friend sql::ObExpr *ObStaticEngineExprCG::get_rt_expr(const ObRawExpr &raw_expr);
  friend sql::ObExpr *ObExprOperator::get_rt_expr(const ObRawExpr &raw_expr) const;
O
oceanbase-admin 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256

  explicit ObRawExpr(ObItemType expr_type = T_INVALID)
      : ObExpr(expr_type),
        magic_num_(0x13572468),
        info_(),
        rel_ids_(),
        expr_levels_(),
        inner_alloc_(NULL),
        expr_factory_(NULL),
        expr_level_(-1),
        is_explicited_reference_(false),
        ref_count_(0),
        is_for_generated_column_(false),
        rt_expr_(NULL),
        extra_(0),
        orig_expr_(NULL),
        is_calculated_(false),
        is_deterministic_(true)
  {}

X
xy0 已提交
1257
  explicit ObRawExpr(common::ObIAllocator &alloc, ObItemType expr_type = T_INVALID)
O
oceanbase-admin 已提交
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
      : ObExpr(alloc, expr_type),
        magic_num_(0x13572468),
        info_(),
        rel_ids_(),
        expr_levels_(),
        inner_alloc_(&alloc),
        expr_factory_(NULL),
        expr_level_(-1),
        is_explicited_reference_(false),
        ref_count_(0),
        is_for_generated_column_(false),
        rt_expr_(NULL),
        extra_(0),
        orig_expr_(NULL),
        is_calculated_(false),
        is_deterministic_(true)
  {}
  virtual ~ObRawExpr();
X
xy0 已提交
1276
  int assign(const ObRawExpr &other);
O
oceanbase-admin 已提交
1277
  int deep_copy(
X
xy0 已提交
1278
      ObRawExprFactory &expr_factory, const ObRawExpr &other, const uint64_t types, bool use_new_allocator = false);
O
oceanbase-admin 已提交
1279
  virtual int replace_expr(
X
xy0 已提交
1280
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) = 0;
O
oceanbase-admin 已提交
1281 1282 1283 1284 1285 1286 1287 1288
  virtual void clear_child() = 0;

  virtual void reset();
  /// whether is the same expression.
  /// Compare the two expression tree.
  bool has_generalized_column() const;
  bool has_enum_set_column() const;
  bool has_specified_pseudocolumn() const;
X
xy0 已提交
1289
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const = 0;
O
oceanbase-admin 已提交
1290 1291 1292 1293

  bool is_generalized_column() const;
  void unset_result_flag(uint32_t result_flag);
  // void set_op_factory(ObExprOperatorFactory &factory) { op_factory_ = &factory; }
X
xy0 已提交
1294 1295
  void set_allocator(common::ObIAllocator &alloc);
  void set_expr_factory(ObRawExprFactory &factory)
O
oceanbase-admin 已提交
1296 1297 1298
  {
    expr_factory_ = &factory;
  }
X
xy0 已提交
1299
  ObRawExprFactory *get_expr_factory()
O
oceanbase-admin 已提交
1300 1301 1302 1303
  {
    return expr_factory_;
  }

X
xy0 已提交
1304
  void set_expr_info(const ObExprInfo &info);
O
oceanbase-admin 已提交
1305
  int add_flag(int32_t flag);
X
xy0 已提交
1306 1307
  int add_flags(const ObExprInfo &flags);
  int add_child_flags(const ObExprInfo &flags);
O
oceanbase-admin 已提交
1308 1309 1310 1311
  bool has_flag(ObExprInfoFlag flag) const;
  int clear_flag(int32_t flag);
  bool has_const_or_const_expr_flag() const;
  bool has_hierarchical_query_flag() const;
X
xy0 已提交
1312 1313
  const ObExprInfo &get_expr_info() const;
  ObExprInfo &get_expr_info();
O
oceanbase-admin 已提交
1314

X
xy0 已提交
1315
  void set_relation_ids(const ObRelIds &rel_ids);
O
oceanbase-admin 已提交
1316
  int add_relation_id(int64_t rel_idx);
X
xy0 已提交
1317 1318 1319
  int add_relation_ids(const ObRelIds &rel_ids);
  ObRelIds &get_relation_ids();
  const ObRelIds &get_relation_ids() const;
O
oceanbase-admin 已提交
1320

X
xy0 已提交
1321
  int add_expr_levels(const ObExprLevels &expr_levels)
O
oceanbase-admin 已提交
1322 1323 1324
  {
    return expr_levels_.add_members(expr_levels);
  }
X
xy0 已提交
1325
  ObExprLevels &get_expr_levels()
O
oceanbase-admin 已提交
1326 1327 1328
  {
    return expr_levels_;
  }
X
xy0 已提交
1329
  const ObExprLevels &get_expr_levels() const
O
oceanbase-admin 已提交
1330 1331 1332 1333 1334 1335
  {
    return expr_levels_;
  }

  // implemented base on get_param_count() and get_param_expr() interface,
  // children are visited in get_param_expr() return order.
X
xy0 已提交
1336 1337 1338
  int preorder_accept(ObRawExprVisitor &visitor);
  int postorder_accept(ObRawExprVisitor &visitor);
  int postorder_replace(ObRawExprVisitor &visitor);
O
oceanbase-admin 已提交
1339

X
xy0 已提交
1340
  virtual int do_visit(ObRawExprVisitor &visitor) = 0;
O
oceanbase-admin 已提交
1341 1342 1343 1344 1345 1346 1347 1348

  // skip visit child for expr visitor. (ObSetIterRawExpr)
  virtual bool skip_visit_child() const
  {
    return false;
  }

  virtual int64_t get_param_count() const = 0;
X
xy0 已提交
1349 1350
  virtual const ObRawExpr *get_param_expr(int64_t index) const = 0;
  virtual ObRawExpr *&get_param_expr(int64_t index) = 0;
O
oceanbase-admin 已提交
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
  virtual int64_t get_output_column() const
  {
    return -1;
  }

  inline bool is_not_null() const
  {
    return get_result_type().has_result_flag(OB_MYSQL_NOT_NULL_FLAG);
  }
  inline bool is_auto_increment() const
  {
    return get_result_type().has_result_flag(OB_MYSQL_AUTO_INCREMENT_FLAG);
  }
  inline bool is_rand_func_expr() const
  {
    return has_flag(IS_RAND_FUNC);
  }
  inline bool is_obj_access_expr() const
  {
    return T_OBJ_ACCESS_REF == get_expr_type();
  }
  inline bool is_assoc_index_expr() const
  {
    return T_FUN_PL_ASSOCIATIVE_INDEX == get_expr_type();
  }
1376 1377
  bool is_non_pure_sys_func_expr() const;
  bool is_specified_pseudocolumn_expr() const;
X
xy0 已提交
1378
  void set_alias_column_name(const common::ObString &alias_name)
O
oceanbase-admin 已提交
1379 1380 1381
  {
    alias_column_name_ = alias_name;
  }
X
xy0 已提交
1382
  const common::ObString &get_alias_column_name() const
O
oceanbase-admin 已提交
1383 1384 1385
  {
    return alias_column_name_;
  }
X
xy0 已提交
1386
  inline const common::ObString &get_root_alias_column_name() const
O
oceanbase-admin 已提交
1387 1388 1389
  {
    return OB_NOT_NULL(orig_expr_) ? orig_expr_->get_root_alias_column_name() : alias_column_name_;
  }
X
xy0 已提交
1390
  inline ObRawExpr *get_orig_expr() const
O
oceanbase-admin 已提交
1391 1392 1393
  {
    return orig_expr_;
  }
X
xy0 已提交
1394
  inline const ObRawExpr *get_root_orig_expr() const
O
oceanbase-admin 已提交
1395 1396 1397
  {
    return OB_NOT_NULL(orig_expr_) ? orig_expr_->get_root_orig_expr() : this;
  }
X
xy0 已提交
1398 1399
  int set_expr_name(const common::ObString &expr_name);
  const common::ObString &get_expr_name() const
O
oceanbase-admin 已提交
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
  {
    return expr_name_;
  }
  inline void set_expr_level(int32_t expr_level)
  {
    expr_level_ = expr_level;
  }
  inline int32_t get_expr_level() const
  {
    return expr_level_;
  }
  inline uint64_t hash(uint64_t seed) const
  {
    seed = common::do_hash(type_, seed);
    seed = common::do_hash(expr_class_, seed);
    seed = result_type_.hash(seed);
    seed = hash_internal(seed);
    return seed;
  }
  inline bool is_type_to_str_expr() const
  {
    return (T_FUN_ENUM_TO_STR == type_ || T_FUN_SET_TO_STR == type_ || T_FUN_ENUM_TO_INNER_TYPE == type_ ||
            T_FUN_SET_TO_INNER_TYPE == type_);
  }
  virtual uint64_t hash_internal(uint64_t seed) const = 0;

X
xy0 已提交
1426 1427 1428
  int get_name(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type = EXPLAIN_UNINITIALIZED) const;
  int get_type_and_length(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const;
  virtual int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const = 0;
O
oceanbase-admin 已提交
1429 1430

  // post-processing for expressions
X
xy0 已提交
1431
  int formalize(const ObSQLSessionInfo *my_session);
O
oceanbase-admin 已提交
1432 1433
  int pull_relation_id_and_levels(int32_t cur_stmt_level);
  int extract_info();
X
xj0 已提交
1434 1435 1436
  int deduce_type(const ObSQLSessionInfo* my_session = NULL);
  bool is_bool_expr() const;
  inline ObExprInfo& get_flags()
O
oceanbase-admin 已提交
1437 1438 1439
  {
    return info_;
  }
X
xy0 已提交
1440 1441
  int set_enum_set_values(const common::ObIArray<common::ObString> &values);
  const common::ObIArray<common::ObString> &get_enum_set_values() const
O
oceanbase-admin 已提交
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
  {
    return enum_set_values_;
  }
  bool is_explicited_reference() const
  {
    return is_explicited_reference_;
  }
  void set_explicited_reference()
  {
    ref_count_++;
    is_explicited_reference_ = true;
  }
  void clear_explicited_referece()
  {
    ref_count_ = 0;
    is_explicited_reference_ = false;
  }
  int64_t get_ref_count() const
  {
    return ref_count_;
  }
  bool is_for_generated_column() const
  {
    return is_for_generated_column_;
  }
  void set_for_generated_column()
  {
    is_for_generated_column_ = true;
  }
  void clear_for_generated_column()
  {
    is_for_generated_column_ = false;
  }
X
xy0 已提交
1475
  void set_rt_expr(sql::ObExpr *expr)
O
oceanbase-admin 已提交
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
  {
    rt_expr_ = expr;
  }
  void reset_rt_expr()
  {
    rt_expr_ = NULL;
  }
  void set_extra(uint64_t extra)
  {
    extra_ = extra;
  }
  void set_is_calculated(bool is_calculated)
  {
    is_calculated_ = is_calculated;
  }
  uint64_t get_extra() const
  {
    return extra_;
  }
  bool is_calculated() const
  {
    return is_calculated_;
  }
X
xy0 已提交
1499
  void set_orig_expr(ObRawExpr *expr)
O
oceanbase-admin 已提交
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
  {
    orig_expr_ = expr;
  }
  bool is_deterministic() const
  {
    return is_deterministic_;
  }
  void set_is_deterministic(bool is_deterministic)
  {
    is_deterministic_ = is_deterministic;
  }
  VIRTUAL_TO_STRING_KV(N_ITEM_TYPE, type_, N_RESULT_TYPE, result_type_, N_EXPR_INFO, info_, N_REL_ID, rel_ids_,
      K_(expr_level), K_(expr_levels), K_(enum_set_values), K_(is_explicited_reference), K_(ref_count),
      K_(is_for_generated_column), K_(extra), K_(is_calculated));

G
gm 已提交
1515
public:
O
oceanbase-admin 已提交
1516 1517
  uint32_t magic_num_;

G
gm 已提交
1518
protected:
O
oceanbase-admin 已提交
1519 1520 1521
  static const int64_t COMMON_MULTI_NUM = 16;
  static const int64_t COMMON_ENUM_SET_VALUE_NUM = 4;

G
gm 已提交
1522
protected:
O
oceanbase-admin 已提交
1523 1524 1525 1526
  ObExprInfo info_;   // flags
  ObRelIds rel_ids_;  // related table idx
  // means the raw expr contain which level variables(column, aggregate expr, set expr or subquery expr)
  ObExprLevels expr_levels_;
X
xy0 已提交
1527 1528
  common::ObIAllocator *inner_alloc_;
  ObRawExprFactory *expr_factory_;
O
oceanbase-admin 已提交
1529 1530
  common::ObString alias_column_name_;
  int32_t expr_level_;
1531
  common::ObSEArray<common::ObString, 1, common::ModulePageAllocator, true> enum_set_values_;  //string_map
O
oceanbase-admin 已提交
1532 1533 1534 1535 1536
  common::ObString expr_name_;
  // for column expr, agg expr, window function expr and query ref exprs
  bool is_explicited_reference_;
  int64_t ref_count_;
  bool is_for_generated_column_;
X
xy0 已提交
1537
  sql::ObExpr *rt_expr_;
O
oceanbase-admin 已提交
1538 1539

  uint64_t extra_;
X
xy0 已提交
1540
  ObRawExpr *orig_expr_;   // orig raw expr before pre cast of pre calc.
O
oceanbase-admin 已提交
1541 1542
  bool is_calculated_;     // for code gerenation in static engine.
  bool is_deterministic_;  // expr is deterministic, given the same inputs, returns the same result
G
gm 已提交
1543
private:
O
oceanbase-admin 已提交
1544 1545 1546
  DISALLOW_COPY_AND_ASSIGN(ObRawExpr);
};

X
xy0 已提交
1547
inline void ObRawExpr::set_allocator(ObIAllocator &alloc)
O
oceanbase-admin 已提交
1548 1549 1550 1551 1552 1553 1554 1555 1556
{
  inner_alloc_ = &alloc;
  result_type_.set_allocator(&alloc);
}

inline void ObRawExpr::unset_result_flag(uint32_t result_flag)
{
  result_type_.unset_result_flag(result_flag);
}
X
xy0 已提交
1557
inline void ObRawExpr::set_relation_ids(const ObRelIds &rel_ids)
O
oceanbase-admin 已提交
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
{
  rel_ids_ = rel_ids;
}

inline int ObRawExpr::add_relation_id(int64_t rel_idx)
{
  int ret = common::OB_SUCCESS;
  if (rel_idx < 0) {
    ret = common::OB_INVALID_ARGUMENT;
  } else {
    ret = rel_ids_.add_member(rel_idx);
  }
  return ret;
}

X
xy0 已提交
1573
inline int ObRawExpr::add_relation_ids(const ObRelIds &rel_ids)
O
oceanbase-admin 已提交
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
{
  return rel_ids_.add_members(rel_ids);
}

inline int ObRawExpr::add_flag(int32_t flag)
{
  int ret = common::OB_SUCCESS;
  if (OB_FAIL(info_.add_member(flag))) {
    // add_member will print log,here no need
  } else if (flag <= IS_INFO_MASK_END) {
    if (OB_FAIL(info_.add_member(CNT_INFO_MASK_BEGIN + flag))) {
      // add_member will print log,here no need
    }
  } else {
  }
  return ret;
}

inline int ObRawExpr::clear_flag(int32_t flag)
{
  return info_.del_member(flag);
}

inline bool ObRawExpr::has_const_or_const_expr_flag() const
{
  return has_flag(IS_CONST) || has_flag(IS_CONST_EXPR);
}

inline bool ObRawExpr::has_hierarchical_query_flag() const
{
  return has_flag(CNT_PRIOR) || has_flag(CNT_LEVEL) || has_flag(CNT_CONNECT_BY_ISLEAF) ||
         has_flag(CNT_CONNECT_BY_ISCYCLE) || has_flag(CNT_CONNECT_BY_ROOT) || has_flag(CNT_SYS_CONNECT_BY_PATH);
  ;
}

X
xy0 已提交
1609
inline int ObRawExpr::add_flags(const ObExprInfo &flags)
O
oceanbase-admin 已提交
1610 1611 1612 1613 1614 1615 1616 1617
{
  return info_.add_members(flags);
}

inline bool ObRawExpr::has_flag(ObExprInfoFlag flag) const
{
  return info_.has_member(flag);
}
X
xy0 已提交
1618
inline bool ObRawExpr::same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context) const
O
oceanbase-admin 已提交
1619 1620 1621 1622 1623
{
  UNUSED(check_context);
  return (get_expr_type() == expr.get_expr_type() && get_result_type() == expr.get_result_type());
}

X
xy0 已提交
1624
inline void ObRawExpr::set_expr_info(const ObExprInfo &info)
O
oceanbase-admin 已提交
1625 1626 1627
{
  info_ = info;
}
X
xy0 已提交
1628
inline ObExprInfo &ObRawExpr::get_expr_info()
O
oceanbase-admin 已提交
1629 1630 1631
{
  return info_;
}
X
xy0 已提交
1632
inline const ObExprInfo &ObRawExpr::get_expr_info() const
O
oceanbase-admin 已提交
1633 1634 1635
{
  return info_;
}
X
xy0 已提交
1636
inline ObRelIds &ObRawExpr::get_relation_ids()
O
oceanbase-admin 已提交
1637 1638 1639
{
  return rel_ids_;
}
X
xy0 已提交
1640
inline const ObRelIds &ObRawExpr::get_relation_ids() const
O
oceanbase-admin 已提交
1641 1642 1643 1644 1645 1646
{
  return rel_ids_;
}

////////////////////////////////////////////////////////////////
class ObTerminalRawExpr : public ObRawExpr {
G
gm 已提交
1647
public:
O
oceanbase-admin 已提交
1648 1649
  explicit ObTerminalRawExpr(ObItemType expr_type = T_INVALID) : ObRawExpr(expr_type)
  {}
X
xy0 已提交
1650
  explicit ObTerminalRawExpr(common::ObIAllocator &alloc, ObItemType expr_type = T_INVALID)
O
oceanbase-admin 已提交
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
      : ObRawExpr(alloc, expr_type)
  {}
  virtual ~ObTerminalRawExpr()
  {}
  virtual void clear_child()
  {}

  virtual int64_t get_param_count() const
  {
    return 0;
  }
X
xy0 已提交
1662
  virtual const ObRawExpr *get_param_expr(int64_t index) const
O
oceanbase-admin 已提交
1663 1664 1665 1666
  {
    UNUSED(index);
    return NULL;
  }
X
xy0 已提交
1667
  virtual ObRawExpr *&get_param_expr(int64_t index);
O
oceanbase-admin 已提交
1668 1669 1670 1671 1672
  virtual uint64_t hash_internal(uint64_t seed) const
  {
    return seed;
  }

G
gm 已提交
1673 1674
protected:
private:
O
oceanbase-admin 已提交
1675 1676 1677 1678 1679
  DISALLOW_COPY_AND_ASSIGN(ObTerminalRawExpr);
};

////////////////////////////////////////////////////////////////
class ObConstRawExpr : public ObTerminalRawExpr, public jit::expr::ObConstExpr {
G
gm 已提交
1680
public:
X
xj0 已提交
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
  ObConstRawExpr() 
    : is_date_unit_(false), 
      is_literal_bool_(false)/*: precalc_expr_(NULL)*/
  { 
    ObRawExpr::set_expr_class(ObRawExpr::EXPR_CONST); 
  }
  ObConstRawExpr(common::ObIAllocator& alloc)
      : ObExpr(alloc), 
        ObTerminalRawExpr(alloc), 
        ObConstExpr(), 
        is_date_unit_(false),
        is_literal_bool_(false)
  {
    ObRawExpr::set_expr_class(ObRawExpr::EXPR_CONST);
  }
  ObConstRawExpr(const oceanbase::common::ObObj& val, ObItemType expr_type = T_INVALID)
      : ObExpr(expr_type), 
        ObTerminalRawExpr(expr_type), 
        ObConstExpr(), 
        is_date_unit_(false),
        is_literal_bool_(false)
O
oceanbase-admin 已提交
1702 1703 1704 1705 1706 1707
  {
    set_value(val);
    set_expr_class(ObExpr::EXPR_CONST);
  }
  virtual ~ObConstRawExpr()
  {}
X
xy0 已提交
1708
  int assign(const ObConstRawExpr &other);
O
oceanbase-admin 已提交
1709

X
xy0 已提交
1710
  int deep_copy(ObRawExprFactory &expr_factory, const ObConstRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
1711 1712
      bool use_new_allocator = false);
  virtual int replace_expr(
X
xy0 已提交
1713 1714 1715 1716
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
  void set_value(const oceanbase::common::ObObj &val);
  void set_literal_prefix(const common::ObString &name);
  void set_expr_obj_meta(const common::ObObjMeta &meta)
O
oceanbase-admin 已提交
1717 1718 1719
  {
    obj_meta_ = meta;
  }
X
xy0 已提交
1720
  const common::ObObjMeta &get_expr_obj_meta() const
O
oceanbase-admin 已提交
1721 1722 1723
  {
    return obj_meta_;
  }
X
xy0 已提交
1724
  const common::ObString &get_literal_prefix() const
O
oceanbase-admin 已提交
1725 1726 1727 1728 1729 1730 1731 1732 1733
  {
    return literal_prefix_;
  }
  void set_is_date_unit();
  void reset_is_date_unit();
  bool is_date_unit()
  {
    return true == is_date_unit_;
  }
L
LINxiansheng 已提交
1734
  virtual void reset() override;
X
xy0 已提交
1735
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
1736

X
xy0 已提交
1737
  virtual int do_visit(ObRawExprVisitor &visitor) override;
O
oceanbase-admin 已提交
1738

L
LINxiansheng 已提交
1739
  virtual uint64_t hash_internal(uint64_t seed) const override;
X
xj0 已提交
1740 1741 1742
  int get_name_internal(char* buf, const int64_t buf_len, int64_t& pos, ExplainType type) const override;
  void set_is_literal_bool(const bool is_literal_bool) { is_literal_bool_ = is_literal_bool; }
  bool is_literal_bool() const { return is_literal_bool_; }
O
oceanbase-admin 已提交
1743 1744
  DECLARE_VIRTUAL_TO_STRING;

G
gm 已提交
1745
private:
O
oceanbase-admin 已提交
1746 1747 1748
  common::ObString literal_prefix_;  // used in compile phase.
  common::ObObjMeta obj_meta_;
  bool is_date_unit_;
X
xj0 已提交
1749 1750
  // for mysql mode to distinguish tinyint and literal bool
  bool is_literal_bool_;
O
oceanbase-admin 已提交
1751

G
gm 已提交
1752
private:
O
oceanbase-admin 已提交
1753 1754 1755 1756 1757
  DISALLOW_COPY_AND_ASSIGN(ObConstRawExpr);
};

////////////////////////////////////////////////////////////////
class ObVarRawExpr : public ObTerminalRawExpr, public jit::expr::ObVarExpr {
G
gm 已提交
1758
public:
O
oceanbase-admin 已提交
1759 1760 1761 1762
  ObVarRawExpr()
  {
    ObExpr::set_expr_class(ObExpr::EXPR_VAR);
  }
X
xy0 已提交
1763
  ObVarRawExpr(common::ObIAllocator &alloc)
O
oceanbase-admin 已提交
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
      : ObExpr(alloc), ObTerminalRawExpr(alloc), ObVarExpr(), result_type_assigned_(false)
  {
    ObExpr::set_expr_class(ObExpr::EXPR_VAR);
  }
  ObVarRawExpr(ObItemType expr_type = T_INVALID)
      : ObExpr(expr_type), ObTerminalRawExpr(expr_type), ObVarExpr(), result_type_assigned_(false)
  {
    set_expr_class(ObExpr::EXPR_VAR);
  }
  virtual ~ObVarRawExpr()
  {}

X
xy0 已提交
1776
  int assign(const ObVarRawExpr &other);
O
oceanbase-admin 已提交
1777

X
xy0 已提交
1778
  int deep_copy(ObRawExprFactory &expr_factory, const ObVarRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
1779 1780
      bool use_new_allocator = false);
  virtual int replace_expr(
X
xy0 已提交
1781 1782
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
1783

X
xy0 已提交
1784
  virtual int do_visit(ObRawExprVisitor &visitor) override;
O
oceanbase-admin 已提交
1785

X
xy0 已提交
1786
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
O
oceanbase-admin 已提交
1787 1788 1789 1790 1791 1792 1793 1794 1795
  void set_result_type_assigned(bool v)
  {
    result_type_assigned_ = v;
  }
  bool get_result_type_assigned()
  {
    return result_type_assigned_;
  }

G
gm 已提交
1796
private:
O
oceanbase-admin 已提交
1797 1798 1799 1800 1801 1802
  bool result_type_assigned_;
  DISALLOW_COPY_AND_ASSIGN(ObVarRawExpr);
};

////////////////////////////////////////////////////////////////
class ObUserVarIdentRawExpr : public ObConstRawExpr {
G
gm 已提交
1803
public:
O
oceanbase-admin 已提交
1804 1805
  ObUserVarIdentRawExpr() : is_contain_assign_(false), query_has_udf_(false)
  {}
X
xy0 已提交
1806
  ObUserVarIdentRawExpr(common::ObIAllocator &alloc)
O
oceanbase-admin 已提交
1807 1808
      : ObConstRawExpr(alloc), is_contain_assign_(false), query_has_udf_(false)
  {}
X
xy0 已提交
1809
  ObUserVarIdentRawExpr(const oceanbase::common::ObObj &val, ObItemType expr_type = T_INVALID)
O
oceanbase-admin 已提交
1810 1811 1812 1813
      : ObConstRawExpr(val, expr_type), is_contain_assign_(false), query_has_udf_(false)
  {}
  virtual ~ObUserVarIdentRawExpr()
  {}
X
xy0 已提交
1814
  int assign(const ObUserVarIdentRawExpr &other);
O
oceanbase-admin 已提交
1815

X
xy0 已提交
1816
  int deep_copy(ObRawExprFactory &expr_factory, const ObUserVarIdentRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
1817 1818
      bool use_new_allocator = false);
  virtual int replace_expr(
X
xy0 已提交
1819
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
L
LINxiansheng 已提交
1820
  virtual void reset() override;
X
xy0 已提交
1821
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
1822

X
xy0 已提交
1823
  virtual int do_visit(ObRawExprVisitor &visitor) override;
O
oceanbase-admin 已提交
1824

L
LINxiansheng 已提交
1825
  virtual uint64_t hash_internal(uint64_t seed) const override;
O
oceanbase-admin 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842

  bool get_is_contain_assign() const
  {
    return is_contain_assign_;
  }
  void set_is_contain_assign(bool is_contain_assign)
  {
    is_contain_assign_ = is_contain_assign;
  }
  bool get_query_has_udf() const
  {
    return query_has_udf_;
  }
  void set_query_has_udf(bool query_has_udf)
  {
    query_has_udf_ = query_has_udf;
  }
X
xy0 已提交
1843
  bool is_same_variable(const ObObj &obj) const;
O
oceanbase-admin 已提交
1844 1845
  DECLARE_VIRTUAL_TO_STRING;

G
gm 已提交
1846
private:
O
oceanbase-admin 已提交
1847 1848 1849
  bool is_contain_assign_;
  bool query_has_udf_;

G
gm 已提交
1850
private:
O
oceanbase-admin 已提交
1851 1852 1853 1854 1855 1856
  DISALLOW_COPY_AND_ASSIGN(ObUserVarIdentRawExpr);
};

////////////////////////////////////////////////////////////////
class ObLogicalOperator;
class ObQueryRefRawExpr : public ObTerminalRawExpr {
G
gm 已提交
1857
public:
O
oceanbase-admin 已提交
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
  ObQueryRefRawExpr()
      : ObTerminalRawExpr(),
        ref_id_(common::OB_INVALID_ID),
        output_column_(0),
        is_set_(false),
        is_cursor_(false),
        ref_type_(OB_STMT)
  {
    ref_stmt_ = NULL;
    ref_operator_ = NULL;
    set_expr_class(ObExpr::EXPR_QUERY_REF);
  }

X
xy0 已提交
1871
  ObQueryRefRawExpr(common::ObIAllocator &alloc)
O
oceanbase-admin 已提交
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
      : ObTerminalRawExpr(alloc),
        ref_id_(common::OB_INVALID_ID),
        output_column_(0),
        is_set_(false),
        is_cursor_(false),
        ref_type_(OB_STMT)
  {
    ref_stmt_ = NULL;
    ref_operator_ = NULL;
    set_expr_class(ObExpr::EXPR_QUERY_REF);
  }
  ObQueryRefRawExpr(int64_t id, ObItemType expr_type = T_INVALID)
      : ObTerminalRawExpr(expr_type),
        ref_id_(id),
        output_column_(0),
        is_set_(false),
        is_cursor_(false),
        ref_type_(OB_STMT)
  {
    ref_stmt_ = NULL;
    ref_operator_ = NULL;
    set_expr_class(ObExpr::EXPR_QUERY_REF);
  }
  virtual ~ObQueryRefRawExpr()
  {}
X
xy0 已提交
1897 1898
  int assign(const ObQueryRefRawExpr &other);
  int deep_copy(ObStmtFactory &stmt_factory, ObRawExprFactory &expr_factory, const ObQueryRefRawExpr &other);
O
oceanbase-admin 已提交
1899
  virtual int replace_expr(
X
xy0 已提交
1900
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
O
oceanbase-admin 已提交
1901 1902
  int64_t get_ref_id() const;
  void set_ref_id(int64_t id);
X
xy0 已提交
1903 1904 1905
  ObSelectStmt *get_ref_stmt();
  const ObSelectStmt *get_ref_stmt() const;
  void set_ref_stmt(ObSelectStmt *ref_stmt)
O
oceanbase-admin 已提交
1906 1907 1908 1909
  {
    ref_stmt_ = ref_stmt;
    ref_type_ = OB_STMT;
  }
X
xy0 已提交
1910
  ObLogicalOperator *get_ref_operator()
O
oceanbase-admin 已提交
1911 1912 1913
  {
    return (OB_LOGICAL_OPERATOR == ref_type_ ? ref_operator_ : NULL);
  }
X
xy0 已提交
1914
  const ObLogicalOperator *get_ref_operator() const
O
oceanbase-admin 已提交
1915 1916 1917
  {
    return (OB_LOGICAL_OPERATOR == ref_type_ ? ref_operator_ : NULL);
  }
X
xy0 已提交
1918
  void set_ref_operator(ObLogicalOperator *ref_opeator)
O
oceanbase-admin 已提交
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
  {
    ref_operator_ = ref_opeator;
    ref_type_ = OB_LOGICAL_OPERATOR;
  }
  bool is_ref_stmt() const
  {
    return OB_STMT == ref_type_;
  }
  bool is_ref_operator() const
  {
    return OB_LOGICAL_OPERATOR == ref_type_;
  }
  void set_output_column(int64_t output_column);
L
LINxiansheng 已提交
1932
  int64_t get_output_column() const override;
X
xy0 已提交
1933
  int add_column_type(const ObExprResType &type)
O
oceanbase-admin 已提交
1934 1935 1936
  {
    return column_types_.push_back(type);
  }
X
xy0 已提交
1937
  const common::ObIArray<ObExprResType> &get_column_types() const
O
oceanbase-admin 已提交
1938 1939 1940
  {
    return column_types_;
  }
X
xy0 已提交
1941
  common::ObIArray<ObExprResType> &get_column_types()
O
oceanbase-admin 已提交
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
  {
    return column_types_;
  }
  void set_is_set(bool is_set)
  {
    is_set_ = is_set;
  }
  bool is_set() const
  {
    return is_set_;
  }
  void set_cursor(bool is_cursor)
  {
    is_cursor_ = is_cursor;
  }
  bool is_cursor() const
  {
    return is_cursor_;
  }
L
LINxiansheng 已提交
1961
  virtual void reset() override;
X
xy0 已提交
1962
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
1963

X
xy0 已提交
1964
  virtual int do_visit(ObRawExprVisitor &visitor) override;
O
oceanbase-admin 已提交
1965

L
LINxiansheng 已提交
1966
  virtual uint64_t hash_internal(uint64_t seed) const override
O
oceanbase-admin 已提交
1967 1968 1969 1970
  {
    return common::do_hash(ref_id_, seed);
  }

X
xy0 已提交
1971
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
O
oceanbase-admin 已提交
1972 1973 1974 1975
  VIRTUAL_TO_STRING_KV(N_ITEM_TYPE, type_, N_RESULT_TYPE, result_type_, N_EXPR_INFO, info_, N_REL_ID, rel_ids_, N_ID,
      ref_id_, K_(expr_level), K_(expr_levels), K_(output_column), K_(is_set), K_(is_cursor), K_(column_types),
      K_(enum_set_values));

G
gm 已提交
1976
private:
O
oceanbase-admin 已提交
1977 1978 1979
  DISALLOW_COPY_AND_ASSIGN(ObQueryRefRawExpr);
  int64_t ref_id_;
  union {
X
xy0 已提交
1980 1981
    ObSelectStmt *ref_stmt_;
    ObLogicalOperator *ref_operator_;
O
oceanbase-admin 已提交
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
  };
  enum RefType { OB_STMT = 0, OB_LOGICAL_OPERATOR = 1 };
  int64_t output_column_;
  bool is_set_;
  bool is_cursor_;
  common::ObSEArray<ObExprResType, 64, common::ModulePageAllocator, true> column_types_;
  RefType ref_type_;
};

inline int64_t ObQueryRefRawExpr::get_ref_id() const
{
  return ref_id_;
}
inline void ObQueryRefRawExpr::set_ref_id(int64_t id)
{
  ref_id_ = id;
}

inline void ObQueryRefRawExpr::set_output_column(int64_t output_column)
{
  output_column_ = output_column;
}

inline int64_t ObQueryRefRawExpr::get_output_column() const
{
  return output_column_;
}

////////////////////////////////////////////////////////////////
class ObColumnRefRawExpr : public ObTerminalRawExpr, public jit::expr::ObColumnRefExpr {
G
gm 已提交
2012
public:
O
oceanbase-admin 已提交
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
  ObColumnRefRawExpr()
      : ObExpr(),
        ObTerminalRawExpr(),
        jit::expr::ObColumnRefExpr(),
        table_id_(common::OB_INVALID_ID),
        column_id_(common::OB_INVALID_ID),
        database_name_(),
        table_name_(),
        synonym_name_(),
        synonym_db_name_(),
        column_name_(),
        column_flags_(0),
        dependant_expr_(NULL),
        is_lob_column_(false),
        is_unpivot_mocked_column_(false),
        is_hidden_(false),
2029
        from_alias_table_(false),
O
oceanbase-admin 已提交
2030 2031 2032 2033 2034
        real_expr_(nullptr)
  {
    set_expr_class(ObExpr::EXPR_COLUMN_REF);
  }

X
xy0 已提交
2035
  ObColumnRefRawExpr(common::ObIAllocator &alloc)
O
oceanbase-admin 已提交
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
      : ObExpr(alloc),
        ObTerminalRawExpr(alloc),
        ObColumnRefExpr(alloc),
        table_id_(common::OB_INVALID_ID),
        column_id_(common::OB_INVALID_ID),
        database_name_(),
        table_name_(),
        synonym_name_(),
        synonym_db_name_(),
        column_name_(),
        column_flags_(0),
        dependant_expr_(NULL),
        is_lob_column_(false),
        is_unpivot_mocked_column_(false),
        is_hidden_(false),
2051
        from_alias_table_(false),
O
oceanbase-admin 已提交
2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
        real_expr_(nullptr)
  {
    set_expr_class(ObExpr::EXPR_COLUMN_REF);
  }

  ObColumnRefRawExpr(uint64_t first_id, uint64_t second_id, ObItemType expr_type = T_INVALID)
      : ObExpr(expr_type),
        ObTerminalRawExpr(expr_type),
        ObColumnRefExpr(expr_type),
        table_id_(first_id),
        column_id_(second_id),
        database_name_(),
        table_name_(),
        synonym_name_(),
        synonym_db_name_(),
        column_name_(),
        column_flags_(0),
        dependant_expr_(NULL),
        is_lob_column_(false),
        is_unpivot_mocked_column_(false),
        is_hidden_(false),
2073
        from_alias_table_(false),
O
oceanbase-admin 已提交
2074 2075 2076 2077 2078 2079 2080
        real_expr_(nullptr)
  {
    set_expr_class(ObExpr::EXPR_COLUMN_REF);
  }

  virtual ~ObColumnRefRawExpr()
  {}
X
xy0 已提交
2081 2082
  int assign(const ObColumnRefRawExpr &other);
  int deep_copy(ObRawExprFactory &expr_factory, const ObColumnRefRawExpr &other, bool use_new_allocator = false);
O
oceanbase-admin 已提交
2083
  virtual int replace_expr(
X
xy0 已提交
2084
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
O
oceanbase-admin 已提交
2085 2086
  uint64_t get_table_id() const;
  uint64_t get_column_id() const;
X
xy0 已提交
2087 2088
  uint64_t &get_table_id();
  uint64_t &get_column_id();
O
oceanbase-admin 已提交
2089 2090
  void set_ref_id(uint64_t table_id, uint64_t column_id);
  void set_table_id(uint64_t table_id);
X
xy0 已提交
2091 2092
  void set_column_attr(const common::ObString &table_name, const common::ObString &column_name);
  inline void set_table_name(const common::ObString &table_name)
O
oceanbase-admin 已提交
2093 2094 2095
  {
    table_name_ = table_name;
  }
X
xy0 已提交
2096
  inline common::ObString &get_table_name()
O
oceanbase-admin 已提交
2097 2098 2099
  {
    return table_name_;
  }
X
xy0 已提交
2100
  inline const common::ObString &get_table_name() const
O
oceanbase-admin 已提交
2101 2102 2103
  {
    return table_name_;
  }
X
xy0 已提交
2104
  inline void set_synonym_name(const common::ObString &synonym_name)
O
oceanbase-admin 已提交
2105 2106 2107
  {
    synonym_name_ = synonym_name;
  }
X
xy0 已提交
2108
  inline common::ObString &get_synonym_name()
O
oceanbase-admin 已提交
2109 2110 2111
  {
    return synonym_name_;
  }
X
xy0 已提交
2112
  inline const common::ObString &get_synonym_name() const
O
oceanbase-admin 已提交
2113 2114 2115
  {
    return synonym_name_;
  }
X
xy0 已提交
2116
  inline void set_synonym_db_name(const common::ObString &synonym_db_name)
O
oceanbase-admin 已提交
2117 2118 2119
  {
    synonym_db_name_ = synonym_db_name;
  }
X
xy0 已提交
2120
  inline common::ObString &get_synonym_db_name()
O
oceanbase-admin 已提交
2121 2122 2123
  {
    return synonym_db_name_;
  }
X
xy0 已提交
2124
  inline const common::ObString &get_synonym_db_name() const
O
oceanbase-admin 已提交
2125 2126 2127
  {
    return synonym_db_name_;
  }
X
xy0 已提交
2128
  inline void set_column_name(const common::ObString &column_name)
O
oceanbase-admin 已提交
2129 2130 2131
  {
    column_name_ = column_name;
  }
X
xy0 已提交
2132
  inline common::ObString &get_column_name()
O
oceanbase-admin 已提交
2133 2134 2135
  {
    return column_name_;
  }
X
xy0 已提交
2136
  inline const common::ObString &get_column_name() const
O
oceanbase-admin 已提交
2137 2138 2139
  {
    return column_name_;
  }
X
xy0 已提交
2140
  inline void set_database_name(const common::ObString &db_name)
O
oceanbase-admin 已提交
2141 2142 2143
  {
    database_name_ = db_name;
  }
X
xy0 已提交
2144
  inline const common::ObString &get_database_name() const
O
oceanbase-admin 已提交
2145 2146 2147
  {
    return database_name_;
  }
X
xy0 已提交
2148
  inline common::ObString &get_database_name()
O
oceanbase-admin 已提交
2149 2150 2151 2152 2153 2154 2155
  {
    return database_name_;
  }
  inline int64_t get_cte_generate_column_projector_offset() const
  {
    return get_column_id();
  }
L
LINxiansheng 已提交
2156
  virtual void reset() override;
X
xy0 已提交
2157
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
2158

X
xy0 已提交
2159
  virtual int do_visit(ObRawExprVisitor &visitor) override;
O
oceanbase-admin 已提交
2160

L
LINxiansheng 已提交
2161
  virtual uint64_t hash_internal(uint64_t seed) const override;
2162

X
xy0 已提交
2163 2164 2165 2166 2167 2168 2169 2170
  bool is_from_alias_table() const
  {
    return from_alias_table_;
  }
  void set_from_alias_table(bool value)
  {
    from_alias_table_ = value;
  }
O
oceanbase-admin 已提交
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
  inline bool is_generated_column() const
  {
    return share::schema::ObSchemaUtils::is_generated_column(column_flags_);
  }
  inline bool is_default_expr_v2_column() const
  {
    return share::schema::ObSchemaUtils::is_default_expr_v2_column(column_flags_);
  }
  inline bool is_virtual_generated_column() const
  {
    return share::schema::ObSchemaUtils::is_virtual_generated_column(column_flags_);
  }
  inline bool is_stored_generated_column() const
  {
    return share::schema::ObSchemaUtils::is_stored_generated_column(column_flags_);
  }
  inline bool is_fulltext_column() const
  {
    return share::schema::ObSchemaUtils::is_fulltext_column(column_flags_);
  }
  inline bool is_cte_generated_column() const
  {
    return share::schema::ObSchemaUtils::is_cte_generated_column(column_flags_);
  }
  inline bool has_generated_column_deps() const
  {
    return column_flags_ & GENERATED_DEPS_CASCADE_FLAG;
  }
  inline bool is_table_part_key_column() const
  {
    return column_flags_ & TABLE_PART_KEY_COLUMN_FLAG;
  }
  void set_column_flags(uint64_t column_flags)
  {
    column_flags_ = column_flags;
  }
  inline uint64_t get_column_flags() const
  {
    return column_flags_;
  }
X
xy0 已提交
2211
  inline const ObRawExpr *get_dependant_expr() const
O
oceanbase-admin 已提交
2212 2213 2214
  {
    return dependant_expr_;
  }
X
xy0 已提交
2215
  inline ObRawExpr *get_dependant_expr()
O
oceanbase-admin 已提交
2216 2217 2218
  {
    return dependant_expr_;
  }
X
xy0 已提交
2219
  inline void set_dependant_expr(ObRawExpr *expr)
O
oceanbase-admin 已提交
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
  {
    dependant_expr_ = expr;
  }
  bool is_lob_column() const
  {
    return is_lob_column_;
  }
  void set_lob_column(bool is_lob_column)
  {
    is_lob_column_ = is_lob_column;
  }
  bool is_unpivot_mocked_column() const
  {
    return is_unpivot_mocked_column_;
  }
  void set_unpivot_mocked_column(const bool value)
  {
    is_unpivot_mocked_column_ = value;
  }
  bool is_hidden_column() const
  {
    return is_hidden_;
  }
  void set_hidden_column(const bool value)
  {
    is_hidden_ = value;
  }

X
xy0 已提交
2248
  inline ObRawExpr *get_real_expr()
O
oceanbase-admin 已提交
2249 2250 2251
  {
    return real_expr_;
  }
X
xy0 已提交
2252
  inline void set_real_expr(ObRawExpr *expr)
O
oceanbase-admin 已提交
2253 2254 2255 2256
  {
    real_expr_ = expr;
  }

X
xy0 已提交
2257
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
O
oceanbase-admin 已提交
2258 2259 2260 2261 2262 2263

  VIRTUAL_TO_STRING_KV(N_ITEM_TYPE, type_, N_RESULT_TYPE, result_type_, N_EXPR_INFO, info_, N_REL_ID, rel_ids_, N_TID,
      table_id_, N_CID, column_id_, K_(database_name), K_(table_name), K_(synonym_name), K_(synonym_db_name),
      K_(column_name), K_(expr_level), K_(expr_levels), K_(column_flags), K_(enum_set_values), K_(is_lob_column),
      K_(is_unpivot_mocked_column), K_(is_hidden));

G
gm 已提交
2264
private:
O
oceanbase-admin 已提交
2265 2266 2267 2268 2269 2270 2271 2272 2273
  DISALLOW_COPY_AND_ASSIGN(ObColumnRefRawExpr);
  uint64_t table_id_;
  uint64_t column_id_;
  common::ObString database_name_;
  common::ObString table_name_;
  common::ObString synonym_name_;
  common::ObString synonym_db_name_;
  common::ObString column_name_;
  uint64_t column_flags_;          // same as flags in ObColumnSchemaV2
X
xy0 已提交
2274
  ObRawExpr *dependant_expr_;      // TODO
O
oceanbase-admin 已提交
2275 2276 2277
  bool is_lob_column_;             // TODO add lob column
  bool is_unpivot_mocked_column_;  // used for unpivot
  bool is_hidden_;                 // used for print hidden column
2278
  bool from_alias_table_;
X
xy0 已提交
2279
  ObRawExpr *real_expr_;  // for oracle virtual table that is mapping a real table
O
oceanbase-admin 已提交
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
};

inline void ObColumnRefRawExpr::set_ref_id(uint64_t table_id, uint64_t column_id)
{
  table_id_ = table_id;
  column_id_ = column_id;
}

inline void ObColumnRefRawExpr::set_table_id(uint64_t table_id)
{
  table_id_ = table_id;
}

inline uint64_t ObColumnRefRawExpr::get_table_id() const
{
  return table_id_;
}

inline uint64_t ObColumnRefRawExpr::get_column_id() const
{
  return column_id_;
}

X
xy0 已提交
2303
inline uint64_t &ObColumnRefRawExpr::get_table_id()
O
oceanbase-admin 已提交
2304 2305 2306 2307
{
  return table_id_;
}

X
xy0 已提交
2308
inline uint64_t &ObColumnRefRawExpr::get_column_id()
O
oceanbase-admin 已提交
2309 2310 2311 2312 2313 2314
{
  return column_id_;
}

////////////////////////////////////////////////////////////////
class ObSetOpRawExpr : public ObTerminalRawExpr {
G
gm 已提交
2315
public:
O
oceanbase-admin 已提交
2316 2317 2318 2319
  ObSetOpRawExpr() : ObTerminalRawExpr(), idx_(-1)
  {
    set_expr_class(ObExpr::EXPR_SET_OP);
  }
X
xy0 已提交
2320
  ObSetOpRawExpr(common::ObIAllocator &alloc) : ObTerminalRawExpr(alloc), idx_(-1)
O
oceanbase-admin 已提交
2321 2322 2323 2324 2325
  {
    set_expr_class(ObExpr::EXPR_SET_OP);
  }
  virtual ~ObSetOpRawExpr()
  {}
X
xy0 已提交
2326
  virtual int do_visit(ObRawExprVisitor &visitor) override;
O
oceanbase-admin 已提交
2327
  virtual int replace_expr(
X
xy0 已提交
2328 2329 2330
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
2331 2332

  int deep_copy(
X
xy0 已提交
2333
      ObRawExprFactory &expr_factory, const ObSetOpRawExpr &other, const uint64_t copy_types, bool use_new_allocator);
O
oceanbase-admin 已提交
2334

X
xy0 已提交
2335
  int assign(const ObSetOpRawExpr &other);
O
oceanbase-admin 已提交
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347

  void set_idx(int64_t idx)
  {
    idx_ = idx;
  }
  int64_t get_idx() const
  {
    return idx_;
  }
  VIRTUAL_TO_STRING_KV(N_ITEM_TYPE, type_, N_RESULT_TYPE, result_type_, N_EXPR_INFO, info_, N_REL_ID, rel_ids_,
      K_(expr_level), K_(expr_levels), K_(idx));

G
gm 已提交
2348
private:
O
oceanbase-admin 已提交
2349 2350 2351 2352 2353 2354
  DISALLOW_COPY_AND_ASSIGN(ObSetOpRawExpr);
  int64_t idx_;
};

////////////////////////////////////////////////////////////////
class ObAliasRefRawExpr : public ObRawExpr {
G
gm 已提交
2355
public:
O
oceanbase-admin 已提交
2356 2357 2358 2359
  ObAliasRefRawExpr() : ObRawExpr(), ref_expr_(NULL)
  {
    set_expr_class(ObExpr::EXPR_ALIAS_REF);
  }
X
xy0 已提交
2360
  ObAliasRefRawExpr(common::ObIAllocator &alloc) : ObRawExpr(alloc), ref_expr_(NULL), project_index_(OB_INVALID_INDEX)
O
oceanbase-admin 已提交
2361 2362 2363 2364 2365 2366
  {
    set_expr_class(ObExpr::EXPR_ALIAS_REF);
  }

  virtual ~ObAliasRefRawExpr()
  {}
X
xy0 已提交
2367 2368
  int assign(const ObAliasRefRawExpr &other);
  int deep_copy(ObRawExprFactory &expr_factory, const ObAliasRefRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
2369 2370
      bool use_new_allocator = false);
  virtual int replace_expr(
X
xy0 已提交
2371 2372 2373 2374
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
  const ObRawExpr *get_ref_expr() const;
  ObRawExpr *get_ref_expr();
  void set_ref_expr(ObRawExpr *ref_expr)
O
oceanbase-admin 已提交
2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385
  {
    ref_expr_ = ref_expr;
  }
  bool is_ref_query_output() const
  {
    return ref_expr_->is_query_ref_expr() && project_index_ != OB_INVALID_INDEX;
  }
  int64_t get_project_index() const
  {
    return project_index_;
  }
X
xy0 已提交
2386
  void set_query_output(ObQueryRefRawExpr *query_ref, int64_t project_index)
O
oceanbase-admin 已提交
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
  {
    ref_expr_ = query_ref;
    project_index_ = project_index;
  }
  virtual void clear_child() override
  {
    ref_expr_ = NULL;
    project_index_ = OB_INVALID_INDEX;
  }
  virtual int64_t get_param_count() const override
  {
    return 1;
  }
X
xy0 已提交
2400 2401 2402
  virtual const ObRawExpr *get_param_expr(int64_t index) const override;
  virtual ObRawExpr *&get_param_expr(int64_t index) override;
  virtual int do_visit(ObRawExprVisitor &visitor) override;
L
LINxiansheng 已提交
2403
  virtual uint64_t hash_internal(uint64_t seed) const override;
X
xy0 已提交
2404 2405
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
2406
  VIRTUAL_TO_STRING_KV_CHECK_STACK_OVERFLOW(N_ITEM_TYPE, type_, N_RESULT_TYPE, result_type_, N_EXPR_INFO, info_, N_REL_ID, rel_ids_, N_VALUE,
O
oceanbase-admin 已提交
2407 2408
      ref_expr_, K_(enum_set_values));

G
gm 已提交
2409
private:
O
oceanbase-admin 已提交
2410
  DISALLOW_COPY_AND_ASSIGN(ObAliasRefRawExpr);
X
xy0 已提交
2411
  ObRawExpr *ref_expr_;
O
oceanbase-admin 已提交
2412 2413 2414 2415 2416
  int64_t project_index_;  // project index of the subquery
};

////////////////////////////////////////////////////////////////
class ObNonTerminalRawExpr : public ObRawExpr {
G
gm 已提交
2417
public:
O
oceanbase-admin 已提交
2418 2419
  ObNonTerminalRawExpr() : ObRawExpr(), op_(NULL), input_types_()
  {}
X
xy0 已提交
2420
  ObNonTerminalRawExpr(common::ObIAllocator &alloc) : ObRawExpr(alloc), op_(NULL), input_types_()
O
oceanbase-admin 已提交
2421 2422 2423 2424 2425 2426
  {}
  virtual ~ObNonTerminalRawExpr()
  {
    free_op();
  }

X
xy0 已提交
2427 2428
  int assign(const ObNonTerminalRawExpr &other);
  int deep_copy(ObRawExprFactory &expr_factory, const ObNonTerminalRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
2429 2430
      bool use_new_allocator = false);
  virtual int replace_expr(
X
xy0 已提交
2431
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
L
LINxiansheng 已提交
2432
  virtual void reset() override
O
oceanbase-admin 已提交
2433 2434 2435 2436 2437
  {
    free_op();
    input_types_.reset();
  }

X
xy0 已提交
2438
  virtual ObExprOperator *get_op();
O
oceanbase-admin 已提交
2439 2440 2441 2442
  void free_op();
  /*
   * store the target type of paramters.
   */
X
xy0 已提交
2443
  int set_input_types(const ObIExprResTypes &input_types);
O
oceanbase-admin 已提交
2444

X
xy0 已提交
2445
  inline const ObExprResTypes &get_input_types()
O
oceanbase-admin 已提交
2446 2447 2448 2449 2450 2451 2452 2453
  {
    return input_types_;
  }
  inline int64_t get_input_types_count() const
  {
    return input_types_.count();
  }

L
LINxiansheng 已提交
2454
  virtual uint64_t hash_internal(uint64_t seed) const override
O
oceanbase-admin 已提交
2455 2456 2457 2458
  {
    return seed;
  }

G
gm 已提交
2459
protected:
O
oceanbase-admin 已提交
2460
  // data members
X
xy0 已提交
2461
  ObExprOperator *op_;
O
oceanbase-admin 已提交
2462 2463 2464 2465 2466 2467
  ObExprResTypes input_types_;
  DISALLOW_COPY_AND_ASSIGN(ObNonTerminalRawExpr);
};

////////////////////////////////////////////////////////////////
class ObOpRawExpr : public ObNonTerminalRawExpr, public jit::expr::ObOpExpr {
G
gm 已提交
2468
public:
O
oceanbase-admin 已提交
2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479
  ObOpRawExpr()
      : ObExpr(),
        ObNonTerminalRawExpr(),
        ObOpExpr(),
        exprs_(),
        subquery_key_(T_WITH_NONE),
        deduce_type_adding_implicit_cast_(true)
  {
    set_expr_class(ObExpr::EXPR_OPERATOR);
  }

X
xy0 已提交
2480
  ObOpRawExpr(common::ObIAllocator &alloc)
O
oceanbase-admin 已提交
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490
      : ObExpr(alloc),
        ObNonTerminalRawExpr(alloc),
        ObOpExpr(alloc),
        exprs_(),
        subquery_key_(T_WITH_NONE),
        deduce_type_adding_implicit_cast_(true)
  {
    set_expr_class(ObExpr::EXPR_OPERATOR);
  }

X
xy0 已提交
2491
  ObOpRawExpr(ObRawExpr *first_expr, ObRawExpr *second_expr, ObItemType type);  // binary op
O
oceanbase-admin 已提交
2492 2493
  virtual ~ObOpRawExpr()
  {}
X
xy0 已提交
2494 2495
  int assign(const ObOpRawExpr &other);
  int deep_copy(ObRawExprFactory &expr_factory, const ObOpRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
2496 2497
      bool use_new_allocator = false);
  virtual int replace_expr(
X
xy0 已提交
2498 2499 2500 2501 2502
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
  int set_param_expr(ObRawExpr *expr);                                                        // unary op
  int set_param_exprs(ObRawExpr *first_expr, ObRawExpr *second_expr);                         // binary op
  int set_param_exprs(ObRawExpr *first_expr, ObRawExpr *second_expr, ObRawExpr *third_expr);  // triple op
  int add_param_expr(ObRawExpr *expr);
O
oceanbase-admin 已提交
2503
  int remove_param_expr(int64_t index);
X
xy0 已提交
2504
  int replace_param_expr(int64_t index, ObRawExpr *expr);
O
oceanbase-admin 已提交
2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516

  bool deduce_type_adding_implicit_cast() const
  {
    return deduce_type_adding_implicit_cast_;
  }
  void set_deduce_type_adding_implicit_cast(const bool v)
  {
    deduce_type_adding_implicit_cast_ = v;
  }

  void set_expr_type(ObItemType type);

X
xy0 已提交
2517
  common::ObIArray<ObRawExpr *> &get_param_exprs()
O
oceanbase-admin 已提交
2518 2519 2520
  {
    return exprs_;
  }
X
xy0 已提交
2521
  const common::ObIArray<ObRawExpr *> &get_param_exprs() const
O
oceanbase-admin 已提交
2522 2523 2524 2525
  {
    return exprs_;
  }

L
LINxiansheng 已提交
2526
  int64_t get_param_count() const override;
X
xy0 已提交
2527 2528
  const ObRawExpr *get_param_expr(int64_t index) const override;
  ObRawExpr *&get_param_expr(int64_t index) override;
L
LINxiansheng 已提交
2529
  virtual int64_t get_output_column() const override
O
oceanbase-admin 已提交
2530 2531 2532 2533
  {
    return T_OP_ROW == get_expr_type() ? get_param_count() : -1;
  }

L
LINxiansheng 已提交
2534 2535
  virtual void clear_child() override;
  virtual void reset() override;
X
xy0 已提交
2536
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
2537 2538

  // used for jit expr
L
LINxiansheng 已提交
2539
  virtual int64_t get_children_count() const override
O
oceanbase-admin 已提交
2540 2541 2542 2543
  {
    return exprs_.count();
  }
  // used for jit expr
X
xy0 已提交
2544
  virtual int get_children(jit::expr::ExprArray &jit_exprs) const override;
O
oceanbase-admin 已提交
2545

X
xy0 已提交
2546
  void set_subquery_key(ObSubQueryKey &key)
O
oceanbase-admin 已提交
2547 2548 2549 2550 2551 2552 2553 2554
  {
    subquery_key_ = key;
  }
  ObSubQueryKey get_subquery_key()
  {
    return subquery_key_;
  }

X
xy0 已提交
2555
  virtual int do_visit(ObRawExprVisitor &visitor) override;
O
oceanbase-admin 已提交
2556

L
LINxiansheng 已提交
2557
  virtual uint64_t hash_internal(uint64_t seed) const override;
O
oceanbase-admin 已提交
2558

X
xy0 已提交
2559
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
O
oceanbase-admin 已提交
2560
  int get_subquery_comparison_name(
X
xy0 已提交
2561
      const common::ObString &symbol, char *buf, int64_t buf_len, int64_t &pos, ExplainType type) const;
2562 2563
  VIRTUAL_TO_STRING_KV_CHECK_STACK_OVERFLOW(N_ITEM_TYPE, type_, N_RESULT_TYPE, result_type_, N_EXPR_INFO, info_,
      N_REL_ID, rel_ids_, K_(expr_levels), N_CHILDREN, exprs_);
O
oceanbase-admin 已提交
2564

G
gm 已提交
2565
protected:
X
xy0 已提交
2566
  common::ObSEArray<ObRawExpr *, COMMON_MULTI_NUM, common::ModulePageAllocator, true> exprs_;
O
oceanbase-admin 已提交
2567 2568 2569 2570
  ObSubQueryKey subquery_key_;

  bool deduce_type_adding_implicit_cast_;

G
gm 已提交
2571
private:
O
oceanbase-admin 已提交
2572 2573 2574
  DISALLOW_COPY_AND_ASSIGN(ObOpRawExpr);
};

X
xy0 已提交
2575
inline const ObRawExpr *ObOpRawExpr::get_param_expr(int64_t index) const
O
oceanbase-admin 已提交
2576
{
X
xy0 已提交
2577
  const ObRawExpr *expr = NULL;
O
oceanbase-admin 已提交
2578 2579 2580 2581 2582 2583 2584
  if (index >= 0 && index < exprs_.count()) {
    expr = exprs_.at(index);
  }
  return expr;
}

// here must return in two branch, because ret value is a *&
X
xy0 已提交
2585
inline ObRawExpr *&ObOpRawExpr::get_param_expr(int64_t index)
O
oceanbase-admin 已提交
2586 2587 2588 2589 2590 2591 2592 2593
{
  if (index >= 0 && index < exprs_.count()) {
    return exprs_.at(index);
  } else {
    return USELESS_POINTER;
  }
}

X
xy0 已提交
2594
inline int ObOpRawExpr::add_param_expr(ObRawExpr *expr)
O
oceanbase-admin 已提交
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
{
  return exprs_.push_back(expr);
}

inline int ObOpRawExpr::remove_param_expr(int64_t index)
{
  int ret = common::OB_SUCCESS;
  if (index < 0 || index >= exprs_.count()) {
    ret = common::OB_INVALID_ARGUMENT;
  } else {
    ret = exprs_.remove(index);
  }
  return ret;
}

X
xy0 已提交
2610
inline int ObOpRawExpr::replace_param_expr(int64_t index, ObRawExpr *expr)
O
oceanbase-admin 已提交
2611 2612 2613 2614 2615
{
  int ret = common::OB_SUCCESS;
  if (index < 0 || index >= exprs_.count()) {
    ret = common::OB_INVALID_ARGUMENT;
  } else {
X
xy0 已提交
2616
    ObRawExpr *&target_expr = exprs_.at(index);
O
oceanbase-admin 已提交
2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642
    if (OB_NOT_NULL(expr) && expr != target_expr) {
      expr->set_orig_expr(target_expr);
    }
    target_expr = expr;
  }
  return ret;
}

inline int64_t ObOpRawExpr::get_param_count() const
{
  return exprs_.count();
}

inline uint64_t ObOpRawExpr::hash_internal(uint64_t seed) const
{
  uint64_t hash_value = seed;
  for (int64_t i = 0; i < exprs_.count(); ++i) {
    if (NULL != exprs_.at(i)) {
      hash_value = common::do_hash(*(exprs_.at(i)), hash_value);
    }
  }
  return hash_value;
}

////////////////////////////////////////////////////////////////
class ObCaseOpRawExpr : public ObNonTerminalRawExpr, public jit::expr::ObCaseOpExpr {
G
gm 已提交
2643
public:
O
oceanbase-admin 已提交
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655
  ObCaseOpRawExpr()
      : ObExpr(),
        ObNonTerminalRawExpr(),
        ObCaseOpExpr(),
        arg_expr_(NULL),
        when_exprs_(),
        then_exprs_(),
        default_expr_(NULL),
        is_decode_func_(false)
  {
    set_expr_class(ObExpr::EXPR_CASE_OPERATOR);
  }
X
xy0 已提交
2656
  ObCaseOpRawExpr(common::ObIAllocator &alloc)
O
oceanbase-admin 已提交
2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669
      : ObExpr(alloc),
        ObNonTerminalRawExpr(alloc),
        ObCaseOpExpr(),
        arg_expr_(NULL),
        when_exprs_(),
        then_exprs_(),
        default_expr_(NULL),
        is_decode_func_(false)
  {
    set_expr_class(ObExpr::EXPR_CASE_OPERATOR);
  }
  virtual ~ObCaseOpRawExpr()
  {}
X
xy0 已提交
2670 2671
  int assign(const ObCaseOpRawExpr &other);
  int deep_copy(ObRawExprFactory &expr_factory, const ObCaseOpRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
2672 2673
      bool use_new_allocator = false);
  virtual int replace_expr(
X
xy0 已提交
2674 2675 2676 2677 2678 2679 2680
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
  const ObRawExpr *get_arg_param_expr() const;
  const ObRawExpr *get_default_param_expr() const;
  const ObRawExpr *get_when_param_expr(int64_t index) const;
  const ObRawExpr *get_then_param_expr(int64_t index) const;
  ObRawExpr *&get_arg_param_expr();
  inline common::ObIArray<ObRawExpr *> &get_when_param_exprs()
O
oceanbase-admin 已提交
2681 2682 2683
  {
    return when_exprs_;
  }
X
xy0 已提交
2684
  inline common::ObIArray<ObRawExpr *> &get_then_param_exprs()
O
oceanbase-admin 已提交
2685 2686 2687
  {
    return then_exprs_;
  }
X
xy0 已提交
2688 2689 2690 2691 2692 2693 2694 2695 2696 2697
  ObRawExpr *&get_default_param_expr();
  ObRawExpr *&get_when_param_expr(int64_t index);
  ObRawExpr *&get_then_param_expr(int64_t index);
  void set_arg_param_expr(ObRawExpr *expr);
  void set_default_param_expr(ObRawExpr *expr);
  int add_when_param_expr(ObRawExpr *expr);
  int add_then_param_expr(ObRawExpr *expr);
  int replace_when_param_expr(int64_t index, ObRawExpr *expr);
  int replace_then_param_expr(int64_t index, ObRawExpr *expr);
  int replace_param_expr(int64_t index, ObRawExpr *new_expr);
O
oceanbase-admin 已提交
2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
  int64_t get_when_expr_size() const;
  int64_t get_then_expr_size() const;
  bool is_arg_case() const
  {
    return NULL != arg_expr_;
  }
  bool is_decode_func() const
  {
    return is_decode_func_;
  }

L
LINxiansheng 已提交
2709 2710
  virtual void clear_child() override;
  virtual void reset() override;
X
xy0 已提交
2711
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
2712

X
xy0 已提交
2713
  virtual int do_visit(ObRawExprVisitor &visitor) override;
O
oceanbase-admin 已提交
2714

L
LINxiansheng 已提交
2715
  virtual int64_t get_param_count() const override;
X
xy0 已提交
2716 2717
  virtual const ObRawExpr *get_param_expr(int64_t index) const override;
  virtual ObRawExpr *&get_param_expr(int64_t index) override;
O
oceanbase-admin 已提交
2718 2719

  // used for jit
L
LINxiansheng 已提交
2720
  virtual int64_t get_children_count() const override
O
oceanbase-admin 已提交
2721 2722 2723 2724
  {
    return get_param_count();
  }

X
xy0 已提交
2725
  virtual int get_children(jit::expr::ExprArray &jit_exprs) const override;
O
oceanbase-admin 已提交
2726

L
LINxiansheng 已提交
2727
  virtual uint64_t hash_internal(uint64_t seed) const override;
O
oceanbase-admin 已提交
2728

X
xy0 已提交
2729
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
2730
  VIRTUAL_TO_STRING_KV_CHECK_STACK_OVERFLOW(N_ITEM_TYPE, type_, N_RESULT_TYPE, result_type_, N_EXPR_INFO, info_, N_REL_ID, rel_ids_,
O
oceanbase-admin 已提交
2731 2732 2733
      K_(expr_levels), N_ARG_CASE, arg_expr_, N_DEFAULT, default_expr_, N_WHEN, when_exprs_, N_THEN, then_exprs_,
      N_DECODE, is_decode_func_);

G
gm 已提交
2734
private:
O
oceanbase-admin 已提交
2735
  DISALLOW_COPY_AND_ASSIGN(ObCaseOpRawExpr);
X
xy0 已提交
2736 2737 2738 2739
  ObRawExpr *arg_expr_;
  common::ObSEArray<ObRawExpr *, COMMON_MULTI_NUM, common::ModulePageAllocator, true> when_exprs_;
  common::ObSEArray<ObRawExpr *, COMMON_MULTI_NUM, common::ModulePageAllocator, true> then_exprs_;
  ObRawExpr *default_expr_;
O
oceanbase-admin 已提交
2740 2741 2742
  bool is_decode_func_;
};

X
xy0 已提交
2743
inline const ObRawExpr *ObCaseOpRawExpr::get_arg_param_expr() const
O
oceanbase-admin 已提交
2744 2745 2746
{
  return arg_expr_;
}
X
xy0 已提交
2747
inline const ObRawExpr *ObCaseOpRawExpr::get_default_param_expr() const
O
oceanbase-admin 已提交
2748 2749 2750
{
  return default_expr_;
}
X
xy0 已提交
2751
inline const ObRawExpr *ObCaseOpRawExpr::get_when_param_expr(int64_t index) const
O
oceanbase-admin 已提交
2752
{
X
xy0 已提交
2753
  ObRawExpr *expr = NULL;
O
oceanbase-admin 已提交
2754 2755 2756 2757 2758 2759
  if (OB_LIKELY(index >= 0 && index < when_exprs_.count())) {
    expr = when_exprs_.at(index);
  } else {
  }
  return expr;
}
X
xy0 已提交
2760
inline const ObRawExpr *ObCaseOpRawExpr::get_then_param_expr(int64_t index) const
O
oceanbase-admin 已提交
2761
{
X
xy0 已提交
2762
  ObRawExpr *expr = NULL;
O
oceanbase-admin 已提交
2763 2764 2765 2766 2767 2768
  if (OB_LIKELY(index >= 0 || index < then_exprs_.count())) {
    expr = then_exprs_.at(index);
  } else {
  }
  return expr;
}
X
xy0 已提交
2769
inline ObRawExpr *&ObCaseOpRawExpr::get_arg_param_expr()
O
oceanbase-admin 已提交
2770 2771 2772
{
  return arg_expr_;
}
X
xy0 已提交
2773
inline ObRawExpr *&ObCaseOpRawExpr::get_default_param_expr()
O
oceanbase-admin 已提交
2774 2775 2776 2777 2778
{
  return default_expr_;
}

// here must return in two branch, because ret value is a *&
X
xy0 已提交
2779
inline ObRawExpr *&ObCaseOpRawExpr::get_when_param_expr(int64_t index)
O
oceanbase-admin 已提交
2780 2781 2782 2783 2784 2785 2786 2787 2788
{
  if (OB_LIKELY(index >= 0 && index < when_exprs_.count())) {
    return when_exprs_.at(index);
  } else {
    return USELESS_POINTER;
  }
}

// here must return in two branch, because ret value is a *&
X
xy0 已提交
2789
inline ObRawExpr *&ObCaseOpRawExpr::get_then_param_expr(int64_t index)
O
oceanbase-admin 已提交
2790 2791 2792 2793 2794 2795 2796
{
  if (OB_LIKELY(index >= 0 && index < then_exprs_.count())) {
    return then_exprs_.at(index);
  } else {
    return USELESS_POINTER;
  }
}
X
xy0 已提交
2797
inline void ObCaseOpRawExpr::set_arg_param_expr(ObRawExpr *expr)
O
oceanbase-admin 已提交
2798 2799 2800
{
  arg_expr_ = expr;
}
X
xy0 已提交
2801
inline void ObCaseOpRawExpr::set_default_param_expr(ObRawExpr *expr)
O
oceanbase-admin 已提交
2802 2803 2804
{
  default_expr_ = expr;
}
X
xy0 已提交
2805
inline int ObCaseOpRawExpr::add_when_param_expr(ObRawExpr *expr)
O
oceanbase-admin 已提交
2806 2807 2808 2809
{
  int ret = when_exprs_.push_back(expr);
  return ret;
}
X
xy0 已提交
2810
inline int ObCaseOpRawExpr::add_then_param_expr(ObRawExpr *expr)
O
oceanbase-admin 已提交
2811 2812 2813 2814 2815
{
  int ret = then_exprs_.push_back(expr);
  return ret;
}

X
xy0 已提交
2816
inline int ObCaseOpRawExpr::replace_when_param_expr(int64_t index, ObRawExpr *expr)
O
oceanbase-admin 已提交
2817 2818 2819 2820 2821 2822 2823 2824 2825 2826
{
  int ret = common::OB_SUCCESS;
  if (OB_UNLIKELY((index < 0 || index >= when_exprs_.count()))) {
    ret = common::OB_ERR_UNEXPECTED;
  } else {
    when_exprs_.at(index) = expr;
  }
  return ret;
}

X
xy0 已提交
2827
inline int ObCaseOpRawExpr::replace_then_param_expr(int64_t index, ObRawExpr *expr)
O
oceanbase-admin 已提交
2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
{
  int ret = common::OB_SUCCESS;
  if (OB_UNLIKELY((index < 0 || index >= then_exprs_.count()))) {
    ret = common::OB_ERR_UNEXPECTED;
  } else {
    then_exprs_.at(index) = expr;
  }
  return ret;
}

inline int64_t ObCaseOpRawExpr::get_when_expr_size() const
{
  return when_exprs_.count();
}
inline int64_t ObCaseOpRawExpr::get_then_expr_size() const
{
  return then_exprs_.count();
}
inline int64_t ObCaseOpRawExpr::get_param_count() const
{
  return when_exprs_.count() + then_exprs_.count() + (NULL == arg_expr_ ? 0 : 1) + (NULL == default_expr_ ? 0 : 1);
}
inline uint64_t ObCaseOpRawExpr::hash_internal(uint64_t seed) const
{
  if (arg_expr_) {
    seed = common::do_hash(*arg_expr_, seed);
  }

  for (int64_t i = 0; i < when_exprs_.count(); ++i) {
    if (OB_LIKELY(NULL != when_exprs_.at(i))) {
      seed = common::do_hash(*(when_exprs_.at(i)), seed);
    }
  }
  for (int64_t i = 0; i < then_exprs_.count(); ++i) {
    if (OB_LIKELY(NULL != then_exprs_.at(i))) {
      seed = common::do_hash(*(then_exprs_.at(i)), seed);
    }
  }

  if (NULL != default_expr_) {
    seed = common::do_hash(*default_expr_, seed);
  }

  seed = common::do_hash(is_decode_func_, seed);

  return seed;
}

////////////////////////////////////////////////////////////////
class ObAggFunRawExpr : public ObRawExpr {
G
gm 已提交
2878
public:
O
oceanbase-admin 已提交
2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894
  ObAggFunRawExpr()
      : ObRawExpr(),
        real_param_exprs_(),
        push_down_sum_expr_(NULL),
        push_down_count_expr_(NULL),
        push_down_synopsis_expr_(NULL),
        distinct_(false),
        order_items_(),
        separator_param_expr_(NULL),
        udf_meta_(),
        linear_inter_expr_(NULL),
        is_nested_aggr_(false)
  {
    set_expr_class(ObExpr::EXPR_AGGR);
    order_items_.set_label(common::ObModIds::OB_SQL_AGGR_FUNC_ARR);
  }
X
xy0 已提交
2895
  ObAggFunRawExpr(common::ObIAllocator &alloc)
O
oceanbase-admin 已提交
2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
      : ObRawExpr(alloc),
        real_param_exprs_(),
        push_down_sum_expr_(NULL),
        push_down_count_expr_(NULL),
        push_down_synopsis_expr_(NULL),
        distinct_(false),
        order_items_(),
        separator_param_expr_(NULL),
        udf_meta_(),
        linear_inter_expr_(NULL),
        is_nested_aggr_(false)
  {
    set_expr_class(ObExpr::EXPR_AGGR);
    order_items_.set_label(common::ObModIds::OB_SQL_AGGR_FUNC_ARR);
  }
X
xy0 已提交
2911
  ObAggFunRawExpr(const common::ObSEArray<ObRawExpr *, 1, common::ModulePageAllocator, true> &real_param_exprs,
O
oceanbase-admin 已提交
2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
      bool is_distinct, ObItemType expr_type = T_INVALID)
      : ObRawExpr(expr_type),
        real_param_exprs_(real_param_exprs),
        push_down_sum_expr_(NULL),
        push_down_count_expr_(NULL),
        push_down_synopsis_expr_(NULL),
        distinct_(is_distinct),
        order_items_(),
        separator_param_expr_(NULL),
        udf_meta_(),
        linear_inter_expr_(NULL),
        is_nested_aggr_(false)
  {
    set_expr_class(ObExpr::EXPR_AGGR);
    order_items_.set_label(common::ObModIds::OB_SQL_AGGR_FUNC_ARR);
  }
  virtual ~ObAggFunRawExpr()
  {}
X
xy0 已提交
2930
  int assign(const ObAggFunRawExpr &other);
O
oceanbase-admin 已提交
2931

X
xy0 已提交
2932
  int deep_copy(ObRawExprFactory &expr_factory, const ObAggFunRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
2933 2934
      bool use_new_allocator = false);
  virtual int replace_expr(
X
xy0 已提交
2935 2936 2937 2938 2939 2940
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
  int add_real_param_expr(ObRawExpr *expr);
  int replace_real_param_expr(int64_t index, ObRawExpr *expr);
  int replace_param_expr(int64_t index, ObRawExpr *expr);
  void set_push_down_sum_expr(ObRawExpr *expr);
  void set_push_down_count_expr(ObRawExpr *expr);
O
oceanbase-admin 已提交
2941 2942 2943
  bool contain_nested_aggr() const;
  bool is_param_distinct() const;
  void set_param_distinct(bool is_distinct);
X
xy0 已提交
2944 2945
  void set_separator_param_expr(ObRawExpr *separator_param_expr);
  void set_linear_inter_expr(ObRawExpr *linear_inter_expr);
O
oceanbase-admin 已提交
2946 2947
  bool is_nested_aggr() const;
  void set_in_nested_aggr(bool is_nested);
X
xy0 已提交
2948
  int add_order_item(const OrderItem &order_item);
L
LINxiansheng 已提交
2949 2950
  virtual void clear_child() override;
  virtual void reset() override;
X
xy0 已提交
2951
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
2952

L
LINxiansheng 已提交
2953
  virtual int64_t get_param_count() const override
O
oceanbase-admin 已提交
2954 2955 2956
  {
    return real_param_exprs_.count() + order_items_.count();
  }
X
xy0 已提交
2957 2958
  virtual const ObRawExpr *get_param_expr(int64_t index) const override;
  virtual ObRawExpr *&get_param_expr(int64_t index) override;
O
oceanbase-admin 已提交
2959 2960 2961 2962
  inline int64_t get_real_param_count() const
  {
    return real_param_exprs_.count();
  }
X
xy0 已提交
2963
  inline const common::ObIArray<ObRawExpr *> &get_real_param_exprs() const
O
oceanbase-admin 已提交
2964 2965 2966
  {
    return real_param_exprs_;
  }
X
xy0 已提交
2967
  inline common::ObIArray<ObRawExpr *> &get_real_param_exprs_for_update()
O
oceanbase-admin 已提交
2968 2969 2970
  {
    return real_param_exprs_;
  }
X
xy0 已提交
2971 2972
  virtual int do_visit(ObRawExprVisitor &visitor) override;
  inline ObRawExpr *&get_push_down_sum_expr()
O
oceanbase-admin 已提交
2973 2974 2975
  {
    return push_down_sum_expr_;
  }
X
xy0 已提交
2976
  inline ObRawExpr *&get_push_down_count_expr()
O
oceanbase-admin 已提交
2977 2978 2979
  {
    return push_down_count_expr_;
  }
X
xy0 已提交
2980
  inline ObRawExpr *&get_push_down_synopsis_expr()
O
oceanbase-admin 已提交
2981 2982 2983
  {
    return push_down_synopsis_expr_;
  }
X
xy0 已提交
2984
  inline ObRawExpr *get_separator_param_expr() const
O
oceanbase-admin 已提交
2985 2986 2987
  {
    return separator_param_expr_;
  }
X
xy0 已提交
2988
  inline ObRawExpr *get_linear_inter_expr() const
O
oceanbase-admin 已提交
2989 2990 2991
  {
    return linear_inter_expr_;
  }
X
xy0 已提交
2992
  inline const common::ObIArray<OrderItem> &get_order_items() const
O
oceanbase-admin 已提交
2993 2994 2995
  {
    return order_items_;
  }
X
xy0 已提交
2996
  inline common::ObIArray<OrderItem> &get_order_items_for_update()
O
oceanbase-admin 已提交
2997 2998 2999 3000
  {
    return order_items_;
  }

L
LINxiansheng 已提交
3001
  virtual uint64_t hash_internal(uint64_t seed) const override
O
oceanbase-admin 已提交
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018
  {
    for (int64_t i = 0; i < real_param_exprs_.count(); ++i) {
      if (OB_LIKELY(NULL != real_param_exprs_.at(i))) {
        seed = common::do_hash(*real_param_exprs_.at(i), seed);
      }
    }
    seed = common::do_hash(distinct_, seed);
    for (int64_t i = 0; i < order_items_.count(); ++i) {
      seed = common::do_hash(order_items_.at(i), seed);
    }
    if (NULL != separator_param_expr_) {
      seed = common::do_hash(*separator_param_expr_, seed);
    }
    return seed;
  }

  // set udf meta to this expr
X
xy0 已提交
3019
  int set_udf_meta(const share::schema::ObUDF &udf);
O
oceanbase-admin 已提交
3020 3021 3022 3023 3024
  const share::schema::ObUDFMeta get_udf_meta()
  {
    return udf_meta_;
  }

X
xy0 已提交
3025 3026
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
  const char *get_name_dblink(ObItemType expr_type) const;
3027 3028 3029 3030
  VIRTUAL_TO_STRING_KV_CHECK_STACK_OVERFLOW(N_ITEM_TYPE, type_, N_RESULT_TYPE, result_type_, N_EXPR_INFO, info_,
      N_REL_ID, rel_ids_, K_(expr_level), K_(expr_levels), N_CHILDREN, real_param_exprs_, N_DISTINCT, distinct_,
      N_ORDER_BY, order_items_, N_SEPARATOR_PARAM_EXPR, separator_param_expr_, K_(udf_meta), N_LINEAR_INTER_EXPR,
      linear_inter_expr_);
O
oceanbase-admin 已提交
3031

G
gm 已提交
3032
private:
O
oceanbase-admin 已提交
3033 3034
  DISALLOW_COPY_AND_ASSIGN(ObAggFunRawExpr);
  // real_param_exprs_.count() == 0 means '*'
X
xy0 已提交
3035 3036 3037 3038
  common::ObSEArray<ObRawExpr *, 1, common::ModulePageAllocator, true> real_param_exprs_;
  ObRawExpr *push_down_sum_expr_;
  ObRawExpr *push_down_count_expr_;
  ObRawExpr *push_down_synopsis_expr_;
O
oceanbase-admin 已提交
3039 3040 3041
  bool distinct_;
  // used for group_concat/rank/percent rank/dense rank/cume dist
  common::ObArray<OrderItem, common::ModulePageAllocator, true> order_items_;
X
xy0 已提交
3042
  ObRawExpr *separator_param_expr_;
O
oceanbase-admin 已提交
3043 3044 3045 3046
  // use for udf function info
  share::schema::ObUDFMeta udf_meta_;
  // a pre allocated expr used to compute linear interpolation
  // not contain any sharable expr, no need to deep copy
X
xy0 已提交
3047
  ObRawExpr *linear_inter_expr_;
O
oceanbase-admin 已提交
3048 3049 3050
  bool is_nested_aggr_;
};

X
xy0 已提交
3051
inline int ObAggFunRawExpr::add_real_param_expr(ObRawExpr *expr)
O
oceanbase-admin 已提交
3052 3053 3054 3055 3056 3057 3058 3059 3060 3061
{
  int ret = common::OB_SUCCESS;
  if (OB_UNLIKELY(NULL == expr)) {
    ret = common::OB_INVALID_ARGUMENT;
  } else {
    ret = real_param_exprs_.push_back(expr);
  }
  return ret;
}

X
xy0 已提交
3062
inline int ObAggFunRawExpr::replace_real_param_expr(int64_t index, ObRawExpr *expr)
O
oceanbase-admin 已提交
3063 3064 3065 3066 3067 3068 3069
{
  int ret = common::OB_SUCCESS;
  if (OB_UNLIKELY(index < 0 || index >= real_param_exprs_.count())) {
    ret = common::OB_INVALID_ARGUMENT;
  } else if (OB_UNLIKELY(NULL == expr)) {
    ret = common::OB_INVALID_ARGUMENT;
  } else {
X
xy0 已提交
3070
    ObRawExpr *&target_expr = real_param_exprs_.at(index);
O
oceanbase-admin 已提交
3071 3072 3073 3074 3075
    target_expr = expr;
  }
  return ret;
}

X
xy0 已提交
3076
inline int ObAggFunRawExpr::replace_param_expr(int64_t index, ObRawExpr *expr)
O
oceanbase-admin 已提交
3077 3078 3079 3080 3081 3082 3083
{
  int ret = common::OB_SUCCESS;
  if (OB_UNLIKELY(index < 0 || index >= get_param_count())) {
    ret = common::OB_INVALID_ARGUMENT;
  } else if (OB_UNLIKELY(NULL == expr)) {
    ret = common::OB_INVALID_ARGUMENT;
  } else if (index >= real_param_exprs_.count()) {
X
xy0 已提交
3084
    ObRawExpr *&target_expr = order_items_.at(index - real_param_exprs_.count()).expr_;
O
oceanbase-admin 已提交
3085 3086
    target_expr = expr;
  } else {
X
xy0 已提交
3087
    ObRawExpr *&target_expr = real_param_exprs_.at(index);
O
oceanbase-admin 已提交
3088 3089 3090 3091 3092
    target_expr = expr;
  }
  return ret;
}

X
xy0 已提交
3093
inline void ObAggFunRawExpr::set_push_down_sum_expr(ObRawExpr *expr)
O
oceanbase-admin 已提交
3094 3095 3096
{
  push_down_sum_expr_ = expr;
}
X
xy0 已提交
3097
inline void ObAggFunRawExpr::set_push_down_count_expr(ObRawExpr *expr)
O
oceanbase-admin 已提交
3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127
{
  push_down_count_expr_ = expr;
}
inline bool ObAggFunRawExpr::is_param_distinct() const
{
  return distinct_;
}
inline bool ObAggFunRawExpr::is_nested_aggr() const
{
  return is_nested_aggr_;
}
inline bool ObAggFunRawExpr::contain_nested_aggr() const
{
  bool ret = false;
  for (int64 i = 0; !ret && i < get_param_count(); i++) {
    if (get_param_expr(i)->has_flag(CNT_AGG)) {
      ret = true;
    } else { /*do nothing.*/
    }
  }
  return ret;
}
inline void ObAggFunRawExpr::set_param_distinct(bool is_distinct)
{
  distinct_ = is_distinct;
}
inline void ObAggFunRawExpr::set_in_nested_aggr(bool is_nested)
{
  is_nested_aggr_ = is_nested;
}
X
xy0 已提交
3128
inline void ObAggFunRawExpr::set_separator_param_expr(ObRawExpr *separator_param_expr)
O
oceanbase-admin 已提交
3129 3130 3131
{
  separator_param_expr_ = separator_param_expr;
}
X
xy0 已提交
3132
inline void ObAggFunRawExpr::set_linear_inter_expr(ObRawExpr *linear_inter_expr)
O
oceanbase-admin 已提交
3133 3134 3135
{
  linear_inter_expr_ = linear_inter_expr;
}
X
xy0 已提交
3136
inline int ObAggFunRawExpr::add_order_item(const OrderItem &order_item)
O
oceanbase-admin 已提交
3137 3138 3139 3140 3141 3142 3143 3144
{
  return order_items_.push_back(order_item);
}

////////////////////////////////////////////////////////////////
// for normal system function, func_name_ is used to distinguish them.
// for special system function, ObRawExpr::type_ can be reset. Such function may not need name
class ObSysFunRawExpr : public ObOpRawExpr {
G
gm 已提交
3145
public:
X
xy0 已提交
3146
  ObSysFunRawExpr(common::ObIAllocator &alloc) : ObOpRawExpr(alloc), func_name_(), operator_id_(common::OB_INVALID_ID)
O
oceanbase-admin 已提交
3147 3148 3149 3150 3151 3152 3153 3154 3155
  {
    set_expr_class(ObExpr::EXPR_SYS_FUNC);
  }
  ObSysFunRawExpr() : ObOpRawExpr(), func_name_(), operator_id_(common::OB_INVALID_ID)
  {
    set_expr_class(ObExpr::EXPR_SYS_FUNC);
  }
  virtual ~ObSysFunRawExpr()
  {}
X
xy0 已提交
3156 3157
  virtual int assign(const ObSysFunRawExpr &other);
  virtual int deep_copy(ObRawExprFactory &expr_factory, const ObSysFunRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
3158
      bool use_new_allocator = false);
X
xy0 已提交
3159 3160
  void set_func_name(const common::ObString &name);
  const common::ObString &get_func_name() const;
L
LINxiansheng 已提交
3161
  virtual void clear_child() override;
O
oceanbase-admin 已提交
3162
  int check_param_num();
X
xj0 已提交
3163 3164
  int check_param_num(int param_count);
  virtual ObExprOperator* get_op() override;
L
LINxiansheng 已提交
3165
  virtual void reset() override;
X
xy0 已提交
3166
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
3167

X
xy0 已提交
3168
  virtual int do_visit(ObRawExprVisitor &visitor) override;
O
oceanbase-admin 已提交
3169

L
LINxiansheng 已提交
3170
  virtual uint64_t hash_internal(uint64_t seed) const override
O
oceanbase-admin 已提交
3171 3172 3173 3174 3175
  {
    uint64_t hash_ret = common::do_hash(func_name_, seed);
    return hash_ret;
  }

X
xy0 已提交
3176 3177 3178
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
  int get_cast_type_name(char *buf, int64_t buf_len, int64_t &pos) const;
  int get_column_conv_name(char *buf, int64_t buf_len, int64_t &pos, ExplainType type) const;
O
oceanbase-admin 已提交
3179 3180 3181 3182 3183 3184 3185 3186 3187
  void set_op_id(int64_t operator_id)
  {
    operator_id_ = operator_id;
  }
  int64_t get_op_id() const
  {
    return operator_id_;
  }

3188 3189
  VIRTUAL_TO_STRING_KV_CHECK_STACK_OVERFLOW(N_ITEM_TYPE, type_, N_RESULT_TYPE, result_type_, N_EXPR_INFO, info_,
      N_REL_ID, rel_ids_, K_(expr_levels), N_FUNC, func_name_, N_CHILDREN, exprs_, K_(enum_set_values));
O
oceanbase-admin 已提交
3190

G
gm 已提交
3191
private:
X
xj0 已提交
3192
  int check_param_num_internal(int32_t param_num, int32_t param_count, ObExprOperatorType type);
O
oceanbase-admin 已提交
3193 3194 3195 3196 3197
  DISALLOW_COPY_AND_ASSIGN(ObSysFunRawExpr);
  common::ObString func_name_;
  uint64_t operator_id_;
};

X
xy0 已提交
3198
inline void ObSysFunRawExpr::set_func_name(const common::ObString &name)
O
oceanbase-admin 已提交
3199 3200 3201
{
  func_name_ = name;
}
X
xy0 已提交
3202
inline const common::ObString &ObSysFunRawExpr::get_func_name() const
O
oceanbase-admin 已提交
3203 3204 3205 3206 3207
{
  return func_name_;
}

class ObSequenceRawExpr : public ObSysFunRawExpr {
G
gm 已提交
3208
public:
X
xy0 已提交
3209
  ObSequenceRawExpr(common::ObIAllocator &alloc) : ObSysFunRawExpr(alloc), name_(), action_(), sequence_id_(0)
O
oceanbase-admin 已提交
3210 3211 3212 3213
  {}
  ObSequenceRawExpr() : ObSysFunRawExpr(), name_(), action_(), sequence_id_(0)
  {}
  virtual ~ObSequenceRawExpr() = default;
X
xy0 已提交
3214 3215
  virtual int assign(const ObSequenceRawExpr &other);
  virtual int deep_copy(ObRawExprFactory &expr_factory, const ObSequenceRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
3216
      bool use_new_allocator = false);
X
xy0 已提交
3217 3218
  int set_sequence_meta(const common::ObString &name, const common::ObString &action, uint64_t sequence_id);
  const common::ObString &get_name()
O
oceanbase-admin 已提交
3219 3220 3221
  {
    return name_;
  }
X
xy0 已提交
3222
  const common::ObString &get_action()
O
oceanbase-admin 已提交
3223 3224 3225 3226 3227 3228 3229
  {
    return action_;
  }
  uint64_t get_sequence_id() const
  {
    return sequence_id_;
  }
X
xy0 已提交
3230 3231
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
  virtual int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
O
oceanbase-admin 已提交
3232

G
gm 已提交
3233
private:
O
oceanbase-admin 已提交
3234 3235 3236 3237 3238 3239
  common::ObString name_;    // sequence object name
  common::ObString action_;  // NEXTVAL or CURRVAL
  uint64_t sequence_id_;
};

class ObNormalDllUdfRawExpr : public ObSysFunRawExpr {
G
gm 已提交
3240
public:
X
xy0 已提交
3241
  ObNormalDllUdfRawExpr(common::ObIAllocator &alloc) : ObSysFunRawExpr(alloc), udf_meta_(), udf_attributes_()
O
oceanbase-admin 已提交
3242 3243 3244 3245 3246
  {}
  ObNormalDllUdfRawExpr() : ObSysFunRawExpr(), udf_meta_(), udf_attributes_()
  {}
  virtual ~ObNormalDllUdfRawExpr()
  {}
X
xy0 已提交
3247 3248
  virtual int assign(const ObNormalDllUdfRawExpr &other);
  virtual int deep_copy(ObRawExprFactory &expr_factory, const ObNormalDllUdfRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
3249
      bool use_new_allocator = false);
X
xy0 已提交
3250 3251 3252 3253
  int set_udf_meta(const share::schema::ObUDF &udf);
  int add_udf_attribute_name(const common::ObString &name);
  int add_udf_attribute(const ObRawExpr *expr, const ParseNode *node);
  const share::schema::ObUDFMeta &get_udf_meta() const
O
oceanbase-admin 已提交
3254 3255 3256
  {
    return udf_meta_;
  }
X
xy0 已提交
3257
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
3258

G
gm 已提交
3259
private:
O
oceanbase-admin 已提交
3260 3261 3262 3263 3264 3265
  // for udf function info
  share::schema::ObUDFMeta udf_meta_;
  common::ObSEArray<common::ObString, 16> udf_attributes_;  // name of input expr
};

class ObPLSQLCodeSQLErrmRawExpr : public ObSysFunRawExpr {
G
gm 已提交
3266
public:
X
xy0 已提交
3267
  ObPLSQLCodeSQLErrmRawExpr(common::ObIAllocator &alloc) : ObSysFunRawExpr(alloc), is_sqlcode_(true)
O
oceanbase-admin 已提交
3268 3269 3270 3271 3272
  {}
  ObPLSQLCodeSQLErrmRawExpr() : ObSysFunRawExpr(), is_sqlcode_(true)
  {}
  virtual ~ObPLSQLCodeSQLErrmRawExpr()
  {}
X
xy0 已提交
3273 3274
  virtual int assign(const ObPLSQLCodeSQLErrmRawExpr &other);
  virtual int deep_copy(ObRawExprFactory &expr_factory, const ObPLSQLCodeSQLErrmRawExpr &other,
O
oceanbase-admin 已提交
3275
      const uint64_t copy_types, bool use_new_allocator = false);
X
xy0 已提交
3276
  virtual ObExprOperator *get_op() override;
O
oceanbase-admin 已提交
3277 3278 3279 3280
  void set_is_sqlcode(bool is_sqlcode)
  {
    is_sqlcode_ = is_sqlcode;
  }
3281
  bool get_is_sqlcode() const
O
oceanbase-admin 已提交
3282 3283 3284
  {
    return is_sqlcode_;
  }
3285 3286
  VIRTUAL_TO_STRING_KV_CHECK_STACK_OVERFLOW(N_ITEM_TYPE, type_, N_RESULT_TYPE, result_type_, N_EXPR_INFO, info_,
      N_REL_ID, rel_ids_, K_(expr_level), K_(expr_levels), K_(is_sqlcode), N_CHILDREN, exprs_);
O
oceanbase-admin 已提交
3287

G
gm 已提交
3288
private:
O
oceanbase-admin 已提交
3289 3290
  DISALLOW_COPY_AND_ASSIGN(ObPLSQLCodeSQLErrmRawExpr);

G
gm 已提交
3291
private:
O
oceanbase-admin 已提交
3292 3293 3294 3295
  bool is_sqlcode_;
};

class ObObjAccessRawExpr : public ObOpRawExpr {
G
gm 已提交
3296
public:
X
xy0 已提交
3297
  ObObjAccessRawExpr(common::ObIAllocator &alloc)
O
oceanbase-admin 已提交
3298 3299 3300 3301 3302 3303
      : ObOpRawExpr(alloc), get_attr_func_(0), func_name_(), var_indexs_(), for_write_(false)
  {}
  ObObjAccessRawExpr() : ObOpRawExpr(), get_attr_func_(0), func_name_(), var_indexs_(), for_write_(false)
  {}
  virtual ~ObObjAccessRawExpr()
  {}
X
xy0 已提交
3304 3305
  int assign(const ObObjAccessRawExpr &other);
  int deep_copy(ObRawExprFactory &expr_factory, const ObObjAccessRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
3306
      bool use_new_allocator = false);
X
xy0 已提交
3307
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
3308

X
xy0 已提交
3309
  const common::ObIArray<int64_t> &get_var_indexs() const
O
oceanbase-admin 已提交
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320
  {
    return var_indexs_;
  }
  void set_get_attr_func_addr(uint64_t get_attr_func)
  {
    get_attr_func_ = get_attr_func;
  }
  uint64_t get_get_attr_func_addr() const
  {
    return get_attr_func_;
  }
X
xy0 已提交
3321
  void set_func_name(const common::ObString &func_name)
O
oceanbase-admin 已提交
3322 3323 3324
  {
    func_name_ = func_name;
  }
X
xy0 已提交
3325
  const common::ObString &get_func_name() const
O
oceanbase-admin 已提交
3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337
  {
    return func_name_;
  }
  bool for_write() const
  {
    return for_write_;
  }
  void set_write(bool for_write)
  {
    for_write_ = for_write;
  }

G
gm 已提交
3338
private:
O
oceanbase-admin 已提交
3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
  DISALLOW_COPY_AND_ASSIGN(ObObjAccessRawExpr);
  uint64_t get_attr_func_;
  common::ObString func_name_;
  common::ObSEArray<int64_t, 4, common::ModulePageAllocator, true> var_indexs_;
  bool for_write_;
};

enum ObMultiSetType {
  MULTISET_TYPE_INVALID = -1,
  MULTISET_TYPE_UNION,
  MULTISET_TYPE_INTERSECT,
  MULTISET_TYPE_EXCEPT,
  MULTISET_TYPE_SUBMULTISET,
  MULTISET_TYPE_MEMBER_OF,
  MULTISET_TYPE_IS_SET,
  MULTISET_TYPE_EMPTY,
};

enum ObMultiSetModifier {
  MULTISET_MODIFIER_INVALID = -1,
  MULTISET_MODIFIER_ALL,
  MULTISET_MODIFIER_DISTINCT,
  MULTISET_MODIFIER_NOT,
};

class ObMultiSetRawExpr : public ObOpRawExpr {
G
gm 已提交
3365
public:
X
xy0 已提交
3366
  ObMultiSetRawExpr(common::ObIAllocator &alloc)
O
oceanbase-admin 已提交
3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379
      : ObOpRawExpr(alloc),
        ms_modifier_(ObMultiSetModifier::MULTISET_MODIFIER_INVALID),
        ms_type_(ObMultiSetType::MULTISET_TYPE_INVALID)
  {}
  ObMultiSetRawExpr()
      : ObOpRawExpr(),
        ms_modifier_(ObMultiSetModifier::MULTISET_MODIFIER_INVALID),
        ms_type_(ObMultiSetType::MULTISET_TYPE_INVALID)
  {}

  virtual ~ObMultiSetRawExpr()
  {}

X
xy0 已提交
3380 3381
  int assign(const ObMultiSetRawExpr &other);
  int deep_copy(ObRawExprFactory &expr_factory, const ObMultiSetRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
3382
      bool use_new_allocator = false);
X
xy0 已提交
3383
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402

  inline ObMultiSetModifier get_multiset_modifier() const
  {
    return ms_modifier_;
  }
  inline ObMultiSetType get_multiset_type() const
  {
    return ms_type_;
  }

  void set_multiset_modifier(ObMultiSetModifier modifier)
  {
    ms_modifier_ = modifier;
  }
  void set_multiset_type(ObMultiSetType type)
  {
    ms_type_ = type;
  }

G
gm 已提交
3403
private:
O
oceanbase-admin 已提交
3404 3405 3406 3407 3408 3409
  DISALLOW_COPY_AND_ASSIGN(ObMultiSetRawExpr);
  ObMultiSetModifier ms_modifier_;
  ObMultiSetType ms_type_;
};

class ObCollPredRawExpr : public ObMultiSetRawExpr {
G
gm 已提交
3410
public:
X
xy0 已提交
3411
  ObCollPredRawExpr(common::ObIAllocator &alloc) : ObMultiSetRawExpr(alloc)
O
oceanbase-admin 已提交
3412 3413 3414 3415
  {}
  virtual ~ObCollPredRawExpr()
  {}

X
xy0 已提交
3416 3417
  int assign(const ObCollPredRawExpr &other);
  int deep_copy(ObRawExprFactory &expr_factory, const ObCollPredRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
3418
      bool use_new_allocator = false);
X
xy0 已提交
3419
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
3420

G
gm 已提交
3421
private:
O
oceanbase-admin 已提交
3422 3423 3424 3425
  DISALLOW_COPY_AND_ASSIGN(ObCollPredRawExpr);
};

class ObFunMatchAgainst : public ObNonTerminalRawExpr {
G
gm 已提交
3426
public:
O
oceanbase-admin 已提交
3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437
  ObFunMatchAgainst()
      : ObNonTerminalRawExpr(),
        mode_flag_(NATURAL_LANGUAGE_MODE),
        match_columns_(NULL),
        real_column_(NULL),
        search_key_(NULL),
        /*search_tree_(NULL)*/
        fulltext_filter_(NULL)
  {
    set_expr_class(ObExpr::EXPR_DOMAIN_INDEX);
  }
X
xy0 已提交
3438
  ObFunMatchAgainst(common::ObIAllocator &alloc)
O
oceanbase-admin 已提交
3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451
      : ObNonTerminalRawExpr(alloc),
        mode_flag_(NATURAL_LANGUAGE_MODE),
        match_columns_(NULL),
        real_column_(NULL),
        search_key_(NULL),
        /*search_tree_(NULL)*/
        fulltext_filter_(NULL)
  {
    set_expr_class(ObExpr::EXPR_DOMAIN_INDEX);
  }

  virtual ~ObFunMatchAgainst()
  {}
X
xy0 已提交
3452
  virtual int do_visit(ObRawExprVisitor &visitor) override;
L
LINxiansheng 已提交
3453
  virtual uint64_t hash_internal(uint64_t seed) const override;
X
xy0 已提交
3454 3455
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
3456 3457 3458 3459 3460 3461 3462 3463
  inline void set_mode_flag(ObMatchAgainstMode mode_flag)
  {
    mode_flag_ = mode_flag;
  }
  inline ObMatchAgainstMode get_mode_flag() const
  {
    return mode_flag_;
  }
X
xy0 已提交
3464
  inline void set_match_columns(ObRawExpr *match_columns)
O
oceanbase-admin 已提交
3465 3466 3467
  {
    match_columns_ = match_columns;
  }
X
xy0 已提交
3468
  inline const ObRawExpr *get_match_columns() const
O
oceanbase-admin 已提交
3469 3470 3471
  {
    return match_columns_;
  }
X
xy0 已提交
3472
  inline ObRawExpr *get_match_columns()
O
oceanbase-admin 已提交
3473 3474 3475
  {
    return match_columns_;
  }
X
xy0 已提交
3476
  inline void set_search_key(ObRawExpr *search_key)
O
oceanbase-admin 已提交
3477 3478 3479
  {
    search_key_ = search_key;
  }
X
xy0 已提交
3480
  inline const ObRawExpr *get_search_key() const
O
oceanbase-admin 已提交
3481 3482 3483
  {
    return search_key_;
  }
X
xy0 已提交
3484
  inline ObRawExpr *get_search_key()
O
oceanbase-admin 已提交
3485 3486 3487
  {
    return search_key_;
  }
X
xy0 已提交
3488
  inline void set_real_column(ObColumnRefRawExpr *real_column)
O
oceanbase-admin 已提交
3489 3490 3491
  {
    real_column_ = real_column;
  }
X
xy0 已提交
3492
  inline const ObColumnRefRawExpr *get_real_column() const
O
oceanbase-admin 已提交
3493 3494 3495 3496 3497
  {
    return real_column_;
  }
  //  inline void set_search_tree(ObRawExpr *search_tree) { search_tree_ = search_tree; }
  //  inline const ObRawExpr *get_search_tree() const { return search_tree_; }
X
xy0 已提交
3498
  inline void set_fulltext_filter(ObRawExpr *fulltext_key)
O
oceanbase-admin 已提交
3499 3500 3501
  {
    fulltext_filter_ = fulltext_key;
  }
X
xy0 已提交
3502
  inline const ObRawExpr *get_fulltext_filter() const
O
oceanbase-admin 已提交
3503 3504 3505
  {
    return fulltext_filter_;
  }
X
xy0 已提交
3506
  inline ObRawExpr *get_fulltext_filter()
O
oceanbase-admin 已提交
3507 3508 3509
  {
    return fulltext_filter_;
  }
L
LINxiansheng 已提交
3510
  int64_t get_param_count() const override
O
oceanbase-admin 已提交
3511 3512 3513
  {
    return 2;
  }
X
xy0 已提交
3514 3515
  const ObRawExpr *get_param_expr(int64_t index) const override;
  ObRawExpr *&get_param_expr(int64_t index) override;
L
LINxiansheng 已提交
3516
  void clear_child() override;
O
oceanbase-admin 已提交
3517
  virtual int replace_expr(
X
xy0 已提交
3518
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
O
oceanbase-admin 已提交
3519 3520 3521
  VIRTUAL_TO_STRING_KV(
      N_ITEM_TYPE, type_, N_RESULT_TYPE, result_type_, N_EXPR_INFO, info_, N_REL_ID, rel_ids_, K_(mode_flag));

G
gm 已提交
3522
private:
O
oceanbase-admin 已提交
3523 3524
  DISALLOW_COPY_AND_ASSIGN(ObFunMatchAgainst);
  ObMatchAgainstMode mode_flag_;
X
xy0 已提交
3525 3526 3527 3528
  ObRawExpr *match_columns_;
  ObColumnRefRawExpr *real_column_;
  ObRawExpr *search_key_;
  ObRawExpr *fulltext_filter_;
O
oceanbase-admin 已提交
3529 3530 3531
};

class ObSetIterRawExpr : public ObNonTerminalRawExpr {
G
gm 已提交
3532
public:
O
oceanbase-admin 已提交
3533 3534 3535 3536
  ObSetIterRawExpr() : ObNonTerminalRawExpr(), left_iter_(NULL), right_iter_(NULL)
  {
    set_expr_class(ObExpr::EXPR_DOMAIN_INDEX);
  }
X
xy0 已提交
3537
  ObSetIterRawExpr(common::ObIAllocator &alloc) : ObNonTerminalRawExpr(alloc), left_iter_(NULL), right_iter_(NULL)
O
oceanbase-admin 已提交
3538 3539 3540 3541 3542 3543
  {
    set_expr_class(ObExpr::EXPR_DOMAIN_INDEX);
  }

  virtual ~ObSetIterRawExpr()
  {}
X
xy0 已提交
3544 3545
  virtual int do_visit(ObRawExprVisitor &visitor) override;
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
L
LINxiansheng 已提交
3546
  int64_t get_param_count() const override
O
oceanbase-admin 已提交
3547 3548 3549
  {
    return 2;
  }
X
xy0 已提交
3550 3551 3552
  const ObRawExpr *get_param_expr(int64_t index) const override;
  ObRawExpr *&get_param_expr(int64_t index) override;
  void set_left_expr(ObRawExpr *left_iter)
O
oceanbase-admin 已提交
3553 3554 3555
  {
    left_iter_ = left_iter;
  }
X
xy0 已提交
3556
  void set_right_expr(ObRawExpr *right_iter)
O
oceanbase-admin 已提交
3557 3558 3559
  {
    right_iter_ = right_iter;
  }
X
xy0 已提交
3560
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override
O
oceanbase-admin 已提交
3561 3562 3563 3564 3565
  {
    UNUSED(expr);
    UNUSED(check_context);
    return false;
  }
L
LINxiansheng 已提交
3566
  inline void clear_child() override
O
oceanbase-admin 已提交
3567 3568 3569 3570 3571 3572
  {
    left_iter_ = NULL;
    right_iter_ = NULL;
  }
  VIRTUAL_TO_STRING_KV(N_ITEM_TYPE, type_, N_EXPR_INFO, info_, N_REL_ID, rel_ids_);

G
gm 已提交
3573
private:
O
oceanbase-admin 已提交
3574
  DISALLOW_COPY_AND_ASSIGN(ObSetIterRawExpr);
X
xy0 已提交
3575 3576
  ObRawExpr *left_iter_;
  ObRawExpr *right_iter_;
O
oceanbase-admin 已提交
3577 3578 3579
};

class ObRowIterRawExpr : public ObTerminalRawExpr {
G
gm 已提交
3580
public:
O
oceanbase-admin 已提交
3581 3582 3583 3584
  ObRowIterRawExpr() : ObTerminalRawExpr(), iter_idx_(common::OB_INVALID_INDEX)
  {
    set_expr_class(ObExpr::EXPR_DOMAIN_INDEX);
  }
X
xy0 已提交
3585
  ObRowIterRawExpr(common::ObIAllocator &alloc) : ObTerminalRawExpr(alloc), iter_idx_(common::OB_INVALID_INDEX)
O
oceanbase-admin 已提交
3586 3587 3588 3589 3590 3591
  {
    set_expr_class(ObExpr::EXPR_DOMAIN_INDEX);
  }

  virtual ~ObRowIterRawExpr()
  {}
X
xy0 已提交
3592 3593
  virtual int do_visit(ObRawExprVisitor &visitor) override;
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override
O
oceanbase-admin 已提交
3594 3595 3596 3597 3598
  {
    UNUSED(expr);
    UNUSED(check_context);
    return false;
  }
X
xy0 已提交
3599
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
O
oceanbase-admin 已提交
3600 3601 3602 3603 3604 3605 3606 3607 3608 3609
  inline void set_iter_idx(int64_t iter_idx)
  {
    iter_idx_ = iter_idx;
  }
  inline int64_t get_iter_idx() const
  {
    return iter_idx_;
  }
  VIRTUAL_TO_STRING_KV(N_ITEM_TYPE, type_, N_EXPR_INFO, info_, N_REL_ID, rel_ids_, K_(iter_idx));

G
gm 已提交
3610
private:
O
oceanbase-admin 已提交
3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635
  DISALLOW_COPY_AND_ASSIGN(ObRowIterRawExpr);
  int64_t iter_idx_;
};

////////////////////////////////////////////////////////////////
// eg :
//  partition by class order by score rows between 5 preceding and 5 following : WINDOW_ROWS
//  partition by class order by score range between 5 preceding and 5 following : WINDOW_RANGE
// new
enum WindowType {
  WINDOW_ROWS,
  WINDOW_RANGE,
  WINDOW_MAX,
};
enum BoundType {
  BOUND_UNBOUNDED,
  BOUND_CURRENT_ROW,
  BOUND_INTERVAL,
};

enum BoundExprIdx { BOUND_EXPR_ADD = 0, BOUND_EXPR_SUB, BOUND_EXPR_MAX };

struct Bound {
  OB_UNIS_VERSION_V(1);

G
gm 已提交
3636
public:
O
oceanbase-admin 已提交
3637 3638 3639 3640 3641 3642 3643
  Bound()
      : type_(BOUND_UNBOUNDED),
        is_preceding_(false),
        is_nmb_literal_(false),
        interval_expr_(NULL),
        date_unit_expr_(NULL)
  {
X
xy0 已提交
3644
    MEMSET(exprs_, 0, sizeof(ObRawExpr *) * BOUND_EXPR_MAX);
O
oceanbase-admin 已提交
3645 3646 3647
  }

  int deep_copy(
X
xy0 已提交
3648
      ObRawExprFactory &expr_factory, const Bound &other, const uint64_t copy_types, bool use_new_allocator = false);
O
oceanbase-admin 已提交
3649
  virtual int replace_expr(
X
xy0 已提交
3650
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs);
O
oceanbase-admin 已提交
3651

O
obdev 已提交
3652 3653
  bool same_as(const Bound &other, ObExprEqualCheckContext *check_context) const;

O
oceanbase-admin 已提交
3654 3655 3656
  BoundType type_;
  bool is_preceding_;
  bool is_nmb_literal_;
X
xy0 已提交
3657 3658
  ObRawExpr *interval_expr_;
  ObRawExpr *date_unit_expr_;
O
oceanbase-admin 已提交
3659 3660
  ;

X
xy0 已提交
3661
  ObRawExpr *exprs_[BOUND_EXPR_MAX];
O
oceanbase-admin 已提交
3662 3663 3664 3665 3666
  TO_STRING_KV(K_(type), K_(is_preceding), K_(is_nmb_literal), KP_(interval_expr), K_(date_unit_expr));
};

////////////////////////////////////////////////////////////////
struct ObFrame {
G
gm 已提交
3667
public:
O
oceanbase-admin 已提交
3668 3669 3670 3671 3672 3673 3674 3675 3676 3677
  ObFrame() : win_type_(WINDOW_MAX), is_between_(false)
  {}
  inline void set_window_type(WindowType win_type)
  {
    win_type_ = win_type;
  }
  inline void set_is_between(bool is_between)
  {
    is_between_ = is_between;
  }
X
xy0 已提交
3678
  inline void set_upper(const Bound &upper)
O
oceanbase-admin 已提交
3679 3680 3681
  {
    upper_ = upper;
  }
X
xy0 已提交
3682
  inline void set_lower(const Bound &lower)
O
oceanbase-admin 已提交
3683 3684 3685
  {
    lower_ = lower;
  }
O
obdev 已提交
3686
  inline WindowType get_window_type() const
O
oceanbase-admin 已提交
3687 3688 3689
  {
    return win_type_;
  }
O
obdev 已提交
3690
  inline bool is_between() const
O
oceanbase-admin 已提交
3691 3692 3693
  {
    return is_between_;
  }
X
xy0 已提交
3694
  inline Bound &get_upper()
O
oceanbase-admin 已提交
3695 3696 3697
  {
    return upper_;
  }
X
xy0 已提交
3698
  inline Bound &get_lower()
O
oceanbase-admin 已提交
3699 3700 3701 3702
  {
    return lower_;
  }

X
xy0 已提交
3703
  int assign(const ObFrame &other);
O
oceanbase-admin 已提交
3704 3705 3706 3707 3708 3709 3710 3711

  WindowType win_type_;
  bool is_between_;
  Bound upper_;
  Bound lower_;
};

struct ObWindow : public ObFrame {
G
gm 已提交
3712
public:
O
oceanbase-admin 已提交
3713 3714 3715 3716 3717
  ObWindow() : has_frame_orig_(false)
  {
    partition_exprs_.set_label(common::ObModIds::OB_SQL_WINDOW_FUNC);
    order_items_.set_label(common::ObModIds::OB_SQL_WINDOW_FUNC);
  }
X
xy0 已提交
3718
  inline int set_partition_exprs(const common::ObIArray<ObRawExpr *> &exprs)
O
oceanbase-admin 已提交
3719 3720 3721
  {
    return partition_exprs_.assign(exprs);
  }
X
xy0 已提交
3722
  inline int set_order_items(const common::ObIArray<OrderItem> &items)
O
oceanbase-admin 已提交
3723 3724 3725
  {
    return order_items_.assign(items);
  }
X
xy0 已提交
3726
  inline void set_win_name(common::ObString &win_name)
O
oceanbase-admin 已提交
3727 3728 3729 3730 3731 3732 3733
  {
    win_name_ = win_name;
  }
  inline void set_has_frame_orig(int64_t has_frame_orig)
  {
    has_frame_orig_ = has_frame_orig;
  }
X
xy0 已提交
3734
  inline common::ObString &get_win_name()
O
oceanbase-admin 已提交
3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745
  {
    return win_name_;
  }
  inline bool has_frame_orig()
  {
    return has_frame_orig_;
  }
  inline bool has_order_items()
  {
    return order_items_.count() > 0;
  }
X
xy0 已提交
3746
  inline const common::ObIArray<ObRawExpr *> &get_partition_exprs() const
O
oceanbase-admin 已提交
3747 3748 3749
  {
    return partition_exprs_;
  }
X
xy0 已提交
3750
  inline common::ObIArray<ObRawExpr *> &get_partition_exprs()
O
oceanbase-admin 已提交
3751 3752 3753
  {
    return partition_exprs_;
  }
X
xy0 已提交
3754
  inline const common::ObIArray<OrderItem> &get_order_items() const
O
oceanbase-admin 已提交
3755 3756 3757
  {
    return order_items_;
  }
X
xy0 已提交
3758
  inline common::ObIArray<OrderItem> &get_order_items()
O
oceanbase-admin 已提交
3759 3760 3761
  {
    return order_items_;
  }
X
xy0 已提交
3762
  int assign(const ObWindow &other);
3763
  int remove_const_params();
O
oceanbase-admin 已提交
3764

X
xy0 已提交
3765
  common::ObArray<ObRawExpr *, common::ModulePageAllocator, true> partition_exprs_;
O
oceanbase-admin 已提交
3766 3767 3768 3769 3770 3771 3772
  common::ObArray<OrderItem, common::ModulePageAllocator, true> order_items_;
  // used in resolver
  common::ObString win_name_;
  bool has_frame_orig_;
};

class ObWinFunRawExpr : public ObRawExpr, public ObWindow {
G
gm 已提交
3773
public:
O
oceanbase-admin 已提交
3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784
  ObWinFunRawExpr()
      : ObRawExpr(),
        ObWindow(),
        func_type_(T_MAX),
        is_distinct_(false),
        is_ignore_null_(false),
        is_from_first_(false),
        agg_expr_(NULL)
  {
    set_expr_class(ObExpr::EXPR_WINDOW);
  }
X
xy0 已提交
3785
  ObWinFunRawExpr(common::ObIAllocator &alloc)
O
oceanbase-admin 已提交
3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798
      : ObRawExpr(alloc),
        ObWindow(),
        func_type_(T_MAX),
        is_distinct_(false),
        is_ignore_null_(false),
        is_from_first_(false),
        agg_expr_(NULL)
  {
    set_expr_class(ObExpr::EXPR_WINDOW);
  }
  virtual ~ObWinFunRawExpr()
  {}

X
xy0 已提交
3799 3800
  int assign(const ObWinFunRawExpr &other);
  int deep_copy(ObRawExprFactory &expr_factory, const ObWinFunRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
3801
      bool use_new_allocator = false);
X
xy0 已提交
3802
  int replace_param_expr(int64_t partition_expr_index, ObRawExpr *expr);
O
oceanbase-admin 已提交
3803
  virtual int replace_expr(
X
xy0 已提交
3804
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
O
oceanbase-admin 已提交
3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820
  inline void set_func_type(ObItemType func_type)
  {
    func_type_ = func_type;
  }
  inline void set_is_distinct(bool is_distinct)
  {
    is_distinct_ = is_distinct;
  }
  inline void set_is_ignore_null(bool is_ignore_null)
  {
    is_ignore_null_ = is_ignore_null;
  }
  inline void set_is_from_first(bool is_from_first)
  {
    is_from_first_ = is_from_first;
  }
X
xy0 已提交
3821
  inline int set_func_params(const common::ObIArray<ObRawExpr *> &params)
O
oceanbase-admin 已提交
3822 3823 3824
  {
    return func_params_.assign(params);
  }
X
xy0 已提交
3825
  inline void set_agg_expr(ObAggFunRawExpr *agg_expr)
O
oceanbase-admin 已提交
3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844
  {
    agg_expr_ = agg_expr;
  }
  inline ObItemType get_func_type() const
  {
    return func_type_;
  }
  inline bool is_distinct()
  {
    return is_distinct_;
  }
  inline bool is_ignore_null()
  {
    return is_ignore_null_;
  }
  inline bool is_from_first()
  {
    return is_from_first_;
  }
X
xy0 已提交
3845
  inline const common::ObIArray<ObRawExpr *> &get_func_params() const
O
oceanbase-admin 已提交
3846 3847 3848
  {
    return func_params_;
  }
X
xy0 已提交
3849
  inline common::ObIArray<ObRawExpr *> &get_func_params()
O
oceanbase-admin 已提交
3850 3851 3852
  {
    return func_params_;
  }
X
xy0 已提交
3853
  inline ObAggFunRawExpr *get_agg_expr()
O
oceanbase-admin 已提交
3854 3855 3856
  {
    return agg_expr_;
  }
X
xy0 已提交
3857
  inline ObAggFunRawExpr *get_agg_expr() const
O
oceanbase-admin 已提交
3858 3859 3860 3861
  {
    return agg_expr_;
  }

L
LINxiansheng 已提交
3862
  virtual void clear_child() override;
X
xy0 已提交
3863
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
3864

L
LINxiansheng 已提交
3865
  virtual int64_t get_param_count() const override
O
oceanbase-admin 已提交
3866 3867 3868 3869 3870
  {
    int64_t cnt = (agg_expr_ != NULL ? agg_expr_->get_param_count() : 0) + func_params_.count() +
                  partition_exprs_.count() + order_items_.count() + (upper_.interval_expr_ != NULL ? 1 : 0) +
                  (lower_.interval_expr_ != NULL ? 1 : 0);
    for (int64_t i = 0; i < 2; ++i) {
X
xy0 已提交
3871
      const Bound *bound = 0 == i ? &upper_ : &lower_;
O
oceanbase-admin 已提交
3872 3873 3874 3875 3876 3877 3878 3879
      for (int64_t j = 0; j < BOUND_EXPR_MAX; ++j) {
        if (NULL != bound->exprs_[j]) {
          cnt++;
        }
      }
    }
    return cnt;
  }
X
xy0 已提交
3880 3881 3882
  virtual const ObRawExpr *get_param_expr(int64_t index) const override;
  virtual ObRawExpr *&get_param_expr(int64_t index) override;
  virtual int do_visit(ObRawExprVisitor &visitor) override;
O
oceanbase-admin 已提交
3883

L
LINxiansheng 已提交
3884
  virtual uint64_t hash_internal(uint64_t seed) const override;
O
oceanbase-admin 已提交
3885

X
xy0 已提交
3886
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
3887
  VIRTUAL_TO_STRING_KV_CHECK_STACK_OVERFLOW(N_ITEM_TYPE, type_, N_RESULT_TYPE, result_type_, N_EXPR_INFO, info_, N_REL_ID, rel_ids_,
O
oceanbase-admin 已提交
3888 3889 3890
      K_(expr_level), K_(expr_levels), K_(func_type), K_(is_distinct), K_(func_params), K_(partition_exprs),
      K_(order_items), K_(win_type), K_(is_between), K_(upper), K_(lower), KPC_(agg_expr));

G
gm 已提交
3891
public:
O
oceanbase-admin 已提交
3892 3893
  common::ObString sort_str_;

G
gm 已提交
3894
private:
O
oceanbase-admin 已提交
3895 3896 3897 3898 3899
  DISALLOW_COPY_AND_ASSIGN(ObWinFunRawExpr);
  ObItemType func_type_;
  bool is_distinct_;
  bool is_ignore_null_;
  bool is_from_first_;
X
xy0 已提交
3900 3901
  common::ObArray<ObRawExpr *, common::ModulePageAllocator, true> func_params_;
  ObAggFunRawExpr *agg_expr_;
O
oceanbase-admin 已提交
3902 3903 3904 3905
};

////////////////////////////////////////////////////////////////
class ObPseudoColumnRawExpr : public ObTerminalRawExpr {
G
gm 已提交
3906
public:
O
oceanbase-admin 已提交
3907 3908 3909 3910
  ObPseudoColumnRawExpr() : ObTerminalRawExpr(), table_id_(common::OB_INVALID_ID)
  {
    set_expr_class(ObExpr::EXPR_PSEUDO_COLUMN);
  }
X
xy0 已提交
3911
  ObPseudoColumnRawExpr(common::ObIAllocator &alloc) : ObTerminalRawExpr(alloc), table_id_(common::OB_INVALID_ID)
O
oceanbase-admin 已提交
3912 3913 3914 3915
  {
    set_expr_class(ObExpr::EXPR_PSEUDO_COLUMN);
  }
  virtual ~ObPseudoColumnRawExpr(){};
X
xy0 已提交
3916 3917
  int assign(const ObPseudoColumnRawExpr &other);
  int deep_copy(ObRawExprFactory &expr_factory, const ObPseudoColumnRawExpr &other, const uint64_t copy_types,
O
oceanbase-admin 已提交
3918 3919
      bool use_new_allocator = false);
  virtual int replace_expr(
X
xy0 已提交
3920 3921
      const common::ObIArray<ObRawExpr *> &other_exprs, const common::ObIArray<ObRawExpr *> &new_exprs) override;
  virtual bool same_as(const ObRawExpr &expr, ObExprEqualCheckContext *check_context = NULL) const override;
O
oceanbase-admin 已提交
3922

X
xy0 已提交
3923
  virtual int do_visit(ObRawExprVisitor &visitor) override;
L
LINxiansheng 已提交
3924
  virtual uint64_t hash_internal(uint64_t seed) const override;
X
xy0 已提交
3925
  int get_name_internal(char *buf, const int64_t buf_len, int64_t &pos, ExplainType type) const override;
O
oceanbase-admin 已提交
3926 3927 3928 3929 3930 3931 3932 3933
  bool is_hierarchical_query_type() const
  {
    return type_ == T_LEVEL || type_ == T_CONNECT_BY_ISCYCLE || type_ == T_CONNECT_BY_ISLEAF;
  }
  bool is_cte_query_type() const
  {
    return T_CTE_SEARCH_COLUMN == type_ || T_CTE_CYCLE_COLUMN == type_;
  }
X
xy0 已提交
3934
  void set_cte_cycle_value(ObRawExpr *v, ObRawExpr *d_v)
O
oceanbase-admin 已提交
3935 3936 3937 3938
  {
    cte_cycle_value_ = v;
    cte_cycle_default_value_ = d_v;
  };
X
xy0 已提交
3939
  void get_cte_cycle_value(ObRawExpr *&v, ObRawExpr *&d_v)
O
oceanbase-admin 已提交
3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955
  {
    v = cte_cycle_value_;
    d_v = cte_cycle_default_value_;
  };
  void set_table_id(int64_t table_id)
  {
    table_id_ = table_id;
  }
  int64_t get_table_id() const
  {
    return table_id_;
  }

  VIRTUAL_TO_STRING_KV(
      N_ITEM_TYPE, type_, N_RESULT_TYPE, result_type_, N_EXPR_INFO, info_, N_REL_ID, rel_ids_, N_TABLE_ID, table_id_);

G
gm 已提交
3956
private:
X
xy0 已提交
3957 3958
  ObRawExpr *cte_cycle_value_;
  ObRawExpr *cte_cycle_default_value_;
O
oceanbase-admin 已提交
3959 3960 3961 3962 3963
  int64_t table_id_;
  DISALLOW_COPY_AND_ASSIGN(ObPseudoColumnRawExpr);
};
/// visitor interface
class ObRawExprVisitor {
G
gm 已提交
3964
public:
O
oceanbase-admin 已提交
3965 3966 3967 3968 3969 3970
  ObRawExprVisitor()
  {}
  virtual ~ObRawExprVisitor()
  {}

  // OP types: constants, ? etc.
X
xy0 已提交
3971
  virtual int visit(ObConstRawExpr &expr) = 0;
O
oceanbase-admin 已提交
3972
  // OP types: ObObjtypes
X
xy0 已提交
3973
  virtual int visit(ObVarRawExpr &expr) = 0;
O
oceanbase-admin 已提交
3974
  // OP types: subquery, cell index
X
xy0 已提交
3975
  virtual int visit(ObQueryRefRawExpr &expr) = 0;
O
oceanbase-admin 已提交
3976
  // OP types: identify, table.column
X
xy0 已提交
3977
  virtual int visit(ObColumnRefRawExpr &expr) = 0;
O
oceanbase-admin 已提交
3978 3979 3980 3981
  // unary OP types: exists, not, negative, positive
  // binary OP types: +, -, *, /, >, <, =, <=>, IS, IN etc.
  // triple OP types: like, not like, btw, not btw
  // multi OP types: and, or, ROW
X
xy0 已提交
3982
  virtual int visit(ObOpRawExpr &expr) = 0;
O
oceanbase-admin 已提交
3983
  // OP types: case, arg case
X
xy0 已提交
3984
  virtual int visit(ObCaseOpRawExpr &expr) = 0;
O
oceanbase-admin 已提交
3985
  // OP types: aggregate functions e.g. max, min, avg, count, sum
X
xy0 已提交
3986
  virtual int visit(ObAggFunRawExpr &expr) = 0;
O
oceanbase-admin 已提交
3987
  // OP types: system functions
X
xy0 已提交
3988 3989 3990
  virtual int visit(ObSysFunRawExpr &expr) = 0;
  virtual int visit(ObSetOpRawExpr &expr) = 0;
  virtual int visit(ObAliasRefRawExpr &expr)
O
oceanbase-admin 已提交
3991 3992 3993 3994
  {
    UNUSED(expr);
    return common::OB_SUCCESS;
  }
X
xy0 已提交
3995
  virtual int visit(ObFunMatchAgainst &expr)
O
oceanbase-admin 已提交
3996 3997 3998 3999
  {
    UNUSED(expr);
    return common::OB_SUCCESS;
  }
X
xy0 已提交
4000
  virtual int visit(ObSetIterRawExpr &expr)
O
oceanbase-admin 已提交
4001 4002 4003 4004
  {
    UNUSED(expr);
    return common::OB_SUCCESS;
  }
X
xy0 已提交
4005
  virtual int visit(ObRowIterRawExpr &expr)
O
oceanbase-admin 已提交
4006 4007 4008 4009
  {
    UNUSED(expr);
    return common::OB_SUCCESS;
  }
X
xy0 已提交
4010
  virtual int visit(ObWinFunRawExpr &expr)
O
oceanbase-admin 已提交
4011 4012 4013 4014
  {
    UNUSED(expr);
    return common::OB_SUCCESS;
  }
X
xy0 已提交
4015
  virtual int visit(ObPseudoColumnRawExpr &expr)
O
oceanbase-admin 已提交
4016 4017 4018 4019
  {
    UNUSED(expr);
    return common::OB_SUCCESS;
  }
X
xy0 已提交
4020
  virtual bool skip_child(ObRawExpr &expr)
O
oceanbase-admin 已提交
4021 4022 4023 4024 4025
  {
    UNUSED(expr);
    return false;
  }

G
gm 已提交
4026
private:
O
oceanbase-admin 已提交
4027 4028 4029 4030 4031
  // disallow copy
  DISALLOW_COPY_AND_ASSIGN(ObRawExprVisitor);
};

class ObRawExprFactory {
G
gm 已提交
4032
public:
X
xy0 已提交
4033
  explicit ObRawExprFactory(common::ObIAllocator &alloc) : allocator_(alloc), expr_store_(alloc)
O
oceanbase-admin 已提交
4034 4035 4036 4037 4038 4039 4040 4041 4042 4043
  {}
  ~ObRawExprFactory()
  {
    if (!THIS_WORKER.has_req_flag()) {
      destory();
    }
  }
  //~ObRawExprFactory() { }

  template <typename ExprType>
X
xy0 已提交
4044
  inline int create_raw_expr(ObItemType expr_type, ExprType *&raw_expr)
O
oceanbase-admin 已提交
4045 4046
  {
    int ret = common::OB_SUCCESS;
X
xy0 已提交
4047
    void *ptr = allocator_.alloc(sizeof(ExprType));
O
oceanbase-admin 已提交
4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078
    raw_expr = NULL;
    if (OB_UNLIKELY(NULL == ptr)) {
      ret = common::OB_ALLOCATE_MEMORY_FAILED;
      SQL_RESV_LOG(ERROR, "no more memory to create raw expr");
    } else {
      raw_expr = new (ptr) ExprType(allocator_);
      raw_expr->set_allocator(allocator_);
      raw_expr->set_expr_factory(*this);
      raw_expr->set_expr_type(expr_type);
      if (OB_FAIL(expr_store_.store_obj(raw_expr))) {
        SQL_RESV_LOG(WARN, "store raw expr failed", K(ret));
        raw_expr->~ExprType();
        raw_expr = NULL;
      } else {
        SQL_RESV_LOG(DEBUG, "create_raw_expr", K(expr_type), "expr_type", get_type_name(expr_type), K(lbt()));
      }
    }
    return ret;
  }

  inline void destory()
  {
    DLIST_FOREACH_NORET(node, expr_store_.get_obj_list())
    {
      if (node != NULL && node->get_obj() != NULL) {
        node->get_obj()->~ObRawExpr();
      }
    }
    expr_store_.destory();
  }

X
xy0 已提交
4079
  inline common::ObIAllocator &get_allocator()
O
oceanbase-admin 已提交
4080 4081 4082 4083 4084
  {
    return allocator_;
  }
  TO_STRING_KV("", "");

G
gm 已提交
4085
private:
X
xy0 已提交
4086 4087
  common::ObIAllocator &allocator_;
  common::ObObjStore<ObRawExpr *, common::ObIAllocator &, true> expr_store_;
O
oceanbase-admin 已提交
4088

G
gm 已提交
4089
private:
O
oceanbase-admin 已提交
4090 4091 4092 4093
  DISALLOW_COPY_AND_ASSIGN(ObRawExprFactory);
};

class ObRawExprPointer {
G
gm 已提交
4094
public:
O
oceanbase-admin 已提交
4095 4096 4097
  ObRawExprPointer();

  virtual ~ObRawExprPointer();
X
xy0 已提交
4098 4099 4100
  int get(ObRawExpr *&expr);
  int set(ObRawExpr *expr);
  int add_ref(ObRawExpr **expr);
O
oceanbase-admin 已提交
4101 4102
  TO_STRING_KV("", "");

G
gm 已提交
4103
private:
X
xy0 已提交
4104
  common::ObSEArray<ObRawExpr **, 1> expr_group_;
O
oceanbase-admin 已提交
4105 4106 4107 4108 4109 4110
};

}  // namespace sql
}  // namespace oceanbase

#endif  // OCEANBASE_SQL_RESOLVER_EXPR_RAW_EXPR_