]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug in :func:`.tuple_` construct where the "type" of essentially
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 28 Feb 2014 00:54:49 +0000 (19:54 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 28 Feb 2014 00:57:20 +0000 (19:57 -0500)
the first SQL expression would be applied as the "comparison type"
to a compared tuple value; this has the effect in some cases of an
inappropriate "type coersion" occurring, such as when a tuple that
has a mix of String and Binary values improperly coerces target
values to Binary even though that's not what they are on the left
side.  :func:`.tuple_` now expects heterogeneous types within its
list of values.
fixes #2977

Conflicts:
lib/sqlalchemy/sql/elements.py
test/sql/test_operators.py

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/sql/expression.py
test/sql/test_operators.py

index 611473144d350fc8fd89bec7437c743592e77d1a..803cef479d65dc33325f9c450235d4b1d1026cdb 100644 (file)
 .. changelog::
     :version: 0.8.6
 
+    .. change::
+        :tags: bug, sql
+        :versions: 0.9.4
+        :tickets: 2977
+
+        Fixed bug in :func:`.tuple_` construct where the "type" of essentially
+        the first SQL expression would be applied as the "comparison type"
+        to a compared tuple value; this has the effect in some cases of an
+        inappropriate "type coersion" occurring, such as when a tuple that
+        has a mix of String and Binary values improperly coerces target
+        values to Binary even though that's not what they are on the left
+        side.  :func:`.tuple_` now expects heterogeneous types within its
+        list of values.
+
     .. change::
         :tags: orm, bug
         :versions: 0.9.4
index 5b4198a7c1f8d44d9aa8c4b5b88333969c392854..b60d6d343e93b7784aaa25e13c34d6dafb1e5eb2 100644 (file)
@@ -4028,12 +4028,34 @@ class BooleanClauseList(ClauseList, ColumnElement):
 
 
 class Tuple(ClauseList, ColumnElement):
+    """Represent a SQL tuple."""
 
     def __init__(self, *clauses, **kw):
+        """Return a :class:`.Tuple`.
+
+        Main usage is to produce a composite IN construct::
+
+            from sqlalchemy import tuple_
+
+            tuple_(table.c.col1, table.c.col2).in_(
+                [(1, 2), (5, 12), (10, 19)]
+            )
+
+        .. warning::
+
+            The composite IN construct is not supported by all backends,
+            and is currently known to work on Postgresql and MySQL,
+            but not SQLite.   Unsupported backends will raise
+            a subclass of :class:`~sqlalchemy.exc.DBAPIError` when such
+            an expression is invoked.
+
+        """
+
         clauses = [_literal_as_binds(c) for c in clauses]
-        self.type = kw.pop('type_', None)
-        if self.type is None:
-            self.type = _type_from_args(clauses)
+        self._type_tuple = [arg.type for arg in clauses]
+        self.type = kw.pop('type_', self._type_tuple[0]
+                            if self._type_tuple else sqltypes.NULLTYPE)
+
         super(Tuple, self).__init__(*clauses, **kw)
 
     @property
@@ -4043,8 +4065,8 @@ class Tuple(ClauseList, ColumnElement):
     def _bind_param(self, operator, obj):
         return Tuple(*[
             BindParameter(None, o, _compared_to_operator=operator,
-                             _compared_to_type=self.type, unique=True)
-            for o in obj
+                             _compared_to_type=type_, unique=True)
+            for o, type_ in zip(obj, self._type_tuple)
         ]).self_group()
 
 
index 1108c5daae3f95e88a12af142e5071eacb117ac1..fd70c2d156a42e272c4d2b057b70211685806aa7 100644 (file)
@@ -7,7 +7,7 @@ from sqlalchemy.sql.expression import BinaryExpression, \
                 UnaryExpression, select, union, func, tuple_
 from sqlalchemy.sql import operators, table
 import operator
-from sqlalchemy import String, Integer
+from sqlalchemy import String, Integer, LargeBinary
 from sqlalchemy import exc
 from sqlalchemy.schema import Column, Table, MetaData
 from sqlalchemy.types import TypeEngine, TypeDecorator, UserDefinedType
@@ -1365,3 +1365,23 @@ class ComposedLikeOperatorsTest(fixtures.TestBase, testing.AssertsCompiledSQL):
             dialect=mysql.dialect()
         )
 
+class TupleTypingTest(fixtures.TestBase):
+
+    def _assert_types(self, expr):
+        eq_(expr.clauses[0].type._type_affinity, Integer)
+        eq_(expr.clauses[1].type._type_affinity, String)
+        eq_(expr.clauses[2].type._type_affinity, LargeBinary()._type_affinity)
+
+    def test_type_coersion_on_eq(self):
+        a, b, c = column('a', Integer), column('b', String), column('c', LargeBinary)
+        t1 = tuple_(a, b, c)
+        expr = t1 == (3, 'hi', 'there')
+        self._assert_types(expr.right)
+
+    def test_type_coersion_on_in(self):
+        a, b, c = column('a', Integer), column('b', String), column('c', LargeBinary)
+        t1 = tuple_(a, b, c)
+        expr = t1.in_([(3, 'hi', 'there'), (4, 'Q', 'P')])
+        eq_(len(expr.right.clauses), 2)
+        for elem in expr.right.clauses:
+            self._assert_types(elem)