diff --git a/relational/optimizations.py b/relational/optimizations.py index d8829a3..cf56680 100644 --- a/relational/optimizations.py +++ b/relational/optimizations.py @@ -168,5 +168,19 @@ def subsequent_renames(n): changes+=subsequent_renames(n.left) return changes +def swap_rename_select(n): + '''This function locates things like σ k(ρ j(R)) and replaces + them with ρ j(σ k(R)). Renaming the attributes used in the + selection, so the operation is still valid.''' + #TODO document into the wiki + changes=0 + + #recoursive scan + if n.kind==optimizer.UNARY: + changes+=swap_rename_select(n.child) + elif n.kind==optimizer.BINARY: + changes+=swap_rename_select(n.right) + changes+=swap_rename_select(n.left) + return changes -general_optimizations=[duplicated_select,down_to_unions_subtractions_intersections,duplicated_projection,selection_inside_projection,subsequent_renames] +general_optimizations=[duplicated_select,down_to_unions_subtractions_intersections,duplicated_projection,selection_inside_projection,subsequent_renames,swap_rename_select] diff --git a/relational/optimizer.py b/relational/optimizer.py index fc31c16..8fa7947 100644 --- a/relational/optimizer.py +++ b/relational/optimizer.py @@ -194,8 +194,8 @@ if __name__=="__main__": #a=tokenize("(a-b*c)*(b-c)") #print tree("σ i==2 (c ᑌ d - (aᑎb))") == tree("σ i==3 (c ᑌ d - (aᑎb))") - a=general_optimize("ρ age➡a,cognome➡cogn(ρ ciccio➡age,nome➡nom(R))") + a=general_optimize("σ age==3(ρ ciccio➡age,nome➡nom(R))") #a=general_optimize("σ i==2 (σ b>5 (d))") - print '=========',a + print a #print node(a) #print tokenize("(a)") \ No newline at end of file