explain_physical_operator.cpp 3.1 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/27.
//

#include <sstream>
#include "sql/operator/explain_physical_operator.h"
#include "common/log/log.h"

using namespace std;

RC ExplainPhysicalOperator::open()
{
  ASSERT(children_.size() == 1, "explain must has 1 child");
  return RC::SUCCESS;
}

RC ExplainPhysicalOperator::close()
{
  for (std::unique_ptr<PhysicalOperator> &child_oper : children_) {
    child_oper->close();
  }
  return RC::SUCCESS;
}

RC ExplainPhysicalOperator::next()
{
  if (!physical_plan_.empty()) {
    return RC::RECORD_EOF;
  }
L
Longda Feng 已提交
40

羽飞's avatar
羽飞 已提交
41 42
  stringstream ss;
  ss << "OPERATOR(NAME)\n";
L
Longda Feng 已提交
43

羽飞's avatar
羽飞 已提交
44 45 46 47 48
  int level = 0;
  std::vector<bool> ends;
  ends.push_back(true);
  const auto children_size = static_cast<int>(children_.size());
  for (int i = 0; i < children_size - 1; i++) {
L
Longda Feng 已提交
49
    to_string(ss, children_[i].get(), level, false /*last_child*/, ends);
羽飞's avatar
羽飞 已提交
50 51
  }
  if (children_size > 0) {
L
Longda Feng 已提交
52
    to_string(ss, children_[children_size - 1].get(), level, true /*last_child*/, ends);
羽飞's avatar
羽飞 已提交
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
  }

  physical_plan_ = ss.str();

  std::vector<TupleCell> cells;
  TupleCell cell;
  cell.set_string(physical_plan_.c_str());
  cells.emplace_back(cell);
  tuple_.set_cells(cells);
  return RC::SUCCESS;
}

Tuple *ExplainPhysicalOperator::current_tuple()
{
  return &tuple_;
}

/**
 * 递归打印某个算子
 * @param os 结果输出到这里
 * @param oper 将要打印的算子
 * @param level 当前算子在第几层
 * @param last_child 当前算子是否是当前兄弟节点中最后一个节点
 * @param ends 表示当前某个层级上的算子,是否已经没有其它的节点,以判断使用什么打印符号
 */
L
Longda Feng 已提交
78 79
void ExplainPhysicalOperator::to_string(
    std::ostream &os, PhysicalOperator *oper, int level, bool last_child, std::vector<bool> &ends)
羽飞's avatar
羽飞 已提交
80
{
L
Longda Feng 已提交
81
  for (int i = 0; i < level - 1; i++) {
羽飞's avatar
羽飞 已提交
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
    if (ends[i]) {
      os << "  ";
    } else {
      os << "│ ";
    }
  }
  if (level > 0) {
    if (last_child) {
      os << "└─";
      ends[level - 1] = true;
    } else {
      os << "├─";
    }
  }

  os << oper->name();
  std::string param = oper->param();
  if (!param.empty()) {
    os << "(" << param << ")";
  }
  os << '\n';

  if (static_cast<int>(ends.size()) < level + 2) {
    ends.resize(level + 2);
  }
  ends[level + 1] = false;

  std::vector<std::unique_ptr<PhysicalOperator>> &children = oper->children();
  const auto size = static_cast<int>(children.size());
  for (auto i = 0; i < size - 1; i++) {
L
Longda Feng 已提交
112
    to_string(os, children[i].get(), level + 1, false /*last_child*/, ends);
羽飞's avatar
羽飞 已提交
113 114
  }
  if (size > 0) {
L
Longda Feng 已提交
115
    to_string(os, children[size - 1].get(), level + 1, true /*last_child*/, ends);
羽飞's avatar
羽飞 已提交
116 117
  }
}