Skip to content
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

Adding a generate_alias_name macro #1363

Merged
merged 9 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions core/dbt/include/global_project/macros/etc/get_custom_alias.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

{#
Renders a alias name given a custom alias name. If the custom
alias name is none, then the resulting alias is just the filename of the
model. If a alias override is specified, then that is used.

This macro can be overriden in projects to define different semantics
for rendering a alias name.

Arguments:
custom_alias_name: The custom alias name specified for a model, or none

#}
{% macro generate_alias_name(node, custom_alias_name=none) -%}

{%- if custom_alias_name is none -%}

{{ node.name }}

{%- else -%}

{{ custom_alias_name | trim }}

{%- endif -%}

{%- endmacro %}
45 changes: 44 additions & 1 deletion core/dbt/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self, root_project_config, all_projects, macro_manifest):
)
self.macro_manifest = macro_manifest
self._get_schema_func = None
self._get_alias_func = None

def get_schema_func(self):
"""The get_schema function is set by a few different things:
Expand Down Expand Up @@ -94,6 +95,44 @@ def get_schema(_):
self._get_schema_func = get_schema
return self._get_schema_func

def get_alias_func(self):
"""The get_alias function is set by a few different things:
- if there is a 'generate_alias_name' macro in the root project,
it will be used.
- if that does not exist but there is a 'generate_alias_name'
macro in the 'dbt' internal project, that will be used
- if neither of those exist (unit tests?), a function that returns
the 'default alias' as set in the model's filename or alias
configuration.
"""
if self._get_alias_func is not None:
return self._get_alias_func

get_alias_macro = self.macro_manifest.find_macro_by_name(
'generate_alias_name',
self.root_project_config.project_name
)
if get_alias_macro is None:
get_alias_macro = self.macro_manifest.find_macro_by_name(
'generate_alias_name',
GLOBAL_PROJECT_NAME
)
if get_alias_macro is None:
def get_alias(node, custom_alias_name=None):
if custom_alias_name is None:
return node.name
else:
return custom_alias_name
else:
root_context = dbt.context.parser.generate_macro(
get_alias_macro, self.root_project_config,
self.macro_manifest
)
get_alias = get_alias_macro.generator(root_context)

self._get_alias_func = get_alias
return self._get_alias_func

def _build_intermediate_node_dict(self, config, node_dict, node_path,
package_project_config, tags, fqn,
agate_table, archive_config,
Expand Down Expand Up @@ -168,7 +207,11 @@ def _update_parsed_node_info(self, parsed_node, config):
schema_override = config.config.get('schema')
get_schema = self.get_schema_func()
parsed_node.schema = get_schema(schema_override).strip()
parsed_node.alias = config.config.get('alias', parsed_node.get('name'))

alias_override = config.config.get('alias')
get_alias = self.get_alias_func()
parsed_node.alias = get_alias(parsed_node, alias_override).strip()

parsed_node.database = config.config.get(
'database', self.default_database
).strip()
Expand Down
21 changes: 21 additions & 0 deletions test/integration/043_custom_aliases_test/macros/macros.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

{% macro generate_alias_name(node, custom_alias_name=none) -%}
{%- if custom_alias_name is none -%}
{{ node.name }}
{%- else -%}
custom_{{ custom_alias_name | trim }}
{%- endif -%}
{%- endmacro %}


{% macro string_literal(s) -%}
{{ adapter_macro('test.string_literal', s) }}
{%- endmacro %}

{% macro default__string_literal(s) %}
'{{ s }}'::text
{% endmacro %}

{% macro bigquery__string_literal(s) %}
cast('{{ s }}' as string)
{% endmacro %}
3 changes: 3 additions & 0 deletions test/integration/043_custom_aliases_test/models/model1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ config(materialized='table', alias='alias') }}

select {{ string_literal(this.name) }} as model_name
3 changes: 3 additions & 0 deletions test/integration/043_custom_aliases_test/models/model2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ config(materialized='table') }}

select {{ string_literal(this.name) }} as model_name
15 changes: 15 additions & 0 deletions test/integration/043_custom_aliases_test/models/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2

models:
nydnarb marked this conversation as resolved.
Show resolved Hide resolved
- name: model1
columns:
- name: model_name
tests:
- accepted_values:
values: ['custom_alias']
- name: model2
columns:
- name: model_name
tests:
- accepted_values:
values: ['model2']
22 changes: 22 additions & 0 deletions test/integration/043_custom_aliases_test/test_custom_aliases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from test.integration.base import DBTIntegrationTest


class TestAliases(DBTIntegrationTest):
@property
def schema(self):
return "custom_aliases_043"

@property
def models(self):
return "test/integration/043_custom_aliases_test/models"

@property
def project_config(self):
return {
"macro-paths": ['test/integration/043_custom_aliases_test/macros'],
}

def test__customer_alias_name(self):
results = self.run_dbt(['run'])
self.assertEqual(len(results), 1)
self.run_dbt(['test'])