ob_logical_operator.h 78.0 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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
/**
 * 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_OB_LOGICAL_OPERATOR_H
#define OCEANBASE_SQL_OB_LOGICAL_OPERATOR_H

#include "lib/allocator/page_arena.h"
#include "lib/container/ob_array.h"
#include "lib/container/ob_array_iterator.h"
#include "lib/json/ob_json.h"
#include "sql/resolver/expr/ob_raw_expr.h"
#include "sql/resolver/expr/ob_raw_expr_util.h"
#include "sql/resolver/ob_stmt.h"
#include "sql/rewrite/ob_equal_analysis.h"
#include "sql/optimizer/ob_log_operator_factory.h"
#include "sql/optimizer/ob_opt_est_sel.h"
#include "sql/optimizer/ob_optimizer.h"
#include "sql/optimizer/ob_optimizer_util.h"
#include "sql/optimizer/ob_raw_expr_check_dep.h"
#include "sql/optimizer/ob_sharding_info.h"
#include "sql/engine/px/ob_px_op_size_factor.h"
#include "sql/ob_sql_context.h"
#include "sql/optimizer/ob_fd_item.h"

namespace oceanbase {
namespace sql {
class planText;
struct partition_location {
  int64_t partition_id;
  // server id
  TO_STRING_KV(K(partition_id));
};
static const char ID[] = "ID";
static const char OPERATOR[] = "OPERATOR";
static const char NAME[] = "NAME";
static const char ROWS[] = "EST.ROWS";
static const char COST[] = "COST";
static const char OPCOST[] = "OP_COST";

/**
 *  Print log info with expressions
 */
#define EXPLAIN_PRINT_EXPRS(exprs, type)                                                       \
  {                                                                                            \
    int64_t N = -1;                                                                            \
    if (OB_FAIL(ret)) {                           /* Do nothing */                             \
    } else if (OB_FAIL(BUF_PRINTF(#exprs "("))) { /* Do nothing */                             \
    } else if (FALSE_IT(N = exprs.count())) {     /* Do nothing */                             \
    } else if (N == 0) {                                                                       \
      ret = BUF_PRINTF("nil");                                                                 \
    } else {                                                                                   \
      for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {                                        \
        if (OB_FAIL(BUF_PRINTF("["))) { /* Do nothing */                                       \
        } else if (OB_ISNULL(exprs.at(i))) {                                                   \
          ret = OB_ERR_UNEXPECTED;                                                             \
        } else if (OB_FAIL(exprs.at(i)->get_name(buf, buf_len, pos, type))) { /*Do nothing */  \
        } else if (OB_FAIL(BUF_PRINTF("]"))) {                                /* Do nothing */ \
        } else if (i < N - 1) {                                                                \
          ret = BUF_PRINTF(", ");                                                              \
        } else { /* Do nothing */                                                              \
        }                                                                                      \
      }                                                                                        \
    }                                                                                          \
    if (OB_SUCC(ret)) { /* Do nothing */                                                       \
      ret = BUF_PRINTF(")");                                                                   \
    } else { /* Do nothing */                                                                  \
    }                                                                                          \
  }

#define EXPLAIN_PRINT_EXPR(expr, type)                             \
  {                                                                \
    if (OB_FAIL(ret)) {                          /* Do nothing */  \
    } else if (OB_FAIL(BUF_PRINTF(#expr "("))) { /* Do nothing */  \
    } else if (OB_ISNULL(expr)) {                                  \
      ret = BUF_PRINTF("nil");                                     \
    } else if (OB_FAIL(expr->get_name(buf, buf_len, pos, type))) { \
      LOG_WARN("print expr name failed", K(ret));                  \
    } else { /*Do nothing*/                                        \
    }                                                              \
    if (OB_SUCC(ret)) { /* Do nothing */                           \
      ret = BUF_PRINTF(")");                                       \
    } else { /* Do nothing */                                      \
    }                                                              \
  }

/**
 *  Print log info with expressions
 */
#define EXPLAIN_PRINT_SORT_KEYS(exprs, type)                                                         \
  {                                                                                                  \
    int64_t N = -1;                                                                                  \
    if (OB_FAIL(ret)) {                           /* Do nothing */                                   \
    } else if (OB_FAIL(BUF_PRINTF(#exprs "("))) { /* Do nothing */                                   \
    } else if (FALSE_IT(N = exprs.count())) {                                                        \
    } else if (N == 0) {                                                                             \
      BUF_PRINTF("nil");                                                                             \
    } else {                                                                                         \
      for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {                                              \
        if (OB_FAIL(BUF_PRINTF("["))) { /* Do nothing */                                             \
        } else if (OB_ISNULL(exprs.at(i).expr_)) {                                                   \
          ret = common::OB_ERR_UNEXPECTED;                                                           \
        } else if (OB_FAIL(exprs.at(i).expr_->get_name(buf, buf_len, pos, type))) { /* Do nothing */ \
        } else if (exprs.at(i).is_descending()) {                                                    \
          ret = BUF_PRINTF(", DESC");                                                                \
        } else { /* Do nothing */                                                                    \
        }                                                                                            \
        if (OB_FAIL(ret)) {                    /* Do nothing */                                      \
        } else if (OB_FAIL(BUF_PRINTF("]"))) { /* Do nothing */                                      \
        } else if (i < N - 1) {                                                                      \
          ret = BUF_PRINTF(", ");                                                                    \
        } else { /* Do nothing */                                                                    \
        }                                                                                            \
      }                                                                                              \
    }                                                                                                \
    if (OB_SUCC(ret)) { /* Do nothing */                                                             \
      ret = BUF_PRINTF(")");                                                                         \
    } else { /* Do nothing */                                                                        \
    }                                                                                                \
  }

/**
 *  Print log info with exec params
 */
#define EXPLAIN_PRINT_EXEC_PARAMS(exec_params, type)                                       \
  {                                                                                        \
    int64_t N = -1;                                                                        \
    if (OB_FAIL(ret)) {                                 /* Do nothing */                   \
    } else if (OB_FAIL(BUF_PRINTF(#exec_params "("))) { /* Do nothing */                   \
    } else if (FALSE_IT(N = exec_params.count())) {     /* Do nothing */                   \
    } else if (N == 0) {                                                                   \
      ret = BUF_PRINTF("nil");                                                             \
    } else {                                                                               \
      for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {                                    \
        if (OB_FAIL(BUF_PRINTF("["))) { /* Do nothing */                                   \
        } else if (OB_ISNULL(exec_params.at(i).second)) {                                  \
          ret = OB_ERR_UNEXPECTED;                                                         \
        } else if (OB_FAIL(exec_params.at(i).second->get_name(buf, buf_len, pos, type))) { \
        } else if (OB_FAIL(BUF_PRINTF("]"))) { /* Do nothing */                            \
        } else if (i < N - 1) {                                                            \
          ret = BUF_PRINTF(", ");                                                          \
        } else { /* Do nothing */                                                          \
        }                                                                                  \
      }                                                                                    \
    }                                                                                      \
    if (OB_SUCC(ret)) {                                                                    \
      ret = BUF_PRINTF(")");                                                               \
    } else { /* Do nothing */                                                              \
    }                                                                                      \
  }

/**
 *  Print log info with idxs
 */
#define EXPLAIN_PRINT_IDXS(idxs)                                             \
  {                                                                          \
    ObSEArray<int64_t, 4, common::ModulePageAllocator, true> arr;            \
    int64_t N = -1;                                                          \
    if (OB_FAIL(ret)) {                          /* Do nothing */            \
    } else if (OB_FAIL(BUF_PRINTF(#idxs "("))) { /* Do nothing */            \
    } else if (OB_FAIL(idxs.to_array(arr))) {    /* Do nothing */            \
    } else if (FALSE_IT(N = arr.count())) {      /* Do nothing */            \
    } else if (N == 0) {                                                     \
      ret = BUF_PRINTF("nil");                                               \
    } else {                                                                 \
      for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {                      \
        if (OB_FAIL(BUF_PRINTF("["))) {                     /* Do nothing */ \
        } else if (OB_FAIL(BUF_PRINTF("%lu", arr.at(i)))) { /* Do nothing */ \
        } else if (OB_FAIL(BUF_PRINTF("]"))) {              /* Do nothing */ \
        } else if (i < N - 1) {                                              \
          ret = BUF_PRINTF(", ");                                            \
        } else { /* Do nothing */                                            \
        }                                                                    \
      }                                                                      \
    }                                                                        \
    if (OB_SUCC(ret)) { /* Do nothing */                                     \
      ret = BUF_PRINTF(")");                                                 \
    } else { /* Do nothing */                                                \
    }                                                                        \
  }

/**
 *  Print log info with expressions
 */
#define EXPLAIN_PRINT_MERGE_DIRECTIONS(directions)                      \
  {                                                                     \
    int64_t N = -1;                                                     \
    if (OB_FAIL(ret)) {                                /* Do nothing */ \
    } else if (OB_FAIL(BUF_PRINTF(#directions "("))) { /* Do nothing */ \
    } else if (FALSE_IT(N = directions.count())) {     /* Do nothing */ \
    } else if (N == 0) {                                                \
      ret = BUF_PRINTF("nil");                                          \
    } else {                                                            \
      for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {                 \
        if (OB_FAIL(BUF_PRINTF("["))) { /* Do nothing */                \
        } else if (is_descending_direction(directions.at(i))) {         \
          ret = BUF_PRINTF("DESC");                                     \
        } else {                                                        \
          ret = BUF_PRINTF("ASC");                                      \
        }                                                               \
        if (OB_FAIL(BUF_PRINTF("]"))) { /* Do nothing */                \
        } else if (i < N - 1) {                                         \
          ret = BUF_PRINTF(", ");                                       \
        } else { /* Do nothing */                                       \
        }                                                               \
      }                                                                 \
    }                                                                   \
    if (OB_SUCC(ret)) {                                                 \
      ret = BUF_PRINTF(")");                                            \
    } else { /* Do nothing */                                           \
    }                                                                   \
  }

/**
 *  Print log info with expressions
 */
#define EXPLAIN_PRINT_SORT_ITEMS(sort_keys, type)                                       \
  {                                                                                     \
    int64_t N = -1;                                                                     \
    if (OB_FAIL(ret)) {                               /* Do nothing */                  \
    } else if (OB_FAIL(BUF_PRINTF(#sort_keys "("))) { /* Do nothing */                  \
    } else if (FALSE_IT(N = sort_keys.count())) {     /* Do nothing */                  \
    } else if (0 == N) {                                                                \
      ret = BUF_PRINTF("nil");                                                          \
    } else {                                                                            \
      for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {                                 \
        if (OB_FAIL(BUF_PRINTF("["))) { /* Do nothing */                                \
        } else if (OB_ISNULL(sort_keys.at(i).expr_)) {                                  \
          ret = OB_ERR_UNEXPECTED;                                                      \
        } else if (OB_FAIL(sort_keys.at(i).expr_->get_name(buf, buf_len, pos, type))) { \
          LOG_WARN("fail to get name", K(i), K(ret));                                   \
        } else if (OB_FAIL(BUF_PRINTF(", "))) { /* Do nothing */                        \
        } else if (is_ascending_direction(sort_keys.at(i).order_type_)) {               \
          ret = BUF_PRINTF("ASC");                                                      \
        } else {                                                                        \
          ret = BUF_PRINTF("DESC");                                                     \
        }                                                                               \
        if (OB_FAIL(ret)) {                    /* Do nothing */                         \
        } else if (OB_FAIL(BUF_PRINTF("]"))) { /* Do nothing */                         \
        } else if (i < N - 1) {                                                         \
          ret = BUF_PRINTF(", ");                                                       \
        } else { /* Do nothing */                                                       \
        }                                                                               \
      }                                                                                 \
    }                                                                                   \
    if (OB_SUCC(ret)) {                                                                 \
      ret = BUF_PRINTF(")");                                                            \
    } else { /* Do nothing */                                                           \
    }                                                                                   \
  }

/**
 * these operator never generate expr
 */

#define IS_EXPR_PASSBY_OPER(type)                                                  \
  (log_op_def::LOG_GRANULE_ITERATOR == (type) || log_op_def::LOG_LINK == (type) || \
      log_op_def::LOG_EXCHANGE == (type) || log_op_def::LOG_MONITORING_DUMP == (type))

struct FilterCompare {
  FilterCompare(common::ObIArray<ObExprSelPair>& predicate_selectivities)
      : predicate_selectivities_(predicate_selectivities)
  {}
  bool operator()(ObRawExpr* left, ObRawExpr* right)
  {
    return (get_selectivity(left) < get_selectivity(right));
  }
  double get_selectivity(ObRawExpr* expr);

  common::ObIArray<ObExprSelPair>& predicate_selectivities_;
};

class AdjustSortContext {
G
gm 已提交
282
public:
O
oceanbase-admin 已提交
283 284 285 286 287 288 289 290 291
  bool has_exchange_;
  // count the exchange, in-out will be add 2
  int64_t exchange_cnt_;

  AdjustSortContext() : has_exchange_(false), exchange_cnt_(0)
  {}
};

class AllocGIContext {
G
gm 已提交
292
public:
O
oceanbase-admin 已提交
293 294 295 296 297 298 299
  enum GIState {
    GIS_NORMAL = 0,
    GIS_IN_PARTITION_WISE,
    GIS_PARTITION_WITH_AFFINITY,
    GIS_PARTITION,
  };

G
gm 已提交
300
public:
O
oceanbase-admin 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  explicit AllocGIContext(int64_t parallel)
      : alloc_gi_(false),
        tablet_size_(common::OB_DEFAULT_TABLET_SIZE),
        state_(GIS_NORMAL),
        pw_op_ptr_(nullptr),
        exchange_op_above_count_(0),
        multi_child_op_above_count_in_dfo_(0),
        parallel_(parallel),
        partition_count_(0),
        hash_part_(false),
        slave_mapping_type_(SM_NONE),
        is_del_upd_insert_(false),
        is_valid_for_gi_(false),
        enable_gi_partition_pruning_(false)
  {}
  ~AllocGIContext()
  {
    dfo_table_dop_stack_.reset();
  }
  bool managed_by_gi();
  bool is_in_partition_wise_state();
  bool is_in_pw_affinity_state();
  bool is_partition_gi()
  {
    return GIS_PARTITION == state_;
  };
  void set_in_partition_wise_state(ObLogicalOperator* op_ptr);
  bool try_set_out_partition_wise_state(ObLogicalOperator* op_ptr);
  bool is_op_set_pw(ObLogicalOperator* op_ptr);
  int set_pw_affinity_state();
  void enable_gi_partition_pruning()
  {
    enable_gi_partition_pruning_ = true;
  }
  void reset_info();
  GIState get_state();
  void add_exchange_op_count()
  {
    exchange_op_above_count_++;
  }
  bool exchange_above()
  {
    return 0 != exchange_op_above_count_;
  }
  void delete_exchange_op_count()
  {
    exchange_op_above_count_--;
  }
  void add_multi_child_op_count()
  {
    multi_child_op_above_count_in_dfo_++;
  }
  bool multi_child_op_above()
  {
    return 0 != multi_child_op_above_count_in_dfo_;
  }
  void delete_multi_child_op_count()
  {
    multi_child_op_above_count_in_dfo_--;
  }
  void reset_state()
  {
    state_ = GIS_NORMAL;
  }
  void set_force_partition()
  {
    state_ = GIS_PARTITION;
  }
  bool force_partition()
  {
    return GIS_PARTITION == state_;
  }
  int push_current_dfo_dop(int64_t dop);
  int pop_current_dfo_dop();
  inline bool is_in_slave_mapping()
  {
    return SlaveMappingType::SM_NONE != slave_mapping_type_;
  }
  TO_STRING_KV(K(alloc_gi_), K(tablet_size_), K(state_), K(exchange_op_above_count_));
  bool alloc_gi_;
  int64_t tablet_size_;
  GIState state_;
  ObLogicalOperator* pw_op_ptr_;
  int64_t exchange_op_above_count_;
  int64_t multi_child_op_above_count_in_dfo_;
  int64_t parallel_;
  int64_t partition_count_;
  bool hash_part_;
  SlaveMappingType slave_mapping_type_;
  bool is_del_upd_insert_;
  bool is_valid_for_gi_;
  common::ObSEArray<int64_t, 16> dfo_table_dop_stack_;
  bool enable_gi_partition_pruning_;
};

class ObAllocGIInfo {
G
gm 已提交
397
public:
O
oceanbase-admin 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
  ObAllocGIInfo()
      : state_(AllocGIContext::GIS_NORMAL),
        pw_op_ptr_(nullptr),
        multi_child_op_above_count_in_dfo_(0),
        enable_gi_partition_pruning_(false)
  {}
  TO_STRING_KV(K(state_), K(pw_op_ptr_));
  virtual ~ObAllocGIInfo() = default;
  void set_info(AllocGIContext& ctx)
  {
    state_ = ctx.state_;
    pw_op_ptr_ = ctx.pw_op_ptr_;
    multi_child_op_above_count_in_dfo_ = ctx.multi_child_op_above_count_in_dfo_;
    enable_gi_partition_pruning_ = ctx.enable_gi_partition_pruning_;
  }
  void get_info(AllocGIContext& ctx)
  {
    ctx.state_ = state_;
    ctx.pw_op_ptr_ = pw_op_ptr_;
    ctx.multi_child_op_above_count_in_dfo_ = multi_child_op_above_count_in_dfo_;
    ctx.enable_gi_partition_pruning_ = enable_gi_partition_pruning_;
  }
  AllocGIContext::GIState state_;
  ObLogicalOperator* pw_op_ptr_;
  int64_t multi_child_op_above_count_in_dfo_;
  bool enable_gi_partition_pruning_;
};

class AllocMDContext {
G
gm 已提交
427
public:
O
oceanbase-admin 已提交
428 429 430 431 432 433
  AllocMDContext() : org_op_id_(0){};
  ~AllocMDContext() = default;
  int64_t org_op_id_;
};

class AllocExchContext {
G
gm 已提交
434
public:
O
oceanbase-admin 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
  enum DistrStat {
    UNINITIALIZED = 0,
    LOCAL,
    REMOTE,
    DISTRIBUTED,
    MATCH_ALL,
  };
  explicit AllocExchContext(int64_t parallel)
      : group_push_down_replaced_exprs_(),
        plan_type_(UNINITIALIZED),
        parallel_(parallel),
        exchange_allocated_(false),
        servers_(),
        weak_part_exprs_(),
        sharding_conds_()
  {}
  virtual ~AllocExchContext()
  {}
  void add_exchange_type(DistrStat status);  // for calc plan type
  DistrStat get_plan_type() const
  {
    return plan_type_;
  }

  TO_STRING_KV(K(plan_type_), K(parallel_), K(servers_));

  common::ObSEArray<std::pair<ObRawExpr*, ObRawExpr*>, 4, common::ModulePageAllocator, true>
      group_push_down_replaced_exprs_;
  DistrStat plan_type_;
  int64_t parallel_;
  bool exchange_allocated_;
  common::ObSEArray<common::ObAddr, 16> servers_;
  common::ObSEArray<ObRawExpr*, 8, common::ModulePageAllocator, true> weak_part_exprs_;
  common::ObSEArray<ObRawExpr*, 8, common::ModulePageAllocator, true> sharding_conds_;
};

struct ObExchangeInfo {
  struct HashExpr {
    HashExpr() : expr_(NULL)
    {}
    HashExpr(ObRawExpr* expr, const ObObjMeta& cmp_type) : expr_(expr), cmp_type_(cmp_type)
    {}

    TO_STRING_KV(K_(expr), K_(cmp_type));

    ObRawExpr* expr_;

    // Compare type of %expr_ when compare with other values.
    // Objects should convert to %cmp_type_ before calculate hash value.
    //
    // Only type_ and cs_type_ of %cmp_type_ are used right now.
    ObObjMeta cmp_type_;
  };
  ObExchangeInfo()
      : is_task_order_(false),
        slice_count_(1),
        repartition_type_(OB_REPARTITION_NO_REPARTITION),
        repartition_ref_table_id_(OB_INVALID_ID),
        repartition_table_name_(),
        calc_part_id_expr_(NULL),
        repartition_keys_(),
        repartition_sub_keys_(),
        repartition_func_exprs_(),
        keep_ordering_(false),
        dist_method_(ObPQDistributeMethod::MAX_VALUE),
        unmatch_row_dist_method_(ObPQDistributeMethod::MAX_VALUE),
        px_dop_(0),
        px_single_(false),
        pdml_pkey_(false),
        slave_mapping_type_(SlaveMappingType::SM_NONE)
  {}
  virtual ~ObExchangeInfo()
  {}
  void reset();
  int clone(ObExchangeInfo& exch_info);
  int set_repartition_info(const common::ObIArray<ObRawExpr*>& repart_keys,
      const common::ObIArray<ObRawExpr*>& repart_sub_keys, const common::ObIArray<ObRawExpr*>& repart_func_exprs);
  int init_calc_part_id_expr(ObOptimizerContext& opt_ctx);
  void set_keep_ordering(bool keep_ordering)
  {
    keep_ordering_ = keep_ordering;
  }
  void set_slave_mapping_type(SlaveMappingType slave_mapping_type)
  {
    slave_mapping_type_ = slave_mapping_type;
  }
  bool is_repart_exchange() const
  {
    return OB_REPARTITION_NO_REPARTITION != repartition_type_;
  }
  bool is_no_repart_exchange() const
  {
    return OB_REPARTITION_NO_REPARTITION == repartition_type_;
  }
  bool is_two_level_repart() const
  {
    return OB_REPARTITION_ONE_SIDE_TWO_LEVEL == repartition_type_;
  }
  bool is_keep_order() const
  {
    return keep_ordering_;
  }
  bool is_task_order() const
  {
    return is_task_order_;
  }
  uint64_t hash(uint64_t seed) const;

  bool is_pq_hash_dist() const
  {
    return ObPQDistributeMethod::HASH == dist_method_;
  }
  bool is_pq_broadcast_dist() const
  {
    return ObPQDistributeMethod::BROADCAST == dist_method_;
  }
  bool is_pq_pkey() const
  {
    return ObPQDistributeMethod::PARTITION == dist_method_;
  }
  bool is_pq_dist() const
  {
    return dist_method_ < ObPQDistributeMethod::MAX_VALUE;
  }
  SlaveMappingType get_slave_mapping_type() const
  {
    return slave_mapping_type_;
  }

  int append_hash_dist_expr(const common::ObIArray<ObRawExpr*>& exprs);

  bool is_task_order_;
  int64_t slice_count_;
  ObRepartitionType repartition_type_;
  int64_t repartition_ref_table_id_;
  ObString repartition_table_name_;  // just for print plan
  ObRawExpr* calc_part_id_expr_;
  common::ObSEArray<ObRawExpr*, 4, common::ModulePageAllocator, true> repartition_keys_;
  common::ObSEArray<ObRawExpr*, 4, common::ModulePageAllocator, true> repartition_sub_keys_;
  common::ObSEArray<ObRawExpr*, 4, common::ModulePageAllocator, true> repartition_func_exprs_;
  bool keep_ordering_;
  common::ObSEArray<HashExpr, 4, common::ModulePageAllocator, true> hash_dist_exprs_;
  ObPQDistributeMethod::Type dist_method_;
  ObPQDistributeMethod::Type unmatch_row_dist_method_;

  // degree of parallelism, <= 0 means no restrict
  int64_t px_dop_;
  // PX worker must on single thread
  bool px_single_;
  // pdml pkey
  bool pdml_pkey_;
  // slave mapping exchange
  SlaveMappingType slave_mapping_type_;

  TO_STRING_KV(K_(is_task_order), K_(slice_count), K_(repartition_type), K_(repartition_ref_table_id),
      K_(repartition_table_name), K_(calc_part_id_expr), K_(repartition_keys), K_(repartition_sub_keys),
      K_(hash_dist_exprs), "dist_method", ObPQDistributeMethod::get_type_string(dist_method_), K_(px_dop),
      K_(px_single), K_(repartition_func_exprs), K_(keep_ordering), K_(pdml_pkey), K_(slave_mapping_type));

G
gm 已提交
594
private:
O
oceanbase-admin 已提交
595 596 597 598 599 600 601
  DISALLOW_COPY_AND_ASSIGN(ObExchangeInfo);
};

/**
 *  Expr and its producer operator
 */
class ExprProducer {
G
gm 已提交
602
public:
O
oceanbase-admin 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
  ExprProducer()
      : expr_(NULL),
        producer_branch_(common::OB_INVALID_ID),
        consumer_id_(common::OB_INVALID_ID),
        producer_id_(common::OB_INVALID_ID),
        is_shared_(false)
  {}
  ExprProducer(const ObRawExpr* expr, int64_t consumer_id)
      : expr_(expr),
        producer_branch_(common::OB_INVALID_ID),
        consumer_id_(consumer_id),
        producer_id_(common::OB_INVALID_ID),
        is_shared_(false)
  {}

  ExprProducer(const ObRawExpr* expr, int64_t consumer_id, int64_t producer_id)
      : expr_(expr),
        producer_branch_(common::OB_INVALID_ID),
        consumer_id_(consumer_id),
        producer_id_(producer_id),
        is_shared_(false)
  {}

  ExprProducer(const ExprProducer& other)
      : expr_(other.expr_),
        producer_branch_(other.producer_branch_),
        consumer_id_(other.consumer_id_),
        producer_id_(other.producer_id_),
        is_shared_(other.is_shared_)
  {}

  ExprProducer& operator=(const ExprProducer& other)
  {
    expr_ = other.expr_;
    producer_branch_ = other.producer_branch_;
    consumer_id_ = other.consumer_id_;
    producer_id_ = other.producer_id_;
    is_shared_ = other.is_shared_;
    return *this;
  }
  bool not_produced() const
  {
    return common::OB_INVALID_ID == producer_branch_;
  }
  TO_STRING_KV(K_(consumer_id), K_(producer_id), K_(producer_branch), K(is_shared_), KPC_(expr));

  const ObRawExpr* expr_;
  uint64_t producer_branch_;
  uint64_t consumer_id_;
  uint64_t producer_id_;
  bool is_shared_;
};

struct ObAllocExprContext {
  ObAllocExprContext(ObIAllocator& allocator)
      : expr_producers_(), group_replaced_exprs_(), not_produced_exprs_(allocator)
  {}
  ~ObAllocExprContext();

  int set_group_replaced_exprs(common::ObIArray<std::pair<ObRawExpr*, ObRawExpr*> >& exprs)
  {
    return group_replaced_exprs_.assign(exprs);
  }
  int find(const ObRawExpr* expr, ExprProducer*& producer);

  int add(const ExprProducer& producer);

  DISALLOW_COPY_AND_ASSIGN(ObAllocExprContext);

  hash::ObHashMap<uint64_t, int64_t> expr_map_;
  common::ObSEArray<ExprProducer, 4, common::ModulePageAllocator, true> expr_producers_;
  common::ObSEArray<std::pair<ObRawExpr*, ObRawExpr*>, 4, common::ModulePageAllocator, true> group_replaced_exprs_;
  ObRawExprUniqueSet not_produced_exprs_;
};

struct ObPxPipeBlockingCtx {
  // Look through the pipe (may be blocked by block operator), ended with a exchange operator or not.
  class PipeEnd {
G
gm 已提交
681
  public:
O
oceanbase-admin 已提交
682 683 684 685 686 687 688 689 690 691 692 693 694 695
    PipeEnd() : exch_(false)
    {}
    explicit PipeEnd(const bool is_exch) : exch_(is_exch)
    {}
    void set_exch(bool is_exch)
    {
      exch_ = is_exch;
    }
    bool is_exch() const
    {
      return exch_;
    }
    TO_STRING_KV(K_(exch));

G
gm 已提交
696
  private:
O
oceanbase-admin 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
    bool exch_;
  };

  // %in_ filed of OpCtx include the operator it self.
  // e.g. Opctx of SORT:
  //
  //   EXCH
  //    |
  //   MERGE GBY
  //    |
  //    |   out_: is exch
  //    |<----- viewpoint of OpCtx (above operator)
  //    |   in_: is not exch (blocked by sort operator)
  //    |   has_dfo_below_: true
  //   SORT
  //    |
  //   EXCH
  struct OpCtx {
    OpCtx() : out_(), in_(), has_dfo_below_(false), dfo_depth_(-1)
    {}

    PipeEnd out_;
    PipeEnd in_;

    bool has_dfo_below_;
    int64_t dfo_depth_;

    TO_STRING_KV(K_(in), K_(out), K_(has_dfo_below), K_(dfo_depth));
  };

  explicit ObPxPipeBlockingCtx(common::ObIAllocator& alloc);
  ~ObPxPipeBlockingCtx();

  OpCtx* alloc();
  TO_STRING_KV(K(op_ctxs_));

G
gm 已提交
733
private:
O
oceanbase-admin 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747
  common::ObIAllocator& alloc_;
  common::ObArray<OpCtx*> op_ctxs_;
};

typedef common::ObList<common::ObString, common::ObIAllocator> ObStringList;
typedef common::ObList<common::ObString, common::ObIAllocator>::iterator ObStringListIter;
typedef common::ObList<common::ObString, common::ObIAllocator>::const_iterator ObStringListConstIter;
typedef common::ObIArray<common::ObString> ObStringIArray;
typedef common::ObIArray<ObRawExpr*> ObRawExprIArray;
typedef common::ObIArray<OrderItem> ObOrderItemIArray;
typedef common::ObArray<ObRawExpr*> ObRawExprArray;

class GenLinkStmtContext;
class ObLinkStmt {
G
gm 已提交
748
public:
O
oceanbase-admin 已提交
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
  explicit ObLinkStmt(common::ObIAllocator& alloc, const ObRawExprIArray& output_exprs)
      : link_ctx_(NULL),
        alloc_(alloc),
        root_output_exprs_(output_exprs),
        select_strs_(alloc),
        from_strs_(alloc),
        where_strs_(alloc),
        groupby_strs_(alloc),
        having_strs_(alloc),
        orderby_strs_(alloc),
        tmp_buf_(NULL),
        tmp_buf_len_(-1),
        is_inited_(false),
        is_distinct_(false)
  {}
  virtual ~ObLinkStmt()
  {}
  int64_t to_string(char* buf, const int64_t buf_len) const;
  /**
   * link op must not have child link op now, even if cluster A has a dblink to B,
   * and cluster B has a dblink to C.
   * sql in cluster A can access cluster C using synonym or view in cluster B,
   * but cluster A can not get schema info from C because he has no privilege.
   * under precondition above, we can use single ObLinkStmt for the traverse
   * operation, even if there are mutli link ops in plan.
   * but we need add some functions, such as init / reset.
   * the pre() of link op should call init(), and post() call reset().
   * other ops will fill relevant member if ObLinkStmt is inited, or
   * do nothing if not inited.
   */
  int init(GenLinkStmtContext* link_ctx);
  void reset();
  bool is_inited() const
  {
    return is_inited_;
  }

  int get_first_table_blank(ObStringList& strs, ObStringListIter& iter) const;
  int gen_stmt_fmt(char* buf, int32_t buf_len, int32_t& pos) const;
  int get_nl_param_columns(ObRawExprIArray& param_exprs, ObRawExprIArray& access_exprs, ObRawExprIArray& param_columns);

  bool is_select_strs_empty() const
  {
    return select_strs_.empty();
  }
  bool is_same_output_exprs_ignore_seq(const ObRawExprIArray& output_exprs);
  int append_nl_param_idx(int64_t param_idx);
  int try_fill_select_strs(const ObRawExprIArray& exprs);
  int force_fill_select_strs();
  int append_select_strs(const ObRawExprIArray& param_columns);
  int set_select_distinct();
  int fill_from_set(ObSelectStmt::SetOperator set_type, bool is_distinct);
  int fill_from_strs(const TableItem& table_item);
  int fill_from_strs(const ObLinkStmt& sub_stmt);
  int fill_from_strs(const ObLinkStmt& sub_stmt, const common::ObString& alias_name);
  int fill_from_strs(ObJoinType join_type, const ObRawExprIArray& join_conditions, const ObRawExprIArray& join_filters,
      const ObRawExprIArray& pushdown_filters);
  int fill_where_strs(const ObRawExprIArray& exprs, bool skip_nl_param = false);
  int fill_where_rownum(const ObRawExpr* expr);
  int fill_groupby_strs(const ObRawExprIArray& exprs);
  int fill_having_strs(const ObRawExprIArray& exprs);
  int fill_orderby_strs(const ObOrderItemIArray& order_items, const ObRawExprIArray& output_exprs);
  int32_t get_total_size() const;

G
gm 已提交
813
private:
O
oceanbase-admin 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
  int fill_exprs(
      const ObRawExprIArray& exprs, const common::ObString& sep, ObStringList& strs, bool skip_nl_param = false);
  int fill_exprs(const ObRawExprIArray& exprs, const common::ObString& sep, ObStringList& strs, ObStringListIter& iter,
      bool skip_nl_param = false);
  int fill_expr(const ObRawExpr* expr, const common::ObString& sep, ObStringList& strs);
  int fill_expr(const ObRawExpr* expr, const common::ObString& sep, ObStringList& strs, ObStringListIter& iter);
  int32_t get_total_size(const ObStringList& str_list, const common::ObString& clause) const;
  int32_t get_total_size(const ObStringIArray& str_array, const common::ObString& clause) const;
  int32_t get_total_size(const common::ObString& str, const common::ObString& clause) const;
  int merge_string_list(
      const ObStringList& str_array, const common::ObString& clause, char* buf, int32_t buf_len, int32_t& pos) const;
  int append_string(
      const common::ObString str, const common::ObString& clause, char* buf, int32_t buf_len, int32_t& pos) const;
  int append_string(const common::ObString str, char* buf, int32_t buf_len, int32_t& pos) const;
  const common::ObString& join_type_str(ObJoinType join_type) const;
  const common::ObString& set_type_str(ObSelectStmt::SetOperator set_type) const;
  const common::ObString& from_clause_str() const;
  int64_t get_order_item_index(const ObRawExpr* order_item_expr, const ObRawExprIArray& output_exprs);
  int expr_in_nl_param(ObRawExpr* expr, bool& in_nl_param);
  int do_expr_in_nl_param(ObRawExpr* expr, bool& in_nl_param);
  int get_nl_param_columns(ObRawExpr* param_expr, ObRawExprIArray& access_exprs, ObRawExprIArray& param_columns);

G
gm 已提交
836
public:
O
oceanbase-admin 已提交
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
  GenLinkStmtContext* link_ctx_;
  common::ObIAllocator& alloc_;
  const ObRawExprIArray& root_output_exprs_;
  ObStringList select_strs_;
  // now we only consider join / table scan for form_strs,
  // the algorithm is described in add_from_str(),
  // you can see why we use ObDList for from_strs, not ObArray.
  ObStringList from_strs_;
  // where_strs stores table scan range condition and filters only,
  // the join conditions and filters are stored in from_strs.
  ObStringList where_strs_;
  ObStringList groupby_strs_;
  ObStringList having_strs_;
  ObStringList orderby_strs_;
  common::ObString limit_str_;
  common::ObString offset_str_;
  char* tmp_buf_;
  int32_t tmp_buf_len_;
  bool is_inited_;
  bool is_distinct_;

G
gm 已提交
858
public:
O
oceanbase-admin 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
  static const common::ObString TABLE_BLANK_;
  static const common::ObString JOIN_ON_;
  static const common::ObString LEFT_BRACKET_;
  static const common::ObString RIGHT_BRACKET_;
  static const common::ObString SEP_DOT_;
  static const common::ObString SEP_COMMA_;
  static const common::ObString SEP_AND_;
  static const common::ObString SEP_SPACE_;
  static const common::ObString UNION_ALL_;
  static const common::ObString ORDER_ASC_;
  static const common::ObString ORDER_DESC_;
  static const common::ObString ROWNUM_;
  static const common::ObString LESS_EQUAL_;
  static const common::ObString SELECT_CLAUSE_;
  static const common::ObString SELECT_DIS_CLAUSE_;
  static const common::ObString FROM_CLAUSE_;
  static const common::ObString WHERE_CLAUSE_;
  static const common::ObString GROUPBY_CLAUSE_;
  static const common::ObString HAVING_CLAUSE_;
  static const common::ObString ORDERBY_CLAUSE_;
  static const common::ObString LIMIT_CLAUSE_;
  static const common::ObString OFFSET_CLAUSE_;
  static const common::ObString NULL_STR_;
};

class GenLinkStmtContext {
G
gm 已提交
885
public:
O
oceanbase-admin 已提交
886 887 888 889 890 891 892 893 894 895 896
  GenLinkStmtContext() : dblink_id_(OB_INVALID_ID), link_stmt_(NULL), nl_param_idxs_()
  {}
  uint64_t dblink_id_;
  ObLinkStmt* link_stmt_;
  // param_store index for nl_params.
  ObArray<int64_t> nl_param_idxs_;
};

class Path;
class ObLogPlan;
class ObLogicalOperator {
G
gm 已提交
897
public:
O
oceanbase-admin 已提交
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 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 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 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 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
  friend class ObLogExprValues;
  friend class ObLogFunctionTable;
  typedef common::ObBitSet<common::OB_MAX_BITSET_SIZE> PPDeps;
  /**
   *  Child operator related constant definition
   */
  static const int64_t first_child = 0;
  static const int64_t second_child = 1;
  static const int64_t third_child = 2;
  static const int64_t fourth_child = 3;
  static const int64_t TOPN_LIMIT_COUNT = 30000;
  static constexpr double TOPN_ROWS_RATIO = .2;
  static const int64_t SEQUENTIAL_EXECUTION_THRESHOLD = 1000;

  ObLogicalOperator(ObLogPlan& plan);
  virtual ~ObLogicalOperator();

  /**
   *  Get operator type
   */
  inline log_op_def::ObLogOpType get_type() const
  {
    return type_;
  }

  /**
   *  Set operator type
   */
  inline void set_type(log_op_def::ObLogOpType type)
  {
    type_ = type;
  }

  /**
   *  Get the number of child operators
   */
  inline int64_t get_num_of_child() const
  {
    return child_.count();
  }
  inline int64_t get_child_id() const
  {
    return child_id_;
  }
  inline void set_child_id(const int64_t idx)
  {
    child_id_ = idx;
  }
  /**
   *  Attache to a logical plan tree
   */
  inline void set_plan(ObLogPlan& plan)
  {
    my_plan_ = &plan;
  }
  /**
   *  Attache to a logical plan tree
   */
  inline void set_stmt(ObDMLStmt* stmt)
  {
    stmt_ = stmt;
  }

  /**
   *  Get the logical plan tree
   */
  virtual inline ObLogPlan* get_plan()
  {
    return my_plan_;
  }
  virtual inline const ObLogPlan* get_plan() const
  {
    return my_plan_;
  }

  /**
   *  Get the logical plan tree
   */
  inline ObDMLStmt* get_stmt()
  {
    return stmt_;
  }
  inline const ObDMLStmt* get_stmt() const
  {
    return stmt_;
  }
  /**
   *  Set my parent operator
   */
  inline ObLogicalOperator* get_parent()
  {
    return parent_;
  }

  int get_parent(ObLogicalOperator* root, ObLogicalOperator*& parent);

  virtual int append_not_produced_exprs(ObRawExprUniqueSet& not_produced_exprs) const;
  virtual int inner_append_not_produced_exprs(ObRawExprUniqueSet& not_produced_exprs) const;

  void set_parent(ObLogicalOperator* parent);

  /*
   * set parent based on child relationship
   */
  int adjust_parent_child_relationship();
  /**
   *  Get the cardinality
   */
  inline double get_card() const
  {
    return card_;
  }

  /**
   *  Set the cardinality
   */
  inline void set_card(double card)
  {
    card_ = card;
  }

  inline double get_width() const
  {
    return width_;
  }
  void set_width(double width)
  {
    width_ = width;
  }

  inline int64_t get_plan_depth() const
  {
    return plan_depth_;
  }
  void set_plan_depth(int64_t plan_depth)
  {
    plan_depth_ = plan_depth;
  }

  PxOpSizeFactor get_px_est_size_factor() const
  {
    return px_est_size_factor_;
  }
  PxOpSizeFactor& get_px_est_size_factor()
  {
    return px_est_size_factor_;
  }

  inline ObEstSelInfo* get_est_sel_info()
  {
    return est_sel_info_;
  }

  virtual int re_est_cost(const ObLogicalOperator* parent, double need_row_count, bool& re_est)
  {
    re_est = false;
    UNUSED(parent);
    UNUSED(need_row_count);
    return OB_SUCCESS;
  }

  /**
   *  Get the output expressions
   */
  inline common::ObIArray<ObRawExpr*>& get_output_exprs()
  {
    return output_exprs_;
  }
  inline const common::ObIArray<ObRawExpr*>& get_output_exprs() const
  {
    return output_exprs_;
  }

  /**
   *  Get the filter expressions
   */
  inline common::ObIArray<ObRawExpr*>& get_filter_exprs()
  {
    return filter_exprs_;
  }
  inline const common::ObIArray<ObRawExpr*>& get_filter_exprs() const
  {
    return filter_exprs_;
  }

  /**
   *  Get the pushdown filter expressions
   */
  inline common::ObIArray<ObRawExpr*>& get_pushdown_filter_exprs()
  {
    return pushdown_filter_exprs_;
  }
  inline const common::ObIArray<ObRawExpr*>& get_pushdown_filter_exprs() const
  {
    return pushdown_filter_exprs_;
  }

  /**
   *  Get the filter expressions
   */
  inline common::ObIArray<ObRawExpr*>& get_startup_exprs()
  {
    return startup_exprs_;
  }
  inline const common::ObIArray<ObRawExpr*>& get_startup_exprs() const
  {
    return startup_exprs_;
  }
Z
zl0 已提交
1106 1107 1108 1109
  inline int add_startup_exprs(const common::ObIArray<ObRawExpr *> &exprs)
  {
    return append(startup_exprs_, exprs);
  }
O
oceanbase-admin 已提交
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 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 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 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 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 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 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 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 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 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 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793

  /**
   *  Get a specified child operator
   */
  inline ObLogicalOperator* get_child(const int64_t index) const
  {
    return OB_LIKELY(index >= 0 && index < child_.count()) ? child_.at(index) : NULL;
  }

  /**
   *  Set a specified child operator
   */
  void set_child(const int64_t idx, ObLogicalOperator* child_op);

  /**
   *  Add a child operator at the next available position
   */
  int add_child(ObLogicalOperator* child_op);

  /**
   *  Set the first child operator
   */
  void set_left_child(ObLogicalOperator* child_op)
  {
    if (NULL != child_op) {
      child_op->child_id_ = 0;
    }
    set_child(first_child, child_op);
  }

  /**
   *  Set the second child operator
   */
  void set_right_child(ObLogicalOperator* child_op)
  {
    if (NULL != child_op) {
      child_op->child_id_ = 1;
    }
    set_child(second_child, child_op);
  }

  int reset_number_of_parent();
  int aggregate_number_of_parent();
  int need_copy_plan_tree(bool& need_copy);

  /**
   *  Get the optimization cost up to this point
   */
  inline const double& get_cost() const
  {
    return cost_;
  }

  inline double& get_cost()
  {
    return cost_;
  }

  /*
   * get optimization cost for current operator
   */
  inline const double& get_op_cost() const
  {
    return op_cost_;
  }

  inline double& get_op_cost()
  {
    return op_cost_;
  }

  /**
   *  Set the optimization cost
   */
  inline void set_cost(double cost)
  {
    cost_ = cost;
  }

  inline void set_op_cost(double op_cost)
  {
    op_cost_ = op_cost;
  }

  inline void set_branch_id(uint64_t branch_id)
  {
    branch_id_ = branch_id;
  }

  inline void set_id(uint64_t id)
  {
    id_ = id;
  }

  inline uint64_t get_op_id()
  {
    return op_id_;
  }
  inline void set_op_id(uint64_t op_id)
  {
    op_id_ = op_id;
  }
  inline bool is_partition_wise()
  {
    return is_partition_wise_;
  }
  inline void set_is_partition_wise(bool is_partition_wise)
  {
    is_partition_wise_ = is_partition_wise;
  }
  inline bool get_is_block_gi_allowed()
  {
    return is_block_gi_allowed_;
  }
  inline void set_is_block_gi_allowed(bool is_block_gi_allowed)
  {
    is_block_gi_allowed_ = is_block_gi_allowed;
  }

  /*
   * get cur_op's output ordering
   */
  inline common::ObIArray<OrderItem>& get_op_ordering()
  {
    return op_ordering_;
  }

  inline const common::ObIArray<OrderItem>& get_op_ordering() const
  {
    return op_ordering_;
  }

  inline const ObFdItemSet& get_fd_item_set() const
  {
    return NULL == fd_item_set_ ? empty_fd_item_set_ : *fd_item_set_;
  }

  int check_order_unique(bool& order_unique) const;

  void set_est_sel_info(ObEstSelInfo* sel_info)
  {
    est_sel_info_ = sel_info;
  }

  void set_fd_item_set(const ObFdItemSet* other)
  {
    fd_item_set_ = other;
  }

  void set_table_set(const ObRelIds* table_set)
  {
    table_set_ = table_set;
  }

  inline const ObRelIds& get_table_set() const
  {
    return NULL == table_set_ ? empty_table_set_ : *table_set_;
  }

  int set_op_ordering(const common::ObIArray<OrderItem>& op_ordering);

  void reset_op_ordering()
  {
    op_ordering_.reset();
  }

  inline common::ObIArray<OrderItem>& get_local_ordering()
  {
    return op_local_ordering_;
  }

  int set_local_ordering(const common::ObIArray<OrderItem>& op_local_ordering);

  void reset_local_ordering()
  {
    op_local_ordering_.reset();
  }

  int get_ordering_input_equal_sets(EqualSets& ordering_in_eset) const;

  int get_sharding_input_equal_sets(EqualSets& sharding_in_eset) const;

  int get_input_const_exprs(common::ObIArray<ObRawExpr*>& const_exprs) const;

  inline void set_sharding_output_equal_sets(const EqualSets* equal_sets)
  {
    sharding_output_equal_sets_ = equal_sets;
  }
  inline void set_ordering_output_equal_sets(const EqualSets* equal_sets)
  {
    ordering_output_equal_sets_ = equal_sets;
  }
  inline const EqualSets& get_ordering_output_equal_sets() const
  {
    return NULL == ordering_output_equal_sets_ ? empty_expr_sets_ : *ordering_output_equal_sets_;
  }
  inline const EqualSets& get_sharding_output_equal_sets() const
  {
    return NULL == sharding_output_equal_sets_ ? get_ordering_output_equal_sets() : *sharding_output_equal_sets_;
  }
  inline common::ObIArray<ObRawExpr*>& get_output_const_exprs()
  {
    return const_exprs_;
  }
  inline const common::ObIArray<ObRawExpr*>& get_output_const_exprs() const
  {
    return const_exprs_;
  }
  /*
   * get cur_op's expected_ordering
   */
  inline common::ObIArray<OrderItem>& get_expected_ordering()
  {
    return expected_ordering_;
  }

  int set_expected_ordering(const common::ObIArray<OrderItem>& expected_ordering);

  inline void set_is_at_most_one_row(bool is_at_most_one_row)
  {
    is_at_most_one_row_ = is_at_most_one_row;
  }
  inline bool get_is_at_most_one_row()
  {
    return is_at_most_one_row_;
  }
  inline bool get_is_at_most_one_row() const
  {
    return is_at_most_one_row_;
  }

  void* get_traverse_ctx() const
  {
    return traverse_ctx_;
  }
  void set_traverse_ctx(void* ctx)
  {
    traverse_ctx_ = ctx;
  }

  // clear plan tree's traverse ctx
  void clear_all_traverse_ctx();
  template <typename Allocator>
  int init_all_traverse_ctx(Allocator& alloc);

  inline int64_t get_interesting_order_info() const
  {
    return interesting_order_info_;
  }
  inline void set_interesting_order_info(int64_t info)
  {
    interesting_order_info_ = info;
  }
  inline bool has_any_interesting_order_info_flag() const
  {
    return interesting_order_info_ > 0;
  }
  /*
   * re set op's ordering according child's ordering
   */
  virtual int transmit_op_ordering();

  /**
   * set whether op is local order
   */
  virtual int transmit_local_ordering();

  /**
   *  Copy an operator without copying its child
   */
  virtual int copy_without_child(ObLogicalOperator*& out) = 0;

  int clone(ObLogicalOperator*& other);

  /**
   *  Set a series of properties for the operator
   */
  virtual int set_properties()
  {
    return common::OB_SUCCESS;
  }

  /**
   *  Do explain collect width operation(pre)
   */
  int explain_collect_width_pre(void* ctx);

  /**
   *  Do explain collect width operation(post)
   */
  int explain_collect_width_post(void* ctx);

  /**
   *  Do explain write buffer operation(pre)
   */
  int explain_write_buffer_pre(void* ctx);

  int explain_index_selection_info_pre(void* ctx);

  /**
   *  Do explain write buffer operation(pre)
   */
  int explain_write_buffer_post(void* ctx);

  /**
   *  Do explain write buffer with output & filter exprs
   */
  int explain_write_buffer_output_pre(void* ctx);

  /**
   *  Do explain write buffer with outline
   */
  int explain_write_buffer_outline_pre(void* ctx);
  void reset_outline_state()
  {
    is_added_outline_ = false;
  }

  /**
   *  Do pre-child-traverse operation
   */
  int do_pre_traverse_operation(const TraverseOp& op, void* ctx);

  /**
   *  Do post-child-traverse operation
   */
  int do_post_traverse_operation(const TraverseOp& op, void* ctx);

  /*
   *  set topn
   */
  int set_sort_topn();
  /**
   *  Rerturn a JSON object of the operator
   *
   *  'buf' is used when calling 'to_string()' internally.
   */
  virtual int to_json(char* buf, const int64_t buf_len, int64_t& pos, json::Value*& ret_val);

  /**
   *  Get predefined operator name
   */
  virtual const char* get_name() const
  {
    return log_op_def::get_op_name(type_);
  }

  /**
   * Get the length of the operator name
   */
  inline virtual int32_t get_explain_name_length() const
  {
    return ((int32_t)strlen(get_name()));
  }

  virtual int get_explain_name_internal(char* buf, const int64_t buf_len, int64_t& pos)
  {
    int ret = common::OB_SUCCESS;
    ret = BUF_PRINTF("%s", get_name());
    return ret;
  }

  virtual int64_t to_string(char* buf, const int64_t buf_len) const
  {
    UNUSED(buf);
    UNUSED(buf_len);
    return common::OB_SUCCESS;
  }

  virtual int explain_index_selection_info(char* buf, int64_t& buf_len, int64_t& pos)
  {
    int ret = common::OB_SUCCESS;
    UNUSED(buf);
    UNUSED(buf_len);
    UNUSED(pos);
    return ret;
  }

  /**
   *  Allocate output expr pre-traverse
   */
  virtual int allocate_expr_pre(ObAllocExprContext& ctx);

  virtual int reordering_project_columns();

  int get_next_producer_id(ObLogicalOperator* node, uint64_t& producer_id);

  int check_param_expr_should_be_added(const ObRawExpr* param_expr, bool& should_add);

  int add_exprs_to_ctx(ObAllocExprContext& ctx, const ObIArray<ObRawExpr*>& exprs);

  int add_exprs_to_ctx(ObAllocExprContext& ctx, const ObIArray<ObRawExpr*>& exprs, uint64_t producer_id);

  int add_exprs_to_ctx(
      ObAllocExprContext& ctx, const ObIArray<ObRawExpr*>& exprs, uint64_t producer_id, uint64_t consumer_id);

  int add_exprs_to_ctx(ObAllocExprContext& ctx, const ObIArray<ObColumnRefRawExpr*>& exprs);

  int extract_specific_exprs(const ObIArray<ObRawExpr*>& exprs, ObIArray<ExprProducer>* ctx,
      ObIArray<ObRawExpr*>& fix_producer_exprs, ObIArray<ObRawExpr*>& shard_exprs);
  int extract_specific_exprs(ObRawExpr* expr, ObIArray<ExprProducer>* ctx, ObIArray<ObRawExpr*>& fix_producer_exprs,
      ObIArray<ObRawExpr*>& shard_exprs);
  bool is_fix_producer_expr(const ObRawExpr& expr);

  bool is_shared_expr(const ObIArray<ExprProducer>* ctx, const ObRawExpr* expr);

  int find_consumer_id_for_shared_expr(const ObIArray<ExprProducer>* ctx, const ObRawExpr* expr, uint64_t& consumer_id);
  /**
   *  Allocate output expr post-traverse
   *
   *  The default implementation will iterate over the context to see if there are
   *  more exprs that can be produced - ie. the exprs that they depend on have been
   *  produced. An expression will be categorized into filter, join predicates or
   *  output expressions and placed at proper location once it is produced.
   */
  virtual int allocate_expr_post(ObAllocExprContext& ctx);

  int expr_can_be_produced(const ObRawExpr* expr, ObAllocExprContext& gen_expr_ctx, bool& can_be_produced);

  int expr_has_been_produced(const ObRawExpr* expr, ObAllocExprContext& gen_expr_ctx, bool& has_been_produced);

  virtual int compute_const_exprs();

  virtual int compute_equal_set();

  virtual int compute_fd_item_set();

  virtual int deduce_const_exprs_and_ft_item_set(ObFdItemSet& fd_item_set);

  virtual int compute_op_ordering();

  virtual int compute_op_interesting_order_info();

  virtual int compute_table_set();

  virtual int compute_one_row_info();

  virtual int init_est_sel_info();

  virtual int est_cost()
  {
    return OB_SUCCESS;
  }

  /**
   * @brief compute_property
   * convert property fields from a path into a logical operator
   */
  virtual int compute_property(Path* path);

  /**
   * @brief compute_property
   * compute properties of an operator.
   * called by non-terminal operator
   * 1. ordering, 2. unique sets, 3. equal_sets
   * 4. est_sel_info, 5. cost, card, width
   */
  virtual int compute_property();

  bool share_property() const
  {
    return type_ != log_op_def::LOG_EXCHANGE && type_ != log_op_def::LOG_GRANULE_ITERATOR &&
           type_ != log_op_def::LOG_MONITORING_DUMP && type_ != log_op_def::LOG_SORT &&
           type_ != log_op_def::LOG_SUBPLAN_FILTER && type_ != log_op_def::LOG_MATERIAL &&
           type_ != log_op_def::LOG_JOIN_FILTER;
  }

  /*
   * re_calc_cost: children's cost + op_cost
   */
  virtual int re_calc_cost();

  /*
   *  move useless sort operator, set merge sort,task order,prefix sort
   */
  int adjust_sort_operator(AdjustSortContext* adjust_sort_ctx);
  /*
   * set prefix sort, merge, task order
   */
  int inner_adjust_sort_operator(AdjustSortContext* adjust_sort_ctx, bool& need_remove);
  /*
   * remove log sort(current node)
   */
  int remove_sort();

  /**
   *  Add an array of exprs into context
   *
   *  This helper function add all the expressions into an array of ExprProducer
   *  (should be moved to some util files)
   */
  int push_down_limit(AllocExchContext* ctx, ObRawExpr* limit_count_expr, ObRawExpr* limit_offset_expr,
      bool should_push_limit, bool is_fetch_with_ties, ObLogicalOperator*& exchange_point);
  /**
   *  Allocate a pair of exchange nodes above this operator
   */
  int allocate_exchange_nodes_above(bool is_remote, AllocExchContext& ctx, ObExchangeInfo& exch_info);
  /**
   *  Pre-traverse function for allocating granule iterator
   */
  virtual int allocate_granule_pre(AllocGIContext& ctx);
  /**
   *	Post-traverse function for allocating granule iterator
   */
  virtual int allocate_granule_post(AllocGIContext& ctx);
  /**
   *	Allocate granule iterator above this operator
   */
  virtual int allocate_granule_nodes_above(AllocGIContext& ctx);
  /**
   *	Set granule iterator affinity
   */
  virtual int set_granule_nodes_affinity(AllocGIContext& ctx, int64_t child_index);
  /*
   * Allocate gi above all the table scans below this operator.
   */
  int allocate_gi_recursively(AllocGIContext& ctx);
  /**
   * Allocate m dump operator.
   */
  int allocate_monitoring_dump_node_above(uint64_t flags, uint64_t line_id);

  int gen_location_constraint(void* ctx);

  /**
   * Generate a table's location constraint for pdml index maintain op
   */
  int get_tbl_loc_cons_for_pdml_index(LocationConstraint& loc_cons);
  /**
   * Generate a table's location constraint for table scan op
   */
  int get_tbl_loc_cons_for_scan(LocationConstraint& loc_cons);

  /**
   * @brief Generate a table's location constraint for insert op
   */
  int get_tbl_loc_cons_for_insert(LocationConstraint& loc_cons, bool& is_multi_part_dml);

  /**
   *  Allocate a pair of exchange nodes between this operator and the index'th
   *  child
   */
  int allocate_exchange_nodes_below(int64_t index, AllocExchContext& ctx, ObExchangeInfo& exch_info);

  int allocate_grouping_style_exchange_below(AllocExchContext* ctx, common::ObIArray<ObRawExpr*>& partition_exprs);

  int allocate_stmt_order_by_above(ObLogicalOperator*& top);

  int simplify_ordered_exprs(const ObIArray<OrderItem>& candi_sort_key, ObIArray<OrderItem>& simplified_sort_key);

  int simplify_ordered_exprs(const ObIArray<ObRawExpr*>& candi_exprs, ObIArray<ObRawExpr*>& simplified_exprs);

  int simplify_exprs(const ObIArray<ObRawExpr*>& candi_exprs, ObIArray<ObRawExpr*>& simplified_exprs) const;

  int check_need_sort_above_node(const ObIArray<OrderItem>& expected_order_items, bool& need_sort) const;
  int check_need_sort_above_node(const common::ObIArray<ObRawExpr*>& expected_order_exprs,
      const common::ObIArray<ObOrderDirection>* expected_order_directions, bool& need_sort) const;
  int check_need_sort_below_node(
      const int64_t index, const common::ObIArray<OrderItem>& expected_order_items, bool& need);

  // check whether we need allocate sort, mainly for group/distinct clause
  int check_need_sort_for_grouping_op(
      const int64_t index, const common::ObIArray<OrderItem>& order_items, bool& need_sort);
  int check_need_sort_for_grouping_op(const int64_t index, const common::ObIArray<ObRawExpr*>& order_expr,
      const ObIArray<ObOrderDirection>& directions, bool& need_sort);

  int check_if_is_prefix(const common::ObIArray<ObRawExpr*>* order_exprs,
      const common::ObIArray<OrderItem>* order_items, ObLogicalOperator* op, bool& is_prefix);
  // whether operator keep input(child at idx) ordering
  //@param[out]: keep which idx child's ordering.
  // and it's special for granule iterator
  bool is_keep_input_ordering();

  int get_table_scan(ObLogicalOperator*& tsc, uint64_t table_id);

  int get_order_item_by_plan(ObIArray<OrderItem>& order_items);

  // get operator ordering with multi-partitions
  // If no parts ordering, return NULL. This is used for check whether sort needed.
  static const common::ObIArray<OrderItem>* get_op_ordering_in_parts(ObLogicalOperator* op);

  int check_and_allocate_material(const int64_t index);

  /**
   * Check if a exchange need rescan is below me.
   */
  int check_exchange_rescan(bool& need_rescan);

  /**
   * Check if has exchange below.
   */
  int check_has_exchange_below(bool& has_exchange) const;
  /**
   *  Post-traverse function for allocating output expressions
   */
  virtual int allocate_exchange_post(AllocExchContext* ctx) = 0;
  int reselect_duplicate_table_replica();
  virtual int get_duplicate_table_replica(ObIArray<ObAddr>& valid_addrs);
  int set_duplicate_table_replica(const ObIArray<ObAddr>& addrs);
  virtual int set_duplicate_table_replica(const ObAddr& addr);
  int should_use_sequential_execution(bool& use_seq_exec) const;

  int check_if_match_repart(const EqualSets& equal_sets, const common::ObIArray<ObRawExpr*>& src_join_key,
      const common::ObIArray<ObRawExpr*>& target_join_key, const ObLogicalOperator& target_child, bool& is_match);
  int check_is_table_scan(const ObLogicalOperator& op, bool& is_table_scan);

  int get_repartition_table_info(
      ObLogicalOperator& op, ObString& table_name, uint64_t& table_id, uint64_t& ref_table_id);

  int compute_sharding_and_allocate_exchange(AllocExchContext* ctx, const EqualSets& equal_sets,
      const ObIArray<ObRawExpr*>& hash_left_keys, const ObIArray<ObRawExpr*>& hash_right_keys,
      const ObIArray<ObExprCalcType>& hash_calc_types, const ObIArray<ObRawExpr*>& left_keys,
      const ObIArray<ObRawExpr*>& right_keys, ObLogicalOperator& left_child, ObLogicalOperator& right_child,
      const JoinDistAlgo best_method, const SlaveMappingType slave_mapping_type, const ObJoinType join_type,
      ObShardingInfo& sharding_info);

  int update_sharding_conds(ObIArray<ObRawExpr*>& sharding_conds, const JoinDistAlgo best_method,
      const ObJoinType join_type, const ObExchangeInfo& left_exch_info, const ObExchangeInfo& right_exch_info);

  int compute_repartition_distribution_info(const EqualSets& equal_sets, const ObIArray<ObRawExpr*>& src_keys,
      const ObIArray<ObRawExpr*>& target_keys, ObLogicalOperator& target_child, ObExchangeInfo& exch_info,
      ObShardingInfo& sharding_info);

  int compute_hash_distribution_info(const ObIArray<ObRawExpr*>& left_keys, const ObIArray<ObRawExpr*>& right_keys,
      const ObIArray<ObExprCalcType>& calc_types, ObExchangeInfo& l_exch_info, ObExchangeInfo& r_exch_info,
      ObShardingInfo& sharding_info);

  int compute_repartition_func_info(const EqualSets& equal_sets, const common::ObIArray<ObRawExpr*>& src_join_key,
      const common::ObIArray<ObRawExpr*>& target_join_key, const ObShardingInfo& target_sharding,
      ObRawExprFactory& expr_factory, ObExchangeInfo& exch_info);

  int get_repartition_keys(const EqualSets& equal_sets, const common::ObIArray<ObRawExpr*>& src_keys,
      const common::ObIArray<ObRawExpr*>& target_keys, const common::ObIArray<ObRawExpr*>& target_part_keys,
      common::ObIArray<ObRawExpr*>& src_part_keys);

  int choose_best_distribution_method(
      AllocExchContext& ctx, uint64_t method, bool sm_hint, JoinDistAlgo& best_method, SlaveMappingType& sm_type) const;
  int should_use_slave_mapping(bool& enable_sm) const;
  int get_hash_hash_cost(double& cost) const;
  int get_broadcast_cost(int64_t child_idx, AllocExchContext& ctx, double& cost) const;
  int get_repartition_cost(int64_t child_idx, double& cost) const;
  int get_b2host_cost(int64_t child_idx, AllocExchContext& ctx, double& cost) const;

  virtual int allocate_exchange(AllocExchContext* ctx, ObExchangeInfo& exch_info);

  // Operator is block operator if outputting data after all children's data processed.
  //   block operator: group by, sort, material ...
  //   non block operator: limit, join, ...
  virtual bool is_block_op() const
  {
    return false;
  }
  // Non block operator may start outputting data after some children's data processed,
  // is_block_input() indicate those children.
  // e.g.: first child of hash join is block input, second child is not block input.
  virtual bool is_block_input(const int64_t child_idx) const
  {
    UNUSED(child_idx);
    return is_block_op();
  }

  virtual bool is_consume_child_1by1() const
  {
    return false;
  }

  // PX pipe blocking phase add material operator to DFO, to make sure DFO can be scheduled
  // in consumer/producer threads model. top-down stage markup the output side has block
  // operator or not. bottom-up stage markup the input side and add material operator in the
  // following case:
  //   1. operator got more then one input with out block operator.
  //   2. no block operator between two DFO.
  //
  // Notice: We do not mark block operator directly in traverse context, we marking whether
  // data can streaming to an exchange operator instead.
  virtual int px_pipe_blocking_pre(ObPxPipeBlockingCtx& ctx);
  virtual int px_pipe_blocking_post(ObPxPipeBlockingCtx& ctx);

  // generate local order plan for merge sort receive operator, but for top exchange, exchang-in is exchange for coord
  // so it need to forbit generating local order plan
  // pre function to tag top exchange flag and post function to remove tag top exchange flag
  virtual int set_exchange_cnt_pre(AdjustSortContext* ctx);
  virtual int set_exchange_cnt_post(AdjustSortContext* ctx);
  virtual int allocate_link_post();
Z
zl0 已提交
1794 1795
  virtual int allocate_startup_expr_post();
  int allocate_startup_expr_post(int64_t child_idx);
O
oceanbase-admin 已提交
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 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 1897
  int allocate_link_node_above(int64_t child_idx);

  virtual int generate_link_sql_pre(GenLinkStmtContext& link_ctx);

  /**
   *  Start plan tree traverse
   *
   *  For each traverse operation, we provide a opportunity of operation both
   *  before and after accessing the children. This should be sufficient to
   *  support both top-down and bottom-up traversal.
   */
  int do_plan_tree_traverse(const TraverseOp& operation, void* ctx);

  int should_allocate_gi_for_dml(bool& is_valid);
  /**
   *  Get the operator id
   */
  uint64_t get_operator_id() const
  {
    return id_;
  }

  /**
   *  Mark this operator as the root of the plan
   */
  void mark_is_plan_root()
  {
    is_plan_root_ = true;
  }

  void set_is_plan_root(bool is_root)
  {
    is_plan_root_ = is_root;
  }

  /**
   *  Check if this operator is the root of a logical plan
   */
  bool is_plan_root() const
  {
    return is_plan_root_;
  }

  /**
   *  Mark an expr as 'produced'
   */
  int mark_expr_produced(
      ObRawExpr* expr, uint64_t branch_id, uint64_t producer_id, ObAllocExprContext& gen_expr_ctx, bool& found);
  /**
   *  Get the operator's hash value
   */
  virtual uint64_t hash(uint64_t seed) const;
  /**
   *  Generate hash value for output exprs
   */
  inline uint64_t hash_output_exprs(uint64_t seed) const
  {
    return ObOptimizerUtil::hash_exprs(seed, output_exprs_);
  }

  /**
   *  Generate hash value for filter exprs
   */
  inline uint64_t hash_filter_exprs(uint64_t seed) const
  {
    return ObOptimizerUtil::hash_exprs(seed, filter_exprs_);
  }

  inline uint64_t hash_pushdown_filter_exprs(uint64_t seed) const
  {
    return ObOptimizerUtil::hash_exprs(seed, pushdown_filter_exprs_);
  }

  inline uint64_t hash_startup_exprs(uint64_t seed) const
  {
    return ObOptimizerUtil::hash_exprs(seed, startup_exprs_);
  }

  /**
   *  Add an expr to output_exprs_
   */
  int add_expr_to_output(const ObRawExpr* expr);

  /**
   *  Add all select expr to the output of the root operator
   */
  int add_plan_root_exprs(ObAllocExprContext& ctx);
  /**
   *  Get operator's sharding information
   */
  ObShardingInfo& get_sharding_info()
  {
    return sharding_info_;
  }
  const ObShardingInfo& get_sharding_info() const
  {
    return sharding_info_;
  }

  bool is_dml_operator() const
  {
    return (log_op_def::LOG_UPDATE == type_ || log_op_def::LOG_DELETE == type_ || log_op_def::LOG_INSERT == type_ ||
Z
zl0 已提交
1898
            log_op_def::LOG_INSERT_ALL == type_ || log_op_def::LOG_MERGE == type_);
O
oceanbase-admin 已提交
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
  }
  bool is_duplicated_checker_op() const
  {
    return log_op_def::LOG_CONFLICT_ROW_FETCHER == type_;
  }
  bool is_expr_operator() const
  {
    return (log_op_def::LOG_EXPR_VALUES == type_ || log_op_def::LOG_VALUES == type_);
  }
  /*
   * replace agg expr generated by group by push down during allocating exchange
   */
  int replace_generated_agg_expr(const common::ObIArray<std::pair<ObRawExpr*, ObRawExpr*> >& to_replace_exprs);
  // set merge_sort for exchange
  int set_merge_sort(ObLogicalOperator* op, AdjustSortContext* adjust_sort_ctx, bool& need_remove);

  // check if this operator is some kind of table scan.
  // the default return value is false
  // Note: The default table scan operator ObLogTableScan overrides this
  //       method to return true.
  //       IT'S RECOMMENDED THAT any table scan operator derive from ObLogTableScan,
  //       or you MAY NEED TO CHANGE EVERY CALLER of this method.
  virtual bool is_table_scan() const
  {
    return false;
  }

  int allocate_material(const int64_t index);
  bool is_scan_operator(log_op_def::ObLogOpType type);

  // Find the first operator through the child operators recursively
  // Found: return OB_SUCCESS, set %op
  // Not found: return OB_SUCCESS, set %op to NULL
  int find_first_recursive(const log_op_def::ObLogOpType type, ObLogicalOperator*& op);
  // the operator use these interfaces to allocate gi
  int pw_allocate_granule_pre(AllocGIContext& ctx);
  int pw_allocate_granule_post(AllocGIContext& ctx);

  int allocate_distinct_below(int64_t index, ObIArray<ObRawExpr*>& distinct_exprs, const AggregateAlgo algo);

  int allocate_sort_below(const int64_t index, const ObIArray<OrderItem>& order_keys);

  int allocate_topk_below(const int64_t index, ObSelectStmt* stmt);

  int allocate_material_below(const int64_t index);

  int allocate_limit_below(const int64_t index, ObRawExpr* limit_expr);

  int allocate_group_by_below(
      int64_t index, ObIArray<ObRawExpr*>& group_by_exprs, const AggregateAlgo algo, const bool from_pivot);

  int check_match_remote_sharding(const ObAddr& server, bool& is_match) const;

  static int check_match_server_addr(const ObPhyTableLocationInfo& phy_loc_info, const ObAddr& server, bool& is_match);

  int print_operator_for_use_join_filter(planText& plan_text);

  int compute_sharding_equal_sets(const ObIArray<ObRawExpr*>& sharding_conds);

  int prune_weak_part_exprs(
      const AllocExchContext& ctx, const ObIArray<ObRawExpr*>& input_exprs, ObIArray<ObRawExpr*>& output_exprs);

  int prune_weak_part_exprs(const AllocExchContext& ctx, const ObIArray<ObRawExpr*>& left_input_exprs,
      const ObIArray<ObRawExpr*>& right_input_exprs, ObIArray<ObRawExpr*>& left_output_exprs,
      ObIArray<ObRawExpr*>& right_output_exprs);

  bool check_weak_part_expr(const ObRawExpr* expr, const ObIArray<ObRawExpr*>& weak_part_exprs);

  int compute_basic_sharding_info(AllocExchContext* ctx, bool& is_basic);
  int compute_recursive_cte_sharding_info(AllocExchContext* ctx, ObLogicalOperator* op, bool& is_basic);
  int check_contain_fake_cte_table(ObLogicalOperator* child, bool& contain_rcte);
  static int compute_basic_sharding_info(
      ObIArray<ObShardingInfo*>& input_shardings, ObShardingInfo& output_sharding, bool& is_basic);
  int gen_calc_part_id_expr(uint64_t table_id, uint64_t ref_table_id, ObRawExpr*& expr);

  // dblink
  void set_dblink_id(uint64_t dblink_id)
  {
    dblink_id_ = dblink_id;
  }
  uint64_t get_dblink_id() const
  {
    return dblink_id_;
  }

  int check_is_uncertain_plan();

  int get_part_exprs(uint64_t table_id, uint64_t ref_table_id, share::schema::ObPartitionLevel& part_level,
      ObRawExpr*& part_expr, ObRawExpr*& subpart_expr);

  int check_fulfill_cut_ratio_condition(int64_t dop, double ndv, bool& is_fulfill);

G
gm 已提交
1991
public:
O
oceanbase-admin 已提交
1992 1993 1994 1995
  /* child operators */
  ObSEArray<ObLogicalOperator*, 16, common::ModulePageAllocator, true> child_;
  // ObLogicalOperator *child_[max_num_of_child];

G
gm 已提交
1996
protected:
O
oceanbase-admin 已提交
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
  enum TraverseType { JOIN_METHOD, MATERIAL_NL, LEADING, PQ_DIST, PQ_MAP };
  enum JoinTreeType { INVALID_TYPE, LEFT_DEEP, RIGHT_DEEP, BUSHY };

  /**
   *  Check if all expresionss are produced by some operator.
   */
  int all_expr_produced(common::ObIArray<ExprProducer>* ctx, bool& all_produced);

  /**
   *  Print the footer of the explain plan table
   */
  int print_plan_annotation(char* buf, int64_t& buf_len, int64_t& pos, ExplainType type);

  /**
   *  Interface for operators to print their specific info in the plan table footer
   */
  virtual int print_my_plan_annotation(char* buf, int64_t& buf_len, int64_t& pos, ExplainType type)
  {
    int ret = common::OB_SUCCESS;
    UNUSED(buf);
    UNUSED(buf_len);
    UNUSED(pos);
    UNUSED(type);
    return ret;
  }

  /**
   *  Interface for operators to print their specific info in the plan table footer
   */
  virtual int print_plan_head_annotation(char* buf, int64_t& buf_len, int64_t& pos, ExplainType type)
  {
    int ret = common::OB_SUCCESS;
    UNUSED(buf);
    UNUSED(buf_len);
    UNUSED(pos);
    UNUSED(type);
    return ret;
  }

  //    virtual int print_outline(char *buf, int64_t &buf_len, int64_t &pos, bool is_oneline)
  virtual int print_outline(planText& plan)
  {
    int ret = common::OB_SUCCESS;
    UNUSED(plan);
    return ret;
  }

  virtual int print_operator_for_outline(planText& plan)
  {
    int ret = common::OB_SUCCESS;
    UNUSED(plan);
    return ret;
  }

  virtual int is_used_join_type_hint(JoinAlgo join_algo, bool& is_used)
  {
    int ret = common::OB_SUCCESS;
    UNUSED(join_algo);
    UNUSED(is_used);
    return ret;
  }

  virtual int is_used_in_leading_hint(bool& is_used)
  {
    int ret = common::OB_SUCCESS;
    UNUSED(is_used);
    return ret;
  }

  int traverse_join_plan_ldr(planText& plan_text, TraverseType traverse_type, JoinTreeType join_tree_type);
  int traverse_join_plan_pre(
      planText& plan_text, TraverseType traverse_type, JoinTreeType join_tree_type, bool is_need_print = true);
  int print_qb_name(planText& plan_text);

  /**
   *  Allocate dummy access if the current output is empty
   */
  int allocate_dummy_access();

  /**
   *  Allocate dummy output if the current output is empty
   */
  virtual int allocate_dummy_output();

  /**
   *  Allocate dummy output and access if the current output is empty
   */
  int allocate_dummy_output_access();

  int project_pruning_pre();
  void project_pruning(common::ObIArray<ObRawExpr*>& exprs, PPDeps& deps, const char* reason = nullptr);

  int check_output_dep_common(ObRawExprCheckDep& checker);
  virtual int check_output_dep_specific(ObRawExprCheckDep& checker)
  {
    (void)(checker);
    return common::OB_SUCCESS;
  }
  virtual int check_output_dep(common::ObIArray<ObRawExpr*>& child_output, PPDeps& deps);

  virtual int inner_replace_generated_agg_expr(
      const common::ObIArray<std::pair<ObRawExpr*, ObRawExpr*> >& to_replace_exprs);
  int replace_exprs_action(const common::ObIArray<std::pair<ObRawExpr*, ObRawExpr*> >& to_replace_exprs,
      common::ObIArray<ObRawExpr*>& dest_exprs);
  int replace_expr_action(
      const common::ObIArray<std::pair<ObRawExpr*, ObRawExpr*> >& to_replace_exprs, ObRawExpr*& dest_expr);
  int make_order_keys(common::ObIArray<ObRawExpr*>& exprs, common::ObIArray<OrderItem>& items);

  static int explain_print_partitions(
      const common::ObIArray<int64_t>& partitions, const bool two_level, char* buf, int64_t& buf_len, int64_t& pos);

  int check_sharding_compatible_with_reduce_expr(const ObShardingInfo& sharding,
      const common::ObIArray<ObRawExpr*>& reduce_exprs, const EqualSets& equal_sets, bool& compatible) const;

  int get_percentage_value(ObRawExpr* percentage_expr, double& percentage, bool& is_null_value);

  int check_need_sort_for_local_order(const int64_t index, const common::ObIArray<OrderItem>* order_items, bool& need);

G
gm 已提交
2115
protected:
O
oceanbase-admin 已提交
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
  int alloc_partition_id_expr(uint64_t table_id, ObAllocExprContext& ctx, ObPseudoColumnRawExpr*& partition_id_expr);

  void add_join_dist_flag(uint64_t& flags, JoinDistAlgo method) const
  {
    flags |= method;
  }

  void remove_join_dist_flag(uint64_t& flags, JoinDistAlgo method) const
  {
    flags &= ~method;
  }

  bool has_join_dist_flag(uint64_t flags, JoinDistAlgo method) const
  {
    return !!(flags & method);
  }

  void clear_join_dist_flag(uint64_t& flags) const
  {
    flags = 0;
  }

G
gm 已提交
2138
protected:
O
oceanbase-admin 已提交
2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
  log_op_def::ObLogOpType type_;
  ObLogPlan* my_plan_;                                                                // the entry point of the plan
  common::ObSEArray<ObRawExpr*, 8, common::ModulePageAllocator, true> output_exprs_;  // expressions produced
  common::ObSEArray<ObRawExpr*, 8, common::ModulePageAllocator, true> filter_exprs_;  // filtering exprs.
  common::ObSEArray<ObRawExpr*, 8, common::ModulePageAllocator, true> pushdown_filter_exprs_;
  common::ObSEArray<ObRawExpr*, 8, common::ModulePageAllocator, true> startup_exprs_;  // startup filtering exprs.
  common::ObSEArray<ObRawExpr*, 16, common::ModulePageAllocator, true> const_exprs_;

  const EqualSets* ordering_output_equal_sets_;
  const EqualSets* sharding_output_equal_sets_;
  const ObFdItemSet* fd_item_set_;
  const ObRelIds* table_set_;
  ObEstSelInfo* est_sel_info_;

  ObDMLStmt* stmt_;
  uint64_t id_;  // operator 0-based depth-first id
  uint64_t branch_id_;
  uint64_t op_id_;

G
gm 已提交
2158
private:
O
oceanbase-admin 已提交
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 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
  /**
   *  Numbering the operator
   */
  int numbering_operator_pre(NumberingCtx& ctx);
  void numbering_operator_post(NumberingCtx& ctx);
  /**
   *  allocate md operator
   */
  int alloc_md_post(AllocMDContext& ctx);
  /**
   * Numbering px transmit op with dfo id
   * for Explain display
   */
  int numbering_exchange_pre(NumberingExchangeCtx& ctx);
  int numbering_exchange_post(NumberingExchangeCtx& ctx);
  /**
   * Estimate size level for different operator when px
   */
  int px_estimate_size_factor_pre();
  int px_estimate_size_factor_post();
  /**
   * for px rescan function
   */
  int px_rescan_pre();
  int mark_child_exchange_rescanable();
  int check_subplan_filter_child_exchange_rescanable();

  bool has_expr_as_input(const ObRawExpr* expr) const;

  /**
   *  Re-order the filter expressions according to their selectivity
   *
   *  Re-order the filter expression vector so that the more selective predicate will
   *  be evaluated earlier.
   */
  int reorder_filter_exprs();
  int is_need_print_join_method(const planText& plan_text, JoinAlgo join_algo, bool& is_need);
  int is_need_print_operator_for_leading(const planText& plan_text, bool& is_need);
  int print_operator_for_use_join(planText& plan_text);
  int print_operator_for_leading(planText& plan_text);
  int inner_set_merge_sort(ObLogicalOperator* producer, ObLogicalOperator* consumer, ObLogicalOperator* op_sort,
      bool& need_remove, bool global_order);

  // private function, just used for allocating join filter node.
  int check_allocate_bf_condition(bool& allocate);
  int set_join_condition_to_bf(ObLogicalOperator* filter, bool is_create);
  // get the table dop for this dfo
  int calc_current_dfo_table_dop(ObLogicalOperator* root, int64_t& table_dop, bool& found_base_table);

G
gm 已提交
2208
private:
O
oceanbase-admin 已提交
2209 2210 2211
  int64_t child_id_;           // parent child
  ObLogicalOperator* parent_;  // parent operator
  bool is_plan_root_;          // plan root operator
G
gm 已提交
2212
protected:
O
oceanbase-admin 已提交
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232
  double cost_;     // cost up to this point
  double op_cost_;  // cost for this operator
  double card_;     // cardinality
  double width_;    // average row width
  ObShardingInfo sharding_info_;
  bool is_added_outline_;  // used for outline
  bool is_added_leading_outline_;
  bool is_added_leading_hints_;
  common::ObSEArray<OrderItem, 8, common::ModulePageAllocator, true> expected_ordering_;
  // temporary traverse context pointer, maybe modified in every plan_tree_traverse
  void* traverse_ctx_;
  ObPwjConstraint strict_pwj_constraint_;
  ObPwjConstraint non_strict_pwj_constraint_;
  bool is_partition_wise_;
  bool is_block_gi_allowed_;
  PxOpSizeFactor px_est_size_factor_;
  // dblink
  uint64_t dblink_id_;
  int64_t plan_depth_;

G
gm 已提交
2233
private:
O
oceanbase-admin 已提交
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
  // unify to use get, set and reset function instead of using op_ordering_
  bool is_at_most_one_row_;
  common::ObSEArray<OrderItem, 8, common::ModulePageAllocator, true> op_ordering_;
  // denotes current op will deliver local order data
  common::ObSEArray<OrderItem, 8, common::ModulePageAllocator, true> op_local_ordering_;
  const ObRawExprSets& empty_expr_sets_;
  const ObFdItemSet& empty_fd_item_set_;
  const ObRelIds& empty_table_set_;
  /*
   * number of parent node during the optimization of first-phase
   * this variable is for special purpose, do not use this variable for other cases
   */
  int64_t num_of_parent_;
  int64_t interesting_order_info_;
};

template <typename Allocator>
int ObLogicalOperator::init_all_traverse_ctx(Allocator& alloc)
{
  int ret = common::OB_SUCCESS;
  for (int64_t i = 0; OB_SUCC(ret) && i < get_num_of_child(); i++) {
    if (OB_ISNULL(get_child(i))) {
      ret = OB_ERR_UNEXPECTED;
      SQL_OPT_LOG(WARN, "NULL child", K(ret));
    } else if (OB_FAIL(get_child(i)->init_all_traverse_ctx(alloc))) {
      SQL_OPT_LOG(WARN, "init all traverse ctx failed", K(ret));
    }
  }
  if (OB_SUCC(ret)) {
    if (OB_ISNULL(traverse_ctx_ = alloc.alloc())) {
      ret = common::OB_ALLOCATE_MEMORY_FAILED;
      SQL_OPT_LOG(WARN, "allocate traverse context failed", K(ret));
    }
  }
  return ret;
}

}  // end of namespace sql
}  // end of namespace oceanbase

#endif  // OCEANBASE_SQL_OB_LOGICAL_OPERATOR_H