From: Mike Bayer Date: Mon, 26 Aug 2019 13:45:06 +0000 (-0400) Subject: Remove erroneous assertion from array._bind_param X-Git-Tag: rel_1_4_0b1~747^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4b5d162ed028558ac38c687d69f26ce93741789d;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Remove erroneous assertion from array._bind_param 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 --- diff --git a/doc/build/changelog/unreleased_13/4822.rst b/doc/build/changelog/unreleased_13/4822.rst new file mode 100644 index 0000000000..94bb77739a --- /dev/null +++ b/doc/build/changelog/unreleased_13/4822.rst @@ -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. diff --git a/lib/sqlalchemy/dialects/postgresql/array.py b/lib/sqlalchemy/dialects/postgresql/array.py index 81bde2a02c..40e05c3f0b 100644 --- a/lib/sqlalchemy/dialects/postgresql/array.py +++ b/lib/sqlalchemy/dialects/postgresql/array.py @@ -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, diff --git a/test/dialect/postgresql/test_compiler.py b/test/dialect/postgresql/test_compiler.py index b65361bdad..83e3ee3fd2 100644 --- a/test/dialect/postgresql/test_compiler.py +++ b/test/dialect/postgresql/test_compiler.py @@ -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)))