@@ -43,7 +43,7 @@ def _init_database(self):
43
43
except Exception as e :
44
44
raise ValueError (f"Failed to connect to Snowflake database: { e } " )
45
45
46
- def execute_query (self , query : str , max_results : int = None ) -> list [dict [str , Any ]]:
46
+ def execute_query (self , query : str ) -> list [dict [str , Any ]]:
47
47
"""Execute a SQL query and return results as a list of dictionaries"""
48
48
if not self .session :
49
49
self ._init_database ()
@@ -52,10 +52,9 @@ def execute_query(self, query: str, max_results: int = None) -> list[dict[str, A
52
52
try :
53
53
result = self .session .sql (query ).to_pandas ()
54
54
result_rows = result .to_dict (orient = "records" )
55
+ query_id = str (uuid .uuid4 ())
55
56
56
- if max_results and len (result_rows ) > max_results :
57
- return result_rows [:max_results ], True
58
- return result_rows , False
57
+ return result_rows , query_id
59
58
60
59
except Exception as e :
61
60
logger .error (f'Database error executing "{ query } ": { e } ' )
@@ -109,7 +108,7 @@ async def handle_list_tables(arguments, db, *_):
109
108
FROM { db .connection_config ['database' ]} .information_schema.tables
110
109
WHERE table_schema = '{ db .connection_config ['schema' ].upper ()} '
111
110
"""
112
- results , _ = db .execute_query (query )
111
+ results , query_id = db .execute_query (query )
113
112
return [types .TextContent (type = "text" , text = data_to_yaml (results ), artifacts = [{"type" : "dataframe" , "data" : results }])]
114
113
115
114
@@ -127,24 +126,31 @@ async def handle_describe_table(arguments, db, *_):
127
126
FROM { database_name } .information_schema.columns
128
127
WHERE table_schema = '{ schema_name } ' AND table_name = '{ table_name } '
129
128
"""
130
- results , _ = db .execute_query (query )
131
- return [types .TextContent (type = "text" , text = data_to_yaml (results ), artifacts = [{"type" : "dataframe" , "data" : results }])]
129
+ results , query_id = db .execute_query (query )
130
+ return [
131
+ types .TextContent (
132
+ type = "text" , text = data_to_yaml (results ), artifacts = [{"type" : "dataframe" , "data" : results , "query_id" : query_id }]
133
+ )
134
+ ]
132
135
133
136
134
137
async def handle_read_query (arguments , db , write_detector , * _ ):
135
138
MAX_RESULTS = 50
136
139
if write_detector .analyze_query (arguments ["query" ])["contains_write" ]:
137
140
raise ValueError ("Calls to read_query should not contain write operations" )
138
141
139
- results , truncated = db .execute_query (arguments ["query" ], MAX_RESULTS )
140
- query_id = str (uuid .uuid4 ())
141
-
142
- results_text = data_to_yaml (results )
143
- if truncated :
142
+ results , query_id = db .execute_query (arguments ["query" ])
143
+ truncate = len (results ) > MAX_RESULTS
144
+ results_text = data_to_yaml (results [:MAX_RESULTS ])
145
+ if truncate :
144
146
results_text += f"\n Results of query have been truncated. There are { len (results ) - MAX_RESULTS } more rows."
145
147
results_text += f"\n query_id = { query_id } "
146
148
147
- return [types .TextContent (type = "text" , text = results_text , artifacts = [{"type" : "dataframe" , "data" : results }])]
149
+ return [
150
+ types .TextContent (
151
+ type = "text" , text = results_text , artifacts = [{"type" : "dataframe" , "data" : results , "query_id" : query_id }]
152
+ )
153
+ ]
148
154
149
155
150
156
async def handle_append_insight (arguments , db , _ , __ , server ):
@@ -162,7 +168,7 @@ async def handle_write_query(arguments, db, _, allow_write, __):
162
168
if arguments ["query" ].strip ().upper ().startswith ("SELECT" ):
163
169
raise ValueError ("SELECT queries are not allowed for write_query" )
164
170
165
- results , _ = db .execute_query (arguments ["query" ])
171
+ results , query_id = db .execute_query (arguments ["query" ])
166
172
return [types .TextContent (type = "text" , text = str (results ))]
167
173
168
174
@@ -172,21 +178,21 @@ async def handle_create_table(arguments, db, _, allow_write, __):
172
178
if not arguments ["query" ].strip ().upper ().startswith ("CREATE TABLE" ):
173
179
raise ValueError ("Only CREATE TABLE statements are allowed" )
174
180
175
- db .execute_query (arguments ["query" ])
176
- return [types .TextContent (type = "text" , text = "Table created successfully" )]
181
+ results , query_id = db .execute_query (arguments ["query" ])
182
+ return [types .TextContent (type = "text" , text = f "Table created successfully. query_id = { query_id } " )]
177
183
178
184
179
185
async def prefetch_tables (db : SnowflakeDB , credentials : dict ) -> str :
180
186
"""Prefetch table and column information"""
181
187
try :
182
188
logger .info ("Prefetching table descriptions" )
183
- table_results , _ = db .execute_query (
189
+ table_results , query_id = db .execute_query (
184
190
f"""SELECT table_name, comment
185
191
FROM { credentials ['database' ]} .information_schema.tables
186
192
WHERE table_schema = '{ credentials ['schema' ].upper ()} '"""
187
193
)
188
194
189
- column_results , _ = db .execute_query (
195
+ column_results , query_id = db .execute_query (
190
196
f"""SELECT table_name, column_name, data_type, comment
191
197
FROM { credentials ['database' ]} .information_schema.columns
192
198
WHERE table_schema = '{ credentials ['schema' ].upper ()} '"""
0 commit comments