-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_create_db_dresden.py
313 lines (277 loc) · 12.9 KB
/
my_create_db_dresden.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
import duckdb
from datadiscoverybench.utils import load_dresden_db, load_git_tables_db
from collections import defaultdict
import time
import os
from typing import Dict, List, Set, Tuple
import pandas as pd
tau = 2
def query_in_Row(x, y):
result_in_Row = f"""
SELECT colX1.TableId, colX1.RowId, colY.RowId
FROM
(SELECT TableId, RowId
FROM ALLTables
WHERE CellValue IN {x}
GROUP BY TableId, RowId
HAVING COUNT(DISTINCT CellValue) >= {tau}
) AS colX1,
(SELECT TableId, RowId
FROM ALLTables
WHERE CellValue IN {y}
GROUP BY TableId, RowId
HAVING COUNT(DISTINCT CellValue) >= {tau}
) AS colY
WHERE colX1.TableId = colY.TableId
AND colX1.RowId <> colY.RowId
"""
return result_in_Row
def query_in_Row_multi(x1, x2, y):
result_in_Row = f"""
SELECT colX1.TableId, colX1.RowId, colX2.RowId, colY.RowId
FROM
(SELECT TableId, RowId
FROM ALLTables
WHERE CellValue IN {x1}
GROUP BY TableId, RowId
HAVING COUNT(DISTINCT CellValue) >= {tau}
) AS colX1,
(SELECT TableId, RowId
FROM ALLTables
WHERE CellValue IN {x2}
GROUP BY TableId, RowId
HAVING COUNT(DISTINCT CellValue) >= {tau}
) AS colX2,
(SELECT TableId, RowId
FROM ALLTables
WHERE CellValue IN {y}
GROUP BY TableId, RowId
HAVING COUNT(DISTINCT CellValue) >= {tau}
) AS colY
WHERE colX1.TableId = colY.TableId
AND colX1.RowId <> colY.RowId
AND colX1.tableid = colX2.tableid
AND colX1.RowId <> colX2.RowId AND colX2.RowId <> colY.RowId
"""
return result_in_Row
import nltk
class Tokenizer():
def __init__(self) -> None:
pass
def tokenize(self, text):
stopwords = ['a', 'the', 'of', 'on', 'in', 'an', 'and', 'is', 'at', 'are', 'as', 'be', 'but', 'by', 'for', 'it',
'no', 'not', 'or', 'such', 'that', 'their', 'there', 'these', 'to', 'was', 'with', 'they', 'will', 'v', 've',
'd']
punct = ['!', '?', "'", '"']
try:
tok = ' '.join([w for w in nltk.tokenize.casual_tokenize(text, preserve_case=False) if w not in stopwords])
tok = ' '.join([w for w in nltk.tokenize.wordpunct_tokenize(tok) if w not in punct])
except:
return None
return tok
class trans:
def __init__(self):
self.con = None
self.con1 = None
self.con2 = None
self.connect_memory()
self.cache = {}
self.df = None
self.tableId_old = set()
def candidate_table(self, result_row, con):
tableId = set()
for row in range(result_row.shape[0]):
tableId.add(result_row.iloc[row][0])
tableId = tableId - self.tableId_old
if len(tableId) > 0:
for i in tableId:
my_query = f"""
SELECT *
FROM ALLTables
WHERE TableId == '{i}'
"""
if self.df is None:
self.df = con.execute(my_query).fetch_df()
else:
new_df = con.execute(my_query).fetch_df()
self.df = pd.concat([self.df, new_df])
def find_direct_trans_in_row(self, result_row, con, find_item, examples):
Toker = Tokenizer()
x_list = list(item[0] for item in examples)
y_list = list(item[1] for item in examples)
result_pairs = {}
for row in range(result_row.shape[0]):
TableId = result_row.iloc[row][0]
RowId = result_row.iloc[row][1]
RowId_2 = result_row.iloc[row][2]
my_query = f"""
SELECT *
FROM ALLTables
WHERE TableId == '{TableId}'
"""
self.df = con.execute(my_query).fetch_df()
# Determine if x and y are in the same row. If no, continue
tau_num = 0
df = self.df.loc[self.df['TableId'] == TableId]
for x, y in examples:
if x in df['CellValue'].values and y in df['CellValue'].values:
# num += 1
x_col = set()
res_1 = df.loc[df['CellValue'] == x]
res_1 = res_1.loc[res_1['RowId'] == RowId]
for i in range(res_1.shape[0]):
ColumnId1 = res_1.iloc[i][2]
x_col.add(ColumnId1)
y_col = set()
res_2 = df.loc[df['CellValue'] == y]
res_2 = res_2.loc[res_2['RowId'] == RowId_2]
for i in range(res_2.shape[0]):
ColumnId2 = res_2.iloc[i][2]
y_col.add(ColumnId2)
tag = False
for element in list(x_col):
if element in list(y_col):
tag = True
break
if tag:
if TableId not in result_pairs.keys():
result_pairs[TableId] = {(x, y)}
else:
result_pairs[TableId].add((x, y))
tau_num += 1
if tau_num < 2:
if tau_num == 1:
del result_pairs[TableId]
continue
# find the quries in condidate tables
for xq in find_item:
if xq in df['CellValue'].values:
res_1 = df.loc[df['CellValue'] == xq]
res_1 = res_1.loc[res_1['RowId'] == RowId]
for i in range(res_1.shape[0]):
ColumnId = res_1.iloc[i][2]
res_2 = df.loc[df['ColumnId'] == ColumnId]
res_2 = res_2.loc[res_2['RowId'] == RowId_2]
yq = res_2.iloc[0][0]
if not yq.strip():
continue
yq = str(Toker.tokenize(yq))
if TableId not in result_pairs.keys():
result_pairs[TableId] = {(xq, yq)}
else:
if xq in [x for x, y in result_pairs[TableId]]:
continue
result_pairs[TableId].add((xq, yq))
result_list = [x for x, y in result_pairs[TableId]]
if len([x for x in result_list if x not in x_list]) == 0:
del result_pairs[TableId]
self.tableId_old.add(k for k in result_pairs.keys())
return result_pairs
def connect_memory(self):
start = time.time()
dir_path = '/home/wang/remote/DataDiscoveryBenchmark/datadiscoverybench/data/dresden'
if not os.path.isdir(dir_path + '/disk_db/'):
os.mkdir(dir_path + '/disk_db/')
self.con = duckdb.connect(database=dir_path + '/disk_db/' + 'db1.txt')
self.con1 = duckdb.connect(database=dir_path + '/disk_db/' + 'db2.txt')
self.con2 = duckdb.connect(database=dir_path + '/disk_db/' + 'db3.txt')
# print(dir_path + '/disk_db/' + 'db1.txt')
# load_dresden_db(self.con, parts=list(range(250)), store_db=False)
# load_dresden_db(self.con1, parts=list(range(250, 350)), store_db=False)
# load_dresden_db(self.con2, parts=list(range(350, 500)), store_db=False)
# print(self.con.execute("SELECT count(*) FROM AllTables").fetch_df())
# print(self.con1.execute("SELECT count(*) FROM AllTables").fetch_df())
# print(self.con2.execute("SELECT count(*) FROM AllTables").fetch_df())
end = time.time()
print('loadung run time: %s Minutes' % ((end - start) * 0.01666667))
def direct_transformation(self, examples: Set[Tuple[str, str]], queries: Set[str]
) -> Dict[str, Set[Tuple[str, str]]]:
tables = defaultdict(set)
x = tuple(item[0] for item in examples)
y = tuple(item[1] for item in examples)
low_queries = {item.lower() for item in queries}
for i in range(2):
if i == 0:
result_tables = self.con.execute(query_in_Row(x, y)).fetch_df()
all_answers = self.find_direct_trans_in_row(result_tables, self.con, low_queries, examples)
elif i == 1:
result_tables = self.con1.execute(query_in_Row(x, y)).fetch_df()
all_answers = self.find_direct_trans_in_row(result_tables, self.con1, low_queries, examples)
elif i == 2:
result_tables = self.con2.execute(query_in_Row(x, y)).fetch_df()
all_answers = self.find_direct_trans_in_row(result_tables, self.con2, low_queries, examples)
if not result_tables.empty:
pass
# print("result_tables:", result_tables)
for tableid, pairs in all_answers.items():
for pair in pairs:
tables[tableid].add(pair)
return dict(tables)
def find_multi_trans_in_row(self, result_row, con, find_item, examples):
Toker = Tokenizer()
result_pairs = {}
for row in range(result_row.shape[0]):
TableId = result_row.iloc[row][0]
RowId = result_row.iloc[row][1]
RowId_2 = result_row.iloc[row][2]
RowId_3 = result_row.iloc[row][3]
my_query = f"""
SELECT *
FROM ALLTables
WHERE TableId == '{TableId}'
"""
self.df = con.execute(my_query).fetch_df()
df = self.df
for x1, x2 in find_item:
if x1 in df['CellValue'].values and x2 in df['CellValue'].values:
print("xq:", x1)
res_1 = df.loc[df['CellValue'] == x1]
res_1 = res_1.loc[res_1['RowId'] == RowId]
print(res_1)
for i in range(res_1.shape[0]):
ColumnId = res_1.iloc[i][2]
res_2 = df.loc[df['ColumnId'] == ColumnId]
res_2 = res_2.loc[res_2['RowId'] == RowId_3]
print("res_2:", res_2)
yq = res_2.iloc[0][0]
# print(xq, yq)
if not yq.strip():
continue
yq = str(Toker.tokenize(yq))
if TableId not in result_pairs.keys():
result_pairs[TableId] = {((x1, x2), yq)}
else:
if (x1, x2) in [x for x, y in result_pairs[TableId]]:
continue
result_pairs[TableId].add(((x1, x2), yq))
self.tableId_old.add(k for k in result_pairs.keys())
# print("pairs:", result_pairs[TableId])
return result_pairs
def multi_transformation(self, examples: Set[Tuple[Tuple[str, str], str]], queries: Set[Tuple[str, str]]
) -> Dict[str, Set[Tuple[Tuple[str, str], str]]]:
tables = defaultdict(set)
x = list(item[0] for item in examples)
x1 = tuple(item[0] for item in x)
x2 = tuple(item[1] for item in x)
y = tuple(item[1] for item in examples)
# low_queries = {item.lower() for item in queries}
for i in range(2):
if i == 0:
result_tables = self.con.execute(query_in_Row_multi(x1, x2, y)).fetch_df()
all_answers = self.find_multi_trans_in_row(result_tables, self.con, queries, examples)
elif i == 1:
result_tables = self.con1.execute(query_in_Row_multi(x1, x2, y)).fetch_df()
all_answers = self.find_multi_trans_in_row(result_tables, self.con1, queries, examples)
elif i == 2:
result_tables = self.con2.execute(query_in_Row_multi(x1, x2, y)).fetch_df()
all_answers = self.find_multi_trans_in_row(result_tables, self.con2, queries, examples)
print("result_tables:", result_tables)
if not result_tables.empty:
pass
# print("result_tables:", result_tables)
for tableid, pairs in all_answers.items():
for pair in pairs:
tables[tableid].add(pair)
# print("+_+_+_+_+_+_+__")
# print("keys:", len(list(tables[tableid)))
return dict(tables)