From b80fe7dba149a1c28309d47f9bde223b63e1cb93 Mon Sep 17 00:00:00 2001
From: wym <1057857414@qq.com>
Date: Sun, 25 Jun 2023 07:18:57 -0700
Subject: [PATCH 1/2] 1

---
 src/execution/executor_projection.h | 43 +++++++++++++++++------------
 1 file changed, 26 insertions(+), 17 deletions(-)

diff --git a/src/execution/executor_projection.h b/src/execution/executor_projection.h
index 7610136..e4b8177 100755
--- a/src/execution/executor_projection.h
+++ b/src/execution/executor_projection.h
@@ -15,9 +15,10 @@ See the Mulan PSL v2 for more details. */
 #include "index/ix.h"
 #include "system/sm.h"
 
-class ProjectionExecutor : public AbstractExecutor {
-   private:
-    std::unique_ptr<AbstractExecutor> prev_;        // 投影节点的儿子节点
+class ProjectionExecutor : public AbstractExecutor
+{
+private:
+    std::unique_ptr<AbstractExecutor> prev_; // 投影节点的儿子节点
     /*
     在查询执行计划中,投影操作通常是位于查询计划的顶层,它的作用是选择和投影出特定的列或字段。
     子节点是指在查询计划中位于投影操作之前的其他操作节点,它们的输出结果将作为投影操作的输入。
@@ -25,18 +26,20 @@ class ProjectionExecutor : public AbstractExecutor {
     子节点可以是扫描操作(如表扫描或索引扫描)、过滤操作(如谓词过滤)或其他转换操作(如连接、聚合等)。
     子节点的输出将作为投影操作的输入,投影操作将从子节点的输出中选择指定的列,并进行投影操作,生成最终的结果。
     */
-    std::vector<ColMeta> cols_;                     // 需要投影的字段
-    size_t len_;                                    // 字段总长度
+    std::vector<ColMeta> cols_; // 需要投影的字段
+    size_t len_;                // 字段总长度
     std::vector<size_t> sel_idxs_;
-    std::unique_ptr<RmRecord> curr_record_;                  
+    std::unique_ptr<RmRecord> curr_record_;
 
-   public:
-    ProjectionExecutor(std::unique_ptr<AbstractExecutor> prev, const std::vector<TabCol> &sel_cols) {
+public:
+    ProjectionExecutor(std::unique_ptr<AbstractExecutor> prev, const std::vector<TabCol> &sel_cols)
+    {
         prev_ = std::move(prev);
 
         size_t curr_offset = 0;
         auto &prev_cols = prev_->cols();
-        for (auto &sel_col : sel_cols) {
+        for (auto &sel_col : sel_cols)
+        {
             auto pos = get_col(prev_cols, sel_col);
             sel_idxs_.push_back(pos - prev_cols.begin());
             auto col = *pos;
@@ -47,36 +50,42 @@ class ProjectionExecutor : public AbstractExecutor {
         len_ = curr_offset;
     }
 
-    void beginTuple() override {
+    void beginTuple() override
+    {
         prev_->beginTuple();
     }
 
-    void nextTuple() override {
+    void nextTuple() override
+    {
         prev_->nextTuple();
         curr_record_ = Next();
     }
 
-    std::unique_ptr<RmRecord> Next() override {
+    std::unique_ptr<RmRecord> Next() override
+    {
 
-        if (curr_record_) {
+        if (curr_record_)
+        {
             return std::move(curr_record_);
         }
 
         std::unique_ptr<RmRecord> record = prev_->Next();
 
-        if (record) {
+        if (record)
+        {
             // 创建一个向量来存储投影后的记录数据
-            char* data = new char[len_];  // 分配内存
+            char *data = new char[len_]; // 分配内存
 
             // 遍历选定的列,并复制对应的数据
-            for (size_t i = 0; i < sel_idxs_.size(); ++i) {
+            for (size_t i = 0; i < sel_idxs_.size(); ++i)
+            {
                 size_t sel_idx = sel_idxs_[i];
                 size_t offset = cols_[i].offset;
                 std::memcpy(data + offset, record->data + prev_->cols()[sel_idx].offset, cols_[i].len);
             }
 
             // 使用投影后的数据创建一个新的 RmRecord,并返回它
-            return std::make_unique<RmRecord>(len_,data);
+            return std::make_unique<RmRecord>(len_, data);
         }
         return nullptr;
     }
-- 
GitLab


From b027e8d67209b5f7746085b780e25561fa562f8e Mon Sep 17 00:00:00 2001
From: wym <1057857414@qq.com>
Date: Sun, 25 Jun 2023 07:22:38 -0700
Subject: [PATCH 2/2] 111

---
 src/execution/executor_seq_scan.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/execution/executor_seq_scan.h b/src/execution/executor_seq_scan.h
index bf987d3..f1ed70a 100755
--- a/src/execution/executor_seq_scan.h
+++ b/src/execution/executor_seq_scan.h
@@ -108,6 +108,7 @@ class SeqScanExecutor : public AbstractExecutor {
 
     SmManager *sm_manager_;
     std::vector<ColMeta> cols_check_;         // 条件语句中所有用到列的列的元数据信息
+    std::unordered_map<std::string, ColMeta> ColName_to_ColMeta_;
 
    public:
     SeqScanExecutor(SmManager *sm_manager, std::string tab_name, std::vector<Condition> conds, Context *context) {
@@ -119,27 +120,26 @@ class SeqScanExecutor : public AbstractExecutor {
         cols_ = tab.cols;
         len_ = cols_.back().offset + cols_.back().len;
         //hashmap
-        std::unordered_map<std::string, ColMeta> ColName_to_ColMeta_=tab.ColName_to_ColMeta;
+        ColName_to_ColMeta_=tab.ColName_to_ColMeta;
         context_ = context;
 
         fed_conds_ = conds_;//后续查询优化可以从fedcond入手!!!!!!!!!!!!!!!!!!!
-
         //初始化条件语句中所有用到列的列的元数据信息
-        int i=0;
-        for (const auto& condition : fed_conds_) {
+        int con_size=fed_conds_.size();
+        for (int loop=0;loop<con_size;loop++) {
+            auto temp_con=fed_conds_[loop];
 
             // 检查左操作数是否为列操作数
-            if (!condition.lhs_col.tab_name.empty() && !condition.lhs_col.col_name.empty()) {
+            if (!temp_con.lhs_col.tab_name.empty() && !temp_con.lhs_col.col_name.empty()) {
                 // 查找colmeta
-                ColMeta temp=ColName_to_ColMeta_[condition.lhs_col.col_name];
+                ColMeta temp=ColName_to_ColMeta_[temp_con.lhs_col.col_name];
                 // 将元数据对象添加到列的元数据向量中
                 cols_check_.push_back(temp);
-        }
-
+            }
             // 检查右操作数是否为列操作数
-            if (!condition.is_rhs_val && !condition.rhs_col.tab_name.empty() && !condition.rhs_col.col_name.empty()) {
+            if (!temp_con.is_rhs_val && !temp_con.rhs_col.tab_name.empty() && !temp_con.rhs_col.col_name.empty()) {
                 // 查找colmeta
-                ColMeta temp=ColName_to_ColMeta_[condition.rhs_col.col_name];
+                ColMeta temp=ColName_to_ColMeta_[temp_con.rhs_col.col_name];
                 // 将元数据对象添加到列的元数据向量中
                 cols_check_.push_back(temp);
             }
-- 
GitLab