summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/operators.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2017-03-14 12:00:56 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2017-03-14 17:01:07 -0400
commitf3b6f4f8da5223fae0a1dd948d4266b2e49e317c (patch)
tree9cae69a0b1680161a5e6604371a17b5766c3dc34 /lib/sqlalchemy/sql/operators.py
parent596e322543df6ff380243c9cb0cf9997252329f6 (diff)
downloadsqlalchemy-f3b6f4f8da5223fae0a1dd948d4266b2e49e317c.tar.gz
Add "empty in" strategies; default to "static"
The longstanding behavior of the :meth:`.Operators.in_` and :meth:`.Operators.not_in_` operators emitting a warning when the right-hand condition is an empty sequence has been revised; a new flag :paramref:`.create_engine.empty_in_strategy` allows an empty "IN" expression to generate a simple boolean expression, or to invoke the previous behavior of dis-equating the expression to itself, with or without a warning. The default behavior is now to emit the simple boolean expression, allowing an empty IN to be evaulated without any performance penalty. Change-Id: I65cc37f2d7cf65a59bf217136c42fee446929352 Fixes: #3907
Diffstat (limited to 'lib/sqlalchemy/sql/operators.py')
-rw-r--r--lib/sqlalchemy/sql/operators.py32
1 files changed, 31 insertions, 1 deletions
diff --git a/lib/sqlalchemy/sql/operators.py b/lib/sqlalchemy/sql/operators.py
index 1690d546b..01bee62cf 100644
--- a/lib/sqlalchemy/sql/operators.py
+++ b/lib/sqlalchemy/sql/operators.py
@@ -458,6 +458,17 @@ class ColumnOperators(Operators):
"other" may be a tuple/list of column expressions,
or a :func:`~.expression.select` construct.
+ In the case that ``other`` is an empty sequence, the compiler
+ produces an "empty in" expression. This defaults to the
+ expression "1 != 1" to produce false in all cases. The
+ :paramref:`.create_engine.empty_in_strategy` may be used to
+ alter this behavior.
+
+ .. versionchanged:: 1.2 The :meth:`.ColumnOperators.in_` and
+ :meth:`.ColumnOperators.notin_` operators
+ now produce a "static" expression for an empty IN sequence
+ by default.
+
"""
return self.operate(in_op, other)
@@ -467,7 +478,16 @@ class ColumnOperators(Operators):
This is equivalent to using negation with
:meth:`.ColumnOperators.in_`, i.e. ``~x.in_(y)``.
- .. versionadded:: 0.8
+ In the case that ``other`` is an empty sequence, the compiler
+ produces an "empty not in" expression. This defaults to the
+ expression "1 = 1" to produce true in all cases. The
+ :paramref:`.create_engine.empty_in_strategy` may be used to
+ alter this behavior.
+
+ .. versionchanged:: 1.2 The :meth:`.ColumnOperators.in_` and
+ :meth:`.ColumnOperators.notin_` operators
+ now produce a "static" expression for an empty IN sequence
+ by default.
.. seealso::
@@ -957,6 +977,14 @@ def comma_op(a, b):
raise NotImplementedError()
+def empty_in_op(a, b):
+ raise NotImplementedError()
+
+
+def empty_notin_op(a, b):
+ raise NotImplementedError()
+
+
def concat_op(a, b):
return a.concat(b)
@@ -1073,6 +1101,8 @@ _PRECEDENCE = {
ne: 5,
is_distinct_from: 5,
isnot_distinct_from: 5,
+ empty_in_op: 5,
+ empty_notin_op: 5,
gt: 5,
lt: 5,
ge: 5,