xnuinside / simple-ddl-parser

Simple DDL Parser to parse SQL (HQL, TSQL, AWS Redshift, BigQuery, Snowflake and other dialects) ddl files to json/python dict with full information about columns: types, defaults, primary keys, etc. & table properties, types, domains, etc.
MIT License
183 stars 41 forks source link

Snowflake support { AUTOINCREMENT | IDENTITY } with [ { ORDER | NOORDER } ] optional parameter #213

Closed dmaresma closed 10 months ago

dmaresma commented 10 months ago

Describe the bug Adding support Snowflake CREATE TABLE { AUTOINCREMENT | IDENTITY } column definition with explicit Order or NoOrder optional parameters

https://docs.snowflake.com/en/sql-reference/sql/create-table To Reproduce run the following tests ` def test_autoincrement_order():

test for https://github.com/xnuinside/simple-ddl-parser/issues/20x

ddl = """CREATE TABLE table (surrogatekey_SK NUMBER(38,0) NOT NULL autoincrement start 1 increment 1 ORDER COMMENT 'Record Identification Number Ordered')"""
result = DDLParser(ddl).run(group_by_type=True)
expected = {
    "ddl_properties": [],
    "domains": [],
    "schemas": [],
    "sequences": [],
    "tables": [
        {
            "alter": {},
            "checks": [],
            "columns": [
                {
                    "check": None,
                    "comment": "Record Identification Number Ordered",
                    "default": None,
                    "name": "surrogatekey_SK",
                    "nullable": True,
                    "references": None,
                    "size": None,
                    "type": "int",
                    "unique": False,
                    "autoincrement":True,
                    "start" : 1,
                    "increment": 1
                }
            ],
            "index": [],
            "partitioned_by": [],
            "primary_key": [],
            "schema": None,
            "table_name": "table",
            "tablespace": None,
        }
    ],
    "types": [],
}
assert result == expected

def test_autoincrement_noorder():

test for https://github.com/xnuinside/simple-ddl-parser/issues/20x

ddl = """CREATE TABLE table (surrogatekey_SK NUMBER(38,0) NOT NULL autoincrement start 1 increment 1 NOORDER COMMENT 'Record Identification Number NoOrdered')"""
result = DDLParser(ddl).run(group_by_type=True)
expected = {
    "ddl_properties": [],
    "domains": [],
    "schemas": [],
    "sequences": [],
    "tables": [
        {
            "alter": {},
            "checks": [],
            "columns": [
                {
                    "check": None,
                    "comment": "Record Identification Number NoOrdered",
                    "default": None,
                    "name": "surrogatekey_SK",
                    "nullable": True,
                    "references": None,
                    "size": None,
                    "type": "int",
                    "unique": False,
                    "autoincrement":True,
                    "start" : 1,
                    "increment": None
                }
            ],
            "index": [],
            "partitioned_by": [],
            "primary_key": [],
            "schema": None,
            "table_name": "table",
            "tablespace": None,
        }
    ],
    "types": [],
}
assert result == expected`

Expected behavior no regression, keep autoincrement: true , increment:none in case of NOORDER and 1 in case of ORDER

xnuinside commented 10 months ago

@dmaresma thanks one more time for your PR, I released 0.31.2 with your changes