-
Notifications
You must be signed in to change notification settings - Fork 79
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
Zero-Downtime option for CTAS recreation #95
Open
SOVALINUX
wants to merge
2
commits into
Tomme:master
Choose a base branch
from
SOVALINUX:feature/zero-downtime-ctas
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,87 @@ | ||
from dataclasses import dataclass | ||
import json | ||
|
||
from dbt.adapters.base.relation import BaseRelation, Policy | ||
from typing import ( | ||
Optional, Type, TypeVar | ||
) | ||
from dbt.contracts.relation import ( | ||
RelationType | ||
) | ||
|
||
from dbt.adapters.base.relation import BaseRelation, Policy | ||
from dbt.events import AdapterLogger | ||
logger = AdapterLogger("Athena") | ||
ZERODOWNTIME_CTAS = {} | ||
|
||
@dataclass | ||
class AthenaIncludePolicy(Policy): | ||
database: bool = False | ||
schema: bool = True | ||
identifier: bool = True | ||
|
||
Self = TypeVar('Self', bound='AthenaRelation') | ||
|
||
@dataclass(frozen=True, eq=False, repr=False) | ||
class AthenaRelation(BaseRelation): | ||
quote_character: str = "" | ||
include_policy: Policy = AthenaIncludePolicy() | ||
|
||
@classmethod | ||
def create( | ||
cls: Type[Self], | ||
database: Optional[str] = None, | ||
schema: Optional[str] = None, | ||
identifier: Optional[str] = None, | ||
type: Optional[RelationType] = None, | ||
**kwargs, | ||
) -> Self: | ||
# Override default implementation to handle zero-downtime table recreation in Athena | ||
# Tables created with zero-downtime has the names like `ctas_<original_name>_<epoch_time_millis>` | ||
# This method called from several points: | ||
# 1: table.sql with all these extra parameters inside kwargs | ||
# it's actually a place where the decision on zero-downtime tables takes place | ||
# 2: compilation.py:_compile_node() when all the models are being rendered with jinja | ||
# it's not called directly, but through | ||
# providers.py: ref implementations | ||
# While jinja renders raw SQL it calls builtin ref function, which calls this method | ||
# | ||
# In case 1 we mark the model as ZERODOWNTIME_CTAS | ||
# In case 2 if the model marked as ZERODOWNTIME_CTAS, then we give the actual name in DB instead of original one | ||
|
||
if 'model' in kwargs and 'graph' in kwargs and 'ctas_id' in kwargs: | ||
model = kwargs.get("model") | ||
graph = kwargs.get("graph") | ||
alias = kwargs.get("alias") | ||
model_old_name = '{}.{}.{}'.format(model.get('resource_type'), model.get('package_name'), alias) | ||
alias_full_name = '{}.{}.{}'.format(database, schema, alias) | ||
gmodel = graph.get('nodes').get(model_old_name) | ||
ctas_id = kwargs.get("ctas_id") | ||
if gmodel is not None and alias_full_name not in ZERODOWNTIME_CTAS: | ||
# TODO: It's actually questionable do we need to update the global graph | ||
old_name = model.get('relation_name') | ||
model_id_parts = model.get('unique_id').split('.')[0:-1] | ||
model_id_parts.append(ctas_id) | ||
model_rel_parts = model.get('relation_name').split('.')[0:-1] | ||
model_rel_parts.append(ctas_id) | ||
gmodel.update({ | ||
'name': ctas_id, | ||
'alias': alias, | ||
'unique_id': '.'.join(model_id_parts), | ||
'relation_name': '.'.join(model_rel_parts), | ||
'ctas': ctas_id, | ||
}) | ||
|
||
ZERODOWNTIME_CTAS.update({alias_full_name: ctas_id}) | ||
id_full_name = '{}.{}.{}'.format(database, schema, identifier) | ||
final_id = identifier | ||
if 'graph' not in kwargs and id_full_name in ZERODOWNTIME_CTAS: | ||
final_id = ZERODOWNTIME_CTAS.get(id_full_name) | ||
kwargs.update({ | ||
'path': { | ||
'database': database, | ||
'schema': schema, | ||
'identifier': final_id, | ||
}, | ||
'type': type, | ||
}) | ||
return cls.from_dict(kwargs) |
22 changes: 16 additions & 6 deletions
22
dbt/include/athena/macros/materializations/models/table/table.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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curios question: why not let the code do this themselves? Like create the real table with a temp name and the name which corresponds to the filename as a view with this simple select star?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jankatins Good catch actually - Thanks! Haven't considered such an option to be honest. Will think on it - since it might eliminate several downsides of the solution