Skip to content

Commit

Permalink
Include parent adapters in dispatch (#3296)
Browse files Browse the repository at this point in the history
* Add test, expect fail

* Include parent adapters in dispatch

* Use adapter type, not credentials type

* Adjust adapter_macro deprecation test

* fix test/unit/test_context.py to use postgres profile

* Add changelog note

* Redshift default column encoding now AUTO

Co-authored-by: Gerda Shank <[email protected]>
  • Loading branch information
jtcohen6 and gshank authored May 2, 2021
1 parent 29f0278 commit dd2633d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- Switch from externally storing parsing state in ParseResult object to using Manifest ([#3163](http://github.com/fishtown-analytics/dbt/issues/3163), [#3219](https://github.com/fishtown-analytics/dbt/pull/3219))
- Switch from loading project files in separate parsers to loading in one place([#3244](http://github.com/fishtown-analytics/dbt/issues/3244), [#3248](https://github.com/fishtown-analytics/dbt/pull/3248))
- Update schema/generic tests to use test materialization when executing. ([#3192](https://github.com/fishtown-analytics/dbt/issues/3192), [#3286](https://github.com/fishtown-analytics/dbt/pull/3286))
- For adapters that inherit from other adapters (e.g. `dbt-postgres` &rarr; `dbt-redshift`), `adapter.dispatch()` will now include parent macro implementations as viable candidates ([#2923](https://github.com/fishtown-analytics/dbt/issues/2923), [#3296](https://github.com/fishtown-analytics/dbt/pull/3296))

Contributors:
- [@yu-iskw](https://github.com/yu-iskw) ([#2928](https://github.com/fishtown-analytics/dbt/pull/2928))
Expand Down
13 changes: 8 additions & 5 deletions core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

from dbt import deprecations
from dbt.adapters.base.column import Column
from dbt.adapters.factory import get_adapter, get_adapter_package_names
from dbt.adapters.factory import (
get_adapter, get_adapter_package_names, get_adapter_type_names
)
from dbt.clients import agate_helper
from dbt.clients.jinja import get_rendered, MacroGenerator, MacroStack
from dbt.config import RuntimeConfig, Project
Expand Down Expand Up @@ -107,10 +109,11 @@ def commit(self):
return self._adapter.commit_if_has_connection()

def _get_adapter_macro_prefixes(self) -> List[str]:
# a future version of this could have plugins automatically call fall
# back to their dependencies' dependencies by using
# `get_adapter_type_names` instead of `[self.config.credentials.type]`
search_prefixes = [self._adapter.type(), 'default']
# order matters for dispatch:
# 1. current adapter
# 2. any parent adapters (dependencies)
# 3. 'default'
search_prefixes = get_adapter_type_names(self._adapter.type()) + ['default']
return search_prefixes

def dispatch(
Expand Down
17 changes: 17 additions & 0 deletions test/integration/012_deprecation_tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ def test_postgres_adapter_macro_fail(self):

@use_profile('redshift')
def test_redshift_adapter_macro(self):
self.assertEqual(deprecations.active_deprecations, set())
# pick up the postgres macro
self.run_dbt(strict=False)
expected = {'adapter-macro'}
self.assertEqual(expected, deprecations.active_deprecations)

@use_profile('bigquery')
def test_bigquery_adapter_macro(self):
self.assertEqual(deprecations.active_deprecations, set())
# picked up the default -> error
with self.assertRaises(dbt.exceptions.CompilationException) as exc:
Expand Down Expand Up @@ -147,6 +155,15 @@ def test_postgres_adapter_macro_pkg_fail(self):

@use_profile('redshift')
def test_redshift_adapter_macro_pkg(self):
self.assertEqual(deprecations.active_deprecations, set())
# pick up the postgres macro
self.assertEqual(deprecations.active_deprecations, set())
self.run_dbt(strict=False)
expected = {'adapter-macro'}
self.assertEqual(expected, deprecations.active_deprecations)

@use_profile('bigquery')
def test_bigquery_adapter_macro_pkg(self):
self.assertEqual(deprecations.active_deprecations, set())
# picked up the default -> error
with self.assertRaises(dbt.exceptions.CompilationException) as exc:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ dispatch_to_parent() }}
select 1 as id
17 changes: 16 additions & 1 deletion test/integration/016_macro_tests/macros/my_macros.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,23 @@
{% endmacro %}


{# there is no no default__dispatch_to_nowhere! #}
{# there is no default__dispatch_to_nowhere! #}
{% macro dispatch_to_nowhere() %}
{% set macro = adapter.dispatch('dispatch_to_nowhere') %}
{{ macro() }}
{% endmacro %}


{% macro dispatch_to_parent() %}
{% set macro = adapter.dispatch('dispatch_to_parent') %}
{{ macro() }}
{% endmacro %}

{% macro default__dispatch_to_parent() %}
{% set msg = 'No default implementation of dispatch_to_parent' %}
{{ exceptions.raise_compiler_error(msg) }}
{% endmacro %}

{% macro postgres__dispatch_to_parent() %}
{{ return('') }}
{% endmacro %}
14 changes: 14 additions & 0 deletions test/integration/016_macro_tests/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ def test_postgres_invalid_macro(self):
assert "In dispatch: No macro named 'dispatch_to_nowhere' found" in str(exc.value)


class TestDispatchMacroUseParent(DBTIntegrationTest):
@property
def schema(self):
return "test_macros_016"

@property
def models(self):
return "dispatch-inheritance-models"

@use_profile('redshift')
def test_redshift_inherited_macro(self):
self.run_dbt(['run'])


class TestMacroOverrideBuiltin(DBTIntegrationTest):
@property
def schema(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _redshift_stats(self):
"encoded": {
"id": "encoded",
"label": "Encoded",
"value": "Y",
"value": AnyStringWith('AUTO'),
"description": "Indicates whether any column in the table has compression encoding defined.",
"include": True
},
Expand Down
27 changes: 23 additions & 4 deletions test/unit/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,22 @@ def assert_has_keys(
},
}

POSTGRES_PROFILE_DATA = {
'target': 'test',
'quoting': {},
'outputs': {
'test': {
'type': 'postgres',
'host': 'localhost',
'schema': 'analytics',
'user': 'test',
'pass': 'test',
'dbname': 'test',
'port': 1,
}
},
}

PROJECT_DATA = {
'name': 'root',
'version': '0.1',
Expand Down Expand Up @@ -368,6 +384,9 @@ def get_include_paths():
def config():
return config_from_parts_or_dicts(PROJECT_DATA, PROFILE_DATA)

@pytest.fixture
def config_postgres():
return config_from_parts_or_dicts(PROJECT_DATA, POSTGRES_PROFILE_DATA)

@pytest.fixture
def manifest_fx(config):
Expand Down Expand Up @@ -399,8 +418,8 @@ def redshift_adapter(config, get_adapter):


@pytest.fixture
def postgres_adapter(config, get_adapter):
adapter = postgres.PostgresAdapter(config)
def postgres_adapter(config_postgres, get_adapter):
adapter = postgres.PostgresAdapter(config_postgres)
inject_adapter(adapter, postgres.Plugin)
get_adapter.return_value = adapter
yield adapter
Expand Down Expand Up @@ -521,13 +540,13 @@ def test_resolve_specific(config, manifest_extended, redshift_adapter, get_inclu
'dbt', 'root']).macro is rs_macro


def test_resolve_default(config, manifest_extended, postgres_adapter, get_include_paths):
def test_resolve_default(config_postgres, manifest_extended, postgres_adapter, get_include_paths):
dbt_macro = manifest_extended.macros['macro.dbt.default__some_macro']
package_macro = manifest_extended.macros['macro.root.default__some_macro']

ctx = providers.generate_runtime_model(
model=mock_model(),
config=config,
config=config_postgres,
manifest=manifest_extended,
)

Expand Down

0 comments on commit dd2633d

Please sign in to comment.