From 772f6d3eb7420c6445e2d82699449d30839cc017 Mon Sep 17 00:00:00 2001
From: leijsen <20215135@stud.tjut.edu.cn>
Date: Tue, 25 Jun 2024 19:46:16 +0800
Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E4=BF=AE=E5=A4=8D=E6=9D=A1?=
 =?UTF-8?q?=E4=BB=B6=E6=9F=A5=E8=AF=A2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/execution/execution_conddep.h | 200 ++++++++++++++++--------------
 src/execution/executor_seq_scan.h |  75 ++++++-----
 2 files changed, 151 insertions(+), 124 deletions(-)

diff --git a/src/execution/execution_conddep.h b/src/execution/execution_conddep.h
index 0b821fd..01998d5 100644
--- a/src/execution/execution_conddep.h
+++ b/src/execution/execution_conddep.h
@@ -1,19 +1,18 @@
 #pragma once
-#include "common/common.h"
 #include "execution_defs.h"
+#include "common/common.h"
 #include "index/ix.h"
 #include "system/sm.h"
 
 class ConditionDependedExecutor {
-   protected:
-    std::string tab_name_;
-    SmManager *sm_manager_;
-
-   public:
-    ConditionDependedExecutor() = default;
-    ConditionDependedExecutor(std::string tab_name, SmManager *sm_manager) : tab_name_(tab_name), sm_manager_(sm_manager) {}
-
-    Value get_record_value(const RmRecord &record, int offset, int len, ColType type) {
+    protected:
+        std::string tab_name_;
+        SmManager *sm_manager_;
+    public:
+        ConditionDependedExecutor() = default;
+        ConditionDependedExecutor(std::string tab_name, SmManager *sm_manager) : tab_name_(tab_name), sm_manager_(sm_manager) {}
+
+    Value get_record_value(const RmRecord &record, int offset, int len, ColType type){
         Value val;
         val.type = type;
 
@@ -22,49 +21,49 @@ class ConditionDependedExecutor {
         } else if (type == TYPE_FLOAT) {
             val.set_float(*(float *)(record.data + offset));
         } else if (type == TYPE_STRING) {
-            val.set_str(std::string(record.data + offset));
-        }
-         else if (type == TYPE_DATETIME) {
+            val.set_str(std::string(record.data + offset, len));
+        } else if (type == TYPE_DATETIME) {
             uint64_t tmp;
             memcpy((char*)&tmp, record.data + offset, len);
             val.set_datetime(tmp);
         } else if (type == TYPE_BIGINT) {
             int64_t tmp;
             memcpy((char*)&tmp, record.data + offset, len);
-            val.set_bigint(tmp);
+            val.set_bigint(tmp); 
         } else {
              throw InvalidTypeError();
         }
 
         return val;
     }
-
+    
     Value get_record_value(const RmRecord &record, const TabCol &col) {
         Value val;
 
         auto col_meta = sm_manager_->db_.get_table(tab_name_).get_col(col.col_name)[0];
 
         val.type = col_meta.type;
-
+        
         if (col_meta.type == TYPE_INT) {
             val.set_int(*(int *)(record.data + col_meta.offset));
         } else if (col_meta.type == TYPE_FLOAT) {
             val.set_float(*(float *)(record.data + col_meta.offset));
         } else if (col_meta.type == TYPE_STRING) {
-
+            
             int offset = col_meta.offset;
             int len = col_meta.len;
 
-            val.set_str(std::string(record.data + offset));
+            val.set_str(std::string(record.data + offset, len));
         } else if (col_meta.type == TYPE_DATETIME) {
             uint64_t tmp;
-            memcpy((char *)&tmp, record.data + col_meta.offset, col_meta.len);
+            memcpy((char*)&tmp, record.data + col_meta.offset, col_meta.len);
             val.set_datetime(tmp);
         } else if (col_meta.type == TYPE_BIGINT) {
             int64_t tmp;
-            memcpy((char *)&tmp, record.data + col_meta.offset, col_meta.len);
+            memcpy((char*)&tmp, record.data + col_meta.offset, col_meta.len);
             val.set_bigint(tmp);
-        } else {
+        }
+        else {
             throw InvalidTypeError();
         }
 
@@ -74,28 +73,28 @@ class ConditionDependedExecutor {
     }
 
     // 从Record中取出某一列的Value
-    Value get_record_value(const std::unique_ptr<RmRecord> &record, const ColMeta &col_meta) const {
+    Value get_record_value(const std::unique_ptr<RmRecord> &record, const ColMeta& col_meta) const {
         Value val;
         val.type = col_meta.type;
-
-        if (col_meta.type == TYPE_INT) {
+  
+        if(col_meta.type == TYPE_INT) {
             val.set_int(*(int *)(record->data + col_meta.offset));
-        } else if (col_meta.type == TYPE_FLOAT) {
+        }else if(col_meta.type == TYPE_FLOAT) {
             val.set_float(*(float *)(record->data + col_meta.offset));
-        } else if (col_meta.type == TYPE_STRING) {
+        }else if(col_meta.type == TYPE_STRING) {
             int offset = col_meta.offset;
             int len = col_meta.len;
 
-            val.set_str(std::string(record->data + offset));
-        } else if (col_meta.type == TYPE_BIGINT) {
+            val.set_str(std::string(record->data + offset, len));
+        }else if(col_meta.type == TYPE_BIGINT) {
             int64_t tmp;
-            memcpy((char *)&tmp, record->data + col_meta.offset, col_meta.len);
+            memcpy((char*)&tmp, record->data + col_meta.offset, col_meta.len);
             val.set_bigint(tmp);
-        } else if (col_meta.type == TYPE_DATETIME) {
+        }else if(col_meta.type == TYPE_DATETIME) {
             uint64_t tmp;
-            memcpy((char *)&tmp, record->data + col_meta.offset, col_meta.len);
+            memcpy((char*)&tmp, record->data + col_meta.offset, col_meta.len);
             val.set_datetime(tmp);
-        } else {
+        }else {
             throw InvalidTypeError();
         }
         val.init_raw(col_meta.len);
@@ -111,114 +110,115 @@ class ConditionDependedExecutor {
         return true;
     }
 
-    bool check_cond(Value left, Value right, CompOp op) {
+    bool check_cond(Value left, Value right, CompOp op){
 
         double cp_res = 0;
         if (left.type == TYPE_INT && right.type == TYPE_INT) {
-            std::cout << "left.int_val: " << left.int_val << " right.int_val: " << right.int_val << std::endl;
-            if (left.int_val > right.int_val) {
+            std::cout<<"left.int_val: "<<left.int_val<<" right.int_val: "<<right.int_val<<std::endl;
+            if (left.int_val > right.int_val){
                 cp_res = 1;
-            } else if (left.int_val < right.int_val) {
+            } else if (left.int_val < right.int_val){
                 cp_res = -1;
             } else {
                 cp_res = 0;
             }
         } else if (left.type == TYPE_FLOAT && right.type == TYPE_FLOAT) {
-            std::cout << "left.float_val: " << left.float_val << " right.float_val: " << right.float_val << std::endl;
-
-            if (left.float_val > right.float_val) {
+            std::cout<<"left.float_val: "<<left.float_val<<" right.float_val: "<<right.float_val<<std::endl;
+            
+            if (left.float_val > right.float_val){
                 cp_res = 1;
-            } else if (left.float_val < right.float_val) {
+            } else if (left.float_val < right.float_val){
                 cp_res = -1;
             } else {
                 cp_res = 0;
             }
         } else if (left.type == TYPE_STRING && right.type == TYPE_STRING) {
 
-            std::cout << "left.str_val: " << left.str_val << " right.str_val: " << right.str_val << std::endl;
+            std::cout<<"left.str_val: "<<left.str_val<<" right.str_val: "<<right.str_val<<std::endl;
 
             cp_res = left.str_val.compare(right.str_val);
 
-        } else if (left.type == TYPE_DATETIME && right.type == TYPE_DATETIME) {
-            std::cout << "left.datetime_val: " << left.datetime_val.encode_to_string() << " right.datetime_val: " << right.datetime_val.encode_to_string();
+        } else if (left.type==TYPE_DATETIME && right.type == TYPE_DATETIME) {
+            std::cout<<"left.datetime_val: "<<left.datetime_val.encode_to_string()<<" right.datetime_val: "<<right.datetime_val.encode_to_string();
 
-            cp_res = left.datetime_val.encode() - right.datetime_val.encode();
+            cp_res = left.datetime_val.encode()-right.datetime_val.encode();
         } else if (left.type == TYPE_BIGINT && right.type == TYPE_BIGINT) {
-            std::cout << "left.bigint_val: " << left.bigint_val << " right.bigint_val: " << right.bigint_val << std::endl;
-
-            if (left.bigint_val > right.bigint_val) {
+            std::cout<<"left.bigint_val: "<<left.bigint_val<<" right.bigint_val: "<<right.bigint_val<<std::endl;
+            
+            if (left.bigint_val > right.bigint_val){
                 cp_res = 1;
-            } else if (left.bigint_val < right.bigint_val) {
+            } else if (left.bigint_val < right.bigint_val){
                 cp_res = -1;
             } else {
                 cp_res = 0;
             }
-        }
+        } 
         // float&int float&bigint int&float int&bigint bigint&float bigint&int
         else if (left.type == TYPE_INT && right.type == TYPE_FLOAT) {
-            std::cout << "left.int_val: " << left.int_val << " right.float_val: " << right.float_val << std::endl;
-
-            if (left.int_val > right.float_val) {
+            std::cout<<"left.int_val: "<<left.int_val<<" right.float_val: "<<right.float_val<<std::endl;
+            
+            if (left.int_val > right.float_val){
                 cp_res = 1;
-            } else if (left.int_val < right.float_val) {
+            } else if (left.int_val < right.float_val){
                 cp_res = -1;
             } else {
                 cp_res = 0;
             }
         } else if (left.type == TYPE_FLOAT && right.type == TYPE_INT) {
-            std::cout << "left.float_val: " << left.float_val << " right.int_val: " << right.int_val << std::endl;
-            if (left.float_val > right.int_val) {
+            std::cout<<"left.float_val: "<<left.float_val<<" right.int_val: "<<right.int_val<<std::endl;
+            if (left.float_val > right.int_val){
                 cp_res = 1;
-            } else if (left.float_val < right.int_val) {
+            } else if (left.float_val < right.int_val){
                 cp_res = -1;
             } else {
                 cp_res = 0;
             }
         } else if (left.type == TYPE_INT && right.type == TYPE_BIGINT) {
-            std::cout << "left.int_val: " << left.int_val << " right.bigint_val: " << right.bigint_val << std::endl;
-
-            if (left.int_val > right.bigint_val) {
+            std::cout<<"left.int_val: "<<left.int_val<<" right.bigint_val: "<<right.bigint_val<<std::endl;
+            
+            if (left.int_val > right.bigint_val){
                 cp_res = 1;
-            } else if (left.int_val < right.bigint_val) {
+            } else if (left.int_val < right.bigint_val){
                 cp_res = -1;
             } else {
                 cp_res = 0;
             }
         } else if (left.type == TYPE_BIGINT && right.type == TYPE_INT) {
-            std::cout << "left.bigint_val: " << left.bigint_val << " right.int_val: " << right.int_val << std::endl;
-
-            if (left.bigint_val > right.int_val) {
+            std::cout<<"left.bigint_val: "<<left.bigint_val<<" right.int_val: "<<right.int_val<<std::endl;
+            
+            if (left.bigint_val > right.int_val){
                 cp_res = 1;
-            } else if (left.bigint_val < right.int_val) {
+            } else if (left.bigint_val < right.int_val){
                 cp_res = -1;
             } else {
                 cp_res = 0;
             }
         } else if (left.type == TYPE_BIGINT && right.type == TYPE_FLOAT) {
-            std::cout << "left.bigint_val: " << left.bigint_val << " right.float_val: " << right.float_val << std::endl;
-
-            if (left.bigint_val > right.float_val) {
+            std::cout<<"left.bigint_val: "<<left.bigint_val<<" right.float_val: "<<right.float_val<<std::endl;
+            
+            if (left.bigint_val > right.float_val){
                 cp_res = 1;
-            } else if (left.bigint_val < right.float_val) {
+            } else if (left.bigint_val < right.float_val){
                 cp_res = -1;
             } else {
                 cp_res = 0;
             }
         } else if (left.type == TYPE_FLOAT && right.type == TYPE_BIGINT) {
-            std::cout << "left.float_val: " << left.float_val << " right.bigint_val: " << right.bigint_val << std::endl;
-
-            if (left.float_val > right.bigint_val) {
+            std::cout<<"left.float_val: "<<left.float_val<<" right.bigint_val: "<<right.bigint_val<<std::endl;
+            
+            if (left.float_val > right.bigint_val){
                 cp_res = 1;
-            } else if (left.float_val < right.bigint_val) {
+            } else if (left.float_val < right.bigint_val){
                 cp_res = -1;
             } else {
                 cp_res = 0;
             }
-        } else {
+        }
+        else {
             throw InternalError("Unexpected value pair field type");
         }
 
-        std::cout << "compare result: " << cp_res << std::endl;
+        std::cout<<"compare result: "<<cp_res<<std::endl;
 
         switch (op) {
             case OP_EQ:
@@ -238,7 +238,7 @@ class ConditionDependedExecutor {
         }
     }
 
-    bool check_cond(Value left, Value right, Condition cond) {
+    bool check_cond(Value left, Value right, Condition cond){
         return check_cond(left, right, cond.op);
     }
 
@@ -259,60 +259,70 @@ class ConditionDependedExecutor {
         return sm_manager_;
     }
 
-    Value *insert_compatible(ColType targetType, Value sourceValue) {
+    // bool check_conds(const std::vector<Condition> &conds, const RmRecord &left, const RmRecord &right) {
+    //     for (auto &cond : conds) {
+    //         if (!check_cond(cond, left, right)) {
+    //             return false;
+    //         }
+    //     }
+    //     return true;
+    // }
+
+
+    Value* insert_compatible(ColType targetType, Value sourceValue){
         // 把source转换成目标类型的Value
-        Value *targetValue = new Value();
+        Value* targetValue = new Value();
 
-        if (targetType == TYPE_INT) {
-            if (sourceValue.type == TYPE_INT) {
+        if (targetType==TYPE_INT){
+            if (sourceValue.type==TYPE_INT){
                 targetValue->set_int(sourceValue.int_val);
                 return targetValue;
-            } else if (sourceValue.type == TYPE_FLOAT) {
+            } else if (sourceValue.type==TYPE_FLOAT){
                 targetValue->set_int(sourceValue.float_val);
                 return targetValue;
-            } else if (sourceValue.type == TYPE_BIGINT) {
+            } else if (sourceValue.type==TYPE_BIGINT){
                 throw TypeOverflowError("INT", std::to_string(sourceValue.bigint_val));
                 return nullptr;
             } else {
                 return nullptr;
             }
-        } else if (targetType == TYPE_FLOAT) {
-            if (sourceValue.type == TYPE_INT) {
+        } else if (targetType==TYPE_FLOAT){
+            if (sourceValue.type==TYPE_INT){
                 targetValue->set_float(sourceValue.int_val);
                 return targetValue;
-            } else if (sourceValue.type == TYPE_FLOAT) {
-                std::cout << "sourceValue.float_val: " << sourceValue.float_val << std::endl;
-
+            } else if (sourceValue.type==TYPE_FLOAT){
+                std::cout<<"sourceValue.float_val: "<<sourceValue.float_val<<std::endl;
+                
                 targetValue->set_float(sourceValue.float_val);
                 return targetValue;
-            } else if (sourceValue.type == TYPE_BIGINT) {
+            } else if (sourceValue.type==TYPE_BIGINT){
                 targetValue->set_float(sourceValue.bigint_val);
                 return targetValue;
             } else {
                 return nullptr;
             }
-        } else if (targetType == TYPE_BIGINT) {
-            if (sourceValue.type == TYPE_INT) {
+        } else if (targetType==TYPE_BIGINT){
+            if (sourceValue.type==TYPE_INT){
                 targetValue->set_bigint(sourceValue.int_val);
                 return targetValue;
-            } else if (sourceValue.type == TYPE_FLOAT) {
+            } else if (sourceValue.type==TYPE_FLOAT){
                 targetValue->set_bigint(sourceValue.float_val);
                 return targetValue;
-            } else if (sourceValue.type == TYPE_BIGINT) {
+            } else if (sourceValue.type==TYPE_BIGINT){
                 targetValue->set_bigint(sourceValue.bigint_val);
                 return targetValue;
             } else {
                 return nullptr;
             }
-        } else if (targetType == TYPE_STRING) {
-            if (sourceValue.type == TYPE_STRING) {
+        } else if (targetType==TYPE_STRING){
+            if (sourceValue.type==TYPE_STRING){
                 targetValue->set_string(sourceValue.str_val);
                 return targetValue;
             } else {
                 return nullptr;
             }
-        } else if (targetType == TYPE_DATETIME) {
-            if (sourceValue.type == TYPE_DATETIME) {
+        } else if (targetType==TYPE_DATETIME){
+            if (sourceValue.type==TYPE_DATETIME){
                 targetValue->set_datetime(sourceValue.datetime_val);
                 return targetValue;
             } else {
diff --git a/src/execution/executor_seq_scan.h b/src/execution/executor_seq_scan.h
index 353e9ce..d1026f4 100644
--- a/src/execution/executor_seq_scan.h
+++ b/src/execution/executor_seq_scan.h
@@ -10,31 +10,31 @@ See the Mulan PSL v2 for more details. */
 
 #pragma once
 
-#include "execution_conddep.h"
 #include "execution_defs.h"
 #include "execution_manager.h"
 #include "executor_abstract.h"
+#include "execution_conddep.h"
 #include "index/ix.h"
 #include "system/sm.h"
 
 class SeqScanExecutor : public AbstractExecutor, public ConditionDependedExecutor {
    private:
-    std::string tab_name_;             // 表的名称
-    std::vector<Condition> conds_;     // scan的条件
-    RmFileHandle *fh_;                 // 表的数据文件句柄
-    std::vector<ColMeta> cols_;        // scan后生成的记录的字段
-    size_t len_;                       // scan后生成的每条记录的长度
-    std::vector<Condition> fed_conds_; // 同conds_,两个字段相同
+    // std::string tab_name_;              // 表的名称
+    std::vector<Condition> conds_;      // scan的条件
+    RmFileHandle *fh_;                  // 表的数据文件句柄
+    std::vector<ColMeta> cols_;         // scan后生成的记录的字段
+    size_t len_;                        // scan后生成的每条记录的长度
+    std::vector<Condition> fed_conds_;  // 同conds_,两个字段相同
 
     Rid rid_;
-    std::unique_ptr<RecScan> scan_; // table_iterator
+    std::unique_ptr<RecScan> scan_;     // table_iterator
 
-    SmManager *sm_manager_;
-    bool is_end_;
+    // SmManager *sm_manager_;
+    bool isend;
 
    public:
-    SeqScanExecutor(SmManager *sm_manager, std::string tab_name, std::vector<Condition> conds, Context *context)
-     : ConditionDependedExecutor(tab_name, sm_manager) {
+    SeqScanExecutor(SmManager *sm_manager, std::string tab_name, std::vector<Condition> conds, Context *context) {
+
         sm_manager_ = sm_manager;
         tab_name_ = std::move(tab_name);
         conds_ = std::move(conds);
@@ -42,7 +42,7 @@ class SeqScanExecutor : public AbstractExecutor, public ConditionDependedExecuto
         fh_ = sm_manager_->fhs_.at(tab_name_).get();
         cols_ = tab.cols;
         len_ = cols_.back().offset + cols_.back().len;
-        is_end_ = false;
+        isend = false;
 
         context_ = context;
 
@@ -51,37 +51,38 @@ class SeqScanExecutor : public AbstractExecutor, public ConditionDependedExecuto
         RmScan *scan = new RmScan(fh_);
         scan_ = std::unique_ptr<RecScan>(scan);
 
-        if (context != nullptr) {
+        if(context != nullptr) {
             context->lock_mgr_->lock_shared_on_table(context->txn_, fh_->GetFd());
         }
     }
 
-    const std::vector<ColMeta> &cols() const override { return cols_; }
-
-    size_t tupleLen() const override {
-        return len_;
-    }
+    size_t tupleLen() const override { return len_; }
 
-    std::string getType() override {
-        return "SeqScan";
-    }
+    std::string getType() override { return "SeqScanExecutor"; }
 
-    bool is_end() const override {
-        return scan_->is_end();
-    }
+    const std::vector<ColMeta> &cols() const override { return cols_; }
 
     void beginTuple() override {
+        // 为scan_赋值
+        // RmScan *scan = new RmScan(fh_);
+        // scan_ = std::unique_ptr<RecScan>(scan);
         scan_->begin();
         auto rec = scan_.get()->rid();
         nextTuple();
 
-        is_end_ = false;
+        isend = false;
     }
 
     void nextTuple() override {
-        for (scan_->next(); !scan_->is_end(); scan_->next()) {
+        // 先检查此条记录是否满足所有条件
+        // 再next下一条
+        // 返回的是此条!!!!!!!!!!!!!!!!!
+        scan_->next();
+        while (!scan_->is_end()) {
             auto rec = scan_.get()->rid();
             auto record = fh_->get_record(rec, context_);
+
+            // 检查所有条件
             bool flag = true;
 
             for (auto &cond : conds_) {
@@ -90,21 +91,37 @@ class SeqScanExecutor : public AbstractExecutor, public ConditionDependedExecuto
                     break;
                 }
             }
+
             if (flag) {
                 rid_ = rec;
+                // scan_->next();
                 return;
             }
+            scan_->next();
         }
-        is_end_ = true;
+        isend = true;
+    }
+
+    bool is_end() const override {
+    
+        return scan_->is_end();
+        
     }
 
     std::unique_ptr<RmRecord> Next() override {
-        //        return nullptr;
+        // nextTuple();
+        // if (scan_->is_end()) {
+        //     std::cout<<"executor_seq_scan Next is_end"<<std::endl;
+        //     return nullptr;
+        // }
+        std::cout<<"getting next from seq scan"<<std::endl;
         auto rec = rid();
+
         auto record = fh_->get_record(rec, context_);
 
         return record;
     }
 
     Rid &rid() override { return rid_; }
+
 };
\ No newline at end of file
-- 
GitLab