Untitled

 avatar
unknown
c_cpp
a year ago
1.9 kB
3
Indexable
#include <memory>
#include "execution/plans/abstract_plan.h"
#include "execution/plans/limit_plan.h"
#include "execution/plans/sort_plan.h"
#include "execution/plans/topn_plan.h"
#include "optimizer/optimizer.h"

namespace bustub {

auto Optimizer::OptimizeSortLimitAsTopN(const AbstractPlanNodeRef &plan) -> AbstractPlanNodeRef {
  // TODO(student): implement sort + limit -> top N optimizer rule

  std::cout << "Plan's type = " << static_cast<int>(plan->GetType()) << std::endl;

  std::vector<AbstractPlanNodeRef> children;

  for (const auto &child : plan->GetChildren()) {
    children.emplace_back(OptimizeOrderByAsIndexScan(child));
  }
  auto children_size = children.size();
  auto optimized_plan = plan->CloneWithChildren(std::move(children));

  std::cout << children_size << std::endl;

  if (static_cast<int>(children_size) == 1) {
    std::cout << "parent type = " << static_cast<int>(optimized_plan->GetType()) << std::endl;
    std::cout << "child type = " << static_cast<int>(optimized_plan->GetChildAt(0)->GetType()) << std::endl;
    if (optimized_plan->GetType() == PlanType::Limit && optimized_plan->GetChildAt(0)->GetType() == PlanType::Sort) {
      std::cout << "type match" << std::endl;
      const auto &limit_plan = dynamic_cast<const LimitPlanNode &>(*optimized_plan);
      const auto &sort_plan = dynamic_cast<const SortPlanNode &>(*optimized_plan->GetChildAt(0));

      return std::make_shared<TopNPlanNode>(sort_plan.output_schema_, optimized_plan->GetChildAt(0),
                                            sort_plan.GetOrderBy(), limit_plan.GetLimit());
    }

    // TODO(chien-yu-liu): update optimized_plan to GetChildAt(0)
    auto test = optimized_plan->GetChildAt(0);
    // optimized_plan = std::unique_ptr<const bustub::AbstractPlanNode>(optimized_plan->GetChildAt(0));
    // optimized_plan = std::make_unique(optimized_plan->GetChildAt(0));
    
    
  }

  return plan;
}

}  // namespace bustub
Leave a Comment