From 77f641429f019d06cc467ec4e57ae94f808d70bd Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 25 Nov 2010 12:20:13 -0500 Subject: - Fixed operator precedence rules for multiple chains of a single non-associative operator. I.e. "x - (y - z)" will compile as "x - (y - z)" and not "x - y - z". Also works with labels, i.e. "x - (y - z).label('foo')" [ticket:1984] - Single element tuple expressions inside an IN clause parenthesize correctly, also from [ticket:1984], added tests for PG - re-fix again importlater, [ticket:1983] --- lib/sqlalchemy/sql/operators.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/sql/operators.py') diff --git a/lib/sqlalchemy/sql/operators.py b/lib/sqlalchemy/sql/operators.py index 6f70b1778..68e30e646 100644 --- a/lib/sqlalchemy/sql/operators.py +++ b/lib/sqlalchemy/sql/operators.py @@ -83,10 +83,14 @@ def desc_op(a): def asc_op(a): return a.asc() + _commutative = set([eq, ne, add, mul]) def is_commutative(op): return op in _commutative +_associative = _commutative.union([concat_op, and_, or_]) + + _smallest = symbol('_smallest') _largest = symbol('_largest') @@ -131,5 +135,8 @@ _PRECEDENCE = { } def is_precedent(operator, against): - return (_PRECEDENCE.get(operator, _PRECEDENCE[_smallest]) <= + if operator is against and operator in _associative: + return False + else: + return (_PRECEDENCE.get(operator, _PRECEDENCE[_smallest]) <= _PRECEDENCE.get(against, _PRECEDENCE[_largest])) -- cgit v1.2.1