-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_engine.py
More file actions
439 lines (383 loc) · 13 KB
/
sql_engine.py
File metadata and controls
439 lines (383 loc) · 13 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
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
import sys
METADATA_FILE = 'metadata.txt'
db = dict()
reqd_table = []
reqd_cols = []
selected_cols = []
reqd_rows = set()
col_attr = dict()
col_vals = dict()
colToTable = dict()
hasDistinct = False
aggr = ''
aggrs = {"sum", "average", "max", "min", "count", "avg"}
def read_metadata():
newTable = False
curTable = ""
with open(METADATA_FILE,"r") as f:
for line in f:
line = line.strip()
if(len(line) == 0):
continue
if(line == "<begin_table>"):
newTable = True
elif(newTable == True):
db[line] = dict()
curTable = line
newTable = False
elif(line != "<end_table>"):
db[curTable][line] = list()
colToTable[line] = curTable
def read_csv():
for table in db:
with open(table+".csv", "r") as f:
for line in f:
vals = line.split(",")
i = 0
for col in db[table]:
db[table][col].append(int(vals[i].replace('\"', '').strip()))
i += 1
def process_cols(cols):
colnames = []
for col in cols:
if(col.lower() == "distinct"):
global hasDistinct
hasDistinct = True
continue
parts = col.replace('(', ' ').replace(')', ' ').split()
col_attr[parts[-1]] = "none"
if(parts[0].lower() in aggrs):
col_attr[parts[-1]] = parts[0]
global aggr
aggr = parts[0].lower()
colnames.append(parts[-1])
return colnames
def setTable(tableList):
setup_table = []
for i in range(len(tableList)):
table = tableList[i]
if table in db:
reqd_cols.extend(list(db[table].keys()))
rowCount = len(db[table][next(iter(db[table]))])
temp_table = []
for j in range(rowCount):
rowvals = []
for col in db[table]:
rowvals.append(db[table][col][j])
temp_table.append(rowvals)
setup_table.append(temp_table)
else:
print("Error: Table not found")
sys.exit(1)
itr = 0
import itertools
for element in itertools.product(*setup_table):
reqd_table.append(sum(list((element)), []))
reqd_rows.add(itr)
itr += 1
def applyWhere(where):
isAnd = False
if 'and' in where.lower():
isAnd = True
parts = where.replace('and','or').replace('AND', 'or').replace('OR', 'or').split('or')
condOutput = []
for cond in parts:
cond = cond.strip()
col = ""
val = 0
op = ""
for i in range(len(cond)):
if(cond[i] == '='):
col = cond[:i]
val = int(cond[i+1:])
op = cond[i]
break
if(cond[i] == '<'):
if(cond[i+1] == '='):
col = cond[:i]
val = int(cond[i+2:])
op = "<="
break
col = cond[:i]
val = int(cond[i+1:])
op = "<"
break
if(cond[i] == '>'):
if(cond[i+1] == '='):
col = cond[:i]
val = int(cond[i+2:])
op = ">="
break
col = cond[:i]
val = int(cond[i+1:])
op = ">"
break
colIndex = 0
try:
colIndex = reqd_cols.index(col.strip())
except:
print("Error: Column not found in WHERE Clause")
sys.exit()
rowsToDel = []
for row in range(len(reqd_table)):
for col in range(len(reqd_table[row])):
if(col == colIndex):
delRow = False
if((op == "=") and (reqd_table[row][col] != val)):
delRow = True
if((op == ">") and (reqd_table[row][col] <= val)):
delRow = True
if((op == "<") and (reqd_table[row][col] >= val)):
delRow = True
if((op == ">=") and (reqd_table[row][col] < val)):
delRow = True
if((op == "<=") and (reqd_table[row][col] > val)):
delRow = True
if(delRow):
rowsToDel.append(row)
condOutput.append(rowsToDel)
if(isAnd):
for i in condOutput:
for j in i:
if j in reqd_rows:
reqd_rows.remove(j)
else:
toDel = set(condOutput[0])
for s in condOutput[1:]:
toDel.intersection_update(s)
for i in toDel:
if i in reqd_rows:
reqd_rows.remove(i)
def applyGroup(col_list,group_col,orderBy):
aggr_cols = dict()
group_col = group_col.split(',')[0].strip()
order = []
if(group_col not in reqd_cols):
print("Group by column not found")
exit()
header = colToTable[group_col].lower() + '.' + group_col.lower() + ','
for i in col_list:
cur = i.replace('(',' ').replace(')', '').split()
if cur[0].lower() in aggrs:
aggr_cols[cur[1]] = cur[0]
order.append(cur[1])
else:
order.append(cur[0])
groupDict = dict()
resultDict = dict()
for i in range(len(reqd_table)):
if i in reqd_rows:
groupDict[reqd_table[i][reqd_cols.index(group_col)]] = []
for i in range(len(reqd_table)):
if i in reqd_rows:
groupDict[reqd_table[i][reqd_cols.index(group_col)]].append(reqd_table[i])
for i in groupDict:
tmpList = [[] for i in range(len(reqd_cols))]
for j in range(len(groupDict[i])):
for k in range(len(groupDict[i][j])):
tmpList[k].append(groupDict[i][j][k])
resultDict[i] = tmpList
finalDict = dict()
headerRem = []
for i in resultDict:
finalDict[i] = []
for j in range(len(resultDict[i])):
if reqd_cols[j] in aggr_cols:
if(aggr_cols[reqd_cols[j]].lower() == "sum"):
finalDict[i].append(sum(resultDict[i][j]))
if(aggr_cols[reqd_cols[j]].lower() == "max"):
finalDict[i].append(max(resultDict[i][j]))
if(aggr_cols[reqd_cols[j]].lower() == "min"):
finalDict[i].append(min(resultDict[i][j]))
if(aggr_cols[reqd_cols[j]].lower() == "count"):
finalDict[i].append(len(resultDict[i][j]))
if(aggr_cols[reqd_cols[j]].lower() == "average" or aggr_cols[reqd_cols[j]].lower() == "avg"):
finalDict[i].append(int(sum(resultDict[i][j])/len(resultDict[i][j])))
tmp = colToTable[reqd_cols[j]].lower() + '.' + reqd_cols[j].lower()
if tmp not in headerRem:
headerRem.append(tmp)
header += tmp + ','
header = header[:-1]
print(header)
if(len(orderBy) == 0):
for i in finalDict:
row_vals = []
row_vals.append(str(i))
for j in finalDict[i]:
row_vals.append(str(j))
print(','.join(row_vals))
else:
orderBy = orderBy.split()[-1].lower()
if(orderBy == "desc"):
for i in sorted(finalDict,reverse=True):
row_vals = []
row_vals.append(str(i))
for j in finalDict[i]:
row_vals.append(str(j))
print(','.join(row_vals))
else:
for i in sorted(finalDict):
row_vals = []
row_vals.append(str(i))
for j in finalDict[i]:
row_vals.append(str(j))
print(','.join(row_vals))
sys.exit()
def handleAggr(selectedColumn):
vals = []
for i in range(len(reqd_table)):
if i in reqd_rows:
vals.append(reqd_table[i][selectedColumn])
global selected_cols
selected_cols = reqd_cols[selectedColumn]
printHeader()
if(aggr == "average" or aggr == "avg"):
print(sum(vals)/len(vals))
if(aggr == "min"):
print(min(vals))
if(aggr == "max"):
print(max(vals))
if(aggr == "sum"):
print(sum(vals))
if(aggr == "count"):
print(len(vals))
sys.exit()
def getQueryResult(cols, tables):
colnames = process_cols(cols)
selectedColIndex = []
global reqd_cols
for i in range(len(reqd_cols)):
if((reqd_cols[i] in colnames) or (colnames[0] == '*')):
selectedColIndex.append(i)
if(len(selectedColIndex) < len(colnames)):
print("Error: Selected column(s) not found")
sys.exit(1)
if(len(aggr) == 0):
res_table = []
for i in range(len(reqd_table)):
if i in reqd_rows:
row_vals = []
for j in range(len(reqd_table[i])):
if j in selectedColIndex:
if reqd_cols[j] not in selected_cols:
selected_cols.append(reqd_cols[j])
row_vals.append(str(reqd_table[i][j]))
res_table.append(row_vals)
reqd_cols = selected_cols
if(hasDistinct):
checked = []
for e in res_table:
if e not in checked:
checked.append(e)
res_table = checked
return res_table
else:
handleAggr(selectedColIndex[0])
def printHeader():
header = ""
for i in selected_cols:
header += colToTable[i].lower() + '.' + i.lower() + ','
header = header[:-1]
print(header)
def applyOrder(table, order):
parts = order.split()
order_col = parts[0].strip()
order_type = parts[1].lower().strip() if (len(parts) > 1) else "asc"
selectedColIndex = -1
for i in range(len(reqd_cols)):
if (reqd_cols[i] == order_col):
selectedColIndex = i
if(selectedColIndex == -1):
print("Error: Order by column not found")
sys.exit()
for i in range(len(table)):
table[i] = list(map(int, table[i]))
if(order_type == "asc"):
return sorted(table, key = lambda x: x[selectedColIndex])
else:
return sorted(table, key = lambda x: x[selectedColIndex], reverse = True)
def parse_query(query):
if(query[-1] == ';'):
query = query[0:-1]
else:
print("Error: Semicolon missing in SQL query")
return
tokens = query.split()
if(tokens[0].lower() != "select"):
print("Only SELECT statements are supported")
return
if(len(tokens) < 4):
print("Invalid SQL syntax")
cols = ""
tables = ""
where = ""
group = ""
order = ""
colsRead = False
tablesRead = False
readWhere = False
readGroup = False
readOrder = False
for token in tokens:
if(token.lower() == "select"):
continue
if(token.lower() == "from"):
colsRead = True
continue
if(colsRead == False):
cols += token + " "
continue
if(token.lower() == "where" or token.lower() == "group" or token.lower() == "order"):
tablesRead = True
if(token.lower() == "where"):
readWhere = True
elif(token.lower() == "group"):
readGroup = True
readWhere = False
else:
readOrder = True
readWhere = False
readGroup = False
continue
if(tablesRead == False):
tables += token + " "
continue
if(readWhere == True):
where += token + " "
continue
if(readGroup == True):
if(token.lower() != "by"):
group += token + " "
continue
if(readOrder == True):
if(token.lower() != "by"):
order += token + " "
col_list = cols.replace(',', ' ').split()
table_list = tables.replace(',', ' ').split()
if((readWhere == True and len(where) == 0) or (readGroup == True and len(group) == 0)
or (readOrder == True and len(order) == 0) or len(table_list) == 0 or len(col_list) == 0):
print("Error: Invalid SQL Syntax")
sys.exit(1)
setTable(table_list)
if(len(where)):
applyWhere(where)
if(len(group)):
applyGroup(col_list,group,order)
res_table = getQueryResult(col_list, table_list)
if(len(order)):
res_table = applyOrder(res_table, order)
for i in range(len(res_table)):
res_table[i] = list(map(str, res_table[i]))
printHeader()
for row in res_table:
row_vals = []
for col in row:
row_vals.append(col)
print(','.join(row_vals))
def main():
read_metadata()
read_csv()
if(len(sys.argv)<2):
print("Provide query in the argument")
parse_query(sys.argv[1])
main()