Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cchambers-rdi opened this issue Jan 14, 2025 · 1 comment · Fixed by #4618
Closed

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

cchambers-rdi opened this issue Jan 14, 2025 · 1 comment · Fixed by #4618
Assignees

Comments

@cchambers-rdi
Copy link

cchambers-rdi commented Jan 14, 2025

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.

@cchambers-rdi
Copy link
Author

Added version to OP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants