File size: 27,692 Bytes
778d47d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
import json
import os
import re
import random
import sqlparse
from tqdm import tqdm

from nltk.tokenize import word_tokenize
from nltk import ngrams
from sql_metadata import Parser
from utils.db_utils import get_db_schema
from utils.bird_csv_utils import load_db_descriptions
import subprocess

random.seed(42)

def extract_large_numbers(text):
    number_information = []
    patterns = {
        'thousand': 10**3,
        'million': 10**6,
        'billion': 10**9,
        'trillion': 10**12
    }
    
    for word, multiplier in patterns.items():
        matches = re.findall(r'(\d+\.?\d*)\s*{}'.format(word), text, flags=re.IGNORECASE)
        for match in matches:
            number = float(match) * multiplier
            number_information.append(match + " " + word + " = " + str(int(number)))
    
    for phrase, number in {'thousands of': 10**3, 'millions of': 10**6, 'billions of': 10**9, 'trillions of': 10**12}.items():
        if phrase in text:
            number_information.append(phrase + " = " + str(int(number)))
    
    large_number_evidence = ""
    for info in number_information:
        large_number_evidence += info + "; "
    
    return large_number_evidence.strip()

def remove_table_alias(s):
    try:
        tables_aliases = Parser(s).tables_aliases
    except Exception as e:
        return s

    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():
        # remove AS clauses
        s = s.replace("AS " + k + " ", "")
        # replace table alias with thier original names
        s = s.replace(k, v)
    
    return s

def remove_similar_comments(names, comments):
    '''
    Remove table (or column) comments that have a high degree of similarity with their names
    
    Arguments:
        names: a list of table (or column) names
        comments: a list of table (or column) comments
    
    Returns:
        new_comments: a list of new table (or column) comments
    '''
    new_comments = []
    for name, comment in zip(names, comments):    
        if name.replace("_", "").replace(" ", "") == comment.replace("_", "").replace(" ", ""):
            new_comments.append("")
        else:
            new_comments.append(comment)
    
    return new_comments

def str_replace_ignore_case(evidence, schema_item_name):
    evidence = re.sub(re.escape(schema_item_name), schema_item_name, evidence, 0, re.IGNORECASE)

    return evidence

def obtain_n_grams(sequence, max_n):
    '''
    returns all grams of sequence less than or equal to `max_n`
    '''
    tokens = word_tokenize(sequence)
    all_grams = []
    for n in range(1, max_n + 1):
        all_grams.extend([" ".join(gram) for gram in ngrams(tokens, n)])
    
    return all_grams

def preprocess_evidence(evidence, schema_items):
    if evidence.strip() == "":
        return ""

    evidence = evidence.strip()
    # if evidence does not end with ";", add a ";" char
    if not evidence.endswith(";"):
        evidence += ";"
    
    # lowercase schema items appeared in the evidence
    for table in schema_items:
        if table["table_name"] in evidence.lower():
            evidence = str_replace_ignore_case(evidence, table["table_name"])

        for column_name in table["column_names"]:
            if column_name in evidence.lower():
                evidence = str_replace_ignore_case(evidence, column_name)
    
    evidence = evidence.replace("< =", "<=").replace("> =", ">=")

    return evidence

import os
from multiprocessing import Pool
from itertools import repeat
from tqdm import tqdm
import sqlparse
# from moz_sql_parser import Parser  # Assuming you're using moz_sql_parser

def process_data(data, db_path, db_comments, db_content_index_api, source, use_evidence, mode,
                 all_db_descriptions=None):
    sample = {}
    db_id = data["db_id"]

    sample["source"] = source
    sample["db_id"] = db_id
    sample["db_path"] = os.path.join(db_path, db_id, db_id + ".sqlite")

    if "spider-syn" in source:
        sample["question"] = data["SpiderSynQuestion"]
        sample["evidence"] = ""
    elif "bird" in source:
        sample["question"] = data["question"]
    elif "bank" in source:
        sample["question"] = data["question"]
        sample["evidence"] = extract_large_numbers(data["question"])
    else:
        sample["question"] = data["question"]
        sample["evidence"] = ""
    
    if "\n" in sample["question"]:
        sample["question"] = sample["question"].replace("\n", " ")
    
    db_descriptions = all_db_descriptions.get(db_id, {}) if all_db_descriptions else {}
    sample["schema"] = get_db_schema(
        db_content_index_api, source, sample["question"], sample["db_path"],
        db_comments, db_id, db_descriptions=db_descriptions,
    )
    if "bird" in source:
        evidence = preprocess_evidence(data["evidence"], sample["schema"]["schema_items"])
        sample["evidence"] = evidence

    if "\n" in sample["evidence"]:
        sample["evidence"] = sample["evidence"].replace("\n", " ")

    sample["text"] = sample["evidence"] + " " + sample["question"] \
        if use_evidence and sample["evidence"] != "" else sample["question"]

    if mode in ["train", "dev"]:
        sql = data["SQL"] if source in ["bird-dev", "bird-train"] else data["query"]
        sample['sql'] = sql
        # sample["sql"] = remove_table_alias(sqlparse.format(sql, keyword_case="upper", identifier_case="lower"))
    elif mode == "test":
        sample["sql"] = ""
    
    sample["table_labels"], sample["column_labels"] = [], []
    try:
        sql_tokens = [token.value for token in Parser(sample["sql"].lower()).tokens]
    except Exception as e:
        sql_tokens = sample["sql"].lower().split()
    
    for table_info in sample["schema"]["schema_items"]:
        if mode in ["train", "dev"]:
            table_name = table_info["table_name"]
            sample["table_labels"].append(1 if table_name in sql_tokens else 0)
            sample["column_labels"].append([
                1 if column_name in sql_tokens or f"{table_name}.{column_name}" in sql_tokens else 0
                for column_name in table_info["column_names"]
            ])
        elif mode == "test":
            sample["table_labels"].append(0)
            sample["column_labels"].append([0 for _ in range(len(table_info["column_names"]))])

    # Coarse-grained matching between the input text and all contents in the database

    
    return sample

def process_data_wrapper(args):
    return process_data(*args)


def _load_bird_descriptions(db_path, source):
    """Return {db_id: descriptions} when source is BIRD, else empty dict."""
    if "bird" not in source:
        return {}
    from pathlib import Path
    all_desc = {}
    root = Path(db_path)
    for db_dir in sorted(root.iterdir()):
        if db_dir.is_dir():
            all_desc[db_dir.name] = load_db_descriptions(str(db_dir))
    return all_desc

def spider_style_dataset(
    dataset_path, 
    db_path, 
    db_content_index_api, 
    source, 
    table_json_path,
    use_evidence,
    mode,
    output_file
):
    '''
    Load spider-style dataset
    
    Arguments:
        dataset_path: directory to load the dataset from
        db_path: directory of databases (used for extracting schema, including tables, columns, column contents, and foreign keys)
        db_content_index_path: directory of database content sparse index
        source: source of examples
        table_json_path: directory to load additional database information (used for extracting comments for tables and columns)
        use_evidence: whether to use the additional evidence in the input sequence
    Returns:
        returned_dataset: prepared dataset
    '''
    dataset = json.load(open(dataset_path))
    additional_db_info = json.load(open(table_json_path))

    # load old results from output_file if it exists
    if os.path.exists(output_file):
        with open(output_file, 'r', encoding='utf-8') as f:
            processed_dataset = [json.loads(line) for line in f]
        processed_dataset_dict = {f"{sample['db_id']} {sample['question']}": sample for sample in processed_dataset}
    else:
        processed_dataset_dict = dict()
    
    # filter out processed samples
    dataset = [data for data in dataset if f"{data['db_id']} {data.get('question', '')}" not in processed_dataset_dict]

    db_comments = dict()
    # record comments for tables and columns
    for db_info in additional_db_info:
        comment_dict = dict()

        column_names = [column_name.lower() for _, column_name in db_info["column_names_original"]]
        table_idx_of_each_column = [t_idx for t_idx, _ in db_info["column_names_original"]]
        column_comments = [column_comment.lower() for _, column_comment in db_info["column_names"]]
        
        assert len(column_names) == len(column_comments)
        column_comments = remove_similar_comments(column_names, column_comments)

        table_names = [table_name.lower() for table_name in db_info["table_names_original"]]
        table_comments = [table_comment.lower() for table_comment in db_info["table_names"]]
        
        assert len(table_names) == len(table_comments)
        table_comments = remove_similar_comments(table_names, table_comments)

        # enumerate each table and its columns
        for table_idx, (table_name, table_comment) in enumerate(zip(table_names, table_comments)):
            comment_dict[table_name] = {
                "table_comment": table_comment,
                "column_comments": dict()
            }
            for t_idx, column_name, column_comment in zip(table_idx_of_each_column, column_names, column_comments):
                # record columns in current table
                if t_idx == table_idx:
                    comment_dict[table_name]["column_comments"][column_name] = column_comment

        db_comments[db_info["db_id"]] = comment_dict


    all_db_descriptions = _load_bird_descriptions(db_path, source)

    args_iter = zip(
        dataset,
        repeat(db_path),
        repeat(db_comments),
        repeat(db_content_index_api),
        repeat(source),
        repeat(use_evidence),
        repeat(mode),
        repeat(all_db_descriptions),
    )


    pool =  Pool(processes=16)
    f_out = open(output_file, 'a+', encoding='utf-8')

    try:
        for sample in tqdm(
            pool.imap_unordered(process_data_wrapper, args_iter),
            total=len(dataset),
            desc="Processing dataset"
        ):
            # Write the JSON serialized sample to the file
            f_out.write(json.dumps(sample, ensure_ascii=False) + '\n')
    except Exception as e:
        print(e)
        f_out.close()
        pool.close()
        import sys
        sys.exit()
    
    f_out.close()
    pool.close()

    # rearrange the dataset, load jsonl file, rearrange the dataset to correct order with the same order as the original dataset, key= {db_id + question}
    processed_dataset = []
    with open(output_file, 'r', encoding='utf-8') as f_in:
        for line in f_in:
            sample = json.loads(line)
            processed_dataset.append(sample)

    dataset = json.load(open(dataset_path))
    rearranged_dataset = []
    for data in dataset:
        db_id = data["db_id"]
        if "spider-syn" in source:
            question = data["SpiderSynQuestion"]
        else:
            question = data["question"]

        key = db_id + " " + question.replace("\n", " ")
        for sample in processed_dataset:
            if sample["db_id"] + " " + sample["question"].replace("\n", " ") == key:
                rearranged_dataset.append(sample)
                break
    
    # save the rearranged dataset to json file, replace jsonl in output_file to json
    with open(output_file.replace(".jsonl", ".json"), 'w+', encoding='utf-8') as f_out:
        json.dump(rearranged_dataset, f_out, indent=2, ensure_ascii=False)

    return rearranged_dataset
    

if __name__ == "__main__":
    print("BIRD-dev (with evidence)")
    # BIRD dev set (1534 examples)
    bird_with_evidence_dev = spider_style_dataset(
        dataset_path = "./data/bird-062024/dev/dev.json", 
        db_path = "./data/bird-062024/dev/dev_databases", 
        db_content_index_api = "http://localhost:8005",
        source = "bird-dev",
        table_json_path = "./data/bird-062024/dev/dev_tables.json",
        use_evidence = True,
        mode = "dev",
        output_file="data/full_value_matching_sft_bird_062024_with_evidence_dev_text2sql.jsonl"
    )

    # print("BIRD (with evidence) train")
    # # BIRD training set with evidence (9428 examples)
    # bird_with_evidence_train = spider_style_dataset(
    #     dataset_path = "./data/bird-062024/train/train.json", 
    #     db_path = "./data/bird-062024/train/train_databases", 
    #     db_content_index_api = "http://localhost:8005",
    #     source = "bird-train",
    #     table_json_path = "./data/bird-062024/train/train_tables.json",
    #     use_evidence = True,
    #     mode = "train",
    #     output_file="data/full_value_matching_sft_bird_062024_with_evidence_train_text2sql.jsonl"
    # )


    # print("BIRD-dev (with evidence)")
    # bird_with_evidence_dev = spider_style_dataset(
    #     dataset_path = "data/sft_data_collections/bird/dev/dev.json", 
    #     db_path = "data/sft_data_collections/bird/dev/dev_databases", 
    #     db_content_index_path = "data/sft_data_collections/bird/dev/db_contents_index",
    #     source = "bird-dev",
    #     table_json_path = "data/sft_data_collections/bird/dev/dev_tables.json",
    #     use_evidence = True,
    #     mode = "dev",
    #     output_file="./data/sft_bird_with_evidence_dev_text2sql.jsonl"
    # )

    # print("BIRD (with evidence) train")
    # # BIRD training set with evidence (9428 examples)
    # bird_with_evidence_train = spider_style_dataset(
    #     dataset_path = "data/sft_data_collections/bird/train/train.json", 
    #     db_path = "data/sft_data_collections/bird/train/train_databases", 
    #     db_content_index_path = "data/sft_data_collections/bird/train/db_contents_index",
    #     source = "bird-train",
    #     table_json_path = "data/sft_data_collections/bird/train/train_tables.json",
    #     use_evidence = True,
    #     mode = "train",
    #     output_file="./data/sft_bird_with_evidence_train_text2sql.jsonl"
    # )

    # print("preparing training sets.....")
    # print("spider-train")
    # spider_train = []
    # # Spider training set-1 (7000 + 1658 examples)
    # for spider_train_set in ["train_spider.json", "train_others.json"]:
    #     spider_train.extend(
    #         spider_style_dataset(
    #             dataset_path = os.path.join("./data/sft_data_collections/spider/", spider_train_set), 
    #             db_path = "./data/sft_data_collections/spider/database", 
    #             db_content_index_path = "./data/sft_data_collections/spider/db_contents_index",
    #             source = "spider-train",
    #             table_json_path = "./data/sft_data_collections/spider/tables.json",
    #             use_evidence = False,
    #             mode = "train",
    #             output_file=f"./data/sft_spider_train_text2sql_{spider_train_set}.jsonl"
    #         )
    #     )
    # with open("./data/sft_spider_train_text2sql.json", "w") as f:
    #     f.write(json.dumps(spider_train, indent = 2, ensure_ascii = False))
    # print("preparing training sets.....")
    # print("spider-train")
    # spider_train = []
    # # Spider training set-1 (7000 + 1658 examples)
    # for spider_train_set in ["train_spider.json", "train_others.json"]:
    #     spider_train.extend(
    #         spider_style_dataset(
    #             dataset_path = os.path.join("./data/sft_data_collections/spider/", spider_train_set), 
    #             db_path = "./data/sft_data_collections/spider/database", 
    #             db_content_index_api = "http://localhost:8005",
    #             source = "spider-train",
    #             table_json_path = "./data/sft_data_collections/spider/tables_update.json",
    #             use_evidence = False,
    #             mode = "train",
    #             output_file=f"./data/sft_spider_train_with_meaning_text2sql_{spider_train_set}.jsonl"
    #         )
    #     )
    # with open("./data/sft_spider_train_with_meaning_text2sql.json", "w") as f:
    #     f.write(json.dumps(spider_train, indent = 2, ensure_ascii = False))

    # print("preparing training sets.....")
    # print("spider-train-augmented")
    # spider_train = []
    # spider_dev = spider_style_dataset(
    #     dataset_path = "./data/sft_data_collections/spider/train_augmented.json", 
    #     db_path = "./data/sft_data_collections/spider/database", 
    #     db_content_index_api = "http://localhost:8005",
    #     source = "spider-train",
    #     table_json_path = "./data/sft_data_collections/spider/tables.json",
    #     use_evidence = False,
    #     mode = "train",
    #     output_file='./data/sft_spider_train_augmented_text2sql.jsonl'
    # )

    # print("BIRD (without evidence) train")
    # # BIRD training set (9428 examples)
    # bird_train = spider_style_dataset(
    #     dataset_path = "./data/bird-062024/train/train.json", 
    #     db_path = "./data/bird-062024/train/train_databases", 
    #     db_content_index_path = "./data/bird-062024/train/db_contents_index",
    #     source = "bird-train",
    #     table_json_path = "./data/bird-062024/train/train_tables.json",
    #     use_evidence = False,
    #     mode = "train"
    # )
    # with open("./data/sft_bird_train_text2sql.json", "w") as f:
    #     f.write(json.dumps(bird_train, indent = 2, ensure_ascii = False))

    
    # with open("./data/sft_bird_with_evidence_train_text2sql.json", "w") as f:
    #     f.write(json.dumps(bird_with_evidence_train, indent = 2, ensure_ascii = False))
    
    
    # print("---------------------------------------------------------------------------")
    # print("preparing dev sets.....")
    # print("spider-dev")
    # # Spider development set (1034 examples)
    # spider_dev = spider_style_dataset(
    #     dataset_path = "./data/sft_data_collections/spider/dev.json", 
    #     db_path = "./data/sft_data_collections/spider/database", 
    #     db_content_index_api = "http://localhost:8005",
    #     source = "spider-dev",
    #     table_json_path = "./data/sft_data_collections/spider/tables.json",
    #     use_evidence = False,
    #     mode = "dev",
    #     output_file='./data/1_value_sft_spider_dev_text2sql.jsonl'
    # )

    # print("---------------------------------------------------------------------------")
    # print("preparing dev sets.....")
    # print("spider-dev")
    # # Spider development set (1034 examples)
    # spider_dev = spider_style_dataset(
    #     dataset_path = "./data/sft_data_collections/spider/dev.json", 
    #     db_path = "./data/sft_data_collections/spider/database", 
    #     db_content_index_api = "http://localhost:8005",
    #     source = "spider-dev",
    #     table_json_path = "./data/sft_data_collections/spider/tables_update.json",
    #     use_evidence = False,
    #     mode = "dev",
    #     output_file='./data/sft_spider_dev_with_meaning_text2sql.jsonl'
    # )


    # print("spider-dk")
    # # Spider-DK development set (535 examples)
    # spider_dk = spider_style_dataset(
    #     dataset_path = "./data/sft_data_collections/Spider-DK/Spider-DK.json", 
    #     db_path = "./data/sft_data_collections/spider/database", 
    #     db_content_index_api = f"http://localhost:8005",
    #     source = "spider-dk",
    #     table_json_path = "./data/sft_data_collections/Spider-DK/tables.json",
    #     use_evidence = False,
    #     mode = "dev",
    #     output_file='./data/1_value_sft_spider_dk_text2sql.jsonl'
    # )
    
    # print("spider-syn")
    # # Spider-Syn development set (1034 examples)
    # spider_syn = spider_style_dataset(
    #     dataset_path = "./data/sft_data_collections/Spider-Syn/Spider-Syn/dev.json", 
    #     db_path = "./data/sft_data_collections/spider/database", 
    #     db_content_index_api = f"http://localhost:8005",
    #     source = "spider-syn-dev",
    #     table_json_path = "./data/sft_data_collections/spider/tables.json",
    #     use_evidence = False,
    #     mode = "dev",
    #     output_file='./data/1_value_sft_spider_syn_text2sql.jsonl'
    # )
    
    # print("spider-realistic")
    # # Spider-Realistic development set (507 examples)
    # spider_realistic = spider_style_dataset(
    #     dataset_path = "./data/sft_data_collections/spider-realistic/spider-realistic.json", 
    #     db_path = "./data/sft_data_collections/spider/database", 
    #    db_content_index_api = f"http://localhost:8005",
    #     source = "spider-realistic",
    #     table_json_path = "./data/sft_data_collections/spider/tables.json",
    #     use_evidence = False,
    #     mode = "dev",
    #     output_file='./data/1_value_sft_spider_realistic_text2sql.jsonl'
    # )

    # import signal
    # print("DR.spider")
    # dr_spider = []
    # # Dr.Spider has 17 perturbation test sets
    # test_set_names = os.listdir("./data/sft_data_collections/diagnostic-robustness-text-to-sql/data")
    # test_set_names.remove("Spider-dev")
    # port = 8005
    # for test_set_name in test_set_names:
    #     if test_set_name.startswith("DB_"):
    #         database_file_path = "database_post_perturbation"
    #         table_file_name = "tables_post_perturbation.json"
    #     else:
    #         database_file_path = "databases"
    #         table_file_name = "tables.json"

    #     source = "dr.spider-{}".format(test_set_name)
    #     # run db content retrieval for each test set
    #     process = subprocess.Popen(f"python db_content_retrieval/lsh_api.py --port {port} --db_content_index {source}", shell=True)
    #     pid = process.pid
    #     # os.system(f"python db_content_retrieval/lsh_api.py --db_content_index {source}")
    #     import time
    #     time.sleep(10)
    #     dr_spider.extend(
    #             spider_style_dataset(
    #             dataset_path = os.path.join("./data/sft_data_collections/diagnostic-robustness-text-to-sql/data/", test_set_name, "questions_post_perturbation.json"), 
    #             db_path = os.path.join("./data/sft_data_collections/diagnostic-robustness-text-to-sql/data/", test_set_name, database_file_path), 
    #             db_content_index_api = f"http://localhost:{port}",
    #             source = source,
    #             table_json_path = os.path.join("./data/sft_data_collections/diagnostic-robustness-text-to-sql/data/", test_set_name, table_file_name),
    #             use_evidence = False,
    #             mode = "dev",
    #             output_file=f'./data/sft_dr_spider_text2sql_{test_set_name}.jsonl'
    #         )
    #     )
    #     # kill db content retrieval server
    #     # os.kill(pid, signal.SIGTERM)   # usually kills processes
    #     # os.kill(pid, signal.SIGKILL)   # should always kill a process
    #     os.system(f"kill -9 `ps aux | grep lsh_api.py | awk '{{print $2}}'`")
    #     time.sleep(2)
    # with open("./data/sft_dr_spider_text2sql.json", "w") as f:
    #     f.write(json.dumps(dr_spider, indent = 2, ensure_ascii = False))


    # print("BIRD-dev (without evidence)")
    # # BIRD dev set (1534 examples)
    # bird_dev = spider_style_dataset(
    #     dataset_path = "./data/bird-062024/dev/dev.json", 
    #     db_path = "./data/bird-062024/dev/dev_databases", 
    #     db_content_index_path = "./data/bird-062024/dev/db_contents_index",
    #     source = "bird-dev",
    #     table_json_path = "./data/bird-062024/dev/dev_tables.json",
    #     use_evidence = False,
    #     mode = "dev"
    # )
    # with open("./data/sft_bird_dev_text2sql.json", "w") as f:
    #     f.write(json.dumps(bird_dev, indent = 2, ensure_ascii = False))

    
    # print("Bank_Financials dev set")
    # # Bank_Financials dev set (92 examples)
    # bank_dev = spider_style_dataset(
    #     dataset_path = "./data/sft_data_collections/domain_datasets/Bank_Financials_dev.json", 
    #     db_path = "./data/sft_data_collections/domain_datasets/databases", 
    #     db_content_index_api = "http://localhost:8005",
    #     source = "bank_financials-dev",
    #     table_json_path = "./data/sft_data_collections/domain_datasets/tables.json",
    #     use_evidence = True,
    #     mode = "dev",
    #     output_file="./data/sft_bank_financials_dev_text2sql.jsonl"
    # )
    
    # print("Aminer_Simplified dev set")
    # # Aminer_Simplified dev set (xxx examples)
    # aminer_dev = spider_style_dataset(
    #     dataset_path = "./data/sft_data_collections/domain_datasets/Aminer_Simplified_dev.json", 
    #     db_path = "./data/sft_data_collections/domain_datasets/databases", 
    #     db_content_index_api = "http://localhost:8005",
    #     source = "aminer_simplified-dev",
    #     table_json_path = "./data/sft_data_collections/domain_datasets/tables.json",
    #     use_evidence = True,
    #     mode = "dev",
    #     output_file="./data/sft_aminer_simplified_dev_text2sql.jsonl"
    # )


    # print("Bank_Financials train")
    # # Bank_Financials train set
    # bank_train = spider_style_dataset(
    #     dataset_path = "./data/sft_data_collections/domain_datasets/Bank_Financials_train.json", 
    #     db_path = "./data/sft_data_collections/domain_datasets/databases", 
    #     db_content_index_api = "http://localhost:8005",
    #     source = "bank_financials-train",
    #     table_json_path = "./data/sft_data_collections/domain_datasets/tables.json",
    #     use_evidence = True,
    #     mode = "train",
    #     output_file="./data/sft_bank_financials_train_text2sql.jsonl"
    # )

    # print("Aminer_Simplified train")
    # # Aminer_Simplified train set
    # aminer_train = spider_style_dataset(
    #     dataset_path = "./data/sft_data_collections/domain_datasets/Aminer_Simplified_train.json", 
    #     db_path = "./data/sft_data_collections/domain_datasets/databases", 
    #     db_content_index_api = "http://localhost:8005",
    #     source = "aminer_simplified-train",
    #     table_json_path = "./data/sft_data_collections/domain_datasets/tables.json",
    #     use_evidence = True,
    #     mode = "train",
    #     output_file="./data/sft_aminer_simplified_train_text2sql.jsonl"
    # )
    
    # print("Spider + BIRD + Bank_Financials + Aminer_Simplified train set (ALL MERGED)")
    # # merge all available training data
    # with open("./data/sft_all_merged_train_text2sql.json", "w") as f:
    #     f.write(json.dumps(spider_train + bird_with_evidence_train + bank_train + aminer_train, indent = 2, ensure_ascii = False))
    

    pass


    # Other un-official SFT files for testing
    # print("preparing training sets.....")
    # print("spider-train")
    # os.remove('./data/sft_spider_train_domain_geo.jsonl')
    # spider_style_dataset(
    #     dataset_path = './data/sft_data_collections/spider_domain_geo.json', 
    #     db_path = "./data/sft_data_collections/spider/database", 
    #     db_content_index_api = "http://localhost:8005",
    #     source = "spider-train",
    #     table_json_path = "./data/sft_data_collections/spider/tables.json",
    #     use_evidence = False,
    #     mode = "train",
    #     output_file=f"./data/sft_spider_train_domain_geo.jsonl"
    # )