combination of and_() and or_() produces unexpected sql output
Issue #3353
closed
For the given filter spec, the sql generated does not match what I would expect to see:
.filter(or_( \
and_(func.len(ExtendedCDR.Source) > 6, ExtendedCDR.DestinationContext=='ael-outbound-dial'), \
and_(func.len(ExtendedCDR.Destination)>6,ExtendedCDR.DestinationContext=='ael-std-extension') \
) \
This produces:
(len(cdr_extended.[Source]) > ? AND
cdr_extended.[DestinationContext] = ? OR
len(cdr_extended.[Destination]) > ? AND
cdr_extended.[DestinationContext] = ?)
Note only a single set of parantheses wrapping the logic, whereas I would expect to see:
(
(len(cdr_extended.[Source]) > ? AND cdr_extended.[DestinationContext] = ?)
OR
(len(cdr_extended.[Destination]) > ? AND cdr_extended.[DestinationContext] = ?)
)
If I am misunderstanding how and_ and or_ are supposed to work together, can you set me straight, or alternatively let me know how I can cause sqlalchemy to emit the query with the proper order of operations?
Comments (2)
-
repo owner -
repo owner - changed status to closed
confirmed on IRC
- Log in to comment
Here's a test case that illustrates every possible combination of values for both expressions and shows that on Postgresql at least they are evaluated as equivalent regardless of parenthesization:
here's a gist showing the output: https://gist.github.com/zzzeek/75a5f02fc1549a6d15c5