@@ -44,6 +44,21 @@ def list_tables(database: str, like: str = None):
44
44
query += f" LIKE '{ like } '"
45
45
result = client .command (query )
46
46
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
+
47
62
def get_table_info (table ):
48
63
logger .info (f"Getting schema info for table { database } .{ table } " )
49
64
schema_query = f"DESCRIBE TABLE { database } .`{ table } `"
@@ -55,6 +70,11 @@ def get_table_info(table):
55
70
column_dict = {}
56
71
for i , col_name in enumerate (column_names ):
57
72
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
58
78
columns .append (column_dict )
59
79
60
80
create_table_query = f"SHOW CREATE TABLE { database } .`{ table } `"
@@ -63,6 +83,7 @@ def get_table_info(table):
63
83
return {
64
84
"database" : database ,
65
85
"name" : table ,
86
+ "comment" : table_comments .get (table ),
66
87
"columns" : columns ,
67
88
"create_table_query" : create_table_result ,
68
89
}
0 commit comments