conjunction_simplification_rule.cpp 2.9 KB
Newer Older
羽飞's avatar
羽飞 已提交
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
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
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/26.
//

#include "common/log/log.h"
#include "sql/optimizer/conjunction_simplification_rule.h"
#include "sql/expr/expression.h"

RC try_to_get_bool_constant(std::unique_ptr<Expression> &expr, bool &constant_value)
{
  if (expr->type() == ExprType::VALUE && expr->value_type() == BOOLEANS) {
    auto value_expr = static_cast<ValueExpr *>(expr.get());
    constant_value = value_expr->get_tuple_cell().get_boolean();
    return RC::SUCCESS;
  }
  return RC::GENERIC_ERROR;
}
RC ConjunctionSimplificationRule::rewrite(std::unique_ptr<Expression> &expr, bool &change_made)
{
  RC rc = RC::SUCCESS;
  if (expr->type() != ExprType::CONJUNCTION) {
    return rc;
  }

  change_made = false;
  auto conjunction_expr = static_cast<ConjunctionExpr *>(expr.get());
  std::vector<std::unique_ptr<Expression>> &child_exprs = conjunction_expr->children();
  // 先看看有没有能够直接去掉的表达式。比如AND时恒为true的表达式可以删除
  // 或者是否可以直接计算出当前表达式的值。比如AND时,如果有一个表达式为false,那么整个表达式就是false
L
Longda Feng 已提交
40
  for (auto iter = child_exprs.begin(); iter != child_exprs.end();) {
羽飞's avatar
羽飞 已提交
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
    bool constant_value = false;
    rc = try_to_get_bool_constant(*iter, constant_value);
    if (rc != RC::SUCCESS) {
      rc = RC::SUCCESS;
      ++iter;
      continue;
    }

    if (conjunction_expr->conjunction_type() == ConjunctionExpr::Type::AND) {
      if (constant_value == true) {
        child_exprs.erase(iter);
      } else {
        // always be false
        std::unique_ptr<Expression> child_expr = std::move(child_exprs.front());
        child_exprs.clear();
        expr = std::move(child_expr);
        return rc;
      }
    } else {
      // conjunction_type == OR
      if (constant_value == true) {
        // always be true
        std::unique_ptr<Expression> child_expr = std::move(child_exprs.front());
        child_exprs.clear();
        expr = std::move(child_expr);
        return rc;
      } else {
        child_exprs.erase(iter);
      }
    }
  }
  if (child_exprs.size() == 1) {
    LOG_TRACE("conjunction expression has only 1 child");
    std::unique_ptr<Expression> child_expr = std::move(child_exprs.front());
    child_exprs.clear();
    expr = std::move(child_expr);

    change_made = true;
  }

  return rc;
}