-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bugfix to include size, scale and precision in column.datatype
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
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{%- macro oracle__type_string() -%} | ||
VARCHAR2(4000) | ||
{%- endmacro -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}} |