Skip to content

Commit

Permalink
Adding AbstractNode class.
Browse files Browse the repository at this point in the history
AbstractNode will provide interface for Operator and eventually
AbstractExpressions as well.

Note there are a few road blocks before the rest of the rewriter can be
changed to cleanly use abstract classes:
(1) Similarly abstract OperatorExpressions.
(2) We will have to find a good place to hide OpType, which is currently
an enum type (cannot be abstracted) and pervades the code base. This may
be solved by abstracting at the group level, but will have to look into
it.
(3) Need to clean up and separate interfaces between AbstractNode,
OperatorNode, and Operator classes.
  • Loading branch information
Newton Xie authored and newtoncx committed Apr 7, 2019
1 parent 484d76d commit 209c46a
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 160 deletions.
117 changes: 117 additions & 0 deletions src/include/optimizer/abstract_node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// abstract_node.h
//
// Identification: src/include/optimizer/abstract_node.h
//
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#pragma once

#include "optimizer/property_set.h"
#include "util/hash_util.h"

#include <memory>
#include <string>

namespace peloton {
namespace optimizer {

enum class OpType {
Undefined = 0,
// Special match operators
Leaf,
// Logical ops
Get,
LogicalExternalFileGet,
LogicalQueryDerivedGet,
LogicalProjection,
LogicalFilter,
LogicalMarkJoin,
LogicalDependentJoin,
LogicalSingleJoin,
InnerJoin,
LeftJoin,
RightJoin,
OuterJoin,
SemiJoin,
LogicalAggregateAndGroupBy,
LogicalInsert,
LogicalInsertSelect,
LogicalDelete,
LogicalUpdate,
LogicalLimit,
LogicalDistinct,
LogicalExportExternalFile,
// Separate between logical and physical ops
LogicalPhysicalDelimiter,
// Physical ops
DummyScan, /* Dummy Physical Op for SELECT without FROM*/
SeqScan,
IndexScan,
ExternalFileScan,
QueryDerivedScan,
OrderBy,
PhysicalLimit,
Distinct,
InnerNLJoin,
LeftNLJoin,
RightNLJoin,
OuterNLJoin,
InnerHashJoin,
LeftHashJoin,
RightHashJoin,
OuterHashJoin,
Insert,
InsertSelect,
Delete,
Update,
Aggregate,
HashGroupBy,
SortGroupBy,
ExportExternalFile,
};

//===--------------------------------------------------------------------===//
// Abstract Node
//===--------------------------------------------------------------------===//
//TODO(ncx): dependence on OperatorVisitor
class OperatorVisitor;

struct AbstractNode {
AbstractNode() {}

~AbstractNode() {}

virtual void Accept(OperatorVisitor *v) const = 0;

virtual std::string GetName() const = 0;

// TODO(ncx): problematic dependence on OpType
virtual OpType GetType() const = 0;

virtual bool IsLogical() const = 0;

virtual bool IsPhysical() const = 0;

virtual hash_t Hash() const {
OpType t = GetType();
return HashUtil::Hash(&t);
}

virtual bool operator==(const AbstractNode &r) {
return GetType() == r.GetType();
}

virtual bool IsDefined() const { return node != nullptr; }

private:
std::shared_ptr<AbstractNode> node;
};

} // namespace optimizer
} // namespace peloton
4 changes: 2 additions & 2 deletions src/include/optimizer/input_column_deriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ class InputColumnDeriver : public OperatorVisitor {
* @brief Provide all tuple value expressions needed in the expression
*/
void ScanHelper();
void AggregateHelper(const BaseOperatorNode *);
void JoinHelper(const BaseOperatorNode *op);
void AggregateHelper(const AbstractNode *);
void JoinHelper(const AbstractNode *op);

/**
* @brief Some operators, for example limit, directly pass down column
Expand Down
116 changes: 18 additions & 98 deletions src/include/optimizer/operator_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#pragma once

#include "optimizer/abstract_node.h"
#include "optimizer/property_set.h"
#include "util/hash_util.h"

Expand All @@ -21,103 +22,24 @@
namespace peloton {
namespace optimizer {

enum class OpType {
Undefined = 0,
// Special match operators
Leaf,
// Logical ops
Get,
LogicalExternalFileGet,
LogicalQueryDerivedGet,
LogicalProjection,
LogicalFilter,
LogicalMarkJoin,
LogicalDependentJoin,
LogicalSingleJoin,
InnerJoin,
LeftJoin,
RightJoin,
OuterJoin,
SemiJoin,
LogicalAggregateAndGroupBy,
LogicalInsert,
LogicalInsertSelect,
LogicalDelete,
LogicalUpdate,
LogicalLimit,
LogicalDistinct,
LogicalExportExternalFile,
// Separate between logical and physical ops
LogicalPhysicalDelimiter,
// Physical ops
DummyScan, /* Dummy Physical Op for SELECT without FROM*/
SeqScan,
IndexScan,
ExternalFileScan,
QueryDerivedScan,
OrderBy,
PhysicalLimit,
Distinct,
InnerNLJoin,
LeftNLJoin,
RightNLJoin,
OuterNLJoin,
InnerHashJoin,
LeftHashJoin,
RightHashJoin,
OuterHashJoin,
Insert,
InsertSelect,
Delete,
Update,
Aggregate,
HashGroupBy,
SortGroupBy,
ExportExternalFile,
};

//===--------------------------------------------------------------------===//
// Operator Node
//===--------------------------------------------------------------------===//
class OperatorVisitor;

struct BaseOperatorNode {
BaseOperatorNode() {}

virtual ~BaseOperatorNode() {}

virtual void Accept(OperatorVisitor *v) const = 0;

virtual std::string GetName() const = 0;

virtual OpType GetType() const = 0;

virtual bool IsLogical() const = 0;

virtual bool IsPhysical() const = 0;

virtual std::vector<PropertySet> RequiredInputProperties() const {
return {};
}

virtual hash_t Hash() const {
OpType t = GetType();
return HashUtil::Hash(&t);
}

virtual bool operator==(const BaseOperatorNode &r) {
return GetType() == r.GetType();
}
};

// Curiously recurring template pattern
// TODO(ncx): this templating would be nice to clean up
template <typename T>
struct OperatorNode : public BaseOperatorNode {
struct OperatorNode : public AbstractNode {
OperatorNode() {}

virtual ~OperatorNode() {}

void Accept(OperatorVisitor *v) const;

std::string GetName() const { return name_; }
virtual std::string GetName() const { return name_; }

OpType GetType() const { return type_; }
virtual OpType GetType() const { return type_; }

bool IsLogical() const;

Expand All @@ -128,32 +50,26 @@ struct OperatorNode : public BaseOperatorNode {
static OpType type_;
};

class Operator {
class Operator : public AbstractNode {
public:
Operator();

Operator(BaseOperatorNode *node);
Operator(AbstractNode *node);

// Calls corresponding visitor to node
void Accept(OperatorVisitor *v) const;

// Return name of operator
std::string GetName() const;

// Return operator type
OpType GetType() const;

// Operator contains Logical node
bool IsLogical() const;

// Operator contains Physical node
bool IsPhysical() const;

hash_t Hash() const;

bool operator==(const Operator &r);

// Operator contains physical or logical operator node
bool IsDefined() const;

template <typename T>
Expand All @@ -164,8 +80,12 @@ class Operator {
return nullptr;
}

static std::string name_;

static OpType type_;

private:
std::shared_ptr<BaseOperatorNode> node;
std::shared_ptr<AbstractNode> node;
};

} // namespace optimizer
Expand All @@ -174,8 +94,8 @@ class Operator {
namespace std {

template <>
struct hash<peloton::optimizer::BaseOperatorNode> {
typedef peloton::optimizer::BaseOperatorNode argument_type;
struct hash<peloton::optimizer::AbstractNode> {
typedef peloton::optimizer::AbstractNode argument_type;
typedef std::size_t result_type;
result_type operator()(argument_type const &s) const { return s.Hash(); }
};
Expand Down
Loading

0 comments on commit 209c46a

Please sign in to comment.