diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2021-04-29 19:53:02 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@ci3.zzzcomputing.com> | 2021-04-29 19:53:02 +0000 |
| commit | dc5485b7ecdbe1cbed34fcb8d748fbe975aee140 (patch) | |
| tree | 7f0456f166b53fecf881c6e214b69dc7db4944e3 /lib/sqlalchemy/sql | |
| parent | 28493bf4bc35a4802b57b02a8b389cec7b6dcbb6 (diff) | |
| parent | aba308868544b21bafa0b3435701ddc908654b0a (diff) | |
| download | sqlalchemy-dc5485b7ecdbe1cbed34fcb8d748fbe975aee140.tar.gz | |
Merge "Use non-subquery form for empty IN"
Diffstat (limited to 'lib/sqlalchemy/sql')
| -rw-r--r-- | lib/sqlalchemy/sql/coercions.py | 11 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 50 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/elements.py | 10 |
3 files changed, 59 insertions, 12 deletions
diff --git a/lib/sqlalchemy/sql/coercions.py b/lib/sqlalchemy/sql/coercions.py index b7aba9d74..820fc1bf1 100644 --- a/lib/sqlalchemy/sql/coercions.py +++ b/lib/sqlalchemy/sql/coercions.py @@ -561,14 +561,9 @@ class InElementImpl(RoleImpl): return element.self_group(against=operator) elif isinstance(element, elements.BindParameter): - if not element.expanding: - # coercing to expanding at the moment to work with the - # lambda system. not sure if this is the right approach. - # is there a valid use case to send a single non-expanding - # param to IN? check for ARRAY type? - element = element._clone(maintain_key=True) - element.expanding = True - + # previously we were adding expanding flags here but + # we now do this in the compiler where we have more context + # see compiler.py -> _render_in_expr_w_bindparam return element else: return element diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index f3ae8c44f..57ffdf86b 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -1904,6 +1904,45 @@ class SQLCompiler(Compiled): binary, override_operator=operators.match_op ) + def visit_in_op_binary(self, binary, operator, **kw): + return self._render_in_expr_w_bindparam(binary, operator, **kw) + + def visit_not_in_op_binary(self, binary, operator, **kw): + return self._render_in_expr_w_bindparam(binary, operator, **kw) + + def _render_in_expr_w_bindparam(self, binary, operator, **kw): + opstring = OPERATORS[operator] + + if isinstance(binary.right, elements.BindParameter): + if not binary.right.expanding or not binary.right.expand_op: + # note that by cloning here, we rely upon the + # _cache_key_bind_match dictionary to resolve + # clones of bindparam() objects to the ones that are + # present in our cache key. + binary.right = binary.right._clone(maintain_key=True) + binary.right.expanding = True + binary.right.expand_op = operator + + return self._generate_generic_binary(binary, opstring, **kw) + + def visit_empty_set_op_expr(self, type_, expand_op): + if expand_op is operators.not_in_op: + if len(type_) > 1: + return "(%s)) OR (1 = 1" % ( + ", ".join("NULL" for element in type_) + ) + else: + return "NULL) OR (1 = 1" + elif expand_op is operators.in_op: + if len(type_) > 1: + return "(%s)) AND (1 != 1" % ( + ", ".join("NULL" for element in type_) + ) + else: + return "NULL) AND (1 != 1" + else: + return self.visit_empty_set_expr(type_) + def visit_empty_set_expr(self, element_types): raise NotImplementedError( "Dialect '%s' does not support empty set expression." @@ -1960,12 +1999,12 @@ class SQLCompiler(Compiled): to_update = [] if parameter.type._is_tuple_type: - replacement_expression = self.visit_empty_set_expr( - parameter.type.types + replacement_expression = self.visit_empty_set_op_expr( + parameter.type.types, parameter.expand_op ) else: - replacement_expression = self.visit_empty_set_expr( - [parameter.type] + replacement_expression = self.visit_empty_set_op_expr( + [parameter.type], parameter.expand_op ) elif isinstance(values[0], (tuple, list)): @@ -3900,6 +3939,9 @@ class StrSQLCompiler(SQLCompiler): for t in extra_froms ) + def visit_empty_set_op_expr(self, type_, expand_op): + return self.visit_empty_set_expr(type_) + def visit_empty_set_expr(self, type_): return "SELECT 1 WHERE 1!=1" diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 696f3b249..e27b97802 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -1411,7 +1411,17 @@ class BindParameter(roles.InElementRole, ColumnElement): self.callable = callable_ self.isoutparam = isoutparam self.required = required + + # indicate an "expanding" parameter; the compiler sets this + # automatically in the compiler _render_in_expr_w_bindparam method + # for an IN expression self.expanding = expanding + + # this is another hint to help w/ expanding and is typically + # set in the compiler _render_in_expr_w_bindparam method for an + # IN expression + self.expand_op = None + self.literal_execute = literal_execute if _is_crud: self._is_crud = True |
