Repository: kayak/pypika (python)

Issue:
An alias is set in the GROUP BY when a CASE term is used, which is invalid
A fix for the use of alias' in GROUP BY clauses was previously added in https://github.com/kayak/pypika/pull/42.

However, this fix was only added to the Function class.

If a CASE statement is being used in the GROUP BY clause, then this needs to stop adding the alias after the CASE 'END' in the GROUP BY clause, as this is invalid SQL.

Pull request description:
Fix alias appearing in group by case statement. Fixes #51.

Patch:
diff --git a/README.rst b/README.rst
index 9f67128..b0215c0 100644
--- a/README.rst
+++ b/README.rst
@@ -525,7 +525,7 @@ using the ``when`` method and to set the default value using ``else_``.
         Case()
            .when(customers.fname == "Tom", "It was Tom")
            .when(customers.fname == "John", "It was John")
-           else_("It was someone else.").as_('who_was_it'),
+           .else_("It was someone else.").as_('who_was_it')
     )
 
 
diff --git a/pypika/queries.py b/pypika/queries.py
index 13b8b81..277c856 100644
--- a/pypika/queries.py
+++ b/pypika/queries.py
@@ -599,9 +599,9 @@ class QueryBuilder(Selectable, Term):
     def _where_sql(self, quote_char=None, **kwargs):
         return ' WHERE {where}'.format(where=self._wheres.get_sql(quote_char=quote_char, subquery=True, **kwargs))
 
-    def _group_sql(self, quote_char=None, with_alias=None, **kwargs):
+    def _group_sql(self, quote_char=None, with_alias=False, **kwargs):
         return ' GROUP BY {groupby}'.format(
-            groupby=','.join(term.get_sql(quote_char=quote_char, **kwargs)
+            groupby=','.join(term.get_sql(quote_char=quote_char, with_alias=with_alias, **kwargs)
                              for term in self._groupbys)
         )
 
diff --git a/pypika/terms.py b/pypika/terms.py
index fa826fe..cc30bc8 100644
--- a/pypika/terms.py
+++ b/pypika/terms.py
@@ -559,7 +559,7 @@ class Case(Term):
 
         case_sql = 'CASE {cases}{else_} END'.format(cases=cases, else_=else_)
 
-        if self.alias is None:
+        if not with_alias or self.alias is None:
             return case_sql
 
         return alias_sql(case_sql, self.alias, kwargs.get('quote_char'))


Test patch:
diff --git a/pypika/tests/test_selects.py b/pypika/tests/test_selects.py
index d25a3de..f094f57 100644
--- a/pypika/tests/test_selects.py
+++ b/pypika/tests/test_selects.py
@@ -255,6 +255,25 @@ class GroupByTests(unittest.TestCase):
 
         self.assertEqual('SELECT SUM("foo") "bar" FROM "abc" GROUP BY SUM("foo")', str(q))
 
+    def test_groupby_with_case_does_not_show_an_alias(self):
+        q = Query.from_(self.t).select(
+            fn.Sum(self.t.foo).as_('bar'),
+            Case()
+                .when(self.t.fname == "Tom", "It was Tom")
+                .else_("It was someone else.").as_('who_was_it')
+        ).groupby(
+            Case()
+                .when(self.t.fname == "Tom", "It was Tom")
+                .else_("It was someone else.").as_('who_was_it')
+        )
+
+        self.assertEqual("SELECT SUM(\"foo\") \"bar\","
+                         "CASE WHEN \"fname\"='Tom' THEN 'It was Tom' "
+                         "ELSE 'It was someone else.' END \"who_was_it\" "
+                         "FROM \"abc\" "
+                         "GROUP BY "
+                         "CASE WHEN \"fname\"='Tom' THEN 'It was Tom' ELSE 'It was someone else.' END", str(q))
+
     def test_mysql_query_uses_backtick_quote_chars(self):
         q = MySQLQuery.from_(self.t).groupby(self.t.foo).select(self.t.foo)