Skip to content

Commit

Permalink
feat: add columns to seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
Vixtir committed Sep 20, 2023
1 parent 44b17db commit 20c36cb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
10 changes: 9 additions & 1 deletion odd_dbt/domain/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from odd_models import DataEntity, DataTransformer, DataSet, DataInput
from odd_models import DataEntity, DataTransformer, DataSet, DataInput, DataSetField
import abc


Expand Down Expand Up @@ -37,6 +37,11 @@ def add_output(self, oddrn: str) -> None:
self.data_transformer.outputs.append(oddrn)


class ColumnEntity(DataSetField):
def __init__(self, **data: dict):
super().__init__(**data)


class SeedEntity(NodeEntity):
def __init__(self, **data: dict):
super().__init__(**data)
Expand All @@ -52,3 +57,6 @@ def add_input(self, oddrn: str) -> None:
def add_output(self, oddrn: str) -> None:
if oddrn not in self.data_input.outputs:
self.data_input.outputs.append(oddrn)

def add_column(self, column: ColumnEntity) -> None:
self.dataset.field_list.append(column)
30 changes: 24 additions & 6 deletions odd_dbt/mapper/lineage.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import traceback
from typing import Optional, Union

from dbt.contracts.graph.nodes import ModelNode, SeedNode
from odd_dbt.mapper.types import DBT_TO_ODD
from dbt.contracts.graph.nodes import ModelNode, SeedNode, ColumnInfo
from odd_models.models import (
DataEntityList,
DataEntityType,
)
from oddrn_generator import DbtGenerator

from odd_models import DataSetFieldType
from odd_dbt import logger
from odd_dbt.domain.context import DbtContext
from odd_dbt.domain.model import ModelEntity, SeedEntity, NodeEntity
from odd_dbt.domain.model import ModelEntity, SeedEntity, NodeEntity, ColumnEntity
from odd_dbt.mapper.generator import create_generator
from odd_dbt.mapper.metadata import get_model_metadata

Expand Down Expand Up @@ -64,14 +64,32 @@ def map_node(self, node: Union[ModelNode, SeedNode]) -> Optional[NodeEntity]:
return self.map_seed(node)

def map_seed(self, node: SeedNode) -> SeedEntity:
return SeedEntity(
self._generator.set_oddrn_paths(seeds=node.unique_id)

seed_entity = SeedEntity(
name=node.unique_id,
oddrn=self._generator.get_oddrn_by_path("seeds", node.unique_id),
oddrn=self._generator.get_oddrn_by_path("seeds"),
owner=None,
type=DataEntityType.FILE,
metadata=[get_model_metadata(node)],
)

for column in node.columns.values():
seed_entity.add_column(self.map_column(column, generator=self._generator))

return seed_entity

def map_column(self, column: ColumnInfo, generator: DbtGenerator) -> ColumnEntity:
oddrn = generator.get_oddrn_by_path("seeds") + f"/columns/{column.name}"
return ColumnEntity(
name=column.name,
oddrn=oddrn,
type=DataSetFieldType(
type=DBT_TO_ODD['UNKNOWN'],
is_nullable=True
),
)

def map_model(self, node: ModelNode) -> ModelEntity:
self._generator.set_oddrn_paths(models=node.unique_id)
model_entity = ModelEntity(
Expand Down
6 changes: 5 additions & 1 deletion odd_dbt/mapper/metadata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dbt.contracts.graph.nodes import ModelNode, TestNode
from dbt.contracts.graph.nodes import ModelNode, TestNode, ColumnInfo
from odd_models import MetadataExtension


Expand Down Expand Up @@ -29,3 +29,7 @@ def get_metadata(test_node: TestNode) -> MetadataExtension:
def get_model_metadata(model_node: ModelNode) -> MetadataExtension:
schema_url = "https://raw.githubusercontent.com/opendatadiscovery/opendatadiscovery-specification/main/specification/extensions/dbt.json#/definitions/DataTransformer"
return MetadataExtension(schema_url=schema_url, metadata=model_node.to_dict())

def get_column_metadata(column_info: ColumnInfo) -> MetadataExtension:
schema_url = "https://raw.githubusercontent.com/opendatadiscovery/opendatadiscovery-specification/main/specification/extensions/dbt.json#/definitions/DataSetField"
return MetadataExtension(schema_url=schema_url, metadata=column_info.to_dict())
30 changes: 30 additions & 0 deletions odd_dbt/mapper/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from odd_models.models import Type

DBT_TO_ODD: dict[str, Type] = {
"INT": Type.TYPE_INTEGER,
"INTEGER": Type.TYPE_INTEGER,
"SMALLINT": Type.TYPE_INTEGER,
"BIGINT": Type.TYPE_INTEGER,
"NUMBER": Type.TYPE_NUMBER,
"DECIMAL": Type.TYPE_NUMBER,
"NUMERIC": Type.TYPE_NUMBER,
"DOUBLE": Type.TYPE_NUMBER,
"REAL": Type.TYPE_NUMBER,
"FLOAT": Type.TYPE_NUMBER,
"FIXED": Type.TYPE_NUMBER,
"STRING": Type.TYPE_STRING,
"TEXT": Type.TYPE_STRING,
"VARCHAR": Type.TYPE_STRING,
"CHAR": Type.TYPE_CHAR,
"CHARACTER": Type.TYPE_CHAR,
"BOOLEAN": Type.TYPE_BOOLEAN,
"DATETIME": Type.TYPE_DATETIME,
"DATE": Type.TYPE_DATETIME,
"TIMESTAMP": Type.TYPE_DATETIME,
"TIMESTAMP_LTZ": Type.TYPE_DATETIME,
"TIMESTAMP_NTZ": Type.TYPE_DATETIME,
"TIMESTAMP_TZ": Type.TYPE_DATETIME,
"ARRAY": Type.TYPE_LIST,
"VARIANT": Type.TYPE_LIST,
"UNKNOWN": Type.TYPE_UNKNOWN,
}

0 comments on commit 20c36cb

Please sign in to comment.