- # if we are ANDing or ORing, add an inner join
- # also if we are NOTing, but we are looking for a tag *value*
- if($op eq "AND")
- {
- $join= "INNER";
- }
- elsif($op eq "NOT")
- {
- $not=1;
- $join = ($hasvals ? "INNER" : "LEFT");
- }
- elsif($op eq "OR")
- {
- $join="INNER" unless($left && $left->name() && $left->name() eq "NOT")
- }
+ # if right child is a NOT, we don't need extra join/brackets
+ # NOT will do the same and we will end up with an extra one
+ $join= "INNER" unless($right && $right->name() && $right->name() eq "NOT");
+ }
+ elsif($op eq "NOT")
+ {
+ $not=1;
+ $join = ($hasvals ? "INNER" : "LEFT");
+ }
+ elsif($op eq "OR")
+ {
+ # if the rightmost part of the left sub-expression ends in
+ # NOT, then we need an extra join. This doesn't apply if
+ # (as above) the righthand expression is a NOT.
+ $join="INNER" if(($self->right_ends_in_not($left)) &&
+ !($right && $right->name() && $right->name() eq "NOT"));