@@ -176,8 +176,17 @@ def _run_query(self, query):
176176 :type query: str
177177
178178 :return: The output result of the query, which can either be the actual query result or an error message.
179- :rtype: str
179+ :rtype: dict
180+
181+ Example:
182+ ::
180183
184+ {
185+ "data": "[{\" machine_type\" : \" n1-standard-1\" , \" status\" : \" RUNNING\" , \" num_instances\" : 3}, ...]",
186+ "error": "stderr message if present",
187+ "exception": "ERROR: {\" exception\" : \" <exception message>\" , \" doc\" : \" <exception doc>\" , \" params\" : \" <params>\" , \" stdout\" : \" <stdout>\" , \" stderr\" : \" <stderr>\" }
188+ }
189+
181190 Possible error messages include:
182191 - Indications that the StackQL binary wasn't found.
183192 - Generic error messages for other exceptions encountered during the query execution.
@@ -219,11 +228,6 @@ def _run_query(self, query):
219228 }
220229 output ["exception" ] = f"ERROR: { json .dumps (error_details )} "
221230
222- # output = {'data': <stdout str>, 'error': <stderr str>, 'exception': <exception as a json string> }
223- # for a statement you would expect 'error' to exist
224- # for a query you would expect 'data' to exist
225- # 'exception' in output indicates an error occurred during the execution (statement or query)
226-
227231 return output
228232
229233 def __init__ (self ,
@@ -487,12 +491,20 @@ def executeStmt(self, query):
487491 else :
488492 return result
489493 else :
494+
490495 # returns either...
491496 # {'error': '<error message>'} if something went wrong; or
492497 # {'message': '<message>'} if the statement was executed successfully
498+
493499 result = self ._run_query (query )
494500 if "exception" in result :
495- return {"error" : result ["exception" ]}
501+ exception_msg = result ["exception" ]
502+ if self .output == 'pandas' :
503+ return pd .DataFrame ({'error' : [exception_msg ]}) if exception_msg else pd .DataFrame ({'error' : []})
504+ elif self .output == 'csv' :
505+ return exception_msg
506+ else :
507+ return {"error" : exception_msg }
496508
497509 # message on stderr
498510 message = result ["error" ]
@@ -543,35 +555,57 @@ def execute(self, query, suppress_errors=True):
543555 return result
544556 else :
545557 # returns either...
546- # {'error': <error json str>} if something went wrong; or
547- # [{<data>} ] if the statement was executed successfully, messages to stderr are ignored
558+ # [ {'error': <error json str>}] if something went wrong; or
559+ # [{<row1>},... ] if the statement was executed successfully, messages to stderr are ignored
548560
549561 output = self ._run_query (query )
550562 if "exception" in output :
551- return {"error" : output ["exception" ]}
563+ exception_msg = output ["exception" ]
564+ if self .output == 'pandas' :
565+ return pd .DataFrame ({'error' : [exception_msg ]}) if exception_msg else pd .DataFrame ({'error' : []})
566+ elif self .output == 'csv' :
567+ return exception_msg
568+ else :
569+ return [{"error" : exception_msg }]
552570
553571 if "data" in output :
572+ data = output ["data" ]
554573 # theres data, return it
555574 if self .output == 'csv' :
556- return output [ " data" ]
575+ return data
557576 elif self .output == 'pandas' :
558577 try :
559- return pd .read_json (StringIO (output [ " data" ] ))
578+ return pd .read_json (StringIO (data ))
560579 except ValueError :
561580 return pd .DataFrame ([{"error" : "Invalid JSON output" }])
562581 else : # Assume 'dict' output
563582 try :
564- return json .loads (output [ " data" ] )
583+ return json .loads (data )
565584 except ValueError :
566- return {"error" : f"Invalid JSON output : { output ['data' ]} " }
567- else :
568- if "error" in output :
569- if suppress_errors :
570- return []
571- else :
572- return {"error" : output ["error" ]}
585+ return [{"error" : f"Invalid JSON output : { data } " }]
586+
587+ if "error" in output :
588+ # theres no data but there is stderr from the request, could be an expected error like a 404
589+ if suppress_errors :
590+ # we dont care about errors, return an empty list
591+ csv_ret = ""
592+ pd_ret = pd .DataFrame ()
593+ dict_ret = []
594+ else :
595+ # we care about errors, return the error
596+ err_msg = output ["error" ]
597+ csv_ret = err_msg
598+ pd_ret = pd .DataFrame ([{"error" : err_msg }])
599+ dict_ret = [{"error" : err_msg }]
600+
601+ if self .output == 'csv' :
602+ return csv_ret
603+ elif self .output == 'pandas' :
604+ return pd_ret
605+ else : # Assume 'dict' output
606+ return dict_ret
607+
573608
574- #
575609 # asnyc query support
576610 #
577611
0 commit comments