Repository: tobymao/sqlglot

Issue:
Parsing fails for DDL with AggregateFunctions containing a keyword
Hi Sqlglot team! While playing around with Sqlglot recently I ran into an issue parsing some DDL for the `clickhouse` dialect. Specifically it looks like parsing fails for cases where a Clickhouse table definition includes a column of type `AggregateFunction` which uses a parameterless function without arguments that is also a keyword. For example:

```sql
CREATE TABLE my_db.my_table
  (
      someId UUID,
      aggregatedColumn AggregateFunction(any, String),
  )
ENGINE = AggregatingMergeTree()
ORDER BY (someId)
```

Version: `26.4.1`
Dialect: `clickhouse` (but could apply to any other dialect utilizing aggregate functions)

Links:
* https://clickhouse.com/docs/en/sql-reference/data-types/aggregatefunction
* https://clickhouse.com/docs/en/sql-reference/aggregate-functions/reference/any

Hints from issue comments:

Solution patch:
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
index 83c8bb2731..99f0280899 100644
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -5003,7 +5003,7 @@ def _parse_types(
                 expressions = self._parse_csv(self._parse_equality)
             elif is_aggregate:
                 func_or_ident = self._parse_function(anonymous=True) or self._parse_id_var(
-                    any_token=False, tokens=(TokenType.VAR,)
+                    any_token=False, tokens=(TokenType.VAR, TokenType.ANY)
                 )
                 if not func_or_ident or not self._match(TokenType.COMMA):
                     return None


Test patch:
diff --git a/tests/dialects/test_clickhouse.py b/tests/dialects/test_clickhouse.py
index de072cdae5..5ea0e54fed 100644
--- a/tests/dialects/test_clickhouse.py
+++ b/tests/dialects/test_clickhouse.py
@@ -682,6 +682,33 @@ def test_geom_types(self):
             with self.subTest(f"Casting to ClickHouse {data_type}"):
                 self.validate_identity(f"SELECT CAST(val AS {data_type})")
 
+    def test_aggregate_function_column_with_any_keyword(self):
+        # Regression test for https://github.com/tobymao/sqlglot/issues/4723
+        self.validate_all(
+            """
+            CREATE TABLE my_db.my_table
+            (
+                someId UUID,
+                aggregatedColumn AggregateFunction(any, String),
+                aggregatedColumnWithParams AggregateFunction(any(somecolumn), String),
+            )
+            ENGINE = AggregatingMergeTree()
+            ORDER BY (someId)
+                    """,
+            write={
+                "clickhouse": """CREATE TABLE my_db.my_table (
+  someId UUID,
+  aggregatedColumn AggregateFunction(any, String),
+  aggregatedColumnWithParams AggregateFunction(any(somecolumn), String)
+)
+ENGINE=AggregatingMergeTree()
+ORDER BY (
+  someId
+)""",
+            },
+            pretty=True,
+        )
+
     def test_ddl(self):
         db_table_expr = exp.Table(this=None, db=exp.to_identifier("foo"), catalog=None)
         create_with_cluster = exp.Create(