diff --git a/core/dbt/adapters/base/relation.py b/core/dbt/adapters/base/relation.py index 443b3a4c35a..d6bef95c57c 100644 --- a/core/dbt/adapters/base/relation.py +++ b/core/dbt/adapters/base/relation.py @@ -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 = { @@ -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, @@ -98,7 +106,7 @@ 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(): @@ -106,7 +114,8 @@ def matches(self, database=None, schema=None, identifier=None): 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 diff --git a/core/dbt/node_runners.py b/core/dbt/node_runners.py index 6c3875b11db..a467a5aa24a 100644 --- a/core/dbt/node_runners.py +++ b/core/dbt/node_runners.py @@ -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') diff --git a/plugins/bigquery/dbt/adapters/bigquery/relation.py b/plugins/bigquery/dbt/adapters/bigquery/relation.py index e4f982b2cbe..066eab8a1b2 100644 --- a/plugins/bigquery/dbt/adapters/bigquery/relation.py +++ b/plugins/bigquery/dbt/adapters/bigquery/relation.py @@ -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 = { @@ -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): @@ -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 diff --git a/plugins/snowflake/dbt/adapters/snowflake/relation.py b/plugins/snowflake/dbt/adapters/snowflake/relation.py index e89b71d89cf..0532e0ffb11 100644 --- a/plugins/snowflake/dbt/adapters/snowflake/relation.py +++ b/plugins/snowflake/dbt/adapters/snowflake/relation.py @@ -16,7 +16,8 @@ class SnowflakeRelation(BaseRelation): 'database': True, 'schema': True, 'identifier': True, - } + }, + 'dbt_created': False, } SCHEMA = { @@ -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 diff --git a/test/integration/001_simple_copy_test/models/get_and_ref.sql b/test/integration/001_simple_copy_test/models/get_and_ref.sql new file mode 100644 index 00000000000..89d55c7381e --- /dev/null +++ b/test/integration/001_simple_copy_test/models/get_and_ref.sql @@ -0,0 +1,3 @@ +{%- do adapter.get_relation(database=target.database, schema=target.schema, identifier='materialized') -%} + +select * from {{ ref('materialized') }} diff --git a/test/integration/001_simple_copy_test/test_simple_copy.py b/test/integration/001_simple_copy_test/test_simple_copy.py index 89ecfa1e585..b66fed57007 100644 --- a/test/integration/001_simple_copy_test/test_simple_copy.py +++ b/test/integration/001_simple_copy_test/test_simple_copy.py @@ -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): @@ -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() @@ -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) @@ -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")], @@ -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")], @@ -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")], @@ -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): @@ -186,9 +188,9 @@ 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")], @@ -196,9 +198,9 @@ 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"]) # can't run the test as this one's identifiers will be the wrong case @@ -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")],