summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/compiler.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/compiler.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/compiler.py')
-rw-r--r--lib/sqlalchemy/sql/compiler.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index bfa22c206..a1d5a879d 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -1003,6 +1003,30 @@ class SQLCompiler(Compiled):
return "NOT %s" % self.visit_binary(
binary, override_operator=operators.match_op)
+ def _emit_empty_in_warning(self):
+ util.warn(
+ 'The IN-predicate was invoked with an '
+ 'empty sequence. This results in a '
+ 'contradiction, which nonetheless can be '
+ 'expensive to evaluate. Consider alternative '
+ 'strategies for improved performance.')
+
+ def visit_empty_in_op_binary(self, binary, operator, **kw):
+ if self.dialect._use_static_in:
+ return "1 != 1"
+ else:
+ if self.dialect._warn_on_empty_in:
+ self._emit_empty_in_warning()
+ return self.process(binary.left != binary.left)
+
+ def visit_empty_notin_op_binary(self, binary, operator, **kw):
+ if self.dialect._use_static_in:
+ return "1 = 1"
+ else:
+ if self.dialect._warn_on_empty_in:
+ self._emit_empty_in_warning()
+ return self.process(binary.left == binary.left)
+
def visit_binary(self, binary, override_operator=None,
eager_grouping=False, **kw):