Skip to content

Commit

Permalink
Add docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
syou6162 committed Feb 10, 2024
1 parent 63e45bb commit e456855
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions dbterd/adapters/targets/mermaid/mermaid_test_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,28 @@ def run(manifest, catalog, **kwargs):


def replace_column_name(column_name: str) -> str:
"""Replace column names containing special characters.
To prevent mermaid from not being able to render column names that may contain special characters.
Args:
column_name (str): column name
Returns:
str: Column name with special characters substituted
"""
return column_name.replace(" ", "-").replace(".", "__")


def match_complex_column_type(column_type: str) -> Optional[str]:
"""Returns the root type from nested complex types.
As an example, if the input is `Struct<field1 string, field2 string>`, return `Struct`.
Args:
column_type (str): column type
Returns:
Optional[str]: Returns the root type if the input type is a nested complex type, otherwise returns `None` for primitive types
"""
pattern = r"(\w+)<(\w+\s+\w+(\s*,\s*\w+\s+\w+)*)>"
match = re.match(pattern, column_type)
if match:
Expand All @@ -31,6 +49,15 @@ def match_complex_column_type(column_type: str) -> Optional[str]:


def replace_column_type(column_type: str) -> str:
"""If the type string contains special characters that cannot be drawn by mermaid, replace them with strings that can be drawn.
If the type string contains a nested complex type, omit it to make it easier to read.
Args:
column_type (str): column type
Returns:
str: Type of column with special characters are substituted or omitted
"""
# Some specific DWHs may have types that cannot be drawn in mermaid, such as `Struct<first_name string, last_name string>`.
# These types may be nested and can be very long, so omit them
complex_column_type = match_complex_column_type(column_type)
Expand Down

0 comments on commit e456855

Please sign in to comment.