-
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
Compile on-run-(start|end) hooks to file #412
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6306909
mostly working
drewbanin 821256b
fix bug for test
drewbanin d5470f8
Merge branch 'development' into compile-on-run-hooks-to-file
drewbanin 0572e9d
code cleanup, pep8
drewbanin 6296e8e
code cleanup
drewbanin 061678c
typo
drewbanin 01bb4d7
RunHookTypes --> RunHookType
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
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,12 +5,11 @@ | |
import os | ||
import time | ||
import itertools | ||
from datetime import datetime | ||
|
||
from dbt.adapters.factory import get_adapter | ||
from dbt.logger import GLOBAL_LOGGER as logger | ||
|
||
from dbt.utils import get_materialization, NodeType, is_type | ||
from dbt.utils import get_materialization, NodeType, is_type, get_nodes_by_tags | ||
|
||
import dbt.clients.jinja | ||
import dbt.compilation | ||
|
@@ -370,26 +369,6 @@ def execute_archive(profile, node, context): | |
return result | ||
|
||
|
||
def run_hooks(profile, hooks, context, source): | ||
if type(hooks) not in (list, tuple): | ||
hooks = [hooks] | ||
|
||
ctx = { | ||
"target": profile, | ||
"state": "start", | ||
"invocation_id": context['invocation_id'], | ||
"run_started_at": context['run_started_at'] | ||
} | ||
|
||
compiled_hooks = [ | ||
dbt.clients.jinja.get_rendered(hook, ctx) for hook in hooks | ||
] | ||
|
||
adapter = get_adapter(profile) | ||
|
||
return adapter.execute_all(profile=profile, sqls=compiled_hooks) | ||
|
||
|
||
def track_model_run(index, num_nodes, run_model_result): | ||
invocation_id = dbt.tracking.active_user.invocation_id | ||
dbt.tracking.track_model_run({ | ||
|
@@ -461,10 +440,8 @@ def call_table_exists(schema, table): | |
return adapter.table_exists( | ||
profile, schema, table, node.get('name')) | ||
|
||
self.run_started_at = datetime.now() | ||
|
||
return { | ||
"run_started_at": datetime.now(), | ||
"run_started_at": dbt.tracking.active_user.run_started_at, | ||
"invocation_id": dbt.tracking.active_user.invocation_id, | ||
"get_columns_in_table": call_get_columns_in_table, | ||
"get_missing_columns": call_get_missing_columns, | ||
|
@@ -513,7 +490,6 @@ def execute_node(self, node, flat_graph, existing, profile, adapter): | |
return node, result | ||
|
||
def compile_node(self, node, flat_graph): | ||
|
||
compiler = dbt.compilation.Compiler(self.project) | ||
node = compiler.compile_node(node, flat_graph) | ||
return node | ||
|
@@ -634,6 +610,18 @@ def as_concurrent_dep_list(self, linker, nodes_to_run): | |
|
||
return concurrent_dependency_list | ||
|
||
def run_hooks(self, profile, flat_graph, hook_type): | ||
adapter = get_adapter(profile) | ||
|
||
nodes = flat_graph.get('nodes', {}).values() | ||
start_hooks = get_nodes_by_tags(nodes, {hook_type}, NodeType.Operation) | ||
hooks = [self.compile_node(hook, flat_graph) for hook in start_hooks] | ||
|
||
master_connection = adapter.begin(profile) | ||
compiled_hooks = [hook['wrapped_sql'] for hook in hooks] | ||
adapter.execute_all(profile=profile, sqls=compiled_hooks) | ||
master_connection = adapter.commit(master_connection) | ||
|
||
def on_model_failure(self, linker, selected_nodes): | ||
def skip_dependent(node): | ||
dependent_nodes = linker.get_dependent_nodes(node.get('unique_id')) | ||
|
@@ -687,12 +675,7 @@ def execute_nodes(self, flat_graph, node_dependency_list, on_failure, | |
start_time = time.time() | ||
|
||
if should_run_hooks: | ||
master_connection = adapter.begin(profile) | ||
run_hooks(self.project.get_target(), | ||
self.project.cfg.get('on-run-start', []), | ||
self.node_context({}), | ||
'on-run-start hooks') | ||
master_connection = adapter.commit(master_connection) | ||
self.run_hooks(profile, flat_graph, dbt.utils.RunHookTypes.Start) | ||
|
||
def get_idx(node): | ||
return node_id_to_index_map.get(node.get('unique_id')) | ||
|
@@ -739,12 +722,7 @@ def get_idx(node): | |
pool.join() | ||
|
||
if should_run_hooks: | ||
adapter.begin(profile) | ||
run_hooks(self.project.get_target(), | ||
self.project.cfg.get('on-run-end', []), | ||
self.node_context({}), | ||
'on-run-end hooks') | ||
adapter.commit(master_connection) | ||
self.run_hooks(profile, flat_graph, dbt.utils.RunHookTypes.End) | ||
|
||
execution_time = time.time() - start_time | ||
|
||
|
@@ -755,18 +733,35 @@ def get_idx(node): | |
|
||
def get_ancestor_ephemeral_nodes(self, flat_graph, linked_graph, | ||
selected_nodes): | ||
node_names = { | ||
node: flat_graph['nodes'].get(node).get('name') | ||
for node in selected_nodes | ||
if node in flat_graph['nodes'] | ||
} | ||
|
||
include_spec = [ | ||
'+{}'.format(node_names[node]) | ||
for node in selected_nodes if node in node_names | ||
] | ||
|
||
all_ancestors = dbt.graph.selector.select_nodes( | ||
self.project, | ||
linked_graph, | ||
['+{}'.format(flat_graph.get('nodes').get(node).get('name')) | ||
for node in selected_nodes], | ||
include_spec, | ||
[]) | ||
|
||
return set([ancestor for ancestor in all_ancestors | ||
if(flat_graph['nodes'][ancestor].get( | ||
'resource_type') == NodeType.Model and | ||
get_materialization( | ||
flat_graph['nodes'][ancestor]) == 'ephemeral')]) | ||
res = [] | ||
|
||
for ancestor in all_ancestors: | ||
if ancestor not in flat_graph['nodes']: | ||
continue | ||
ancestor_node = flat_graph['nodes'][ancestor] | ||
is_model = ancestor_node.get('resource_type') == NodeType.Model | ||
is_ephemeral = get_materialization(ancestor_node) == 'ephemeral' | ||
if is_model and is_ephemeral: | ||
res.append(ancestor) | ||
|
||
return set(res) | ||
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. 👍 |
||
|
||
def get_nodes_to_run(self, graph, include_spec, exclude_spec, | ||
resource_types, tags): | ||
|
@@ -874,7 +869,8 @@ def compile_models(self, include_spec, exclude_spec): | |
NodeType.Model, | ||
NodeType.Test, | ||
NodeType.Archive, | ||
NodeType.Analysis | ||
NodeType.Analysis, | ||
NodeType.Operation | ||
] | ||
|
||
return self.run_types_from_graph(include_spec, | ||
|
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 |
---|---|---|
|
@@ -28,6 +28,13 @@ class NodeType(object): | |
Test = 'test' | ||
Archive = 'archive' | ||
Macro = 'macro' | ||
Operation = 'operation' | ||
|
||
|
||
class RunHookTypes: | ||
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.
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. reasonable |
||
Start = 'on-run-start' | ||
End = 'on-run-end' | ||
Both = [Start, End] | ||
|
||
|
||
class This(object): | ||
|
@@ -263,6 +270,11 @@ def get_pseudo_test_path(node_name, source_path, test_type): | |
return os.path.join(*pseudo_path_parts) | ||
|
||
|
||
def get_pseudo_hook_path(hook_name): | ||
path_parts = ['hooks', "{}.sql".format(hook_name)] | ||
return os.path.join(*path_parts) | ||
|
||
|
||
def get_run_status_line(results): | ||
total = len(results) | ||
errored = len([r for r in results if r.errored or r.failed]) | ||
|
@@ -277,3 +289,12 @@ def get_run_status_line(results): | |
errored=errored, | ||
skipped=skipped | ||
)) | ||
|
||
|
||
def get_nodes_by_tags(nodes, match_tags, resource_type): | ||
matched_nodes = [] | ||
for node in nodes: | ||
node_tags = node.get('tags', set()) | ||
if len(node_tags & match_tags): | ||
matched_nodes.append(node) | ||
return matched_nodes |
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.
looks like this file was
chmod +x
ed -- can you undo that