From: Federico Caselli Date: Fri, 9 Sep 2022 19:29:53 +0000 (+0200) Subject: Support GROUP BY ROLLUP on MySql/MariaDB X-Git-Tag: rel_2_0_0b1~59^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=efbe38d8e8186c1816d2d3b052f92965787649da;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Support GROUP BY ROLLUP on MySql/MariaDB 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 --- diff --git a/doc/build/changelog/unreleased_20/8503.rst b/doc/build/changelog/unreleased_20/8503.rst new file mode 100644 index 0000000000..d4b6307d40 --- /dev/null +++ b/doc/build/changelog/unreleased_20/8503.rst @@ -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. diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 4b0a5e6c5e..c0521f61e8 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -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) diff --git a/test/dialect/mysql/test_compiler.py b/test/dialect/mysql/test_compiler.py index 9d2c43bfea..a4a0b24e49 100644 --- a/test/dialect/mysql/test_compiler.py +++ b/test/dialect/mysql/test_compiler.py @@ -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):