@@ -44,6 +44,21 @@ def list_tables(database: str, like: str = None):
4444 query += f" LIKE '{ like } '"
4545 result = client .command (query )
4646
47+ # Get all table comments in one query
48+ table_comments_query = f"SELECT name, comment FROM system.tables WHERE database = '{ database } '"
49+ table_comments_result = client .query (table_comments_query )
50+ table_comments = {row [0 ]: row [1 ] for row in table_comments_result .result_rows }
51+
52+ # Get all column comments in one query
53+ column_comments_query = f"SELECT table, name, comment FROM system.columns WHERE database = '{ database } '"
54+ column_comments_result = client .query (column_comments_query )
55+ column_comments = {}
56+ for row in column_comments_result .result_rows :
57+ table , col_name , comment = row
58+ if table not in column_comments :
59+ column_comments [table ] = {}
60+ column_comments [table ][col_name ] = comment
61+
4762 def get_table_info (table ):
4863 logger .info (f"Getting schema info for table { database } .{ table } " )
4964 schema_query = f"DESCRIBE TABLE { database } .`{ table } `"
@@ -55,6 +70,11 @@ def get_table_info(table):
5570 column_dict = {}
5671 for i , col_name in enumerate (column_names ):
5772 column_dict [col_name ] = row [i ]
73+ # Add comment from our pre-fetched comments
74+ if table in column_comments and column_dict ['name' ] in column_comments [table ]:
75+ column_dict ['comment' ] = column_comments [table ][column_dict ['name' ]]
76+ else :
77+ column_dict ['comment' ] = None
5878 columns .append (column_dict )
5979
6080 create_table_query = f"SHOW CREATE TABLE { database } .`{ table } `"
@@ -63,6 +83,7 @@ def get_table_info(table):
6383 return {
6484 "database" : database ,
6585 "name" : table ,
86+ "comment" : table_comments .get (table ),
6687 "columns" : columns ,
6788 "create_table_query" : create_table_result ,
6889 }
0 commit comments