-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdata_process.py
More file actions
329 lines (259 loc) · 11.6 KB
/
data_process.py
File metadata and controls
329 lines (259 loc) · 11.6 KB
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
import os
import io
import json
import random
import numpy as np
import torch
import argparse
import logging
IGNORE_INDEX = -100
CLAUSE_KEYWORDS = ['select', 'from', 'where', 'group by', 'having', 'order by', 'limit', 'intersect', 'union', 'except',
'union all']
JOIN_KEYWORDS = ['join', 'on', 'as', 'right join', 'inner join', 'left join']
OTHER_KEYWORDS = ['distinct']
BIRD_KEYWORDS = ['if', 'else', 'datediff', 'over', 'instr', 'case', 'partition by', 'iif', 'float', 'real', 'when',
'int', 'using', 'timestampdiff', 'then', 'substr', 'cast', 'integer', 'strftime', 'end']
WHERE_OPS = ['not', 'between', 'in', 'like', 'exists', 'not null', 'null']
AGG_OPS = ['max', 'min', 'count', 'sum', 'avg']
COND_OPS = ['and']
ORDER_OPS = ['desc', 'asc']
SQL_KEYWORDS = []
SQL_KEYWORDS.extend(CLAUSE_KEYWORDS)
SQL_KEYWORDS.extend(JOIN_KEYWORDS)
SQL_KEYWORDS.extend(OTHER_KEYWORDS)
SQL_KEYWORDS.extend(BIRD_KEYWORDS)
# SQL_KEYWORDS.extend(WHERE_OPS)
SQL_KEYWORDS.extend(AGG_OPS)
SQL_KEYWORDS.extend(COND_OPS)
SQL_KEYWORDS.extend(ORDER_OPS)
def set_seed(seed: int):
"""
Set random seed for reproducibility across random, numpy, and torch.
Args:
seed (int): The seed value to set.
"""
random.seed(seed) # Python 内置的随机数生成器
np.random.seed(seed) # NumPy 随机数生成器
torch.manual_seed(seed) # PyTorch 随机数生成器(CPU)
torch.cuda.manual_seed(seed) # PyTorch 随机数生成器(GPU)
torch.cuda.manual_seed_all(seed) # 多个 GPU 的随机数种子
torch.backends.cudnn.deterministic = True # 保证卷积操作的确定性
torch.backends.cudnn.benchmark = False # 禁用自动优化,确保结果一致
def _make_w_io_base(f, mode: str):
if not isinstance(f, io.IOBase):
f_dirname = os.path.dirname(f)
if f_dirname != "":
os.makedirs(f_dirname, exist_ok=True)
f = open(f, mode=mode)
return f
def _make_r_io_base(f, mode: str):
if not isinstance(f, io.IOBase):
f = open(f, mode=mode)
return f
def jdump(obj, f, mode="w", indent=4, default=str):
"""Dump a str or dictionary to a file in json format.
Args:
obj: An object to be written.
f: A string path to the location on disk.
mode: Mode for opening the file.
indent: Indent for storing json dictionaries.
default: A function to handle non-serializable entries; defaults to `str`.
"""
f = _make_w_io_base(f, mode)
if isinstance(obj, (dict, list)):
json.dump(obj, f, indent=indent, default=default)
elif isinstance(obj, str):
f.write(obj)
else:
raise ValueError(f"Unexpected type: {type(obj)}")
f.close()
def jload(f, mode="r"):
"""Load a .json file into a dictionary."""
f = _make_r_io_base(f, mode)
jdict = json.load(f)
f.close()
return jdict
def truncate_sql_before_keywords(sql: str, keywords: list) -> str:
"""
Truncate the SQL query randomly before one of the clause keywords (case-sensitive, for uppercase keywords).
Args:
sql (str): The SQL query to truncate.
keywords (list): List of clause keywords in uppercase.
Returns:
str: Truncated SQL query.
"""
# Find positions of all keywords in the SQL query
positions = []
for keyword in keywords:
start_idx = 0
while (start_idx := sql.find(keyword.upper(), start_idx)) != -1:
positions.append(start_idx) # Add the position before the keyword
start_idx += len(keyword)
# If no keywords are found, return the original SQL
if not positions:
return sql
# Randomly select a truncation point
truncation_point = random.choice([pos for pos in positions if pos != 0])
# Truncate the SQL at the selected point
return sql[:truncation_point].strip()
def truncate_sql_before_keywords_v2(sql: str, keywords: list) -> list:
"""
Return all possible truncated SQL queries, truncating before one of the clause keywords.
Ensure that the truncation respects the order from short to long keywords.
Args:
sql (str): The SQL query to truncate.
keywords (list): List of clause keywords in uppercase.
Returns:
list: A list of all possible truncated SQL queries.
"""
# 对关键字按长度从短到长排序,确保长关键字不在短关键字之前截断
keywords = sorted(keywords, key=lambda k: len(k))
# 查找所有关键字的位置
positions = []
for keyword in keywords:
start_idx = 0
while (start_idx := sql.find(keyword, start_idx)) != -1:
positions.append((start_idx, keyword)) # 保存位置和关键字
start_idx += len(keyword)
# 如果没有找到任何关键字,返回原始 SQL 查询
if not positions:
return [sql]
# 按照关键字位置排序,确保先截断离起始位置最近的关键字
positions.sort()
# 生成所有可能的截断后的 SQL 查询
truncated_sqls = []
for i, (pos, keyword) in enumerate(positions):
if pos != 0: # 如果位置不在开头,才进行截断
truncated_sqls.append(sql[:pos].strip())
# 添加原始 SQL 查询到列表
truncated_sqls.append(sql)
return truncated_sqls
def normal_process(list_data_dict: list[dict, ...], args):
datasets_li = []
for idx, example in enumerate(list_data_dict):
dic_ = {}
if args.step_by_step:
dic_["input"] = f"{example['input']}"
dic_["target"] = f"{example['target']}"
else:
dic_["instruction"] = example['question']
dic_["input"] = example['schema'] + "\nAnswer: "
dic_['output'] = example['sql']
dic_['db_id'] = example['db_id']
dic_['history'] = []
# dic_["input"] = example['input']
# dic_["input"] = example['schema'] + "\nAnswer: "
# dic_['output'] = example['sql']
# dic_['db_id'] = example['db_id']
# dic_['history'] = []
datasets_li.append(dic_)
logging.warning("Storing data... This may take some time...")
jdump(datasets_li, os.path.join(os.path.dirname(args.file),
f"normal_process_{os.path.splitext(os.path.basename(args.file))[0]}.json"))
def random_truncation_process(list_data_dict: list[dict, ...], args):
ratio = args.ratio
total_examples = len(list_data_dict)
num_incomplete = int(total_examples * ratio)
# 随机选择一些索引来生成未完全的 SQL
incomplete_indices = set(random.sample(range(total_examples), num_incomplete))
datasets_li = []
for idx, example in enumerate(list_data_dict):
dic_ = {}
# 生成 SQL(未完全或完整)
if idx in incomplete_indices:
# 截断 SQL 生成未完全版本
if args.step_by_step:
incomplete_sql = truncate_sql_before_keywords(example['target'], CLAUSE_KEYWORDS)
dic_["input"] = f"{example['input']}" + f"{incomplete_sql}"
dic_["target"] = f"{example['target']}"
else:
incomplete_sql = truncate_sql_before_keywords(example['sql'], CLAUSE_KEYWORDS)
dic_["instruction"] = f"{example['question']}"
source = f"{example['schema']}" + "\nAnswer: "
source += incomplete_sql
dic_["input"] = source
dic_["output"] = f"{example['sql']}"
dic_['history'] = []
datasets_li.append(dic_)
else:
if args.step_by_step:
dic_["input"] = f"{example['input']}"
dic_["target"] = f"{example['target']}"
else:
dic_["instruction"] = f"{example['question']}"
source = f"{example['schema']}" + "\nAnswer: "
dic_["input"] = source
dic_["output"] = f"{example['sql']}"
dic_["history"] = []
datasets_li.append(dic_)
logging.warning("Storing data... This may take some time...")
jdump(datasets_li, os.path.join(os.path.dirname(args.file),
f"random_truncation_{os.path.splitext(os.path.basename(args.file))[0]}.json"))
def Progressive_Truncation(list_data_dict: list[dict, ...], args):
ratio = args.ratio
total_examples = len(list_data_dict)
num_incomplete = int(total_examples * ratio)
# 随机选择一些索引来生成未完全的 SQL
incomplete_indices = set(random.sample(range(total_examples), num_incomplete))
datasets_li = []
for idx, example in enumerate(list_data_dict):
dic_ = {}
# 生成 SQL(未完全或完整)
if idx in incomplete_indices:
# 截断 SQL 生成未完全版本
if args.step_by_step:
incomplete_sqls = truncate_sql_before_keywords_v2(example['target'], SQL_KEYWORDS)
else:
incomplete_sqls = truncate_sql_before_keywords_v2(example['sql'], SQL_KEYWORDS)
for incomplete_sql in incomplete_sqls:
temp_ = {}
if args.step_by_step:
temp_["input"] = f"{example['input']}" + f"{incomplete_sql}"
temp_["target"] = f"{example['target']}"
else:
temp_["instruction"] = f"{example['question']}"
source = f"{example['schema']}" + "\nAnswer: "
source += incomplete_sql
temp_["input"] = source
temp_["output"] = f"{example['sql']}"
temp_['history'] = []
datasets_li.append(temp_)
else:
if args.step_by_step:
dic_["input"] = f"{example['input']}"
dic_["target"] = f"{example['target']}"
else:
dic_["instruction"] = f"{example['question']}"
source = f"{example['schema']}" + "\nAnswer: "
dic_["input"] = source
dic_["output"] = f"{example['sql']}"
dic_["history"] = []
datasets_li.append(dic_)
logging.warning("Storing data... This may take some time...")
jdump(datasets_li, os.path.join(os.path.dirname(args.file),
f"progressive_truncation_{os.path.splitext(os.path.basename(args.file))[0]}.json"))
def preprocess(args):
data_path = args.file
process_type = args.type
logging.warning("Loading data...")
list_data_dict = jload(data_path)
logging.warning("Formatting inputs...")
if process_type == "Normal":
normal_process(list_data_dict, args)
elif process_type == "Random":
random_truncation_process(list_data_dict, args)
elif process_type == "Progressive":
Progressive_Truncation(list_data_dict, args)
if __name__ == '__main__':
set_seed(42)
parser = argparse.ArgumentParser(description="A simple argument parser example")
parser.add_argument("--file", type=str, help="The name of the file to process", default="data/bird_train_plus.json")
parser.add_argument("--type", type=str,
help="Processing Type (Normal | Random Truncation | Progressive Truncation)", default="Progressive")
parser.add_argument("--step_by_step",action="store_true", help="Disable step-by-step processing. Default is True.",
default=True)
parser.add_argument("--ratio", type=float, help="Truncated Sample Ratio", default=0.4)
args = parser.parse_args()
print(f"处理的文件:{args.file}, 类型为:{args.type}, 比例为:{args.ratio}\n")
preprocess(args)
logging.warning("Processing complete.")