Repository: tobymao/sqlglot

Issue:
Redshift Dialect - GROUP BY clause causes parsing error although query is valid
**Fully reproducible code snippet**
``` CREATE OR REPLACE VIEW my_view AS
SELECT id,
       AVG(average_metric1)                   AS m1,
       AVG(average_metric2)                   AS m2
FROM table
GROUP BY id
WITH NO SCHEMA BINDING;
```
This query causes the following parsing error when running the below code, although query is valid in **Redshift**:

`parse(query, read="redshift")`

![image](https://github.com/tobymao/sqlglot/assets/19167079/f0552550-3ddf-4ecd-8cca-7306c4a8d069)

Removing `GROUP BY id` solves the parsing issue, however is not a valid Redshift query in that case.


**Official Documentation**
Please include links to official SQL documentation related to your issue.



Discussion:
Please make sure to either include documentation or examples that prove the syntax is valid, i.e. by actually running it against the engine and showing there's no syntax error.

In this case I tried to run this query against Redshift and I got back a syntax error:

```
ERROR: syntax error at or near "WITH" Position: 28
```

Query (also tried your query as-is, no difference):
```sql
SELECT *
FROM t
GROUP BY a
WITH NO SCHEMA BINDING;
```

I'm gonna go ahead and close this as a non-issue. Happy to continue chatting / reopen this if I missed something.
The only way of proving that syntax is right is by running it, I don't know how else.
The query above is adjusted for example purposes as the original one is company-internal and therefore confidential.

Regarding the documentation, there is no documentation that's pinpointing the error here. It's just an SQL knowledge that if there are aggregates in the SELECT statement, **the column(s) that aggregates are based on** should also be in `GROUP BY` statement. Here is docs if it helps: https://docs.aws.amazon.com/redshift/latest/dg/r_GROUP_BY_clause.html

I believe that `sqlglot` doesn't (or missed to) check the semantics of this query for Redshift. Please let me know where to start and I'm willing to work on it.

Lastly, I appreciate for this package and contributions, but closing down the issues right away seems like an odd choice here for me. At least, keep it open for a day or two for the discussions happening. 
Thanks!

Resolving patch:
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -2952,6 +2952,7 @@ def _parse_group(self, skip_group_by_token: bool = False) -> t.Optional[exp.Grou
             cube = None
             totals = None
 
+            index = self._index
             with_ = self._match(TokenType.WITH)
             if self._match(TokenType.ROLLUP):
                 rollup = with_ or self._parse_wrapped_csv(self._parse_column)
@@ -2966,6 +2967,8 @@ def _parse_group(self, skip_group_by_token: bool = False) -> t.Optional[exp.Grou
                 elements["totals"] = True  # type: ignore
 
             if not (grouping_sets or rollup or cube or totals):
+                if with_:
+                    self._retreat(index)
                 break
 
         return self.expression(exp.Group, **elements)  # type: ignore


Test patch:
diff --git a/tests/dialects/test_redshift.py b/tests/dialects/test_redshift.py
--- a/tests/dialects/test_redshift.py
+++ b/tests/dialects/test_redshift.py
@@ -6,6 +6,9 @@ class TestRedshift(Validator):
     dialect = "redshift"
 
     def test_redshift(self):
+        self.validate_identity(
+            "CREATE OR REPLACE VIEW v1 AS SELECT id, AVG(average_metric1) AS m1, AVG(average_metric2) AS m2 FROM t GROUP BY id WITH NO SCHEMA BINDING"
+        )
         self.validate_all(
             "SELECT APPROXIMATE COUNT(DISTINCT y)",
             read={