Skip to content

Commit

Permalink
#23 #26 Improve error handling for relation macros (#27)
Browse files Browse the repository at this point in the history
Restructure relation macros to better handle errors, improving consistency of getting the relation types.

Description of the issue can be found in #23 and #26

Test plan:
The test cases are available in GH Issue #23 and Issue #26
  • Loading branch information
tovganesh authored Apr 29, 2022
1 parent 215263e commit 7059bac
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions dbt/include/impala/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -196,48 +196,49 @@

{% macro is_relation_present(relation) -%}
{% set result_set = run_query('show tables in ' ~ relation.schema ~ ' like "' ~ relation.identifier ~ '"') %}
{%- if result_set.rows -%}
{% do return(true) %}
{% else %}
{% do return(false) %}
{% endif %}

{% if execute %}
{%- for rs in result_set -%}
{% do return(true) %}
{%- endfor -%}
{%- endif -%}

{% do return(false) %}
{% endmacro %}

{% macro get_relation_type(relation) -%}
{% set rel_type = 'table' %}
{% set relation_exists = is_relation_present(relation) %}

{%- if not relation_exists -%}
{% do return(rel_type) %}
{%- endif -%}

{% set result_set = run_query('describe extended ' ~ relation) %}
{%- if relation_exists -%}
{% set result_set = run_query('describe extended ' ~ relation) %}

{% if execute %}
{%- for rs in result_set -%}
{%- if rs[0].startswith('Table Type') -%}
{%- if rs[1].startswith('VIRTUAL_VIEW') -%}
{% set rel_type = 'view' %}
{% do return(rel_type) %}
{%- elif rs[1].startswith('MANAGED_TABLE') -%}
{% set rel_type = 'table' %}
{% do return(rel_type) %}
{%- elif rs[1].startswith('EXTERNAL_TABLE') -%}
{% set rel_type = 'table' %}
{% do return(rel_type) %}
{% if execute %}
{%- for rs in result_set -%}
{%- if rs[0].startswith('Table Type') -%}
{%- if rs[1].startswith('VIRTUAL_VIEW') -%}
{% set rel_type = 'view' %}
{% do return(rel_type) %}
{%- elif rs[1].startswith('MANAGED_TABLE') -%}
{% set rel_type = 'table' %}
{% do return(rel_type) %}
{%- elif rs[1].startswith('EXTERNAL_TABLE') -%}
{% set rel_type = 'table' %}
{% do return(rel_type) %}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{% endif %}

{%- endfor -%}
{%- endif -%}
{%- endif -%}
{% do return(rel_type) %}
{% endmacro %}

{% macro impala__rename_relation(from_relation, to_relation) -%}
{% set from_rel_type = get_relation_type(from_relation) %}

{% call statement('drop_relation') %}
drop {{ to_relation.type }} if exists {{ to_relation }}
drop {{ from_rel_type }} if exists {{ to_relation }}
{% endcall %}
{% call statement('rename_relation') -%}
{% if not from_rel_type %}
Expand Down

0 comments on commit 7059bac

Please sign in to comment.