@@ -109,11 +109,48 @@ def query_external_table_schema(self, path: DbPath) -> Dict[str, tuple]:
109109 assert len (d ) == len (rows )
110110 return d
111111
112+ def select_view_columns (self , path : DbPath ) -> str :
113+ _ , schema , table = self ._normalize_table_path (path )
114+
115+ return (
116+ """select * from pg_get_cols('{}.{}')
117+ cols(view_schema name, view_name name, col_name name, col_type varchar, col_num int)
118+ """ .format (schema , table )
119+ )
120+
121+ def query_pg_get_cols (self , path : DbPath ) -> Dict [str , tuple ]:
122+ rows = self .query (self .select_view_columns (path ), list )
123+
124+ if not rows :
125+ raise RuntimeError (f"{ self .name } : View '{ '.' .join (path )} ' does not exist, or has no columns" )
126+
127+ output = {}
128+ for r in rows :
129+ col_name = r [2 ]
130+ type_info = r [3 ].split ('(' )
131+ base_type = type_info [0 ]
132+ precision = None
133+ scale = None
134+
135+ if len (type_info ) > 1 :
136+ if base_type == 'numeric' :
137+ precision , scale = type_info [1 ][:- 1 ].split (',' )
138+ precision = int (precision )
139+ scale = int (scale )
140+
141+ out = [col_name , base_type , None , precision , scale ]
142+ output [col_name ] = tuple (out )
143+
144+ return output
145+
112146 def query_table_schema (self , path : DbPath ) -> Dict [str , tuple ]:
113147 try :
114148 return super ().query_table_schema (path )
115149 except RuntimeError :
116- return self .query_external_table_schema (path )
150+ try :
151+ return self .query_external_table_schema (path )
152+ except RuntimeError :
153+ return self .query_pg_get_cols ()
117154
118155 def _normalize_table_path (self , path : DbPath ) -> DbPath :
119156 if len (path ) == 1 :
0 commit comments