From 3b4bbbb2a3d337d0af1ba5ccb0d29d1c48735e83 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 15 Aug 2020 15:08:09 -0400 Subject: Create a real type for Tuple() and handle appropriately in compiler Improved the :func:`_sql.tuple_` construct such that it behaves predictably when used in a columns-clause context. The SQL tuple is not supported as a "SELECT" columns clause element on most backends; on those that do (PostgreSQL, not surprisingly), the Python DBAPI does not have a "nested type" concept so there are still challenges in fetching rows for such an object. Use of :func:`_sql.tuple_` in a :func:`_sql.select` or :class:`_orm.Query` will now raise a :class:`_exc.CompileError` at the point at which the :func:`_sql.tuple_` object is seen as presenting itself for fetching rows (i.e., if the tuple is in the columns clause of a subquery, no error is raised). For ORM use,the :class:`_orm.Bundle` object is an explicit directive that a series of columns should be returned as a sub-tuple per row and is suggested by the error message. Additionally ,the tuple will now render with parenthesis in all contexts. Previously, the parenthesization would not render in a columns context leading to non-defined behavior. As part of this change, Tuple receives a dedicated datatype which appears to allow us the very desirable change of removing the bindparam._expanding_in_types attribute as well as ClauseList._tuple_values (which might already have not been needed due to #4645). Fixes: #5127 Change-Id: Iecafa0e0aac2f1f37ec8d0e1631d562611c90200 --- lib/sqlalchemy/sql/elements.py | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) (limited to 'lib/sqlalchemy/sql/elements.py') diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 8a506446d..a17612034 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -1059,7 +1059,6 @@ class BindParameter(roles.InElementRole, ColumnElement): ] _is_crud = False - _expanding_in_types = () _is_bind_parameter = True _key_is_anon = False @@ -1372,15 +1371,6 @@ class BindParameter(roles.InElementRole, ColumnElement): else: self.type = type_ - def _with_expanding_in_types(self, types): - """Return a copy of this :class:`.BindParameter` in - the context of an expanding IN against a tuple. - - """ - cloned = self._clone(maintain_key=True) - cloned._expanding_in_types = types - return cloned - def _with_value(self, value, maintain_key=False): """Return a copy of this :class:`.BindParameter` with the given value set. @@ -2141,7 +2131,6 @@ class ClauseList( self.group_contents = kwargs.pop("group_contents", True) if kwargs.pop("_flatten_sub_clauses", False): clauses = util.flatten_iterator(clauses) - self._tuple_values = kwargs.pop("_tuple_values", False) self._text_converter_role = text_converter_role = kwargs.pop( "_literal_as_text_role", roles.WhereHavingRole ) @@ -2168,7 +2157,6 @@ class ClauseList( self.group = True self.operator = operator self.group_contents = True - self._tuple_values = False self._is_implicitly_boolean = False return self @@ -2212,8 +2200,6 @@ class BooleanClauseList(ClauseList, ColumnElement): __visit_name__ = "clauselist" inherit_cache = True - _tuple_values = False - def __init__(self, *arg, **kw): raise NotImplementedError( "BooleanClauseList has a private constructor" @@ -2471,8 +2457,11 @@ or_ = BooleanClauseList.or_ class Tuple(ClauseList, ColumnElement): """Represent a SQL tuple.""" + __visit_name__ = "tuple" + _traverse_internals = ClauseList._traverse_internals + [] + @util.preload_module("sqlalchemy.sql.sqltypes") def __init__(self, *clauses, **kw): """Return a :class:`.Tuple`. @@ -2496,15 +2485,12 @@ class Tuple(ClauseList, ColumnElement): invoked. """ + sqltypes = util.preloaded.sql_sqltypes clauses = [ coercions.expect(roles.ExpressionElementRole, c) for c in clauses ] - self._type_tuple = [arg.type for arg in clauses] - self.type = kw.pop( - "type_", - self._type_tuple[0] if self._type_tuple else type_api.NULLTYPE, - ) + self.type = sqltypes.TupleType(*[arg.type for arg in clauses]) super(Tuple, self).__init__(*clauses, **kw) @@ -2520,7 +2506,8 @@ class Tuple(ClauseList, ColumnElement): _compared_to_operator=operator, unique=True, expanding=True, - )._with_expanding_in_types(self._type_tuple) + type_=self.type, + ) else: return Tuple( *[ @@ -2532,9 +2519,13 @@ class Tuple(ClauseList, ColumnElement): unique=True, type_=type_, ) - for o, compared_to_type in zip(obj, self._type_tuple) + for o, compared_to_type in zip(obj, self.type.types) ] - ).self_group() + ) + + def self_group(self, against=None): + # Tuple is parenthsized by definition. + return self class Case(ColumnElement): -- cgit v1.2.1