Implemented swap_rename_projection general optimization

git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@182 014f5005-505e-4b48-8d0a-63407b615a7c
master
LtWorf 2009-05-25 17:04:24 +07:00
parent d51d4bada7
commit 43766eeeae
4 changed files with 57 additions and 4 deletions

@ -84,3 +84,4 @@
- Implemented selection_and_product specific optimization - Implemented selection_and_product specific optimization
- Added stub for converting SQL to relational algebra - Added stub for converting SQL to relational algebra
- Implemented futile_union_intersection_subtraction general optimization - Implemented futile_union_intersection_subtraction general optimization
- Implemented swap_rename_projection general optimization

@ -354,6 +354,56 @@ def tokenize_select(expression):
tokens.append(temp) tokens.append(temp)
return tokens return tokens
def swap_rename_projection(n):
'''This function locates things like π k(ρ j(R))
and replaces them with ρ j(π k(R)).
This will let rename work on a hopefully smaller set
and more important, will hopefully allow further optimizations.
Will also eliminate fields in the rename that are cutted in the projection.
'''
#TODO document into the wiki
changes=0
if n.name=='π' and n.child.name=='ρ':
changes=1
#π index,name(ρ id➡index(R))
_vars={}
for i in n.child.prop.split(','):
q=i.split('')
_vars[q[1].strip()]=q[0].strip()
_pr=n.prop.split(',')
for i in range(len(_pr)):
try:
_pr[i]=_vars[_pr[i].strip()]
except:
pass
_pr_reborn=n.prop.split(',')
for i in list(_vars.keys()):
if i not in _pr_reborn:
_vars.pop(i)
n.name=n.child.name
n.prop=''
for i in _vars.keys():
n.prop+='%s%s,' % (_vars[i],i)
n.prop=n.prop[:-1]
n.child.name='π'
n.child.prop=''
for i in _pr:
n.child.prop+=i+','
n.child.prop=n.child.prop[:-1]
#recoursive scan
if n.kind==optimizer.UNARY:
changes+=swap_rename_projection(n.child)
elif n.kind==optimizer.BINARY:
changes+=swap_rename_projection(n.right)
changes+=swap_rename_projection(n.left)
return changes
def swap_rename_select(n): def swap_rename_select(n):
'''This function locates things like σ k(ρ j(R)) and replaces '''This function locates things like σ k(ρ j(R)) and replaces
them with ρ j(σ k(R)). Renaming the attributes used in the them with ρ j(σ k(R)). Renaming the attributes used in the
@ -501,5 +551,5 @@ def selection_and_product(n,rels):
changes+=selection_and_product(n.left,rels) changes+=selection_and_product(n.left,rels)
return changes return changes
general_optimizations=[duplicated_select,down_to_unions_subtractions_intersections,duplicated_projection,selection_inside_projection,subsequent_renames,swap_rename_select,futile_union_intersection_subtraction,swap_union_renames] general_optimizations=[duplicated_select,down_to_unions_subtractions_intersections,duplicated_projection,selection_inside_projection,subsequent_renames,swap_rename_select,futile_union_intersection_subtraction,swap_union_renames,swap_rename_projection]
specific_optimizations=[selection_and_product] specific_optimizations=[selection_and_product]

@ -284,7 +284,7 @@ if __name__=="__main__":
#print n #print n
#print n.result_format(rels) #print n.result_format(rels)
a=general_optimize("ρ id➡ciao(R) - ρ id➡ciao(Q) ") a=general_optimize('π index,name,turiddu (ρ qq➡ii,id➡index(R))')#"ρ id➡ciao(R) - ρ id➡ciao(Q) ")
#a=general_optimize("σ i==2 (σ b>5 (d))") #a=general_optimize("σ i==2 (σ b>5 (d))")
print a print a
#print node(a) #print node(a)

@ -41,6 +41,8 @@ if __name__ == "__main__":
app = QtGui.QApplication(sys.argv) app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget() Form = QtGui.QWidget()
Form.setFont(QtGui.QFont("Lucida Sans Unicode"))
ui = maingui.Ui_Form() ui = maingui.Ui_Form()
ui.setupUi(Form) ui.setupUi(Form)
Form.show() Form.show()