@@ -7,10 +7,6 @@ dbt docs: https://docs.getdbt.com/docs/contributing/building-a-new-adapter
77 {{ return(' ?' ) }}
88{% endmacro %}
99
10- {% macro iris__current_timestamp() - %}
11- current_timestamp
12- {%- endmacro %}
13-
1410{% macro iris__list_schemas(database) - %}
1511 {% call statement(' list_schemas' , fetch_result= True, auto_begin= False) - %}
1612 select schema_name
@@ -95,7 +91,7 @@ dbt docs: https://docs.getdbt.com/docs/contributing/building-a-new-adapter
9591 select
9692 column_name,
9793 data_type,
98- character_maximum_length,
94+ NULLIF(CAST(character_maximum_length as INT ), 0 ) character_maximum_length,
9995 numeric_precision,
10096 numeric_scale
10197 from information_schema .columns
@@ -108,18 +104,6 @@ dbt docs: https://docs.getdbt.com/docs/contributing/building-a-new-adapter
108104 {{ return(sql_convert_columns_in_relation(table)) }}
109105{% endmacro %}
110106
111-
112- {% macro iris__create_view_as(relation, sql) - %}
113- {%- set sql_header = config .get (' sql_header' , none) - %}
114-
115- {{ sql_header if sql_header is not none }}
116- /* create_view_as */
117- {# create or replace view {{ relation }} #}
118- create table {{ relation }}
119- as {{ sql }}
120-
121- {%- endmacro %}
122-
123107{% macro iris__create_table_as(temporary, relation, compiled_code, language= ' sql' ) - %}
124108 {%- if language == ' sql' - %}
125109 {%- set sql_header = config .get (' sql_header' , none) - %}
@@ -130,11 +114,7 @@ dbt docs: https://docs.getdbt.com/docs/contributing/building-a-new-adapter
130114 {%- endif %}
131115 /* create_table_as */
132116 {{ sql_header if sql_header is not none }}
133- create
134- /* {% if temporary: -%}global temporary{%- endif %} */
135- table
136- {{ relation }}
137- as
117+ create {% if temporary: - %}global temporary{%- endif %} table {{ relation }} as
138118 {{ compiled_code }}
139119 {%- elif language == ' python' - %}
140120 {{ py_write_table(compiled_code= compiled_code, target_relation= relation, temporary= temporary) }}
@@ -144,11 +124,56 @@ dbt docs: https://docs.getdbt.com/docs/contributing/building-a-new-adapter
144124{%- endmacro %}
145125
146126{% macro iris__rename_relation(from_relation, to_relation) - %}
147- {% set target_name = adapter .quote_as_configured (to_relation .identifier , ' identifier' ) %}
148127 {% call statement(' drop_relation' ) %}
149- drop table if exists {{ to_relation }} cascade
128+ drop {{ to_relation .type }} if exists {{ to_relation }} cascade
129+ {% endcall %}
130+ {% if not from_relation .type %}
131+ {% do exceptions .raise_database_error (" Cannot rename a relation with a blank type: " ~ from_relation .identifier ) %}
132+ {% elif from_relation .type == ' table' %}
133+ {% set target_name = adapter .quote_as_configured (to_relation .identifier , ' identifier' ) %}
134+ {% do drop_related_view(from_relation) %}
135+ {%- set target_name = adapter .quote_as_configured (to_relation .identifier , ' identifier' ) %}
136+ {% call statement(' rename_relation' ) - %}
137+ alter {{ from_relation .type }} {{ from_relation }} rename {{ target_name }}
138+ {%- endcall %}
139+ {%- elif from_relation .type == ' view' - %}
140+ {% do adapter .dispatch (' rename_view' )(from_relation, to_relation) %}
141+ {% else - %}
142+ {% do exceptions .raise_database_error (" Unknown type '" ~ from_relation .type ~ " ' for relation: " ~ from_relation .identifier ) %}
143+ {% endif %}
144+
145+ {% endmacro %}
146+
147+ {% macro drop_related_view(relation) %}
148+
149+ {% set to_drop = get_related_views(relation) %}
150+
151+ {% if to_drop is not none and to_drop|length > 0 %}
152+ {% for view in to_drop %}
153+ {% set view_relation = api .Relation .create(
154+ identifier= view[' VIEW_NAME' ],
155+ schema= view[' VIEW_SCHEMA' ],
156+ database= relation .database ,
157+ type= ' view' ) %}
158+ {{ drop_relation_if_exists(view_relation) }}
159+ {% endfor %}
160+ {% endif %}
161+
162+ {% endmacro %}
163+
164+ {% macro get_related_views(relation) %}
165+ {% call statement(' list_tables' , fetch_result= True) %}
166+ SELECT VIEW_SCHEMA,VIEW_NAME
167+ FROM INFORMATION_SCHEMA .VIEW_TABLE_USAGE
168+ WHERE TABLE_SCHEMA= ' {{ relation.schema }}' AND TABLE_NAME= ' {{ relation.identifier }}'
150169 {% endcall %}
170+ {{ return(load_result(' list_tables' ).table) }}
171+ {% endmacro %}
172+
173+ {# {% macro iris__rename_relation(from_relation, to_relation) -%}
174+ {% set target_name = adapter .quote_as_configured (to_relation .identifier , ' identifier' ) %}
175+ {{ print(" !!!! rename_relation: " ~ from_relation .type ~ " :" ~ from_relation .identifier ~ " to " ~ to_relation .type ~ " :" ~ to_relation) }}
151176 {% call statement(' rename_relation' ) - %}
152- alter table {{ from_relation }} rename {{ target_name }}
177+ alter table {{ from_relation . render () }} rename {{ target_name }}
153178 {%- endcall %}
154- {% endmacro %}
179+ {% endmacro %} # }
0 commit comments