Commit 86875ad2 authored by Haoyu Z's avatar Haoyu Z
Browse files

add submissions

parent 8bc1c3e2
No preview for this file type
......@@ -13,8 +13,114 @@ import traceback
tablesWithProvenance = {}
twojoins = ["INNERJOIN", "LEFTJOIN", "RIGHTJOIN", "FULLJOIN", "NATURALJOIN"]
threejoins = ["LEFTOUTERJOIN", "RIGHTOUTERJOIN", "FULLOUTERJOIN"]
def tablesInQuery(query):
parsed = sp.parse(query)[0]
output = []
tokens = query.split(" ")
tokens = list(filter(lambda a: a != "", tokens))
state = "not in"
idx = 0
while idx < len(tokens):
if tokens[idx] == "":
idx += 1
continue
if state == "not in":
if tokens[idx].upper() == "FROM":
state = "from in"
if tokens[idx].upper() == "JOIN":
state = "join in"
elif state == "from in":
if tokens[idx].upper() in ["WHERE", "ON"]:
state = "not in"
idx += 1
continue
if tokens[idx].upper() == "JOIN":
state = "join in"
idx += 1
continue
if (tokens[idx]+tokens[idx+1]).upper() in twojoins:
state = "join in"
idx += 2
continue
if (tokens[idx]+tokens[idx+1]+tokens[idx+2]).upper() in threejoins:
state = "join in"
idx += 3
continue
if "SELECT" in tokens[idx].upper():
idx += 1
state = "not in"
continue
if "," in tokens[idx]:
output.append(tokens[idx].split(",")[0])
else:
if tokens[idx + 1].upper() == "AS":
output.append(tokens[idx])
idx += 2
elif "," in tokens[idx + 1]:
output.append(tokens[idx])
idx += 1
elif tokens[idx+1].upper() == "WHERE" :
state = "not in"
output.append(tokens[idx])
idx += 1
elif tokens[idx+2].upper() == "WHERE":
state = "not in"
output.append(tokens[idx])
idx += 2
elif (tokens[idx+2]+tokens[idx+3]).upper() in twojoins:
output.append(tokens[idx])
idx += 1
elif (tokens[idx+2]+tokens[idx+3]+tokens[idx+4]).upper() in threejoins:
output.append(tokens[idx])
idx += 1
else:
output.append(tokens[idx])
elif state == "join in":
if tokens[idx].upper() in ["WHERE", "ON"]:
state = "not in"
idx += 1
continue
if tokens[idx].upper() == "JOIN":
state = "join in"
idx += 1
continue
if (tokens[idx]+tokens[idx+1]).upper() in twojoins:
state = "join in"
idx += 2
continue
if (tokens[idx]+tokens[idx+1]+tokens[idx+2]).upper() in threejoins:
state = "join in"
idx += 3
continue
if "SELECT" in tokens[idx].upper():
idx += 1
state = "not in"
continue
if tokens[idx+2].upper() == "USING":
output.append(tokens[idx])
idx += 2
state = "not in"
continue
if tokens[idx + 1].upper() == "AS":
output.append(tokens[idx])
idx += 2
elif tokens[idx+1].upper() in ["WHERE", "ON", "JOIN"] or (tokens[idx+1]+tokens[idx+2]).upper() in twojoins or (tokens[idx+1]+tokens[idx+2]+tokens[idx+3]).upper() in threejoins:
output.append(tokens[idx])
else:
output.append(tokens[idx])
idx+=1
idx += 1
return list(set(output))
'''parsed = sp.parse(query)[0]
output = []
state = ""
for token in parsed.tokens:
......@@ -43,7 +149,8 @@ def tablesInQuery(query):
output.append(cur.split("as")[0].strip())
break
return list(set(output))
return list(set(output))'''
#Preprocess query
......
SELECT Students.FirstName, Students.LastName, Courses.Instructor FROM Students INNER JOIN Enrollments ON Students.NetId=Enrollments.NetId INNER JOIN Courses ON Enrollments.CRN=Courses.CRN WHERE (Students.FirstName LIKE 'A%' OR Students.LastName LIKE 'A%') AND (Courses.Instructor LIKE 'A%');
\ No newline at end of file
SELECT DISTINCT S.NetId, S.FirstName, S.LastName FROM Students S, Enrollments E, Courses C WHERE S.NetId = E.NetId AND E.CRN = C.CRN AND S.Department = 'CS' AND C.Department = 'ECE' AND E.Score <= All (SELECT E2.Score FROM Enrollments E2 WHERE E2.CRN = E.CRN) ORDER BY S.LastName, S.NetId DESC;
\ No newline at end of file
SELECT E.CRN,C.Title,C.Department, AVG(Score) AS avgscore FROM Enrollments E, Courses C WHERE E.CRN = C.CRN AND C.Department='ECE' GROUP BY E.CRN HAVING avgscore > 70 ORDER BY avgscore DESC;
\ No newline at end of file
SELECT p1.BrandName, COUNT(DISTINCT p2.CustomerId) as NumOfCustomers FROM Purchases p2 RIGHT JOIN Products p1 ON p2.ProductId = p1.ProductId GROUP BY p1.BrandName ORDER BY NumOfCustomers DESC, p1.BrandName ASC;
\ No newline at end of file
SELECT Students.FirstName, Students.LastName, ROUND(Enrollments.Score,2), Courses.Title, ROUND(E.avgscore,2) FROM Students NATURAL JOIN Enrollments NATURAL JOIN (SELECT CRN, AVG(Score) AS avgscore FROM Enrollments GROUP BY CRN) AS E JOIN Courses USING (CRN) WHERE Score > avgscore ORDER BY Students.FirstName, Courses.Title DESC;
\ No newline at end of file
(SELECT S.NetID, S.FirstName, S.LastName FROM Students S NATURAL JOIN Enrollments E JOIN Courses C USING (CRN) WHERE S.Department = 'CS' AND C.Department = 'CS' AND E.Score > 80) UNION (SELECT S1.NetID, S1.FirstName, S1.LastName FROM Students S1 NATURAL JOIN Enrollments E1 JOIN Courses C1 USING (CRN) WHERE S1.Department = 'ECE' AND C1.Department = 'CS' AND E1.Score > 70) order by NetId
\ No newline at end of file
SELECT FirstName, LastName, COUNT(PurchaseId) AS cnt
FROM Customers LEFT JOIN Purchases USING (CustomerId)
GROUP BY FirstName, LastName
ORDER BY FirstName, LastName DESC;
\ No newline at end of file
SELECT C.CustomerId, C.FirstName, C.LastName, C.PhoneNumber
FROM Customers C NATURAL JOIN Purchases P
WHERE C.CustomerId IN (SELECT C1.CustomerId
FROM Customers C1 NATURAL JOIN Purchases P1
WHERE P1.Price >= ALL(SELECT Price
FROM Purchases
WHERE ProductId = P1.ProductId))
GROUP BY P.CustomerId
ORDER BY P.CustomerId DESC
\ No newline at end of file
......@@ -56,7 +56,7 @@ def main():
os.makedirs("hw1-provenance")
except OSError as e:
print(e)
conn = psycopg2.connect("")
conn = psycopg2.connect("dbname=provdb user=joker password=m")
cur = conn.cursor()
extractData(conn, cur)
conn.close()
......@@ -68,7 +68,7 @@ def main():
submissions = question_submissions[student_id]
for sub_id in list(submissions.keys()):
try:
conn = psycopg2.connect("")
conn = psycopg2.connect("dbname=provdb user=joker password=m")
cur = conn.cursor()
commitAndExecute(conn, cur, 'SET search_path TO public, provsql;')
provenance.exe_query(submissions[sub_id], f, cur, conn)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment