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: table-constraint parse issues #2106

Closed
dmoore247 opened this issue Aug 23, 2023 · 0 comments
Closed

tsql: table-constraint parse issues #2106

dmoore247 opened this issue Aug 23, 2023 · 0 comments

Comments

@dmoore247
Copy link
Contributor

Last fix worked allowing me to test further.
Hopefully this is the last for this one table, found 3 parts of this constraint that (separately) cause a parse error in sqlglot:

  • The 1st parse issue is the ASC in the [zip_cd_mkey] ASC
  • The 2nd parse issue is the WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  • The 3rd parse issue is the 1st occurrence of ON [PRIMARY]

TSQL query (successful on sqlfiddle):

CREATE TABLE [dbo].[mytable](
	[zip_cd_mkey] [int] NOT NULL,
	[zip_cd] [varchar](5) NULL
  CONSTRAINT [pk_mytable] PRIMARY KEY CLUSTERED 
(
	[zip_cd_mkey] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
       ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
image

Fully reproducible code snippet

# parses
tsql0 = """CREATE TABLE [dbo].[t_cmm_dim_geo](
	[zip_cd_mkey] [int] NOT NULL,
	[zip_cd] [varchar](5) NULL,
 CONSTRAINT [pk_t_cmm_dim_geo__zip_cd_mkey] PRIMARY KEY CLUSTERED 
(
	[zip_cd_mkey]
)
) ON [PRIMARY]
"""
sqlglot.parse(sql=tsql0, read="tsql")
# fails on `ASC`
tsql1 = """CREATE TABLE [dbo].[t_cmm_dim_geo](
	[zip_cd_mkey] [int] NOT NULL,
	[zip_cd] [varchar](5) NULL,
 CONSTRAINT [pk_t_cmm_dim_geo__zip_cd_mkey] PRIMARY KEY CLUSTERED 
(
	[zip_cd_mkey] ASC
)
) ON [PRIMARY]
"""
sqlglot.parse(sql=tsql1, read="tsql")
# fails on the `WITH` clause
tsql2 = """CREATE TABLE [dbo].[t_cmm_dim_geo](
	[zip_cd_mkey] [int] NOT NULL,
	[zip_cd] [varchar](5) NULL,
 CONSTRAINT [pk_t_cmm_dim_geo__zip_cd_mkey] PRIMARY KEY CLUSTERED 
(
	[zip_cd_mkey]
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
) ON [PRIMARY]
"""
sqlglot.parse(sql=tsql2, read="tsql")
# fails on the `ON [PRIMARY]` clause
tsql3 = """CREATE TABLE [dbo].[t_cmm_dim_geo](
	[zip_cd_mkey] [int] NOT NULL,
	[zip_cd] [varchar](5) NULL,
 CONSTRAINT [pk_t_cmm_dim_geo__zip_cd_mkey] PRIMARY KEY CLUSTERED 
(
	[zip_cd_mkey]
) ON [PRIMARY]
) ON [PRIMARY]
"""
sqlglot.parse(sql=tsql3, read="tsql")
# fails
tsql4 = """CREATE TABLE [dbo].[mytable](
	[zip_cd_mkey] [int] NOT NULL,
	[zip_cd] [varchar](5) NULL
  CONSTRAINT [pk_mytable] PRIMARY KEY CLUSTERED 
(
	[zip_cd_mkey] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
       ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
"""
sqlglot.parse(sql=tsql4, read="tsql")

Official Documentation
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql?view=sql-server-ver16

<table_constraint> ::=
[ CONSTRAINT constraint_name ]
{
    { PRIMARY KEY | UNIQUE }
        [ CLUSTERED | NONCLUSTERED ]
        ( column_name [ ASC | DESC ] [ ,... n ] )
        [
            WITH FILLFACTOR = fillfactor
           | WITH ( <index_option> [ ,... n ] )
        ]
        [ ON { partition_scheme_name (partition_column_name)
            | filegroup | "default" } ]
    | FOREIGN KEY
        ( column_name [ ,... n ] )
        REFERENCES referenced_table_name [ ( ref_column [ ,... n ] ) ]
        [ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
        [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
        [ NOT FOR REPLICATION ]
    | CHECK [ NOT FOR REPLICATION ] ( logical_expression )
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

No branches or pull requests

1 participant