]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
use bindparam_type in BinaryElementImpl._post_coercion if available
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 1 May 2022 16:28:36 +0000 (12:28 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 1 May 2022 16:28:36 +0000 (12:28 -0400)
Fixed an issue where using :func:`.bindparam` with no explicit data or type
given could be coerced into the incorrect type when used in expressions
such as when using :meth:`.ARRAY.comparator.any` and
:meth:`.ARRAY.comparator.all`.

Fixes: #7979
Change-Id: If7779e713c9a3a5fee496b66e417cfd3fca5b1f9

doc/build/changelog/unreleased_14/7979.rst [new file with mode: 0644]
lib/sqlalchemy/sql/coercions.py
test/sql/test_operators.py

diff --git a/doc/build/changelog/unreleased_14/7979.rst b/doc/build/changelog/unreleased_14/7979.rst
new file mode 100644 (file)
index 0000000..9a82a29
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, sql
+    :tickets: 7979
+
+    Fixed an issue where using :func:`.bindparam` with no explicit data or type
+    given could be coerced into the incorrect type when used in expressions
+    such as when using :meth:`.ARRAY.comparator.any` and
+    :meth:`.ARRAY.comparator.all`.
+
index 2cf67abed35e695f071592117ea72937cf8e0e69..eef5cf211ea1d214fe8d2bd3366d32c5ab04b9b2 100644 (file)
@@ -759,9 +759,11 @@ class BinaryElementImpl(ExpressionElementImpl, RoleImpl):
         except exc.ArgumentError as err:
             self._raise_for_expected(element, err=err)
 
-    def _post_coercion(self, resolved, expr, **kw):
+    def _post_coercion(self, resolved, expr, bindparam_type=None, **kw):
         if resolved.type._isnull and not expr.type._isnull:
-            resolved = resolved._with_binary_element_type(expr.type)
+            resolved = resolved._with_binary_element_type(
+                bindparam_type if bindparam_type is not None else expr.type
+            )
         return resolved
 
 
index 77ca95de7360e022411d2cf7c09f4c3c4c049c62..186b93c54f3ef7549d86444cfa502cd187287c4c 100644 (file)
@@ -5,6 +5,7 @@ import pickle
 
 from sqlalchemy import and_
 from sqlalchemy import between
+from sqlalchemy import bindparam
 from sqlalchemy import exc
 from sqlalchemy import Integer
 from sqlalchemy import join
@@ -3790,6 +3791,24 @@ class AnyAllTest(fixtures.TestBase, testing.AssertsCompiledSQL):
             t.c.data + all_(t.c.arrval), "tab1.data + ALL (tab1.arrval)"
         )
 
+    @testing.combinations("all", "any", argnames="op")
+    def test_any_all_bindparam_coercion(self, t_fixture, op):
+        """test #7979"""
+        t = t_fixture
+
+        if op == "all":
+            expr = t.c.arrval.all(bindparam("param"))
+            expected = "%(param)s = ALL (tab1.arrval)"
+        elif op == "any":
+            expr = t.c.arrval.any(bindparam("param"))
+            expected = "%(param)s = ANY (tab1.arrval)"
+        else:
+            assert False
+
+        is_(expr.left.type._type_affinity, Integer)
+
+        self.assert_compile(expr, expected, dialect="postgresql")
+
     def test_any_array_comparator_accessor(self, t_fixture):
         t = t_fixture