Skip to content

Commit

Permalink
Merge remote-tracking branch 'kickstarter/feature/model-aliasing' int…
Browse files Browse the repository at this point in the history
…o model-aliasing

$ git merge kickstarter/feature/model-aliasing
CONFLICT (content): Merge conflict in dbt/utils.py
CONFLICT (modify/delete): dbt/include/global_project/macros/materializations/table.sql deleted in HEAD and modified in kickstarter/feature/model-aliasing. Version kickstarter/feature/model-aliasing of dbt/include/global_project/macros/materializations/table.sql left in tree.
CONFLICT (modify/delete): dbt/include/global_project/macros/materializations/bigquery.sql deleted in HEAD and modified in kickstarter/feature/model-aliasing. Version kickstarter/feature/model-aliasing of dbt/include/global_project/macros/materializations/bigquery.sql left in tree.

1. dbt/utils.py

   Some major changes are being introduced in 0.10.1: Implement relations api (dbt-labs#727)
   dbt-labs#727
   dbt-labs@5344f54#diff-196bbfafed32edaf1554550f65111f87

   The Relation class was extracted into ...
   ./dbt/api/object.py                   class APIObject(dict)
   ./dbt/adapters/default/relation.py    class DefaultRelation(APIObject)
   ./dbt/adapters/bigquery/relation.py   class BigQueryRelation(DefaultRelation)
   ./dbt/adapters/snowflake/relation.py  class SnowflakeRelation(DefaultRelation)

   Changing node.get('name') to node.get('alias') ...
   ./dbt/adapters/default/relation.py
   ./dbt/adapters/bigquery/relation.py
   ./dbt/adapters/snowflake/relation.py

2. dbt/include/global_project/macros/materializations/table.sql

   This was renamed to ...
   ./dbt/include/global_project/macros/materializations/table/table.sql

3. dbt/include/global_project/macros/materializations/bigquery.sql

   This was split into ...
   ./dbt/include/global_project/macros/materializations/table/bigquery_table.sql
   and ...
   ./dbt/include/global_project/macros/materializations/view/bigquery_view.sql

4. other instances of model['name']

   The following file also mention model['name'] and probably need to change as well ...

   ./dbt/include/global_project/macros/materializations/archive/archive.sql
   ./dbt/include/global_project/macros/materializations/seed/bigquery.sql
   ./dbt/include/global_project/macros/materializations/seed/seed.sql

   Added comentary to ...

   ./dbt/exceptions.py

5. further changes

   Revert model.get('alias') to model.get('name') ...
   print_test_result_line in ./dbt/ui/printer.py (since in this context schema is NOT being used)

   Change model.get('name') to model.get('alias') ...
   print_seed_result_line in ./dbt/ui/printer.py (since in this context schema is also being used)

   Change node.get('name') to node.get('alias') ...
   _node_context in ./dbt/node_runners.py (since in this context schema is also being used)
   call_get_missing_columns in ./dbt/node_runners.py (since in this context schema is also being used)
   call_already_exists in ./dbt/node_runners.py (since in this context schema is also being used)

6. linting

   import lines must be under 80 characters
   https://www.python.org/dev/peps/pep-0328/
  • Loading branch information
jon-rtr committed May 12, 2018
2 parents 4133656 + 5040c88 commit ca7992b
Show file tree
Hide file tree
Showing 23 changed files with 154 additions and 27 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ test-integration:
@echo "Integration test run starting..."
@time docker-compose run test tox -e integration-postgres-py27,integration-postgres-py36,integration-snowflake-py27,integration-snowflake-py36,integration-bigquery-py27,integration-bigquery-py36


test-quick:
@echo "Integration test run starting..."
@time docker-compose run test tox -e integration-postgres-py36 -- -x
2 changes: 1 addition & 1 deletion dbt/adapters/bigquery/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def create_from_node(cls, profile, node, **kwargs):
return cls.create(
project=profile.get('project'),
schema=node.get('schema'),
identifier=node.get('name'),
identifier=node.get('alias'),
**kwargs)

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion dbt/adapters/default/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def create_from_node(cls, profile, node, table_name=None, **kwargs):
return cls.create(
database=profile.get('dbname'),
schema=node.get('schema'),
identifier=node.get('name'),
identifier=node.get('alias'),
table_name=table_name,
**kwargs)

Expand Down
2 changes: 1 addition & 1 deletion dbt/adapters/snowflake/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ def create_from_node(cls, profile, node, **kwargs):
return cls.create(
database=profile.get('database'),
schema=node.get('schema'),
identifier=node.get('name'),
identifier=node.get('alias'),
**kwargs)
5 changes: 3 additions & 2 deletions dbt/contracts/graph/parsed.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from voluptuous import Schema, Required, All, Any, Length, ALLOW_EXTRA
from voluptuous import Optional
from voluptuous import Schema, Required, Optional, All, Any, Length
from voluptuous import ALLOW_EXTRA

import dbt.exceptions

Expand Down Expand Up @@ -34,6 +34,7 @@
Required('unique_id'): All(basestring, Length(min=1, max=255)),
Required('fqn'): All(list, [All(basestring)]),
Required('schema'): basestring,
Optional('alias'): basestring,

Required('refs'): [All(tuple)],

Expand Down
4 changes: 4 additions & 0 deletions dbt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ def ref_bad_context(model, target_model_name, target_model_package):
To fix this, add the following hint to the top of the model "{model_name}":
-- depends_on: {ref_string}"""
# This explicitly references model['name'], instead of model['alias'], for
# better error messages. Ex. If models foo_users and bar_users are aliased
# to 'users', in their respective schemas, then you would want to see
# 'bar_users' in your error messge instead of just 'users'.
error_msg = base_error_msg.format(
model_name=model['name'],
model_path=model['path'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@
{% endcall %}
{% endfor %}

{%- set identifier = model['name'] -%}
{%- set tmp_identifier = model['name'] + '__dbt_archival_tmp' -%}
{%- set identifier = model['alias'] -%}
{%- set tmp_identifier = identifier + '__dbt_archival_tmp' -%}
{%- set tmp_relation = api.Relation.create(identifier=tmp_identifier, type='table') -%}

{% call statement() %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% macro dbt__incremental_delete(target_relation, tmp_relation) -%}

{%- set unique_key = config.require('unique_key') -%}
{%- set identifier = model['name'] -%}
{%- set identifier = model['alias'] -%}

delete
from {{ target_relation }}
Expand All @@ -16,8 +16,8 @@
{%- set sql_where = config.require('sql_where') -%}
{%- set unique_key = config.get('unique_key') -%}

{%- set identifier = model['name'] -%}
{%- set tmp_identifier = model['name'] + '__dbt_incremental_tmp' -%}
{%- set identifier = model['alias'] -%}
{%- set tmp_identifier = identifier + '__dbt_incremental_tmp' -%}

{%- set existing_relations = adapter.list_relations(schema=schema) -%}
{%- set old_relation = adapter.get_relation(relations_list=existing_relations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
{% macro bigquery__load_csv_rows(model) %}

{%- set column_override = model['config'].get('column_types', {}) -%}
{{ adapter.load_dataframe(model['schema'], model['name'], model['agate_table'], column_override) }}
{{ adapter.load_dataframe(model['schema'], model['alias'], model['agate_table'], column_override) }}

{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@

{% materialization seed, default %}

{%- set identifier = model['name'] -%}
{%- set identifier = model['alias'] -%}
{%- set full_refresh_mode = (flags.FULL_REFRESH == True) -%}
{%- set existing_relations = adapter.list_relations(schema=schema) -%}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

{% materialization table, adapter='bigquery' -%}

{%- set identifier = model['name'] -%}
{%- set identifier = model['alias'] -%}
{%- set non_destructive_mode = (flags.NON_DESTRUCTIVE == True) -%}
{%- set existing_relations = adapter.list_relations(schema=schema) -%}
{%- set old_relation = adapter.get_relation(relations_list=existing_relations, identifier=identifier) -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% materialization table, default %}
{%- set identifier = model['name'] -%}
{%- set identifier = model['alias'] -%}
{%- set tmp_identifier = identifier + '__dbt_tmp' -%}
{%- set non_destructive_mode = (flags.NON_DESTRUCTIVE == True) -%}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% materialization view, adapter='bigquery' -%}

{%- set identifier = model['name'] -%}
{%- set identifier = model['alias'] -%}
{%- set non_destructive_mode = (flags.NON_DESTRUCTIVE == True) -%}

{%- set existing_relations = adapter.list_relations(schema=schema) -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{%- materialization view, default -%}

{%- set identifier = model['name'] -%}
{%- set identifier = model['alias'] -%}
{%- set tmp_identifier = identifier + '__dbt_tmp' -%}
{%- set non_destructive_mode = (flags.NON_DESTRUCTIVE == True) -%}

Expand Down
1 change: 1 addition & 0 deletions dbt/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class SourceConfig(object):
AppendListFields = ['pre-hook', 'post-hook']
ExtendDictFields = ['vars', 'column_types', 'quoting']
ClobberFields = [
'alias',
'schema',
'enabled',
'materialized',
Expand Down
9 changes: 4 additions & 5 deletions dbt/node_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,17 @@ def _node_context(cls, adapter, project, node):
def call_get_columns_in_table(schema_name, table_name):
return adapter.get_columns_in_table(
profile, project, schema_name,
table_name, model_name=node.get('name'))
table_name, model_name=node.get('alias'))

def call_get_missing_columns(from_schema, from_table,
to_schema, to_table):
return adapter.get_missing_columns(
profile, project, from_schema, from_table,
to_schema, to_table, node.get('name'))
to_schema, to_table, node.get('alias'))

def call_already_exists(schema, table):
return adapter.already_exists(
profile, project, schema, table, node.get('name'))
profile, project, schema, table, node.get('alias'))

return {
"run_started_at": dbt.tracking.active_user.run_started_at,
Expand Down Expand Up @@ -385,8 +385,7 @@ def after_hooks(cls, project, adapter, results, flat_graph, elapsed):
def describe_node(self):
materialization = dbt.utils.get_materialization(self.node)
schema_name = self.node.get('schema')
node_name = self.node.get('name')

node_name = dbt.utils.get_alias(self.node)
return "{} model {}.{}".format(materialization, schema_name, node_name)

def print_start_line(self):
Expand Down
2 changes: 1 addition & 1 deletion dbt/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def parse_node(node, node_path, root_project_config, package_project_config,
profile = dbt.utils.get_profile_from_project(root_project_config)
default_schema = profile.get('schema', 'public')
node['schema'] = default_schema

node['alias'] = dbt.utils.get_alias(node)
context = dbt.context.parser.generate(node, root_project_config,
{"macros": macros})

Expand Down
4 changes: 2 additions & 2 deletions dbt/ui/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def print_model_result_line(result, schema_name, index, total):
info=info,
model_type=get_materialization(model),
schema=schema_name,
relation=model.get('name')),
relation=model.get('alias')),
status,
index,
total,
Expand Down Expand Up @@ -187,7 +187,7 @@ def print_seed_result_line(result, schema_name, index, total):
"{info} seed file {schema}.{relation}".format(
info=info,
schema=schema_name,
relation=model.get('name')),
relation=model.get('alias')),
status,
index,
total,
Expand Down
9 changes: 7 additions & 2 deletions dbt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@


DBTConfigKeys = [
'alias',
'schema',
'enabled',
'materialized',
Expand Down Expand Up @@ -64,7 +65,7 @@ def get_model_name_or_none(model):
elif isinstance(model, basestring):
name = model
elif isinstance(model, dict):
name = model.get('name')
name = get_alias(model)
else:
name = model.nice_name
return name
Expand All @@ -86,7 +87,7 @@ def model_immediate_name(model, non_destructive):
seeds.
"""

model_name = model.get('name')
model_name = get_alias(model)
is_incremental = (get_materialization(model) == 'incremental')
is_seed = is_type(model, 'seed')

Expand Down Expand Up @@ -298,6 +299,10 @@ def get_materialization(node):
return node.get('config', {}).get('materialized')


def get_alias(node):
return node.get('config', {}).get('alias', node.get('name'))


def is_enabled(node):
return node.get('config', {}).get('enabled') is True

Expand Down
10 changes: 10 additions & 0 deletions test/integration/026_aliases_test/models/foo_alias.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

{{
config(
alias='foo',
materialized='table'
)
}}

SELECT
'{{ this.alias }}' as "tablename"
18 changes: 18 additions & 0 deletions test/integration/026_aliases_test/models/ref_foo_alias.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

{{
config(
materialized='table'
)
}}

WITH trigger_ref AS (
SELECT
*
FROM
-- we should still be able to ref a model by its filepath
{{ ref('foo_alias') }}
)

SELECT
-- this name should still be the filename
'{{ this.alias }}' as "tablename"
60 changes: 60 additions & 0 deletions test/integration/026_aliases_test/test_aliases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from nose.plugins.attrib import attr
from test.integration.base import DBTIntegrationTest


class TestAliases(DBTIntegrationTest):

def setUp(self):
DBTIntegrationTest.setUp(self)

@property
def schema(self):
return "aliases_026"

@property
def models(self):
return "test/integration/026_aliases_test/models"

@property
def profile_config(self):
return {
'test': {
'outputs': {
'dev': {
'type': 'postgres',
'threads': 1,
'host': 'database',
'port': 5432,
'user': "root",
'pass': "password",
'dbname': 'dbt',
'schema': self.unique_schema()
},
},
'target': 'dev'
}
}

@property
def query_foo_alias(self):
return """
select
tablename
from {schema}.foo
""".format(schema=self.unique_schema())

@property
def query_ref_foo_alias(self):
return """
select
tablename
from {schema}.ref_foo_alias
""".format(schema=self.unique_schema())

@attr(type='postgres')
def test__alias_model_name(self):
self.run_dbt(['run'])
result = self.run_sql(self.query_foo_alias, fetch='all')[0][0]
self.assertEqual(result, 'foo')
result = self.run_sql(self.query_ref_foo_alias, fetch='all')[0][0]
self.assertEqual(result, 'ref_foo_alias')
Loading

0 comments on commit ca7992b

Please sign in to comment.