Repository: tobymao/sqlglot

Issue:
Hive Dialect for "GROUPING SETS" error
when parsing 'grouping sets', the column cannot use table alias, and any other functions. Or, there is a ParseError.

A good "GROUPING SETS' case
![image](https://user-images.githubusercontent.com/34465781/215730424-1940fb93-35ef-4f1e-929d-0f9e757edfbd.png)

After adding table alias
![image](https://user-images.githubusercontent.com/34465781/215730550-bfc5c93e-e82b-4afa-8979-3012b5aa1c58.png)
![image](https://user-images.githubusercontent.com/34465781/215730590-0fb11429-c25b-408d-9250-dd803a6f7b92.png)

Or, add some function
![image](https://user-images.githubusercontent.com/34465781/215730908-80e9db5a-e215-4510-b3a3-a035bd3b54a1.png)
![image](https://user-images.githubusercontent.com/34465781/215730922-342bfa9a-15ed-4a57-adf8-70d8bdaa5d28.png)



Discussion:

Resolving patch:
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -1880,11 +1880,11 @@ def _parse_grouping_sets(self) -> t.Optional[t.List[t.Optional[exp.Expression]]]
 
     def _parse_grouping_set(self) -> t.Optional[exp.Expression]:
         if self._match(TokenType.L_PAREN):
-            grouping_set = self._parse_csv(self._parse_id_var)
+            grouping_set = self._parse_csv(self._parse_column)
             self._match_r_paren()
             return self.expression(exp.Tuple, expressions=grouping_set)
 
-        return self._parse_id_var()
+        return self._parse_column()
 
     def _parse_having(self, skip_having_token: bool = False) -> t.Optional[exp.Expression]:
         if not skip_having_token and not self._match(TokenType.HAVING):


Test patch:
diff --git a/tests/dialects/test_hive.py b/tests/dialects/test_hive.py
--- a/tests/dialects/test_hive.py
+++ b/tests/dialects/test_hive.py
@@ -338,6 +338,15 @@ def test_order_by(self):
         )
 
     def test_hive(self):
+        self.validate_identity(
+            "SELECT a, b, SUM(c) FROM tabl AS t GROUP BY a, b GROUPING SETS ((a, b), a)"
+        )
+        self.validate_identity(
+            "SELECT a, b, SUM(c) FROM tabl AS t GROUP BY a, b GROUPING SETS ((t.a, b), a)"
+        )
+        self.validate_identity(
+            "SELECT a, b, SUM(c) FROM tabl AS t GROUP BY a, FOO(b) GROUPING SETS ((a, FOO(b)), a)"
+        )
         self.validate_all(
             "SELECT A.1a AS b FROM test_a AS A",
             write={
@@ -615,3 +624,20 @@ def test_hive(self):
                 "spark": "SELECT * FROM x TABLESAMPLE(1) AS foo",
             },
         )
+        self.validate_all(
+            "SELECT * FROM x TABLESAMPLE(1) AS foo",
+            read={
+                "presto": "SELECT * FROM x AS foo TABLESAMPLE(1)",
+            },
+            write={
+                "presto": "SELECT * FROM x AS foo TABLESAMPLE(1)",
+                "hive": "SELECT * FROM x TABLESAMPLE(1) AS foo",
+                "spark": "SELECT * FROM x TABLESAMPLE(1) AS foo",
+            },
+        )
+        self.validate_all(
+            "SELECT a, SUM(c) FROM t GROUP BY a, DATE_FORMAT(b, 'yyyy') GROUPING SETS ((a, DATE_FORMAT(b, 'yyyy')), a)",
+            write={
+                "hive": "SELECT a, SUM(c) FROM t GROUP BY a, DATE_FORMAT(CAST(b AS TIMESTAMP), 'yyyy') GROUPING SETS ((a, DATE_FORMAT(CAST(b AS TIMESTAMP), 'yyyy')), a)",
+            },
+        )