-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery_optimization.py
More file actions
253 lines (220 loc) · 10.4 KB
/
query_optimization.py
File metadata and controls
253 lines (220 loc) · 10.4 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
import re
from util_order import *
import pandas as pd
import random
from cal_sel import *
import os
import time
from llm import *
from tqdm import tqdm
def generate_output(sql_query, result_dir, file_candidate_dir,data):
where_clause = parse_sql(sql_query)
where_filter_condition = extract_filter_condition(where_clause)
where_filter_attribute = []
for condition in where_filter_condition:
attr = condition.split()[0]
if attr not in where_filter_attribute:
where_filter_attribute.append(attr)
select_clause = re.search(r'SELECT (.+?) FROM', sql_query, re.IGNORECASE).group(1)
select_attributes = [attr.strip() for attr in select_clause.split(',')]
output = pd.DataFrame(columns=select_attributes)
filter_data = {}
all_total_token = 0
all_actual_token = 0
file_candidate_dir = file_candidate_dir + "/candi/"
file_list = os.listdir(file_candidate_dir)
infor = result_dir + "infor.txt"
start_tie = time.time()
for onefile in tqdm(file_list, desc="Processing files", unit="file"):
###############################################################
########################SQL updated parts######################
###############################################################
input_text = ""
file_path = file_candidate_dir + onefile
with open(file_path, "r", encoding='utf-8',errors='ignore') as file:
input_text = file.read()
#extract filter condition
where_clause = parse_sql(sql_query)
###########################################
########acculate selectivity#############
###########################################
filter_cond = extract_filter_condition(where_clause)
attributes = parse_input(input_text)
#calculate total token
total_token = 0
total_token += len(input_text.split()) + 100
all_total_token += total_token
for filter_condtion in filter_cond:
atrribute,operator,value = filter_condtion.split(maxsplit=2)
selectivity = cal_sel(data, {"name": filter_condtion})
if atrribute in attributes:
filter_data[filter_condtion] = {
"name": filter_condtion,
"selectivity": selectivity,
's_value': calculate_s(selectivity, attributes[atrribute]['key_sentences']),
"key_sentences": attributes[atrribute]['key_sentences']
}
else:
filter_data[filter_condtion] = {
"name": filter_condtion,
"selectivity": selectivity,
's_value': 0,
"key_sentences": []
}
###########################################
######## Filter Order Optimization ########
###########################################
try:
sorted_filters = handle_sql(where_clause, filter_data)
except:
continue
actual_token = 0
skipthefile = 0
sql_copy = sql_query
remaining_attributes = select_attributes.copy()
curdata = {}
sorted_filter_cond = []
mapfilter_cond = {}
mapfilter_erro = {}
for filter in sorted_filters:
sorted_filter_cond.append(filter[0])
for filter in sorted_filter_cond:
mapfilter_cond[filter] = 0
mapfilter_erro[filter] = 0
remaining_sorted_filter_cond = sorted_filter_cond.copy()
while remaining_sorted_filter_cond:
filter_cond_tochange = []
filter_cond = remaining_sorted_filter_cond[0]
mapfilter_cond[filter_cond] += 1
if mapfilter_cond[filter_cond] >= 2:
if filter_cond in remaining_sorted_filter_cond:
remaining_sorted_filter_cond.remove(filter_cond)
tochange_filter = [filter_cond,'false']
filter_cond_tochange.append(tochange_filter)
continue
elif mapfilter_cond[filter_cond] == 1:
attribute = filter_cond.split()[0].strip()
key_sentences = ""
try:
for sentence in attributes[attribute]['key_sentences']:
key_sentences += sentence
except:
key_sentences = ""
answer = ask_completion4filtercondANDattr(str(remaining_attributes),str(remaining_sorted_filter_cond),key_sentences)
actual_token += len(key_sentences.split())+100
answer = remove_punctuation(answer)
try:
attributes_cur_all = answer.split("$$")[1]
filter_cond_cur_all = answer.split("$$")[0]
except:
mapfilter_cond[filter_cond] -= 1
mapfilter_erro[filter_cond] += 1
tochange_filter = [filter_cond,'false']
actual_token -= len(key_sentences.split())+100
if key_sentences == "":
filter_cond_tochange.append(tochange_filter)
remaining_sorted_filter_cond.remove(filter_cond)
actual_token += len(key_sentences.split())+100
for ask_filter_cond,bool_value in filter_cond_tochange:
sql_copy = sql_copy.replace(ask_filter_cond,bool_value)
sql_copy2 = sql_copy
bool_value_set_true = calculate_bool_value_true(sql_copy)
bool_value_set_false = calculate_bool_value_false(sql_copy2)
#set true to false
if bool_value_set_true == False:
skipthefile = 1
break
#set false to true
if bool_value_set_false == True:
skipthefile = 0
break
else:
skipthefile = 1
continue
#process attributes_cur_all
try:
minidatas = attributes_cur_all.split("##")
for minidata in minidatas:
dataattr,datavalue = minidata.split(":")
curdata[dataattr] = datavalue
if dataattr in remaining_attributes:
if 'NAN' not in datavalue:
remaining_attributes.remove(dataattr)
except:
a = 1
#extract filter_cond_cur_all
try:
minifilters = filter_cond_cur_all.split("##")
for minifilter in minifilters:
filter_cond_cur,bool_value = minifilter.split(":")
tochange_filter = [filter_cond_cur,bool_value]
if 'NAN' not in bool_value:
if filter_cond_cur in remaining_sorted_filter_cond:
filter_cond_tochange.append(tochange_filter)
remaining_sorted_filter_cond.remove(filter_cond_cur)
else:
if filter_cond_cur == filter_cond:
filter_cond_tochange.append([filter_cond_cur,'false'])
except:
filter_cond_tochange.append([filter_cond,'false'])
# set the filter_cond to the bool_value
for ask_filter_cond,bool_value in filter_cond_tochange:
sql_copy = sql_copy.replace(ask_filter_cond,bool_value)
sql_copy2 = sql_copy
bool_value_set_true = calculate_bool_value_true(sql_copy)
bool_value_set_false = calculate_bool_value_false(sql_copy2)
if bool_value_set_true == False:
skipthefile = 1
break
if bool_value_set_false == True:
skipthefile = 0
break
else:
skipthefile = 1
#if skipthefile == 1, then skip the file
if skipthefile == 0:
key_sentences = ""
mapattr = {}
for attr in select_attributes:
mapattr[attr] = 0
while remaining_attributes:
attr = remaining_attributes[0]
mapattr[attr] += 1
if mapattr[attr] == 2:
remaining_attributes.remove(attr)
curdata[attr] = "NAN"
continue
if attr in attributes:
key_sentences = ""
try:
for sentence in attributes[attr]['key_sentences']:
key_sentences += sentence + " "
except:
key_sentences = ""
answer = ask_completion4Multattribute(str(remaining_attributes), key_sentences)
actual_token += len(key_sentences.split())+75
answer = remove_punctuation(answer)
try:
minidatas = answer.split("##")
for minidata in minidatas:
dataattr,datavalue = minidata.split(":")
curdata[dataattr] = datavalue
if dataattr in remaining_attributes:
if 'NAN' not in datavalue:
remaining_attributes.remove(dataattr)
except:
actual_token -= len(key_sentences.split())+75
if len(curdata) != 0:
curdata['file'] = onefile
output = output.append(curdata,ignore_index=True)
data = data.append(curdata,ignore_index=True)
output.dropna(axis=0, how='all', inplace=True)
all_actual_token += actual_token
end_time = time.time()
with open(infor, "w") as file:
file.write("all_actual_token: "+str(all_actual_token)+"\n")
file.write("all_total_token: "+str(all_total_token)+"\n")
file.write("time: "+str(end_time-start_tie)+"\n")
output.to_csv(result_dir + "output.csv", index=False)
print("output saved to: ", result_dir + "output.csv")
return output