Spaces:
Runtime error
Runtime error
error handling of testing
Browse files
utils.py
CHANGED
|
@@ -5,6 +5,8 @@ from persistStorage import retrieveTablesDataFromLocalDb, saveTablesDataToLocalD
|
|
| 5 |
from config import SCHEMA_INFO_FILE_PATH
|
| 6 |
import os
|
| 7 |
import pickle
|
|
|
|
|
|
|
| 8 |
|
| 9 |
class DataWrapper:
|
| 10 |
def __init__(self, data):
|
|
@@ -28,7 +30,10 @@ class MetaDataLayout:
|
|
| 28 |
"selectedTables":{},
|
| 29 |
"allTables":allTablesAndCols
|
| 30 |
}
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
def setSelection(self, tablesAndCols):
|
| 33 |
"""
|
| 34 |
tablesAndCols : {"table1":["col1", "col2"], "table1":["cola","colb"]}
|
|
@@ -185,4 +190,102 @@ def preProcessGptQueryReponse(gptResponse, metadataLayout: MetaDataLayout):
|
|
| 185 |
schemaName = metadataLayout.schemaName
|
| 186 |
tablesList = metadataLayout.getAllTablesCols().keys()
|
| 187 |
gptResponse = addSchemaToTableInSQL(gptResponse, schemaName=schemaName, tablesList=tablesList)
|
| 188 |
-
return gptResponse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from config import SCHEMA_INFO_FILE_PATH
|
| 6 |
import os
|
| 7 |
import pickle
|
| 8 |
+
import sqlparse
|
| 9 |
+
import json
|
| 10 |
|
| 11 |
class DataWrapper:
|
| 12 |
def __init__(self, data):
|
|
|
|
| 30 |
"selectedTables":{},
|
| 31 |
"allTables":allTablesAndCols
|
| 32 |
}
|
| 33 |
+
|
| 34 |
+
def getDataLayout(self):
|
| 35 |
+
return self.datalayout
|
| 36 |
+
|
| 37 |
def setSelection(self, tablesAndCols):
|
| 38 |
"""
|
| 39 |
tablesAndCols : {"table1":["col1", "col2"], "table1":["cola","colb"]}
|
|
|
|
| 190 |
schemaName = metadataLayout.schemaName
|
| 191 |
tablesList = metadataLayout.getAllTablesCols().keys()
|
| 192 |
gptResponse = addSchemaToTableInSQL(gptResponse, schemaName=schemaName, tablesList=tablesList)
|
| 193 |
+
return gptResponse
|
| 194 |
+
|
| 195 |
+
def remove_with_as(sql_query):
|
| 196 |
+
pattern = r'WITH\s+.*?AS\s*\((?:[^()]|\((?:[^()]+|\([^()]*\))*\))*\)\s*,?'
|
| 197 |
+
|
| 198 |
+
cte_pattern = re.compile(pattern, re.IGNORECASE | re.DOTALL)
|
| 199 |
+
sql_query = cte_pattern.sub('', sql_query)
|
| 200 |
+
|
| 201 |
+
pattern = r'WITH\s+.*?AS\s*\((?:[^()]|\((?:[^()]+|\([^()]*\))*\))*\)\s*'
|
| 202 |
+
cte_pattern = re.compile(pattern, re.IGNORECASE | re.DOTALL)
|
| 203 |
+
sql_query = cte_pattern.sub('', sql_query)
|
| 204 |
+
|
| 205 |
+
pattern = r'subquery\d+\s+AS\s*\((?:[^()]|\((?:[^()]+|\([^()]*\))*\))*\)\s*'
|
| 206 |
+
|
| 207 |
+
cte_pattern = re.compile(pattern, re.IGNORECASE | re.DOTALL)
|
| 208 |
+
sql_query = cte_pattern.sub('', sql_query)
|
| 209 |
+
|
| 210 |
+
return sql_query
|
| 211 |
+
|
| 212 |
+
def construct_with_stats(final_query, subquery_info):
|
| 213 |
+
|
| 214 |
+
with_as_statements = []
|
| 215 |
+
for subquery_name, subquery in subquery_info.items():
|
| 216 |
+
with_as_statement = f"{subquery_name} AS (\n{subquery['result']}\n)"
|
| 217 |
+
with_as_statements.append(with_as_statement)
|
| 218 |
+
|
| 219 |
+
with_as_statement = ",\n".join(with_as_statements)
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
final_query_with_with_as = f"WITH {with_as_statement}\n{final_query}"
|
| 223 |
+
|
| 224 |
+
return final_query_with_with_as
|
| 225 |
+
|
| 226 |
+
def get_keys_matching_pattern(dictionary, pattern):
|
| 227 |
+
return [key for key in dictionary.keys() if re.match(pattern, key)]
|
| 228 |
+
|
| 229 |
+
def get_subquery_info(json_response):
|
| 230 |
+
subquery_keys = get_keys_matching_pattern(json_response, r'subquery\d+')
|
| 231 |
+
return {key:json_response[key] for key in subquery_keys}
|
| 232 |
+
|
| 233 |
+
def construct_final_query(query, json_response):
|
| 234 |
+
query = remove_with_as(query)
|
| 235 |
+
subquery_info = get_subquery_info(json_response)
|
| 236 |
+
final_query_with_with_as = construct_with_stats(query, subquery_info)
|
| 237 |
+
final_query_with_with_as = final_query_with_with_as.replace('\n\n','\n')
|
| 238 |
+
final_query_with_with_as = sqlparse.format(final_query_with_with_as, reindent=True)
|
| 239 |
+
return final_query_with_with_as
|
| 240 |
+
|
| 241 |
+
def getQueryFromGptResponse(gptResponse):
|
| 242 |
+
tryParsing = True
|
| 243 |
+
parsedSql = False
|
| 244 |
+
if tryParsing:
|
| 245 |
+
try:
|
| 246 |
+
txt = gptResponse.split("```json")[-1].split('```')[0].replace('\n', ' ')
|
| 247 |
+
jsonResponse = json.loads(txt)
|
| 248 |
+
sqlResult = jsonResponse['finalResult']
|
| 249 |
+
parsedSql = True
|
| 250 |
+
tryParsing = False
|
| 251 |
+
print("parsed desired result from gpt response using method 1.")
|
| 252 |
+
except:
|
| 253 |
+
print("Couldn't parse desired result from gpt response using method 1.")
|
| 254 |
+
if tryParsing:
|
| 255 |
+
try:
|
| 256 |
+
jsonResponse = json.loads(gptResponse.replace("```json","").replace("```","").replace('\n', ' '))
|
| 257 |
+
sqlResult = jsonResponse['finalResult']
|
| 258 |
+
parsedSql = True
|
| 259 |
+
tryParsing = False
|
| 260 |
+
print("parsed desired result from gpt response using method 2.")
|
| 261 |
+
except:
|
| 262 |
+
print("Couldn't parse desired result from gpt response using method 2")
|
| 263 |
+
if tryParsing:
|
| 264 |
+
try:
|
| 265 |
+
txt = gptResponse.split("```json")[-1].split('```')[0].replace('\n', ' ')
|
| 266 |
+
jsonResponse = json.loads(txt)
|
| 267 |
+
sqlResult = jsonResponse[list(jsonResponse.keys())[0]]['result']
|
| 268 |
+
parsedSql = True
|
| 269 |
+
tryParsing = False
|
| 270 |
+
print("parsed desired result from gpt response using method 3.")
|
| 271 |
+
except:
|
| 272 |
+
print("Couldn't parse desired result from gpt response using method 3.")
|
| 273 |
+
if parsedSql:
|
| 274 |
+
isFormatted = False
|
| 275 |
+
try:
|
| 276 |
+
formattedSql = sqlparse.format(sqlResult, reindent=True)
|
| 277 |
+
responseToReturn = formattedSql
|
| 278 |
+
isFormatted = True
|
| 279 |
+
except:
|
| 280 |
+
isFormatted = False
|
| 281 |
+
if not isFormatted:
|
| 282 |
+
try:
|
| 283 |
+
formattedSql = sqlparse.format(sqlResult['result'], reindent=True)
|
| 284 |
+
responseToReturn = formattedSql
|
| 285 |
+
print("gpt didn't give parsed result. So parsing again. the formatting.")
|
| 286 |
+
except:
|
| 287 |
+
responseToReturn = str(sqlResult)
|
| 288 |
+
else:
|
| 289 |
+
responseToReturn = gptResponse
|
| 290 |
+
jsonResponse = {}
|
| 291 |
+
return responseToReturn, jsonResponse
|