]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Allow Grouping to pass along proxy_set of element
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 21 Jul 2020 16:36:20 +0000 (12:36 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 21 Jul 2020 16:38:33 +0000 (12:38 -0400)
Repaired an issue where the "ORDER BY" clause rendering a label name rather
than a complete expression, which is particularly important for SQL Server,
would fail to occur if the expression were enclosed in a parenthesized
grouping in some cases.   This case has been added to test support.

Fixes: #5470
Change-Id: Ie0e27c39e5d53be78b32f7810f93d2d0536375e7
(cherry picked from commit 30ec982ba697eb320d804164c6bc965ae239abe8)

doc/build/changelog/unreleased_13/5470.rst [new file with mode: 0644]
lib/sqlalchemy/sql/elements.py
test/sql/test_text.py

diff --git a/doc/build/changelog/unreleased_13/5470.rst b/doc/build/changelog/unreleased_13/5470.rst
new file mode 100644 (file)
index 0000000..ed7202d
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, sql
+    :tickets: 5470
+
+    Repaired an issue where the "ORDER BY" clause rendering a label name rather
+    than a complete expression, which is particularly important for SQL Server,
+    would fail to occur if the expression were enclosed in a parenthesized
+    grouping in some cases.   This case has been added to test support.
+
index 91b5f9fe614a075df3cd8c3edef917fefe054e3d..64824df42db532e603acdeccf85ff8166857fae2 100644 (file)
@@ -3387,6 +3387,13 @@ class Grouping(ColumnElement):
     def get_children(self, **kwargs):
         return (self.element,)
 
+    @property
+    def _proxies(self):
+        if isinstance(self.element, ColumnElement):
+            return [self.element]
+        else:
+            return []
+
     @property
     def _from_objects(self):
         return self.element._from_objects
index b960fa47a8cbf9916351099d7f16163fe87ea1a7..99e20744f6976252972c73c734e6be89e83d9973 100644 (file)
@@ -15,6 +15,7 @@ from sqlalchemy import MetaData
 from sqlalchemy import select
 from sqlalchemy import String
 from sqlalchemy import Table
+from sqlalchemy import testing
 from sqlalchemy import text
 from sqlalchemy import union
 from sqlalchemy import util
@@ -662,6 +663,25 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL):
             "FROM mytable AS mytable_1 ORDER BY mytable_1.name",
         )
 
+    @testing.combinations(
+        ((column("q") + 5).label("a"), "a", ()),
+        (column("q").op("+")(5).label("a"), "a", ()),
+        ((column("q") + 5).label("a"), "a DESC", (desc,)),
+        (column("q").op("+")(5).label("a"), "a DESC", (desc,)),
+    )
+    def test_order_by_expr(self, case, expected, modifiers):
+
+        order_by = case
+        for mod in modifiers:
+            order_by = mod(order_by)
+
+        stmt = select([case]).order_by(order_by)
+
+        col_expr = str(case)
+        self.assert_compile(
+            stmt, "SELECT %s AS a ORDER BY %s" % (col_expr, expected)
+        )
+
     def test_order_by_named_label_from_anon_label(self):
         s1 = select([table1.c.myid.label(None).label("foo"), table1.c.name])
         stmt = s1.order_by("foo")