]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Support GROUP BY ROLLUP on MySql/MariaDB
authorFederico Caselli <cfederico87@gmail.com>
Fri, 9 Sep 2022 19:29:53 +0000 (21:29 +0200)
committerFederico Caselli <cfederico87@gmail.com>
Fri, 9 Sep 2022 19:29:53 +0000 (21:29 +0200)
The ``ROLLUP`` function will now correctly render ``WITH ROLLUP`` on
MySql and MariaDB, allowing the use of group by rollup with these
backend.

Fixes: #8503
Change-Id: I9289af3a39ca667a2f0f84f73346ebd4b091fedd

doc/build/changelog/unreleased_20/8503.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/mysql/base.py
test/dialect/mysql/test_compiler.py

diff --git a/doc/build/changelog/unreleased_20/8503.rst b/doc/build/changelog/unreleased_20/8503.rst
new file mode 100644 (file)
index 0000000..d4b6307
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: usecase, mysql, mariadb
+    :tickets: 8503
+
+    The ``ROLLUP`` function will now correctly render ``WITH ROLLUP`` on
+    MySql and MariaDB, allowing the use of group by rollup with these
+    backend.
index 4b0a5e6c5e4cb62c2b49664fdfcfbfd3e2f12b36..c0521f61e82538a134a92bc147d47b493a756f5e 100644 (file)
@@ -1210,6 +1210,12 @@ class MySQLCompiler(compiler.SQLCompiler):
     def visit_random_func(self, fn, **kw):
         return "rand%s" % self.function_argspec(fn)
 
+    def visit_rollup_func(self, fn, **kw):
+        clause = ", ".join(
+            elem._compiler_dispatch(self, **kw) for elem in fn.clauses
+        )
+        return f"{clause} WITH ROLLUP"
+
     def visit_sequence(self, seq, **kw):
         return "nextval(%s)" % self.preparer.format_sequence(seq)
 
index 9d2c43bfead32a7f367b42ac221365460f8e628a..a4a0b24e49b1786296c601151d65c8994793e739 100644 (file)
@@ -560,6 +560,13 @@ class CompileTest(ReservedWordFixture, fixtures.TestBase, AssertsCompiledSQL):
             "ALWAYS AS (x + 2)%s)" % text,
         )
 
+    def test_groupby_rollup(self):
+        t = table("tt", column("foo"), column("bar"))
+        q = sql.select(t.c.foo).group_by(sql.func.rollup(t.c.foo, t.c.bar))
+        self.assert_compile(
+            q, "SELECT tt.foo FROM tt GROUP BY tt.foo, tt.bar WITH ROLLUP"
+        )
+
 
 class SQLTest(fixtures.TestBase, AssertsCompiledSQL):