@@ -43,6 +43,21 @@ def list_tables(database: str, like: str = None):
43
43
query += f" LIKE '{ like } '"
44
44
result = client .command (query )
45
45
46
+ # Get all table comments in one query
47
+ table_comments_query = f"SELECT name, comment FROM system.tables WHERE database = '{ database } '"
48
+ table_comments_result = client .query (table_comments_query )
49
+ table_comments = {row [0 ]: row [1 ] for row in table_comments_result .result_rows }
50
+
51
+ # Get all column comments in one query
52
+ column_comments_query = f"SELECT table, name, comment FROM system.columns WHERE database = '{ database } '"
53
+ column_comments_result = client .query (column_comments_query )
54
+ column_comments = {}
55
+ for row in column_comments_result .result_rows :
56
+ table , col_name , comment = row
57
+ if table not in column_comments :
58
+ column_comments [table ] = {}
59
+ column_comments [table ][col_name ] = comment
60
+
46
61
def get_table_info (table ):
47
62
logger .info (f"Getting schema info for table { database } .{ table } " )
48
63
schema_query = f"DESCRIBE STREAM { database } .`{ table } `"
@@ -54,6 +69,11 @@ def get_table_info(table):
54
69
column_dict = {}
55
70
for i , col_name in enumerate (column_names ):
56
71
column_dict [col_name ] = row [i ]
72
+ # Add comment from our pre-fetched comments
73
+ if table in column_comments and column_dict ['name' ] in column_comments [table ]:
74
+ column_dict ['comment' ] = column_comments [table ][column_dict ['name' ]]
75
+ else :
76
+ column_dict ['comment' ] = None
57
77
columns .append (column_dict )
58
78
59
79
create_table_query = f"SHOW CREATE STREAM { database } .`{ table } `"
@@ -62,6 +82,7 @@ def get_table_info(table):
62
82
return {
63
83
"database" : database ,
64
84
"name" : table ,
85
+ "comment" : table_comments .get (table ),
65
86
"columns" : columns ,
66
87
"create_table_query" : create_table_result ,
67
88
}
0 commit comments