MCPcopy Create free account
hub / github.com/OpenBMB/BMTools / compute_table_schema

Method compute_table_schema

bmtools/tools/db_diag/utils/database.py:229–276  ·  view source on GitHub ↗

schema: {table_name: [field_name]} :param cursor: :return:

(self)

Source from the content-addressed store, hash-verified

227 return self.pgsql_cost_estimation(sql)
228
229 def compute_table_schema(self):
230 """
231 schema: {table_name: [field_name]}
232 :param cursor:
233 :return:
234 """
235
236 if self.args.dbtype == 'postgresql':
237 # cur_path = os.path.abspath('.')
238 # tpath = cur_path + '/sampled_data/'+dbname+'/schema'
239 sql = 'SELECT table_name FROM information_schema.tables WHERE table_schema = \'public\';'
240 success, res = self.execute_sql(sql)
241 #print("======== tables", res)
242 if success == 1:
243 tables = res
244 schema = {}
245 for table_info in tables:
246 table_name = table_info[0]
247 sql = 'SELECT column_name, data_type FROM information_schema.columns WHERE table_name = \'' + table_name + '\';'
248 success, res = self.execute_sql(sql)
249 #print("======== table columns", res)
250 columns = res
251 schema[table_name] = []
252 for col in columns:
253
254 ''' compute the distinct value ratio of the column
255
256 if transfer_field_type(col[1], self.args.dbtype) == DataType.VALUE.value:
257 sql = 'SELECT count({}) FROM {};'.format(col[0], table_name)
258 success, res = self.execute_sql(sql)
259 print("======== column rows", res)
260 num = res
261 if num[0][0] != 0:
262 schema[table_name].append(col[0])
263 '''
264
265 #schema[table_name].append("column {} is of {} type".format(col[0], col[1]))
266 schema[table_name].append("{}".format(col[0]))
267 '''
268 with open(tpath, 'w') as f:
269 f.write(str(schema))
270 '''
271 #print(schema)
272 return schema
273
274 else:
275 logging.error('pgsql_cost_estimation Fails!')
276 return 0
277
278 def simulate_index(self, index):
279 #table_name = index.table()

Callers

nothing calls this directly

Calls 1

execute_sqlMethod · 0.95

Tested by

no test coverage detected