anumaurya114exp commited on
Commit
bfda8a4
·
verified ·
1 Parent(s): 72663f4

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +8 -2
utils.py CHANGED
@@ -213,10 +213,10 @@ 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
  if len(with_as_statements)>0:
222
  final_query_with_with_as = f"WITH {with_as_statement}\n{final_query}"
@@ -232,10 +232,16 @@ def get_subquery_info(json_response):
232
  subquery_keys = get_keys_matching_pattern(json_response, r'subquery\d+')
233
  return {key:json_response[key] for key in subquery_keys}
234
 
 
 
 
 
 
235
  def construct_final_query(query, json_response):
236
  query = remove_with_as(query)
237
  subquery_info = get_subquery_info(json_response)
238
  final_query_with_with_as = construct_with_stats(query, subquery_info)
 
239
  final_query_with_with_as = final_query_with_with_as.replace('\n\n','\n')
240
  final_query_with_with_as = sqlparse.format(final_query_with_with_as, reindent=True)
241
  return final_query_with_with_as
 
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) \n--{subquery['description'].replace('\n',' ')}\n"
217
  with_as_statements.append(with_as_statement)
218
 
219
+ with_as_statement = "\n,\n".join(with_as_statements)
220
 
221
  if len(with_as_statements)>0:
222
  final_query_with_with_as = f"WITH {with_as_statement}\n{final_query}"
 
232
  subquery_keys = get_keys_matching_pattern(json_response, r'subquery\d+')
233
  return {key:json_response[key] for key in subquery_keys}
234
 
235
+ def add_single_query_description(query, json_response):
236
+ if json_response.get("query",None)!=None:
237
+ query = f"{query}\n --{json_response['query']['description'].replace('\n',' ')}\n"
238
+ return query
239
+
240
  def construct_final_query(query, json_response):
241
  query = remove_with_as(query)
242
  subquery_info = get_subquery_info(json_response)
243
  final_query_with_with_as = construct_with_stats(query, subquery_info)
244
+ final_query_with_with_as = add_single_query_description(final_query_with_with_as, json_response)
245
  final_query_with_with_as = final_query_with_with_as.replace('\n\n','\n')
246
  final_query_with_with_as = sqlparse.format(final_query_with_with_as, reindent=True)
247
  return final_query_with_with_as