-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash_based_join.py
executable file
·312 lines (272 loc) · 12.1 KB
/
hash_based_join.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 sys,os,time
sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(1, os.path.abspath('..'))
from charm.toolbox.pairinggroup import ZR
from fhipe import ipe
import csv
import timeit
IN_CLAUSE_MAX_SIZE=1
selectivity_1 = False
# infile contains a CSV of a database table
# returns an array of dictionaries where keys are
# the table attributes and values are strings
def read_table(infile, delimiter=","):
with open(infile) as f:
return [{k: str(v) for k, v in row.items()} for row in csv.DictReader(f, delimiter=delimiter, skipinitialspace=True)]
# for a query:
# SELECT * FROM A
# INNER JOIN B ON A.a = B.b
# WHERE A.x IN (x_c) AND B.y IN (y_c)
# such that a is a primary key, and b is the corresponding foreign key
# returns the count of resulting rows
def hash_based_join(pp, msk, encrypted_table_a, encrypted_table_b, x_c, y_c):
decryptions = []
(detB, B, Bstar, group, g1, g2) = msk
k = group.random(ZR)
k_a = ipe.encryptQuery(msk, k, x_c, IN_CLAUSE_MAX_SIZE)
k_b = ipe.encryptQuery(msk, k, y_c, IN_CLAUSE_MAX_SIZE)
# apply selection first: skip decryption of row if row doesn't satisfy where condition
hash_table = {}
for (pk, x, c_a) in encrypted_table_a:
if len(x_c) == 0 or x in x_c:
d_start = time.time()
d = ipe.decrypt(pp, k_a, c_a)
d_end = time.time()
decryptions.append(d_end - d_start)
hash_table[d] = pk
matches = []
for (pk, y, c_b) in encrypted_table_b:
if len(y_c) != 0 and y not in y_c:
continue
d_start = time.time()
d = ipe.decrypt(pp, k_b, c_b)
d_end = time.time()
decryptions.append(d_end - d_start)
match = hash_table.get(d)
if match:
matches.append((match, pk))
return (matches, decryptions)
######################################### EXPERIMENTS ###########################################
# NOTE: you must add a row with column heads to the tables that you will use. one should use the
# attribute names used here: http://www.tpc.org/tpc_documents_current_versions/pdf/tpc-h_v2.17.1.pdf#page=13
def experiment_1(pp, msk, sf, iters):
print("--------------------------- sf="+sf)
table_a_file = "data/"+sf+"/customer.tbl"
a = "custkey"
pk_a = a
x = "selectivity"
table_b_file = "data/"+sf+"/orders.tbl"
b = "custkey"
pk_b = "orderkey"
y = "selectivity"
print('starting to encrypt customer')
table_a = read_table(table_a_file, '|')
encrypted_table_a = ipe.encryptTable(msk, table_a, pk_a, a, x, IN_CLAUSE_MAX_SIZE)
print('done encrypting customer')
print('starting to encrypt orders')
table_b = read_table(table_b_file, '|')
encrypted_table_b = ipe.encryptTable(msk, table_b, pk_b, b, y, IN_CLAUSE_MAX_SIZE)
print('done encrypting orders')
print('')
selectivity_100_results = []
selectivity_50_results = []
selectivity_25_results = []
selectivity_12_5_results = []
selectivity_1_results = []
for i in range(iters):
# selectivity = 1/100
x_c = ["100"]
y_c = ["100"]
query_start = time.time()
(matches, decryptions) = hash_based_join(pp, msk, encrypted_table_a, encrypted_table_b, x_c, y_c)
query_end = time.time()
selectivity_100_results.append(query_end - query_start)
print('selectivity=1/100 took: {}s overall'.format(query_end - query_start))
print('time spent NOT doing decryption: {}s'.format((query_end - query_start) - sum(decryptions)))
print('time per decryption took: {}ms'.format((sum(decryptions) / len(decryptions)) * 1000))
print('num matches: {}'.format(len(matches)))
print('')
# selectivity = 1/50
x_c = ["50"]
y_c = ["50"]
query_start = time.time()
(matches, decryptions) = hash_based_join(pp, msk, encrypted_table_a, encrypted_table_b, x_c, y_c)
query_end = time.time()
selectivity_50_results.append(query_end - query_start)
print('selectivity=1/50 took: {}s overall'.format(query_end - query_start))
print('time spent NOT doing decryption: {}s'.format((query_end - query_start) - sum(decryptions)))
print('time per decryption took: {}ms'.format((sum(decryptions) / len(decryptions)) * 1000))
print('num matches: {}'.format(len(matches)))
print('')
# selectivity = 1/25
x_c = ["25"]
y_c = ["25"]
query_start = time.time()
(matches, decryptions) = hash_based_join(pp, msk, encrypted_table_a, encrypted_table_b, x_c, y_c)
query_end = time.time()
selectivity_25_results.append(query_end - query_start)
print('selectivity=1/25 took: {}s overall'.format(query_end - query_start))
print('time spent NOT doing decryption: {}s'.format((query_end - query_start) - sum(decryptions)))
print('time per decryption took: {}ms'.format((sum(decryptions) / len(decryptions)) * 1000))
print('num matches: {}'.format(len(matches)))
print('')
# selectivity = 1/12.5
x_c = ["12.5"]
y_c = ["12.5"]
query_start = time.time()
(matches, decryptions) = hash_based_join(pp, msk, encrypted_table_a, encrypted_table_b, x_c, y_c)
query_end = time.time()
selectivity_12_5_results.append(query_end - query_start)
print('selectivity=1/12.5 took: {}s overall'.format(query_end - query_start))
print('time spent NOT doing decryption: {}s'.format((query_end - query_start) - sum(decryptions)))
print('time per decryption took: {}ms'.format((sum(decryptions) / len(decryptions)) * 1000))
print('num matches: {}'.format(len(matches)))
print('')
# selectivity = 1/1
if selectivity_1:
x_c = []
y_c = []
query_start = time.time()
(matches, decryptions) = hash_based_join(pp, msk, encrypted_table_a, encrypted_table_b, x_c, y_c)
query_end = time.time()
selectivity_1_results.append(query_end - query_start)
print('selectivity=1/1 took: {}s overall'.format(query_end - query_start))
print('time spent NOT doing decryption: {}s'.format((query_end - query_start) - sum(decryptions)))
print('time per decryption took: {}ms'.format((sum(decryptions) / len(decryptions)) * 1000))
print('num matches: {}'.format(len(matches)))
print('')
# aggregations
print('--------------- AVERAGES OVER ITERATIONS')
print('selectivity=1/100 took: {}s on average'.format(sum(selectivity_100_results) / len(selectivity_100_results)))
print('selectivity=1/50 took: {}s on average'.format(sum(selectivity_50_results) / len(selectivity_50_results)))
print('selectivity=1/25 took: {}s on average'.format(sum(selectivity_25_results) / len(selectivity_25_results)))
print('selectivity=1/12.5 took: {}s on average'.format(sum(selectivity_12_5_results) / len(selectivity_12_5_results)))
if selectivity_1:
print('selectivity=1/1 took: {}s on average'.format(sum(selectivity_1_results) / len(selectivity_1_results)))
print('\n\n')
def experiment_2(in_clause_max_size, iters):
print("--------------------------- in_clause_max_size={}".format(in_clause_max_size))
global IN_CLAUSE_MAX_SIZE
IN_CLAUSE_MAX_SIZE = in_clause_max_size
(pp, msk) = ipe.setup(3+IN_CLAUSE_MAX_SIZE+1)
table_a_file = "data/0.01/customer.tbl"
a = "custkey"
pk_a = a
x = "selectivity"
table_b_file = "data/0.01/orders.tbl"
b = "custkey"
pk_b = "orderkey"
y = "selectivity"
print('starting to encrypt customer')
table_a = read_table(table_a_file, '|')
encrypted_table_a = ipe.encryptTable(msk, table_a, pk_a, a, x, IN_CLAUSE_MAX_SIZE)
print('done encrypting customer')
print('starting to encrypt orders')
table_b = read_table(table_b_file, '|')
encrypted_table_b = ipe.encryptTable(msk, table_b, pk_b, b, y, IN_CLAUSE_MAX_SIZE)
print('done encrypting orders')
print('')
selectivity_100_results = []
selectivity_50_results = []
selectivity_25_results = []
selectivity_12_5_results = []
selectivity_1_results = []
for i in range(iters):
# selectivity = 1/100
x_c = ["100"]
y_c = ["100"]
query_start = time.time()
(matches, decryptions) = hash_based_join(pp, msk, encrypted_table_a, encrypted_table_b, x_c, y_c)
query_end = time.time()
selectivity_100_results.append(query_end - query_start)
print('selectivity=1/100 took: {}s overall'.format(query_end - query_start))
print('time spent NOT doing decryption: {}s'.format((query_end - query_start) - sum(decryptions)))
print('time per decryption took: {}ms'.format((sum(decryptions) / len(decryptions)) * 1000))
print('num matches: {}'.format(len(matches)))
print('')
# selectivity = 1/50
x_c = ["50"]
y_c = ["50"]
query_start = time.time()
(matches, decryptions) = hash_based_join(pp, msk, encrypted_table_a, encrypted_table_b, x_c, y_c)
query_end = time.time()
selectivity_50_results.append(query_end - query_start)
print('selectivity=1/50 took: {}s overall'.format(query_end - query_start))
print('time spent NOT doing decryption: {}s'.format((query_end - query_start) - sum(decryptions)))
print('time per decryption took: {}ms'.format((sum(decryptions) / len(decryptions)) * 1000))
print('num matches: {}'.format(len(matches)))
print('')
# selectivity = 1/25
x_c = ["25"]
y_c = ["25"]
query_start = time.time()
(matches, decryptions) = hash_based_join(pp, msk, encrypted_table_a, encrypted_table_b, x_c, y_c)
query_end = time.time()
selectivity_25_results.append(query_end - query_start)
print('selectivity=1/25 took: {}s overall'.format(query_end - query_start))
print('time spent NOT doing decryption: {}s'.format((query_end - query_start) - sum(decryptions)))
print('time per decryption took: {}ms'.format((sum(decryptions) / len(decryptions)) * 1000))
print('num matches: {}'.format(len(matches)))
print('')
# selectivity = 1/12.5
x_c = ["12.5"]
y_c = ["12.5"]
query_start = time.time()
(matches, decryptions) = hash_based_join(pp, msk, encrypted_table_a, encrypted_table_b, x_c, y_c)
query_end = time.time()
selectivity_12_5_results.append(query_end - query_start)
print('selectivity=1/12.5 took: {}s overall'.format(query_end - query_start))
print('time spent NOT doing decryption: {}s'.format((query_end - query_start) - sum(decryptions)))
print('time per decryption took: {}ms'.format((sum(decryptions) / len(decryptions)) * 1000))
print('num matches: {}'.format(len(matches)))
print('')
# selectivity = 1/1
if selectivity_1:
x_c = []
y_c = []
query_start = time.time()
(matches, decryptions) = hash_based_join(pp, msk, encrypted_table_a, encrypted_table_b, x_c, y_c)
query_end = time.time()
selectivity_1_results.append(query_end - query_start)
print('selectivity=1/1 took: {}s overall'.format(query_end - query_start))
print('time spent NOT doing decryption: {}s'.format((query_end - query_start) - sum(decryptions)))
print('time per decryption took: {}ms'.format((sum(decryptions) / len(decryptions)) * 1000))
print('num matches: {}'.format(len(matches)))
print('')
# aggregations
print('--------------- AVERAGES OVER ITERATIONS')
print('selectivity=1/100 took: {}s on average'.format(sum(selectivity_100_results) / len(selectivity_100_results)))
print('selectivity=1/50 took: {}s on average'.format(sum(selectivity_50_results) / len(selectivity_50_results)))
print('selectivity=1/25 took: {}s on average'.format(sum(selectivity_25_results) / len(selectivity_25_results)))
print('selectivity=1/12.5 took: {}s on average'.format(sum(selectivity_12_5_results) / len(selectivity_12_5_results)))
if selectivity_1:
print('selectivity=1/1 took: {}s on average'.format(sum(selectivity_1_results) / len(selectivity_1_results)))
print('\n\n')
# MAIN
# experiments to test relationship between overall time and scale factor
"""
(pp, msk) = ipe.setup(3+IN_CLAUSE_MAX_SIZE+1)
experiment_1(pp, msk, "0.01", 20)
experiment_1(pp, msk, "0.02", 20)
experiment_1(pp, msk, "0.03", 20)
experiment_1(pp, msk, "0.04", 20)
experiment_1(pp, msk, "0.05", 20)
experiment_1(pp, msk, "0.06", 20)
experiment_1(pp, msk, "0.07", 20)
experiment_1(pp, msk, "0.08", 20)
experiment_1(pp, msk, "0.09", 20)
experiment_1(pp, msk, "0.1", 20)
"""
# experiments to test relationship between overall time and IN_CLAUSE_MAX_SIZE
"""
experiment_2(1, 20)
experiment_2(2, 20)
experiment_2(3, 20)
experiment_2(5, 20)
experiment_2(5, 20)
experiment_2(6, 20)
experiment_2(7, 20)
experiment_2(8, 20)
experiment_2(9, 20)
experiment_2(10, 20)
"""