-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9daaee2
commit 3440faf
Showing
20 changed files
with
369 additions
and
100 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
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
99 changes: 99 additions & 0 deletions
99
dbt/adapters/maxcompute/relation_configs/_materialized_view.py
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,99 @@ | ||
from dataclasses import dataclass | ||
from typing import Any, Dict, Optional, List | ||
|
||
from dbt.adapters.contracts.relation import ( | ||
RelationConfig, | ||
ComponentName, | ||
) | ||
from dbt.adapters.maxcompute.relation_configs._base import MaxComputeBaseRelationConfig | ||
from dbt.adapters.maxcompute.relation_configs._partition import ( | ||
PartitionConfig, | ||
) | ||
from dbt.adapters.maxcompute.utils import quote_string, quote_ref | ||
|
||
|
||
@dataclass(frozen=True, eq=True, unsafe_hash=True) | ||
class MaxComputeMaterializedViewConfig(MaxComputeBaseRelationConfig): | ||
name: str | ||
project: str | ||
schema: str | ||
lifecycle: Optional[int] = None | ||
build_deferred: bool = False | ||
columns: Optional[List[str]] = None | ||
column_comment: Optional[Dict[str, str]] = None | ||
disable_rewrite: bool = False | ||
table_comment: Optional[str] = None | ||
partition_by: Optional[PartitionConfig] = None | ||
tblProperties: Optional[Dict[str, str]] = None | ||
|
||
@classmethod | ||
def from_dict(cls, config_dict: Dict[str, Any]) -> "MaxComputeMaterializedViewConfig": | ||
# required | ||
kwargs_dict: Dict[str, Any] = { | ||
"name": cls._render_part(ComponentName.Identifier, config_dict["name"]), | ||
"schema": cls._render_part(ComponentName.Schema, config_dict["schema"]), | ||
"project": cls._render_part(ComponentName.Database, config_dict["project"]), | ||
} | ||
for key, value in config_dict.items(): | ||
if key in ["name", "schema", "project"]: | ||
pass | ||
kwargs_dict[key] = value | ||
|
||
if partition := config_dict.get("partition_by"): | ||
kwargs_dict.update({"partition_by": PartitionConfig.parse(partition)}) | ||
|
||
materialized_view: "MaxComputeMaterializedViewConfig" = super().from_dict(kwargs_dict) | ||
return materialized_view | ||
|
||
@classmethod | ||
def parse_relation_config(cls, relation_config: RelationConfig) -> Dict[str, Any]: | ||
config_dict = { | ||
"name": relation_config.identifier, | ||
"schema": relation_config.schema, | ||
"project": relation_config.database, | ||
} | ||
items = ["lifecycle", "build_deferred", "columns", "column_comment", "disable_rewrite", "table_comment", | ||
"partition_by", "tblProperties"] | ||
|
||
if relation_config: | ||
for item in items: | ||
if item in relation_config.config: | ||
config_dict.update({item: relation_config.config[item]}) | ||
return config_dict | ||
|
||
def get_coordinate(self) -> str: | ||
if self.schema is None: | ||
return f"{self.name}" | ||
if self.project is None: | ||
return f"{self.schema}.{self.name}" | ||
return f"{self.project}.{self.schema}.{self.name}" | ||
|
||
def create_table_sql(self) -> str: | ||
sql = f"CREATE MATERIALIZED VIEW IF NOT EXISTS {self.get_coordinate()}\n" | ||
if self.lifecycle and self.lifecycle > 0: | ||
sql += f"LIFECYCLE {self.lifecycle}\n" | ||
if self.build_deferred: | ||
sql += "BUILD DEFERRED\n" | ||
if self.columns and len(self.columns) > 0: | ||
sql += "(" | ||
for column in self.columns: | ||
if self.column_comment and column in self.column_comment: | ||
sql += f"{quote_ref(column)} COMMENT {quote_string(self.column_comment[column])}" | ||
else: | ||
sql += f"{quote_ref(column)}" | ||
sql += ", " | ||
sql = sql[:-2] | ||
sql += ")\n" | ||
if self.disable_rewrite: | ||
sql += " DISABLE REWRITE\n" | ||
if self.table_comment: | ||
sql += f"COMMENT {quote_string(self.table_comment)}\n" | ||
if self.partition_by and len(self.partition_by.fields) > 0: | ||
sql += f"PARTITIONED BY({self.partition_by.render(False)})\n" | ||
if self.tblProperties and len(self.tblProperties) > 0: | ||
sql += "TBLPROPERTIES( " | ||
for k, v in self.tblProperties.items(): | ||
sql += f"\"{k}\"=\"{v}\", " | ||
sql = sql[:-2] | ||
sql += ")\n" | ||
return sql |
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
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
50 changes: 4 additions & 46 deletions
50
dbt/include/maxcompute/macros/relations/materialized_view/alter.sql
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 |
---|---|---|
@@ -1,55 +1,13 @@ | ||
{% macro get_alter_materialized_view_as_sql( | ||
{% macro maxcompute__get_alter_materialized_view_as_sql( | ||
relation, | ||
configuration_changes, | ||
new_config, | ||
sql, | ||
existing_relation, | ||
backup_relation, | ||
intermediate_relation | ||
) %} | ||
{{- log('Applying ALTER to: ' ~ relation) -}} | ||
{{- adapter.dispatch('get_alter_materialized_view_as_sql', 'dbt')( | ||
relation, | ||
configuration_changes, | ||
sql, | ||
existing_relation, | ||
backup_relation, | ||
intermediate_relation | ||
) -}} | ||
{{ get_replace_sql(existing_relation, relation, sql) }} | ||
{% endmacro %} | ||
|
||
|
||
{% macro default__get_alter_materialized_view_as_sql( | ||
relation, | ||
configuration_changes, | ||
sql, | ||
existing_relation, | ||
backup_relation, | ||
intermediate_relation | ||
) %} | ||
{{ exceptions.raise_compiler_error("Materialized views have not been implemented for this adapter.") }} | ||
{% endmacro %} | ||
|
||
|
||
{% macro get_materialized_view_configuration_changes(existing_relation, new_config) %} | ||
/* {# | ||
It's recommended that configuration changes be formatted as follows: | ||
{"<change_category>": [{"action": "<name>", "context": ...}]} | ||
For example: | ||
{ | ||
"indexes": [ | ||
{"action": "drop", "context": "index_abc"}, | ||
{"action": "create", "context": {"columns": ["column_1", "column_2"], "type": "hash", "unique": True}}, | ||
], | ||
} | ||
Either way, `get_materialized_view_configuration_changes` needs to align with `get_alter_materialized_view_as_sql`. | ||
#} */ | ||
{{- log('Determining configuration changes on: ' ~ existing_relation) -}} | ||
{%- do return(adapter.dispatch('get_materialized_view_configuration_changes', 'dbt')(existing_relation, new_config)) -%} | ||
{% endmacro %} | ||
|
||
|
||
{% macro default__get_materialized_view_configuration_changes(existing_relation, new_config) %} | ||
{{ exceptions.raise_compiler_error("Materialized views have not been implemented for this adapter.") }} | ||
{% macro maxcompute__get_materialized_view_configuration_changes(existing_relation, new_config) %} | ||
{% endmacro %} |
7 changes: 3 additions & 4 deletions
7
dbt/include/maxcompute/macros/relations/materialized_view/create.sql
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
{% macro maxcompute__get_create_materialized_view_as_sql(relation, sql) %} | ||
|
||
create materialized view if not exists {{ relation }} | ||
as {{ sql }}; | ||
|
||
{%- set materialized_view = adapter.Relation.materialized_view_from_relation_config(config.model) -%} | ||
{{ materialized_view.create_table_sql() }} | ||
as ({{ sql }}); | ||
{% endmacro %} |
18 changes: 5 additions & 13 deletions
18
dbt/include/maxcompute/macros/relations/materialized_view/drop.sql
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 |
---|---|---|
@@ -1,14 +1,6 @@ | ||
{# /* | ||
This was already implemented. Instead of creating a new macro that aligns with the standard, | ||
this was reused and the default was maintained. This gets called by `drop_relation`, which | ||
actually executes the drop, and `get_drop_sql`, which returns the template. | ||
*/ #} | ||
|
||
{% macro drop_materialized_view(relation) -%} | ||
{{- adapter.dispatch('drop_materialized_view', 'dbt')(relation) -}} | ||
{%- endmacro %} | ||
|
||
|
||
{% macro default__drop_materialized_view(relation) -%} | ||
drop materialized view if exists {{ relation.render() }} cascade | ||
{% macro maxcompute__drop_materialized_view(relation) -%} | ||
{% call statement(name="main") %} | ||
{{- log("replace materialized view will drop it first and then recreate.") -}} | ||
drop materialized view if exists {{ relation.render() }} | ||
{% endcall %} | ||
{%- endmacro %} |
10 changes: 2 additions & 8 deletions
10
dbt/include/maxcompute/macros/relations/materialized_view/refresh.sql
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 |
---|---|---|
@@ -1,9 +1,3 @@ | ||
{% macro refresh_materialized_view(relation) %} | ||
{{- log('Applying REFRESH to: ' ~ relation) -}} | ||
{{- adapter.dispatch('refresh_materialized_view', 'dbt')(relation) -}} | ||
{% endmacro %} | ||
|
||
|
||
{% macro default__refresh_materialized_view(relation) %} | ||
{{ exceptions.raise_compiler_error("`refresh_materialized_view` has not been implemented for this adapter.") }} | ||
{% macro maxcompute__refresh_materialized_view(relation) %} | ||
ALTER MATERIALIZED VIEW {{ relation.render() }} REBUILD; | ||
{% endmacro %} |
11 changes: 3 additions & 8 deletions
11
dbt/include/maxcompute/macros/relations/materialized_view/rename.sql
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 |
---|---|---|
@@ -1,10 +1,5 @@ | ||
{% macro get_rename_materialized_view_sql(relation, new_name) %} | ||
{{- adapter.dispatch('get_rename_materialized_view_sql', 'dbt')(relation, new_name) -}} | ||
{% endmacro %} | ||
|
||
|
||
{% macro default__get_rename_materialized_view_sql(relation, new_name) %} | ||
{% macro maxcompute__get_rename_materialized_view_sql(relation, new_name) %} | ||
{{ exceptions.raise_compiler_error( | ||
"`get_rename_materialized_view_sql` has not been implemented for this adapter." | ||
"maxcompute materialized view not support rename operation." | ||
) }} | ||
{% endmacro %} | ||
{% endmacro %} |
11 changes: 3 additions & 8 deletions
11
dbt/include/maxcompute/macros/relations/materialized_view/replace.sql
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 |
---|---|---|
@@ -1,10 +1,5 @@ | ||
{% macro get_replace_materialized_view_sql(relation, sql) %} | ||
{{- adapter.dispatch('get_replace_materialized_view_sql', 'dbt')(relation, sql) -}} | ||
{% endmacro %} | ||
|
||
|
||
{% macro default__get_replace_materialized_view_sql(relation, sql) %} | ||
{{ exceptions.raise_compiler_error( | ||
"`get_replace_materialized_view_sql` has not been implemented for this adapter." | ||
) }} | ||
{% macro maxcompute__get_replace_materialized_view_sql(relation, sql) %} | ||
{{ drop_materialized_view(relation) }} | ||
{{ get_create_materialized_view_as_sql(relation, sql) }} | ||
{% 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,5 @@ | ||
{% macro maxcompute__drop_table(relation) %} | ||
{% call statement(name="main") %} | ||
drop table if exists {{ relation }} | ||
{% endcall %} | ||
{% 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,3 @@ | ||
{%- macro maxcompute__get_rename_table_sql(relation, new_name) -%} | ||
alter table {{ relation }} rename to {{ new_name }} | ||
{%- 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,5 @@ | ||
{% macro maxcompute__drop_view(relation) %} | ||
{% call statement(name="main") %} | ||
drop view if exists {{ relation }} | ||
{% endcall %} | ||
{% 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,3 @@ | ||
{%- macro maxcompute__get_rename_view_sql(relation, new_name) -%} | ||
alter view {{ relation }} rename to {{ new_name }} | ||
{%- endmacro -%} |
Oops, something went wrong.