Skip to content
This repository has been archived by the owner on Sep 27, 2019. It is now read-only.

Optimizer refactor and cost model additions #1484

Merged
merged 23 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/include/optimizer/abstract_cost_calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// abstract_cost_calculator.h
//
// Identification: src/include/optimizer/abstract_cost_calculator.h
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#pragma once

#include "optimizer/operator_visitor.h"

namespace peloton {
namespace optimizer {

class Memo;

class AbstractCostCalculator : public OperatorVisitor {
public:
virtual double CalculateCost(GroupExpression *gexpr, Memo *memo,
concurrency::TransactionContext *txn) = 0;
};

} // namespace optimizer
} // namespace peloton

63 changes: 32 additions & 31 deletions src/include/optimizer/cost_calculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,52 @@
//
// Peloton
//
// cost_and_stats_calculator.h
// cost_calculator.h
//
// Identification: src/include/optimizer/cost_calculator.h
//
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#pragma once

#include "optimizer/operator_visitor.h"
#include "optimizer/abstract_cost_calculator.h"

namespace peloton {
namespace optimizer {

class Memo;
// Derive cost for a physical group expressionh
class CostCalculator : public OperatorVisitor {
// Derive cost for a physical group expression
class CostCalculator : public AbstractCostCalculator {
public:
double CalculateCost(GroupExpression *gexpr, Memo *memo,
concurrency::TransactionContext *txn);

void Visit(const DummyScan *) override;
void Visit(const PhysicalSeqScan *) override;
void Visit(const PhysicalIndexScan *) override;
void Visit(const ExternalFileScan *) override;
void Visit(const QueryDerivedScan *) override;
void Visit(const PhysicalOrderBy *) override;
void Visit(const PhysicalLimit *) override;
void Visit(const PhysicalInnerNLJoin *) override;
void Visit(const PhysicalLeftNLJoin *) override;
void Visit(const PhysicalRightNLJoin *) override;
void Visit(const PhysicalOuterNLJoin *) override;
void Visit(const PhysicalInnerHashJoin *) override;
void Visit(const PhysicalLeftHashJoin *) override;
void Visit(const PhysicalRightHashJoin *) override;
void Visit(const PhysicalOuterHashJoin *) override;
void Visit(const PhysicalInsert *) override;
void Visit(const PhysicalInsertSelect *) override;
void Visit(const PhysicalDelete *) override;
void Visit(const PhysicalUpdate *) override;
void Visit(const PhysicalHashGroupBy *) override;
void Visit(const PhysicalSortGroupBy *) override;
void Visit(const PhysicalDistinct *) override;
void Visit(const PhysicalAggregate *) override;
CostCalculator(){};

virtual double CalculateCost(GroupExpression *gexpr, Memo *memo,
concurrency::TransactionContext *txn) override;

virtual void Visit(const DummyScan *) override;
virtual void Visit(const PhysicalSeqScan *) override;
virtual void Visit(const PhysicalIndexScan *) override;
virtual void Visit(const QueryDerivedScan *) override;
virtual void Visit(const PhysicalOrderBy *) override;
virtual void Visit(const PhysicalLimit *) override;
virtual void Visit(const PhysicalInnerNLJoin *) override;
virtual void Visit(const PhysicalLeftNLJoin *) override;
virtual void Visit(const PhysicalRightNLJoin *) override;
virtual void Visit(const PhysicalOuterNLJoin *) override;
virtual void Visit(const PhysicalInnerHashJoin *) override;
virtual void Visit(const PhysicalLeftHashJoin *) override;
virtual void Visit(const PhysicalRightHashJoin *) override;
virtual void Visit(const PhysicalOuterHashJoin *) override;
virtual void Visit(const PhysicalInsert *) override;
virtual void Visit(const PhysicalInsertSelect *) override;
virtual void Visit(const PhysicalDelete *) override;
virtual void Visit(const PhysicalUpdate *) override;
virtual void Visit(const PhysicalHashGroupBy *) override;
virtual void Visit(const PhysicalSortGroupBy *) override;
virtual void Visit(const PhysicalDistinct *) override;
virtual void Visit(const PhysicalAggregate *) override;

private:
double HashCost();
Expand Down
31 changes: 31 additions & 0 deletions src/include/optimizer/cost_calculator_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// cost_calculator_factory.h
//
// Identification: src/include/optimizer/cost_calculator_factory.h
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#pragma once
#include "optimizer/cost_calculator.h"

#include "common/exception.h"

namespace peloton {
namespace optimizer {

class CostCalculatorFactory {
public:
/*
* Creates the respective cost calculator given a cost calculator name
*/
static std::unique_ptr<AbstractCostCalculator> CreateCostCalculator(
const std::string &cost_model_name);
};

} // namespace peloton
} // namespace optimizer
3 changes: 3 additions & 0 deletions src/include/optimizer/optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <memory>

#include "optimizer/abstract_optimizer.h"
#include "optimizer/abstract_cost_calculator.h"
#include "optimizer/property_set.h"
#include "optimizer/optimizer_metadata.h"

Expand Down Expand Up @@ -72,6 +73,7 @@ class Optimizer : public AbstractOptimizer {
Optimizer &operator=(Optimizer &&) = delete;

Optimizer();
GustavoAngulo marked this conversation as resolved.
Show resolved Hide resolved
Optimizer(std::unique_ptr<AbstractCostCalculator> cost_model);

std::shared_ptr<planner::AbstractPlan> BuildPelotonPlanTree(
const std::unique_ptr<parser::SQLStatementList> &parse_tree_list,
Expand Down Expand Up @@ -164,6 +166,7 @@ class Optimizer : public AbstractOptimizer {
//////////////////////////////////////////////////////////////////////////////
/// Metadata
OptimizerMetadata metadata_;
std::unique_ptr<AbstractCostCalculator> cost_model_;
};

} // namespace optimizer
Expand Down
13 changes: 11 additions & 2 deletions src/include/optimizer/optimizer_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//
// Peloton
//
// optimizer.h
// optimizer_metadata.h
//
// Identification: src/include/optimizer/optimizer.h
// Identification: src/include/optimizer/optimizer_metadata.h
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
Expand All @@ -13,6 +13,8 @@
#pragma once

#include "common/timer.h"
#include "optimizer/cost_calculator.h"
#include "optimizer/cost_calculator_factory.h"
#include "optimizer/memo.h"
#include "optimizer/group_expression.h"
#include "optimizer/rule.h"
Expand All @@ -35,9 +37,16 @@ class OptimizerMetadata {
settings::SettingId::task_execution_timeout)),
timer(Timer<std::milli>()) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarity, do we still need this method here?


OptimizerMetadata(std::unique_ptr<AbstractCostCalculator> cost_model)
: cost_model(std::move(cost_model)), timeout_limit(settings::SettingsManager::GetInt(
settings::SettingId::task_execution_timeout)),
timer(Timer<std::milli>()) {}


Memo memo;
RuleSet rule_set;
OptimizerTaskPool *task_pool;
std::unique_ptr<AbstractCostCalculator> cost_model;
catalog::CatalogCache *catalog_cache;
unsigned int timeout_limit;
Timer<std::milli> timer;
Expand Down
71 changes: 71 additions & 0 deletions src/include/optimizer/postgres_cost_calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// postgres_cost_calculator.h
//
// Identification: src/include/optimizer/postgres_cost_calculator.h
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//


#pragma once

#include "optimizer/abstract_cost_calculator.h"

// TODO: This is not fully reflective of the postgres cost model. Currently we
// are attempting
// to emulate their hash join cost model

namespace peloton {
namespace optimizer {

class Memo;
// Derive cost for a physical group expression
class PostgresCostCalculator : public AbstractCostCalculator {
public:
double CalculateCost(GroupExpression *gexpr, Memo *memo,
concurrency::TransactionContext *txn) override;

void Visit(const DummyScan *) override;
void Visit(const PhysicalSeqScan *) override;
void Visit(const PhysicalIndexScan *) override;
void Visit(const QueryDerivedScan *) override;
void Visit(const PhysicalOrderBy *) override;
void Visit(const PhysicalLimit *) override;
void Visit(const PhysicalInnerNLJoin *) override;
void Visit(const PhysicalLeftNLJoin *) override;
void Visit(const PhysicalRightNLJoin *) override;
void Visit(const PhysicalOuterNLJoin *) override;
void Visit(const PhysicalInnerHashJoin *) override;
void Visit(const PhysicalLeftHashJoin *) override;
void Visit(const PhysicalRightHashJoin *) override;
void Visit(const PhysicalOuterHashJoin *) override;
void Visit(const PhysicalInsert *) override;
void Visit(const PhysicalInsertSelect *) override;
void Visit(const PhysicalDelete *) override;
void Visit(const PhysicalUpdate *) override;
void Visit(const PhysicalHashGroupBy *) override;
void Visit(const PhysicalSortGroupBy *) override;
void Visit(const PhysicalDistinct *) override;
void Visit(const PhysicalAggregate *) override;

private:
double HashCost();
double SortCost();
double GroupByCost();

/* Checks if keys for a join child only reference one table */
bool IsBaseTable(
const std::vector<std::unique_ptr<expression::AbstractExpression>> &keys);

GroupExpression *gexpr_;
Memo *memo_;
concurrency::TransactionContext *txn_;
double output_cost_ = 0;
};

} // namespace optimizer
} // namespace peloton
Loading