From 8e91cfe529b9b0150c16e52e22e4590bfbbe79fd Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 7 Nov 2022 18:40:03 -0500 Subject: establish consistency for RETURNING column labels The RETURNING clause now renders columns using the routine as that of the :class:`.Select` to generate labels, which will include disambiguating labels, as well as that a SQL function surrounding a named column will be labeled using the column name itself. This is a more comprehensive change than a similar one made for the 1.4 series that adjusted the function label issue only. includes 1.4's changelog for the backported version which also fixes an Oracle issue independently of the 2.0 series. Fixes: #8770 Change-Id: I2ab078a214a778ffe1720dbd864ae4c105a0691d --- lib/sqlalchemy/dialects/mssql/base.py | 19 ++++++++++++++++--- lib/sqlalchemy/sql/compiler.py | 23 ++++++++++++++++++++--- lib/sqlalchemy/sql/dml.py | 4 ++++ lib/sqlalchemy/sql/selectable.py | 8 ++++++-- 4 files changed, 46 insertions(+), 8 deletions(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index a338ba27a..53fe96c9a 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -2295,11 +2295,24 @@ class MSSQLCompiler(compiler.SQLCompiler): columns = [ self._label_returning_column( stmt, - adapter.traverse(c), + adapter.traverse(column), populate_result_map, - {"result_map_targets": (c,)}, + {"result_map_targets": (column,)}, + fallback_label_name=fallback_label_name, + column_is_repeated=repeated, + name=name, + proxy_name=proxy_name, + **kw, + ) + for ( + name, + proxy_name, + fallback_label_name, + column, + repeated, + ) in stmt._generate_columns_plus_names( + True, cols=expression._select_iterables(returning_cols) ) - for c in expression._select_iterables(returning_cols) ] return "OUTPUT " + ", ".join(columns) diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 3e62cb350..97397e9cf 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -3760,7 +3760,6 @@ class SQLCompiler(Compiled): "_label_select_column is only relevant within " "the columns clause of a SELECT or RETURNING" ) - if isinstance(column, elements.Label): if col_expr is not column: result_expr = _CompileLabel( @@ -4416,9 +4415,27 @@ class SQLCompiler(Compiled): populate_result_map: bool, **kw: Any, ) -> str: + columns = [ - self._label_returning_column(stmt, c, populate_result_map, **kw) - for c in base._select_iterables(returning_cols) + self._label_returning_column( + stmt, + column, + populate_result_map, + fallback_label_name=fallback_label_name, + column_is_repeated=repeated, + name=name, + proxy_name=proxy_name, + **kw, + ) + for ( + name, + proxy_name, + fallback_label_name, + column, + repeated, + ) in stmt._generate_columns_plus_names( + True, cols=base._select_iterables(returning_cols) + ) ] return "RETURNING " + ", ".join(columns) diff --git a/lib/sqlalchemy/sql/dml.py b/lib/sqlalchemy/sql/dml.py index 5145a4a16..2d3e3598b 100644 --- a/lib/sqlalchemy/sql/dml.py +++ b/lib/sqlalchemy/sql/dml.py @@ -59,6 +59,7 @@ from .selectable import FromClause from .selectable import HasCTE from .selectable import HasPrefixes from .selectable import Join +from .selectable import SelectLabelStyle from .selectable import TableClause from .selectable import TypedReturnsRows from .sqltypes import NullType @@ -399,6 +400,9 @@ class UpdateBase( ] = util.EMPTY_DICT named_with_column = False + _label_style: SelectLabelStyle = ( + SelectLabelStyle.LABEL_STYLE_DISAMBIGUATE_ONLY + ) table: _DMLTableElement _return_defaults = False diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index 9de015774..488dfe721 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -2193,7 +2193,9 @@ class SelectsRows(ReturnsRows): _label_style: SelectLabelStyle = LABEL_STYLE_NONE def _generate_columns_plus_names( - self, anon_for_dupe_key: bool + self, + anon_for_dupe_key: bool, + cols: Optional[_SelectIterable] = None, ) -> List[_ColumnsPlusNames]: """Generate column names as rendered in a SELECT statement by the compiler. @@ -2204,7 +2206,9 @@ class SelectsRows(ReturnsRows): _column_naming_convention as well. """ - cols = self._all_selected_columns + + if cols is None: + cols = self._all_selected_columns key_naming_convention = SelectState._column_naming_convention( self._label_style -- cgit v1.2.1