Skip to content

TSQL: PRIMARY KEY constraint fails to parse if sort option is given #4610

Closed
@cchambers-rdi

Description

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.

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions