From 7059bac96c21534db96c3f3f4782113559b440bb Mon Sep 17 00:00:00 2001 From: "V. Ganesh" Date: Fri, 29 Apr 2022 16:52:36 +0530 Subject: [PATCH] #23 #26 Improve error handling for relation macros (#27) 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 --- dbt/include/impala/macros/adapters.sql | 57 +++++++++++++------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/dbt/include/impala/macros/adapters.sql b/dbt/include/impala/macros/adapters.sql index 52178c8..8fe2539 100644 --- a/dbt/include/impala/macros/adapters.sql +++ b/dbt/include/impala/macros/adapters.sql @@ -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 %}