Skip to content

Commit

Permalink
Merge branch 'update'
Browse files Browse the repository at this point in the history
  • Loading branch information
357447923 committed Sep 13, 2023
2 parents 4574e5f + a7bfd36 commit 26fbfaa
Show file tree
Hide file tree
Showing 25 changed files with 296 additions and 45 deletions.
6 changes: 4 additions & 2 deletions src/observer/common/date.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include "rc.h"

inline bool is_leap_year(int year) {
Expand Down Expand Up @@ -48,7 +47,10 @@ inline std::string date_to_str(int32_t date) {
int day = date % 100;
int month = (date / 100) % 100;
int year = (date / 10000);
sprintf(str, "%4d-%02d-%02d", year, month, day);
if (year > 9999) {
year = 9999;
}
sprintf(str, "%d-%02d-%02d", year, month, day);
return std::move(std::string(str));
}

Expand Down
17 changes: 0 additions & 17 deletions src/observer/sql/executor/update_executor.h

This file was deleted.

1 change: 1 addition & 0 deletions src/observer/sql/operator/logical_operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ enum class LogicalOperatorType
PROJECTION, ///< 投影,就是select
JOIN, ///< 连接
INSERT, ///< 插入
UPDATE, ///< 更新
DELETE, ///< 删除,删除可能会有子查询
EXPLAIN, ///< 查看执行计划
};
Expand Down
1 change: 1 addition & 0 deletions src/observer/sql/operator/physical_operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum class PhysicalOperatorType
CALC,
STRING_LIST,
DELETE,
UPDATE,
INSERT,
};

Expand Down
21 changes: 21 additions & 0 deletions src/observer/sql/operator/update_logical_operator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "sql/operator/update_logical_operator.h"

UpdateLogicalOperator::UpdateLogicalOperator(Table *table, Value value, const char *field_name) {
table_ = table;
value_ = std::move(value);
char *tmp = (char *)malloc(sizeof(char) * (strlen(field_name) + 1));
strcpy(tmp, field_name);
field_name_ = tmp;
}

UpdateLogicalOperator::~UpdateLogicalOperator() {
if (field_name_ != nullptr) {
free(field_name_);
}
}

// UpdateLogicalOperator::~UpdateLogicalOperator() {
// if (update_map_ != nullptr) {
// delete update_map_;
// }
// }
45 changes: 45 additions & 0 deletions src/observer/sql/operator/update_logical_operator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef MINIOB_SQL_OPERATOR_UPDATE_LOGICAL_OPERATOR_H
#define MINIOB_SQL_OPERATOR_UPDATE_LOGICAL_OPERATOR_H

#include "sql/operator/logical_operator.h"
#include "sql/parser/parse_defs.h"
#include <unordered_map>

/**
* @brief 逻辑算子,用于执行delete语句
* @ingroup LogicalOperator
*/
class UpdateLogicalOperator : public LogicalOperator {

public:
UpdateLogicalOperator(Table *table, Value value, const char *field_name);

~UpdateLogicalOperator() override;

LogicalOperatorType type() const override {
return LogicalOperatorType::UPDATE;
}

inline Table * table() const {
return table_;
}

inline Value& value() {
return value_;
}

inline char * field_name() const {
return field_name_;
}
// const Value* find_value_by_field(const FieldMeta& field_meta) {
// return &update_map_->at(field_meta);
// }

private:
Table * table_ = nullptr;
// std::unordered_map<FieldMeta, Value> *update_map_ = nullptr; // TODO 多字段更新
Value value_;
char * field_name_ = nullptr;
};

#endif
69 changes: 69 additions & 0 deletions src/observer/sql/operator/update_physical_operator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "sql/operator/update_physical_operator.h"
#include "storage/trx/trx.h"
#include "storage/record/record.h"

UpdatePhysicalOperator::UpdatePhysicalOperator(Table *table, Value& value, const char * field_name) {
table_ = table;
value_ = value;
char *tmp = (char *)malloc(sizeof(char) * (strlen(field_name) + 1));
strcpy(tmp, field_name);
field_name_ = tmp;
}

UpdatePhysicalOperator::~UpdatePhysicalOperator() {
if (field_name_ != nullptr) {
delete field_name_;
}
}

RC UpdatePhysicalOperator::open(Trx *trx) {
if (children_.empty()) {
return RC::SUCCESS;
}

std::unique_ptr<PhysicalOperator> &child = children_[0];
RC rc = child->open(trx);
if (rc != RC::SUCCESS) {
LOG_WARN("failed to open child operator: %s", strrc(rc));
return rc;
}

trx_ = trx;

return RC::SUCCESS;
}

RC UpdatePhysicalOperator::close() {
if (!children_.empty()) {
children_[0]->close();
}
return RC::SUCCESS;
}

RC UpdatePhysicalOperator::next() {
RC rc = RC::SUCCESS;
if (children_.empty()) {
return RC::RECORD_EOF;
}

PhysicalOperator *child = children_[0].get();
while (RC::SUCCESS == (rc = child->next())) {
Tuple *tuple = child->current_tuple();
if (nullptr == tuple) {
LOG_WARN("failed to get current record: %s", strrc(rc));
return rc;
}

RowTuple *row_tuple = static_cast<RowTuple *>(tuple);
Record &record = row_tuple->record();
const FieldMeta *field = table_->table_meta().field(field_name_);
int offset = field->offset();
rc = trx_->update_record(table_, record, offset, value_);
if (rc != RC::SUCCESS) {
LOG_WARN("failed to delete record: %s", strrc(rc));
return rc;
}
}

return RC::RECORD_EOF;
}
31 changes: 31 additions & 0 deletions src/observer/sql/operator/update_physical_operator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef MINIOB_UPDATE_PHYSICAL_OPERATOR_H
#define MINIOB_UPDATE_PHYSICAL_OPERATOR_H

#include "sql/operator/physical_operator.h"
#include "sql/parser/parse.h"

class UpdatePhysicalOperator : public PhysicalOperator {
public:

UpdatePhysicalOperator(Table *table, Value& value, const char * field_name);

virtual ~UpdatePhysicalOperator() override;

PhysicalOperatorType type() const override {
return PhysicalOperatorType::UPDATE;
}

RC open(Trx *trx) override;
RC next() override;
RC close() override;

Tuple *current_tuple() override { return nullptr; }

private:
Table * table_ = nullptr;
Value value_;
const char * field_name_ = nullptr;
Trx *trx_ = nullptr;
};

#endif
38 changes: 38 additions & 0 deletions src/observer/sql/optimizer/logical_plan_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ See the Mulan PSL v2 for more details. */
#include "sql/operator/join_logical_operator.h"
#include "sql/operator/project_logical_operator.h"
#include "sql/operator/explain_logical_operator.h"
#include "sql/operator/update_logical_operator.h"

#include "sql/stmt/stmt.h"
#include "sql/stmt/calc_stmt.h"
Expand All @@ -32,6 +33,7 @@ See the Mulan PSL v2 for more details. */
#include "sql/stmt/insert_stmt.h"
#include "sql/stmt/delete_stmt.h"
#include "sql/stmt/explain_stmt.h"
#include "sql/stmt/update_stmt.h"

using namespace std;

Expand Down Expand Up @@ -59,6 +61,11 @@ RC LogicalPlanGenerator::create(Stmt *stmt, unique_ptr<LogicalOperator> &logical
rc = create_plan(delete_stmt, logical_operator);
} break;

case StmtType::UPDATE: {
UpdateStmt *update_stmt = static_cast<UpdateStmt *>(stmt);
rc = create_plan(update_stmt, logical_operator);
}break;

case StmtType::EXPLAIN: {
ExplainStmt *explain_stmt = static_cast<ExplainStmt *>(stmt);
rc = create_plan(explain_stmt, logical_operator);
Expand Down Expand Up @@ -167,6 +174,37 @@ RC LogicalPlanGenerator::create_plan(
return RC::SUCCESS;
}

RC LogicalPlanGenerator::create_plan(UpdateStmt *update_stmt, std::unique_ptr<LogicalOperator> &logical_operator) {
Table *table = update_stmt->table();
FilterStmt *filter_stmt = update_stmt->filter_stmt();
// int count = update_stmt->value_amount();

// 找到要修改的字段来生成执行计划
const FieldMeta *field_meta = table->table_meta().field(update_stmt->attribute_name().c_str());
vector<Field> fields;
fields.push_back(Field(table, field_meta));
// for (int i = 0; i < count; i++) {
// }
unique_ptr<LogicalOperator> table_get_oper(new TableGetLogicalOperator(table, fields, false));

// 过滤执行计划
unique_ptr<LogicalOperator> predicate_oper;
RC rc = create_plan(update_stmt->filter_stmt(), predicate_oper);
if (rc != RC::SUCCESS) {
return rc;
}

unique_ptr<LogicalOperator> update_oper(new UpdateLogicalOperator(table, *update_stmt->values(), update_stmt->attribute_name().c_str()));
if (predicate_oper) {
predicate_oper->add_child(std::move(table_get_oper));
update_oper->add_child(std::move(predicate_oper));
}else {
update_oper->add_child(std::move(table_get_oper));
}
logical_operator = std::move(update_oper);
return RC::SUCCESS;
}

RC LogicalPlanGenerator::create_plan(
DeleteStmt *delete_stmt, unique_ptr<LogicalOperator> &logical_operator)
{
Expand Down
2 changes: 2 additions & 0 deletions src/observer/sql/optimizer/logical_plan_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class CalcStmt;
class SelectStmt;
class FilterStmt;
class InsertStmt;
class UpdateStmt;
class DeleteStmt;
class ExplainStmt;
class LogicalOperator;
Expand All @@ -40,6 +41,7 @@ class LogicalPlanGenerator
RC create_plan(SelectStmt *select_stmt, std::unique_ptr<LogicalOperator> &logical_operator);
RC create_plan(FilterStmt *filter_stmt, std::unique_ptr<LogicalOperator> &logical_operator);
RC create_plan(InsertStmt *insert_stmt, std::unique_ptr<LogicalOperator> &logical_operator);
RC create_plan(UpdateStmt *update_stmt, std::unique_ptr<LogicalOperator> &logical_operator);
RC create_plan(DeleteStmt *delete_stmt, std::unique_ptr<LogicalOperator> &logical_operator);
RC create_plan(ExplainStmt *explain_stmt, std::unique_ptr<LogicalOperator> &logical_operator);
};
26 changes: 26 additions & 0 deletions src/observer/sql/optimizer/physical_plan_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ See the Mulan PSL v2 for more details. */
#include "sql/operator/project_physical_operator.h"
#include "sql/operator/insert_logical_operator.h"
#include "sql/operator/insert_physical_operator.h"
#include "sql/operator/update_logical_operator.h"
#include "sql/operator/update_physical_operator.h"
#include "sql/operator/delete_logical_operator.h"
#include "sql/operator/delete_physical_operator.h"
#include "sql/operator/explain_logical_operator.h"
Expand Down Expand Up @@ -62,6 +64,10 @@ RC PhysicalPlanGenerator::create(LogicalOperator &logical_operator, unique_ptr<P
return create_plan(static_cast<InsertLogicalOperator &>(logical_operator), oper);
} break;

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

case LogicalOperatorType::DELETE: {
return create_plan(static_cast<DeleteLogicalOperator &>(logical_operator), oper);
} break;
Expand Down Expand Up @@ -213,6 +219,26 @@ RC PhysicalPlanGenerator::create_plan(InsertLogicalOperator &insert_oper, unique
return RC::SUCCESS;
}

RC PhysicalPlanGenerator::create_plan(UpdateLogicalOperator &update_oper, std::unique_ptr<PhysicalOperator> &oper) {
vector<unique_ptr<LogicalOperator>> &child_opers = update_oper.children();

unique_ptr<PhysicalOperator> child_physical_oper;
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;
}
}
oper = unique_ptr<PhysicalOperator>(new UpdatePhysicalOperator(update_oper.table(), update_oper.value(), update_oper.field_name()));
if (child_physical_oper) {
oper->add_child(std::move(child_physical_oper));
}
return RC::SUCCESS;
}

RC PhysicalPlanGenerator::create_plan(DeleteLogicalOperator &delete_oper, unique_ptr<PhysicalOperator> &oper)
{
vector<unique_ptr<LogicalOperator>> &child_opers = delete_oper.children();
Expand Down
2 changes: 2 additions & 0 deletions src/observer/sql/optimizer/physical_plan_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class DeleteLogicalOperator;
class ExplainLogicalOperator;
class JoinLogicalOperator;
class CalcLogicalOperator;
class UpdateLogicalOperator;

/**
* @brief 物理计划生成器
Expand All @@ -48,6 +49,7 @@ class PhysicalPlanGenerator
RC create_plan(PredicateLogicalOperator &logical_oper, std::unique_ptr<PhysicalOperator> &oper);
RC create_plan(ProjectLogicalOperator &logical_oper, std::unique_ptr<PhysicalOperator> &oper);
RC create_plan(InsertLogicalOperator &logical_oper, std::unique_ptr<PhysicalOperator> &oper);
RC create_plan(UpdateLogicalOperator &logcial_oper, std::unique_ptr<PhysicalOperator> &oper);
RC create_plan(DeleteLogicalOperator &logical_oper, std::unique_ptr<PhysicalOperator> &oper);
RC create_plan(ExplainLogicalOperator &logical_oper, std::unique_ptr<PhysicalOperator> &oper);
RC create_plan(JoinLogicalOperator &logical_oper, std::unique_ptr<PhysicalOperator> &oper);
Expand Down
3 changes: 2 additions & 1 deletion src/observer/sql/stmt/filter_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ FilterStmt::~FilterStmt()
filter_units_.clear();
}

// TODO 把convert_value抽取到存放类型转换相关方法的文件中
/**
* @brief 类型转换,把value中的类型转为meta对应的类型
*
*/
static RC convert_value(Value& value, const FieldMeta *meta) {
RC convert_value(Value& value, const FieldMeta *meta) {
AttrType type = value.attr_type();
AttrType field_type = meta->type();
RC rc = RC::SUCCESS;
Expand Down
Loading

0 comments on commit 26fbfaa

Please sign in to comment.