Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADAP-608: Finalize Materialized Views #527

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20230711-013016.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Add materialized view support with the new change management framework
time: 2023-07-11T01:30:16.22273-04:00
custom:
Author: mikealfare
Issue: dbt-labs/dbt-core#6911
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dev: ## Installs adapter in develop mode along with development dependencies
dev-uninstall: ## Uninstalls all packages while maintaining the virtual environment
## Useful when updating versions, or if you accidentally installed into the system interpreter
pip freeze | grep -v "^-e" | cut -d "@" -f1 | xargs pip uninstall -y
pip uninstall -y dbt-redshift

.PHONY: mypy
mypy: ## Runs mypy against staged changes for static type checking.
Expand Down
25 changes: 24 additions & 1 deletion dbt/adapters/redshift/impl.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
from collections import namedtuple
from dataclasses import dataclass
from typing import Optional, Set, Any, Dict, Type
from collections import namedtuple
from dbt.adapters.base import PythonJobHelper
from dbt.adapters.base.impl import AdapterConfig, ConstraintSupport
from dbt.adapters.base.meta import available
from dbt.adapters.relation.factory import RelationFactory
from dbt.adapters.sql import SQLAdapter
from dbt.contracts.connection import AdapterResponse
from dbt.contracts.graph.nodes import ConstraintType
from dbt.contracts.relation import RelationType
from dbt.events import AdapterLogger


import dbt.exceptions

from dbt.adapters.redshift import RedshiftConnectionManager, RedshiftRelation
from dbt.adapters.redshift.relation.models import (
RedshiftMaterializedViewRelation,
RedshiftMaterializedViewRelationChangeset,
RedshiftRenderPolicy,
)


logger = AdapterLogger("Redshift")
Expand Down Expand Up @@ -46,6 +53,22 @@ class RedshiftAdapter(SQLAdapter):
ConstraintType.foreign_key: ConstraintSupport.NOT_ENFORCED,
}

@property
def relation_factory(self) -> RelationFactory:
return RelationFactory(
relation_models={
RelationType.MaterializedView: RedshiftMaterializedViewRelation,
},
relation_changesets={
RelationType.MaterializedView: RedshiftMaterializedViewRelationChangeset,
},
relation_can_be_renamed={
RelationType.Table,
RelationType.View,
},
render_policy=RedshiftRenderPolicy,
)

@classmethod
def date_function(cls):
return "getdate()"
Expand Down
105 changes: 0 additions & 105 deletions dbt/adapters/redshift/relation.py

This file was deleted.

25 changes: 25 additions & 0 deletions dbt/adapters/redshift/relation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from dataclasses import dataclass

from dbt.adapters.base.relation import BaseRelation
from dbt.exceptions import DbtRuntimeError

from dbt.adapters.redshift.relation.models import MAX_CHARACTERS_IN_IDENTIFIER


@dataclass(frozen=True, eq=False, repr=False)
class RedshiftRelation(BaseRelation):
def __post_init__(self):
# Check for length of Redshift table/view names.
# Check self.type to exclude test relation identifiers
if (
self.identifier is not None
and self.type is not None
and len(self.identifier) > MAX_CHARACTERS_IN_IDENTIFIER
):
raise DbtRuntimeError(
f"Relation name '{self.identifier}' "
f"is longer than {MAX_CHARACTERS_IN_IDENTIFIER} characters"
)

def relation_max_name_length(self):
return MAX_CHARACTERS_IN_IDENTIFIER
18 changes: 18 additions & 0 deletions dbt/adapters/redshift/relation/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from dbt.adapters.redshift.relation.models.database import RedshiftDatabaseRelation
from dbt.adapters.redshift.relation.models.dist import (
RedshiftDistRelation,
RedshiftDistStyle,
)
from dbt.adapters.redshift.relation.models.materialized_view import (
RedshiftMaterializedViewRelation,
RedshiftMaterializedViewRelationChangeset,
)
from dbt.adapters.redshift.relation.models.policy import (
RedshiftRenderPolicy,
MAX_CHARACTERS_IN_IDENTIFIER,
)
from dbt.adapters.redshift.relation.models.schema import RedshiftSchemaRelation
from dbt.adapters.redshift.relation.models.sort import (
RedshiftSortRelation,
RedshiftSortStyle,
)
36 changes: 36 additions & 0 deletions dbt/adapters/redshift/relation/models/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from dataclasses import dataclass
from typing import Set

from dbt.adapters.relation.models import DatabaseRelation
from dbt.adapters.validation import ValidationMixin, ValidationRule
from dbt.exceptions import DbtRuntimeError

from dbt.adapters.redshift.relation.models.policy import RedshiftRenderPolicy


@dataclass(frozen=True, eq=True, unsafe_hash=True)
class RedshiftDatabaseRelation(DatabaseRelation, ValidationMixin):
"""
This config follow the specs found here:
https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_DATABASE.html

The following parameters are configurable by dbt:
- name: name of the database
"""

# attribution
name: str

# configuration
render = RedshiftRenderPolicy

@property
def validation_rules(self) -> Set[ValidationRule]:
return {
ValidationRule(
validation_check=len(self.name or "") > 0,
validation_error=DbtRuntimeError(
f"dbt-redshift requires a name for a database, received: {self.name}"
),
)
}
Loading