Skip to content

Commit

Permalink
Bugfix to include size, scale and precision in column.datatype
Browse files Browse the repository at this point in the history
1. Fixes Bug #29
2. Introduced new class OracleColumn which can be used to define logic specific to Oracle table column
3. Added an example model in dbt_adbs_test_project to reproduce the error and validate the fix
4. Added a project specific macro in dbt_adbs_test_project to translate the string datatype
  • Loading branch information
aosingh committed Jul 8, 2022
1 parent 44d4bfb commit 0ee5978
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
52 changes: 52 additions & 0 deletions dbt/adapters/oracle/column.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from dataclasses import dataclass
from typing import Dict, ClassVar


from dbt.adapters.base.column import Column


@dataclass
class OracleColumn(Column):
# https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/Data-Types.html#GUID-A3C0D836-BADB-44E5-A5D4-265BA5968483

TYPE_LABELS: ClassVar[Dict[str, str]] = {
"STRING": "VARCHAR2(4000)",
"TIMESTAMP": "TIMESTAMP",
"FLOAT": "FLOAT",
"INTEGER": "INTEGER",
}

STRING_DATATYPES = {'char', 'nchar', 'varchar', 'varchar2', 'nvarchar2'}
NUMBER_DATATYPES = {'number', 'float'}

@property
def data_type(self) -> str:
if self.is_string():
return self.oracle_string_type(self.dtype, self.string_size())
elif self.is_numeric():
return self.numeric_type(self.dtype, self.numeric_precision, self.numeric_scale)
else:
return self.dtype

@classmethod
def oracle_string_type(cls, dtype: str, size: int = None):
"""
- CHAR(SIZE)
- VARCHAR2(SIZE)
- NCHAR(SIZE) or NCHAR
- NVARCHAR2(SIZE)
"""
if size is None:
return dtype
else:
return "{}({})".format(dtype, size)

def is_numeric(self) -> bool:
if self.dtype.lower() in self.NUMBER_DATATYPES:
return True
return super().is_numeric()

def is_string(self) -> bool:
if self.dtype.lower() in self.STRING_DATATYPES:
return True
return super().is_string()
2 changes: 2 additions & 0 deletions dbt/adapters/oracle/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from dbt.adapters.sql import SQLAdapter
from dbt.adapters.base.meta import available
from dbt.adapters.oracle import OracleAdapterConnectionManager
from dbt.adapters.oracle.column import OracleColumn
from dbt.adapters.oracle.relation import OracleRelation
from dbt.contracts.graph.manifest import Manifest

Expand Down Expand Up @@ -69,6 +70,7 @@
class OracleAdapter(SQLAdapter):
ConnectionManager = OracleAdapterConnectionManager
Relation = OracleRelation
Column = OracleColumn

def debug_query(self) -> None:
self.execute("select 1 as id from dual")
Expand Down
3 changes: 3 additions & 0 deletions dbt_adbs_test_project/macros/datatypes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{%- macro oracle__type_string() -%}
VARCHAR2(4000)
{%- endmacro -%}
7 changes: 7 additions & 0 deletions dbt_adbs_test_project/models/union_customer_sales.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ config(materialized='table')}}
{{
dbt_utils.union_relations(
relations=[source('sh_database', 'sales'),
source('sh_database', 'customers')],
source_column_name='dbt_source_relation')
}}

0 comments on commit 0ee5978

Please sign in to comment.