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

Fix casing comparisons on dbt-created relations (#1555) #1558

Merged
merged 3 commits into from
Jun 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 14 additions & 5 deletions core/dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ class BaseRelation(APIObject):
'quote_policy': {
'database': True,
'schema': True,
'identifier': True
'identifier': True,
},
'include_policy': {
'database': True,
'schema': True,
'identifier': True
'identifier': True,
},
'dbt_created': False,
}

PATH_SCHEMA = {
Expand Down Expand Up @@ -75,13 +76,20 @@ class BaseRelation(APIObject):
'include_policy': POLICY_SCHEMA,
'quote_policy': POLICY_SCHEMA,
'quote_character': {'type': 'string'},
'dbt_created': {'type': 'boolean'},
},
'required': ['metadata', 'type', 'path', 'include_policy',
'quote_policy', 'quote_character']
'quote_policy', 'quote_character', 'dbt_created']
}

PATH_ELEMENTS = ['database', 'schema', 'identifier']

def _is_exactish_match(self, field, value):
if self.dbt_created and self.quote_policy.get(field) is False:
return self.get_path_part(field).lower() == value.lower()
else:
return self.get_path_part(field) == value

def matches(self, database=None, schema=None, identifier=None):
search = filter_null_values({
'database': database,
Expand All @@ -98,15 +106,16 @@ def matches(self, database=None, schema=None, identifier=None):
approximate_match = True

for k, v in search.items():
if self.get_path_part(k) != v:
if not self._is_exactish_match(k, v):
exact_match = False

if self.get_path_part(k).lower() != v.lower():
approximate_match = False

if approximate_match and not exact_match:
target = self.create(
database=database, schema=schema, identifier=identifier)
database=database, schema=schema, identifier=identifier
)
dbt.exceptions.approximate_relation_match(target, self)

return exact_match
Expand Down
3 changes: 2 additions & 1 deletion core/dbt/node_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ def execute(self, model, manifest):
materialization_macro.generator(context)()

# we must have built a new model, add it to the cache
relation = self.adapter.Relation.create_from_node(self.config, model)
relation = self.adapter.Relation.create_from_node(self.config, model,
dbt_created=True)
self.adapter.cache_new_relation(relation)

result = context['load_result']('main')
Expand Down
12 changes: 7 additions & 5 deletions plugins/bigquery/dbt/adapters/bigquery/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ class BigQueryRelation(BaseRelation):
'quote_policy': {
'database': True,
'schema': True,
'identifier': True
'identifier': True,
},
'include_policy': {
'database': True,
'schema': True,
'identifier': True
}
'identifier': True,
},
'dbt_created': False,
}

SCHEMA = {
Expand All @@ -43,9 +44,10 @@ class BigQueryRelation(BaseRelation):
'include_policy': BaseRelation.POLICY_SCHEMA,
'quote_policy': BaseRelation.POLICY_SCHEMA,
'quote_character': {'type': 'string'},
'dbt_created': {'type': 'boolean'},
},
'required': ['metadata', 'type', 'path', 'include_policy',
'quote_policy', 'quote_character']
'quote_policy', 'quote_character', 'dbt_created']
}

def matches(self, database=None, schema=None, identifier=None):
Expand All @@ -60,7 +62,7 @@ def matches(self, database=None, schema=None, identifier=None):
pass

for k, v in search.items():
if self.get_path_part(k) != v:
if not self._is_exactish_match(k, v):
return False

return True
Expand Down
6 changes: 4 additions & 2 deletions plugins/snowflake/dbt/adapters/snowflake/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class SnowflakeRelation(BaseRelation):
'database': True,
'schema': True,
'identifier': True,
}
},
'dbt_created': False,
}

SCHEMA = {
Expand All @@ -38,9 +39,10 @@ class SnowflakeRelation(BaseRelation):
'include_policy': BaseRelation.POLICY_SCHEMA,
'quote_policy': BaseRelation.POLICY_SCHEMA,
'quote_character': {'type': 'string'},
'dbt_created': {'type': 'boolean'},
},
'required': ['metadata', 'type', 'path', 'include_policy',
'quote_policy', 'quote_character']
'quote_policy', 'quote_character', 'dbt_created']
}

@classmethod
Expand Down
3 changes: 3 additions & 0 deletions test/integration/001_simple_copy_test/models/get_and_ref.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{%- do adapter.get_relation(database=target.database, schema=target.schema, identifier='materialized') -%}

select * from {{ ref('materialized') }}
62 changes: 32 additions & 30 deletions test/integration/001_simple_copy_test/test_simple_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ def test__postgres__simple_copy(self):
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt()
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)

self.assertManyTablesEqual(["seed", "view_model", "incremental", "materialized"])
self.assertManyTablesEqual(["seed", "view_model", "incremental", "materialized", "get_and_ref"])

self.use_default_project({"data-paths": [self.dir("seed-update")]})
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt()
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)

self.assertManyTablesEqual(["seed", "view_model", "incremental", "materialized"])
self.assertManyTablesEqual(["seed", "view_model", "incremental", "materialized", "get_and_ref"])

@use_profile("postgres")
def test__postgres__dbt_doesnt_run_empty_models(self):
Expand All @@ -43,7 +43,7 @@ def test__postgres__dbt_doesnt_run_empty_models(self):
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt()
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)

models = self.get_models_in_schema()

Expand All @@ -57,7 +57,7 @@ def test__presto__simple_copy(self):
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt(expect_pass=False)
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)
for result in results:
if 'incremental' in result.node.name:
self.assertIn('not implemented for presto', result.error)
Expand All @@ -71,17 +71,17 @@ def test__snowflake__simple_copy(self):
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt()
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)

self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED"])
self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED", "GET_AND_REF"])

self.use_default_project({"data-paths": [self.dir("seed-update")]})
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt()
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)

self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED"])
self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED", "GET_AND_REF"])

self.use_default_project({
"test-paths": [self.dir("tests")],
Expand All @@ -99,9 +99,9 @@ def test__snowflake__simple_copy__quoting_off(self):
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt()
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)

self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED"])
self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED", "GET_AND_REF"])

self.use_default_project({
"data-paths": [self.dir("seed-update")],
Expand All @@ -110,9 +110,9 @@ def test__snowflake__simple_copy__quoting_off(self):
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt()
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)

self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED"])
self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED", "GET_AND_REF"])

self.use_default_project({
"test-paths": [self.dir("tests")],
Expand Down Expand Up @@ -150,22 +150,24 @@ def test__bigquery__simple_copy(self):
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt()
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)

self.assertTablesEqual("seed","view_model")
self.assertTablesEqual("seed","incremental")
self.assertTablesEqual("seed","materialized")
self.assertTablesEqual("seed", "view_model")
self.assertTablesEqual("seed", "incremental")
self.assertTablesEqual("seed", "materialized")
self.assertTablesEqual("seed", "get_and_ref")

self.use_default_project({"data-paths": [self.dir("seed-update")]})

results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt()
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)

self.assertTablesEqual("seed","view_model")
self.assertTablesEqual("seed","incremental")
self.assertTablesEqual("seed","materialized")
self.assertTablesEqual("seed", "view_model")
self.assertTablesEqual("seed", "incremental")
self.assertTablesEqual("seed", "materialized")
self.assertTablesEqual("seed", "get_and_ref")


class TestSimpleCopyQuotingIdentifierOn(BaseTestSimpleCopy):
Expand All @@ -186,19 +188,19 @@ def test__snowflake__simple_copy__quoting_on(self):
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt()
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)

self.assertManyTablesEqual(["seed", "view_model", "incremental", "materialized"])
self.assertManyTablesEqual(["seed", "view_model", "incremental", "materialized", "get_and_ref"])

self.use_default_project({
"data-paths": [self.dir("seed-update")],
})
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt()
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)

self.assertManyTablesEqual(["seed", "view_model", "incremental", "materialized"])
self.assertManyTablesEqual(["seed", "view_model", "incremental", "materialized", "get_and_ref"])

# can't run the test as this one's identifiers will be the wrong case

Expand All @@ -218,18 +220,18 @@ def test__snowflake__simple_copy(self):
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt()
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)

self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED"])
self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED", "GET_AND_REF"])

self.use_default_project({"data-paths": [self.dir("seed-update")]})

results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
results = self.run_dbt()
self.assertEqual(len(results), 6)
self.assertEqual(len(results), 7)

self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED"])
self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED", "GET_AND_REF"])

self.use_default_project({
"test-paths": [self.dir("tests")],
Expand Down