Skip to content

Commit 85c7005

Browse files
author
xuda
committed
update
1 parent 6e8cfaf commit 85c7005

19 files changed

+509
-15
lines changed

deps/3rd/libevent

Submodule libevent updated 203 files
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2+
miniob is licensed under Mulan PSL v2.
3+
*/
4+
5+
#include "sql/executor/drop_table_executor.h"
6+
7+
#include "common/log/log.h"
8+
#include "event/session_event.h"
9+
#include "event/sql_event.h"
10+
#include "sql/stmt/stmt.h"
11+
#include "sql/stmt/drop_table_stmt.h"
12+
#include "sql/executor/sql_result.h"
13+
#include "storage/db/db.h"
14+
15+
RC DropTableExecutor::execute(SQLStageEvent *sql_event)
16+
{
17+
RC rc = RC::SUCCESS;
18+
19+
SessionEvent *session_event = sql_event->session_event();
20+
SqlResult *sql_result = session_event->sql_result();
21+
Stmt *stmt = sql_event->stmt();
22+
23+
if (stmt == nullptr || stmt->type() != StmtType::DROP_TABLE) {
24+
LOG_WARN("invalid stmt for DropTableExecutor");
25+
rc = RC::INVALID_ARGUMENT;
26+
sql_result->set_return_code(rc);
27+
return rc;
28+
}
29+
30+
auto *drop_stmt = static_cast<DropTableStmt *>(stmt);
31+
Db *db = drop_stmt->db();
32+
33+
if (db == nullptr) {
34+
LOG_WARN("db is null in DropTableStmt");
35+
rc = RC::INVALID_ARGUMENT;
36+
sql_result->set_return_code(rc);
37+
return rc;
38+
}
39+
40+
const std::string &table_name = drop_stmt->table_name();
41+
42+
rc = db->drop_table(table_name.c_str());
43+
if (rc != RC::SUCCESS) {
44+
LOG_WARN("failed to drop table %s, rc=%s", table_name.c_str(), strrc(rc));
45+
}
46+
47+
sql_result->set_return_code(rc);
48+
return rc;
49+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include "common/sys/rc.h"
4+
5+
class SQLStageEvent;
6+
7+
/**
8+
* 执行 DROP TABLE 的 executor
9+
*/
10+
class DropTableExecutor
11+
{
12+
public:
13+
static RC execute(SQLStageEvent *sql_event);
14+
};

src/observer/sql/operator/logical_operator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum class LogicalOperatorType
3838
JOIN, ///< 连接
3939
INSERT, ///< 插入
4040
DELETE, ///< 删除,删除可能会有子查询
41+
UPDATE, ///< 更新
4142
EXPLAIN, ///< 查看执行计划
4243
GROUP_BY, ///< 分组
4344
};

src/observer/sql/operator/physical_operator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ string physical_operator_type_name(PhysicalOperatorType type)
2525
case PhysicalOperatorType::PREDICATE: return "PREDICATE";
2626
case PhysicalOperatorType::INSERT: return "INSERT";
2727
case PhysicalOperatorType::DELETE: return "DELETE";
28+
case PhysicalOperatorType::UPDATE: return "UPDATE";
2829
case PhysicalOperatorType::PROJECT: return "PROJECT";
2930
case PhysicalOperatorType::STRING_LIST: return "STRING_LIST";
3031
case PhysicalOperatorType::HASH_GROUP_BY: return "HASH_GROUP_BY";

src/observer/sql/operator/physical_operator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ enum class PhysicalOperatorType
4848
STRING_LIST,
4949
DELETE,
5050
INSERT,
51+
UPDATE,
5152
SCALAR_GROUP_BY,
5253
HASH_GROUP_BY,
5354
GROUP_BY_VEC,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2+
miniob is licensed under Mulan PSL v2.
3+
You can use this software according to the terms and conditions of the Mulan PSL v2.
4+
You may obtain a copy of Mulan PSL v2 at:
5+
http://license.coscl.org.cn/MulanPSL2
6+
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7+
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8+
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9+
See the Mulan PSL v2 for more details. */
10+
11+
#pragma once
12+
13+
#include "sql/operator/logical_operator.h"
14+
#include "common/value.h"
15+
#include "storage/table/table.h"
16+
#include "storage/field/field_meta.h"
17+
18+
/**
19+
* @brief 更新逻辑算子
20+
* @ingroup LogicalOperator
21+
*/
22+
class UpdateLogicalOperator : public LogicalOperator
23+
{
24+
public:
25+
UpdateLogicalOperator(Table *table, const FieldMeta *field, Value value)
26+
: table_(table), field_(field), value_(std::move(value))
27+
{}
28+
virtual ~UpdateLogicalOperator() = default;
29+
30+
LogicalOperatorType type() const override { return LogicalOperatorType::UPDATE; }
31+
OpType get_op_type() const override { return OpType::LOGICALUPDATE; }
32+
33+
Table *table() const { return table_; }
34+
const FieldMeta *field_meta() const { return field_; }
35+
Value &value() { return value_; }
36+
37+
private:
38+
Table *table_ = nullptr;
39+
const FieldMeta *field_ = nullptr;
40+
Value value_;
41+
};
42+
43+
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2+
miniob is licensed under Mulan PSL v2.
3+
You can use this software according to the terms and conditions of the Mulan PSL v2.
4+
You may obtain a copy of Mulan PSL v2 at:
5+
http://license.coscl.org.cn/MulanPSL2
6+
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7+
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8+
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9+
See the Mulan PSL v2 for more details. */
10+
11+
//
12+
// Created by MiniOB Update Implementor.
13+
//
14+
15+
#include "sql/operator/update_physical_operator.h"
16+
#include "common/log/log.h"
17+
#include "sql/expr/tuple.h"
18+
#include "storage/table/table.h"
19+
#include "storage/table/table_meta.h"
20+
#include "storage/trx/trx.h"
21+
#include <string.h>
22+
23+
RC UpdatePhysicalOperator::set_value_to_record(char *record_data, const Value &value, const FieldMeta *field)
24+
{
25+
size_t copy_len = field->len();
26+
const size_t data_len = value.length();
27+
if (field->type() == AttrType::CHARS) {
28+
if (copy_len > data_len) {
29+
copy_len = data_len + 1; // include '\0'
30+
}
31+
}
32+
memcpy(record_data + field->offset(), value.data(), copy_len);
33+
return RC::SUCCESS;
34+
}
35+
36+
RC UpdatePhysicalOperator::open(Trx *trx)
37+
{
38+
if (children_.empty()) {
39+
return RC::SUCCESS;
40+
}
41+
42+
trx_ = trx;
43+
44+
unique_ptr<PhysicalOperator> &child = children_[0];
45+
46+
RC rc = child->open(trx);
47+
if (rc != RC::SUCCESS) {
48+
LOG_WARN("failed to open child operator: %s", strrc(rc));
49+
return rc;
50+
}
51+
52+
while (OB_SUCC(rc = child->next())) {
53+
Tuple *tuple = child->current_tuple();
54+
if (nullptr == tuple) {
55+
LOG_WARN("failed to get current tuple");
56+
rc = RC::INTERNAL;
57+
break;
58+
}
59+
60+
RowTuple *row_tuple = static_cast<RowTuple *>(tuple);
61+
Record &record = row_tuple->record();
62+
records_.emplace_back(std::move(record));
63+
}
64+
65+
child->close();
66+
67+
if (rc != RC::SUCCESS && rc != RC::RECORD_EOF) {
68+
return rc;
69+
}
70+
71+
const TableMeta &meta = table_->table_meta();
72+
const int rec_size = meta.record_size();
73+
74+
// delete + insert to update rows
75+
for (Record &old_rec : records_) {
76+
Record new_rec;
77+
rc = new_rec.copy_data(old_rec.data(), rec_size);
78+
if (OB_FAIL(rc)) {
79+
LOG_WARN("failed to copy record. rc=%s", strrc(rc));
80+
return rc;
81+
}
82+
83+
Value write_val = value_;
84+
if (write_val.attr_type() != field_->type()) {
85+
Value casted;
86+
rc = Value::cast_to(write_val, field_->type(), casted);
87+
if (OB_FAIL(rc)) {
88+
LOG_WARN("failed to cast value. rc=%s", strrc(rc));
89+
return rc;
90+
}
91+
write_val = std::move(casted);
92+
}
93+
94+
rc = set_value_to_record(const_cast<char *>(new_rec.data()), write_val, field_);
95+
if (OB_FAIL(rc)) {
96+
LOG_WARN("failed to set value to record. rc=%s", strrc(rc));
97+
return rc;
98+
}
99+
100+
rc = trx_->delete_record(table_, old_rec);
101+
if (OB_FAIL(rc)) {
102+
LOG_WARN("failed to delete record. rc=%s", strrc(rc));
103+
return rc;
104+
}
105+
106+
rc = trx_->insert_record(table_, new_rec);
107+
if (OB_FAIL(rc)) {
108+
LOG_WARN("failed to insert record. rc=%s", strrc(rc));
109+
return rc;
110+
}
111+
}
112+
113+
return RC::SUCCESS;
114+
}
115+
116+
RC UpdatePhysicalOperator::next()
117+
{
118+
return RC::RECORD_EOF;
119+
}
120+
121+
RC UpdatePhysicalOperator::close()
122+
{
123+
return RC::SUCCESS;
124+
}
125+
126+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2+
miniob is licensed under Mulan PSL v2.
3+
You can use this software according to the terms and conditions of the Mulan PSL v2.
4+
You may obtain a copy of Mulan PSL v2 at:
5+
http://license.coscl.org.cn/MulanPSL2
6+
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7+
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8+
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9+
See the Mulan PSL v2 for more details. */
10+
11+
#pragma once
12+
13+
#include "sql/operator/physical_operator.h"
14+
#include "common/value.h"
15+
#include "storage/table/table.h"
16+
#include "storage/field/field_meta.h"
17+
18+
/**
19+
* @brief 物理算子,用于执行update语句
20+
* @ingroup PhysicalOperator
21+
*/
22+
class UpdatePhysicalOperator : public PhysicalOperator
23+
{
24+
public:
25+
UpdatePhysicalOperator(Table *table, const FieldMeta *field, Value value)
26+
: table_(table), field_(field), value_(std::move(value)) {}
27+
28+
virtual ~UpdatePhysicalOperator() = default;
29+
30+
PhysicalOperatorType type() const override { return PhysicalOperatorType::UPDATE; }
31+
32+
OpType get_op_type() const override { return OpType::UPDATE; }
33+
34+
RC open(Trx *trx) override;
35+
RC next() override;
36+
RC close() override;
37+
38+
Tuple *current_tuple() override { return nullptr; }
39+
40+
private:
41+
static RC set_value_to_record(char *record_data, const Value &value, const FieldMeta *field);
42+
43+
private:
44+
Table *table_ = nullptr;
45+
const FieldMeta *field_ = nullptr;
46+
Value value_;
47+
Trx *trx_ = nullptr;
48+
vector<Record> records_;
49+
};
50+
51+

0 commit comments

Comments
 (0)