physical_plan_generator.cpp 9.8 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
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/14.
//

羽飞's avatar
羽飞 已提交
15 16
#include <utility>

羽飞's avatar
羽飞 已提交
17 18 19 20 21 22 23 24
#include "sql/optimizer/physical_plan_generator.h"
#include "sql/operator/table_get_logical_operator.h"
#include "sql/operator/table_scan_physical_operator.h"
#include "sql/operator/index_scan_physical_operator.h"
#include "sql/operator/predicate_logical_operator.h"
#include "sql/operator/predicate_physical_operator.h"
#include "sql/operator/project_logical_operator.h"
#include "sql/operator/project_physical_operator.h"
羽飞's avatar
羽飞 已提交
25 26
#include "sql/operator/insert_logical_operator.h"
#include "sql/operator/insert_physical_operator.h"
羽飞's avatar
羽飞 已提交
27 28 29 30 31 32 33 34 35 36 37
#include "sql/operator/delete_logical_operator.h"
#include "sql/operator/delete_physical_operator.h"
#include "sql/operator/explain_logical_operator.h"
#include "sql/operator/explain_physical_operator.h"
#include "sql/operator/join_logical_operator.h"
#include "sql/operator/join_physical_operator.h"
#include "sql/expr/expression.h"
#include "common/log/log.h"

using namespace std;

羽飞's avatar
羽飞 已提交
38
RC PhysicalPlanGenerator::create(LogicalOperator &logical_operator, unique_ptr<PhysicalOperator> &oper)
羽飞's avatar
羽飞 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
{
  RC rc = RC::SUCCESS;

  switch (logical_operator.type()) {
    case LogicalOperatorType::TABLE_GET: {
      return create_plan(static_cast<TableGetLogicalOperator &>(logical_operator), oper);
    } break;

    case LogicalOperatorType::PREDICATE: {
      return create_plan(static_cast<PredicateLogicalOperator &>(logical_operator), oper);
    } break;

    case LogicalOperatorType::PROJECTION: {
      return create_plan(static_cast<ProjectLogicalOperator &>(logical_operator), oper);
    } break;

羽飞's avatar
羽飞 已提交
55 56 57 58
    case LogicalOperatorType::INSERT: {
      return create_plan(static_cast<InsertLogicalOperator &>(logical_operator), oper);
    } break;

羽飞's avatar
羽飞 已提交
59 60 61 62 63 64 65 66 67 68 69
    case LogicalOperatorType::DELETE: {
      return create_plan(static_cast<DeleteLogicalOperator &>(logical_operator), oper);
    } break;

    case LogicalOperatorType::EXPLAIN: {
      return create_plan(static_cast<ExplainLogicalOperator &>(logical_operator), oper);
    } break;

    case LogicalOperatorType::JOIN: {
      return create_plan(static_cast<JoinLogicalOperator &>(logical_operator), oper);
    } break;
L
Longda Feng 已提交
70

羽飞's avatar
羽飞 已提交
71 72 73 74 75 76 77
    default: {
      return RC::INVALID_ARGUMENT;
    }
  }
  return rc;
}

羽飞's avatar
羽飞 已提交
78
RC PhysicalPlanGenerator::create_plan(TableGetLogicalOperator &table_get_oper, unique_ptr<PhysicalOperator> &oper)
羽飞's avatar
羽飞 已提交
79
{
羽飞's avatar
羽飞 已提交
80
  vector<unique_ptr<Expression>> &predicates = table_get_oper.predicates();
羽飞's avatar
羽飞 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93
  // 看看是否有可以用于索引查找的表达式
  Table *table = table_get_oper.table();

  Index *index = nullptr;
  ValueExpr *value_expr = nullptr;
  for (auto &expr : predicates) {
    if (expr->type() == ExprType::COMPARISON) {
      auto comparison_expr = static_cast<ComparisonExpr *>(expr.get());
      // 简单处理,就找等值查询
      if (comparison_expr->comp() != EQUAL_TO) {
        continue;
      }

羽飞's avatar
羽飞 已提交
94 95
      unique_ptr<Expression> &left_expr = comparison_expr->left();
      unique_ptr<Expression> &right_expr = comparison_expr->right();
羽飞's avatar
羽飞 已提交
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
      // 左右比较的一边最少是一个值
      if (left_expr->type() != ExprType::VALUE && right_expr->type() != ExprType::VALUE) {
        continue;
      }

      FieldExpr *field_expr = nullptr;
      if (left_expr->type() == ExprType::FIELD) {
        ASSERT(right_expr->type() == ExprType::VALUE, "right expr should be a value expr while left is field expr");
        field_expr = static_cast<FieldExpr *>(left_expr.get());
        value_expr = static_cast<ValueExpr *>(right_expr.get());
      } else if (right_expr->type() == ExprType::FIELD) {
        ASSERT(left_expr->type() == ExprType::VALUE, "left expr should be a value expr while right is a field expr");
        field_expr = static_cast<FieldExpr *>(right_expr.get());
        value_expr = static_cast<ValueExpr *>(left_expr.get());
      }

      if (field_expr == nullptr) {
        continue;
      }

      const Field &field = field_expr->field();
      index = table->find_index_by_field(field.field_name());
      if (nullptr != index) {
        break;
      }
    }
  }

  if (index != nullptr) {
    ASSERT(value_expr != nullptr, "got an index but value expr is null ?");

    const TupleCell &tuple_cell = value_expr->get_tuple_cell();
L
Longda Feng 已提交
128
    IndexScanPhysicalOperator *index_scan_oper = new IndexScanPhysicalOperator(
羽飞's avatar
羽飞 已提交
129 130 131 132
          table, index, table_get_oper.readonly(), 
          &tuple_cell, true /*left_inclusive*/, 
          &tuple_cell, true /*right_inclusive*/);
          
羽飞's avatar
羽飞 已提交
133
    index_scan_oper->set_predicates(std::move(predicates));
羽飞's avatar
羽飞 已提交
134
    oper = unique_ptr<PhysicalOperator>(index_scan_oper);
羽飞's avatar
羽飞 已提交
135 136
    LOG_TRACE("use index scan");
  } else {
羽飞's avatar
羽飞 已提交
137
    auto table_scan_oper = new TableScanPhysicalOperator(table, table_get_oper.readonly());
羽飞's avatar
羽飞 已提交
138
    table_scan_oper->set_predicates(std::move(predicates));
羽飞's avatar
羽飞 已提交
139
    oper = unique_ptr<PhysicalOperator>(table_scan_oper);
羽飞's avatar
羽飞 已提交
140 141
    LOG_TRACE("use table scan");
  }
L
Longda Feng 已提交
142

羽飞's avatar
羽飞 已提交
143 144 145
  return RC::SUCCESS;
}

羽飞's avatar
羽飞 已提交
146
RC PhysicalPlanGenerator::create_plan(PredicateLogicalOperator &pred_oper, unique_ptr<PhysicalOperator> &oper)
羽飞's avatar
羽飞 已提交
147
{
羽飞's avatar
羽飞 已提交
148
  vector<unique_ptr<LogicalOperator>> &children_opers = pred_oper.children();
羽飞's avatar
羽飞 已提交
149 150 151 152
  ASSERT(children_opers.size() == 1, "predicate logical operator's sub oper number should be 1");

  LogicalOperator &child_oper = *children_opers.front();

羽飞's avatar
羽飞 已提交
153
  unique_ptr<PhysicalOperator> child_phy_oper;
羽飞's avatar
羽飞 已提交
154 155 156 157 158
  RC rc = create(child_oper, child_phy_oper);
  if (rc != RC::SUCCESS) {
    LOG_WARN("failed to create child operator of predicate operator. rc=%s", strrc(rc));
    return rc;
  }
L
Longda Feng 已提交
159

羽飞's avatar
羽飞 已提交
160
  vector<unique_ptr<Expression>> &expressions = pred_oper.expressions();
羽飞's avatar
羽飞 已提交
161 162
  ASSERT(expressions.size() == 1, "predicate logical operator's children should be 1");

羽飞's avatar
羽飞 已提交
163 164 165
  unique_ptr<Expression> expression = std::move(expressions.front());
  oper = unique_ptr<PhysicalOperator>(new PredicatePhysicalOperator(std::move(expression)));
  oper->add_child(std::move(child_phy_oper));
羽飞's avatar
羽飞 已提交
166 167 168
  return rc;
}

羽飞's avatar
羽飞 已提交
169
RC PhysicalPlanGenerator::create_plan(ProjectLogicalOperator &project_oper, unique_ptr<PhysicalOperator> &oper)
羽飞's avatar
羽飞 已提交
170
{
羽飞's avatar
羽飞 已提交
171
  vector<unique_ptr<LogicalOperator>> &child_opers = project_oper.children();
L
Longda Feng 已提交
172

羽飞's avatar
羽飞 已提交
173
  unique_ptr<PhysicalOperator> child_phy_oper;
羽飞's avatar
羽飞 已提交
174 175 176 177 178 179 180 181 182 183 184 185

  RC rc = RC::SUCCESS;
  if (!child_opers.empty()) {
    LogicalOperator *child_oper = child_opers.front().get();
    rc = create(*child_oper, child_phy_oper);
    if (rc != RC::SUCCESS) {
      LOG_WARN("failed to create project logical operator's child physical operator. rc=%s", strrc(rc));
      return rc;
    }
  }

  ProjectPhysicalOperator *project_operator = new ProjectPhysicalOperator;
羽飞's avatar
羽飞 已提交
186
  const vector<Field> &project_fields = project_oper.fields();
L
Longda Feng 已提交
187
  for (const Field &field : project_fields) {
羽飞's avatar
羽飞 已提交
188 189 190 191
    project_operator->add_projection(field.table(), field.meta());
  }

  if (child_phy_oper) {
羽飞's avatar
羽飞 已提交
192
    project_operator->add_child(std::move(child_phy_oper));
羽飞's avatar
羽飞 已提交
193 194
  }

羽飞's avatar
羽飞 已提交
195
  oper = unique_ptr<PhysicalOperator>(project_operator);
羽飞's avatar
羽飞 已提交
196 197 198 199 200

  LOG_TRACE("create a project physical operator");
  return rc;
}

羽飞's avatar
羽飞 已提交
201 202 203 204
RC PhysicalPlanGenerator::create_plan(InsertLogicalOperator &insert_oper, unique_ptr<PhysicalOperator> &oper)
{
  Table *table = insert_oper.table();
  vector<Value> &values = insert_oper.values();
羽飞's avatar
羽飞 已提交
205
  InsertPhysicalOperator *insert_phy_oper = new InsertPhysicalOperator(table, std::move(values));
羽飞's avatar
羽飞 已提交
206 207 208 209 210
  oper.reset(insert_phy_oper);
  return RC::SUCCESS;
}

RC PhysicalPlanGenerator::create_plan(DeleteLogicalOperator &delete_oper, unique_ptr<PhysicalOperator> &oper)
羽飞's avatar
羽飞 已提交
211
{
羽飞's avatar
羽飞 已提交
212
  vector<unique_ptr<LogicalOperator>> &child_opers = delete_oper.children();
羽飞's avatar
羽飞 已提交
213

羽飞's avatar
羽飞 已提交
214
  unique_ptr<PhysicalOperator> child_physical_oper;
羽飞's avatar
羽飞 已提交
215 216 217 218 219 220 221 222 223 224 225

  RC rc = RC::SUCCESS;
  if (!child_opers.empty()) {
    LogicalOperator *child_oper = child_opers.front().get();
    rc = create(*child_oper, child_physical_oper);
    if (rc != RC::SUCCESS) {
      LOG_WARN("failed to create physical operator. rc=%s", strrc(rc));
      return rc;
    }
  }

羽飞's avatar
羽飞 已提交
226
  oper = unique_ptr<PhysicalOperator>(new DeletePhysicalOperator(delete_oper.table()));
羽飞's avatar
羽飞 已提交
227 228

  if (child_physical_oper) {
羽飞's avatar
羽飞 已提交
229
    oper->add_child(std::move(child_physical_oper));
羽飞's avatar
羽飞 已提交
230 231 232 233
  }
  return rc;
}

羽飞's avatar
羽飞 已提交
234
RC PhysicalPlanGenerator::create_plan(ExplainLogicalOperator &explain_oper, unique_ptr<PhysicalOperator> &oper)
羽飞's avatar
羽飞 已提交
235
{
羽飞's avatar
羽飞 已提交
236
  vector<unique_ptr<LogicalOperator>> &child_opers = explain_oper.children();
羽飞's avatar
羽飞 已提交
237 238

  RC rc = RC::SUCCESS;
羽飞's avatar
羽飞 已提交
239 240 241
  unique_ptr<PhysicalOperator> explain_physical_oper(new ExplainPhysicalOperator);
  for (unique_ptr<LogicalOperator> &child_oper : child_opers) {
    unique_ptr<PhysicalOperator> child_physical_oper;
羽飞's avatar
羽飞 已提交
242 243 244 245 246 247
    rc = create(*child_oper, child_physical_oper);
    if (rc != RC::SUCCESS) {
      LOG_WARN("failed to create child physical operator. rc=%s", strrc(rc));
      return rc;
    }

羽飞's avatar
羽飞 已提交
248
    explain_physical_oper->add_child(std::move(child_physical_oper));
羽飞's avatar
羽飞 已提交
249 250
  }

羽飞's avatar
羽飞 已提交
251
  oper = std::move(explain_physical_oper);
羽飞's avatar
羽飞 已提交
252 253 254
  return rc;
}

羽飞's avatar
羽飞 已提交
255
RC PhysicalPlanGenerator::create_plan(JoinLogicalOperator &join_oper, unique_ptr<PhysicalOperator> &oper)
羽飞's avatar
羽飞 已提交
256 257 258
{
  RC rc = RC::SUCCESS;

羽飞's avatar
羽飞 已提交
259
  vector<unique_ptr<LogicalOperator>> &child_opers = join_oper.children();
羽飞's avatar
羽飞 已提交
260 261 262 263 264
  if (child_opers.size() != 2) {
    LOG_WARN("join operator should have 2 children, but have %d", child_opers.size());
    return RC::INTERNAL;
  }

羽飞's avatar
羽飞 已提交
265
  unique_ptr<PhysicalOperator> join_physical_oper(new NestedLoopJoinPhysicalOperator);
羽飞's avatar
羽飞 已提交
266
  for (auto &child_oper : child_opers) {
羽飞's avatar
羽飞 已提交
267
    unique_ptr<PhysicalOperator> child_physical_oper;
羽飞's avatar
羽飞 已提交
268 269 270 271 272 273
    rc = create(*child_oper, child_physical_oper);
    if (rc != RC::SUCCESS) {
      LOG_WARN("failed to create physical child oper. rc=%s", strrc(rc));
      return rc;
    }

羽飞's avatar
羽飞 已提交
274
    join_physical_oper->add_child(std::move(child_physical_oper));
羽飞's avatar
羽飞 已提交
275 276
  }

羽飞's avatar
羽飞 已提交
277
  oper = std::move(join_physical_oper);
羽飞's avatar
羽飞 已提交
278 279
  return rc;
}