diff --git a/.changes/unreleased/Fixes-20240705-165932.yaml b/.changes/unreleased/Fixes-20240705-165932.yaml new file mode 100644 index 000000000..ffe902c92 --- /dev/null +++ b/.changes/unreleased/Fixes-20240705-165932.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Use show ... starts with instead of show ... like in _show_object_metadata +time: 2024-07-05T16:59:32.087555+01:00 +custom: + Author: aranke + Issue: "1102" diff --git a/dbt/adapters/snowflake/impl.py b/dbt/adapters/snowflake/impl.py index 092510e8a..6854b199d 100644 --- a/dbt/adapters/snowflake/impl.py +++ b/dbt/adapters/snowflake/impl.py @@ -156,7 +156,7 @@ def _show_object_metadata(self, relation: SnowflakeRelation) -> Optional[dict]: def get_catalog_for_single_relation( self, relation: SnowflakeRelation ) -> Optional[CatalogTable]: - object_metadata = self._show_object_metadata(relation) + object_metadata = self._show_object_metadata(relation.as_case_sensitive()) if not object_metadata: return None diff --git a/dbt/adapters/snowflake/relation.py b/dbt/adapters/snowflake/relation.py index f477265f0..ace85695b 100644 --- a/dbt/adapters/snowflake/relation.py +++ b/dbt/adapters/snowflake/relation.py @@ -2,12 +2,12 @@ from typing import FrozenSet, Optional, Type from dbt.adapters.base.relation import BaseRelation +from dbt.adapters.contracts.relation import ComponentName, RelationConfig from dbt.adapters.relation_configs import ( RelationConfigBase, RelationConfigChangeAction, RelationResults, ) -from dbt.adapters.contracts.relation import RelationConfig from dbt.adapters.utils import classproperty from dbt_common.exceptions import DbtRuntimeError @@ -106,3 +106,17 @@ def dynamic_table_config_changeset( if config_change_collection.has_changes: return config_change_collection return None + + def as_case_sensitive(self) -> "SnowflakeRelation": + path_part_map = {} + + for path in ComponentName: + if self.include_policy.get_part(path): + part = self.path.get_part(path) + if part: + if self.quote_policy.get_part(path): + path_part_map[path] = part + else: + path_part_map[path] = part.upper() + + return self.replace_path(**path_part_map) diff --git a/dbt/include/snowflake/macros/adapters.sql b/dbt/include/snowflake/macros/adapters.sql index 177720486..4cb4bcffa 100644 --- a/dbt/include/snowflake/macros/adapters.sql +++ b/dbt/include/snowflake/macros/adapters.sql @@ -49,7 +49,7 @@ {% macro snowflake__show_object_metadata(relation) %} {%- set sql -%} - show objects like '{{ relation.identifier }}' in {{ relation.include(identifier=False) }} limit 1 + show objects in {{ relation.include(identifier=False) }} starts with '{{ relation.identifier }}' limit 1 {%- endset -%} {%- set result = run_query(sql) -%} diff --git a/tests/unit/test_relation_as_case_sensitive.py b/tests/unit/test_relation_as_case_sensitive.py new file mode 100644 index 000000000..f362d66b3 --- /dev/null +++ b/tests/unit/test_relation_as_case_sensitive.py @@ -0,0 +1,19 @@ +from dbt.adapters.snowflake.relation import SnowflakeRelation +from dbt.adapters.snowflake.relation_configs import SnowflakeQuotePolicy + + +def test_relation_as_case_sensitive_quoting_true(): + relation = SnowflakeRelation.create( + database="My_Db", + schema="My_ScHeMa", + identifier="My_TaBlE", + quote_policy=SnowflakeQuotePolicy(database=False, schema=True, identifier=False), + ) + + case_sensitive_relation = relation.as_case_sensitive() + case_sensitive_relation.render_limited() + + assert case_sensitive_relation.database == "MY_DB" + assert case_sensitive_relation.schema == "My_ScHeMa" + assert case_sensitive_relation.identifier == "MY_TABLE" + assert case_sensitive_relation.render() == 'MY_DB."My_ScHeMa".MY_TABLE'