Skip to content

Enhance get_relations_by_pattern macro for Snowflake users to allow escape characters in pattern params #1033

@lwachs

Description

@lwachs

Describe the feature

create a Snowflake-specifc override in macros/sql/get_tables_by_pattern_sql.sql to allow a backslash to be used to escape wildcards if used in the pattern parameters.

for example, my desired pattern includes 2 consecutive underscores, but without being able to escape the underscore, Snowflake treats it as a wildcard character. Escaping with a backslash is unsuccessful, since Snowflake does not have a default escape character (except, oddly, when using it as an escape character via the ESCAPE parameter, as in the sample code provided).

Describe alternatives you've considered

for now, i have used a pattern that will work without requiring an escape character, but it relies on at least one of the version numbers being 0, so it's brittle and i would prefer to not have to rely on it always being true

Additional context

this request would pertain specifically to the Snowflake adapter

Who will this benefit?

any Snowflake user who wants to leverage more than a simple string in the pattern matching for the get_relations_by_pattern macro

Are you interested in contributing this feature?

i would love to! here's the sample code that worked when i added it to my organization's copy of the file in dbt_utils\macros\sql\ (ok, technically i only tested the exclude change since that's the one i needed and i only added the schema and table pattern ones as i was writing up this issue because they could benefit as well, but i don't see any reason why the below wouldn't work):

{% macro snowflake__get_tables_by_pattern_sql(schema_pattern, table_pattern, exclude='', database=target.database) %}

        select distinct
            table_schema as {{ adapter.quote('table_schema') }},
            table_name as {{ adapter.quote('table_name') }},
            {{ dbt_utils.get_table_types_sql() }}
        from {{ database }}.information_schema.tables
        where table_schema ilike '{{ schema_pattern }}' {% if '\\' in schema_pattern %}ESCAPE '\\'{% endif %}
        and table_name ilike '{{ table_pattern }}' {% if '\\' in table_pattern %}ESCAPE '\\'{% endif %}
        and table_name not ilike '{{ exclude }}' {% if '\\' in exclude %}ESCAPE '\\'{% endif %}

{% endmacro %}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions