Skip to content

Commit

Permalink
Merge pull request #214 from dmaresma/213/snowflake_improvement
Browse files Browse the repository at this point in the history
feat. ORDER and NOORDER SEQ param
  • Loading branch information
xnuinside authored Dec 15, 2023
2 parents a37c3a2 + 6e78e81 commit ef6b40b
Show file tree
Hide file tree
Showing 7 changed files with 575 additions and 38,084 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@

**v0.31.2**
### Improvements
#### Snowflake update:
1. Added support for Snowflake AUTOINCREMENT | IDENTITY column definitions with optional parameter `ORDER|NOORDER` statement - https://github.com/xnuinside/simple-ddl-parser/issues/213

**v0.31.1**
### Improvements
#### Snowflake update:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "simple-ddl-parser"
version = "0.31.1"
version = "0.31.2"
description = "Simple DDL Parser to parse SQL & dialects like HQL, TSQL (MSSQL), Oracle, AWS Redshift, Snowflake, MySQL, PostgreSQL, etc ddl files to json/python dict with full information about columns: types, defaults, primary keys, etc.; sequences, alters, custom types & other entities from ddl."
authors = ["Iuliia Volkova <[email protected]>"]
license = "MIT"
Expand Down
7 changes: 7 additions & 0 deletions simple_ddl_parser/dialects/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ def p_multiple_tag_equals(self, p):
p[1].extend(p[3])
p[0] = p[1]

def p_option_order_noorder(self, p):
"""option_order_noorder : ORDER
| NOORDER
"""
p_list = remove_par(list(p))
p[0] = {"increment_order": True if p_list[1] == "ORDER" else False}

def p_option_with_tag(self, p):
"""option_with_tag : TAG LP id RP
| TAG LP id DOT id DOT id RP
Expand Down
5 changes: 5 additions & 0 deletions simple_ddl_parser/dialects/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ def p_defcolumn(self, p: List) -> None:
| defcolumn on_update
| defcolumn options
| defcolumn autoincrement
| defcolumn option_order_noorder
| defcolumn option_with_tag
| defcolumn option_with_masking_policy
"""
Expand Down Expand Up @@ -996,15 +997,19 @@ def p_table_name(self, p: List) -> None:
def p_expression_seq(self, p: List) -> None:
"""expr : seq_name
| expr INCREMENT id
| expr INCREMENT BY id
| expr INCREMENT id id
| expr START id
| expr START WITH id
| expr START id id
| expr MINVALUE id
| expr NO MINVALUE
| expr NO MAXVALUE
| expr MAXVALUE id
| expr CACHE id
| expr CACHE
| expr NOORDER
| expr ORDER
"""
# get schema & table name
p_list = list(p)
Expand Down
38,505 changes: 422 additions & 38,083 deletions simple_ddl_parser/parsetab.py

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions simple_ddl_parser/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
"MASKING": "MASKING",
"MASKED": "MASKED",
"WITH": "WITH",
"ORDER": "ORDER",
"NOORDER": "NOORDER"
}
first_liners = {
"LIKE": "LIKE",
Expand Down Expand Up @@ -102,10 +104,14 @@
sequence_reserved = {
"INCREMENT": "INCREMENT",
"START": "START",
"WITH": "WITH",
"MINVALUE": "MINVALUE",
"MAXVALUE": "MAXVALUE",
"CACHE": "CACHE",
"NO": "NO",
"BY": "BY",
"NOORDER": "NOORDER",
"ORDER": "ORDER"
}


Expand Down
128 changes: 128 additions & 0 deletions tests/dialects/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,3 +640,131 @@ def test_double_single_quotes():
"types": [],
}
assert result == expected


def test_autoincrement_order():
# test for https://github.com/xnuinside/simple-ddl-parser/issues/208
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": False,
"references": None,
"size": (38, 0),
"type": "NUMBER",
"unique": False,
"autoincrement": True,
"start" : '1',
"increment": '1',
"increment_order": True
}
],
"index": [],
"partitioned_by": [],
"primary_key": [],
"schema": None,
"table_name": "table",
"tablespace": None,
}
],
"types": [],
}
print(result)
assert result == expected

def test_autoincrement_noorder():
# test for https://github.com/xnuinside/simple-ddl-parser/issues/208
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": False,
"references": None,
"size": (38, 0),
"type": "NUMBER",
"unique": False,
"autoincrement": True,
"start" : '1',
"increment": '1',
"increment_order": False
}
],
"index": [],
"partitioned_by": [],
"primary_key": [],
"schema": None,
"table_name": "table",
"tablespace": None,
}
],
"types": [],
}
print(result)
assert result == expected

def test_order_sequence():
parse_results = DDLParser(
"""
CREATE SEQUENCE dev.incremental_ids_order
START 1
INCREMENT 1
ORDER;
"""
).run()
expected = [
{
"schema": "dev",
"sequence_name": "incremental_ids",
"increment": 1,
"start": 1,
"order": True,
}
]
assert expected == parse_results

def test_order_sequence():
parse_results = DDLParser(
"""
CREATE SEQUENCE dev.incremental_ids_order
START WITH 1
INCREMENT BY 1
NOORDER;
"""
).run()
expected = [
{
"schema": "dev",
"sequence_name": "incremental_ids_order",
"increment_by": 1,
"start_with": 1,
"noorder": True,
}
]
assert expected == parse_results

0 comments on commit ef6b40b

Please sign in to comment.