Skip to content

Commit

Permalink
Feat(snowflake): add support for GROUP BY ALL (#1864)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Jun 30, 2023
1 parent d6c1569 commit 2e1a2b8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,7 @@ class Group(Expression):
"cube": False,
"rollup": False,
"totals": False,
"all": False,
}


Expand Down
4 changes: 4 additions & 0 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,10 @@ def from_sql(self, expression: exp.From) -> str:

def group_sql(self, expression: exp.Group) -> str:
group_by = self.op_expressions("GROUP BY", expression)

if expression.args.get("all"):
return f"{group_by} ALL"

grouping_sets = self.expressions(expression, key="grouping_sets", indent=False)
grouping_sets = (
f"{self.seg('GROUPING SETS')} {self.wrap(grouping_sets)}" if grouping_sets else ""
Expand Down
3 changes: 3 additions & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,9 @@ def _parse_group(self, skip_group_by_token: bool = False) -> t.Optional[exp.Grou

elements = defaultdict(list)

if self._match(TokenType.ALL):
return self.expression(exp.Group, all=True)

while True:
expressions = self._parse_csv(self._parse_conjunction)
if expressions:
Expand Down
4 changes: 4 additions & 0 deletions tests/dialects/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class TestSnowflake(Validator):
dialect = "snowflake"

def test_snowflake(self):
self.validate_identity("SELECT SUM(amount) FROM mytable GROUP BY ALL")
self.validate_identity("WITH x AS (SELECT 1 AS foo) SELECT foo FROM IDENTIFIER('x')")
self.validate_identity("WITH x AS (SELECT 1 AS foo) SELECT IDENTIFIER('foo') FROM x")
self.validate_identity("INITCAP('iqamqinterestedqinqthisqtopic', 'q')")
Expand Down Expand Up @@ -33,6 +34,9 @@ def test_snowflake(self):
self.validate_identity(
'COPY INTO NEW_TABLE ("foo", "bar") FROM (SELECT $1, $2, $3, $4 FROM @%old_table)'
)
self.validate_identity(
"SELECT state, city, SUM(retail_price * quantity) AS gross_revenue FROM sales GROUP BY ALL"
)

self.validate_all("CAST(x AS BYTEINT)", write={"snowflake": "CAST(x AS INT)"})
self.validate_all("CAST(x AS CHAR VARYING)", write={"snowflake": "CAST(x AS VARCHAR)"})
Expand Down

0 comments on commit 2e1a2b8

Please sign in to comment.