transform sql json to sql query, this is only for NL2SQL, eg. select a, b where a op val1
(sql, cols)
| 185 | return tokens |
| 186 | |
| 187 | def sql2query(sql, cols): |
| 188 | """ |
| 189 | transform sql json to sql query, this is only for NL2SQL, eg. select a, b where a op val1 |
| 190 | """ |
| 191 | |
| 192 | sels = sql['sel'] |
| 193 | aggs = sql['agg'] |
| 194 | op = sql["cond_conn_op"] |
| 195 | conds = sql["conds"] |
| 196 | |
| 197 | condstrs = [f'{cols[cond[0]]} {op_sql_dict[cond[1]]} "{cond[2]}"' |
| 198 | for cond in conds] |
| 199 | cond_str = f" {conn_sql_dict[op]} ".join(condstrs) |
| 200 | |
| 201 | def agg_col(agg, col): |
| 202 | if agg == 0: |
| 203 | return cols[col] |
| 204 | else: |
| 205 | return f"{agg_sql_dict[agg]} ( {cols[col]} )" |
| 206 | |
| 207 | selstrs = [agg_col(i,j) for i,j in zip(aggs, sels)] |
| 208 | sel_str = ' , '.join(selstrs) |
| 209 | |
| 210 | return f"SELECT {sel_str} WHERE {cond_str}" |
| 211 | |
| 212 | def query2sql(query, cols, single_equal=False, with_value=True): |
| 213 |