Skip to content

Commit 7a73468

Browse files
stop setting quoted_identifiers_ignore_case (#1159)
1 parent 960da40 commit 7a73468

File tree

4 files changed

+34
-137
lines changed

4 files changed

+34
-137
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
kind: Under the Hood
2+
body: remove reliance on setting session `quoted_identifiers_ignore_case` parameter
3+
to normalize snowflake metadata queries
4+
time: 2025-06-16T15:52:09.002616-07:00
5+
custom:
6+
Author: colin-rogers-dbt
7+
Issue: "1159"

dbt-snowflake/src/dbt/adapters/snowflake/impl.py

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ def list_relations_without_caching(
276276
raise
277277

278278
columns = ["database_name", "schema_name", "name", "kind", "is_dynamic", "is_iceberg"]
279-
279+
schema_objects = schema_objects.rename(
280+
column_names=[col.lower() for col in schema_objects.column_names]
281+
)
280282
return [self._parse_list_relations_result(obj) for obj in schema_objects.select(columns)]
281283

282284
def _parse_list_relations_result(self, result: "agate.Row") -> SnowflakeRelation:
@@ -482,58 +484,34 @@ def build_catalog_relation(self, model: RelationConfig) -> Optional[CatalogRelat
482484
return None
483485

484486
@available
485-
def describe_dynamic_table(self, relation: RelationConfig) -> Dict[str, Any]:
487+
def describe_dynamic_table(self, relation: SnowflakeRelation) -> Dict[str, Any]:
486488
"""
487489
Get all relevant metadata about a dynamic table to return as a dict to Agate Table row
488490
489491
Args:
490492
relation (SnowflakeRelation): the relation to describe
491493
"""
492-
493-
original_val: Optional[str] = None
494-
try:
495-
# Store old QUOTED_IDENTIFIERS_IGNORE_CASE
496-
show_param_sql = "show parameters like 'QUOTED_IDENTIFIERS_IGNORE_CASE' in SESSION"
497-
show_param_res = self.execute(show_param_sql)
498-
if show_param_res:
499-
param_qid = show_param_res[0].query_id
500-
scan_param_sql = f"SELECT * FROM TABLE(RESULT_SCAN('{param_qid}'))"
501-
param_scan_res, rows = self.execute(scan_param_sql, fetch=True)
502-
if param_scan_res and param_scan_res.code == "SUCCESS":
503-
try:
504-
original_val = rows[0][1]
505-
except (IndexError, TypeError):
506-
original_val = None
507-
508-
# falsify QUOTED_IDENTIFIERS_IGNORE_CASE for execution only
509-
self.execute("alter session set QUOTED_IDENTIFIERS_IGNORE_CASE = FALSE", fetch=False)
510-
511-
show_sql = (
512-
f"show dynamic tables like '{relation.identifier}' "
513-
f"in schema {relation.database}.{relation.schema}"
494+
quoting = relation.quote_policy
495+
schema = f'"{relation.schema}"' if quoting.schema else relation.schema
496+
database = f'"{relation.database}"' if quoting.database else relation.database
497+
show_sql = (
498+
f"show dynamic tables like '{relation.identifier}' in schema {database}.{schema}"
499+
)
500+
res, dt_table = self.execute(show_sql, fetch=True)
501+
if res.code != "SUCCESS":
502+
raise DbtRuntimeError(f"Could not get dynamic query metadata: {show_sql} failed")
503+
# normalize column names to lower case, this still preserves column order
504+
dt_table = dt_table.rename(column_names=[name.lower() for name in dt_table.column_names])
505+
return {
506+
"dynamic_table": dt_table.select(
507+
[
508+
"name",
509+
"schema_name",
510+
"database_name",
511+
"text",
512+
"target_lag",
513+
"warehouse",
514+
"refresh_mode",
515+
]
514516
)
515-
show_res = self.execute(show_sql)
516-
if show_res:
517-
query_id = show_res[0].query_id
518-
scan_sql = (
519-
"select "
520-
' "name", "schema_name", "database_name", "text", "target_lag", "warehouse", '
521-
' "refresh_mode" '
522-
f"from TABLE(RESULT_SCAN('{query_id}'))"
523-
)
524-
res, dt_table = self.execute(scan_sql, fetch=True)
525-
if res.code != "SUCCESS":
526-
raise DbtRuntimeError(
527-
f"Could not get dynamic query metadata: {scan_sql} failed"
528-
)
529-
return {"dynamic_table": dt_table}
530-
531-
return {"dynamic_table": None}
532-
finally:
533-
if original_val is None:
534-
self.execute("ALTER SESSION UNSET QUOTED_IDENTIFIERS_IGNORE_CASE", fetch=False)
535-
else:
536-
bool_str = "TRUE" if original_val.strip().lower() == "true" else "FALSE"
537-
self.execute(
538-
f"ALTER SESSION SET QUOTED_IDENTIFIERS_IGNORE_CASE = {bool_str}", fetch=False
539-
)
517+
}

dbt-snowflake/src/dbt/include/snowflake/macros/metadata/list_relations_without_caching.sql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
{%- set paginated_state = namespace(paginated_results=[], watermark=none) -%}
1919

20-
{%- do run_query('alter session set quoted_identifiers_ignore_case = false;') -%}
21-
2220
{#-
2321
loop an extra time to catch the breach of max iterations
2422
Note: while range is 0-based, loop.index starts at 1
@@ -46,8 +44,6 @@
4644

4745
{%- endfor -%}
4846

49-
{%- do run_query('alter session unset quoted_identifiers_ignore_case;') -%}
50-
5147
{#- grab the first table in the paginated results to access the `merge` method -#}
5248
{%- set agate_table = paginated_state.paginated_results[0] -%}
5349
{%- do return(agate_table.merge(paginated_state.paginated_results)) -%}

dbt-snowflake/tests/functional/adapter/test_quoted_identifiers_flag.py

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)