Repository: tobymao/sqlglot

Issue:
Parse error: character varying
I am trying to parse a create function sql. The error message is like this.

```
ParseError: Expecting ). Line 1, Col: 81.
  CREATE OR REPLACE FUNCTION function_name (input_a character varying DEFAULT NULL::character varying)
```
The first "character" is underlined.

It seems **character varying** cannot be parse correctly, which should be equivalent to VARCHAR.

> The notations varchar(n) and char(n) are aliases for character varying(n) and character(n), respectively. If specified, the length must be greater than zero and cannot exceed 10485760. character without length specifier is equivalent to character(1). If character varying is used without length specifier, the type accepts strings of any size.

Reference: https://www.postgresql.org/docs/current/datatype-character.html

Discussion:
tobymao: nice

Patch:
diff --git a/sqlglot/dialects/tsql.py b/sqlglot/dialects/tsql.py
index c3757c90..b9f932bc 100644
--- a/sqlglot/dialects/tsql.py
+++ b/sqlglot/dialects/tsql.py
@@ -408,7 +408,7 @@ class TSQL(Dialect):
             ):
                 return this
 
-            expressions = self._parse_csv(self._parse_udf_kwarg)
+            expressions = self._parse_csv(self._parse_function_parameter)
             return self.expression(exp.UserDefinedFunction, this=this, expressions=expressions)
 
     class Generator(generator.Generator):
diff --git a/sqlglot/expressions.py b/sqlglot/expressions.py
index ccab8a1b..0af1dbe5 100644
--- a/sqlglot/expressions.py
+++ b/sqlglot/expressions.py
@@ -825,10 +825,6 @@ class UserDefinedFunction(Expression):
     arg_types = {"this": True, "expressions": False, "wrapped": False}
 
 
-class UserDefinedFunctionKwarg(Expression):
-    arg_types = {"this": True, "kind": True, "default": False}
-
-
 class CharacterSet(Expression):
     arg_types = {"this": True, "default": False}
 
diff --git a/sqlglot/generator.py b/sqlglot/generator.py
index 9bd089cf..7b1376d8 100644
--- a/sqlglot/generator.py
+++ b/sqlglot/generator.py
@@ -1905,11 +1905,6 @@ class Generator:
         )
         return f"{this}{expressions}"
 
-    def userdefinedfunctionkwarg_sql(self, expression: exp.UserDefinedFunctionKwarg) -> str:
-        this = self.sql(expression, "this")
-        kind = self.sql(expression, "kind")
-        return f"{this} {kind}"
-
     def joinhint_sql(self, expression: exp.JoinHint) -> str:
         this = self.sql(expression, "this")
         expressions = self.expressions(expression, flat=True)
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
index 4d04aa67..467d6a9e 100644
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -1710,7 +1710,7 @@ class Parser(metaclass=_Parser):
         index = self._index
 
         if self._match(TokenType.L_PAREN):
-            columns = self._parse_csv(lambda: self._parse_column_def(self._parse_id_var()))
+            columns = self._parse_csv(self._parse_function_parameter)
             self._match_r_paren() if columns else self._retreat(index)
         else:
             columns = None
@@ -2749,6 +2749,9 @@ class Parser(metaclass=_Parser):
         self._match_r_paren(this)
         return self._parse_window(this)
 
+    def _parse_function_parameter(self) -> t.Optional[exp.Expression]:
+        return self._parse_column_def(self._parse_id_var())
+
     def _parse_user_defined_function(
         self, kind: t.Optional[TokenType] = None
     ) -> t.Optional[exp.Expression]:
@@ -2760,7 +2763,7 @@ class Parser(metaclass=_Parser):
         if not self._match(TokenType.L_PAREN):
             return this
 
-        expressions = self._parse_csv(self._parse_udf_kwarg)
+        expressions = self._parse_csv(self._parse_function_parameter)
         self._match_r_paren()
         return self.expression(
             exp.UserDefinedFunction, this=this, expressions=expressions, wrapped=True
@@ -2786,15 +2789,6 @@ class Parser(metaclass=_Parser):
 
         return self.expression(exp.SessionParameter, this=this, kind=kind)
 
-    def _parse_udf_kwarg(self) -> t.Optional[exp.Expression]:
-        this = self._parse_id_var()
-        kind = self._parse_types()
-
-        if not kind:
-            return this
-
-        return self.expression(exp.UserDefinedFunctionKwarg, this=this, kind=kind)
-
     def _parse_lambda(self) -> t.Optional[exp.Expression]:
         index = self._index
 


Test patch:
diff --git a/tests/dialects/test_postgres.py b/tests/dialects/test_postgres.py
index 74889610..f0117bc2 100644
--- a/tests/dialects/test_postgres.py
+++ b/tests/dialects/test_postgres.py
@@ -10,6 +10,13 @@ class TestPostgres(Validator):
         self.validate_identity("CREATE TABLE test (foo HSTORE)")
         self.validate_identity("CREATE TABLE test (foo JSONB)")
         self.validate_identity("CREATE TABLE test (foo VARCHAR(64)[])")
+
+        self.validate_all(
+            "CREATE OR REPLACE FUNCTION function_name (input_a character varying DEFAULT NULL::character varying)",
+            write={
+                "postgres": "CREATE OR REPLACE FUNCTION function_name(input_a VARCHAR DEFAULT CAST(NULL AS VARCHAR))",
+            },
+        )
         self.validate_all(
             "CREATE TABLE products (product_no INT UNIQUE, name TEXT, price DECIMAL)",
             write={