-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
405 lines (329 loc) · 17.2 KB
/
preprocessing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import re
import json
import argparse
import os
import difflib
from utils.bridge_content_encoder import get_database_matches
from sql_metadata import Parser
from tqdm import tqdm
sql_keywords = ['select', 'from', 'where', 'group', 'order', 'limit', 'intersect', 'union', \
'except', 'join', 'on', 'as', 'not', 'between', 'in', 'like', 'is', 'exists', 'max', 'min', \
'count', 'sum', 'avg', 'and', 'or', 'desc', 'asc', 'order by', 'group by', 'distinct']
ops = ["=", "!=", ">", ">=", "<", "<="]
def parse_option():
parser = argparse.ArgumentParser("")
parser.add_argument('--mode', type = str, default = "train")
parser.add_argument('--table_path', type = str, default = "./data/spider/tables.json")
parser.add_argument('--input_dataset_path', type = str, default = "./data/spider/train_spider.json",
help = '''
options:
./data/spider/train_spider.json
./data/spider/dev.json
''')
parser.add_argument('--output_dataset_path', type = str, default = "./data/pre-processing/preprocessed_dataset.json",
help = "the filepath of preprocessed dataset.")
parser.add_argument('--db_path', type = str, default = "./data/spider/database",
help = "the filepath of database.")
parser.add_argument("--preprocess_multispider", action="store_true", help="whether to preprocess Multi-Spider dataset")
opt = parser.parse_args()
return opt
def get_db_contents(question, table_name_original, column_names_original, db_id, db_path):
matched_contents = []
# extract matched contents for each column
for column_name_original in column_names_original:
matches = get_database_matches(
question,
table_name_original,
column_name_original,
db_path + "/{}/{}.sqlite".format(db_id, db_id)
)
matches = sorted(matches)
matched_contents.append(matches)
return matched_contents
def get_db_schemas(all_db_infos):
db_schemas = {}
for db in all_db_infos:
table_names_original = db["table_names_original"]
table_names = db["table_names"]
column_names_original = db["column_names_original"]
column_names = db["column_names"]
column_types = db["column_types"]
db_schemas[db["db_id"]] = {}
primary_keys, foreign_keys = [], []
# record primary keys
for pk_column_idx in db["primary_keys"]:
pk_table_name_original = table_names_original[column_names_original[pk_column_idx][0]]
pk_column_name_original = column_names_original[pk_column_idx][1]
primary_keys.append(
{
"table_name_original": pk_table_name_original.lower(),
"column_name_original": pk_column_name_original.lower()
}
)
db_schemas[db["db_id"]]["pk"] = primary_keys
# record foreign keys
for source_column_idx, target_column_idx in db["foreign_keys"]:
fk_source_table_name_original = table_names_original[column_names_original[source_column_idx][0]]
fk_source_column_name_original = column_names_original[source_column_idx][1]
fk_target_table_name_original = table_names_original[column_names_original[target_column_idx][0]]
fk_target_column_name_original = column_names_original[target_column_idx][1]
foreign_keys.append(
{
"source_table_name_original": fk_source_table_name_original.lower(),
"source_column_name_original": fk_source_column_name_original.lower(),
"target_table_name_original": fk_target_table_name_original.lower(),
"target_column_name_original": fk_target_column_name_original.lower(),
}
)
db_schemas[db["db_id"]]["fk"] = foreign_keys
db_schemas[db["db_id"]]["schema_items"] = []
for idx, table_name_original in enumerate(table_names_original):
column_names_original_list = []
column_names_list = []
column_types_list = []
for column_idx, (table_idx, column_name_original) in enumerate(column_names_original):
if idx == table_idx:
column_names_original_list.append(column_name_original.lower())
column_names_list.append(column_names[column_idx][1].lower())
column_types_list.append(column_types[column_idx])
db_schemas[db["db_id"]]["schema_items"].append({
"table_name_original": table_name_original.lower(),
"table_name": table_names[idx].lower(),
"column_names": column_names_list,
"column_names_original": column_names_original_list,
"column_types": column_types_list
})
return db_schemas
def get_db_schemas_for_multispider(all_db_infos):
db_schemas = {}
for db in all_db_infos:
table_names_original = db["table_names_original"]
table_names = db["table_names"]
column_names_original = db["column_names_original"]
column_names = db["column_names"]
column_types = db["column_types"]
db_schemas[db["db_id"]] = {}
primary_keys, foreign_keys = [], []
# record primary keys
for pk_column_idx in db["primary_keys"]:
pk_table_name = table_names[column_names[pk_column_idx][0]]
pk_column_name = column_names[pk_column_idx][1]
primary_keys.append(
{
"table_name": pk_table_name.lower(),
"column_namel": pk_column_name.lower()
}
)
db_schemas[db["db_id"]]["pk"] = primary_keys
# record foreign keys
for source_column_idx, target_column_idx in db["foreign_keys"]:
fk_source_table_name = table_names[column_names[source_column_idx][0]]
fk_source_column_name = column_names[source_column_idx][1]
fk_target_table_name = table_names[column_names[target_column_idx][0]]
fk_target_column_name = column_names[target_column_idx][1]
foreign_keys.append(
{
"source_table_name": fk_source_table_name.lower(),
"source_column_name": fk_source_column_name.lower(),
"target_table_name": fk_target_table_name.lower(),
"target_column_name": fk_target_column_name.lower(),
}
)
db_schemas[db["db_id"]]["fk"] = foreign_keys
db_schemas[db["db_id"]]["schema_items"] = []
for idx, table_name_original in enumerate(table_names_original):
column_names_original_list = []
column_names_list = []
column_types_list = []
for column_idx, (table_idx, column_name_original) in enumerate(column_names_original):
if idx == table_idx:
column_names_original_list.append(column_name_original.lower())
column_names_list.append(column_names[column_idx][1].lower())
column_types_list.append(column_types[column_idx])
db_schemas[db["db_id"]]["schema_items"].append({
"table_name_original": table_name_original.lower(),
"table_name": table_names[idx].lower(),
"column_names": column_names_list,
"column_names_original": column_names_original_list,
"column_types": column_types_list
})
return db_schemas
def normalization(sql):
def white_space_fix(s):
parsed_s = Parser(s)
s = " ".join([token.value for token in parsed_s.tokens])
return s
# convert everything except text between single quotation marks to lower case
def lower(s):
in_quotation = False
out_s = ""
for char in s:
if in_quotation:
out_s += char
else:
out_s += char.lower()
if char == "'":
if in_quotation:
in_quotation = False
else:
in_quotation = True
return out_s
# remove ";"
def remove_semicolon(s):
if s.endswith(";"):
s = s[:-1]
return s
# double quotation -> single quotation
def double2single(s):
return s.replace("\"", "'")
def add_asc(s):
pattern = re.compile(r'order by (?:\w+ \( \S+ \)|\w+\.\w+|\w+)(?: (?:\+|\-|\<|\<\=|\>|\>\=) (?:\w+ \( \S+ \)|\w+\.\w+|\w+))*')
if "order by" in s and "asc" not in s and "desc" not in s:
for p_str in pattern.findall(s):
s = s.replace(p_str, p_str + " asc")
return s
def remove_table_alias(s):
tables_aliases = Parser(s).tables_aliases
new_tables_aliases = {}
for i in range(1,11):
if "t{}".format(i) in tables_aliases.keys():
new_tables_aliases["t{}".format(i)] = tables_aliases["t{}".format(i)]
tables_aliases = new_tables_aliases
for k, v in tables_aliases.items():
s = s.replace("as " + k + " ", "")
s = s.replace(k, v)
return s
processing_func = lambda x : remove_table_alias(add_asc(lower(white_space_fix(double2single(remove_semicolon(x))))))
return processing_func(sql)
def isNegativeInt(string):
if string.startswith("-") and string[1:].isdigit():
return True
else:
return False
def isFloat(string):
if string.startswith("-"):
string = string[1:]
s = string.split(".")
if len(s)>2:
return False
else:
for s_i in s:
if not s_i.isdigit():
return False
return True
def main(opt):
dataset = json.load(open(opt.input_dataset_path))
all_db_infos = json.load(open(opt.table_path))
assert opt.mode in ["train", "eval", "test"]
if opt.preprocess_multispider:
db_schemas = get_db_schemas_for_multispider(all_db_infos)
else:
db_schemas = get_db_schemas(all_db_infos)
preprocessed_dataset = []
for data in tqdm(dataset, desc=f"Preprocessing {opt.mode} dataset.."):
if data['query'] == 'SELECT T1.company_name FROM Third_Party_Companies AS T1 JOIN Maintenance_Contracts AS T2 ON T1.company_id = T2.maintenance_contract_company_id JOIN Ref_Company_Types AS T3 ON T1.company_type_code = T3.company_type_code ORDER BY T2.contract_end_date DESC LIMIT 1':
data['query'] = 'SELECT T1.company_type FROM Third_Party_Companies AS T1 JOIN Maintenance_Contracts AS T2 ON T1.company_id = T2.maintenance_contract_company_id ORDER BY T2.contract_end_date DESC LIMIT 1'
data['query_toks'] = ['SELECT', 'T1.company_type', 'FROM', 'Third_Party_Companies', 'AS', 'T1', 'JOIN', 'Maintenance_Contracts', 'AS', 'T2', 'ON', 'T1.company_id', '=', 'T2.maintenance_contract_company_id', 'ORDER', 'BY', 'T2.contract_end_date', 'DESC', 'LIMIT', '1']
data['query_toks_no_value'] = ['select', 't1', '.', 'company_type', 'from', 'third_party_companies', 'as', 't1', 'join', 'maintenance_contracts', 'as', 't2', 'on', 't1', '.', 'company_id', '=', 't2', '.', 'maintenance_contract_company_id', 'order', 'by', 't2', '.', 'contract_end_date', 'desc', 'limit', 'value']
data['question'] = 'What is the type of the company who concluded its contracts most recently?'
data['question_toks'] = ['What', 'is', 'the', 'type', 'of', 'the', 'company', 'who', 'concluded', 'its', 'contracts', 'most', 'recently', '?']
if data['query'].startswith('SELECT T1.fname FROM student AS T1 JOIN lives_in AS T2 ON T1.stuid = T2.stuid WHERE T2.dormid IN'):
data['query'] = data['query'].replace('IN (SELECT T2.dormid)', 'IN (SELECT T3.dormid)')
index = data['query_toks'].index('(') + 2
# assert data['query_toks'][index] == 'T2.dormid'
data['query_toks'][index] = 'T3.dormid'
index = data['query_toks_no_value'].index('(') + 2
# assert data['query_toks_no_value'][index] == 't2'
data['query_toks_no_value'][index] = 't3'
question = data["question"].replace("\u2018", "'").replace("\u2019", "'").replace("\u201c", "'").replace("\u201d", "'").strip()
db_id = data["db_id"]
if opt.mode == "test":
sql, norm_sql = "", ""
sql_tokens = []
else:
sql = data["query"].strip()
norm_sql = normalization(sql).strip()
sql_tokens = norm_sql.split()
# Find values mentioned in the question
preprocessed_data = {}
preprocessed_data["question"] = question
preprocessed_data["db_id"] = db_id
preprocessed_data["sql"] = sql
preprocessed_data["norm_sql"] = norm_sql
preprocessed_data["db_schema"] = []
preprocessed_data["pk"] = db_schemas[db_id]["pk"]
preprocessed_data["fk"] = db_schemas[db_id]["fk"]
preprocessed_data["table_labels"] = []
preprocessed_data["column_labels"] = []
# add database information (including table name, column name, ..., table_labels, and column labels)
for table in db_schemas[db_id]["schema_items"]:
db_contents = get_db_contents(
question,
table["table_name_original"],
table["column_names_original"],
db_id,
opt.db_path
)
preprocessed_data["db_schema"].append({
"table_name_original":table["table_name_original"],
"table_name":table["table_name"],
"column_names":table["column_names"],
"column_names_original":table["column_names_original"],
"column_types":table["column_types"],
"db_contents": db_contents
})
# extract table and column classification labels
if table["table_name_original"] in sql_tokens: # for used tables
preprocessed_data["table_labels"].append(1)
column_labels = []
for column_name_original in table["column_names_original"]:
if column_name_original in sql_tokens or \
table["table_name_original"]+"."+column_name_original in sql_tokens: # for used columns
column_labels.append(1)
else:
column_labels.append(0)
preprocessed_data["column_labels"].append(column_labels)
else: # for unused tables and their columns
preprocessed_data["table_labels"].append(0)
preprocessed_data["column_labels"].append([0 for _ in range(len(table["column_names_original"]))])
# Find values mentioned in the sql
table_names_original, table_dot_column_names_original, column_names_original = [], [], []
for table in db_schemas[db_id]["schema_items"]:
table_name_original = table["table_name_original"]
table_names_original.append(table_name_original)
for column_name_original in ["*"]+table["column_names_original"]:
table_dot_column_names_original.append(table_name_original+"."+column_name_original)
column_names_original.append(column_name_original)
not_value= table_names_original + column_names_original + table_dot_column_names_original + sql_keywords + ops + ["(", ")", ",", ";"]
not_value_lowered = [x.lower() for x in not_value]
parsed_sql = Parser(sql)
sql_values = []
for token in parsed_sql.tokens:
if str(token).lower() not in not_value_lowered:
sql_values.append(str(token).replace("'", "").replace("%","").strip())
# Further post-processing
sql_values_filtered = []
# Here, we discard the values followed by limit
# as this value is not explicitly mentioned in the question
sql_limit_value = None
if parsed_sql.limit_and_offset is not None:
sql_limit_value = str(parsed_sql.limit_and_offset[0])
for v in sql_values:
if (v.isnumeric() or (v.startswith("-") and v[1:].isnumeric())):
if v != sql_limit_value:
sql_values_filtered.append(v)
else:
# if not numeric value, than the string must be included in the question
if v in question:
sql_values_filtered.append(v)
preprocessed_data["sql_values"] = sql_values_filtered
preprocessed_dataset.append(preprocessed_data)
# Create path if output_dataset_path does not exist
if not os.path.exists(os.path.dirname(opt.output_dataset_path)):
os.makedirs(os.path.dirname(opt.output_dataset_path))
with open(opt.output_dataset_path, "w") as f:
preprocessed_dataset_str = json.dumps(preprocessed_dataset, indent = 2, ensure_ascii = False)
f.write(preprocessed_dataset_str)
if __name__ == "__main__":
opt = parse_option()
main(opt)