Skip to content

Commit 708a315

Browse files
committed
Fix: Normalize before applying quotes in get_relation and list_relations methods of the runtime dbt adapter
1 parent b3b1d52 commit 708a315

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

sqlmesh/dbt/adapter.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,11 @@ def __init__(
245245
def get_relation(
246246
self, database: t.Optional[str], schema: str, identifier: str
247247
) -> t.Optional[BaseRelation]:
248-
return self.load_relation(
249-
self.relation_type.create(
250-
database=database,
251-
schema=schema,
252-
identifier=identifier,
253-
quote_policy=self.quote_policy,
254-
)
255-
)
248+
target_table = exp.table_(identifier, db=schema, catalog=database)
249+
# Normalize before converting to a relation; otherwise, it will be too late,
250+
# as quotes will have already been applied.
251+
target_table = self._normalize(target_table)
252+
return self.load_relation(self._table_to_relation(target_table))
256253

257254
def load_relation(self, relation: BaseRelation) -> t.Optional[BaseRelation]:
258255
mapped_table = self._map_table_name(self._normalize(self._relation_to_table(relation)))
@@ -262,10 +259,15 @@ def load_relation(self, relation: BaseRelation) -> t.Optional[BaseRelation]:
262259
return self._table_to_relation(mapped_table)
263260

264261
def list_relations(self, database: t.Optional[str], schema: str) -> t.List[BaseRelation]:
265-
reference_relation = self.relation_type.create(
266-
database=database, schema=schema, quote_policy=self.quote_policy
262+
target_schema = exp.Table(
263+
this=None,
264+
db=exp.to_identifier(schema),
265+
catalog=exp.to_identifier(database) if database else None,
267266
)
268-
return self.list_relations_without_caching(reference_relation)
267+
# Normalize before converting to a relation; otherwise, it will be too late,
268+
# as quotes will have already been applied.
269+
target_schema = self._normalize(target_schema)
270+
return self.list_relations_without_caching(self._table_to_relation(target_schema))
269271

270272
def list_relations_without_caching(self, schema_relation: BaseRelation) -> t.List[BaseRelation]:
271273
from sqlmesh.dbt.relation import RelationType

tests/dbt/test_adapter.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,35 @@ def test_on_run_start_end(copy_to_temp_path):
379379
"CREATE OR REPLACE TABLE schema_table_sushi__dev_nested_package AS SELECT 'sushi__dev' AS schema",
380380
]
381381
)
382+
383+
384+
def test_adapter_get_relation_normalization(
385+
sushi_test_project: Project, runtime_renderer: t.Callable
386+
):
387+
# Simulate that the quote policy is set to quote everything to make
388+
# sure that we normalize correctly even if quotes are applied
389+
with mock.patch.object(
390+
SnowflakeConfig,
391+
"quote_policy",
392+
Policy(identifier=True, schema=True, database=True),
393+
):
394+
context = sushi_test_project.context
395+
assert context.target
396+
engine_adapter = context.target.to_sqlmesh().create_engine_adapter()
397+
engine_adapter._default_catalog = '"memory"'
398+
renderer = runtime_renderer(context, engine_adapter=engine_adapter, dialect="snowflake")
399+
400+
engine_adapter.create_schema('"FOO"')
401+
engine_adapter.create_table(
402+
table_name='"FOO"."BAR"', columns_to_types={"baz": exp.DataType.build("int")}
403+
)
404+
405+
assert (
406+
renderer("{{ adapter.get_relation(database=None, schema='foo', identifier='bar') }}")
407+
== '"memory"."FOO"."BAR"'
408+
)
409+
410+
assert (
411+
renderer("{{ adapter.list_relations(database=None, schema='foo') }}")
412+
== '[<SnowflakeRelation "memory"."FOO"."BAR">]'
413+
)

0 commit comments

Comments
 (0)