Skip to content

Commit e738acb

Browse files
GustavoAnguloapavlo
authored andcommitted
Fix for optimizer rule bug (cmu-db#1468)
* False negative in rule bug * Fix for false positive rule bug * Remove print
1 parent 1de8979 commit e738acb

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

src/optimizer/group_expression.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,21 @@ hash_t GroupExpression::Hash() const {
8787
bool GroupExpression::operator==(const GroupExpression &r) {
8888
bool eq = (op == r.Op());
8989

90-
for (size_t i = 0; i < child_groups.size(); ++i) {
91-
eq = eq && (child_groups[i] == r.child_groups[i]);
90+
auto left_groups = child_groups;
91+
auto right_groups = r.child_groups;
92+
93+
std::sort(left_groups.begin(), left_groups.end());
94+
95+
std::sort(right_groups.begin(), right_groups.end());
96+
for (size_t i = 0; i < left_groups.size(); ++i) {
97+
eq = eq && (left_groups[i] == right_groups[i]);
9298
}
9399

94100
return eq;
95101
}
96102

97103
void GroupExpression::SetRuleExplored(Rule *rule) {
98-
rule_mask_.set(rule->GetRuleIdx()) = true;
104+
rule_mask_.set(rule->GetRuleIdx(), true);
99105
}
100106

101107
bool GroupExpression::HasRuleExplored(Rule *rule) {

src/optimizer/memo.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ GroupExpression *Memo::InsertExpression(std::shared_ptr<GroupExpression> gexpr,
4343
auto it = group_expressions_.find(gexpr.get());
4444

4545
if (it != group_expressions_.end()) {
46-
PELOTON_ASSERT(target_group == UNDEFINED_GROUP ||
47-
target_group == (*it)->GetGroupID());
4846
gexpr->SetGroupID((*it)->GetGroupID());
4947
return *it;
5048
} else {

test/optimizer/optimizer_rule_test.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -261,5 +261,32 @@ TEST_F(OptimizerRuleTests, SimpleAssociativeRuleTest2) {
261261
delete root_context;
262262
}
263263

264+
TEST_F(OptimizerRuleTests, RuleBitmapTest) {
265+
Optimizer optimizer;
266+
auto &memo = optimizer.GetMetadata().memo;
267+
268+
auto dummy_operator = std::make_shared<OperatorExpression>(LogicalGet::make());
269+
auto dummy_group = memo.InsertExpression(optimizer.GetMetadata().MakeGroupExpression(dummy_operator), false);
270+
271+
auto rule1 = new InnerJoinCommutativity();
272+
auto rule2 = new GetToSeqScan();
273+
274+
EXPECT_FALSE(dummy_group->HasRuleExplored(rule1));
275+
EXPECT_FALSE(dummy_group->HasRuleExplored(rule2));
276+
277+
dummy_group->SetRuleExplored(rule1);
278+
279+
EXPECT_TRUE(dummy_group->HasRuleExplored(rule1));
280+
EXPECT_FALSE(dummy_group->HasRuleExplored(rule2));
281+
282+
dummy_group->SetRuleExplored(rule2);
283+
284+
EXPECT_TRUE(dummy_group->HasRuleExplored(rule1));
285+
EXPECT_TRUE(dummy_group->HasRuleExplored(rule2));
286+
287+
delete rule1;
288+
delete rule2;
289+
}
290+
264291
} // namespace test
265292
} // namespace peloton

test/sql/testing_sql_util.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ ResultType TestingSQLUtil::ExecuteSQLQuery(
9898
return status;
9999
}
100100

101+
void PrintPlan(planner::AbstractPlan *plan, int level = 0) {
102+
auto spacing = std::string(level, '\t');
103+
if (plan->GetPlanNodeType() == PlanNodeType::SEQSCAN) {
104+
auto scan = dynamic_cast<planner::AbstractScan *>(plan);
105+
LOG_INFO("%s%s(%s)", spacing.c_str(), scan->GetInfo().c_str(),
106+
scan->GetTable()->GetName().c_str());
107+
} else {
108+
LOG_INFO("%s%s", spacing.c_str(), plan->GetInfo().c_str());
109+
}
110+
for (size_t i = 0; i < plan->GetChildren().size(); i++) {
111+
PrintPlan(plan->GetChildren()[i].get(), level + 1);
112+
}
113+
return;
114+
}
115+
101116
// Execute a SQL query end-to-end with the specific optimizer
102117
ResultType TestingSQLUtil::ExecuteSQLQueryWithOptimizer(
103118
std::unique_ptr<optimizer::AbstractOptimizer> &optimizer,

0 commit comments

Comments
 (0)