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

Commit 2070980

Browse files
author
GustavoAngulo
committed
Created class and constructor + check
1 parent d35f466 commit 2070980

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/include/common/internal_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,7 @@ std::ostream &operator<<(std::ostream &os, const PropertyType &type);
13041304
enum class RuleType : uint32_t {
13051305
// Transformation rules (logical -> logical)
13061306
INNER_JOIN_COMMUTE = 0,
1307+
INNER_JOIN_ASSOCIATE,
13071308

13081309
// Don't move this one
13091310
LogicalPhysicalDelimiter,

src/include/optimizer/rule_impls.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ class InnerJoinCommutativity : public Rule {
3838
OptimizeContext *context) const override;
3939
};
4040

41+
/**
42+
* @brief (A join B) join C -> A join (B join C)
43+
*/
44+
45+
class InnerJoinAssociativity : public Rule {
46+
public:
47+
InnerJoinAssociativity();
48+
49+
bool Check(std::shared_ptr<OperatorExpression> plan,
50+
OptimizeContext *context) const override;
51+
52+
void Transform(std::shared_ptr<OperatorExpression> input,
53+
std::vector<std::shared_ptr<OperatorExpression>> &transformed,
54+
OptimizeContext *context) const override;
55+
};
56+
4157
//===--------------------------------------------------------------------===//
4258
// Implementation rules
4359
//===--------------------------------------------------------------------===//

src/optimizer/rule_impls.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,51 @@ void InnerJoinCommutativity::Transform(
6868
transformed.push_back(result_plan);
6969
}
7070

71+
///////////////////////////////////////////////////////////////////////////////
72+
/// InnerJoinAssociativity
73+
InnerJoinAssociativity::InnerJoinAssociativity() {
74+
type_ = RuleType::INNER_JOIN_ASSOCIATE;
75+
76+
// Create left nested join
77+
auto left_child = std::make_shared<Pattern>(OpType::InnerJoin);
78+
left_child->AddChild(std::make_shared<Pattern>(OpType::Leaf));
79+
left_child->AddChild(std::make_shared<Pattern>(OpType::Leaf));
80+
81+
std::shared_ptr<Pattern> right_child(std::make_shared<Pattern>(OpType::Leaf));
82+
83+
match_pattern = std::make_shared<Pattern>(OpType::InnerJoin);
84+
match_pattern->AddChild(left_child);
85+
match_pattern->AddChild(right_child);
86+
}
87+
88+
//TODO: As far as I know, theres nothing else that needs to be checked
89+
bool InnerJoinAssociativity::Check(std::shared_ptr<OperatorExpression> expr,
90+
OptimizeContext *context) const {
91+
(void)context;
92+
(void)expr;
93+
return true;
94+
}
95+
96+
void InnerJoinAssociativity::Transform(
97+
std::shared_ptr<OperatorExpression> input,
98+
std::vector<std::shared_ptr<OperatorExpression>> &transformed,
99+
UNUSED_ATTRIBUTE OptimizeContext *context) const {
100+
auto join_op = input->Op().As<LogicalInnerJoin>();
101+
auto join_predicates =
102+
std::vector<AnnotatedExpression>(join_op->join_predicates);
103+
auto result_plan = std::make_shared<OperatorExpression>(
104+
LogicalInnerJoin::make(join_predicates));
105+
std::vector<std::shared_ptr<OperatorExpression>> children = input->Children();
106+
PL_ASSERT(children.size() == 2);
107+
LOG_TRACE(
108+
"Reorder left child with op %s and right child with op %s for inner join",
109+
children[0]->Op().GetName().c_str(), children[1]->Op().GetName().c_str());
110+
result_plan->PushChild(children[1]);
111+
result_plan->PushChild(children[0]);
112+
113+
transformed.push_back(result_plan);
114+
}
115+
71116
//===--------------------------------------------------------------------===//
72117
// Implementation rules
73118
//===--------------------------------------------------------------------===//

0 commit comments

Comments
 (0)