Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify get_tables_by_pattern_sql #13

Merged
merged 2 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docker/dbt/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ RUN apt-get update && \
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/
RUN pip3 install --upgrade pip
RUN mkdir ${DBT_DIR}
# TODO: change with release version
RUN pip install git+https://github.com/starburstdata/dbt-trino.git
RUN pip install dbt-trino==1.3

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RUN pip install dbt-trino~=1.3.*

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, too fast. This is indeed not working as expected.

It seems however that RUN pip install dbt-trino==1.3.* also works.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~= will always install the latest version compatible with the pattern.
== won't update to the newest version if the older version which matches the pattern is already installed.

So I think ~= is slightly better here.

ADD . ${DBT_DIR}
ADD docker/dbt/profiles.yml /root/.dbt/profiles.yml

Expand Down
59 changes: 8 additions & 51 deletions macros/dbt_utils/sql/get_tables_by_pattern_sql.sql
Original file line number Diff line number Diff line change
@@ -1,53 +1,10 @@

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

{% if '%' in schema_pattern %}
{% set schemata=trino_utils._trino__get_matching_schemata(schema_pattern, database) %}
{% else %}
{% set schemata=[schema_pattern] %}
{% endif %}

{% set sql %}
{% for schema in schemata %}
select distinct
table_schema,
table_name,
{{ dbt_utils.get_table_types_sql() }}

from {{ adapter.quote(database) }}.INFORMATION_SCHEMA.TABLES
where lower(table_name) like lower ('{{ table_pattern }}')
and lower(table_name) not like lower ('{{ exclude }}')
and table_schema = '{{ schema }}'

{% if not loop.last %} union all {% endif %}

{% endfor %}
{% endset %}

{{ return(sql) }}

{% endmacro %}


{% macro _trino__get_matching_schemata(schema_pattern, database) %}
{% if execute %}

{% set sql %}
select schema_name from {{ adapter.quote(database) }}.INFORMATION_SCHEMA.SCHEMATA
where lower(schema_name) like lower('{{ schema_pattern }}')
{% endset %}

{% set results=run_query(sql) %}

{% set schemata=results.columns['schema_name'].values() %}

{{ return(schemata) }}

{% else %}

{{ return([]) }}

{% endif %}


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 lower(table_schema) like lower('{{ schema_pattern }}')
and lower(table_name) like lower('{{ table_pattern }}')
and lower(table_name) not like lower('{{ exclude }}')
{% endmacro %}