delete_physical_operator.cpp 1.9 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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/6/27.
//

#include "common/log/log.h"
羽飞's avatar
羽飞 已提交
16
#include "sql/operator/delete_physical_operator.h"
羽飞's avatar
羽飞 已提交
17
#include "storage/record/record.h"
羽飞's avatar
羽飞 已提交
18
#include "storage/common/table.h"
羽飞's avatar
羽飞 已提交
19
#include "storage/trx/trx.h"
羽飞's avatar
羽飞 已提交
20 21
#include "sql/stmt/delete_stmt.h"

羽飞's avatar
羽飞 已提交
22
RC DeletePhysicalOperator::open()
羽飞's avatar
羽飞 已提交
23
{
羽飞's avatar
羽飞 已提交
24 25
  if (children_.empty()) {
    return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
26 27
  }

羽飞's avatar
羽飞 已提交
28
  std::unique_ptr<PhysicalOperator> &child = children_[0];
羽飞's avatar
羽飞 已提交
29 30 31 32 33 34
  RC rc = child->open();
  if (rc != RC::SUCCESS) {
    LOG_WARN("failed to open child operator: %s", strrc(rc));
    return rc;
  }

羽飞's avatar
羽飞 已提交
35 36 37 38 39 40 41 42 43
  return RC::SUCCESS;
}

RC DeletePhysicalOperator::next()
{
  RC rc = RC::SUCCESS;
  if (children_.empty()) {
    return RC::RECORD_EOF;
  }
L
Longda Feng 已提交
44

羽飞's avatar
羽飞 已提交
45
  PhysicalOperator *child = children_[0].get();
羽飞's avatar
羽飞 已提交
46
  while (RC::SUCCESS == (rc = child->next())) {
羽飞's avatar
羽飞 已提交
47 48
    Tuple *tuple = child->current_tuple();
    if (nullptr == tuple) {
羽飞's avatar
羽飞 已提交
49 50 51 52
      LOG_WARN("failed to get current record: %s", strrc(rc));
      return rc;
    }

羽飞's avatar
羽飞 已提交
53 54
    RowTuple *row_tuple = static_cast<RowTuple *>(tuple);
    Record &record = row_tuple->record();
羽飞's avatar
羽飞 已提交
55
    rc = table_->delete_record(trx_, &record);
羽飞's avatar
羽飞 已提交
56 57 58 59 60 61 62 63 64
    if (rc != RC::SUCCESS) {
      LOG_WARN("failed to delete record: %s", strrc(rc));
      return rc;
    }
  }

  return RC::RECORD_EOF;
}

羽飞's avatar
羽飞 已提交
65
RC DeletePhysicalOperator::close()
羽飞's avatar
羽飞 已提交
66
{
羽飞's avatar
羽飞 已提交
67 68 69
  if (!children_.empty()) {
    children_[0]->close();
  }
羽飞's avatar
羽飞 已提交
70 71
  return RC::SUCCESS;
}