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

Commit c8c29e0

Browse files
author
GustavoAngulo
committed
Transform function
1 parent 361e8c4 commit c8c29e0

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

src/optimizer/rule_impls.cpp

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,12 @@ void InnerJoinAssociativity::Transform(
103103
// NOTE: Transforms (left JOIN middle) JOIN right -> left JOIN (middle JOIN right)
104104
// Variables are named accordingly to above transformation
105105

106-
107-
108-
// auto result_plan = std::make_shared<OperatorExpression>(
109-
// LogicalInnerJoin::make(join_predicates));
110-
111106
auto parent_join = input->Op().As<LogicalInnerJoin>();
112107
std::vector<std::shared_ptr<OperatorExpression>> children = input->Children();
113108
auto child_join = children[0]->Op().As<LogicalInnerJoin>();
109+
auto left = children[0]->Children()[0];
110+
auto middle = children[0]->Children()[1];
111+
auto right = children[1];
114112
PL_ASSERT(children.size() == 2);
115113
PL_ASSERT(children[0]->Op().GetType() == OpType::InnerJoin);
116114
PL_ASSERT(children[0]->Children().size() == 2);
@@ -128,8 +126,6 @@ void InnerJoinAssociativity::Transform(
128126
const auto &right_group_aliases_set =
129127
memo.GetGroupByID(right_group_id)->GetTableAliases();
130128

131-
132-
133129
// Redistribute predicates
134130
auto parent_join_predicates = std::vector<AnnotatedExpression>(parent_join->join_predicates);
135131
auto child_join_predicates = std::vector<AnnotatedExpression>(child_join->join_predicates);
@@ -138,19 +134,41 @@ void InnerJoinAssociativity::Transform(
138134
predicates.insert(predicates.end(), parent_join_predicates.begin(), parent_join_predicates.end());
139135
predicates.insert(predicates.end(), child_join_predicates.begin(), child_join_predicates.end());
140136

141-
// for (auto predicate : predicates) {
142-
//
143-
// }
144-
//
145-
//
146-
//
147-
// LOG_TRACE(
148-
// "Reorder left child with op %s and right child with op %s for inner join",
149-
// children[0]->Op().GetName().c_str(), children[1]->Op().GetName().c_str());
150-
// result_plan->PushChild(children[1]);
151-
// result_plan->PushChild(children[0]);
152-
//
153-
// transformed.push_back(result_plan);
137+
std::vector<AnnotatedExpression> new_child_join_predicates;
138+
std::vector<AnnotatedExpression> new_parent_join_predicates;
139+
140+
//TODO: This assumes that predicate pushdown has not occured yet, as it will put all non-join predicates into parent join
141+
for (auto predicate : predicates) {
142+
143+
// New child join predicate must contain middle and right group
144+
if (util::IsSubset(middle_group_aliases_set, predicate.table_alias_set) &&
145+
util::IsSubset(right_group_aliases_set, predicate.table_alias_set))
146+
new_child_join_predicates.emplace_back(predicate);
147+
else
148+
new_parent_join_predicates.emplace_back(predicate);
149+
}
150+
151+
// Construct new child join operator
152+
std::shared_ptr<OperatorExpression> new_child_join =
153+
std::make_shared<OperatorExpression>(
154+
LogicalInnerJoin::make(new_child_join_predicates));
155+
new_child_join->PushChild(middle);
156+
new_child_join->PushChild(right);
157+
158+
// Construct new parent join operator
159+
std::shared_ptr<OperatorExpression> new_parent_join =
160+
std::make_shared<OperatorExpression>(
161+
LogicalInnerJoin::make(new_parent_join_predicates));
162+
new_parent_join->PushChild(left);
163+
new_parent_join->PushChild(new_child_join);
164+
165+
166+
LOG_TRACE(
167+
"Reordered join structured: (%s JOIN %s) JOIN %s",
168+
left->Op().GetName().c_str(), middle->Op().GetName().c_str(), right->Op().GetName().c_str());
169+
170+
transformed.push_back(new_parent_join);
171+
154172
}
155173

156174
//===--------------------------------------------------------------------===//

0 commit comments

Comments
 (0)