MCPcopy Create free account
hub / github.com/PaddlePaddle/Research / query2sql

Function query2sql

NLP/Text2SQL-BASELINE/tools/evaluation/utils.py:212–291  ·  view source on GitHub ↗
(query, cols, single_equal=False, with_value=True)

Source from the content-addressed store, hash-verified

210 return f"SELECT {sel_str} WHERE {cond_str}"
211
212def query2sql(query, cols, single_equal=False, with_value=True):
213
214 cols = [i.lower() for i in cols]
215
216 sql_op_dict = {}
217 sql_agg_dict = {}
218 sql_conn_dict = {}
219 for k, v in op_sql_dict.items():
220 sql_op_dict[v] = k
221 sql_op_dict[v.lower()] = k
222 for k, v in agg_sql_dict.items():
223 sql_agg_dict[v] = k
224 sql_agg_dict[v.lower()] = k
225 for k, v in conn_sql_dict.items():
226 sql_conn_dict[v] = k
227 sql_conn_dict[v.lower()] = k
228
229 query = tokenize_NL2SQL(query, cols, single_equal=single_equal, math=False)
230 assert query[0] == 'select'
231
232 def parse_cols(toks, start_idx):
233 """
234 :returns next idx, (agg, col)
235 """
236 if 'from' in toks:
237 toks = toks[:toks.index('from')]
238 idx = start_idx
239 len_ = len(toks)
240 outs = []
241 while idx < len_:
242 if toks[idx] in AGG_OPS:
243 agg_id = sql_agg_dict[toks[idx]]
244 idx += 1
245 assert idx < len_ and toks[idx] == '(', toks[idx]
246 idx += 1
247 agg, col = toks[start_idx], toks[idx]
248 idx += 1
249 assert idx < len_ and toks[idx] == ')', toks[idx] +''.join(toks)
250 idx += 1
251 outs.append((agg, col))
252 elif toks[idx] == ',':
253 idx += 1
254 else:
255 agg, col = '', toks[idx]
256 idx += 1
257 outs.append(('', col))
258 return outs
259
260 def _format_col(old_col):
261 """format"""
262 if old_col.lower().startswith('table_'):
263 return old_col.split('.', 1)[1]
264 else:
265 return old_col
266
267 if 'where' not in query:
268 cond_index = len(query)
269 conn = ''

Callers 1

evaluate_NL2SQLFunction · 0.85

Calls 5

tokenize_NL2SQLFunction · 0.85
parse_colsFunction · 0.85
_format_colFunction · 0.85
itemsMethod · 0.80
indexMethod · 0.80

Tested by

no test coverage detected