Skip to content

[feature] use bit mask to avoid potential stack over flow if DFS #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions src/main/java/simpledb/optimizer/JoinOptimizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,30 @@ private <T> void subsetDfs(final Set<Set<T>> result, final Set<T> path, final Li
}
}

public <T> HashMap<Integer, Set<Set<T>>> buildMap(List<T> v) {
final HashMap<Integer, Set<Set<T>>> result = new HashMap<>();
int n = v.size();
int state = 0;
while(state < (1 << n)){
Set<T> cur = new HashSet<>();
for(int j = 0; j < n; j ++ ){
if(((state >> j) & 1) == 1){
cur.add(v.get(j));
}
}
int count = Integer.bitCount(state);
if(result.containsKey(count)){
result.get(count).add(cur);
}else{
Set<Set<T>> value = new HashSet<>();
value.add(cur);
result.put(count, value);
}
state ++ ;
}
return result;
}

/**
* Compute a logical, reasonably efficient join on the specified tables. See
* PS4 for hints on how this should be implemented.
Expand All @@ -259,24 +283,30 @@ private <T> void subsetDfs(final Set<Set<T>> result, final Set<T> path, final Li
* when stats or filter selectivities is missing a table in the
* join, or or when another internal error occurs
*/
public List<LogicalJoinNode> orderJoins(Map<String, TableStats> stats, Map<String, Double> filterSelectivities,
boolean explain) throws ParsingException {
final PlanCache pc = new PlanCache();
public List<LogicalJoinNode> orderJoins(
Map<String, TableStats> stats,
Map<String, Double> filterSelectivities, boolean explain)
throws ParsingException {

PlanCache pc = new PlanCache();
int n = this.joins.size();

CostCard costCard = null;
for (int i = 1; i <= this.joins.size(); i++) {
final Set<Set<LogicalJoinNode>> subsets = enumerateSubsets(this.joins, i);
for (final Set<LogicalJoinNode> subPlan : subsets) {
HashMap<Integer, Set<Set<LogicalJoinNode>>> sizeToJoins = buildMap(this.joins);
for(int i = 1; i <= n; i ++ ){
Set<Set<LogicalJoinNode>> sets = sizeToJoins.get(i);
for(Set<LogicalJoinNode> subset: sets){
double bestCost = Double.MAX_VALUE;
for (final LogicalJoinNode removeNode : subPlan) {
final CostCard cc = computeCostAndCardOfSubplan(stats, filterSelectivities, removeNode, subPlan,
bestCost, pc);
if (cc != null) {
for(LogicalJoinNode removedNode: subset){
final CostCard cc = computeCostAndCardOfSubplan(stats, filterSelectivities, removedNode, subset,
bestCost, pc);
if(cc != null){
bestCost = cc.cost;
costCard = cc;
}
}
if (bestCost != Double.MAX_VALUE) {
pc.addPlan(subPlan, bestCost, costCard.card, costCard.plan);
if(bestCost != Double.MAX_VALUE){
pc.addPlan(subset, bestCost, costCard.card, costCard.plan);
}
}
}
Expand Down