]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Remove erroneous assertion from array._bind_param
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 26 Aug 2019 13:45:06 +0000 (09:45 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 26 Aug 2019 13:51:30 +0000 (09:51 -0400)
Fixed bug where Postgresql operators such as
:meth:`.postgresql.ARRAY.Comparator.contains` and
:meth:`.postgresql.ARRAY.Comparator.contained_by` would fail to function
correctly for non-integer values when used against a
:class:`.postgresql.array` object, due to an erroneous assert statement.

Fixes: #4822
Change-Id: I886aca4c86dc7d64e9d0dfc1d910a0ae64d775a1
(cherry picked from commit 4b5d162ed028558ac38c687d69f26ce93741789d)

doc/build/changelog/unreleased_13/4822.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/array.py
test/dialect/postgresql/test_compiler.py

diff --git a/doc/build/changelog/unreleased_13/4822.rst b/doc/build/changelog/unreleased_13/4822.rst
new file mode 100644 (file)
index 0000000..94bb777
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, postgresql
+    :tickets: 4822
+
+    Fixed bug where Postgresql operators such as
+    :meth:`.postgresql.ARRAY.Comparator.contains` and
+    :meth:`.postgresql.ARRAY.Comparator.contained_by` would fail to function
+    correctly for non-integer values when used against a
+    :class:`.postgresql.array` object, due to an erroneous assert statement.
index 81bde2a02cc40c4c4ddbc4f57fb20d11a3bca0cb..40e05c3f0beb5db2e306ef98c057a8ff0c87f3ea 100644 (file)
@@ -113,9 +113,6 @@ class array(expression.Tuple):
 
     def _bind_param(self, operator, obj, _assume_scalar=False, type_=None):
         if _assume_scalar or operator is operators.getitem:
-            # if getitem->slice were called, Indexable produces
-            # a Slice object from that
-            assert isinstance(obj, int)
             return expression.BindParameter(
                 None,
                 obj,
index b65361bdad735cd10f4bc066bb4940ee2968bf2f..83e3ee3fd2318ef01459c1de7e0a01b4e8a1a65b 100644 (file)
@@ -1244,6 +1244,83 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             },
         )
 
+    def test_array_literal_contains(self):
+        self.assert_compile(
+            postgresql.array([1, 2]).contains([3, 4, 5]),
+            "ARRAY[%(param_1)s, %(param_2)s] @> ARRAY[%(param_3)s, "
+            "%(param_4)s, %(param_5)s]",
+            checkparams={
+                "param_1": 1,
+                "param_2": 2,
+                "param_3": 3,
+                "param_4": 4,
+                "param_5": 5,
+            },
+        )
+
+        self.assert_compile(
+            postgresql.array(["a", "b"]).contains([""]),
+            "ARRAY[%(param_1)s, %(param_2)s] @> ARRAY[%(param_3)s]",
+            checkparams={"param_1": "a", "param_2": "b", "param_3": ""},
+        )
+
+        self.assert_compile(
+            postgresql.array(["a", "b"]).contains([]),
+            "ARRAY[%(param_1)s, %(param_2)s] @> ARRAY[]",
+            checkparams={"param_1": "a", "param_2": "b"},
+        )
+
+        self.assert_compile(
+            postgresql.array(["a", "b"]).contains([0]),
+            "ARRAY[%(param_1)s, %(param_2)s] @> ARRAY[%(param_3)s]",
+            checkparams={"param_1": "a", "param_2": "b", "param_3": 0},
+        )
+
+    def test_array_literal_contained_by(self):
+        self.assert_compile(
+            postgresql.array(["a", "b"]).contained_by(["a", "b", "c"]),
+            "ARRAY[%(param_1)s, %(param_2)s] <@ ARRAY[%(param_3)s, "
+            "%(param_4)s, %(param_5)s]",
+            checkparams={
+                "param_1": "a",
+                "param_2": "b",
+                "param_3": "a",
+                "param_4": "b",
+                "param_5": "c",
+            },
+        )
+
+        self.assert_compile(
+            postgresql.array([1, 2]).contained_by([3, 4, 5]),
+            "ARRAY[%(param_1)s, %(param_2)s] <@ ARRAY[%(param_3)s, "
+            "%(param_4)s, %(param_5)s]",
+            checkparams={
+                "param_1": 1,
+                "param_2": 2,
+                "param_3": 3,
+                "param_4": 4,
+                "param_5": 5,
+            },
+        )
+
+        self.assert_compile(
+            postgresql.array(["a", "b"]).contained_by([""]),
+            "ARRAY[%(param_1)s, %(param_2)s] <@ ARRAY[%(param_3)s]",
+            checkparams={"param_1": "a", "param_2": "b", "param_3": ""},
+        )
+
+        self.assert_compile(
+            postgresql.array(["a", "b"]).contained_by([]),
+            "ARRAY[%(param_1)s, %(param_2)s] <@ ARRAY[]",
+            checkparams={"param_1": "a", "param_2": "b"},
+        )
+
+        self.assert_compile(
+            postgresql.array(["a", "b"]).contained_by([0]),
+            "ARRAY[%(param_1)s, %(param_2)s] <@ ARRAY[%(param_3)s]",
+            checkparams={"param_1": "a", "param_2": "b", "param_3": 0},
+        )
+
     def test_array_literal_insert(self):
         m = MetaData()
         t = Table("t", m, Column("data", postgresql.ARRAY(Integer)))