shinichi-takii / ddlparse

DDL parase and Convert to BigQuery JSON schema and DDL statements
https://pypi.org/project/ddlparse/
BSD 3-Clause "New" or "Revised" License
87 stars 29 forks source link

ddlparse throws an error when primary key is composite #36

Closed noahbowman closed 5 years ago

noahbowman commented 5 years ago

It seems to me that with the use of 'delimitedList', the primary key should allow multiple columns.

...Optional(Suppress(_CONSTRAINT) + Optional(_SUPPRESS_QUOTE) + Word(alphanums + "_")("name") + Optional(
                    _SUPPRESS_QUOTE))
                + (
                        (
                                (_PRIMARY_KEY ^ _UNIQUE ^ _UNIQUE_KEY ^ _NOT_NULL)("type")
                                + Optional(_SUPPRESS_QUOTE) + Optional(Word(alphanums + "_"))("name") + Optional(
                            _SUPPRESS_QUOTE)
                                + _LPAR + Group(delimitedList(
                            Optional(_SUPPRESS_QUOTE) + Word(alphanums + "_") + Optional(_SUPPRESS_QUOTE)))(
                            "constraint_columns") + _RPAR
                        )...

However, when provided with a line such as: primary key (column1, column2, column3), pyparsing raises the following exception:

raise ParseException(instring, loc, self.errmsg, self)
pyparsing.ParseException: Expected ")" (at char 325), (line:9, col:38)

When provided a table with a primary key constraint on only one column (primary key (column1)), ddlparse handles this with no problem.

shinichi-takii commented 5 years ago

Thank you for report.

I checked with these SQL.

CREATE TABLE Sample_Table_1 (
    Col_01 varchar(100),
    Col_02 char(200),
    Col_03 integer,
    Col_04 double,
    Col_05 datetime,
    CONSTRAINT const_01 PRIMARY KEY (Col_01)
);
CREATE TABLE Sample_Table_2 (
    Col_01 varchar(100),
    Col_02 char(200),
    Col_03 integer,
    Col_04 double,
    Col_05 datetime,
    CONSTRAINT const_01 PRIMARY KEY (Col_01, Col_02)
);

But I did not find the problem. Please give me the SQL to reproduce.

noahbowman commented 5 years ago

Hello,

Seems I have been mistaken. For reference, I was using the postgresql sytax like below. However, it must have been a result of my own code that the pk constraints were not being parsed, as I tried again with a fresh clone and the issue is no longer. Sorry for the false alarm!

CREATE TABLE Sample_Table_2 (
        Col_01 varchar(100),
        Col_02 char(20  0),
        Col_03 integer,
        Col_04 double,
        Col_05 datetime,
       PRIMARY KEY (Col_01, Col_02)
);

edit: code insert