Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
37 changes: 37 additions & 0 deletions dbt/adapters/oracle/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from dbt.adapters.oracle.relation import OracleRelation
from dbt.contracts.graph.manifest import Manifest

from dbt.exceptions import raise_compiler_error
from dbt.utils import filter_null_values


import agate
Expand Down Expand Up @@ -117,6 +119,21 @@ def verify_database(self, database):
# return an empty string on success so macros can call this
return ''

def _make_match_kwargs(self, database, schema, identifier):
quoting = self.config.quoting
if identifier is not None and quoting["identifier"] is False:
identifier = identifier.upper()

if schema is not None and quoting["schema"] is False:
schema = schema.upper()

if database is not None and quoting["database"] is False:
database = database.upper()

return filter_null_values(
{"identifier": identifier, "schema": schema, "database": database}
)

def get_rows_different_sql(
self,
relation_a: OracleRelation,
Expand Down Expand Up @@ -212,3 +229,23 @@ def list_relations_without_caching(
type=_type
))
return relations

@available
def quote_seed_column(
self, column: str, quote_config: Optional[bool]
) -> str:
quote_columns: bool = False
if isinstance(quote_config, bool):
quote_columns = quote_config
elif quote_config is None:
pass
else:
raise_compiler_error(
f'The seed configuration value of "quote_columns" has an '
f'invalid type {type(quote_config)}'
)

if quote_columns:
return self.quote(column)
else:
return column
48 changes: 26 additions & 22 deletions dbt/include/oracle/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
{{ return(load_result('get_columns_in_query').table.columns | map(attribute='name') | list) }}
{% endmacro %}

{% macro oracle__create_schema(database_name, schema_name) -%}

{% macro oracle__create_schema(relation, schema_name) -%}
{% if relation.database -%}
{{ adapter.verify_database(relation.database) }}
{%- endif -%}
{%- call statement('drop_schema') -%}
{%- call statement('create_schema') -%}
-- Noop for not breaking tests, oracle
-- schemas are actualy users, we can't
-- create it here
Expand Down Expand Up @@ -121,7 +122,7 @@
{%- set sql_header = config.get('sql_header', none) -%}

{{ sql_header if sql_header is not none }}
create view {{ relation.quote(schema=False, identifier=False) }} as
create or replace view {{ relation.include(False, True, True).quote(schema=False, identifier=False) }} as
{{ sql }}

{% endmacro %}
Expand Down Expand Up @@ -172,8 +173,8 @@
from sys.all_tab_columns
)
select
lower(column_name) as "name",
lower(data_type) as "type",
column_name as "name",
data_type as "type",
char_length as "character_maximum_length",
numeric_precision as "numeric_precision",
numeric_scale as "numeric_scale"
Expand Down Expand Up @@ -204,7 +205,7 @@
{% macro oracle__alter_relation_comment(relation, comment) %}
{% set escaped_comment = oracle_escape_comment(comment) %}
{# "comment on table" even for views #}
comment on table {{ relation.quote(schema=False, identifier=False) }} is {{ escaped_comment }}
comment on table {{ relation.include(False, True, True).quote(schema=False, identifier=False) }} is {{ escaped_comment }}
{% endmacro %}

{% macro oracle__persist_docs(relation, model, for_relation, for_columns) -%}
Expand All @@ -217,7 +218,7 @@
{% set comment = column_dict[column_name]['description'] %}
{% set escaped_comment = oracle_escape_comment(comment) %}
{% call statement('alter _column comment', fetch_result=False) -%}
comment on column {{ relation.quote(schema=False, identifier=False) }}.{{ column_name }} is {{ escaped_comment }}
comment on column {{ relation.include(False, True, True).quote(schema=False, identifier=False) }}.{{ column_name }} is {{ escaped_comment }}
{%- endcall %}
{% endfor %}
{% endif %}
Expand All @@ -233,16 +234,16 @@
{%- set tmp_column = column_name + "__dbt_alter" -%}

{% call statement('alter_column_type 1', fetch_result=False) %}
alter table {{ relation.quote(schema=False, identifier=False) }} add column {{ adapter.quote(tmp_column) }} {{ new_column_type }}
alter table {{ relation.include(False, True, True).quote(schema=False, identifier=False) }} add column {{ tmp_column }} {{ new_column_type }}
{% endcall %}
{% call statement('alter_column_type 2', fetch_result=False) %}
update {{ relation.quote(schema=False, identifier=False) }} set {{ adapter.quote(tmp_column) }} = {{ adapter.quote(column_name) }}
update {{ relation.include(False, True, True).quote(schema=False, identifier=False) }} set {{ tmp_column }} = {{ column_name }}
{% endcall %}
{% call statement('alter_column_type 3', fetch_result=False) %}
alter table {{ relation.quote(schema=False, identifier=False) }} drop column {{ adapter.quote(column_name) }} cascade
alter table {{ relation.include(False, True, True).quote(schema=False, identifier=False) }} drop column {{ column_name }} cascade
{% endcall %}
{% call statement('alter_column_type 4', fetch_result=False) %}
rename column {{ relation.quote(schema=False, identifier=False) }}.{{ adapter.quote(tmp_column) }} to {{ adapter.quote(column_name) }}
alter table {{ relation.include(False, True, True).quote(schema=False, identifier=False) }} rename column {{ tmp_column }} to {{ column_name }}
{% endcall %}

{% endmacro %}
Expand All @@ -256,7 +257,7 @@
pragma EXCEPTION_INIT(attempted_ddl_on_in_use_GTT, -14452);
BEGIN
SAVEPOINT start_transaction;
EXECUTE IMMEDIATE 'DROP {{ relation.type }} {{ relation.quote(schema=False, identifier=False) }} cascade constraint';
EXECUTE IMMEDIATE 'DROP {{ relation.type }} {{ relation.include(False, True, True).quote(schema=False, identifier=False) }} cascade constraint';
COMMIT;
EXCEPTION
WHEN attempted_ddl_on_in_use_GTT THEN
Expand All @@ -268,14 +269,17 @@
{% endmacro %}

{% macro oracle__truncate_relation(relation) -%}
{% call statement('truncate_relation') -%}
truncate table {{ relation.quote(schema=False, identifier=False) }}
{%- endcall %}
{#-- To avoid `ORA-01702: a view is not appropriate here` we check that the relation to be truncated is a table #}
{% if relation.is_table %}
{% call statement('truncate_relation') -%}
truncate table {{ relation.include(False, True, True).quote(schema=False, identifier=False) }}
{%- endcall %}
{% endif %}
{% endmacro %}

{% macro oracle__rename_relation(from_relation, to_relation) -%}
{% call statement('rename_relation') -%}
rename {{ from_relation.include(False, False, True).quote(schema=False, identifier=False) }} to {{ to_relation.include(False, False, True).quote(schema=False, identifier=False) }}
ALTER {{ from_relation.type }} {{ from_relation.include(False, True, True).quote(schema=False, identifier=False) }} rename to {{ to_relation.include(False, False, True).quote(schema=False, identifier=False) }}
{%- endcall %}
{% endmacro %}

Expand All @@ -291,7 +295,7 @@
{{ adapter.verify_database(database) }}
{%- endif -%}
{% call statement('list_schemas', fetch_result=True, auto_begin=False) -%}
select lower(username) as "name"
select username as "name"
from sys.all_users
order by username
{% endcall %}
Expand Down Expand Up @@ -329,9 +333,9 @@
'VIEW'
from sys.all_views
)
select lower(table_catalog) as "database_name"
,lower(table_name) as "name"
,lower(table_schema) as "schema_name"
select table_catalog as "database_name"
,table_name as "name"
,table_schema as "schema_name"
,case table_type
when 'BASE TABLE' then 'table'
when 'VIEW' then 'view'
Expand All @@ -352,7 +356,7 @@
{% set dtstring = dt.strftime("%H%M%S") %}
{% set tmp_identifier = 'o$pt_' ~ base_relation.identifier ~ dtstring %}
{% set tmp_relation = base_relation.incorporate(
path={"identifier": tmp_identifier}) -%}
path={"identifier": tmp_identifier, "schema": None}) -%}

{% do return(tmp_relation) %}
{% endmacro %}
Expand All @@ -361,4 +365,4 @@
{% set results = run_query("select SYS_CONTEXT('userenv', 'DB_NAME') FROM DUAL") %}
{% set db_name = results.columns[0].values()[0] %}
{{ return(db_name) }}
{% endmacro %}
{% endmacro %}
14 changes: 7 additions & 7 deletions dbt/include/oracle/macros/catalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@
from sys.all_views
)
select
lower(tables.table_catalog) as "table_database",
lower(tables.table_schema) as "table_schema",
lower(tables.table_name) as "table_name",
lower(tables.table_type) as "table_type",
tables.table_catalog as "table_database",
tables.table_schema as "table_schema",
tables.table_name as "table_name",
tables.table_type as "table_type",
all_tab_comments.comments as "table_comment",
lower(columns.column_name) as "column_name",
columns.column_name as "column_name",
ordinal_position as "column_index",
lower(case
case
when data_type like '%CHAR%'
then data_type || '(' || cast(char_length as varchar(10)) || ')'
else data_type
end) as "column_type",
end as "column_type",
all_col_comments.comments as "column_comment",
tables.table_schema as "table_owner"
from tables
Expand Down
20 changes: 9 additions & 11 deletions dbt/include/oracle/macros/materializations/incremental/helpers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,21 @@
on (temp.{{ unique_key }} = target.{{ unique_key }})
when matched then
update set
{% for col in dest_columns if col.name != unique_key %}
target.{{ col.name }} = temp.{{ col.name }}
{% if not loop.last %}, {% endif %}
{% endfor %}
{% for col in dest_columns if col.name.upper() != unique_key.upper() -%}
target.{{ col.name }} = temp.{{ col.name }}{% if not loop.last %}, {% endif %}
{% endfor -%}
when not matched then
insert( {{ dest_cols_csv }} )
insert({{ dest_cols_csv }})
values(
{% for col in dest_columns %}
temp.{{ col.name }}
{% if not loop.last %}, {% endif %}
{% endfor %}
{% for col in dest_columns -%}
temp.{{ col.name }}{% if not loop.last %}, {% endif %}
{% endfor -%}
)
{%- else %}
{%- else -%}
insert into {{ target_relation }} ({{ dest_cols_csv }})
(
select {{ dest_cols_csv }}
from {{ tmp_relation }}
)
{% endif %}
{%- endif -%}
{%- endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@
{% set backup_identifier = existing_relation.identifier ~ "__dbt_backup" %}
{% set backup_relation = existing_relation.incorporate(path={"identifier": backup_identifier}) %}
{% do adapter.drop_relation(backup_relation) %}

{% do adapter.rename_relation(target_relation, backup_relation) %}
{% if existing_relation.is_view %}
{% do adapter.drop_relation(existing_relation) %}
{% else %}
{% do adapter.rename_relation(existing_relation, backup_relation) %}
{% endif %}
{% set build_sql = create_table_as(False, target_relation, sql) %}
{% do to_drop.append(backup_relation) %}
{% else %}
Expand All @@ -62,6 +65,7 @@
{% do adapter.commit() %}

{% for rel in to_drop %}
{% do adapter.truncate_relation(rel) %}
{% do adapter.drop_relation(rel) %}
{% endfor %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
{{ adapter.dispatch('post_snapshot')(staging_relation) }}
{% endmacro %}

{% macro default__post_snapshot(staging_relation) %}
{# no-op #}
{% macro oracle__post_snapshot(staging_relation) %}
{% do adapter.truncate_relation(staging_relation) %}
{% do adapter.drop_relation(staging_relation) %}
{% endmacro %}


Expand Down
6 changes: 5 additions & 1 deletion dbt/include/oracle/macros/materializations/table/table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@

-- cleanup
{% if old_relation is not none %}
{{ adapter.rename_relation(old_relation, backup_relation) }}
{% if old_relation.is_view %}
{% do adapter.drop_relation(old_relation) %}
{% else %}
{% do adapter.rename_relation(old_relation, backup_relation) %}
{% endif %}
{% endif %}

{{ adapter.rename_relation(intermediate_relation, target_relation) }}
Expand Down
25 changes: 6 additions & 19 deletions dbt/include/oracle/macros/materializations/view/view.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,11 @@
{%- materialization view, adapter='oracle' -%}

{%- set identifier = model['alias'] -%}
{%- set tmp_identifier = model['name'] + '__dbt_tmp' -%}
{%- set backup_identifier = model['name'] + '__dbt_backup' -%}

{%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%}
{%- set target_relation = api.Relation.create(identifier=identifier, schema=schema, database=database,
type='view') -%}
{%- set intermediate_relation = api.Relation.create(identifier=tmp_identifier,
schema=schema, database=database, type='view') -%}
-- the intermediate_relation should not already exist in the database; get_relation
-- will return None in that case. Otherwise, we get a relation that we can drop
-- later, before we try to use this name for the current operation
{%- set preexisting_intermediate_relation = adapter.get_relation(identifier=tmp_identifier,
schema=schema,
database=database) -%}
/*
This relation (probably) doesn't exist yet. If it does exist, it's a leftover from
a previous run, and we're going to try to drop it immediately. At the end of this
Expand All @@ -55,25 +46,21 @@

{{ run_hooks(pre_hooks, inside_transaction=False) }}

-- drop the temp relations if they exist already in the database
{{ drop_relation_if_exists(preexisting_intermediate_relation) }}
{{ drop_relation_if_exists(preexisting_backup_relation) }}

-- `BEGIN` happens here:
{{ run_hooks(pre_hooks, inside_transaction=True) }}

-- if old_relation was a table
{% if old_relation is not none and old_relation.type == 'table' %}
{{ adapter.rename_relation(old_relation, backup_relation) }}
{% endif %}

-- build model
{% call statement('main') -%}
{{ create_view_as(intermediate_relation, sql) }}
{{ create_view_as(target_relation, sql) }}
{%- endcall %}

-- cleanup
-- move the existing view out of the way
{% if old_relation is not none %}
{{ adapter.rename_relation(old_relation, backup_relation) }}
{% endif %}
{{ adapter.rename_relation(intermediate_relation, target_relation) }}

{% do persist_docs(target_relation, model) %}

{{ run_hooks(post_hooks, inside_transaction=True) }}
Expand Down
5 changes: 5 additions & 0 deletions dbt_adbs_test_project/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ version: 1.0
profile: dbt_test
analysis-paths: ['analysis']
test-paths: ['test']
clean-targets: # directories to be removed by `dbt clean`
- "target"
- "dbt_packages"
- "dbt_modules"
- "logs"

quoting:
database: false
Expand Down
25 changes: 25 additions & 0 deletions dbt_adbs_test_project/macros/generate_schema_name.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{#
Copyright (c) 2022, Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#}
{% macro generate_schema_name(custom_schema_name, node) -%}

{%- set default_schema = target.schema -%}
{%- if custom_schema_name is none -%}
{{ default_schema }}
{%- else -%}
{{ custom_schema_name | trim }}
{%- endif -%}

{%- endmacro %}
Loading