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 fa1f76f + 3694400 commit 6b563e2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
16 changes: 12 additions & 4 deletions GroupProjectRASQL/Heuristics/Heuristic3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,19 @@ public override bool Run(Node operation)
if (!resultsInCrossJoin(permu))// if it doesn't cause a crossjoin
{
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
/*
* 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
* Instead this optimal permutation will just be output to console.
*/

*/
Console.WriteLine("\n The Optimal permutation of leaf nodes is : ");

foreach(KeyValuePair< Node, float >op in optimalpermu)
{
Console.WriteLine(op);
}
Console.WriteLine();
break;
}

Expand Down
14 changes: 12 additions & 2 deletions GroupProjectRASQL/Heuristics/Heuristic5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ public Heuristic5(Node root) : base(root){
public override bool Run(Node operation)
{

//If the operation is not projection, this heuristic does not apply
if (!(operation.Data is Projection)) return false;

bool retVal = false;

//If the projection has a valid number of children
if(operation.Children.Count() == 1) {

Node child = operation.Child(0);

////////////////// See what the child of this node is and act accordingly ///////////////

//If child is projection, remove it from tree
if (child.Data is Projection)
{
operation.RemoveChildren();
Expand All @@ -43,7 +48,7 @@ public override bool Run(Node operation)
}



//If child is a join or cartesian:
else if (child.Data is Join || child.Data is Cartesian)
{

Expand All @@ -54,6 +59,7 @@ public override bool Run(Node operation)
HashSet<String> lSubtreeFields = GetFields(child.Child(0));
HashSet<String> rSubtreeFields = GetFields(child.Child(1));

//If there is no condition, the projection can be moved completely
if (child.Data is Cartesian || conditionFields.IsSubsetOf(projectFields)) {

//Remove operation from tree and connect parent and child
Expand All @@ -65,8 +71,11 @@ public override bool Run(Node operation)
retVal = true;
}

//See what fields the sub projections need
lSubtreeFields.IntersectWith(conditionFields.Union(projectFields));
rSubtreeFields.IntersectWith(conditionFields.Union(projectFields));

//Create new subtrees
Node rightSubTree = new Node(new Projection(rSubtreeFields));
Node leftSubTree = new Node(new Projection(lSubtreeFields));

Expand Down Expand Up @@ -112,7 +121,7 @@ public override bool Run(Node operation)




//Create new subtrees
Node leftSubTree = new Node(new Projection(operation.Data.parameter));
Node rightSubTree = new Node(new Projection(operation.Data.parameter));

Expand Down Expand Up @@ -176,6 +185,7 @@ public override bool Run(Node operation)
return retVal;
}

//Function to get fields of subtree
public static HashSet<String> GetFields(Node node) {

if (!(node.Data is Operation)) return null;
Expand Down
18 changes: 18 additions & 0 deletions GroupProjectRASQL/Parser/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@

namespace GroupProjectRASQL.Parser
{
//A state represents a full or partial parse of a substring of the string given to the parser
class State : IEquatable<State>
{

//The non terminal of a rule you are trying to find a valid parse for
private String nonterminal;

//The rest of the rule you are trying to find a valid parse for
private String[] expression;

//How many expressions we have found a valid parse for so far
private int dot;

//The starting position of the substring we're parsing
private int origin;

//The ending position of the substring we're parsing. Not initialised until the state is finished
private int? destination;

public State(String nonterminal, String[] expression, int dot = 0, int origin = 0, int? destination = null)
Expand All @@ -38,32 +49,39 @@ public String getNonterminal()
return nonterminal;
}

//Returns whetehr this state represents a terminal symbol
public bool isTerminal()
{
return nonterminal == null;
}


//returns true if this state is equal to oher
public bool Equals(State other)
{
return null != other && nonterminal == other.nonterminal && expression == other.expression && dot == other.dot && origin == other.origin && destination == other.destination;
}

//Return true if we have found valid parse for every expression
public bool isFinished()
{
return this.dot >= this.expression.Length;
}

//Get next symbol after dot
public String nextSymbol()
{
if (isFinished()) return null;
return this.expression[this.dot];
}

//Return a new state that has the dot one place to the right
public State nextState()
{
return new State(nonterminal, expression, dot + 1, origin);
}

//Get the state as a string. For debigging purposes
public override string ToString()
{
String before = dot > 0 ? expression.SubArray(0, dot).Aggregate((merge, next) => merge += next): "";
Expand Down
24 changes: 22 additions & 2 deletions GroupProjectRASQL/Translator/sqlToRa.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,36 @@ public static String TranslateQuery(Node node) {



//If there is no asterisk add a projection
//If there is an asterisk, there is no selection
bool isSelect = node.Child(0).Child(1).Child(0).Data != "*";

//Create dictionary to store renamed attributes and what they're renamed to
Dictionary<String, String> attributeRenames = new Dictionary<string, string>();

//If there is a select:
if (isSelect)
{

//Translate the select list and store renames
String selectList = TranslateSelectList(node.Child(0).Child(1), ref attributeRenames);

//Add the renames to string
foreach (KeyValuePair<String, String> entry in attributeRenames)
{

returnString += "ρ " + entry.Value + "/" + entry.Key + " (";

}

//Add translated select list
returnString += "π " + selectList + " (";

}

//If there is a where clause, then add a select
bool isWhere = node.Children.Count() == 6 && node.Child(4).Data == "[where]";

//If there is a where, add the where clause and the condition
if (isWhere)
{

Expand All @@ -54,6 +59,7 @@ public static String TranslateQuery(Node node) {
}


//Add the translated from list
returnString += TranslateFromList(node.Child(2).Child(1));

//Close open brackets
Expand All @@ -64,6 +70,7 @@ public static String TranslateQuery(Node node) {

}

//Returns a translated RA equivalent to a select list node
public static String TranslateSelectList(Node node, ref Dictionary<String, String> renames) {

String returnString = "";
Expand All @@ -72,6 +79,7 @@ public static String TranslateSelectList(Node node, ref Dictionary<String, Strin

String name = element.Child(0).TreeToString();

//If the select list contains a rename, add it to the dictionary
bool isRename = element.Children.Count() == 3;
if (isRename)
{
Expand All @@ -83,13 +91,15 @@ public static String TranslateSelectList(Node node, ref Dictionary<String, Strin

}


returnString += name;


//If there are three elements, element 0 is the first element of list, 1 is a comma and 2 is another select list
if (node.Children.Count() == 3)
{

returnString += ",";
//translate rest of list and att it to string
returnString += TranslateSelectList(node.Child(2), ref renames);

}
Expand All @@ -98,16 +108,20 @@ public static String TranslateSelectList(Node node, ref Dictionary<String, Strin

}

//Takes a from list node and returns an equivalent ra string
public static String TranslateFromList(Node node) {

String returnString = "";

//If there are 3 elements, there is a cartesian product between the first and last
bool isCartesian = node.Children.Count() == 3;

if (isCartesian) returnString += "X(";

//Translate and add the from element to the list
returnString += TranslateFromElement(node.Child(0));

//Translate rest of list
if (isCartesian) {

returnString += ", ";
Expand All @@ -121,10 +135,12 @@ public static String TranslateFromList(Node node) {

}

//Takes a from element node and returns equivalent RA string
public static String TranslateFromElement(Node node) {

String returnString = "";

//3 elements in element means there is a table rename
if (node.Children.Count() == 3)
{

Expand All @@ -133,6 +149,7 @@ public static String TranslateFromElement(Node node) {
returnString += ")";

}
//4 elements: named subquery
else if (node.Children.Count() == 4)
{

Expand All @@ -141,12 +158,14 @@ public static String TranslateFromElement(Node node) {
returnString += ")";

}
// join
else if (node.Child(0).Data == "[join]")
{

returnString += TranslateJoin(node.Child(0));

}
//Otherwise, its just a relation
else {

returnString = node.TreeToString();
Expand All @@ -157,6 +176,7 @@ public static String TranslateFromElement(Node node) {

}

//Takes a join node and returns equivalent RA string
public static String TranslateJoin(Node node) {

String returnString = "";
Expand Down

0 comments on commit 6b563e2

Please sign in to comment.