Untitled
unknown
c_cpp
2 years ago
10 kB
14
Indexable
//===----------------------------------------------------------------------===//
//
// BusTub
//
// nested_loop_join_executor.cpp
//
// Identification: src/execution/nested_loop_join_executor.cpp
//
// Copyright (c) 2015-2021, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include "execution/executors/nested_loop_join_executor.h"
// #include <_types/_uint32_t.h>
#include "binder/table_ref/bound_join_ref.h"
#include "common/exception.h"
#include "type/value_factory.h"
namespace bustub {
NestedLoopJoinExecutor::NestedLoopJoinExecutor(ExecutorContext *exec_ctx, const NestedLoopJoinPlanNode *plan,
std::unique_ptr<AbstractExecutor> &&left_executor,
std::unique_ptr<AbstractExecutor> &&right_executor)
: AbstractExecutor(exec_ctx) {
if (!(plan->GetJoinType() == JoinType::LEFT || plan->GetJoinType() == JoinType::INNER)) {
// Note for 2023 Fall: You ONLY need to implement left join and inner join.
throw bustub::NotImplementedException(fmt::format("join type {} not supported", plan->GetJoinType()));
}
plan_ = plan;
left_exec_ = std::move(left_executor);
right_exec_ = std::move(right_executor);
// left_valid_ = false;
// right_position_ = 0;
// left_match_ = false;
}
void NestedLoopJoinExecutor::Init() {
left_exec_->Init();
right_exec_->Init();
ever_ = false;
std::cout << "In Nested Loop Join" << std::endl;
// right_tuples_.clear();
// left_valid_ = left_exec_->Next(&left_tuple_, &left_rid_);
// right_position_ = 0;
// left_match_ = false;
// Tuple right_tuple;
// RID right_rid;
// while(right_exec_->Next(&right_tuple, &right_rid)){
// right_tuples_.push_back(right_tuple);
// }
}
auto NestedLoopJoinExecutor::Next(Tuple *tuple, RID *rid) -> bool {
if (ever_) {
goto HERE;
}
ever_ = true;
left_exec_->Init();
while (true) {
left_valid_ = left_exec_->Next(&left_tuple_, &left_rid_);
if (!left_valid_) {
return false;
}
right_exec_->Init();
found_ = false;
while (true) {
right_valid_ = right_exec_->Next(&right_tuple_, &right_rid_);
syield_ = false;
if (!right_valid_) {
if (!found_ && plan_->GetJoinType() == JoinType::LEFT) {
syield_ = true;
// tyield_
values_.clear();
for (uint32_t i = 0; i < left_exec_->GetOutputSchema().GetColumnCount(); i++) {
values_.push_back(left_tuple_.GetValue(&left_exec_->GetOutputSchema(), i));
}
for (uint32_t i = 0; i < right_exec_->GetOutputSchema().GetColumnCount(); i++) {
auto type = right_exec_->GetOutputSchema().GetColumn(i).GetType();
values_.push_back(ValueFactory::GetNullValueByType(type));
}
{
Tuple new_tuple(values_, &plan_->OutputSchema());
*tuple = new_tuple;
*rid = new_tuple.GetRid();
}
}
} else if (plan_->predicate_ != nullptr && plan_->Predicate()->ToString() == "true") { // added
std::cout << "&&&&&&&&& 1 == 1 case in nested loop join" << std::endl;
found_ = true;
syield_ = true;
// tyield_
values_.clear();
for (uint32_t i = 0; i < left_exec_->GetOutputSchema().GetColumnCount(); i++) {
values_.push_back(left_tuple_.GetValue(&left_exec_->GetOutputSchema(), i));
std::cout << left_tuple_.GetValue(&left_exec_->GetOutputSchema(), i).GetAs<int>() << std::endl;
}
for (uint32_t i = 0; i < right_exec_->GetOutputSchema().GetColumnCount(); i++) {
values_.push_back(right_tuple_.GetValue(&right_exec_->GetOutputSchema(), i));
std::cout << right_tuple_.GetValue(&right_exec_->GetOutputSchema(), i).GetAs<int>() << std::endl;
}
{
Tuple new_tuple(values_, &plan_->OutputSchema());
*tuple = new_tuple;
*rid = new_tuple.GetRid();
}
} else if (plan_->predicate_ == nullptr || plan_->predicate_
->EvaluateJoin(&left_tuple_, left_exec_->GetOutputSchema(),
&right_tuple_, right_exec_->GetOutputSchema())
.GetAs<bool>()) {
std::cout << "==============" << plan_->Predicate()->ToString() << std::endl;
std::cout << "Left plan = " << plan_->GetLeftPlan()->ToString() << std::endl;
std::cout << "Right plan = " << plan_->GetRightPlan()->ToString() << std::endl;
if(plan_->predicate_ == nullptr){
std::cout << "NestedLoopJoin predicate is empty!! should make it 1 == 1" << std::endl;
}
if(plan_->Predicate()->ToString() == "true"){
std::cout << "Predicate is true +++++++++++++++++++=" << std::endl;
}
found_ = true;
syield_ = true;
// tyield_
values_.clear();
for (uint32_t i = 0; i < left_exec_->GetOutputSchema().GetColumnCount(); i++) {
values_.push_back(left_tuple_.GetValue(&left_exec_->GetOutputSchema(), i));
std::cout << left_tuple_.GetValue(&left_exec_->GetOutputSchema(), i).GetAs<int>() << std::endl;
}
for (uint32_t i = 0; i < right_exec_->GetOutputSchema().GetColumnCount(); i++) {
values_.push_back(right_tuple_.GetValue(&right_exec_->GetOutputSchema(), i));
std::cout << right_tuple_.GetValue(&right_exec_->GetOutputSchema(), i).GetAs<int>() << std::endl;
}
{
Tuple new_tuple(values_, &plan_->OutputSchema());
*tuple = new_tuple;
*rid = new_tuple.GetRid();
}
}
if (syield_) {
return true;
}
HERE:
if (!right_valid_) {
break;
}
}
}
// return false;
// if(right_tuples_.empty()){
// // TODO(chien-yu-liu): if left join, emit null
// if(plan_->GetJoinType() == JoinType::LEFT){
// if(!left_valid_){
// return false;
// }
// std::vector<Value> values;
// for(uint32_t i = 0; i < left_exec_->GetOutputSchema().GetColumnCount(); i++){
// values.push_back(left_tuple_.GetValue(&left_exec_->GetOutputSchema(), i));
// }
// for(uint32_t i = 0; i < right_exec_->GetOutputSchema().GetColumnCount(); i++){
// auto type = right_exec_->GetOutputSchema().GetColumn(i).GetType();
// values.push_back(ValueFactory::GetNullValueByType(type));
// }
// Tuple new_tuple(values, &plan_->OutputSchema());
// *tuple = new_tuple;
// *rid = new_tuple.GetRid();
// left_valid_ = left_exec_->Next(&left_tuple_, &left_rid_);
// return true;
// }
// return false;
// }
// while(left_valid_){
// Tuple right_tuple;
// right_tuple = right_tuples_[right_position_];
// if(plan_->predicate_ == nullptr || plan_->predicate_->EvaluateJoin(&left_tuple_, left_exec_->GetOutputSchema(),
// &right_tuple, right_exec_->GetOutputSchema()).GetAs<bool>()){
// left_match_ = true;
// // TODO(chien-yu-liu): emit
// std::vector<Value> values;
// for(uint32_t i = 0; i < left_exec_->GetOutputSchema().GetColumnCount(); i++){
// values.push_back(left_tuple_.GetValue(&left_exec_->GetOutputSchema(), i));
// }
// for(uint32_t i = 0; i < right_exec_->GetOutputSchema().GetColumnCount(); i++){
// values.push_back(right_tuple.GetValue(&right_exec_->GetOutputSchema(), i));
// }
// Tuple new_tuple(values, &plan_->OutputSchema());
// *tuple = new_tuple;
// *rid = new_tuple.GetRid();
// right_position_++;
// if(right_position_ == static_cast<int>(right_tuples_.size())){
// right_position_ = 0;
// left_valid_ = left_exec_->Next(&left_tuple_, &left_rid_);
// left_match_ = false;
// }
// return true;
// }
// right_position_++;
// if(right_position_ == static_cast<int>(right_tuples_.size())){
// right_position_ = 0;
// bool emit = false;
// if(!left_match_ && plan_->GetJoinType() == JoinType::LEFT){
// // TODO(chien-yu-liu): Emit tuple (with right being null)
// std::vector<Value> values;
// for(uint32_t i = 0; i < left_exec_->GetOutputSchema().GetColumnCount(); i++){
// values.push_back(left_tuple_.GetValue(&left_exec_->GetOutputSchema(), i));
// }
// for(uint32_t i = 0; i < right_exec_->GetOutputSchema().GetColumnCount(); i++){
// auto type = right_exec_->GetOutputSchema().GetColumn(i).GetType();
// values.push_back(ValueFactory::GetNullValueByType(type));
// }
// Tuple new_tuple(values, &plan_->OutputSchema());
// *tuple = new_tuple;
// *rid = new_tuple.GetRid();
// emit = true;
// }
// left_valid_ = left_exec_->Next(&left_tuple_, &left_rid_);
// left_match_ = false;
// if(emit){
// return true;
// }
// }
// }
// // Tuple left_tuple;
// // RID left_rid;
// // std::cout << "test 1" << std::endl;
// // while(left_exec_->Next(&left_tuple, &left_rid)){
// // // if(!right_exec_->Next(&right_tuple, &right_rid) && !left_exec_->Next(&left_tuple, &left_rid)){
// // // return false;
// // // }
// // for(const auto& right_tuple : right_tuples_){
// // if(plan_->predicate_ == nullptr || plan_->predicate_->EvaluateJoin(&left_tuple,
// left_exec_->GetOutputSchema(), &right_tuple, right_exec_->GetOutputSchema()).GetAs<bool>()){
// // // std::vector<Value> values(plan_->output_schema_->GetColumnCount());
// // std::vector<Value> values;
// // for(uint32_t i = 0; i < plan_->output_schema_->GetColumnCount(); i++){
// // auto value = plan_->predicate_->EvaluateJoin(&left_tuple, left_exec_->GetOutputSchema(), &right_tuple,
// right_exec_->GetOutputSchema());
// // values.push_back(value);
// // }
// // std::cout << "test3" << std::endl;
// // std::cout << "values.size() = " << values.size() << std::endl;
// // *tuple = Tuple(values, &plan_->OutputSchema());
// // // *tuple = Tuple(values, &plan_->output_schema_);
// // return true;
// // }
// // }
// // }
// return false;
}
} // namespace bustub
Editor is loading...
Leave a Comment