From 723c63641d3a496cb29269be4416bf219632bdef Mon Sep 17 00:00:00 2001 From: LtWorf Date: Thu, 30 Apr 2009 17:39:07 +0000 Subject: [PATCH] added optimization to push down selection when sub-expression has union, intersection or subtraction git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@142 014f5005-505e-4b48-8d0a-63407b615a7c --- relational/optimizations.py | 46 ++++++++++++++++++++++++++++++++++++- relational/optimizer.py | 4 ++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/relational/optimizations.py b/relational/optimizations.py index 2aaa192..60d2374 100644 --- a/relational/optimizations.py +++ b/relational/optimizations.py @@ -43,4 +43,48 @@ def duplicated_select(n): changes+=duplicated_select(n.left) return changes -general_optimizations=[duplicated_select] +def down_to_unions_subtractions_intersections(n): + '''This funcion locates things like σ i==2 (c ᑌ d), where the union + can be a subtraction and an intersection and replaces them with + σ i==2 (c) ᑌ σ i==2(d). + + If the operator is not Union and the right expression is a relation, + the resulting expression will be: σ i==2 (c) - d. + + ''' + changes=0 + _o=('ᑌ','-','ᑎ') + if n.name=='σ' and n.child.name in _o: + + left=optimizer.node() + left.prop=n.prop + left.name=n.name + left.child=n.child.left + left.kind=optimizer.UNARY + if n.child.name=='ᑌ' or n.child.right.kind!=optimizer.RELATION: + right=optimizer.node() + right.prop=n.prop + right.name=n.name + right.child=n.child.right + right.kind=optimizer.UNARY + else: + right=n.child.right + + n.name=n.child.name + n.left=left + n.right=right + n.child=None + n.prop=None + n.kind=optimizer.BINARY + changes+=1 + + #recoursive scan + if n.kind==optimizer.UNARY: + changes+=down_to_unions_subtractions_intersections(n.child) + elif n.kind==optimizer.BINARY: + changes+=down_to_unions_subtractions_intersections(n.right) + changes+=down_to_unions_subtractions_intersections(n.left) + return changes + + +general_optimizations=[duplicated_select,down_to_unions_subtractions_intersections] diff --git a/relational/optimizer.py b/relational/optimizer.py index db3d5b9..ad37c5f 100644 --- a/relational/optimizer.py +++ b/relational/optimizer.py @@ -34,7 +34,7 @@ class node (object): '''This class is a node of a relational expression. Leaves are relations and internal nodes are operations.''' kind=None - def __init__(self,expression): + def __init__(self,expression=None): if expression==None or len(expression)==0: return '''Generates the tree from the tokenized expression''' @@ -179,7 +179,7 @@ if __name__=="__main__": #a= tokenize("(a - (a ᑌ b) * π a,b (a-b)) - ρ 123 (a)") #a= tokenize(u"π a,b (a*b)") #a=tokenize("(a-b*c)*(b-c)") - a=general_optimize("σ i==2 (c ᑌ d- b)") + a=general_optimize("σ i==2 (c ᑌ d - (aᑎb))") #a=general_optimize("σ i==2 (σ b>5 (d))") print a #print node(a)