Skip to content

Commit 284a936

Browse files
committed
Refactor: simplify WITHIN GROUP ... FILTER support
1 parent 1ea0dc2 commit 284a936

File tree

4 files changed

+9
-19
lines changed

4 files changed

+9
-19
lines changed

sqlglot/expressions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1577,7 +1577,7 @@ def recursive(self) -> bool:
15771577

15781578

15791579
class WithinGroup(Expression):
1580-
arg_types = {"this": True, "expression": False, "filter": False}
1580+
arg_types = {"this": True, "expression": False}
15811581

15821582

15831583
# clickhouse supports scalar ctes

sqlglot/generator.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,11 +2724,7 @@ def windowspec_sql(self, expression: exp.WindowSpec) -> str:
27242724
def withingroup_sql(self, expression: exp.WithinGroup) -> str:
27252725
this = self.sql(expression, "this")
27262726
expression_sql = self.sql(expression, "expression")[1:] # order has a leading space
2727-
filter_sql = self.sql(expression, "filter")[1:]
2728-
result = f"{this} WITHIN GROUP ({expression_sql})"
2729-
if filter_sql:
2730-
result = f"{result} FILTER ({filter_sql})"
2731-
return result
2727+
return f"{this} WITHIN GROUP ({expression_sql})"
27322728

27332729
def between_sql(self, expression: exp.Between) -> str:
27342730
this = self.sql(expression, "this")

sqlglot/parser.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6603,25 +6603,19 @@ def _parse_window(
66036603
func = this
66046604
comments = func.comments if isinstance(func, exp.Expression) else None
66056605

6606+
# T-SQL allows the OVER (...) syntax after WITHIN GROUP.
6607+
# https://learn.microsoft.com/en-us/sql/t-sql/functions/percentile-disc-transact-sql?view=sql-server-ver16
6608+
if self._match_text_seq("WITHIN", "GROUP"):
6609+
order = self._parse_wrapped(self._parse_order)
6610+
this = self.expression(exp.WithinGroup, this=this, expression=order)
6611+
66066612
if self._match_pair(TokenType.FILTER, TokenType.L_PAREN):
66076613
self._match(TokenType.WHERE)
66086614
this = self.expression(
66096615
exp.Filter, this=this, expression=self._parse_where(skip_where_token=True)
66106616
)
66116617
self._match_r_paren()
66126618

6613-
# T-SQL allows the OVER (...) syntax after WITHIN GROUP.
6614-
# https://learn.microsoft.com/en-us/sql/t-sql/functions/percentile-disc-transact-sql?view=sql-server-ver16
6615-
if self._match_text_seq("WITHIN", "GROUP"):
6616-
order = self._parse_wrapped(self._parse_order)
6617-
if self._match(TokenType.FILTER):
6618-
where_clause = self._parse_wrapped(self._parse_where)
6619-
this = self.expression(
6620-
exp.WithinGroup, this=this, expression=order, filter=where_clause
6621-
)
6622-
else:
6623-
this = self.expression(exp.WithinGroup, this=this, expression=order)
6624-
66256619
# SQL spec defines an optional [ { IGNORE | RESPECT } NULLS ] OVER
66266620
# Some dialects choose to implement and some do not.
66276621
# https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html

tests/dialects/test_postgres.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ def test_postgres(self):
834834
)
835835

836836
self.validate_identity(
837-
"SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY a) FILTER (WHERE CAST(b AS BOOLEAN)) AS mean_value FROM (VALUES (0, 't')) AS fake_data(a, b)"
837+
"SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY a) FILTER(WHERE CAST(b AS BOOLEAN)) AS mean_value FROM (VALUES (0, 't')) AS fake_data(a, b)"
838838
)
839839

840840
def test_ddl(self):

0 commit comments

Comments
 (0)