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 adapter.get_relation to find quoted case-sensitive relations #1277

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20241218-231834.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Fix adapter.get_relation to find quoted case-sensitive relations
time: 2024-12-18T23:18:34.836877+05:30
custom:
Author: prashantpiyush
Issue: "1252"
19 changes: 16 additions & 3 deletions dbt/adapters/snowflake/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,33 @@ def _catalog_filter_table(
return super()._catalog_filter_table(lowered, used_schemas)

def _make_match_kwargs(self, database, schema, identifier):
# if any path part is already quoted then consider same casing but without quotes
quoting = self.config.quoting
if identifier is not None and quoting["identifier"] is False:
if SnowflakeAdapter._is_quoted(identifier):
identifier = identifier.strip(SnowflakeRelation.quote_character)
elif identifier is not None and quoting["identifier"] is False:
identifier = identifier.upper()

if schema is not None and quoting["schema"] is False:
if SnowflakeAdapter._is_quoted(schema):
schema = schema.strip(SnowflakeRelation.quote_character)
elif schema is not None and quoting["schema"] is False:
schema = schema.upper()

if database is not None and quoting["database"] is False:
if SnowflakeAdapter._is_quoted(database):
database = database.strip(SnowflakeRelation.quote_character)
elif database is not None and quoting["database"] is False:
database = database.upper()

return filter_null_values(
{"identifier": identifier, "schema": schema, "database": database}
)

@classmethod
def _is_quoted(cls, identifier: str) -> bool:
return (identifier is not None
and identifier.startswith(SnowflakeRelation.quote_character)
and identifier.endswith(SnowflakeRelation.quote_character))

def _get_warehouse(self) -> str:
_, table = self.execute("select current_warehouse() as warehouse", fetch=True)
if len(table) == 0 or len(table[0]) == 0:
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_snowflake_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,30 @@ def test_authenticator_private_key_string_authentication_no_passphrase(
]
)

def test_get_relation_without_quotes(self):
with mock.patch.object(self.adapter, "list_relations") as list_relations:
list_relations.return_value = [
SnowflakeAdapter.Relation.create(
database="TEST_DATABASE",
schema="test_schema",
identifier="TEST_TABLE"
)
]
relation = self.adapter.get_relation("test_database", "test_schema", "test_table")
assert relation.render() == "TEST_DATABASE.test_schema.TEST_TABLE"

def test_get_relation_with_quotes(self):
with mock.patch.object(self.adapter, "list_relations") as list_relations:
list_relations.return_value = [
SnowflakeAdapter.Relation.create(
database="test_database",
schema="test_schema",
identifier="test_TABLE"
)
]
relation = self.adapter.get_relation("\"test_database\"", "test_schema", "\"test_TABLE\"")
assert relation.render() == "test_database.test_schema.test_TABLE"


class TestSnowflakeAdapterConversions(TestAdapterConversions):
def test_convert_text_type(self):
Expand Down