Skip to content

Commit

Permalink
Save selector dictionary and write out in manifest [#2693][#2800]
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Nov 6, 2020
1 parent 4e19e87 commit 349e96a
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features
- Added macro get_partitions_metadata(table) to return partition metadata for partitioned table [#2596](https://github.com/fishtown-analytics/dbt/pull/2596)
- Added native python 're' module for regex in jinja templates [#2851](https://github.com/fishtown-analytics/dbt/pull/2851)
- Save selectors.yml dictionary to manifest, allow descriptions ([#2693](https://github.com/fishtown-analytics/dbt/issues/2693), [#2866](https://github.com/fishtown-analytics/dbt/pull/2866))

### Fixes
- Respect --project-dir in dbt clean command ([#2840](https://github.com/fishtown-analytics/dbt/issues/2840), [#2841](https://github.com/fishtown-analytics/dbt/pull/2841))
Expand Down
2 changes: 2 additions & 0 deletions core/dbt/config/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def read_user_config(directory: str) -> UserConfig:
return UserConfig()


# The Profile class is included in RuntimeConfig, so any attribute
# additions must also be set where the RuntimeConfig class is created
@dataclass
class Profile(HasCredentials):
profile_name: str
Expand Down
5 changes: 5 additions & 0 deletions core/dbt/config/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ def create_project(self, rendered: RenderComponents) -> 'Project':
query_comment = _query_comment_from_cfg(cfg.query_comment)

packages = package_config_from_data(rendered.packages_dict)
manifest_selectors = value_or(rendered.selectors_dict, {})
selectors = selector_config_from_data(rendered.selectors_dict)

project = Project(
Expand Down Expand Up @@ -396,6 +397,7 @@ def create_project(self, rendered: RenderComponents) -> 'Project':
snapshots=snapshots,
dbt_version=dbt_version,
packages=packages,
manifest_selectors=manifest_selectors,
selectors=selectors,
query_comment=query_comment,
sources=sources,
Expand Down Expand Up @@ -476,6 +478,8 @@ def to_dict(self):
return self.vars


# The Project class is included in RuntimeConfig, so any attribute
# additions must also be set where the RuntimeConfig class is created
@dataclass
class Project:
project_name: str
Expand Down Expand Up @@ -504,6 +508,7 @@ class Project:
vars: VarProvider
dbt_version: List[VersionSpecifier]
packages: Dict[str, Any]
manifest_selectors: Dict[str, Any]
selectors: SelectorConfig
query_comment: QueryComment
config_version: int
Expand Down
2 changes: 2 additions & 0 deletions core/dbt/config/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def from_parts(
snapshots=project.snapshots,
dbt_version=project.dbt_version,
packages=project.packages,
manifest_selectors=project.manifest_selectors,
selectors=project.selectors,
query_comment=project.query_comment,
sources=project.sources,
Expand Down Expand Up @@ -483,6 +484,7 @@ def from_parts(
snapshots=project.snapshots,
dbt_version=project.dbt_version,
packages=project.packages,
manifest_selectors=project.manifest_selectors,
selectors=project.selectors,
query_comment=project.query_comment,
sources=project.sources,
Expand Down
9 changes: 9 additions & 0 deletions core/dbt/contracts/graph/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ class Manifest:
exposures: MutableMapping[str, ParsedExposure]
disabled: List[CompileResultNode]
files: MutableMapping[str, SourceFile]
selectors: MutableMapping[str, Any]
metadata: ManifestMetadata = field(default_factory=ManifestMetadata)
flat_graph: Dict[str, Any] = field(default_factory=dict)
_docs_cache: Optional[DocCache] = None
Expand All @@ -462,6 +463,7 @@ def from_macros(
docs={},
exposures={},
disabled=[],
selectors={},
files=files,
)

Expand Down Expand Up @@ -732,6 +734,7 @@ def deepcopy(self):
exposures={k: _deepcopy(v) for k, v in self.exposures.items()},
disabled=[_deepcopy(n) for n in self.disabled],
metadata=self.metadata,
selectors=self.root_project.manifest_selectors,
files={k: _deepcopy(v) for k, v in self.files.items()},
)

Expand All @@ -749,6 +752,7 @@ def writable_manifest(self):
macros=self.macros,
docs=self.docs,
exposures=self.exposures,
selectors=self.selectors,
metadata=self.metadata,
disabled=self.disabled,
child_map=forward_edges,
Expand Down Expand Up @@ -942,6 +946,11 @@ class WritableManifest(ArtifactMixin):
'The macros defined in the dbt project and its dependencies'
))
)
selectors: Mapping[UniqueID, Any] = field(
metadata=dict(description=(
'The selectors defined in selectors.yml'
))
)
docs: Mapping[UniqueID, ParsedDocumentation] = field(
metadata=dict(description=(
'The docs defined in the dbt project and its dependencies'
Expand Down
1 change: 1 addition & 0 deletions core/dbt/contracts/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class SelectorDefinition(JsonSchemaMixin):
name: str
definition: Union[str, Dict[str, Any]]
description: str = ''


@dataclass
Expand Down
1 change: 1 addition & 0 deletions core/dbt/parser/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ def create_manifest(self) -> Manifest:
metadata=self.root_project.get_metadata(),
disabled=disabled,
files=self.results.files,
selectors=self.root_project.manifest_selectors,
)
manifest.patch_nodes(self.results.patches)
manifest.patch_macros(self.results.macro_patches)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from test.integration.base import DBTIntegrationTest, use_profile
import yaml
import json
import os

class TestGraphSelection(DBTIntegrationTest):

Expand All @@ -10,6 +13,18 @@ def schema(self):
def models(self):
return "models"

@property
def selectors_config(self):
return yaml.safe_load('''
selectors:
- name: bi_selector
description: This is a BI selector
definition:
method: tag
value: bi
''')


def assert_correct_schemas(self):
with self.get_connection():
exists = self.adapter.check_schema_exists(
Expand Down Expand Up @@ -43,7 +58,7 @@ def test__postgres__specific_model(self):
def test__postgres__tags(self):
self.run_sql_file("seed.sql")

results = self.run_dbt(['run', '--models', 'tag:bi'])
results = self.run_dbt(['run', '--selector', 'bi_selector'])
self.assertEqual(len(results), 2)

created_models = self.get_models_in_schema()
Expand All @@ -52,6 +67,12 @@ def test__postgres__tags(self):
self.assertTrue('users' in created_models)
self.assertTrue('users_rollup' in created_models)
self.assert_correct_schemas()
self.assertTrue(os.path.exists('./target/manifest.json'))
with open('./target/manifest.json') as fp:
manifest = json.load(fp)
self.assertTrue('selectors' in manifest)



@use_profile('postgres')
def test__postgres__tags_and_children(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,7 @@ def expected_seeded_manifest(self, model_database=None):
'maturity': None,
}
},
'selectors': {},
'parent_map': {
'model.test.model': ['seed.test.seed'],
'model.test.second_model': ['seed.test.seed'],
Expand Down Expand Up @@ -1825,6 +1826,7 @@ def expected_postgres_references_manifest(self, model_database=None):
},
},
'exposures': {},
'selectors': {},
'docs': {
'dbt.__overview__': ANY,
'test.column_info': {
Expand Down Expand Up @@ -2322,6 +2324,7 @@ def expected_bigquery_complex_manifest(self):
},
'sources': {},
'exposures': {},
'selectors': {},
'child_map': {
'model.test.clustered': [],
'model.test.multi_clustered': [],
Expand Down Expand Up @@ -2533,6 +2536,7 @@ def expected_redshift_incremental_view_manifest(self):
},
'sources': {},
'exposures': {},
'selectors': {},
'parent_map': {
'model.test.model': ['seed.test.seed'],
'seed.test.seed': []
Expand Down Expand Up @@ -2572,7 +2576,7 @@ def verify_manifest(self, expected_manifest):

manifest_keys = frozenset({
'nodes', 'sources', 'macros', 'parent_map', 'child_map',
'docs', 'metadata', 'docs', 'disabled', 'exposures'
'docs', 'metadata', 'docs', 'disabled', 'exposures', 'selectors',
})

self.assertEqual(frozenset(manifest), manifest_keys)
Expand Down
5 changes: 5 additions & 0 deletions test/unit/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def test__prepend_ctes__already_has_cte(self):
disabled=[],
files={},
exposures={},
selectors={},
)

compiler = dbt.compilation.Compiler(self.config)
Expand Down Expand Up @@ -236,6 +237,7 @@ def test__prepend_ctes__no_ctes(self):
disabled=[],
files={},
exposures={},
selectors={},
)

compiler = dbt.compilation.Compiler(self.config)
Expand Down Expand Up @@ -327,6 +329,7 @@ def test__prepend_ctes(self):
disabled=[],
files={},
exposures={},
selectors={},
)

compiler = dbt.compilation.Compiler(self.config)
Expand Down Expand Up @@ -430,6 +433,7 @@ def test__prepend_ctes__cte_not_compiled(self):
disabled=[],
files={},
exposures={},
selectors={},
)

compiler = dbt.compilation.Compiler(self.config)
Expand Down Expand Up @@ -534,6 +538,7 @@ def test__prepend_ctes__multiple_levels(self):
disabled=[],
files={},
exposures={},
selectors={},
)

compiler = dbt.compilation.Compiler(self.config)
Expand Down
1 change: 1 addition & 0 deletions test/unit/test_graph_selector_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ def manifest(seed, source, ephemeral_model, view_model, table_model, ext_source,
files={},
exposures={},
disabled=[],
selectors={},
)
return manifest

Expand Down
2 changes: 2 additions & 0 deletions test/unit/test_graph_selector_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ def test_parse_simple():
sf = parse_file('''\
selectors:
- name: tagged_foo
description: Selector for foo-tagged models
definition:
tag: foo
''')

assert len(sf.selectors) == 1
assert sf.selectors[0].description == 'Selector for foo-tagged models'
parsed = cli.parse_from_selectors_definition(sf)
assert len(parsed) == 1
assert 'tagged_foo' in parsed
Expand Down
23 changes: 14 additions & 9 deletions test/unit/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def tearDown(self):
def test__no_nodes(self):
manifest = Manifest(
nodes={}, sources={}, macros={}, docs={}, disabled=[], files={},
exposures={},
exposures={}, selectors={},
metadata=ManifestMetadata(generated_at=datetime.utcnow()),
)
self.assertEqual(
Expand All @@ -234,6 +234,7 @@ def test__no_nodes(self):
'sources': {},
'macros': {},
'exposures': {},
'selectors': {},
'parent_map': {},
'child_map': {},
'metadata': {
Expand All @@ -253,7 +254,7 @@ def test__nested_nodes(self):
nodes = copy.copy(self.nested_nodes)
manifest = Manifest(
nodes=nodes, sources={}, macros={}, docs={}, disabled=[], files={},
exposures={},
exposures={}, selectors={},
metadata=ManifestMetadata(generated_at=datetime.utcnow()),
)
serialized = manifest.writable_manifest().to_dict()
Expand Down Expand Up @@ -320,7 +321,7 @@ def test__build_flat_graph(self):
nodes = copy.copy(self.nested_nodes)
sources = copy.copy(self.sources)
manifest = Manifest(nodes=nodes, sources=sources, macros={}, docs={},
disabled=[], files={}, exposures={})
disabled=[], files={}, exposures={}, selectors={})
manifest.build_flat_graph()
flat_graph = manifest.flat_graph
flat_nodes = flat_graph['nodes']
Expand Down Expand Up @@ -365,7 +366,7 @@ def test_no_nodes_with_metadata(self, mock_user):
generated_at=datetime.utcnow(),
)
manifest = Manifest(nodes={}, sources={}, macros={}, docs={},
disabled=[],
disabled=[], selectors={},
metadata=metadata, files={}, exposures={})

self.assertEqual(
Expand All @@ -375,6 +376,7 @@ def test_no_nodes_with_metadata(self, mock_user):
'sources': {},
'macros': {},
'exposures': {},
'selectors': {},
'parent_map': {},
'child_map': {},
'docs': {},
Expand All @@ -395,7 +397,7 @@ def test_no_nodes_with_metadata(self, mock_user):

def test_get_resource_fqns_empty(self):
manifest = Manifest(nodes={}, sources={}, macros={}, docs={},
disabled=[], files={}, exposures={})
disabled=[], files={}, exposures={}, selectors={})
self.assertEqual(manifest.get_resource_fqns(), {})

def test_get_resource_fqns(self):
Expand All @@ -421,7 +423,7 @@ def test_get_resource_fqns(self):
checksum=FileHash.empty(),
)
manifest = Manifest(nodes=nodes, sources=self.sources, macros={}, docs={},
disabled=[], files={}, exposures={})
disabled=[], files={}, exposures={}, selectors={})
expect = {
'models': frozenset([
('snowplow', 'events'),
Expand Down Expand Up @@ -604,7 +606,7 @@ def tearDown(self):
@freezegun.freeze_time('2018-02-14T09:15:13Z')
def test__no_nodes(self):
metadata = ManifestMetadata(generated_at=datetime.utcnow(), invocation_id='01234567-0123-0123-0123-0123456789ab')
manifest = Manifest(nodes={}, sources={}, macros={}, docs={},
manifest = Manifest(nodes={}, sources={}, macros={}, docs={}, selectors={},
disabled=[], metadata=metadata, files={}, exposures={})
self.assertEqual(
manifest.writable_manifest().to_dict(),
Expand All @@ -613,6 +615,7 @@ def test__no_nodes(self):
'macros': {},
'sources': {},
'exposures': {},
'selectors': {},
'parent_map': {},
'child_map': {},
'metadata': {
Expand All @@ -631,7 +634,7 @@ def test__no_nodes(self):
def test__nested_nodes(self):
nodes = copy.copy(self.nested_nodes)
manifest = Manifest(nodes=nodes, sources={}, macros={}, docs={},
disabled=[],
disabled=[], selectors={},
metadata=ManifestMetadata(generated_at=datetime.utcnow()),
files={}, exposures={})
serialized = manifest.writable_manifest().to_dict()
Expand Down Expand Up @@ -696,7 +699,7 @@ def test__nested_nodes(self):
def test__build_flat_graph(self):
nodes = copy.copy(self.nested_nodes)
manifest = Manifest(nodes=nodes, sources={}, macros={}, docs={},
disabled=[],
disabled=[], selectors={},
files={}, exposures={})
manifest.build_flat_graph()
flat_graph = manifest.flat_graph
Expand Down Expand Up @@ -746,6 +749,7 @@ def setUp(self):
disabled=[],
files={},
exposures={},
selectors={},
)


Expand All @@ -766,6 +770,7 @@ def make_manifest(nodes=[], sources=[], macros=[], docs=[]):
disabled=[],
files={},
exposures={},
selectors={},
)


Expand Down
Loading

0 comments on commit 349e96a

Please sign in to comment.