@@ -90,22 +90,30 @@ def create_connect_args(self, url: URL) -> Tuple[List[Any], Dict[str, Any]]:
90
90
91
91
def get_columns (self , connection : Connection ,
92
92
table_name : str , schema : str = None , ** kw ) -> List [Dict [str , Any ]]:
93
- full_table = self ._get_full_table (table_name , schema )
94
- try :
95
- rows = self ._get_table_columns (connection , full_table )
96
- columns = []
97
- for row in rows :
98
- columns .append (dict (
99
- name = row .Column ,
100
- type = datatype .parse_sqltype (row .Type ),
101
- nullable = getattr (row , 'Null' , True ),
102
- default = None ,
103
- ))
104
- return columns
105
- except error .TrinoQueryError as e :
106
- if e .error_name in (error .TABLE_NOT_FOUND , error .SCHEMA_NOT_FOUND , error .CATALOG_NOT_FOUND ):
107
- raise exc .NoSuchTableError (full_table ) from e
108
- raise
93
+ if not self .has_table (connection , table_name , schema ):
94
+ raise exc .NoSuchTableError (f"schema={ schema } , table={ table_name } " )
95
+ schema = schema or self ._get_default_schema_name (connection )
96
+ query = dedent ("""
97
+ SELECT
98
+ "column_name",
99
+ "column_default",
100
+ "is_nullable",
101
+ "data_type"
102
+ FROM "information_schema"."columns"
103
+ WHERE "table_schema" = :schema AND "table_name" = :table
104
+ ORDER BY "ordinal_position" ASC
105
+ """ ).strip ()
106
+ res = connection .execute (sql .text (query ), schema = schema , table = table_name )
107
+ columns = []
108
+ for record in res :
109
+ column = dict (
110
+ name = record .column_name ,
111
+ type = datatype .parse_sqltype (record .data_type ),
112
+ nullable = (record .is_nullable or '' ).upper () == 'YES' ,
113
+ default = record .column_default ,
114
+ )
115
+ columns .append (column )
116
+ return columns
109
117
110
118
def get_pk_constraint (self , connection : Connection ,
111
119
table_name : str , schema : str = None , ** kw ) -> Dict [str , Any ]:
@@ -158,7 +166,11 @@ def get_view_definition(self, connection: Connection, view_name: str, schema: st
158
166
res = connection .execute (sql .text (query ))
159
167
return res .first ()[0 ]
160
168
except error .TrinoQueryError as e :
161
- if e .error_name in (error .TABLE_NOT_FOUND , error .SCHEMA_NOT_FOUND , error .CATALOG_NOT_FOUND ):
169
+ if e .error_name in (
170
+ error .TABLE_NOT_FOUND ,
171
+ error .SCHEMA_NOT_FOUND ,
172
+ error .CATALOG_NOT_FOUND ,
173
+ ):
162
174
raise exc .NoSuchTableError (full_view ) from e
163
175
raise
164
176
@@ -186,7 +198,11 @@ def has_schema(self, connection: Connection, schema: str) -> bool:
186
198
res = connection .execute (sql .text (query ))
187
199
return res .first () is not None
188
200
except error .TrinoQueryError as e :
189
- if e .error_name in (error .TABLE_NOT_FOUND , error .SCHEMA_NOT_FOUND , error .CATALOG_NOT_FOUND ):
201
+ if e .error_name in (
202
+ error .TABLE_NOT_FOUND ,
203
+ error .SCHEMA_NOT_FOUND ,
204
+ error .CATALOG_NOT_FOUND ,
205
+ ):
190
206
return False
191
207
raise
192
208
@@ -200,7 +216,12 @@ def has_table(self, connection: Connection,
200
216
res = connection .execute (sql .text (query ))
201
217
return res .first () is not None
202
218
except error .TrinoQueryError as e :
203
- if e .error_name in (error .TABLE_NOT_FOUND , error .SCHEMA_NOT_FOUND , error .CATALOG_NOT_FOUND ):
219
+ if e .error_name in (
220
+ error .TABLE_NOT_FOUND ,
221
+ error .SCHEMA_NOT_FOUND ,
222
+ error .CATALOG_NOT_FOUND ,
223
+ error .MISSING_SCHEMA_NAME ,
224
+ ):
204
225
return False
205
226
raise
206
227
@@ -255,11 +276,6 @@ def get_isolation_level(self, dbapi_conn: trino_dbapi.Connection) -> str:
255
276
"SERIALIZABLE" ]
256
277
return level_names [dbapi_conn .isolation_level ]
257
278
258
- @staticmethod
259
- def _get_table_columns (connection : Connection , full_table : str ):
260
- stmt = sql .text (f'SHOW COLUMNS FROM { full_table } ' )
261
- return connection .execute (stmt )
262
-
263
279
def _get_full_table (self , table_name : str , schema : str = None , quote : bool = True ) -> str :
264
280
table_part = self .identifier_preparer .quote_identifier (table_name ) if quote else table_name
265
281
if schema :
0 commit comments