diff --git a/CHANGELOG b/CHANGELOG index 6e0f286..fc23812 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ 1.2 - Better tokenizer, gives more indicative errors - Parser gives more indicative errors +- Improved select_union_intersect_subtract optimization to avoid parenthesis whenever possible 1.1 - Incorrect relational operations now raise an exception instead of returning None diff --git a/relational/optimizations.py b/relational/optimizations.py index 0a4db5e..18885cb 100644 --- a/relational/optimizations.py +++ b/relational/optimizations.py @@ -475,7 +475,22 @@ def select_union_intersect_subtract(n): newnode=parser.node() - newnode.prop='((%s) %s (%s))' % (n.left.prop,op,n.right.prop) + if n.left.prop.startswith('(') or n.right.prop.startswith('('): + t_str='(' + if n.left.prop.startswith('('): + t_str+='(%s)' + else: + t_str+='%s' + t_str+=' %s ' + if n.right.prop.startswith('('): + t_str+='(%s)' + else: + t_str+='%s' + t_str+=')' + + newnode.prop= t_str % (n.left.prop,op,n.right.prop) + else: + newnode.prop='%s %s %s' % (n.left.prop,op,n.right.prop) newnode.name=SELECTION newnode.child=n.left.child newnode.kind=parser.UNARY diff --git a/test/select_join_opt.query b/test/select_join_opt.query new file mode 100644 index 0000000..384b709 --- /dev/null +++ b/test/select_join_opt.query @@ -0,0 +1 @@ +σ skill=='C' and chief==0 ((σ age<30 (people) ᑌ σ age>40(people)) ᐅᐊ skills) diff --git a/test/select_join_opt.result b/test/select_join_opt.result new file mode 100644 index 0000000..8a76bff --- /dev/null +++ b/test/select_join_opt.result @@ -0,0 +1,3 @@ +id,name,chief,age,skill +0,jack,0,22,C +4,eve,0,25,C