Skip to content

Commit

Permalink
unit tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-db committed Oct 5, 2023
1 parent c661981 commit fc8b3fb
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 58 deletions.
57 changes: 17 additions & 40 deletions dbt/adapters/databricks/relation_configs/materialized_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
from dbt.exceptions import DbtRuntimeError

from dbt.adapters.databricks.relation_configs.base import DatabricksRelationConfigBase
from dbt.adapters.databricks.relation_configs.partition import (
DatabricksPartitionConfig,
DatabricksPartitionConfigChange,
)
from dbt.adapters.databricks.relation_configs.schedule import DatabricksScheduleConfigChange

from dbt.adapters.databricks.utils import evaluate_bool

Expand Down Expand Up @@ -41,8 +46,8 @@ class DatabricksMaterializedViewConfig(DatabricksRelationConfigBase, RelationCon
database_name: str
query: str
backup: bool = True
partition: str # to be done
schedule: str
partition: Optional[str] = None # to be done
schedule: Optional[str] = None

@property
def path(self) -> str:
Expand All @@ -52,11 +57,9 @@ def path(self) -> str:
if part is not None
)

# can be filled out later
# @property
# def validation_rules(self) -> Set[RelationConfigValidationRule]:
# # sort and dist rules get run by default with the mixin
# return {}
@property
def validation_rules(self) -> Set[RelationConfigValidationRule]:
return {}

@classmethod
def from_dict(cls, config_dict) -> "DatabricksMaterializedViewConfig":
Expand Down Expand Up @@ -130,58 +133,32 @@ def parse_relation_results(cls, relation_results: RelationResults) -> dict:
"mv_name": materialized_view.get("table"),
"schema_name": materialized_view.get("schema"),
"database_name": materialized_view.get("database"),
"autorefresh": materialized_view.get("autorefresh"),
"schedule": materialized_view.get("schedule"),
"query": cls._parse_query(query.get("definition")),
}

# the default for materialized views differs from the default for diststyle in general
# only set it if we got a value
if materialized_view.get("diststyle"):
config_dict.update(
{"dist": DatabricksDistConfig.parse_relation_results(materialized_view)}
)

# TODO: this only shows the first column in the sort key
if materialized_view.get("sortkey1"):
config_dict.update(
{"sort": DatabricksSortConfig.parse_relation_results(materialized_view)}
)

return config_dict


# @dataclass(frozen=True, eq=True, unsafe_hash=True)
# class DatabricksAutoRefreshConfigChange(RelationConfigChange):
# context: Optional[bool] = None

# @property
# def requires_full_refresh(self) -> bool:
# return False


@dataclass
class DatabricksMaterializedViewConfigChangeset:
dist: Optional[DatabricksPartitionConfigChange] = None
autorefresh: Optional[DatabricksAutoRefreshConfigChange] = None
partition: Optional[DatabricksPartitionConfigChange] = None
schedule: Optional[DatabricksScheduleConfigChange] = None

@property
def requires_full_refresh(self) -> bool:
return any(
{
self.autorefresh.requires_full_refresh if self.autorefresh else False,
self.backup.requires_full_refresh if self.backup else False,
self.dist.requires_full_refresh if self.dist else False,
self.sort.requires_full_refresh if self.sort else False,
self.schedule.requires_full_refresh if self.schedule else False,
self.partition.requires_full_refresh if self.partition else False,
}
)

@property
def has_changes(self) -> bool:
return any(
{
self.backup if self.backup else False,
self.dist if self.dist else False,
self.sort if self.sort else False,
self.autorefresh if self.autorefresh else False,
self.schedule if self.schedule else False,
self.partition if self.partition else False,
}
)
27 changes: 10 additions & 17 deletions dbt/adapters/databricks/relation_configs/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
from dbt.dataclass_schema import StrEnum
from dbt.exceptions import DbtRuntimeError

from dbt.adapters.databrick.relation_configs.base import DatabrickRelationConfigBase


from dbt.adapters.databricks.relation_configs.base import DatabricksRelationConfigBase


@dataclass(frozen=True, eq=True, unsafe_hash=True)
class DatabricksPartitionConfig(DatabrickRelationConfigBase, RelationConfigValidationMixin):
class DatabricksPartitionConfig(DatabricksRelationConfigBase, RelationConfigValidationMixin):
"""
This config fallows the specs found here:
https://docs.databricks.com/en/sql/language-manual/sql-ref-partition.html#partitioned-by
Expand All @@ -40,20 +38,19 @@ def __post_init__(self):

@property
def validation_rules(self) -> Set[RelationConfigValidationRule]:

#TODO
# TODO

pass

@classmethod
def from_dict(cls, config_dict):
#TODO
# TODO

pass

@classmethod
def parse_model_node(cls, model_node: ModelNode) -> dict:
#TODO
# TODO
pass

@classmethod
Expand All @@ -76,18 +73,14 @@ def parse_relation_results(cls, relation_results_entry: agate.Row) -> dict:
Returns: a standard dictionary describing this `DatabrickSortConfig` instance
"""
#TODO
# TODO
pass


@dataclass(frozen=True, eq=True, unsafe_hash=True)
class DatabrickSortConfigChange(RelationConfigChange, RelationConfigValidationMixin):
class DatabricksPartitionConfigChange(RelationConfigChange):
context: Optional[bool] = None

@property
def requires_full_refresh(self) -> bool:
return True

@property
def validation_rules(self) -> Set[RelationConfigValidationRule]:
#TODO
pass
return False
83 changes: 83 additions & 0 deletions dbt/adapters/databricks/relation_configs/schedule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from dataclasses import dataclass
from typing import Optional, Set

import agate
from dbt.adapters.relation_configs import (
RelationConfigChange,
RelationConfigChangeAction,
RelationConfigValidationMixin,
RelationConfigValidationRule,
)
from dbt.contracts.graph.nodes import ModelNode
from dbt.adapters.databricks.relation_configs.base import DatabricksRelationConfigBase


@dataclass(frozen=True, eq=True, unsafe_hash=True)
class DatabricksScheduleConfig(DatabricksRelationConfigBase, RelationConfigValidationMixin):
"""
This config fallows the specs found here:
https://docs.databricks.com/en/sql/language-manual/sql-ref-partition.html#partitioned-by
The following parameters are configurable by dbt:
- partition_column: the column identifier to be sorted
- column_type: the type of the specified thing
"""

# sortkey: Optional[FrozenSet[str]] = None

def __post_init__(self):
# # maintains `frozen=True` while allowing for a variable default on `sort_type`
# if self.sortstyle is None and self.sortkey is None:
# object.__setattr__(self, "sortstyle", DatabrickSortStyle.default())
# elif self.sortstyle is None:
# object.__setattr__(self, "sortstyle", DatabrickSortStyle.default_with_columns())
super().__post_init__()

@property
def validation_rules(self) -> Set[RelationConfigValidationRule]:
# TODO

pass

@classmethod
def from_dict(cls, config_dict):
# TODO

pass

@classmethod
def parse_model_node(cls, model_node: ModelNode) -> dict:
# TODO
pass

@classmethod
def parse_relation_results(cls, relation_results_entry: agate.Row) -> dict:
"""
Translate agate objects from the database into a standard dictionary.
Note:
This was only built for materialized views, which does not specify a sortstyle.
Processing of `sortstyle` has been omitted here, which means it's the default (compound).
Args:
relation_results_entry: the description of the sortkey and sortstyle from the database in this format:
agate.Row({
...,
"sortkey1": "<column_name>",
...
})
Returns: a standard dictionary describing this `DatabrickSortConfig` instance
"""
# TODO
pass


@dataclass(frozen=True, eq=True, unsafe_hash=True)
class DatabricksScheduleConfigChange(RelationConfigChange):
context: Optional[bool] = None

@property
def requires_full_refresh(self) -> bool:
return False
4 changes: 3 additions & 1 deletion tests/unit/relation_configs/test_materialized_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import pytest

from dbt.adapters.databricks.relation_configs import DatabricksMaterializedViewConfig
from dbt.adapters.databricks.relation_configs.materialized_view import (
DatabricksMaterializedViewConfig,
)


@pytest.mark.parametrize("bool_value", [True, False, "True", "False", "true", "false"])
Expand Down

0 comments on commit fc8b3fb

Please sign in to comment.