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

#define USING_LOG_PREFIX SQL_REWRITE
#include "sql/rewrite/ob_transform_post_process.h"
#include "sql/resolver/expr/ob_raw_expr.h"
#include "sql/resolver/dml/ob_dml_stmt.h"
W
wangzelin.wzl 已提交
17
#include "sql/resolver/dml/ob_merge_stmt.h"
O
oceanbase-admin 已提交
18 19 20 21 22
#include "sql/session/ob_sql_session_info.h"
#include "sql/rewrite/ob_transform_utils.h"
#include "sql/optimizer/ob_optimizer_util.h"
#include "sql/resolver/expr/ob_raw_expr_util.h"
#include "sql/rewrite/ob_transform_view_merge.h"
W
wangzelin.wzl 已提交
23
#include "share/schema/ob_schema_struct.h"
O
oceanbase-admin 已提交
24 25
#include "common/ob_smart_call.h"

W
wangzelin.wzl 已提交
26 27
namespace oceanbase
{
O
oceanbase-admin 已提交
28
using namespace common;
W
wangzelin.wzl 已提交
29 30
namespace sql
{
O
oceanbase-admin 已提交
31

W
wangzelin.wzl 已提交
32 33 34
int ObTransformPostProcess::transform_one_stmt(ObIArray<ObParentDMLStmt> &parent_stmts,
                                               ObDMLStmt *&stmt,
                                               bool &trans_happened)
O
oceanbase-admin 已提交
35 36 37 38 39 40 41 42 43 44
{
  int ret = OB_SUCCESS;
  UNUSED(parent_stmts);
  bool is_happened = false;
  trans_happened = false;
  if (OB_SUCC(ret)) {
    if (OB_FAIL(transform_for_hierarchical_query(stmt, is_happened))) {
      LOG_WARN("failed to transform hierarchical query", K(ret));
    } else {
      trans_happened |= is_happened;
W
wangzelin.wzl 已提交
45
      LOG_TRACE("succeed to transform hierarchical query in post",  K(is_happened));
O
oceanbase-admin 已提交
46 47 48 49 50 51 52
    }
  }
  if (OB_SUCC(ret)) {
    if (OB_FAIL(pullup_hierarchical_query(stmt, is_happened))) {
      LOG_WARN("failed to pullup hierarchical query", K(ret));
    } else {
      trans_happened |= is_happened;
W
wangzelin.wzl 已提交
53
      LOG_TRACE("succeed to pullup hierarchical query",  K(is_happened));
O
oceanbase-admin 已提交
54 55 56
    }
  }
  if (OB_SUCC(ret)) {
W
wangzelin.wzl 已提交
57 58
    if (OB_FAIL(transform_merge_into_subquery(stmt, is_happened))) {
      LOG_WARN("failed to transform merge into subquery", K(ret));
O
oceanbase-admin 已提交
59 60
    } else {
      trans_happened |= is_happened;
W
wangzelin.wzl 已提交
61
      LOG_TRACE("succeed to transform merge into subquery", K(ret));
O
oceanbase-admin 已提交
62 63
    }
  }
W
wangzelin.wzl 已提交
64 65 66 67 68 69
  if (OB_SUCC(ret)) {
    if (OB_FAIL(pullup_exec_exprs(stmt, is_happened))) {
      LOG_WARN("failed to pullup exec exprs", K(ret));
    } else {
      trans_happened |= is_happened;
      LOG_TRACE("succeed to pullup exec exprs", K(is_happened));
O
oceanbase-admin 已提交
70
    }
W
wangzelin.wzl 已提交
71 72 73 74 75 76 77
  }
  if (OB_SUCC(ret)) {
    if (OB_FAIL(transform_onetime_subquery(stmt, is_happened))) {
      LOG_WARN("failed to extract onetime subquery", K(ret));
    } else {
      trans_happened |= is_happened;
      LOG_TRACE("succeed to extract onetime subquery", K(ret), K(is_happened));
O
oceanbase-admin 已提交
78 79 80 81 82
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95
int ObTransformPostProcess::need_transform(const common::ObIArray<ObParentDMLStmt> &parent_stmts,
                                           const int64_t current_level,
                                           const ObDMLStmt &stmt,
                                           bool &need_trans)
{
  UNUSED(parent_stmts);
  UNUSED(current_level);
  UNUSED(stmt);
  need_trans = true;
  return OB_SUCCESS;
}

int ObTransformPostProcess::transform_for_hierarchical_query(ObDMLStmt *stmt, bool &trans_happened)
O
oceanbase-admin 已提交
96 97 98 99 100 101 102
{
  int ret = OB_SUCCESS;
  trans_happened = false;
  if (OB_ISNULL(stmt)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("stmt is null", K(ret));
  } else if (OB_UNLIKELY(!stmt->is_select_stmt() || !stmt->is_hierarchical_query())) {
W
wangzelin.wzl 已提交
103
    //do nothing
O
oceanbase-admin 已提交
104 105
  } else if (OB_UNLIKELY(stmt->get_from_item_size() != 2)) {
    ret = OB_ERR_UNEXPECTED;
W
wangzelin.wzl 已提交
106
    LOG_WARN("unexpect from item size",K(stmt->get_from_item_size()), K(ret));
O
oceanbase-admin 已提交
107
  } else {
W
wangzelin.wzl 已提交
108 109 110
    ObSelectStmt *sel_stmt = static_cast<ObSelectStmt*>(stmt);
    TableItem *left_table = NULL;
    TableItem *right_table = NULL;
O
oceanbase-admin 已提交
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
    FromItem left_item = sel_stmt->get_from_item(0);
    FromItem right_item = sel_stmt->get_from_item(1);
    if (OB_UNLIKELY(left_item.is_joined_ || right_item.is_joined_)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("unexpect joined table", K(ret));
    } else if (OB_ISNULL(left_table = sel_stmt->get_table_item(left_item))) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("unexpect null table item", K(ret));
    } else if (OB_ISNULL(right_table = sel_stmt->get_table_item(right_item))) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("unexpect null table item", K(ret));
    } else if (OB_UNLIKELY(!left_table->is_generated_table() || !right_table->is_generated_table())) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("unexpect basic table item", K(ret));
    } else if (OB_FAIL(transform_prior_exprs(*sel_stmt, *left_table, *right_table))) {
      LOG_WARN("failed to transform prior exprs", K(ret));
    } else if (OB_FAIL(make_connect_by_joined_table(*sel_stmt, *left_table, *right_table))) {
      LOG_WARN("failed to make connect by joined table", K(ret));
    } else {
      trans_happened = true;
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
136
int ObTransformPostProcess::pullup_hierarchical_query(ObDMLStmt *stmt, bool &trans_happened)
O
oceanbase-admin 已提交
137
{
W
wangzelin.wzl 已提交
138
  //需要检查子查询是否已经完成层次查询的后续改写
O
oceanbase-admin 已提交
139
  int ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
140 141
  TableItem *table = NULL;
  ObSelectStmt *subquery = NULL;
O
oceanbase-admin 已提交
142 143
  ObSEArray<ObParentDMLStmt, 2> dummy_parent_stmts;
  ObTransformViewMerge view_merge(ctx_);
W
wangzelin.wzl 已提交
144
  view_merge.set_transformer_type(VIEW_MERGE);
O
oceanbase-admin 已提交
145 146 147 148 149 150
  view_merge.set_for_post_process(true);
  trans_happened = false;
  if (OB_ISNULL(stmt)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("stmt is null", K(ret));
  } else if (OB_UNLIKELY(stmt->get_from_item_size() != 1)) {
W
wangzelin.wzl 已提交
151
    //do nothing
O
oceanbase-admin 已提交
152 153 154 155
  } else if (OB_ISNULL(table = stmt->get_table_item(stmt->get_from_item(0)))) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("unexpect null table item", K(ret));
  } else if (OB_UNLIKELY(!table->is_generated_table())) {
W
wangzelin.wzl 已提交
156
    //do nothing
O
oceanbase-admin 已提交
157 158 159
  } else if (OB_ISNULL(subquery = table->ref_query_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("unexpect null query ref", K(ret));
W
wangzelin.wzl 已提交
160 161
  } else if (OB_UNLIKELY(!subquery->is_hierarchical_query()) || 
             subquery->get_from_item_size() != 1 ||
O
oceanbase-admin 已提交
162
             stmt->get_semi_info_size() > 0) {
W
wangzelin.wzl 已提交
163 164
    //层次查询没有处理完成,do nothing
  } else if (OB_FAIL(view_merge.transform_self(dummy_parent_stmts, 0, stmt))) {
O
oceanbase-admin 已提交
165 166
    LOG_WARN("failed to merge view for hierarchical query", K(ret));
  } else {
W
wangzelin.wzl 已提交
167
    trans_happened = view_merge.get_trans_happened();
O
oceanbase-admin 已提交
168 169 170 171 172
    LOG_TRACE("success to merge view for hierarchical query", K(trans_happened));
  }
  return ret;
}

W
wangzelin.wzl 已提交
173 174 175
int ObTransformPostProcess::transform_prior_exprs(ObSelectStmt &stmt,
                                                  TableItem &left_table,
                                                  TableItem &right_table)
O
oceanbase-admin 已提交
176 177
{
  int ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
178 179 180 181 182
  ObSEArray<ObRawExpr *, 4> connect_by_prior_exprs;
  ObSEArray<ObRawExpr *, 4> prior_exprs;
  ObSEArray<ObRawExpr *, 4> converted_exprs;
  ObSEArray<ObRawExpr *, 16> relation_exprs;
  if (OB_ISNULL(ctx_) || OB_ISNULL(ctx_->session_info_) || OB_ISNULL(ctx_->expr_factory_)) {
O
oceanbase-admin 已提交
183
    ret = OB_ERR_UNEXPECTED;
W
wangzelin.wzl 已提交
184
    LOG_WARN("invalid param", K(ret), K(ctx_));
O
oceanbase-admin 已提交
185 186
  } else if (OB_FAIL(get_prior_exprs(stmt.get_connect_by_exprs(), connect_by_prior_exprs))) {
    LOG_WARN("failed to get prior exprs", K(ret));
W
wangzelin.wzl 已提交
187 188 189 190 191 192 193 194 195 196 197
  }
  for (int64_t i = 0; OB_SUCC(ret) && i < connect_by_prior_exprs.count(); ++i) {
    ObRawExpr *raw_expr = connect_by_prior_exprs.at(i);
    ObRawExpr *param_expr = NULL;
    if (OB_ISNULL(raw_expr) ||
        OB_UNLIKELY(1 != raw_expr->get_param_count()) ||
        OB_ISNULL(param_expr = raw_expr->get_param_expr(0))) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("expr is null", K(ret));
    } else if (OB_FAIL(stmt.add_connect_by_prior_expr(param_expr))) {
      LOG_WARN("failed to add connect by prior expr", K(ret));
O
oceanbase-admin 已提交
198 199
    }
  }
W
wangzelin.wzl 已提交
200
  //创建column映射关系
O
oceanbase-admin 已提交
201 202 203 204 205
  if (OB_FAIL(ret)) {
  } else if (OB_FAIL(stmt.get_relation_exprs(relation_exprs))) {
    LOG_WARN("get stmt relation exprs fail", K(ret));
  } else if (OB_FAIL(get_prior_exprs(relation_exprs, prior_exprs))) {
    LOG_WARN("failed to get prior exprs", K(ret));
W
wangzelin.wzl 已提交
206 207 208 209 210 211
  } else if (OB_FAIL(modify_prior_exprs(*ctx_->expr_factory_,
                                        stmt,
                                        left_table,
                                        right_table,
                                        prior_exprs,
                                        converted_exprs))) {
O
oceanbase-admin 已提交
212 213 214 215 216 217 218
    LOG_WARN("failed to modify prior exprs");
  } else if (OB_FAIL(stmt.replace_inner_stmt_expr(prior_exprs, converted_exprs))) {
    LOG_WARN("failed to replace stmt expr", K(ret));
  }
  return ret;
}

W
wangzelin.wzl 已提交
219 220
int ObTransformPostProcess::get_prior_exprs(ObIArray<ObRawExpr *> &exprs,
                                            ObIArray<ObRawExpr *> &prior_exprs)
O
oceanbase-admin 已提交
221 222 223 224 225 226 227 228 229 230
{
  int ret = OB_SUCCESS;
  for (int64_t i = 0; OB_SUCC(ret) && i < exprs.count(); ++i) {
    if (OB_FAIL(get_prior_exprs(exprs.at(i), prior_exprs))) {
      LOG_WARN("failed to get prior exprs", K(ret));
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
231 232
int ObTransformPostProcess::get_prior_exprs(ObRawExpr *expr,
                                            ObIArray<ObRawExpr *> &prior_exprs)
O
oceanbase-admin 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(expr)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("expr is null", K(ret));
  } else if (T_OP_PRIOR == expr->get_expr_type()) {
    if (OB_FAIL(add_var_to_array_no_dup(prior_exprs, expr))) {
      LOG_WARN("failed to add prior exprs", K(ret));
    }
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < expr->get_param_count(); ++i) {
      if (OB_FAIL(SMART_CALL(get_prior_exprs(expr->get_param_expr(i), prior_exprs)))) {
        LOG_WARN("failed to get prior exprs", K(ret));
      }
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
252 253 254 255 256 257
int ObTransformPostProcess::modify_prior_exprs(ObRawExprFactory &expr_factory,
                                               ObSelectStmt &stmt,
                                               TableItem &left_table,
                                               TableItem &right_table,
                                               ObIArray<ObRawExpr *> &prior_exprs,
                                               ObIArray<ObRawExpr *> &convert_exprs)
O
oceanbase-admin 已提交
258 259
{
  int ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
260 261 262
  ObSelectStmt *left_stmt = NULL;
  ObSelectStmt *right_stmt = NULL;
  ObRawExprCopier copier(expr_factory);
O
oceanbase-admin 已提交
263 264 265
  if (OB_UNLIKELY(!left_table.is_generated_table() || !right_table.is_generated_table())) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("expect genereated table", K(left_table), K(right_table), K(ret));
W
wangzelin.wzl 已提交
266 267
  } else if (OB_ISNULL(left_stmt = left_table.ref_query_) || 
             OB_ISNULL(right_stmt = right_table.ref_query_)) {
O
oceanbase-admin 已提交
268
    ret = OB_ERR_UNEXPECTED;
W
wangzelin.wzl 已提交
269
    LOG_WARN("unexpect null ref query",K(left_stmt), K(right_stmt), K(ret));
O
oceanbase-admin 已提交
270 271 272 273
  } else if (OB_UNLIKELY(left_stmt->get_select_item_size() != right_stmt->get_select_item_size())) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("select item size is incorrect", K(ret));
  } else {
W
wangzelin.wzl 已提交
274
    //找到左右表一一对应的column expr
O
oceanbase-admin 已提交
275 276
    for (int64_t i = 0; OB_SUCC(ret) && i < left_stmt->get_select_item_size(); ++i) {
      uint64_t column_id = i + OB_APP_MIN_COLUMN_ID;
W
wangzelin.wzl 已提交
277 278
      ObColumnRefRawExpr *left_column = NULL;
      ObColumnRefRawExpr *right_column = NULL;
O
oceanbase-admin 已提交
279 280 281 282 283 284
      if (OB_ISNULL(left_column = stmt.get_column_expr_by_id(left_table.table_id_, column_id))) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("can not find left table`s column", K(column_id), K(ret));
      } else if (OB_ISNULL(right_column = stmt.get_column_expr_by_id(right_table.table_id_, column_id))) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("can not find right table`s column", K(column_id), K(ret));
W
wangzelin.wzl 已提交
285 286
      } else if (OB_FAIL(copier.add_replaced_expr(right_column, left_column))) {
        LOG_WARN("failed to add replace pair", K(ret));
O
oceanbase-admin 已提交
287 288 289
      }
    }
  }
W
wangzelin.wzl 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
  //更新prior表达式引用左表的列
  //取出prior表达式的参数
  for (int64_t i = 0; i < prior_exprs.count() && OB_SUCC(ret); i++) {
    ObRawExpr *raw_expr = prior_exprs.at(i);
    ObRawExpr *new_expr = NULL;
    if (OB_ISNULL(raw_expr) || OB_UNLIKELY(1 != raw_expr->get_param_count())
        || OB_ISNULL(raw_expr->get_param_expr(0))) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("expr is null", K(ret));
    } else if (!raw_expr->get_expr_levels().has_member(stmt.get_current_level())) {
      // do nothing
    } else if (OB_FAIL(copier.copy_on_replace(raw_expr->get_param_expr(0), new_expr))) {
      LOG_WARN("failed to copy on replace expr", K(ret));
    } else {
      raw_expr = new_expr;
    }
    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(convert_exprs.push_back(raw_expr))) {
      LOG_WARN("failed to push back prior param expr", K(ret));
O
oceanbase-admin 已提交
309 310 311 312 313
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
314 315 316
int ObTransformPostProcess::make_connect_by_joined_table(ObSelectStmt &stmt,
                                                        TableItem &left_table,
                                                        TableItem &right_table)
O
oceanbase-admin 已提交
317 318
{
  int ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
319 320 321 322 323
  void *ptr = NULL;
  JoinedTable *joined_table = NULL;
  ObQueryCtx *query_ctx = stmt.get_query_ctx();
  if (OB_ISNULL(ctx_) || OB_ISNULL(query_ctx) ||
      OB_ISNULL(ctx_->allocator_)) {
O
oceanbase-admin 已提交
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
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("parameter has NULL", K(query_ctx), K(ctx_), K(ret));
  } else if (OB_UNLIKELY(NULL == (ptr = ctx_->allocator_->alloc(sizeof(JoinedTable))))) {
    ret = OB_ALLOCATE_MEMORY_FAILED;
    LOG_WARN("alloc memory for JoinedTable failed", "size", sizeof(JoinedTable), K(ret));
  } else if (OB_ISNULL(joined_table = new (ptr) JoinedTable())) {
    LOG_WARN("unexpect null joined table", K(ret));
  } else {
    joined_table->table_id_ = query_ctx->available_tb_id_--;
    joined_table->type_ = TableItem::JOINED_TABLE;
    joined_table->joined_type_ = CONNECT_BY_JOIN;
    joined_table->left_table_ = &left_table;
    joined_table->right_table_ = &right_table;
    if (OB_FAIL(ObTransformUtils::adjust_single_table_ids(joined_table))) {
      LOG_WARN("failed to adjust single table ids", K(ret));
    } else if (OB_FAIL(joined_table->join_conditions_.assign(stmt.get_connect_by_exprs()))) {
      LOG_WARN("fail to assign connect by exprs", K(ret));
    } else {
      stmt.clear_connect_by_exprs();
      stmt.clear_from_items();
      stmt.get_joined_tables().reset();
      if (OB_FAIL(stmt.add_joined_table(joined_table))) {
        LOG_WARN("fail to add joined table", KPC(joined_table), K(ret));
      } else if (OB_FAIL(stmt.add_from_item(joined_table->table_id_, true))) {
        LOG_WARN("add from item", K(ret));
      }
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
355 356
int ObTransformPostProcess::transform_merge_into_subquery(ObDMLStmt *stmt,
                                                          bool &trans_happened)
O
oceanbase-admin 已提交
357 358
{
  int ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
359 360 361 362 363 364 365 366 367 368
  ObMergeStmt *merge_stmt = NULL;
  ObRawExprFactory *expr_factory = NULL;
  ObRawExpr *matched_expr = NULL;
  ObRawExpr *not_matched_expr = NULL;
  bool update_has_subquery = false;
  bool insert_has_subquery = false;
  ObSEArray<ObRawExpr*, 8> condition_subquery_exprs;
  ObSEArray<ObRawExpr*, 8> target_subquery_exprs;
  ObSEArray<ObRawExpr*, 8> delete_subquery_exprs;
  if (OB_ISNULL(stmt) || OB_ISNULL(expr_factory = ctx_->expr_factory_)) {
O
oceanbase-admin 已提交
369
    ret = OB_ERR_UNEXPECTED;
W
wangzelin.wzl 已提交
370 371
    LOG_WARN("stmt is null", K(ret), K(stmt));
  } else if (!stmt->is_merge_stmt() || !stmt->has_subquery()) {
O
oceanbase-admin 已提交
372
    // do nothing
W
wangzelin.wzl 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
  } else if (FALSE_IT(merge_stmt = static_cast<ObMergeStmt *>(stmt))) {
    // do nothing
  } else if (OB_FAIL(create_matched_expr(*merge_stmt, matched_expr, not_matched_expr))) {
    LOG_WARN("failed to build matched expr", K(ret));
  } else if (OB_FAIL(get_update_insert_condition_subquery(merge_stmt,
                                                          matched_expr,
                                                          not_matched_expr,
                                                          update_has_subquery,
                                                          insert_has_subquery,
                                                          condition_subquery_exprs))) {
    LOG_WARN("failed to allocate update insert condition subquery", K(ret));
  } else if (OB_FAIL(get_update_insert_target_subquery(merge_stmt,
                                                       matched_expr,
                                                       not_matched_expr,
                                                       update_has_subquery,
                                                       insert_has_subquery,
                                                       target_subquery_exprs))) {
    LOG_WARN("failed to allocate update insert target subquery", K(ret));
  } else if (OB_FAIL(get_delete_condition_subquery(merge_stmt,
                                                   matched_expr,
                                                   update_has_subquery,
                                                   delete_subquery_exprs))) {
    LOG_WARN("failed to allocate delete condition subquery", K(ret));
  } else if (OB_FAIL(merge_stmt->formalize_stmt_expr_reference())) {
    LOG_WARN("failed to formalize stmt expr reference", K(ret));
  } else {
    trans_happened = true;
  }
  return ret;
}

int ObTransformPostProcess::create_matched_expr(ObMergeStmt &stmt,
                                                ObRawExpr *&matched_flag,
                                                ObRawExpr *&not_matched_flag)
{
  int ret = OB_SUCCESS;
  ObMergeTableInfo& table_info = stmt.get_merge_table_info();
  ObRawExpr *not_null_expr = NULL;
  for (int64_t i = 0; OB_SUCC(ret) && i < table_info.column_exprs_.count(); ++i) {
    ObColumnRefRawExpr *column = NULL;
    if (OB_ISNULL(column = table_info.column_exprs_.at(i))) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("column expr is null", K(ret));
    } else if (column->is_not_null_for_read()) {
      not_null_expr = column;
      break;
    }
  }
  if (OB_SUCC(ret)) {
    if (OB_ISNULL(ctx_) || OB_ISNULL(ctx_->expr_factory_) || OB_ISNULL(ctx_->session_info_)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("context is invalid", K(ret), K(ctx_));
    } else if (OB_ISNULL(not_null_expr)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("failed to find not null expr from merge into target table", K(ret));
    } else if (OB_FAIL(ObRawExprUtils::build_is_not_null_expr(
                         *ctx_->expr_factory_,
                         not_null_expr,
                         true,
                         matched_flag))) {
      LOG_WARN("failed to build is not null expr", K(ret));
    } else if (OB_FAIL(ObRawExprUtils::build_is_not_null_expr(
                         *ctx_->expr_factory_,
                         not_null_expr,
                         false,
                         not_matched_flag))) {
      LOG_WARN("failed to build is null expr", K(ret));
    } else if (OB_FAIL(matched_flag->formalize(ctx_->session_info_))) {
      LOG_WARN("failed to formalize matched expr", K(ret));
    } else if (OB_FAIL(not_matched_flag->formalize(ctx_->session_info_))) {
      LOG_WARN("failed to formalize not matched flag", K(ret));
    }
  }
  return ret;
}

int ObTransformPostProcess::get_update_insert_condition_subquery(ObMergeStmt *merge_stmt,
                                                                 ObRawExpr *matched_expr,
                                                                 ObRawExpr *not_matched_expr,
                                                                 bool &update_has_subquery,
                                                                 bool &insert_has_subquery,
                                                                 ObIArray<ObRawExpr*> &new_subquery_exprs)
{
  int ret = OB_SUCCESS;
  update_has_subquery = false;
  insert_has_subquery = false;
  if (OB_ISNULL(merge_stmt)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("get unexpected null", K(ret), K(merge_stmt), K(ctx_));
  } else if (OB_FAIL(ObOptimizerUtil::check_expr_contain_subquery(merge_stmt->get_update_condition_exprs(),
                                                                  update_has_subquery))) {
    LOG_WARN("failed to check whether expr contain subquery", K(ret));
  } else if (OB_FAIL(ObOptimizerUtil::check_expr_contain_subquery(merge_stmt->get_insert_condition_exprs(),
                                                                  insert_has_subquery))) {
    LOG_WARN("failed to check whether expr contain subquery", K(ret));
  } else if (update_has_subquery &&
             OB_FAIL(generate_merge_conditions_subquery(matched_expr,
                                                        merge_stmt->get_update_condition_exprs()))) {
    LOG_WARN("failed to generate condition subquery", K(ret));
  } else if (insert_has_subquery &&
             OB_FAIL(generate_merge_conditions_subquery(not_matched_expr,
                                                        merge_stmt->get_insert_condition_exprs()))) {
    LOG_WARN("failed to generate condition subquery", K(ret));
  } else if (update_has_subquery &&
             OB_FAIL(append(new_subquery_exprs, merge_stmt->get_update_condition_exprs()))) {
    LOG_WARN("failed to append subquery exprs", K(ret));
  } else if (insert_has_subquery &&
             OB_FAIL(append(new_subquery_exprs, merge_stmt->get_insert_condition_exprs()))) {
    LOG_WARN("failed to append subquery exprs", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < new_subquery_exprs.count(); i++) {
      ObRawExpr *raw_expr = NULL;
      if (OB_ISNULL(raw_expr = new_subquery_exprs.at(i))) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("get unexpected null", K(ret));
      } else if (OB_FAIL(raw_expr->formalize(ctx_->session_info_))) {
        LOG_WARN("failed to formalize case expr", K(ret));
      } else if (OB_FAIL(raw_expr->pull_relation_id_and_levels(merge_stmt->get_current_level()))) {
        LOG_WARN("failed to pull relation id and levels", K(ret));
      } else { /*do nothing*/ }
    }
  }
  return ret;
}

int ObTransformPostProcess::generate_merge_conditions_subquery(ObRawExpr *matched_expr,
                                                               ObIArray<ObRawExpr*> &condition_exprs)
{
  int ret = OB_SUCCESS;
  ObRawExpr *and_expr = NULL;
  ObSEArray<ObRawExpr *, 4> param_conditions;
  ObSEArray<ObRawExpr*, 8> subquery_exprs;
  ObSEArray<ObRawExpr*, 8> non_subquery_exprs;
  ObRawExprFactory *expr_factory = NULL;
  if (OB_ISNULL(matched_expr) || OB_ISNULL(expr_factory = ctx_->expr_factory_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("get unexpected null", K(ret));
  } else if (OB_FAIL(ObOptimizerUtil::classify_subquery_exprs(condition_exprs,
                                                              subquery_exprs,
                                                              non_subquery_exprs))) {
    LOG_WARN("failed to classify subquery exprs", K(ret));
  } else if (subquery_exprs.empty()) {
    /*do nothing*/
  } else if (OB_FAIL(param_conditions.push_back(matched_expr)) ||
             OB_FAIL(append(param_conditions, condition_exprs))) {
    LOG_WARN("failed to push back expr", K(ret));
  } else if (OB_FAIL(ObRawExprUtils::build_and_expr(*expr_factory, param_conditions, and_expr))) {
    LOG_WARN("failed to build matched expr", K(ret));
  } else if (OB_ISNULL(and_expr)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("get unexpected null", K(ret));
O
oceanbase-admin 已提交
524
  } else {
W
wangzelin.wzl 已提交
525 526 527 528
    condition_exprs.reuse();
    if (OB_FAIL(condition_exprs.push_back(and_expr))) {
      LOG_WARN("failed to push back expr", K(ret));
    } else { /*do nothing*/ }
O
oceanbase-admin 已提交
529 530 531 532
  }
  return ret;
}

W
wangzelin.wzl 已提交
533 534 535 536 537 538
int ObTransformPostProcess::get_update_insert_target_subquery(ObMergeStmt *merge_stmt,
                                                              ObRawExpr *matched_expr,
                                                              ObRawExpr *not_matched_expr,
                                                              bool update_has_subquery,
                                                              bool insert_has_subquery,
                                                              ObIArray<ObRawExpr*> &new_subquery_exprs)
O
oceanbase-admin 已提交
539 540
{
  int ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
  ObRawExpr *null_expr = NULL;
  ObSEArray<ObRawExpr*, 8> temp_exprs;
  ObSEArray<ObRawExpr*, 8> assign_exprs;
  ObSEArray<ObRawExpr*, 8> update_subquery_exprs;
  ObSEArray<ObRawExpr*, 8> insert_values_subquery_exprs;
  ObRawExprFactory *expr_factory = NULL;
  if (OB_ISNULL(merge_stmt) || OB_ISNULL(ctx_) ||
      OB_ISNULL(expr_factory = ctx_->expr_factory_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("get unexpected null", K(ret));
  } else if (OB_FAIL(merge_stmt->get_assignments_exprs(assign_exprs))) {
    LOG_WARN("failed to get table assignment exprs", K(ret));
  } else if (OB_FAIL(ObOptimizerUtil::get_subquery_exprs(assign_exprs,
                                                         update_subquery_exprs))) {
    LOG_WARN("failed to get subquery exprs", K(ret));
  } else if (OB_FAIL(ObOptimizerUtil::get_subquery_exprs(merge_stmt->get_values_vector(),
                                                         insert_values_subquery_exprs))) {
    LOG_WARN("failed to get subquery exprs", K(ret));
  } else if (OB_FAIL(ObRawExprUtils::build_null_expr(*expr_factory, null_expr))) {
    LOG_WARN("failed to build null expr", K(ret));
  } else if (OB_ISNULL(null_expr)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("get unexpected null expr", K(null_expr), K(ret));
  } else {
    if (!update_subquery_exprs.empty()) {
      ObRawExpr *update_matched_expr = NULL;
      if (merge_stmt->get_update_condition_exprs().empty()) {
        update_matched_expr = matched_expr;
      } else if (update_has_subquery) {
        if (OB_UNLIKELY(1 != merge_stmt->get_update_condition_exprs().count()) ||
            OB_ISNULL(merge_stmt->get_update_condition_exprs().at(0))) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("get unexpected error", K(ret));
        } else {
          update_matched_expr = merge_stmt->get_update_condition_exprs().at(0);
        }
      } else if (OB_FAIL(temp_exprs.push_back(matched_expr)) ||
                 OB_FAIL(append(temp_exprs, merge_stmt->get_update_condition_exprs()))) {
        LOG_WARN("failed to append exprs", K(ret));
      } else if (OB_FAIL(ObRawExprUtils::build_and_expr(*expr_factory,
                                                        temp_exprs,
                                                        update_matched_expr))) {
        LOG_WARN("failed to build and expr", K(ret));
      } else if (OB_ISNULL(update_matched_expr)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("get unexpected null", K(ret));
      } else { /*do nothing*/ }

      for (int64_t i = 0; OB_SUCC(ret) && i < update_subquery_exprs.count(); i++) {
        ObRawExpr *raw_expr = NULL;
        ObRawExpr *cast_expr = NULL;
        ObRawExpr *case_when_expr = NULL;
        if (OB_ISNULL(raw_expr = update_subquery_exprs.at(i))) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("get unexpected null", K(ret));
        } else if (OB_FAIL(ObRawExprUtils::try_add_cast_expr_above(
                              ctx_->expr_factory_,
                              ctx_->session_info_,
                              *null_expr,
                              raw_expr->get_result_type(),
                              cast_expr))) {
          LOG_WARN("try add cast expr above failed", K(ret));
        } else if (OB_FAIL(ObRawExprUtils::build_case_when_expr(*expr_factory,
                                                                update_matched_expr,
                                                                raw_expr,
                                                                cast_expr,
                                                                case_when_expr))) {
          LOG_WARN("failed to build case when expr", K(ret));
        } else if (OB_ISNULL(case_when_expr)) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("get unexpected null", K(ret));
        } else if (OB_FAIL(new_subquery_exprs.push_back(case_when_expr))) {
          LOG_WARN("failed to push back case when expr", K(ret));
        } else { /*do nothing*/ }
      }
    }
    if (OB_SUCC(ret) && !insert_values_subquery_exprs.empty()) {
      ObRawExpr *insert_matched_expr = NULL;
      if (merge_stmt->get_insert_condition_exprs().empty()) {
        insert_matched_expr = not_matched_expr;
      } else if (insert_has_subquery) {
        if (OB_UNLIKELY(1 != merge_stmt->get_insert_condition_exprs().count()) ||
            OB_ISNULL(merge_stmt->get_insert_condition_exprs().at(0))) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("get unexpected null", K(ret));
        } else {
          insert_matched_expr = merge_stmt->get_insert_condition_exprs().at(0);
        }
      } else if (OB_FAIL(temp_exprs.push_back(not_matched_expr)) ||
                 OB_FAIL(temp_exprs.assign(merge_stmt->get_insert_condition_exprs()))) {
        LOG_WARN("failed to fill and child exprs", K(ret));
      } else if (OB_FAIL(ObRawExprUtils::build_and_expr(*expr_factory, temp_exprs,
                                                        insert_matched_expr))) {
        LOG_WARN("failed to build and expr", K(ret));
      } else if (OB_ISNULL(insert_matched_expr)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("get unexpected null", K(ret));
      } else { /*do nothing*/ }

      for (int64_t i = 0; OB_SUCC(ret) && i < insert_values_subquery_exprs.count(); i++) {
        ObRawExpr *raw_expr = NULL;
        ObRawExpr *case_when_expr = NULL;
        if (OB_ISNULL(raw_expr = insert_values_subquery_exprs.at(i))) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("get unexpected null", K(ret));
        } else if (OB_FAIL(ObRawExprUtils::build_case_when_expr(*expr_factory,
                                                                insert_matched_expr,
                                                                raw_expr,
                                                                null_expr,
                                                                case_when_expr))) {
          LOG_WARN("failed to build case when expr", K(ret));
        } else if (OB_ISNULL(case_when_expr)) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("get unexpected null", K(ret));
        } else if (OB_FAIL(new_subquery_exprs.push_back(case_when_expr))) {
          LOG_WARN("failed to push back subquery exprs", K(ret));
        } else { /*do nothing*/ }
      }
    }
    for (int64_t i = 0; OB_SUCC(ret) && i < new_subquery_exprs.count(); i++) {
      ObRawExpr *raw_expr = NULL;
      if (OB_ISNULL(raw_expr = new_subquery_exprs.at(i))) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("get unexpected null", K(ret));
      } else if (OB_FAIL(raw_expr->formalize(ctx_->session_info_))) {
        LOG_WARN("failed to formalize case expr", K(ret));
      } else if (OB_FAIL(raw_expr->pull_relation_id_and_levels(merge_stmt->get_current_level()))) {
        LOG_WARN("failed to pull relation id and levels", K(ret));
      } else { /*do nothing*/ }
    }
    if (OB_SUCC(ret) && !new_subquery_exprs.empty()) {
      ObSEArray<ObRawExpr*, 8> old_subquery_exprs;
      if (OB_FAIL(append(old_subquery_exprs, update_subquery_exprs)) ||
          OB_FAIL(append(old_subquery_exprs, insert_values_subquery_exprs))) {
        LOG_WARN("failed to append exprs", K(ret));
      } else if (OB_UNLIKELY(old_subquery_exprs.count() != new_subquery_exprs.count())) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("get unexpected array count", K(old_subquery_exprs.count()),
            K(new_subquery_exprs.count()), K(ret));
      } else if (OB_FAIL(merge_stmt->replace_inner_stmt_expr(old_subquery_exprs, new_subquery_exprs))) {
        LOG_WARN("failed to replace merge stmt", K(ret));
      } else { /*do nothing*/ }
    }
  }
  return ret;
}

int ObTransformPostProcess::get_delete_condition_subquery(ObMergeStmt *merge_stmt,
                                                          ObRawExpr *matched_expr,
                                                          bool update_has_subquery,
                                                          ObIArray<ObRawExpr*> &new_subquery_exprs)
{
  int ret = OB_SUCCESS;
  ObRawExprFactory *expr_factory = NULL;
  bool delete_has_subquery = false;
  if (OB_ISNULL(merge_stmt) || OB_ISNULL(ctx_) ||
      OB_ISNULL(expr_factory = ctx_->expr_factory_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("get unexpected null", K(ret));
  } else if (OB_FAIL(ObOptimizerUtil::check_expr_contain_subquery(merge_stmt->get_delete_condition_exprs(),
                                                                  delete_has_subquery))) {
    LOG_WARN("failed to check whether expr contain subquery", K(ret));
  } else if (!delete_has_subquery) {
    /*do nothing*/
  } else {
    ObSqlBitSet<> check_table_set;
    ObSEArray<ObRawExpr*, 8> temp_exprs;
    ObRawExpr *delete_matched_expr = NULL;
    ObSEArray<ObRawExpr*, 8> all_column_exprs;
    ObSEArray<ObRawExpr*, 8> delete_column_exprs;
    if (OB_FAIL(check_table_set.add_member(merge_stmt->get_table_bit_index(
                                           merge_stmt->get_target_table_id())))) {
      LOG_WARN("failed to add table set", K(ret));
    } else if (OB_FAIL(ObRawExprUtils::extract_column_exprs(merge_stmt->get_delete_condition_exprs(),
                                                            all_column_exprs))) {
      LOG_WARN("failed to extract column exprs", K(ret));
    } else if (OB_FAIL(ObTransformUtils::extract_table_exprs(*merge_stmt,
                                                             all_column_exprs,
                                                             check_table_set,
                                                             delete_column_exprs))) {
      LOG_WARN("failed to extract table exprs", K(ret));
    } else if (delete_column_exprs.empty()) {
      /*do nothing*/
    } else {
      ObSEArray<ObRawExpr*, 8> old_exprs;
      ObSEArray<ObRawExpr*, 8> new_exprs;
      ObAssignments &table_assigns  = merge_stmt->get_merge_table_info().assignments_;
      for (int64_t j = 0; OB_SUCC(ret) && j < table_assigns.count(); j++) {
        if (OB_ISNULL(table_assigns.at(j).column_expr_) ||
            OB_ISNULL(table_assigns.at(j).expr_)) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("get unexpected null", K(ret));
        } else if (!ObOptimizerUtil::find_item(delete_column_exprs,
                                               table_assigns.at(j).column_expr_)) {
          /*do nothing*/
        } else if (table_assigns.at(j).expr_->has_flag(CNT_SUB_QUERY) ||
                   table_assigns.at(j).expr_->has_flag(CNT_ONETIME)) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("not support replace column expr with subquery expr", K(ret));
        } else if (OB_FAIL(old_exprs.push_back(table_assigns.at(j).column_expr_)) ||
                   OB_FAIL(new_exprs.push_back(table_assigns.at(j).expr_))) {
          LOG_WARN("failed to push back exprs", K(ret));
        }  else { /*do nothing*/ }
      }
      if (OB_SUCC(ret)) {
        if (OB_FAIL(ObTransformUtils::replace_exprs(old_exprs,
                                                    new_exprs,
                                                    merge_stmt->get_delete_condition_exprs()))) {
          LOG_WARN("failed to replace exprs", K(ret));
        }
      }
    }
    if (OB_FAIL(ret)) {
      /*do nothing*/
    } else if (merge_stmt->get_update_condition_exprs().empty()) {
      delete_matched_expr = matched_expr;
    } else if (update_has_subquery) {
      if (OB_UNLIKELY(1 != merge_stmt->get_update_condition_exprs().count()) ||
          OB_ISNULL(merge_stmt->get_update_condition_exprs().at(0))) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("get unexpected error", K(ret));
      } else {
        delete_matched_expr = merge_stmt->get_update_condition_exprs().at(0);
      }
    } else if (OB_FAIL(temp_exprs.push_back(matched_expr)) ||
               OB_FAIL(append(temp_exprs, merge_stmt->get_update_condition_exprs()))) {
      LOG_WARN("failed to push back exprs", K(ret));
    } else if (OB_FAIL(ObRawExprUtils::build_and_expr(*expr_factory,
                                                      temp_exprs,
                                                      delete_matched_expr))) {
      LOG_WARN("failed to build matched expr", K(ret));
    } else if (OB_ISNULL(delete_matched_expr)) {
O
oceanbase-admin 已提交
773
      ret = OB_ERR_UNEXPECTED;
W
wangzelin.wzl 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
      LOG_WARN("get unexpected null", K(ret));
    } else { /*do nothing*/}

    if (OB_FAIL(ret)) {
      /*do nothing*/
    } else if (OB_FAIL(generate_merge_conditions_subquery(delete_matched_expr,
                                                          merge_stmt->get_delete_condition_exprs()))) {
      LOG_WARN("failed to generate merge conditions", K(ret));
    } else if (OB_FAIL(append(new_subquery_exprs, merge_stmt->get_delete_condition_exprs()))) {
      LOG_WARN("failed to append new exprs", K(ret));
    } else {
      for (int64_t i = 0; OB_SUCC(ret) && i < new_subquery_exprs.count(); i++) {
        ObRawExpr *raw_expr = NULL;
        if (OB_ISNULL(raw_expr = new_subquery_exprs.at(i))) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("get unexpected null", K(ret));
        } else if (OB_FAIL(raw_expr->formalize(ctx_->session_info_))) {
          LOG_WARN("failed to formalize case expr", K(ret));
        } else if (OB_FAIL(raw_expr->pull_relation_id_and_levels(merge_stmt->get_current_level()))) {
          LOG_WARN("failed to pull relation id and levels", K(ret));
        } else { /*do nothing*/ }
      }
    }
  }
  return ret;
}

int ObTransformPostProcess::pullup_exec_exprs(ObDMLStmt *stmt,
                                              bool &trans_happened)
{
  int ret = OB_SUCCESS;
  for (int64_t i = 0; OB_SUCC(ret) && i < stmt->get_subquery_expr_size(); ++i) {
    ObQueryRefRawExpr *query_ref = stmt->get_subquery_exprs().at(i);
    ObSEArray<ObRawExpr *, 4> candi_exprs;
    if (OB_ISNULL(query_ref) || OB_ISNULL(query_ref->get_ref_stmt())) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("query ref is null", K(ret), K(query_ref));
    } else if (query_ref->get_exec_params().empty()) {
      // do nothing
    } else if (OB_FAIL(extract_exec_exprs(query_ref->get_ref_stmt(),
                                          query_ref->get_expr_level(),
                                          candi_exprs))) {
      LOG_WARN("failed to get exec exprs", K(ret));
    } else if (candi_exprs.empty()) {
      // do nothing
    } else if (OB_FAIL(adjust_exec_param_exprs(query_ref,
                                               candi_exprs,
                                               stmt->get_current_level()))) {
      LOG_WARN("failed to build exec param exprs", K(ret));
    } else {
      trans_happened = true;
O
oceanbase-admin 已提交
825 826 827 828 829
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
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 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 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
int ObTransformPostProcess::adjust_exec_param_exprs(ObQueryRefRawExpr *query_ref,
                                                    ObIArray<ObRawExpr *> &candi_exprs,
                                                    const int64_t stmt_level)
{
  int ret = OB_SUCCESS;
  ObSEArray<ObRawExpr *, 4> new_exprs;
  if (OB_ISNULL(ctx_) || OB_ISNULL(ctx_->expr_factory_) || OB_ISNULL(ctx_->session_info_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("transform context is invalid", K(ret));
  }
  for (int64_t i = 0; OB_SUCC(ret) && i < candi_exprs.count(); ++i) {
    ObRawExpr *old_expr = NULL;
    ObRawExpr *new_expr = NULL;
    ObExecParamRawExpr *exec_param = NULL;
    if (OB_ISNULL(old_expr = candi_exprs.at(i))) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("candi expr is null", K(ret));
    } else if (OB_FAIL(ObTransformUtils::decorrelate(old_expr, stmt_level))) {
      LOG_WARN("failed to decorrelate expr", K(ret));
    } else if (old_expr->formalize(ctx_->session_info_)) {
      LOG_WARN("failed to formalize", K(ret));
    } else if (old_expr->is_const_expr()) {
      new_expr = old_expr;
    } else if (OB_FAIL(ctx_->expr_factory_->create_raw_expr(T_QUESTIONMARK, exec_param))) {
      LOG_WARN("failed to create exec param expr", K(ret));
    } else if (OB_ISNULL(exec_param)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("new expr is null", K(ret), K(exec_param));
    } else if (OB_FAIL(exec_param->add_flag(IS_CONST))) {
      LOG_WARN("failed to add flag", K(ret));
    } else if (OB_FAIL(exec_param->add_flag(IS_DYNAMIC_PARAM))) {
      LOG_WARN("failed to add flag", K(ret));
    } else {
      exec_param->set_ref_expr(old_expr);
      exec_param->set_result_type(old_expr->get_result_type());
      exec_param->set_param_index(-1);
      exec_param->set_expr_level(stmt_level);
      new_expr = exec_param;
    }
    if (OB_SUCC(ret) && OB_FAIL(new_exprs.push_back(new_expr))) {
      LOG_WARN("failed to push back new expr", K(ret));
    }
  }
  if (OB_SUCC(ret)) {
    if (OB_FAIL(query_ref->get_ref_stmt()->replace_inner_stmt_expr(candi_exprs,
                                                                   new_exprs))) {
      LOG_WARN("failed to replace inner stmt expr", K(ret));
    }
  }
  for (int64_t i = 0; OB_SUCC(ret) && i < new_exprs.count(); ++i) {
    if (new_exprs.at(i)->is_exec_param_expr()) {
      ObExecParamRawExpr *exec = static_cast<ObExecParamRawExpr *>(new_exprs.at(i));
      if (OB_FAIL(ObTransformUtils::decorrelate(exec->get_ref_expr(), stmt_level))) {
        LOG_WARN("failed to decorrelate ref expr", K(ret));
      }
    }
  }
  if (OB_SUCC(ret)) {
    if (OB_FAIL(query_ref->formalize(ctx_->session_info_))) {
      LOG_WARN("failed to formalize", K(ret));
    } else if (OB_FAIL(query_ref->pull_relation_id_and_levels(stmt_level))) {
      LOG_WARN("failed to pull relation id", K(ret));
    }
  }
  return ret;
}

int ObTransformPostProcess::extract_exec_exprs(ObDMLStmt *stmt,
                                               const int32_t expr_level,
                                               ObIArray<ObRawExpr *> &candi_exprs)
{
  int ret = OB_SUCCESS;
  ObSEArray<ObRawExpr *, 4> exprs;
  ObSEArray<ObSelectStmt *, 4> child_stmts;
  if (OB_FAIL(stmt->get_relation_exprs(exprs))) {
    LOG_WARN("failed to get relation exprs", K(ret));
  } else if (OB_FAIL(stmt->get_child_stmts(child_stmts))) {
    LOG_WARN("failed to get child stmts", K(ret));
  }
  for (int64_t i = 0; OB_SUCC(ret) && i < exprs.count(); ++i) {
    if (OB_FAIL(SMART_CALL(extract_exec_exprs(exprs.at(i),
                                              expr_level,
                                              candi_exprs)))) {
      LOG_WARN("failed to extract exec exprs", K(ret));
    }
  }
  for (int64_t i = 0; OB_SUCC(ret) && i < child_stmts.count(); ++i) {
    if (OB_FAIL(SMART_CALL(extract_exec_exprs(child_stmts.at(i),
                                              expr_level,
                                              candi_exprs)))) {
      LOG_WARN("failed to extract exec exprs", K(ret));
    }
  }
  return ret;
}

int ObTransformPostProcess::extract_exec_exprs(ObRawExpr *expr,
                                               const int32_t expr_level,
                                               ObIArray<ObRawExpr *> &candi_exprs)
{
  int ret = OB_SUCCESS;
  if (!expr->has_flag(CNT_DYNAMIC_PARAM)) {
    // do nothing
  } else if (expr->is_exec_param_expr()) {
    ObExecParamRawExpr *exec = static_cast<ObExecParamRawExpr *>(expr);
    if (exec->is_onetime()) {
      if (OB_FAIL(SMART_CALL(extract_exec_exprs(exec->get_ref_expr(),
                                                expr_level,
                                                candi_exprs)))) {
        LOG_WARN("failed to extract exec exprs", K(ret));
      }
    }
  } else if (!expr->is_const_expr() ||
             expr->has_flag(CNT_AGG) ||
             expr->has_flag(CNT_WINDOW_FUNC) ||
             expr->get_expr_type() == T_OP_ROW ||
             get_expr_level(*expr) != expr_level) {
    for (int64_t i = 0; OB_SUCC(ret) && i < expr->get_param_count(); ++i) {
      if (OB_FAIL(SMART_CALL(extract_exec_exprs(expr->get_param_expr(i),
                                                expr_level,
                                                candi_exprs)))) {
        LOG_WARN("failed to pullup exec exprs", K(ret));
      }
    }
  } else if (OB_FAIL(candi_exprs.push_back(expr))) {
    LOG_WARN("failed to push back expr", K(ret));
  }
  return ret;
}

int64_t ObTransformPostProcess::get_expr_level(const ObRawExpr &expr)
{
  int64_t expr_level = -1;
  int64_t max_size = expr.get_expr_levels().bit_count();
  for (int64_t i = max_size - 1; i >= 0; --i) {
    if (expr.get_expr_levels().has_member(i)) {
      expr_level = i;
      break;
    }
  }
  return expr_level;
}

int ObTransformPostProcess::transform_onetime_subquery(ObDMLStmt *stmt,
                                                       bool &trans_happened)
{
  int ret = OB_SUCCESS;
  ObSEArray<ObRawExpr *, 4> old_exprs;
  ObSEArray<ObRawExpr *, 4> new_exprs;
  ObArray<ObRawExpr *> func_table_exprs;
  if (OB_ISNULL(stmt)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("stmt is null", K(ret));
  } else if (stmt->get_subquery_expr_size() > 0) {
    ObSEArray<ObRawExprPointer, 4> expr_ptrs;
    if (OB_FAIL(stmt->get_relation_exprs(expr_ptrs))) {
      LOG_WARN("failed to get relation exprs", K(ret));
    } else if (OB_FAIL(stmt->get_table_function_exprs(func_table_exprs))) {
      LOG_WARN("failed to get table function exprs", K(ret));
    }
    for (int64_t i = 0; OB_SUCC(ret) && i < expr_ptrs.count(); ++i) {
      bool dummy = false;
      bool is_happend = false;
      ObRawExpr *expr = NULL;
      ObSEArray<ObRawExpr *, 4> onetime_list;
      if (OB_FAIL(expr_ptrs.at(i).get(expr))) {
        LOG_WARN("failed to get expr", K(ret));
      } else if (ObOptimizerUtil::find_item(func_table_exprs, expr)) {
        // do nothing
      } else if (OB_FAIL(extract_onetime_subquery(expr, onetime_list, dummy))) {
        LOG_WARN("failed to extract onetime subquery", K(ret));
      } else if (onetime_list.empty()) {
        // do nothing
      } else if (OB_FAIL(create_onetime_param(stmt, expr, onetime_list,
                                              old_exprs, new_exprs, is_happend))) {
        LOG_WARN("failed to create onetime param", K(ret));
      } else if (!is_happend) {
        // do nothing
      } else if (OB_FAIL(expr->formalize(ctx_->session_info_))) {
        LOG_WARN("failed to formalize condition", K(ret));
      } else if (OB_FAIL(expr_ptrs.at(i).set(expr))) {
        LOG_WARN("failed to set expr", K(ret));
      } else {
        trans_happened = true;
      }
    }
  }
  return ret;
}

/**
 * @brief ObTransformPostProcess::extract_onetime_subquery
 * @param expr
 * @param onetime_list
 * @param is_valid: if a expr is invalid,
 *                  its parent is also invalid while its children can be valid
 * @return
 */
int ObTransformPostProcess::extract_onetime_subquery(ObRawExpr *&expr,
                                                     ObIArray<ObRawExpr *> &onetime_list,
                                                     bool &is_valid)
{
  int ret = OB_SUCCESS;
  bool is_valid_non_correlated_exists = false;
  if (OB_ISNULL(expr)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("expr is null", K(ret));
  } else {
    // if a expr contain psedu column, hierachical expr, any column
    is_valid = (expr != NULL) &&
        !ObOptimizerUtil::has_psedu_column(*expr) &&
        !ObOptimizerUtil::has_hierarchical_expr(*expr) &&
        !expr->has_flag(CNT_COLUMN) &&
        !expr->has_flag(CNT_AGG) &&
        !expr->has_flag(CNT_WINDOW_FUNC) &&
        !expr->has_flag(CNT_ONETIME) &&
        !expr->has_flag(CNT_ALIAS);

    if (is_valid) {
1049
      int64_t ref_count = 0;
W
wangzelin.wzl 已提交
1050
      if (OB_FAIL(is_non_correlated_exists_for_onetime(expr, 
1051 1052
                    is_valid_non_correlated_exists,
                    ref_count))) {
W
wangzelin.wzl 已提交
1053
        LOG_WARN("failed to check non correlated exist for one time", K(ret));
1054 1055 1056
      } else if (!is_valid_non_correlated_exists) {
        // do nothing
      } else if (OB_FAIL(onetime_list.push_back(expr))) {
W
wangzelin.wzl 已提交
1057
        LOG_WARN("failed to push back non-correlated exists", K(ret));
1058 1059
      } else if (ref_count > 1) {
        is_valid = false;
W
wangzelin.wzl 已提交
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 1106 1107 1108 1109 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
      }
    }
  }

  if (OB_SUCC(ret) && expr->is_query_ref_expr()) {
    bool has_ref_assign_user_var = false;
    if (!is_valid) {
      // do nothing
    } else if (expr->get_param_count() > 0) {
      is_valid = false;
    } else if (OB_FAIL(ObOptimizerUtil::check_subquery_has_ref_assign_user_var(
                         expr, has_ref_assign_user_var))) {
      LOG_WARN("failed to check subquery has ref assign user var", K(ret));
    } else if (has_ref_assign_user_var) {
      is_valid = false;
    } else if (expr->get_output_column() == 1 &&
               !static_cast<ObQueryRefRawExpr *>(expr)->is_set()) {
      if (OB_FAIL(onetime_list.push_back(expr))) {
        LOG_WARN("failed to push back candi onetime expr", K(ret));
      }
    }
  }

  if (OB_SUCC(ret) && expr->has_flag(CNT_SUB_QUERY) && !is_valid_non_correlated_exists) {
    for (int64_t i = 0; OB_SUCC(ret) && i < expr->get_param_count(); ++i) {
      bool is_param_valid = false;
      if (OB_FAIL(extract_onetime_subquery(expr->get_param_expr(i),
                                           onetime_list,
                                           is_param_valid))) {
        LOG_WARN("failed to extract onetime subquery", K(ret));
      } else if (!is_param_valid) {
        is_valid = false;
      }
    }
    if (OB_SUCC(ret) && is_valid && !expr->is_query_ref_expr() &&
        expr->get_expr_type() != T_FUN_COLUMN_CONV &&
        expr->get_expr_type() != T_OP_ROW) {
      if (OB_FAIL(onetime_list.push_back(expr))) {
        LOG_WARN("failed to push back candi onetime exprs", K(ret));
      }
    }
  }

  if (OB_SUCC(ret) && is_valid && !is_valid_non_correlated_exists) {
    is_valid = (expr->get_ref_count() <= 1);
  }
  return ret;
}

int ObTransformPostProcess::create_onetime_param(ObDMLStmt *stmt,
                                                 ObRawExpr *&expr,
                                                 const ObIArray<ObRawExpr *> &onetime_list,
                                                 ObIArray<ObRawExpr *> &old_exprs,
                                                 ObIArray<ObRawExpr *> &new_exprs,
                                                 bool &is_happened)
{
  int ret = OB_SUCCESS;
  int64_t idx = -1;
  if (OB_ISNULL(expr) || OB_ISNULL(stmt)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("expr is null", K(ret));
  } else if (ObOptimizerUtil::find_item(old_exprs, expr, &idx)) {
    if (OB_UNLIKELY(idx < 0 || idx >= new_exprs.count())) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("invalid index", K(ret), K(idx), K(new_exprs.count()));
    } else {
      expr = new_exprs.at(idx);
      is_happened = true;
    }
  } else if (ObOptimizerUtil::find_item(onetime_list, expr)) {
    ObExecParamRawExpr *new_expr = NULL;
    if (OB_FAIL(ctx_->expr_factory_->create_raw_expr(T_QUESTIONMARK, new_expr))) {
      LOG_WARN("faield to create exec param expr", K(ret));
    } else if (OB_ISNULL(new_expr)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("new expr is null", K(ret), K(new_expr));
    } else {
      new_expr->set_ref_expr(expr, true);
      new_expr->set_result_type(expr->get_result_type());
      new_expr->set_expr_level(stmt->get_current_level());
      if (OB_FAIL(new_expr->add_flag(IS_CONST))) {
        LOG_WARN("failed to add flag", K(ret));
      } else if (OB_FAIL(new_expr->add_flag(IS_DYNAMIC_PARAM))) {
        LOG_WARN("failed to add flag", K(ret));
      } else if (OB_FAIL(new_expr->add_flag(IS_ONETIME))) {
        LOG_WARN("failed to add flag", K(ret));
      } else if (OB_FAIL(stmt->add_onetime_expr(new_expr))) {
        LOG_WARN("failed to add onetime expr", K(ret));
      } else if (OB_FAIL(old_exprs.push_back(expr))) {
        LOG_WARN("failed to add old expr", K(ret));
      } else if (OB_FAIL(new_exprs.push_back(new_expr))) {
        LOG_WARN("failed to push back new expr", K(ret));
      } else {
        expr = new_expr;
        is_happened = true;
      }
    }
  } else if (expr->has_flag(CNT_SUB_QUERY)) {
    for (int64_t i = 0; OB_SUCC(ret) && i < expr->get_param_count(); ++i) {
      if (OB_FAIL(create_onetime_param(stmt,
                                       expr->get_param_expr(i),
                                       onetime_list,
                                       old_exprs,
                                       new_exprs,
                                       is_happened))) {
        LOG_WARN("failed to create onetime param", K(ret));
      }
    }
  }
  return ret;
}

int ObTransformPostProcess::is_non_correlated_exists_for_onetime(ObRawExpr *expr,
1173 1174
                                                                 bool &is_non_correlated_exists_for_onetime,
                                                                 int64_t &ref_count)
W
wangzelin.wzl 已提交
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
{
  int ret = OB_SUCCESS;
  is_non_correlated_exists_for_onetime = false;
  if (OB_ISNULL(expr)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid expr", K(ret));
  } else if (T_OP_EXISTS == expr->get_expr_type() ||
             T_OP_NOT_EXISTS == expr->get_expr_type()) {
    bool has_ref_assign_user_var = false;
    ObQueryRefRawExpr *query_ref_expr = static_cast<ObQueryRefRawExpr*>(expr->get_param_expr(0));
    if (OB_FAIL(ObOptimizerUtil::check_subquery_has_ref_assign_user_var(
                                 expr, has_ref_assign_user_var))) {
      LOG_WARN("failed to check subquery has ref assign user var", K(ret));
    } else if (!has_ref_assign_user_var && query_ref_expr->get_param_count() == 0) {
      is_non_correlated_exists_for_onetime = true;
1190
      ref_count = query_ref_expr->get_ref_count();
W
wangzelin.wzl 已提交
1191 1192 1193 1194 1195 1196 1197 1198
    }
  }
  return ret;
}

}
}