]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Deliver straight BinaryExpr w/ no negate for any() / all()
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 18 Aug 2020 18:17:06 +0000 (14:17 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 18 Aug 2020 18:19:35 +0000 (14:19 -0400)
Adjusted the :meth:`_types.ARRAY.Comparator.any` and
:meth:`_types.ARRAY.Comparator.all` methods to implement a straight "NOT"
operation for negation, rather than negating the comparison operator.

Fixes: #5518
Change-Id: I87ee9278c321aafe51a679fcfcbb5fbb11307fda
(cherry picked from commit 8bc793c4dbc876722dfaad0ca731938c70b54b6c)

doc/build/changelog/unreleased_13/5518.rst [new file with mode: 0644]
lib/sqlalchemy/sql/sqltypes.py
test/dialect/postgresql/test_compiler.py
test/sql/test_operators.py

diff --git a/doc/build/changelog/unreleased_13/5518.rst b/doc/build/changelog/unreleased_13/5518.rst
new file mode 100644 (file)
index 0000000..1cd2a25
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, postgresql
+    :tickets: 5518
+
+    Adjusted the :meth:`_types.ARRAY.Comparator.any` and
+    :meth:`_types.ARRAY.Comparator.all` methods to implement a straight "NOT"
+    operation for negation, rather than negating the comparison operator.
\ No newline at end of file
index 7309774858e359cceaff749dd2d4003a66bb222c..f6d1c49ea3b871ee60e17af0bf3f16637ced48de 100644 (file)
@@ -2636,9 +2636,13 @@ class ARRAY(SchemaEventTarget, Indexable, Concatenable, TypeEngine):
 
             """
             operator = operator if operator else operators.eq
-            return operator(
+
+            # send plain BinaryExpression so that negate remains at None,
+            # leading to NOT expr for negation.
+            return elements.BinaryExpression(
                 elements._literal_as_binds(other),
                 elements.CollectionAggregate._create_any(self.expr),
+                operator,
             )
 
         @util.dependencies("sqlalchemy.sql.elements")
@@ -2671,9 +2675,13 @@ class ARRAY(SchemaEventTarget, Indexable, Concatenable, TypeEngine):
 
             """
             operator = operator if operator else operators.eq
-            return operator(
+
+            # send plain BinaryExpression so that negate remains at None,
+            # leading to NOT expr for negation.
+            return elements.BinaryExpression(
                 elements._literal_as_binds(other),
                 elements.CollectionAggregate._create_all(self.expr),
+                operator,
             )
 
     comparator_factory = Comparator
index 57099f755c56d847fc516449ab92779ba15aed75..7e082166ba7ad3088d911343228f13a32c2c648b 100644 (file)
@@ -1226,6 +1226,27 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "%(param_1)s = ANY (x)",
             checkparams={"param_1": 4},
         )
+
+        self.assert_compile(
+            c.any(5), "%(param_1)s = ANY (x)", checkparams={"param_1": 5},
+        )
+
+        self.assert_compile(
+            ~c.any(5),
+            "NOT (%(param_1)s = ANY (x))",
+            checkparams={"param_1": 5},
+        )
+
+        self.assert_compile(
+            c.all(5), "%(param_1)s = ALL (x)", checkparams={"param_1": 5},
+        )
+
+        self.assert_compile(
+            ~c.all(5),
+            "NOT (%(param_1)s = ALL (x))",
+            checkparams={"param_1": 5},
+        )
+
         self.assert_compile(
             c.any(5, operator=operators.ne),
             "%(param_1)s != ANY (x)",
index 29ad2802f95d7d4f77d154b03f79082ecdd68cb2..8b2648ff7516e70b889e74098878152980fd4704 100644 (file)
@@ -2790,6 +2790,15 @@ class AnyAllTest(fixtures.TestBase, testing.AssertsCompiledSQL):
             checkparams={"param_1": 5},
         )
 
+    def test_any_array_comparator_negate_accessor(self, t_fixture):
+        t = t_fixture
+
+        self.assert_compile(
+            ~t.c.arrval.any(5, operator.gt),
+            "NOT (:param_1 > ANY (tab1.arrval))",
+            checkparams={"param_1": 5},
+        )
+
     def test_all_array_comparator_accessor(self, t_fixture):
         t = t_fixture
 
@@ -2799,6 +2808,15 @@ class AnyAllTest(fixtures.TestBase, testing.AssertsCompiledSQL):
             checkparams={"param_1": 5},
         )
 
+    def test_all_array_comparator_negate_accessor(self, t_fixture):
+        t = t_fixture
+
+        self.assert_compile(
+            ~t.c.arrval.all(5, operator.gt),
+            "NOT (:param_1 > ALL (tab1.arrval))",
+            checkparams={"param_1": 5},
+        )
+
     def test_any_array_expression(self, t_fixture):
         t = t_fixture