diff --git a/relational/optimizations.py b/relational/optimizations.py index cf56680..9e6f8d0 100644 --- a/relational/optimizations.py +++ b/relational/optimizations.py @@ -168,6 +168,46 @@ def subsequent_renames(n): changes+=subsequent_renames(n.left) return changes +def tokenize_select(expression): + '''This function returns the list of tokens present in a + selection. The expression can't contain parenthesis.''' + op=('//=','**=','and','not','//','**','<<','>>','==','!=','>=','<=','+=','-=','*=','/=','%=','or','+','-','*','/','&','|','^','~','<','>','%','=') + tokens=[] + temp="" + + while len(expression)!=0: + expression=expression.strip() + if expression[0:3] in op:#3char op + tokens.append(temp) + temp="" + tokens.append(expression[0:3]) + expression=expression[3:] + elif expression[0:2] in op:#2char op + tokens.append(temp) + temp="" + tokens.append(expression[0:2]) + expression=expression[2:] + elif expression[0:1] in op:#1char op + tokens.append(temp) + temp="" + tokens.append(expression[0:1]) + expression=expression[1:] + elif expression[0:1]=="'":#Quote + end=expression.index("'",1) + while expression[end-1]=="\\": + end=expression.index("'",end+1) + + #Add string to list + tokens.append(expression[0:end+1]) + expression=expression[end+1:] + else: + temp+=expression[0:1] + expression=expression[1:] + pass + if len(temp)!=0: + tokens.append(temp) + return tokens + 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 @@ -175,6 +215,35 @@ def swap_rename_select(n): #TODO document into the wiki changes=0 + if n.name=='σ' and n.child.name=='ρ': + + #Dictionary containing attributes of rename + _vars={} + for i in n.child.prop.split(','): + q=i.split('➡') + _vars[q[1].strip()]=q[0].strip() + + #tokenizes expression in select + _tokens=tokenize_select(n.prop) + print _tokens, _vars + + #Renaming stuff + for i in range(len(_tokens)): + if _tokens[i] in _vars: + _tokens[i]=_vars[_tokens[i]] + print _tokens, _vars + + n.name='ρ' + n.child.name='σ' + + n.prop=n.child.prop + n.child.prop="" + for i in _tokens: + n.child.prop+=i+ " " + + + pass + #recoursive scan if n.kind==optimizer.UNARY: changes+=swap_rename_select(n.child) diff --git a/relational/optimizer.py b/relational/optimizer.py index 8fa7947..851f198 100644 --- a/relational/optimizer.py +++ b/relational/optimizer.py @@ -194,7 +194,7 @@ 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==3(ρ ciccio➡age,nome➡nom(R))") + a=general_optimize("σ age==3 and qq<=2 or nome!='ciccio d\\'urso'(ρ ciccio➡age,nome➡nom(R))") #a=general_optimize("σ i==2 (σ b>5 (d))") print a #print node(a)