|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Peloton |
| 4 | +// |
| 5 | +// absexpr_expression.h |
| 6 | +// |
| 7 | +// Identification: src/include/optimizer/absexpr_expression.h |
| 8 | +// |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +// AbstractExpression Definition |
| 14 | +#include "expression/abstract_expression.h" |
| 15 | +#include "expression/conjunction_expression.h" |
| 16 | +#include "expression/comparison_expression.h" |
| 17 | +#include "expression/constant_value_expression.h" |
| 18 | + |
| 19 | +#include <memory> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +namespace peloton { |
| 23 | +namespace optimizer { |
| 24 | + |
| 25 | +// (TODO): rethink the AbsExpr_Container/Expression approach in comparion to abstract |
| 26 | +// Most of the core rule/optimizer code relies on the concept of an Operator / |
| 27 | +// OperatorExpression and the interface that the two functions respectively expose. |
| 28 | +// |
| 29 | +// The annoying part is that an AbstractExpression blends together an Operator |
| 30 | +// and OperatorExpression. Second part, the AbstractExpression does not export the |
| 31 | +// correct interface that the rest of the system depends on. |
| 32 | +// |
| 33 | +// As an extreme level of simplification (sort of hacky), an AbsExpr_Container is |
| 34 | +// analogous to Operator and wraps a single AbstractExpression node. AbsExpr_Expression |
| 35 | +// is analogous to OperatorExpression. |
| 36 | +// |
| 37 | +// AbsExpr_Container does *not* handle memory correctly w.r.t internal instantiations |
| 38 | +// from Rule transformation. This is since Peloton itself mixes unique_ptrs and |
| 39 | +// hands out raw pointers which makes adding a shared_ptr here extremely problematic. |
| 40 | +// terrier uses only shared_ptr when dealing with AbstractExpression trees. |
| 41 | + |
| 42 | +class AbsExpr_Container { |
| 43 | + public: |
| 44 | + AbsExpr_Container(); |
| 45 | + |
| 46 | + AbsExpr_Container(const expression::AbstractExpression *expr) { |
| 47 | + node = expr; |
| 48 | + } |
| 49 | + |
| 50 | + // Return operator type |
| 51 | + ExpressionType GetType() const { |
| 52 | + if (IsDefined()) { |
| 53 | + return node->GetExpressionType(); |
| 54 | + } |
| 55 | + return ExpressionType::INVALID; |
| 56 | + } |
| 57 | + |
| 58 | + const expression::AbstractExpression *GetExpr() const { |
| 59 | + return node; |
| 60 | + } |
| 61 | + |
| 62 | + // Operator contains Logical node |
| 63 | + bool IsLogical() const { |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + // Operator contains Physical node |
| 68 | + bool IsPhysical() const { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + std::string GetName() const { |
| 73 | + if (IsDefined()) { |
| 74 | + return node->GetExpressionName(); |
| 75 | + } |
| 76 | + |
| 77 | + return "Undefined"; |
| 78 | + } |
| 79 | + |
| 80 | + hash_t Hash() const { |
| 81 | + if (IsDefined()) { |
| 82 | + return node->Hash(); |
| 83 | + } |
| 84 | + return 0; |
| 85 | + } |
| 86 | + |
| 87 | + bool operator==(const AbsExpr_Container &r) { |
| 88 | + if (IsDefined() && r.IsDefined()) { |
| 89 | + // (TODO): need a better way to determine deep equality |
| 90 | + |
| 91 | + // NOTE: |
| 92 | + // Without proper equality determinations, the groups will |
| 93 | + // not be assigned correctly. Arguably, terrier does this |
| 94 | + // better because a blind ExactlyEquals on different types |
| 95 | + // of ConstantValueExpression under Peloton will crash! |
| 96 | + |
| 97 | + // For now, just return (false). |
| 98 | + // I don't anticipate this will affect correctness, just |
| 99 | + // performance, since duplicate trees will have to evaluated |
| 100 | + // over and over again, rather than being able to "borrow" |
| 101 | + // a previous tree's rewrite. |
| 102 | + // |
| 103 | + // Probably not worth to create a "validator" since porting |
| 104 | + // this to terrier anyways (?). == does not check Value |
| 105 | + // so it's broken. ExactlyEqual requires precondition checking. |
| 106 | + return false; |
| 107 | + } else if (!IsDefined() && !r.IsDefined()) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + // Operator contains physical or logical operator node |
| 114 | + bool IsDefined() const { |
| 115 | + return node != nullptr; |
| 116 | + } |
| 117 | + |
| 118 | + //(TODO): fix memory management once go to terrier |
| 119 | + expression::AbstractExpression *Rebuild(std::vector<expression::AbstractExpression*> children) { |
| 120 | + switch (GetType()) { |
| 121 | + case ExpressionType::COMPARE_EQUAL: |
| 122 | + case ExpressionType::COMPARE_NOTEQUAL: |
| 123 | + case ExpressionType::COMPARE_LESSTHAN: |
| 124 | + case ExpressionType::COMPARE_GREATERTHAN: |
| 125 | + case ExpressionType::COMPARE_LESSTHANOREQUALTO: |
| 126 | + case ExpressionType::COMPARE_GREATERTHANOREQUALTO: |
| 127 | + case ExpressionType::COMPARE_LIKE: |
| 128 | + case ExpressionType::COMPARE_NOTLIKE: |
| 129 | + case ExpressionType::COMPARE_IN: |
| 130 | + case ExpressionType::COMPARE_DISTINCT_FROM: { |
| 131 | + PELOTON_ASSERT(children.size() == 2); |
| 132 | + return new expression::ComparisonExpression(GetType(), children[0], children[1]); |
| 133 | + } |
| 134 | + case ExpressionType::CONJUNCTION_AND: |
| 135 | + case ExpressionType::CONJUNCTION_OR: { |
| 136 | + PELOTON_ASSERT(children.size() == 2); |
| 137 | + return new expression::ConjunctionExpression(GetType(), children[0], children[1]); |
| 138 | + } |
| 139 | + case ExpressionType::VALUE_CONSTANT: { |
| 140 | + PELOTON_ASSERT(children.size() == 0); |
| 141 | + auto cve = static_cast<const expression::ConstantValueExpression*>(node); |
| 142 | + return new expression::ConstantValueExpression(cve->GetValue()); |
| 143 | + } |
| 144 | + default: { |
| 145 | + int type = static_cast<int>(GetType()); |
| 146 | + LOG_ERROR("Unimplemented Rebuild() for %d found", type); |
| 147 | + return nullptr; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private: |
| 153 | + const expression::AbstractExpression *node; |
| 154 | +}; |
| 155 | + |
| 156 | +class AbsExpr_Expression { |
| 157 | + public: |
| 158 | + AbsExpr_Expression(AbsExpr_Container op): op(op) {}; |
| 159 | + |
| 160 | + void PushChild(std::shared_ptr<AbsExpr_Expression> op) { |
| 161 | + children.push_back(op); |
| 162 | + } |
| 163 | + |
| 164 | + void PopChild() { |
| 165 | + children.pop_back(); |
| 166 | + } |
| 167 | + |
| 168 | + const std::vector<std::shared_ptr<AbsExpr_Expression>> &Children() const { |
| 169 | + return children; |
| 170 | + } |
| 171 | + |
| 172 | + const AbsExpr_Container &Op() const { |
| 173 | + return op; |
| 174 | + } |
| 175 | + |
| 176 | + private: |
| 177 | + AbsExpr_Container op; |
| 178 | + std::vector<std::shared_ptr<AbsExpr_Expression>> children; |
| 179 | +}; |
| 180 | + |
| 181 | +} // namespace optimizer |
| 182 | +} // namespace peloton |
| 183 | + |
0 commit comments