-
Notifications
You must be signed in to change notification settings - Fork 38
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
SPIKE: external tables as model materialization #220
Draft
dataders
wants to merge
8
commits into
main
Choose a base branch
from
spike_run_external_materialization
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
814849d
external table materialization
dataders dd36175
temporarily make materialization a macro
dataders 8f37758
lint
dataders 86f07dc
temporarily simplify materialization
dataders cf0b158
model materialization now
dataders a9fef3b
ongoing work
dataders 76b5f4d
improve control flow
dataders 2d5d9d6
force rebuild if exists as view
dataders 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
58 changes: 58 additions & 0 deletions
58
dbt/include/global_project/macros/materializations/external_table/external_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{% materialization external_table, default %} | ||
|
||
{%- set identifier = model['alias'] -%} | ||
{%- set full_refresh_mode = (should_full_refresh()) -%} | ||
|
||
{%- set existing_relation = load_cached_relation(this) -%} | ||
{%- set target_relation = this.incorporate(type=this.ExternalTable) %} | ||
|
||
{{ log('existing_relation: ' ~ existing_relation, info=True) }} | ||
{{ log('existing_relation.type: ' ~ existing_relation.type, info=True) }} | ||
|
||
{%- set exists_as_table = (existing_relation is not none and existing_relation.is_table) -%} | ||
{%- set exists_as_view = (existing_relation is not none and existing_relation.is_view) -%} | ||
{%- set exists_as_external_table = (existing_relation is not none and existing_relation.is_external_table) -%} | ||
|
||
-- build model | ||
{% set build_plan = [] %} | ||
{% set code = 'CREATE' %} | ||
|
||
{% if exists_as_view %} | ||
{% set build_plan = build_plan + [ | ||
drop_relation_if_exists(existing_relation), | ||
create_external_table(target_relation, model.columns.values()) | ||
] %} | ||
{% elif exists_as_table %} | ||
{% if full_refresh_mode %} | ||
{% set build_plan = build_plan + [create_external_table(target_relation, model.columns.values())] %} | ||
{% elif not full_refresh_mode %} | ||
{% set code = 'REFRESH' %} | ||
{% set build_plan = build_plan + refresh_external_table(target_relation) %} | ||
{% endif %} | ||
{% else %} | ||
{% set build_plan = build_plan + [ | ||
create_external_table(target_relation, model.columns.values()) | ||
] %} | ||
{% endif %} | ||
|
||
{% call noop_statement('main', code, code) %} | ||
{{ build_plan }}; | ||
{% endcall %} | ||
|
||
{%- set grant_config = config.get('grants') -%} | ||
|
||
{% set should_revoke = should_revoke(existing_relation, full_refresh_mode) %} | ||
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %} | ||
|
||
{% do persist_docs(target_relation, model) %} | ||
|
||
{{ run_hooks(post_hooks, inside_transaction=True) }} | ||
|
||
-- `COMMIT` happens here | ||
{{ adapter.commit() }} | ||
|
||
{{ run_hooks(post_hooks, inside_transaction=False) }} | ||
|
||
{{ return({'relations': [target_relation]}) }} | ||
|
||
{% endmaterialization %} |
7 changes: 7 additions & 0 deletions
7
...clude/global_project/macros/materializations/external_table/get_create_external_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{% macro create_external_table(relation, columns) %} | ||
{{ adapter.dispatch('create_external_table', 'dbt')(relation, columns) }} | ||
{% endmacro %} | ||
|
||
{% macro default__create_external_table(relation, columns) %} | ||
{{ exceptions.raise_compiler_error("External table creation is not implemented for the default adapter") }} | ||
{% endmacro %} |
54 changes: 54 additions & 0 deletions
54
dbt/include/global_project/macros/materializations/external_table/helpers.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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{% macro refresh_external_table(source_node) %} | ||
{{ return(adapter.dispatch('refresh_external_table', 'dbt')(source_node)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__refresh_external_table(source_node) %} | ||
{% do return([]) %} | ||
{% endmacro %} | ||
|
||
{% macro update_external_table_columns(source_node) %} | ||
{{ return(adapter.dispatch('update_external_table_columns', 'dbt')(source_node)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__update_external_table_columns(source_node) %} | ||
|
||
{% endmacro %} | ||
|
||
{%- macro create_external_schema(source_node) -%} | ||
{{ adapter.dispatch('create_external_schema', 'dbt')(source_node) }} | ||
{%- endmacro -%} | ||
|
||
{%- macro default__create_external_schema(source_node) -%} | ||
{%- set fqn -%} | ||
{%- if source_node.database -%} | ||
{{ source_node.database }}.{{ source_node.schema }} | ||
{%- else -%} | ||
{{ source_node.schema }} | ||
{%- endif -%} | ||
{%- endset -%} | ||
|
||
{%- set ddl -%} | ||
create schema if not exists {{ fqn }} | ||
{%- endset -%} | ||
|
||
{{ return(ddl) }} | ||
{%- endmacro -%} | ||
|
||
|
||
{% macro exit_transaction() %} | ||
{{ return(adapter.dispatch('exit_transaction', 'dbt')()) }} | ||
{% endmacro %} | ||
|
||
{% macro default__exit_transaction() %} | ||
{{ return('') }} | ||
{% endmacro %} | ||
|
||
{% macro dropif(node) %} | ||
{{ adapter.dispatch('dropif', 'dbt')(node) }} | ||
{% endmacro %} | ||
|
||
{% macro default__dropif() %} | ||
{{ exceptions.raise_compiler_error( | ||
"Dropping external tables is not implemented for the default adapter" | ||
) }} | ||
{% endmacro %} |
Oops, something went wrong.
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.
I need help here understanding why
existing_relation.type
returnsNone
when the existing relation is an External Table