Repository: tobymao/sqlglot

Issue:
GROUP BY DISTINCT fails when parsing presto query
**Before you file an issue**
- [x] Make sure you specify the "read" dialect eg. `parse_one(sql, read="spark")`
- [x] Make sure you specify the "write" dialect eg. `ast.sql(dialect="duckdb")`

**Fully reproducible code snippet**
```
import sqlglot
asts = sqlglot.parse(q, dialect='presto')
q = "select json_extract_scalar(CAST(extra AS JSON), '$.value_b'), count(*) from table_a GROUP BY GROUP BY DISTINCT(json_extract_scalar(CAST(extra AS JSON), '$.value_b'))"
asts = sqlglot.parse(q, dialect='presto')
```

**Official Documentation**
Please include links to official SQL documentation related to your issue.
https://prestodb.io/docs/current/sql/select.html

Discussion:

Patch:
diff --git a/sqlglot/generator.py b/sqlglot/generator.py
index 6563accb..b3e586c7 100644
--- a/sqlglot/generator.py
+++ b/sqlglot/generator.py
@@ -1803,10 +1803,15 @@ class Generator(metaclass=_Generator):
         return f"{self.seg('FROM')} {self.sql(expression, 'this')}"
 
     def group_sql(self, expression: exp.Group) -> str:
-        group_by = self.op_expressions("GROUP BY", expression)
+        group_by_all = expression.args.get("all")
+        if group_by_all is True:
+            modifier = " ALL"
+        elif group_by_all is False:
+            modifier = " DISTINCT"
+        else:
+            modifier = ""
 
-        if expression.args.get("all"):
-            return f"{group_by} ALL"
+        group_by = self.op_expressions(f"GROUP BY{modifier}", expression)
 
         grouping_sets = self.expressions(expression, key="grouping_sets", indent=False)
         grouping_sets = (
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
index ab91576f..c438a703 100644
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -3463,10 +3463,12 @@ class Parser(metaclass=_Parser):
         if not skip_group_by_token and not self._match(TokenType.GROUP_BY):
             return None
 
-        elements = defaultdict(list)
+        elements: t.Dict[str, t.Any] = defaultdict(list)
 
         if self._match(TokenType.ALL):
-            return self.expression(exp.Group, all=True)
+            elements["all"] = True
+        elif self._match(TokenType.DISTINCT):
+            elements["all"] = False
 
         while True:
             expressions = self._parse_csv(self._parse_conjunction)


Test patch:
diff --git a/tests/dialects/test_presto.py b/tests/dialects/test_presto.py
index 5df3d0e8..e1d8c068 100644
--- a/tests/dialects/test_presto.py
+++ b/tests/dialects/test_presto.py
@@ -612,6 +612,15 @@ class TestPresto(Validator):
         self.validate_identity(
             "SELECT * FROM example.testdb.customer_orders FOR TIMESTAMP AS OF CAST('2022-03-23 09:59:29.803 Europe/Vienna' AS TIMESTAMP)"
         )
+        self.validate_identity(
+            "SELECT origin_state, destination_state, origin_zip, SUM(package_weight) FROM shipping GROUP BY ALL CUBE (origin_state, destination_state), ROLLUP (origin_state, origin_zip)"
+        )
+        self.validate_identity(
+            "SELECT origin_state, destination_state, origin_zip, SUM(package_weight) FROM shipping GROUP BY DISTINCT CUBE (origin_state, destination_state), ROLLUP (origin_state, origin_zip)"
+        )
+        self.validate_identity(
+            "SELECT JSON_EXTRACT_SCALAR(CAST(extra AS JSON), '$.value_b'), COUNT(*) FROM table_a GROUP BY DISTINCT (JSON_EXTRACT_SCALAR(CAST(extra AS JSON), '$.value_b'))"
+        )
 
         self.validate_all(
             "SELECT LAST_DAY_OF_MONTH(CAST('2008-11-25' AS DATE))",