### DistinctExecutor Implementation Source: https://github.com/jlu-xiurui/cmu15445-2021-fall/blob/ghess/p2-refinement/notes/Project 3 QUERY EXECUTION.md Implements a DistinctExecutor that removes duplicate tuples using a hash set. The MakeKey method converts a tuple into a DistinctKey for hashing. Init() clears the hash set and initializes the child executor. Next() iterates through child tuples, inserting unique ones into the set and returning them. ```C++ class DistinctExecutor : public AbstractExecutor { ... std::unordered_set set_; DistinctKey MakeKey(const Tuple *tuple) { std::vector values; const Schema *schema = GetOutputSchema(); for (uint32_t i = 0; i < schema->GetColumnCount(); ++i) { values.emplace_back(tuple->GetValue(schema, i)); } return {values}; } }; DistinctExecutor::DistinctExecutor(ExecutorContext *exec_ctx, const DistinctPlanNode *plan, std::unique_ptr &&child_executor) : AbstractExecutor(exec_ctx), plan_(plan), child_executor_(child_executor.release()) {} void DistinctExecutor::Init() { set_.clear(); child_executor_->Init(); } bool DistinctExecutor::Next(Tuple *tuple, RID *rid) { while (child_executor_->Next(tuple, rid)) { auto key = MakeKey(tuple); if (set_.count(key) == 0U) { set_.insert(key); return true; } } return false; } ``` -------------------------------- ### LimitExecutor Implementation Source: https://github.com/jlu-xiurui/cmu15445-2021-fall/blob/ghess/p2-refinement/notes/Project 3 QUERY EXECUTION.md Implements a LimitExecutor to restrict the number of output tuples based on a limit defined in the plan. Initializes by calling the child executor's Init() and resetting the limit. The Next() method returns tuples from the child until the limit is reached. ```C++ LimitExecutor::LimitExecutor(ExecutorContext *exec_ctx, const LimitPlanNode *plan, std::unique_ptr &&child_executor) : AbstractExecutor(exec_ctx), plan_(plan), child_executor_(child_executor.release()) { limit_ = plan_->GetLimit(); } void LimitExecutor::Init() { child_executor_->Init(); limit_ = plan_->GetLimit(); } bool LimitExecutor::Next(Tuple *tuple, RID *rid) { if (limit_ == 0 || !child_executor_->Next(tuple, rid)) { return false; } --limit_; return true; } ``` -------------------------------- ### DistinctKey Struct and Hash Implementation Source: https://github.com/jlu-xiurui/cmu15445-2021-fall/blob/ghess/p2-refinement/notes/Project 3 QUERY EXECUTION.md Defines a DistinctKey struct for use in a hash table to identify unique tuples. Includes an equality operator and a std::hash specialization for DistinctKey to enable its use in std::unordered_set. ```C++ namespace bustub { struct DistinctKey { std::vector value_; bool operator==(const DistinctKey &other) const { for (uint32_t i = 0; i < other.value_.size(); i++) { if (value_[i].CompareEquals(other.value_[i]) != CmpBool::CmpTrue) { return false; } } return true; } }; } // namespace bustub namespace std { /** Implements std::hash on AggregateKey */ template <> struct hash { std::size_t operator()(const bustub::DistinctKey &key) const { size_t curr_hash = 0; for (const auto &value : key.value_) { if (!value.IsNull()) { curr_hash = bustub::HashUtil::CombineHashes(curr_hash, bustub::HashUtil::HashValue(&value)); } } return curr_hash; } }; ... ``` -------------------------------- ### DeleteTuple with Lock Management in C++ Source: https://github.com/jlu-xiurui/cmu15445-2021-fall/blob/ghess/p2-refinement/notes/Project 4 CONCURRENCY CONTROL.md Handles tuple deletion, acquiring appropriate locks based on the transaction isolation level. For REPEATABLE_READ, it upgrades existing read locks; otherwise, it acquires new exclusive locks. ```C++ bool DeleteExecutor::Next([[maybe_unused]] Tuple *tuple, RID *rid) { auto exec_ctx = GetExecutorContext(); Transaction *txn = exec_ctx_->GetTransaction(); TransactionManager *txn_mgr = exec_ctx->GetTransactionManager(); LockManager *lock_mgr = exec_ctx->GetLockManager(); while (child_executor_->Next(tuple, rid)) { if (txn->GetIsolationLevel() != IsolationLevel::REPEATABLE_READ) { if (!lock_mgr->LockExclusive(txn, *rid)) { txn_mgr->Abort(txn); } } else { if (!lock_mgr->LockUpgrade(txn, *rid)) { txn_mgr->Abort(txn); } } if (table_info_->table_->MarkDelete(*rid, txn)) { for (auto indexinfo : indexes_) { indexinfo->index_->DeleteEntry(tuple->KeyFromTuple(*child_executor_->GetOutputSchema(), i ndexinfo->key_schema_, indexinfo->index_->GetKeyAttrs()), *rid, txn); IndexWriteRecord iwr(*rid, table_info_->oid_, WType::DELETE, *tuple, *tuple, indexinfo->i ndex_oid_, exec_ctx->GetCatalog()); txn->AppendIndexWriteRecord(iwr); } } } return false; } ``` -------------------------------- ### InsertTuple with Exclusive Lock in C++ Source: https://github.com/jlu-xiurui/cmu15445-2021-fall/blob/ghess/p2-refinement/notes/Project 4 CONCURRENCY CONTROL.md Handles tuple insertion into a table, acquiring an exclusive write lock on the inserted tuple. If the transaction isolation level is not REPEATABLE_READ, it acquires a new exclusive lock. Otherwise, it attempts to upgrade an existing read lock to a write lock. ```C++ bool InsertExecutor::Next([[maybe_unused]] Tuple *tuple, RID *rid) { auto exec_ctx = GetExecutorContext(); Transaction *txn = exec_ctx_->GetTransaction(); TransactionManager *txn_mgr = exec_ctx->GetTransactionManager(); LockManager *lock_mgr = exec_ctx->GetLockManager(); Tuple tmp_tuple; RID tmp_rid; if (is_raw_) { for (uint32_t idx = 0; idx < size_; idx++) { const std::vector &raw_value = plan_->RawValuesAt(idx); tmp_tuple = Tuple(raw_value, &table_info_->schema_); if (table_info_->table_->InsertTuple(tmp_tuple, &tmp_rid, txn)) { if (!lock_mgr->LockExclusive(txn, tmp_rid)) { txn_mgr->Abort(txn); } for (auto indexinfo : indexes_) { indexinfo->index_->InsertEntry( tmp_tuple.KeyFromTuple(table_info_->schema_, indexinfo->key_schema_, indexinfo->index_->GetKeyAttrs()), tmp_rid, txn); IndexWriteRecord iwr(*rid, table_info_->oid_, WType::INSERT, *tuple, *tuple, indexinfo->index_oid_, exec_ctx->GetCatalog()); txn->AppendIndexWriteRecord(iwr); } } } return false; } while (child_executor_->Next(&tmp_tuple, &tmp_rid)) { if (table_info_->table_->InsertTuple(tmp_tuple, &tmp_rid, txn)) { if (!lock_mgr->LockExclusive(txn, *rid)) { txn_mgr->Abort(txn); } for (auto indexinfo : indexes_) { indexinfo->index_->InsertEntry(tmp_tuple.KeyFromTuple(*child_executor_->GetOutputSchema(), indexinfo->key_schema_, indexinfo->index_->GetKeyAttrs()), tmp_rid, txn); txn->GetIndexWriteSet()->emplace_back(tmp_rid, table_info_->oid_, WType::INSERT, tmp_tuple, tmp_tuple, indexinfo->index_oid_, exec_ctx->GetCatalog()); } } } return false; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.