rewriter.cpp 1.9 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
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
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
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 PSL v2 for more details. */

//
// Created by Wangyunlai on 2022/12/29.
//

#include "sql/optimizer/rewriter.h"
#include "sql/optimizer/expression_rewriter.h"
#include "sql/optimizer/predicate_rewrite.h"
#include "sql/optimizer/predicate_pushdown_rewriter.h"
#include "sql/operator/logical_operator.h"

Rewriter::Rewriter()
{
  rewrite_rules_.emplace_back(new ExpressionRewriter);
  rewrite_rules_.emplace_back(new PredicateRewriteRule);
  rewrite_rules_.emplace_back(new PredicatePushdownRewriter);
}

RC Rewriter::rewrite(std::unique_ptr<LogicalOperator> &oper, bool &change_made)
{
  RC rc = RC::SUCCESS;

  change_made = false;
L
Longda Feng 已提交
33
  for (std::unique_ptr<RewriteRule> &rule : rewrite_rules_) {
羽飞's avatar
羽飞 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    bool sub_change_made = false;
    rc = rule->rewrite(oper, sub_change_made);
    if (rc != RC::SUCCESS) {
      LOG_WARN("failed to rewrite logical operator. rc=%s", strrc(rc));
      return rc;
    }

    if (sub_change_made && !change_made) {
      change_made = true;
    }
  }

  if (rc != RC::SUCCESS) {
    return rc;
  }
L
Longda Feng 已提交
49

羽飞's avatar
羽飞 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  std::vector<std::unique_ptr<LogicalOperator>> &child_opers = oper->children();
  for (auto &child_oper : child_opers) {
    bool sub_change_made = false;
    rc = this->rewrite(child_oper, sub_change_made);
    if (rc != RC::SUCCESS) {
      LOG_WARN("failed to rewrite child oper. rc=%s", strrc(rc));
      return rc;
    }

    if (sub_change_made && !change_made) {
      change_made = true;
    }
  }
  return rc;
}