Skip to content

Commit

Permalink
refactor(snowflake): remove unnecessary schema setting
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored and kszucs committed Aug 5, 2023
1 parent b85b424 commit 9b0e6c8
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions ibis/backends/snowflake/__init__.py
Expand Up @@ -224,17 +224,16 @@ def do_connect(
@sa.event.listens_for(engine, "connect")
def connect(dbapi_connection, connection_record):
"""Register UDFs on a `"connect"` event."""
dialect = engine.dialect
quote = dialect.preparer(dialect).quote_identifier
with dbapi_connection.cursor() as cur:
database, schema = cur.execute(
"SELECT CURRENT_DATABASE(), CURRENT_SCHEMA()"
).fetchone()
try:
cur.execute("CREATE DATABASE IF NOT EXISTS ibis_udfs")
# snowflake activates a database on creation
cur.execute(f"USE SCHEMA {database}.{schema}")
for name, defn in _SNOWFLAKE_MAP_UDFS.items():
cur.execute(self._make_udf(name, defn))
cur.execute(f"USE SCHEMA {quote(database)}.{quote(schema)}")
except Exception as e: # noqa: BLE001
warnings.warn(
f"Unable to create map UDFs, some functionality will not work: {e}"
Expand Down Expand Up @@ -397,19 +396,14 @@ def _get_sqla_table(
schema = default_schema
*db, schema = schema.split(".")
db = "".join(db) or database or default_db
ident = ".".join(filter(None, (db, schema)))
if ident:
with self.begin() as con:
con.exec_driver_sql(f"USE {ident}")
ident = ".".join(map(self._quote, filter(None, (db, schema))))
try:
result = super()._get_sqla_table(
name, schema=schema, autoload=autoload, database=db, **kwargs
)
except sa.exc.NoSuchTableError:
raise sa.exc.NoSuchTableError(name)

with self.begin() as con:
con.exec_driver_sql(f"USE {default_db}.{default_schema}")
result.schema = ident
return result

Expand Down

0 comments on commit 9b0e6c8

Please sign in to comment.