Repository: tobymao/sqlglot

Issue:
TSQL: PRIMARY KEY constraint fails to parse if sort option is given
The following CREATE TABLE statement parses just fine:
```
import sqlglot

test = """CREATE TABLE dbo.test (
      id INT,
      name VARCHAR(50),
      CONSTRAINT pk_testTable PRIMARY KEY (id)
      ) ;"""
        
    parsed_script = sqlglot.parse(test, read="tsql")
```

However, when I add ASC or DESC to the PRIMARY KEY constraint, it fails to parse:

```
import sqlglot

test = """CREATE TABLE dbo.test (
    id INT,
    name VARCHAR(50),
    CONSTRAINT pk_testTable PRIMARY KEY (id DESC)
    ) ;"""
        
    parsed_script = sqlglot.parse(test, read="tsql")
```

Error Message:

```
Exception has occurred: ParseError
Expecting ). Line 4, Col: 45.
  bo.test (
        id INT,
        name VARCHAR(50),
        CONSTRAINT pk_testTable PRIMARY KEY (id [4mDESC[0m)
        ) ;
  File "C:\*snip*\main.py", line 96, in main
    parsed_script = sqlglot.parse(test, read="tsql")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\*snip*\main.py", line 116, in <module>
    main()
sqlglot.errors.ParseError: Expecting ). Line 4, Col: 45.
  bo.test (
        id INT,
        name VARCHAR(50),
        CONSTRAINT pk_testTable PRIMARY KEY (id [4mDESC[0m)
        ) ;
```

Official Documentation: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql?view=sql-server-ver16#constraint

Edit: sqlglot version is 25.21.3.

Hints from issue comments:
@cchambers-rdi: Added version to OP.

Solution patch:
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
index 7f310b3b58..ae3f6cde66 100644
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -5902,7 +5902,7 @@ def _parse_foreign_key(self) -> exp.ForeignKey:
         )
 
     def _parse_primary_key_part(self) -> t.Optional[exp.Expression]:
-        return self._parse_field()
+        return self._parse_ordered() or self._parse_field()
 
     def _parse_period_for_system_time(self) -> t.Optional[exp.PeriodForSystemTimeConstraint]:
         if not self._match(TokenType.TIMESTAMP_SNAPSHOT):


Test patch:
diff --git a/tests/dialects/test_tsql.py b/tests/dialects/test_tsql.py
index 738120a2ee..fe1aa9c5dd 100644
--- a/tests/dialects/test_tsql.py
+++ b/tests/dialects/test_tsql.py
@@ -436,6 +436,13 @@ def test_tsql(self):
             "'a' + 'b'",
         )
 
+        self.validate_identity(
+            "CREATE TABLE db.t1 (a INTEGER, b VARCHAR(50), CONSTRAINT c PRIMARY KEY (a DESC))",
+        )
+        self.validate_identity(
+            "CREATE TABLE db.t1 (a INTEGER, b INTEGER, CONSTRAINT c PRIMARY KEY (a DESC, b))"
+        )
+
     def test_option(self):
         possible_options = [
             "HASH GROUP",