Skip to content

Commit 7bf8e84

Browse files
cpcloudgforsyth
authored andcommitted
fix(snowflake): fix quoting across all apis
1 parent 9b65b48 commit 7bf8e84

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

ibis/backends/base/sql/alchemy/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,10 @@ def _get_table_identifier(self, *, name, namespace):
991991
db=schema,
992992
catalog=db,
993993
quoted=self.compiler.translator_class._quote_table_names,
994+
).transform(
995+
lambda node: node.__class__(this=node.this, quoted=True)
996+
if isinstance(node, sg.exp.Identifier)
997+
else node
994998
)
995999
return table
9961000

@@ -1015,7 +1019,9 @@ def _get_sqla_table(
10151019
def drop_table(
10161020
self, name: str, database: str | None = None, force: bool = False
10171021
) -> None:
1018-
table = sg.table(name, db=database)
1022+
table = sg.table(
1023+
name, db=database, quoted=self.compiler.translator_class._quote_table_names
1024+
)
10191025
drop_table = sg.exp.Drop(kind="TABLE", exists=force, this=table)
10201026
drop_table_sql = drop_table.sql(dialect=self.name)
10211027
with self.begin() as con:

ibis/backends/snowflake/__init__.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -551,47 +551,48 @@ def _get_temp_view_definition(
551551
yield f"CREATE OR REPLACE TEMPORARY VIEW {name} AS {definition}"
552552

553553
def create_database(self, name: str, force: bool = False) -> None:
554-
current_database = self.current_database
555-
current_schema = self.current_schema
556-
name = self._quote(name)
557-
if_not_exists = "IF NOT EXISTS " * force
554+
create_stmt = sg.exp.Create(
555+
kind="DATABASE", this=sg.to_identifier(name, quoted=True), exists=force
556+
).sql(self.name)
557+
current_ident = sg.table(
558+
self.current_schema, db=self.current_database, quoted=True
559+
).sql(self.name)
558560
with self.begin() as con:
559-
con.exec_driver_sql(f"CREATE DATABASE {if_not_exists}{name}")
561+
con.exec_driver_sql(create_stmt)
560562
# Snowflake automatically switches to the new database after creating
561563
# it per
562564
# https://docs.snowflake.com/en/sql-reference/sql/create-database#general-usage-notes
563565
# so we switch back to the original database and schema
564-
con.exec_driver_sql(
565-
f"USE SCHEMA {self._quote(current_database)}.{self._quote(current_schema)}"
566-
)
566+
con.exec_driver_sql(f"USE SCHEMA {current_ident}")
567567

568568
def drop_database(self, name: str, force: bool = False) -> None:
569569
current_database = self.current_database
570570
if name == current_database:
571571
raise com.UnsupportedOperationError(
572572
"Dropping the current database is not supported because its behavior is undefined"
573573
)
574-
name = self._quote(name)
575-
if_exists = "IF EXISTS " * force
574+
drop_stmt = sg.exp.Drop(
575+
kind="DATABASE", this=sg.to_identifier(name, quoted=True), exists=force
576+
).sql(self.name)
576577
with self.begin() as con:
577-
con.exec_driver_sql(f"DROP DATABASE {if_exists}{name}")
578+
con.exec_driver_sql(drop_stmt)
578579

579580
def create_schema(
580581
self, name: str, database: str | None = None, force: bool = False
581582
) -> None:
582-
name = ".".join(map(self._quote, filter(None, [database, name])))
583-
if_not_exists = "IF NOT EXISTS " * force
584-
current_database = self.current_database
585-
current_schema = self.current_schema
583+
create_stmt = sg.exp.Create(
584+
kind="SCHEMA", this=sg.table(name, db=database, quoted=True), exists=force
585+
).sql(self.name)
586+
current_ident = sg.table(
587+
self.current_schema, db=self.current_database, quoted=True
588+
).sql(self.name)
586589
with self.begin() as con:
587-
con.exec_driver_sql(f"CREATE SCHEMA {if_not_exists}{name}")
590+
con.exec_driver_sql(create_stmt)
588591
# Snowflake automatically switches to the new schema after creating
589592
# it per
590593
# https://docs.snowflake.com/en/sql-reference/sql/create-schema#usage-notes
591594
# so we switch back to the original schema
592-
con.exec_driver_sql(
593-
f"USE SCHEMA {self._quote(current_database)}.{self._quote(current_schema)}"
594-
)
595+
con.exec_driver_sql(f"USE SCHEMA {current_ident}")
595596

596597
def drop_schema(
597598
self, name: str, database: str | None = None, force: bool = False
@@ -603,10 +604,11 @@ def drop_schema(
603604
"Dropping the current schema is not supported because its behavior is undefined"
604605
)
605606

606-
name = ".".join(map(self._quote, filter(None, [database, name])))
607-
if_exists = "IF EXISTS " * force
607+
drop_stmt = sg.exp.Drop(
608+
kind="SCHEMA", this=sg.table(name, db=database, quoted=True), exists=force
609+
).sql(self.name)
608610
with self.begin() as con:
609-
con.exec_driver_sql(f"DROP SCHEMA {if_exists}{name}")
611+
con.exec_driver_sql(drop_stmt)
610612

611613
def create_table(
612614
self,

0 commit comments

Comments
 (0)