116116)
117117from sqlbuild .adapters .snowflake ._helpers .show_metadata import (
118118 is_missing_object_error ,
119- is_missing_schema_error ,
120119 listed_relation_from_show_table ,
121120 listed_relation_from_show_view ,
122121)
@@ -1817,14 +1816,23 @@ def read_schema_relation_listing(
18171816 ) -> SchemaRelationListing :
18181817 """List one schema's tables and views with SHOW, which needs no running warehouse."""
18191818
1819+ show_database : str = self ._show_database (
1820+ connection = connection , database = database , schema = schema
1821+ )
18201822 scope : str = self ._show_schema_scope (
18211823 connection = connection , database = database , schema = schema
18221824 )
18231825 tables : list [dict [str , object ]] = self ._fetch_paged_show_rows (
1824- connection = connection , query = f"SHOW TABLES IN SCHEMA { scope } "
1826+ connection = connection ,
1827+ query = f"SHOW TABLES IN SCHEMA { scope } " ,
1828+ show_database = show_database ,
1829+ schema = schema ,
18251830 )
18261831 views : list [dict [str , object ]] = self ._fetch_paged_show_rows (
1827- connection = connection , query = f"SHOW VIEWS IN SCHEMA { scope } "
1832+ connection = connection ,
1833+ query = f"SHOW VIEWS IN SCHEMA { scope } " ,
1834+ show_database = show_database ,
1835+ schema = schema ,
18281836 )
18291837 entries : dict [str , ListedRelation ] = {}
18301838 row : dict [str , object ]
@@ -1853,8 +1861,13 @@ def read_schema_column_listing(
18531861 scope : str = self ._show_schema_scope (
18541862 connection = connection , database = database , schema = schema
18551863 )
1856- rows : list [dict [str , object ]] = self ._fetch_show_rows (
1857- connection = connection , query = f"SHOW COLUMNS IN SCHEMA { scope } "
1864+ rows : list [dict [str , object ]] = self ._fetch_schema_show_rows (
1865+ connection = connection ,
1866+ query = f"SHOW COLUMNS IN SCHEMA { scope } " ,
1867+ show_database = self ._show_database (
1868+ connection = connection , database = database , schema = schema
1869+ ),
1870+ schema = schema ,
18581871 )
18591872 if len (rows ) != _SHOW_RESULT_LIMIT :
18601873 columns : dict [str , list [ColumnInfo ]] = {}
@@ -1955,14 +1968,20 @@ def _show_schema_scope(
19551968 ) -> str :
19561969 """Qualify SHOW with the database so a missing one is reported, never listed empty."""
19571970
1971+ resolved : str = self ._show_database (connection = connection , database = database , schema = schema )
1972+ return f"{ self .render_identifier (resolved )} .{ self .render_identifier (schema )} "
1973+
1974+ def _show_database (
1975+ self , * , connection : _SnowflakeConnection , database : str | None , schema : str
1976+ ) -> str :
19581977 resolved : str | None = (
19591978 database if database is not None else self ._current_database (connection = connection )
19601979 )
19611980 if resolved is None :
19621981 raise AdapterUserError (
19631982 message = f"Snowflake schema { schema } needs a database; the session has none"
19641983 )
1965- return f" { self . render_identifier ( resolved ) } . { self . render_identifier ( schema ) } "
1984+ return resolved
19661985
19671986 @staticmethod
19681987 def _current_database (* , connection : _SnowflakeConnection ) -> str | None :
@@ -1980,15 +1999,15 @@ def _current_database(*, connection: _SnowflakeConnection) -> str | None:
19801999 return current
19812000
19822001 def _fetch_paged_show_rows (
1983- self , * , connection : _SnowflakeConnection , query : str
2002+ self , * , connection : _SnowflakeConnection , query : str , show_database : str , schema : str
19842003 ) -> list [dict [str , object ]]:
19852004 """Page SHOW output past its row cap; SHOW orders rows by name."""
19862005
19872006 rows : list [dict [str , object ]] = []
19882007 page_query : str = f"{ query } LIMIT { _SHOW_RESULT_LIMIT } "
19892008 while True :
1990- page : list [dict [str , object ]] = self ._fetch_show_rows (
1991- connection = connection , query = page_query
2009+ page : list [dict [str , object ]] = self ._fetch_schema_show_rows (
2010+ connection = connection , query = page_query , show_database = show_database , schema = schema
19922011 )
19932012 rows .extend (page )
19942013 if len (page ) < _SHOW_RESULT_LIMIT :
@@ -2000,24 +2019,80 @@ def _fetch_paged_show_rows(
20002019 def _fetch_show_rows (
20012020 * , connection : _SnowflakeConnection , query : str
20022021 ) -> list [dict [str , object ]]:
2003- """Run one SHOW command; a missing or unauthorized schema lists nothing ."""
2022+ """Run one SHOW command and return its rows keyed by lowercase output column ."""
20042023
20052024 def fetch () -> list [dict [str , object ]]:
20062025 cursor : Any = connection .cursor ()
20072026 try :
2008- try :
2009- cursor .execute (query )
2010- except Exception as error :
2011- if is_missing_schema_error (error ):
2012- return []
2013- raise
2027+ cursor .execute (query )
20142028 names : list [str ] = [str (column [0 ]).lower () for column in cursor .description or ()]
20152029 return [dict (zip (names , row , strict = False )) for row in cursor .fetchall ()]
20162030 finally :
20172031 cursor .close ()
20182032
20192033 return run_recorded_inspection_query (sql = query , run = fetch )
20202034
2035+ def _fetch_schema_show_rows (
2036+ self , * , connection : _SnowflakeConnection , query : str , show_database : str , schema : str
2037+ ) -> list [dict [str , object ]]:
2038+ """Run SHOW ... IN SCHEMA; a schema that does not exist lists nothing, as before."""
2039+
2040+ try :
2041+ return self ._fetch_show_rows (connection = connection , query = query )
2042+ except Exception as error :
2043+ if not is_missing_object_error (error ) or self ._schema_exists (
2044+ connection = connection , show_database = show_database , schema = schema
2045+ ):
2046+ raise
2047+ return []
2048+
2049+ def _schema_exists (
2050+ self , * , connection : _SnowflakeConnection , show_database : str , schema : str
2051+ ) -> bool :
2052+ """Tell a missing schema from a missing database or another failure, once per plan."""
2053+
2054+ def read () -> bool :
2055+ return self ._read_schema_exists (
2056+ connection = connection , show_database = show_database , schema = schema
2057+ )
2058+
2059+ catalog : InspectionCatalog | None = active_inspection_catalog (
2060+ adapter = self , connection = connection
2061+ )
2062+ if catalog is None :
2063+ return read ()
2064+ return catalog .remember (
2065+ key = (
2066+ "snowflake_schema_exists" ,
2067+ self ._information_schema_identifier (show_database ),
2068+ self ._information_schema_identifier (schema ),
2069+ ),
2070+ compute = read ,
2071+ )
2072+
2073+ def _read_schema_exists (
2074+ self , * , connection : _SnowflakeConnection , show_database : str , schema : str
2075+ ) -> bool :
2076+ stored_schema : str = self ._information_schema_identifier (schema )
2077+ query : str = (
2078+ f"SHOW SCHEMAS LIKE '{ stored_schema .replace (chr (39 ), chr (39 ) * 2 )} ' "
2079+ f"IN DATABASE { self .render_identifier (show_database )} "
2080+ )
2081+ try :
2082+ rows : list [dict [str , object ]] = self ._fetch_show_rows (
2083+ connection = connection , query = query
2084+ )
2085+ except Exception as error :
2086+ if not is_missing_object_error (error ):
2087+ raise
2088+ raise AdapterUserError (
2089+ message = (
2090+ f"Snowflake database { show_database } does not exist or the current role "
2091+ f"cannot use it: { error } "
2092+ )
2093+ ) from error
2094+ return any (str (row .get ("name" )) == stored_schema for row in rows )
2095+
20212096 def read_relation_columns (
20222097 self , * , connection : _SnowflakeConnection , relation : RelationInfo
20232098 ) -> tuple [ColumnInfo , ...] | None :
0 commit comments