@@ -64,17 +64,26 @@ class Redshift(PostgreSQL):
6464 CONNECT_URI_PARAMS = ["database?" ]
6565
6666 def select_table_schema (self , path : DbPath ) -> str :
67- schema , table = self ._normalize_table_path (path )
67+ database , schema , table = self ._normalize_table_path (path )
68+
69+ info_schema_path = ["information_schema" , "columns" ]
70+ if database :
71+ info_schema_path .insert (0 , database )
6872
6973 return (
70- "SELECT column_name, data_type, datetime_precision, numeric_precision, numeric_scale FROM information_schema.columns "
74+ f "SELECT column_name, data_type, datetime_precision, numeric_precision, numeric_scale FROM { '.' . join ( info_schema_path ) } "
7175 f"WHERE table_name = '{ table .lower ()} ' AND table_schema = '{ schema .lower ()} '"
7276 )
7377
7478 def select_external_table_schema (self , path : DbPath ) -> str :
75- schema , table = self ._normalize_table_path (path )
79+ database , schema , table = self ._normalize_table_path (path )
80+
81+ db_clause = ""
82+ if database :
83+ db_clause = f" AND redshift_database_name = '{ database .lower ()} '"
7684
77- return f"""SELECT
85+ return (
86+ f"""SELECT
7887 columnname AS column_name
7988 , CASE WHEN external_type = 'string' THEN 'varchar' ELSE external_type END AS data_type
8089 , NULL AS datetime_precision
@@ -83,6 +92,8 @@ def select_external_table_schema(self, path: DbPath) -> str:
8392 FROM svv_external_columns
8493 WHERE tablename = '{ table .lower ()} ' AND schemaname = '{ schema .lower ()} '
8594 """
95+ + db_clause
96+ )
8697
8798 def query_external_table_schema (self , path : DbPath ) -> Dict [str , tuple ]:
8899 rows = self .query (self .select_external_table_schema (path ), list )
@@ -98,3 +109,15 @@ def query_table_schema(self, path: DbPath) -> Dict[str, tuple]:
98109 return super ().query_table_schema (path )
99110 except RuntimeError :
100111 return self .query_external_table_schema (path )
112+
113+ def _normalize_table_path (self , path : DbPath ) -> DbPath :
114+ if len (path ) == 1 :
115+ return None , self .default_schema , path [0 ]
116+ elif len (path ) == 2 :
117+ return None , path [0 ], path [1 ]
118+ elif len (path ) == 3 :
119+ return path
120+
121+ raise ValueError (
122+ f"{ self .name } : Bad table path for { self } : '{ '.' .join (path )} '. Expected format: table, schema.table, or database.schema.table"
123+ )
0 commit comments