Skip to content

Commit

Permalink
Merge in latest from main.
Browse files Browse the repository at this point in the history
  • Loading branch information
cfhowes committed Jan 5, 2024
2 parents 0c44ef9 + bb52593 commit 9788bc4
Show file tree
Hide file tree
Showing 8 changed files with 46,374 additions and 440 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
### Improvements
1. Support for KEY statement in CREATE TABLE statements. KEY statements will now create INDEX entries in the DDL parser.

**v0.31.3**
### Improvements
#### Snowflake update:
1. Added support for Snowflake Virtual Column definition in External Column `AS ()` statement - https://github.com/xnuinside/simple-ddl-parser/issues/218

**v0.31.2**
### 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.2"
version = "0.31.3"
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
3 changes: 2 additions & 1 deletion simple_ddl_parser/dialects/hql.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
class HQL:
def p_expression_location(self, p):
"""expr : expr LOCATION STRING
| expr LOCATION DQ_STRING"""
| expr LOCATION DQ_STRING
| expr LOCATION table_property_equals"""
p[0] = p[1]
p_list = list(p)
p[0]["location"] = p_list[-1]
Expand Down
57 changes: 56 additions & 1 deletion simple_ddl_parser/dialects/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ def p_expression_cluster_by(self, p):
def p_table_property_equals(self, p: List) -> None:
"""table_property_equals : id id id_or_string
| id id_or_string
| LP id id id_or_string RP
| LP id_or_string RP
"""
p_list = remove_par(list(p))
p[0] = str(p_list[-1])

def p_table_property_equals_int(self, p: List) -> None:
"""table_property_equals_int : id id id_or_string
| id id_or_string
| LP id id id_or_string RP
| LP id_or_string RP
"""
p_list = remove_par(list(p))
p[0] = int(p_list[-1])
Expand All @@ -36,7 +47,7 @@ def p_table_property_equals_bool(self, p: List) -> None:
p[0] = False

def p_expression_data_retention_time_in_days(self, p):
"""expr : expr DATA_RETENTION_TIME_IN_DAYS table_property_equals"""
"""expr : expr DATA_RETENTION_TIME_IN_DAYS table_property_equals_int"""
p[0] = p[1]
p_list = remove_par(list(p))
p[0]["data_retention_time_in_days"] = p_list[-1]
Expand Down Expand Up @@ -119,3 +130,47 @@ def p_option_with_masking_policy(self, p):
"""
p_list = remove_par(list(p))
p[0] = {"with_masking_policy": f"{p_list[-5]}.{p_list[-3]}.{p_list[-1]}"}

def p_expression_catalog(self, p):
"""expr : expr CATALOG table_property_equals"""
p[0] = p[1]
p_list = remove_par(list(p))
p[0]["catalog"] = p_list[-1]

def p_expression_file_format(self, p):
"""expr : expr FILE_FORMAT table_property_equals"""
p[0] = p[1]
p_list = remove_par(list(p))
p[0]["file_format"] = p_list[-1]

def p_expression_stage_file_format(self, p):
"""expr : expr STAGE_FILE_FORMAT table_property_equals"""
p[0] = p[1]
p_list = remove_par(list(p))
p[0]["stage_file_format"] = p_list[-1]

def p_expression_table_format(self, p):
"""expr : expr TABLE_FORMAT table_property_equals"""
p[0] = p[1]
p_list = remove_par(list(p))
p[0]["table_format"] = p_list[-1]

def p_expression_auto_refresh(self, p):
"""expr : expr AUTO_REFRESH table_property_equals_bool"""
p[0] = p[1]
p_list = remove_par(list(p))
p[0]["auto_refresh"] = p_list[-1]

def p_as_virtual(self, p: List):
"""as_virtual : AS LP id LP id LP pid RP COMMA pid RP RP
| AS LP id LP pid RP RP
| AS LP multi_id RP"""
_as = ""
# Simple function else Nested function call
if len(p) == 5:
_as = p[3]
else:
# _as = p[3]+p[4]+p[5]+p[6]+",".join(p[7])+p[8]+p[9]+",".join(p[10])+p[11]
for i in p[3:len(p) - 1]:
_as += i if isinstance(i, str) else ",".join(i)
p[0] = {"generated": {"as": _as}}
31 changes: 19 additions & 12 deletions simple_ddl_parser/dialects/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def p_create_table(self, p: List):
| CREATE OR REPLACE id TABLE
"""
# id - for EXTERNAL, TRANSIENT, TEMPORARY, GLOBAL, LOCAL, TEMP, VOLATILE
# id - for EXTERNAL, TRANSIENT, TEMPORARY, GLOBAL, LOCAL, TEMP, VOLATILE, ICEBERG
# get schema & table name
p[0] = {}
p_list = list(p)
Expand Down Expand Up @@ -315,9 +315,9 @@ def p_column(self, p: List) -> None:
| column LP id COMMA id RP
| column LP id COMMA id RP c_type
"""
if p[1] == 'KEY':
if p[1] == "KEY":
# This is an index
p[0] = {'index_stmt': True, 'name': p[2]["type"], 'columns': ''}
p[0] = {"index_stmt": True, "name": p[2]["type"], "columns": ""}
return
if p[1] and isinstance(p[1], dict) and p[1].get("index_stmt") is True:
p[1]["columns"] = remove_par(list(p))[2]
Expand Down Expand Up @@ -410,6 +410,7 @@ def p_defcolumn(self, p: List) -> None:
| defcolumn option_order_noorder
| defcolumn option_with_tag
| defcolumn option_with_masking_policy
| defcolumn as_virtual
"""
p[0] = p[1]
p_list = list(p)
Expand Down Expand Up @@ -865,15 +866,21 @@ def p_expression_table(self, p: List) -> None: # noqa R701
if not p[0].get("index"):
p[0]["index"] = []
index_data = p_list[-1]
p[0]["index"].append({
"clustered": False,
"columns": [index_data["columns"]],
"detailed_columns": [
{"name": index_data["columns"], "nulls": "LAST", "order": "ASC"}
],
"index_name": index_data["name"],
"unique": False,
})
p[0]["index"].append(
{
"clustered": False,
"columns": [index_data["columns"]],
"detailed_columns": [
{
"name": index_data["columns"],
"nulls": "LAST",
"order": "ASC",
}
],
"index_name": index_data["name"],
"unique": False,
}
)
elif "check" in p_list[-1]:
p[0] = self.extract_check_data(p, p_list)
elif "enforced" in p_list[-1]:
Expand Down
Loading

0 comments on commit 9788bc4

Please sign in to comment.