dbt-labs / dbt-codegen

Macros that generate dbt code
https://hub.getdbt.com/dbt-labs/codegen/latest/
Apache License 2.0
464 stars 102 forks source link

Increase readability of `generate_model_import_ctes.sql` by using `VERBOSE` mode for `re` #91

Closed b-per closed 1 year ago

b-per commented 1 year ago

Describe the feature

Modify generate_model_import_ctes.sql to use the verbose mode (adding x to the i flag) for re, allowing to split regexes between different lines and providing the ability to add comments to those.

Additional context

Here is an example of regex without verbose (all in 1 line):

{% set re = modules.re %}
{% set my_string = <my_sql_code> %}
{% set re_pattern = '(?i)(from|join)\s+({{\s*ref\s*\(\s*[\'\"]?)([^)\'\"]+)([\'\"]?\s*)(\)\s*}})' 
%}

{% set is_match = re.findall(re_pattern, my_string) %}

and with verbose:

{% set re = modules.re %}
{% set my_string = <my_sql_code> %}
{% set re_pattern = "(?ix)

    # first matching group
    (from|join)\s+       # from or join followed by at least a sep character

    # second matching group
    (
    {{                   # opening {{
    \s*ref\s*\(          # the word ref followed by a (
    \s*[\'\"]?
    )

    ([^)\'\"]+)([\'\"]?\s*)(
    \)\s*                # closing ) for ref
    }}                   # closing {{
    )" 
%}

{% set is_match = re.findall(re_pattern, my_string) %}

Who will this benefit?

People wanting to understand/enhance regexes to find tables in models.

Are you interested in contributing this feature?

Why not but keen to let someone else look at it

graciegoheen commented 1 year ago

meme_for_benoit_2