Skip to content

Commit

Permalink
Merge branch 'master' of github.com:TGNThump/COMP208G17
Browse files Browse the repository at this point in the history
  • Loading branch information
TGNThump committed May 2, 2018
2 parents cef5464 + 0ba726e commit 2272b54
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 562 deletions.
18 changes: 9 additions & 9 deletions GroupProjectRASQL/Heuristics/Heuristic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ public abstract class Heuristic

protected Queue<Node> remainingNodes = new Queue<Node>();

public Heuristic(Node root)
public Heuristic(Node root) // create the heuristics- saving the root of the tree
{
this.root = root;
}

public void Init()
public void Init() // init the heuristic
{
isStarted = true;
remainingNodes = new Queue<Node>();
for (Node node = root.Child(); node != null && node != root; node = node.getNextNode())
isStarted = true; // istarted flag
remainingNodes = new Queue<Node>(); // queue for the nodes
for (Node node = root.Child(); node != null && node != root; node = node.getNextNode()) // for every non-null non-root node
{
remainingNodes.Enqueue(node);
remainingNodes.Enqueue(node); // add it to the queue
}
}

public void Step()
public void Step() // step through the currently active heuristic - called by ui button
{
if (!isStarted) Init();
if (isComplete) return;
Expand All @@ -46,9 +46,9 @@ public void Step()
if (!stop && !isComplete) Step();
}

public void Complete()
public void Complete() // complete the currently active heurisitc - called by ui button
{
while (!isComplete) Step();
while (!isComplete) Step(); // while not done - step without interuptions
}

public void Reset()
Expand Down
69 changes: 40 additions & 29 deletions GroupProjectRASQL/Heuristics/Heuristic2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,72 @@ public Heuristic2(Node root) : base(root){}
public override bool Run(Node operation)
{
if (!(operation.Data is Selection)) { return false; } // if the current node is a selection

Node newChild = null;
Selection selection = (Selection)operation.Data; // cast it to selection

IEnumerable<String> relationNames = selection.getFieldNames().Select(name => name.Split('.')[0]).Distinct();
if (relationNames.Count() < 1) { return false; }
if (relationNames.Count() == 1)
IEnumerable<String> relationNames = selection.getFieldNames().Select(name => name.Split('.')[0]).Distinct(); // Get the relation. portion of the conditions of a selection and add them to a ienumerable
if (relationNames.Count() < 1) { return false; } // If it accesses no tables - an error case - return false
if (relationNames.Count() == 1)// if it accesses one table it will be moved above it
{
Console.WriteLine("Relations = 1");
//Console.WriteLine("Relations = 1"); //


newChild = operation.Where(node =>
newChild = operation.Where(node => // Search the reset of the tree
{
if (node.Data is Relation) return ((Relation)node.Data).name == relationNames.Single();
if (node.Data is RenameRelation) return ((RenameRelation)node.Data).getNewName() == relationNames.Single();
return false;
}).SingleOrDefault();
if (node.Data is Relation) return ((Relation)node.Data).name == relationNames.Single(); // if its a relation with the first ( and only in this case ) name return it
if (node.Data is RenameRelation) return ((RenameRelation)node.Data).getNewName() == relationNames.Single(); // or if its a renamed relation with the same name return it
return false;//else don't
}).SingleOrDefault();//make sure there is only one- then convert the output from a list/Ienumerable to a single Node
}



if (relationNames.Count() > 1)
if (relationNames.Count() > 1) // If it accesses multiple tables, it will be moved over the join that accesses all of them.
{
Console.WriteLine("Relations > 1");
//Console.WriteLine("Relations > 1");

newChild = operation.Where(Node =>
newChild = operation.Where(Node => // find the join the select should go over
{
if (Node.Data is Join)
if (Node.Data is Join) // if join
{
Join join = (Join)Node.Data; // cast it to a join
IEnumerable<String> joins = join.getFieldNames();
bool isCorrectFlag = true;
foreach (String table in joins)
IEnumerable<Node> joins = Node.Where(NodeTables => // find the tables this join accesses by searching the tree below it for relations
{
Console.WriteLine(table);
if (!relationNames.Contains(table))
if (NodeTables.Data is Relation)//if relation
{
isCorrectFlag = false;
return true;//return it
}
return false;//else don't
});
List<String> joinnames = new List<String>(); // make a list for the names
foreach( Node node in joins) // convert the node positions into name strings for comparison
{
Relation re = (Relation)node.Data;
joinnames.Add(re.name);
}//
foreach(String relationName in relationNames) // for each relation in the selected to be moved
{
if (!(joinnames.Contains(relationName))) { return false; }// if it is not contained in the list of relations the join accesses, this join cannot be the correct position so return false.
}
return isCorrectFlag;
return true;// if it is, in all cases, it must be the correct position, so return true.
}
return false;
}).SingleOrDefault();
return false;// if there are no joins - error case - return false
}).SingleOrDefault();// make sure it only returns a single node

}

for (Node current = newChild; !current.Equals(operation); current = current.Parent)

for (Node current = newChild; !current.Equals(operation); current = current.Parent) // check if there is a rename relation between the newchild ( the position to move over ) and the current select
{
if (current.Data is RenameAttribute) { return false; }
if (current.Data is RenameAttribute) { return false; }// if their is, the select cannot be moved - so return false.
}



// Peform the move
operation.Parent.RemoveChild(operation);
operation.Parent.AddChildren(operation.Children);
operation.RemoveChildren();
Expand All @@ -80,7 +91,7 @@ public override bool Run(Node operation)
newChild.Parent.RemoveChild(newChild);
newParent.AddChild(operation);
operation.AddChild(newChild);

return true;
}
}
Expand Down
124 changes: 53 additions & 71 deletions GroupProjectRASQL/Heuristics/Heuristic3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,160 +22,142 @@ public class Heuristic3 : Heuristic

public Heuristic3(Node root) : base(root)
{
isRun = false;
isRun = false; // There are alot of one time things in this heuristic so there is a flag to prevent unnessaccery execution of code
}

public override bool Run(Node operation)
{

if (!isRun)
if (!isRun) // if this is the first time
{
isRun = true;
SelectionList = root.Where(node =>
isRun = true; // Set the has been run before tag to true
SelectionList = root.Where(node => // collect selections into one list
{
if (node.Data is Selection)
if (node.Data is Selection)//if selection
{
return true;
return true;// add to list
}
return false;
return false;// else don't
});
RelationList = root.Where(node =>
RelationList = root.Where(node =>//collect relations
{
if (node.Data is Relation)
if (node.Data is Relation)//if relation
{
return true;
return true;//add
}
return false;
return false;//don't
});

foreach ( Node re in RelationList)
foreach ( Node re in RelationList) // convert the relation list into a dictionary with the worse case scenario
{
RelationDict.Add(re, 1);
RelationDict.Add(re, 1); // worst case ( everything is unique ) is a selectivity ratio of 1
}

foreach (Node Selection in SelectionList)
foreach (Node Selection in SelectionList) // foreach selection
{
foreach( String field in Selection.Data.getFieldNames())
foreach( String field in Selection.Data.getFieldNames()) // foreach field of each selection
{
string[] fieldsplit = field.Split('.');
string[] fieldsplit = field.Split('.'); // split them - getting the relation names

Node fieldSplitNode = operation.Where(node =>
Node fieldSplitNode = operation.Where(node => // Find the relation pointers based on there names.
{
if (node.Data is Relation) return ((Relation)node.Data).name == fieldsplit[0];
if (node.Data is RenameRelation) return ((RenameRelation)node.Data).getNewName() == fieldsplit[0];
return false;
}).SingleOrDefault();

RelationDict[fieldSplitNode] *= selectivityEstimate(fieldSplitNode, fieldsplit[1]);
RelationDict[fieldSplitNode] *= selectivityEstimate(fieldSplitNode, fieldsplit[1]);// Find the total selectivity, by timesing

}
}


RelationDictList = RelationDict.ToList();
RelationDictList = RelationDict.ToList(); // Convert the relation dictionary to a list of KVPs so it can be sorted

RelationDictList.Sort(
RelationDictList.Sort( // sort this list on the value - smallest first.
delegate (KeyValuePair<Node, float> pair1,
KeyValuePair<Node, float> pair2)
{
return pair1.Value.CompareTo(pair2.Value);
}
);
IList<KeyValuePair<Node, float>> optimalpermu;
foreach (var permu in Permutate(RelationDictList, RelationDictList.Count))
foreach (var permu in Permutate(RelationDictList, RelationDictList.Count))// move through possible permutations of the leaf nodes in optimality order.
{

if (!resultsInCrossJoin(permu))
if (!resultsInCrossJoin(permu))// if it doesn't cause a crossjoin
{
optimalpermu = permu;
optimalpermu = permu;// then this is the optimal leaf order
/*
* Unfortunatly due to time constraints the manipulation of the graph to cause this could not be completed.
* This would handle moving the selects and joins to impletement this order
*/
break;
}

foreach ( var kvp in permu)
{
Console.Write(kvp.Key);
Console.Write(" : ");
Console.Write(kvp.Value);
Console.Write("\n");

}
}









}















return false;
}

public float selectivityEstimate(Node relation, String field)
public float selectivityEstimate(Node relation, String field) // Selectivity estimate
{
Relation relationData = (Relation)relation.Data;
return (float)relationData.GetField(field).getDistinctCount() / (float)relationData.GetField(field).getCount();
return (float)relationData.GetField(field).getDistinctCount() / (float)relationData.GetField(field).getCount(); // Take the number of unique fields and divide it by the total fields to get a estimate ration.
}

public bool resultsInCrossJoin(IList<KeyValuePair<Node, float>> permu)
public bool resultsInCrossJoin(IList<KeyValuePair<Node, float>> permu)// will it result in a cross join
{
bool possibleFlag;
for (int i = 0; i<permu.Count()-1;i++)
bool possibleFlag; // flag
for (int i = 0; i<permu.Count()-1;i++) // for every key value pair in the permutation passed to this function
{
possibleFlag = false;
foreach(Node currentSelect in SelectionList)
foreach (Node currentSelect in SelectionList) // for each select
{
if (currentSelect.Contains(permu[i].Key))
List<String> currentSelectList = new List<String>();
foreach(String field in currentSelect.Data.getFieldNames()) // get the tables it acts on
{
currentSelectList.Add(field.Split('.')[0]);
}

Relation permui= (Relation)permu[i].Key.Data;// Cast the permutations
Relation permupitwo = (Relation)permu[i+1].Key.Data;



if (currentSelectList.Contains(permui.name))
{
if (currentSelect.Contains(permu[i+1].Key))
if (currentSelectList.Contains(permupitwo.name))
{
possibleFlag = true;
possibleFlag = true; // if the select acts on both the tables in some way- switch the flag to true- as this means a cross join doesn't need to be created to use it
}
}
}
if (!possibleFlag) { return true; }// if its not possible, return that it results in a corssjoin
}
Console.WriteLine("Asking for crossjoins");
Console.WriteLine(permu);


return true;
return false;// else return it doesn't cause one.
}


public void RotateRight<T>(IList<T> sequence, int count)
public void RotateRight<T>(IList<T> sequence, int count)// Permutation
{
T tmp = sequence[count - 1];
sequence.RemoveAt(count - 1);
sequence.Insert(0, tmp);
}

public IEnumerable<IList<KeyValuePair<Node, float>>> Permutate(IList<KeyValuePair<Node, float>> sequence, int count)
public IEnumerable<IList<KeyValuePair<Node, float>>> Permutate(IList<KeyValuePair<Node, float>> sequence, int count) // Permutaton recursive function
{
Console.WriteLine("Asking for permutations");
Console.WriteLine(count);

if (count == 1) yield return sequence;
if (count == 1) yield return sequence;//base case
else
{
for (int i = 0; i < count; i++)
Expand Down
Loading

0 comments on commit 2272b54

Please sign in to comment.