forked from cmu-db/peloton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_expression.cpp
100 lines (81 loc) · 3.03 KB
/
group_expression.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//===----------------------------------------------------------------------===//
//
// Peloton
//
// group_expression.cpp
//
// Identification: src/optimizer/group_expression.cpp
//
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include "common/internal_types.h"
#include "optimizer/group_expression.h"
#include "optimizer/group.h"
#include "optimizer/rule.h"
namespace peloton {
namespace optimizer {
//===--------------------------------------------------------------------===//
// Group Expression
//===--------------------------------------------------------------------===//
GroupExpression::GroupExpression(Operator op, std::vector<GroupID> child_groups)
: group_id(UNDEFINED_GROUP),
op(op),
child_groups(child_groups),
stats_derived_(false) {}
GroupID GroupExpression::GetGroupID() const { return group_id; }
void GroupExpression::SetGroupID(GroupID id) { group_id = id; }
void GroupExpression::SetChildGroupID(int child_group_idx, GroupID group_id) {
child_groups[child_group_idx] = group_id;
}
const std::vector<GroupID> &GroupExpression::GetChildGroupIDs() const {
return child_groups;
}
GroupID GroupExpression::GetChildGroupId(int child_idx) const {
return child_groups[child_idx];
}
Operator GroupExpression::Op() const { return op; }
double GroupExpression::GetCost(
std::shared_ptr<PropertySet> &requirements) const {
return std::get<0>(lowest_cost_table_.find(requirements)->second);
}
std::vector<std::shared_ptr<PropertySet>> GroupExpression::GetInputProperties(
std::shared_ptr<PropertySet> requirements) const {
return std::get<1>(lowest_cost_table_.find(requirements)->second);
}
void GroupExpression::SetLocalHashTable(
const std::shared_ptr<PropertySet> &output_properties,
const std::vector<std::shared_ptr<PropertySet>> &input_properties_list,
double cost) {
auto it = lowest_cost_table_.find(output_properties);
if (it == lowest_cost_table_.end()) {
// No other cost to compare against
lowest_cost_table_.insert(std::make_pair(
output_properties, std::make_tuple(cost, input_properties_list)));
} else {
// Only insert if the cost is lower than the existing cost
if (std::get<0>(it->second) > cost) {
lowest_cost_table_[output_properties] =
std::make_tuple(cost, input_properties_list);
}
}
}
hash_t GroupExpression::Hash() const {
size_t hash = op.Hash();
for (size_t i = 0; i < child_groups.size(); ++i) {
hash = HashUtil::CombineHashes(hash,
HashUtil::Hash<GroupID>(&(child_groups[i])));
}
return hash;
}
bool GroupExpression::operator==(const GroupExpression &r) {
return (op == r.Op()) && (child_groups == r.child_groups);
}
void GroupExpression::SetRuleExplored(Rule *rule) {
rule_mask_.set(rule->GetRuleIdx(), true);
}
bool GroupExpression::HasRuleExplored(Rule *rule) {
return rule_mask_.test(rule->GetRuleIdx());
}
} // namespace optimizer
} // namespace peloton