Skip to content

Commit 4a3c640

Browse files
committed
Merge remote-tracking branch 'gitlab/test' into masttf
2 parents a1472b1 + a0ffab6 commit 4a3c640

32 files changed

Lines changed: 798 additions & 438 deletions

.github/workflows/format.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ name: Format C++ Code
22

33
on:
44
push:
5-
branches:
6-
- main
75
pull_request:
86
types:
97
- opened

CMakeLists.txt

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,11 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
66
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
77

88
set(CMAKE_CXX_STANDARD 17)
9+
# set(CMAKE_CXX_FLAGS "-Wall -O2 -g -ggdb3")
10+
set(CMAKE_CXX_FLAGS "-Wall -Ofast -march=native -flto")
911

10-
# 设置编译器警告选项
11-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
12-
13-
# 设置不同构建类型的编译选项
14-
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
15-
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -ggdb3 -DDEBUG")
16-
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g -DNDEBUG")
17-
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
18-
19-
# 如果没有指定构建类型,默认使用 Release (O3 优化)
20-
if(NOT CMAKE_BUILD_TYPE)
21-
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
22-
message(STATUS "构建类型未指定,默认使用 Release 模式 (O3 优化)")
23-
endif()
24-
25-
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
26-
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
27-
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
28-
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
12+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g")
13+
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g")
2914

3015

3116
enable_testing()

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ target_link_libraries(rmdb parser execution readline pthread planner analyze com
2222

2323
# unit_test
2424
add_executable(unit_test unit_test.cpp)
25-
target_link_libraries(unit_test storage lru_replacer record gtest_main) # add gtest
25+
target_link_libraries(unit_test storage lru_replacer shared_lru_replacer record gtest_main) # add gtest

src/analyze/analyze.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -669,10 +669,10 @@ void Analyze::check_clause(const std::vector<std::string> &tab_names, std::vecto
669669
} else { // rhs_type == ConditionRhsType::RHS_EXPR
670670
// 检查表达式内部是否都是数值类型
671671
// TODO: 暂时表达式涉及的列只能是左边表中的列
672-
std::vector<ColMeta> ltable_cols;
673-
get_all_cols({cond.lhs_col.tab_name}, ltable_cols);
674-
CheckArithExprType(cond.rhs_expr->lhs, ltable_cols);
675-
CheckArithExprType(cond.rhs_expr->rhs, ltable_cols);
672+
// std::vector<ColMeta> ltable_cols;
673+
// get_all_cols({cond.lhs_col.tab_name}, ltable_cols);
674+
// CheckArithExprType(cond.rhs_expr->lhs, ltable_cols);
675+
// CheckArithExprType(cond.rhs_expr->rhs, ltable_cols);
676676
// 假设算术表达式的结果总是数值类型 (例如 FLOAT 用于比较)
677677
// 更精确的类型推断可以后续添加 (例如 INT + INT = INT, INT + FLOAT = FLOAT)
678678
rhs_type = ColType::TYPE_FLOAT; // Assume float for comparison simplicity

src/common/config.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ static constexpr int64_t TXN_START_ID = 1LL << 62; // first txn id
3535
static constexpr int64_t INVALID_TS = -1; // invalid log sequence number
3636
static constexpr int HEADER_PAGE_ID = 0; // the header page id
3737
static constexpr int PAGE_SIZE = 4096; // size of a data page in byte 4KB
38-
static constexpr int BUFFER_POOL_SIZE = 65536; // size of buffer pool 256MB
39-
// static constexpr int BUFFER_POOL_SIZE = 262144; // size of buffer pool 1GB
38+
// static constexpr int BUFFER_POOL_SIZE = 1024 * 256 / 4; // size of buffer pool 256MB
39+
static constexpr int BUFFER_POOL_SIZE = 1024 * 1024 / 4; // size of buffer pool 1GB
40+
static constexpr int BUFFER_POOL_INSTANCE_SIZE = 16; // size of buffer pool instance
4041
static constexpr int LOG_BUFFER_SIZE = (1024 * PAGE_SIZE); // size of a log buffer in byte
4142
static constexpr int BUCKET_SIZE = 50; // size of extendible hash bucket
4243

src/execution/execution_common.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ std::unique_ptr<RmRecord> mvcc_get_record(const Rid &rid, Context *context_, RmF
106106
return rec;
107107
}
108108

109-
bool mvcc_insert_index(const TabMeta &tab_, RmRecord &rec, Rid rid, Context *context_, TransactionManager *txn_mgr,
110-
SmManager *sm_manager) {
109+
bool mvcc_insert_index(const TabMeta &tab_, std::unique_ptr<RmRecord> &rec, Rid rid, Context *context_,
110+
TransactionManager *txn_mgr, SmManager *sm_manager) {
111111
RmFileHandle *fh_ = sm_manager->fhs_.at(tab_.name).get();
112112
std::vector<std::unique_ptr<char[]>> inserted_keys; // 记录已插入的键值
113113
inserted_keys.reserve(tab_.indexes.size()); // 预分配空间以提高性能
@@ -118,7 +118,7 @@ bool mvcc_insert_index(const TabMeta &tab_, RmRecord &rec, Rid rid, Context *con
118118
auto key = std::make_unique<char[]>(index.col_tot_len);
119119
int offset = 0;
120120
for (size_t j = 0; j < static_cast<size_t>(index.col_num); ++j) {
121-
memcpy(key.get() + offset, rec.data + index.cols[j].offset, index.cols[j].len);
121+
memcpy(key.get() + offset, rec->data + index.cols[j].offset, index.cols[j].len);
122122
offset += index.cols[j].len;
123123
}
124124
// 检查索引项是否已存在
@@ -165,13 +165,13 @@ bool mvcc_insert_index(const TabMeta &tab_, RmRecord &rec, Rid rid, Context *con
165165
* @param col 列元数据
166166
* @return 提取出的 Value
167167
*/
168-
Value GetColumnValue(const RmRecord &record, const ColMeta &col) {
168+
Value GetColumnValue(const std::unique_ptr<RmRecord> &record, const ColMeta &col) {
169169
Value val;
170-
val.set_col_data(col.type, record.data + col.offset, col.len);
170+
val.set_col_data(col.type, record->data + col.offset, col.len);
171171
return val;
172172
}
173173

174-
Value EvaluateExpr(const ExprTerm &term, const RmRecord &record, const std::vector<ColMeta> &cols) {
174+
Value EvaluateExpr(const ExprTerm &term, const std::unique_ptr<RmRecord> &record, const std::vector<ColMeta> &cols) {
175175
TRACE_FUNCTION
176176
switch (term.term_type) {
177177
case TermType::VALUE: {
@@ -180,11 +180,16 @@ Value EvaluateExpr(const ExprTerm &term, const RmRecord &record, const std::vect
180180
}
181181
case TermType::COLUMN: {
182182
// 查找列并返回值
183+
if (record == nullptr) {
184+
throw InternalError("Record is null in record evaluation for column: " + term.col.to_string());
185+
}
183186
for (const auto &col_meta : cols) {
184187
if (col_meta.name == term.col.col_name && col_meta.tab_name == term.col.tab_name) {
185188
return GetColumnValue(record, col_meta);
186189
}
187190
}
191+
// 如果没有找到对应的列,抛出异常
192+
throw InternalError("Column not found: " + term.col.to_string() + " in record evaluation");
188193
}
189194
case TermType::EXPR: {
190195
// 递归计算左右操作数

src/execution/execution_common.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ std::unique_ptr<RmRecord> mvcc_get_record(const Rid &rid, Context *context_, RmF
2525

2626
bool get_lock_and_check_conflict(Transaction *txn, TransactionManager *txn_mgr, RmFileHandle *fh_, const Rid &rid);
2727

28-
bool mvcc_insert_index(const TabMeta &tab_, RmRecord &rec, Rid rid, Context *context_, TransactionManager *txn_mgr,
29-
SmManager *sm_manager);
28+
bool mvcc_insert_index(const TabMeta &tab_, std::unique_ptr<RmRecord> &rec, Rid rid, Context *context_,
29+
TransactionManager *txn_mgr, SmManager *sm_manager);
3030

3131
/**
3232
* @brief 递归地计算算术表达式的值
@@ -35,6 +35,6 @@ bool mvcc_insert_index(const TabMeta &tab_, RmRecord &rec, Rid rid, Context *con
3535
* @param cols 表的所有列元数据
3636
* @return 计算得到的 Value
3737
*/
38-
Value EvaluateExpr(const ExprTerm &term, const RmRecord &record, const std::vector<ColMeta> &cols);
38+
Value EvaluateExpr(const ExprTerm &term, const std::unique_ptr<RmRecord> &record, const std::vector<ColMeta> &cols);
3939

40-
Value GetColumnValue(const RmRecord &record, const ColMeta &col);
40+
Value GetColumnValue(const std::unique_ptr<RmRecord> &record, const ColMeta &col);

src/execution/execution_group.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,9 @@ class GroupExecutor : public AbstractExecutor {
187187
rhs_agg_types.emplace_back(cond.rhs_col.agg_type);
188188
break;
189189
case ConditionRhsType::RHS_EXPR:
190-
// TODO: 实现 EvaluateAggrExpr 来处理包含聚合的表达式
191-
// rhs_val = EvaluateAggrExpr(ExprTerm(cond.rhs_expr), rec, rec_cols);
192-
// 暂时抛出错误,因为 EvaluateExpr 不适用于聚合上下文
193-
throw RMDBError(
194-
"Arithmetic expressions involving aggregates in HAVING clause RHS not yet fully supported.");
195190
break;
196191
default:
197-
throw RMDBError("Unsupported ConditionRhsType in HAVING clause");
192+
throw InternalError("Unsupported ConditionRhsType in HAVING clause");
198193
}
199194
});
200195
std::vector<Value> rhs_aggr_vals = get_aggr_values(rec_cols, records, rhs_cols, rhs_agg_types);
@@ -207,8 +202,11 @@ class GroupExecutor : public AbstractExecutor {
207202
rhs_vals[i] = rhs_aggr_vals[j]; // 使用聚合计算的值
208203
j++;
209204
break;
205+
case ConditionRhsType::RHS_EXPR:
206+
rhs_vals[i] = EvaluateExpr(ExprTerm(conds[i].rhs_expr), nullptr, rec_cols);
207+
break;
210208
default:
211-
throw RMDBError("Unsupported ConditionRhsType in HAVING clause");
209+
throw InternalError("Unsupported ConditionRhsType in HAVING clause");
212210
}
213211
}
214212
// 遍历所有条件,检查是否满足
@@ -245,14 +243,10 @@ class GroupExecutor : public AbstractExecutor {
245243
rhs_val = get_aggr_value(rec_cols, rec, cond.rhs_col, cond.rhs_col.agg_type);
246244
break;
247245
case ConditionRhsType::RHS_EXPR:
248-
// TODO: 实现 EvaluateAggrExpr 来处理包含聚合的表达式
249-
// rhs_val = EvaluateAggrExpr(ExprTerm(cond.rhs_expr), rec, rec_cols);
250-
// 暂时抛出错误,因为 EvaluateExpr 不适用于聚合上下文
251-
throw RMDBError(
252-
"Arithmetic expressions involving aggregates in HAVING clause RHS not yet fully supported.");
246+
rhs_val = EvaluateExpr(ExprTerm(cond.rhs_expr), nullptr, rec_cols);
253247
break;
254248
default:
255-
throw RMDBError("Unsupported ConditionRhsType in HAVING clause");
249+
throw InternalError("Unsupported ConditionRhsType in HAVING clause");
256250
}
257251
// 比较左右两侧的值
258252
return Value::compare(lhs_val, rhs_val, cond.op);

src/execution/executor_abstract.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ class AbstractExecutor {
136136
* @return true表示满足所有条件
137137
* @throw ExecutionError 当评估过程出错时
138138
*/
139-
bool eval_conds(const std::vector<ColMeta> &rec_cols, const std::vector<Condition> &conds, const RmRecord *rec) {
139+
bool eval_conds(const std::vector<ColMeta> &rec_cols, const std::vector<Condition> &conds,
140+
const std::unique_ptr<RmRecord> &rec) {
140141
for (const auto &cond : conds) {
141142
if (!eval_cond(rec_cols, cond, rec)) {
142143
return false;
@@ -154,7 +155,7 @@ class AbstractExecutor {
154155
* @throw IncompatibleTypeError 当类型不兼容时
155156
* @throw InternalError 当遇到非法操作符时
156157
*/
157-
bool eval_cond(const std::vector<ColMeta> &rec_cols, const Condition &cond, const RmRecord *rec) {
158+
bool eval_cond(const std::vector<ColMeta> &rec_cols, const Condition &cond, const std::unique_ptr<RmRecord> &rec) {
158159
TRACE_FUNCTION
159160
auto lhs_col = get_col(rec_cols, cond.lhs_col);
160161
char *lhs_data = rec->data + lhs_col->offset;
@@ -180,7 +181,7 @@ class AbstractExecutor {
180181
case ConditionRhsType::RHS_EXPR:
181182
// 计算表达式的值
182183
// 注意:需要将 ArithExpr 包装在 ExprTerm 中传递
183-
rhs_expr_val = EvaluateExpr(ExprTerm(cond.rhs_expr), *rec, rec_cols);
184+
rhs_expr_val = EvaluateExpr(ExprTerm(cond.rhs_expr), rec, rec_cols);
184185
// 检查计算结果的 raw 是否有效
185186
rhs_expr_val.raw.reset(); // 确保 raw 被正确初始化
186187
rhs_expr_val.init_raw(); // 初始化 raw 缓冲区

src/execution/executor_index_scan.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class IndexScanExecutor : public AbstractExecutor {
200200
while (!is_end()) {
201201
rid_ = scan_->rid();
202202
auto rec = fh_->get_record(rid_, context_);
203-
if (eval_conds(cols_, fed_conds_, rec.get())) {
203+
if (eval_conds(cols_, fed_conds_, rec)) {
204204
return;
205205
}
206206
scan_->next();
@@ -222,7 +222,7 @@ class IndexScanExecutor : public AbstractExecutor {
222222
while (!scan_->is_end()) {
223223
rid_ = scan_->rid();
224224
auto rec = fh_->get_record(rid_, context_);
225-
if (eval_conds(cols_, fed_conds_, rec.get())) {
225+
if (eval_conds(cols_, fed_conds_, rec)) {
226226
return;
227227
}
228228
scan_->next();

0 commit comments

Comments
 (0)