4545from dremioai .api .dremio .catalog import get_schema , get_lineage , get_descriptions
4646from csv import reader
4747from io import StringIO
48+ from sqlglot import parse_one
49+ from sqlglot import expressions
4850
4951logger = log .logger (__name__ )
5052
@@ -244,6 +246,31 @@ async def invoke(self) -> Dict[str, Any]:
244246
245247class RunSqlQuery (Tools ):
246248 For : ClassVar [Annotated [ToolType , ToolType .FOR_SELF | ToolType .FOR_DATA_PATTERNS ]]
249+ _safe = [
250+ expressions .Select ,
251+ expressions .With ,
252+ expressions .Union ,
253+ ]
254+
255+ @staticmethod
256+ def ensure_query_allowed (s : str ):
257+ if settings .instance ().dremio .allow_dml :
258+ return
259+
260+ try :
261+ q = parse_one (s )
262+ if any (isinstance (q , t ) for t in RunSqlQuery ._safe ):
263+ return
264+ except :
265+ if not re .search (
266+ r"\b(drop|insert|update|truncate|delete|copy into|alter|create)\b" ,
267+ s ,
268+ re .IGNORECASE ,
269+ ):
270+ return
271+ raise ValueError (
272+ "The query contains a DML statement. Only select queries are allowed"
273+ )
247274
248275 async def invoke (self , s : str ) -> Dict [str , List [Any ]]:
249276 """Run a SELECT sql query on the Dremio cluster and return the results.
@@ -253,11 +280,7 @@ async def invoke(self, s: str) -> Dict[str, List[Any]]:
253280 Args:
254281 s: sql query
255282 """
256- # TODO: graduate to a more sophisticated SQL parser and check to allow better queries
257- if re .search (r"(drop|insert|update|truncate|delete)" , s , re .IGNORECASE ):
258- raise ValueError (
259- "The query contains a DML statement. Only select queries are allowed"
260- )
283+ RunSqlQuery .ensure_query_allowed (s )
261284 try :
262285 s = f"/* dremioai: submitter={ self .__class__ .__name__ } */\n { s } "
263286 df = await sql .run_query (query = s , use_df = True )
@@ -476,6 +499,8 @@ async def invoke(self) -> Dict[str, Any]:
476499 "jvm_gc_pause_seconds" : "Indicates how long the JVM was paused for garbage collection, and also is a rubric to know if the system is in use" ,
477500 "memory_heap_usage" : "Indicates the amount of memory used by the JVM" ,
478501 "memory_heap_committed" : "Indicates the amount of memory committed by the JVM" ,
502+ "dremio_engine_executors" : "Number of executors running in the Dremio engine. It correlates to dremio_engine_replica_running using engine_id label" ,
503+ "dremio_engine_replica_running" : "Number of running replicas in the Dremio engine. It correlates to dremio_engine_executors using engine_id label" ,
479504 }
480505
481506
0 commit comments