-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
wrap models using macros #356
Merged
cmcarthur
merged 39 commits into
feature/schema-tests-defined-by-macros
from
kill-model-py
Apr 5, 2017
Merged
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
c591815
code cleanup + fix full-refresh opt
drewbanin 3d4d72d
make integration tests actually test flags
drewbanin f651568
accidently create operations
drewbanin e080f73
Handle concurrent `DROP ... CASCADE`s in Redshift (#349)
cmcarthur fe79e07
working with a few rough edges
da6c467
Fix/full refresh (#350)
drewbanin f94f455
Merge branch 'development' into feature/already-exists-compilation-co…
drewbanin 7f54175
test that should be green if macro parsing works the right way
23d42d2
wip
drewbanin cacf5c9
Merge branch 'feature/already-exists-compilation-context' into contex…
drewbanin b43be9e
validate macros don't use context vars
drewbanin ace4b49
fix tests
drewbanin bc0e1c2
working with a few rough edges
694d07b
test that should be green if macro parsing works the right way
c55ff34
inject into jinja with custom extension
drewbanin 8537229
wip
drewbanin 4846515
validate macros don't use context vars
drewbanin c06ae6d
fix tests
drewbanin e03f804
Merge branch 'contextless-macros' of github.com:fishtown-analytics/db…
drewbanin 2b3f46e
add test for `target` in macros, disallow target
drewbanin e2c279b
add license to RTD
drewbanin ce96825
wip - materialize via macros
drewbanin 5ffe4a5
always release connections at the end of execute_node (#354)
cmcarthur d907fca
merged development
ab4d5a3
tests passing
a266308
integration tests passing, again
e533b36
unit tests & pep8 passing
d3d2da5
merge contextless macros branch
cb2bba4
functional test tweaks
f1f39c1
remove extra raise
0b810c6
fix integration tests
4f663a0
allow global project macros to be referenced without a namespace
837bff4
handle compilation errors
0574958
add manifest file
drewbanin 1063500
include global project files in dbt dist
drewbanin 161d70c
Merge pull request #361 from fishtown-analytics/include-global-project
drewbanin 8b59748
rename funcs to adapter
b7044d9
remove compile task
2921a00
don't throw compilr error for this/target in macro
drewbanin 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import dbt.project | ||
import dbt.utils | ||
import dbt.include | ||
import dbt.wrapper | ||
|
||
from dbt.model import Model | ||
from dbt.utils import This, Var, is_enabled, get_materialization, NodeType, \ | ||
|
@@ -269,13 +270,18 @@ def get_compiler_context(self, linker, model, flat_graph): | |
context = self.project.context() | ||
adapter = get_adapter(self.project.run_environment()) | ||
|
||
if dbt.flags.NON_DESTRUCTIVE or \ | ||
get_materialization(model) == 'incremental': | ||
table_name = model.get('name') | ||
else: | ||
table_name = '{}__dbt_tmp'.format(model.get('name')) | ||
|
||
# built-ins | ||
context['ref'] = self.__ref(context, model, flat_graph) | ||
context['config'] = self.__model_config(model, linker) | ||
context['this'] = This( | ||
context['env']['schema'], | ||
(model.get('name') if dbt.flags.NON_DESTRUCTIVE | ||
else '{}__dbt_tmp'.format(model.get('name'))), | ||
table_name, | ||
model.get('name') | ||
) | ||
context['var'] = Var(model, context=context) | ||
|
@@ -291,33 +297,6 @@ def get_compiler_context(self, linker, model, flat_graph): | |
|
||
return context | ||
|
||
def get_context(self, linker, model, models): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
# THIS IS STILL USED FOR WRAPPING, BUT SHOULD GO AWAY | ||
# - Connor | ||
runtime = RuntimeContext(model=model) | ||
|
||
context = self.project.context() | ||
|
||
# built-ins | ||
context['ref'] = self.__ref(context, model, models) | ||
context['config'] = self.__model_config(model, linker) | ||
context['this'] = This( | ||
context['env']['schema'], model.immediate_name, model.name | ||
) | ||
context['var'] = Var(model, context=context) | ||
context['target'] = self.project.get_target() | ||
|
||
# these get re-interpolated at runtime! | ||
context['run_started_at'] = '{{ run_started_at }}' | ||
context['invocation_id'] = '{{ invocation_id }}' | ||
|
||
adapter = get_adapter(self.project.run_environment()) | ||
context['sql_now'] = adapter.date_function() | ||
|
||
runtime.update_global(context) | ||
|
||
return runtime | ||
|
||
def compile_node(self, linker, node, flat_graph): | ||
logger.debug("Compiling {}".format(node.get('unique_id'))) | ||
|
||
|
@@ -403,21 +382,13 @@ def compile_graph(self, linker, flat_graph): | |
pass | ||
|
||
else: | ||
model = Model( | ||
self.project, | ||
injected_node.get('root_path'), | ||
injected_node.get('path'), | ||
all_projects.get(injected_node.get('package_name'))) | ||
|
||
cfg = injected_node.get('config', {}) | ||
model._config = cfg | ||
|
||
context = self.get_context(linker, | ||
model, | ||
injected_graph.get('nodes')) | ||
|
||
wrapped_stmt = model.compile( | ||
injected_node.get('injected_sql'), self.project, context) | ||
context = self.get_compiler_context(linker, injected_node, injected_graph) | ||
wrapped_stmt = dbt.wrapper.wrap( | ||
injected_node, | ||
self.project, | ||
context, | ||
flat_graph) | ||
|
||
injected_node['wrapped_sql'] = wrapped_stmt | ||
wrapped_graph['nodes'][name] = injected_node | ||
|
57 changes: 57 additions & 0 deletions
57
dbt/include/global_project/macros/materializations/incremental.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,57 @@ | ||
|
||
|
||
{# this shouldnt be duplicated - use the existing macro! #} | ||
{% macro dbt__create_table(schema, identifier, dist, sort, sql) -%} | ||
|
||
create table {{ schema }}.{{ identifier }} {{ dist }} {{ sort }} as ( | ||
{{ sql }} | ||
); | ||
|
||
{%- endmacro %} | ||
|
||
{% macro dbt__incremental_delete(schema, identifier, unique_key) -%} | ||
|
||
delete | ||
from "{{ schema }}"."{{ identifier }}" | ||
where ({{ unique_key }}) in ( | ||
select ({{ unique_key }}) from "{{ identifier }}__dbt_incremental_tmp" | ||
); | ||
|
||
{%- endmacro %} | ||
|
||
{% macro dbt__create_incremental(schema, identifier, dist, sort, sql, sql_where, funcs, unique_key=None) -%} | ||
|
||
{% if not funcs['already_exists'](schema, identifier) -%} | ||
|
||
{{ dbt__create_table(schema, identifier, dist, sort, sql) }} | ||
|
||
{%- else -%} | ||
|
||
create temporary table "{{ identifier }}__dbt_incremental_tmp" as ( | ||
with dbt_incr_sbq as ( | ||
{{ sql }} | ||
) | ||
select * from dbt_incr_sbq | ||
where coalesce({{ sql_where }}, TRUE) | ||
); | ||
|
||
-- DBT_OPERATION { function: expand_column_types_if_needed, args: { temp_table: "{{ identifier }}__dbt_incremental_tmp", to_schema: "{{ schema }}", to_table: "{{ identifier }}"} } | ||
|
||
{% set dest_columns = funcs['get_columns_in_table'](schema, identifier) %} | ||
{% set dest_cols_csv = dest_columns | map(attribute='quoted') | join(', ') %} | ||
|
||
{% if unique_key is defined -%} | ||
|
||
{{ dbt__incremental_delete(schema, identifier, unique_key) }} | ||
|
||
{%- endif %} | ||
|
||
insert into "{{ schema }}"."{{ identifier }}" ({{ dest_cols_csv }}) | ||
( | ||
select {{ dest_cols_csv }} | ||
from "{{ identifier }}__dbt_incremental_tmp" | ||
); | ||
|
||
{%- endif %} | ||
|
||
{%- 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,8 @@ | ||
|
||
{% macro dbt__create_table(schema, identifier, dist, sort, sql) -%} | ||
|
||
create table {{ schema }}.{{ identifier }} {{ dist }} {{ sort }} as ( | ||
{{ 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,8 @@ | ||
|
||
{% macro dbt__create_view(schema, identifier, sql) -%} | ||
|
||
create view {{ schema }}.{{ identifier }} as ( | ||
{{ sql }} | ||
); | ||
|
||
{%- endmacro %} |
20 changes: 20 additions & 0 deletions
20
dbt/include/global_project/macros/materializations/wrapper.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,20 @@ | ||
|
||
{% macro dbt__wrap(sql, pre_hooks, post_hooks) -%} | ||
|
||
-- Compiled by DBT | ||
|
||
{% for hook in pre_hooks %} | ||
|
||
{{ hook }}; | ||
|
||
{% endfor %} | ||
|
||
{{ sql }} | ||
|
||
{% for hook in post_hooks -%} | ||
|
||
{{ hook }}; | ||
|
||
{% endfor %} | ||
|
||
{% 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 |
---|---|---|
|
@@ -31,6 +31,7 @@ def main(args=None): | |
handle(args) | ||
|
||
except RuntimeError as e: | ||
raise | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cmcarthur we should remove this -- i just threw it in there for debugging |
||
logger.info("Encountered an error:") | ||
logger.info(str(e)) | ||
sys.exit(1) | ||
|
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
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.
great catch