Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/app.cpython-313.pyc
Binary file not shown.
52 changes: 43 additions & 9 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,29 @@ def __init__(self, name, attr, tuples):
self.attrIndex = {attr:i for i, attr in enumerate(attr)}

def __str__(self):
result = f"{self.name} = {{ {', '.join(self.attr)}\n"
result = f"{self.name} = {{{', '.join(self.attr)}}}\n"
for row in self.tuples:
result += f" {', '.join(str(val) for val in row)}\n"
result += "}"
value = [f'"{val}"' for val in row]
result += f" {', '.join(value)}\n"
# result += "}"
return result
# result = f"{self.name} = {{{', '.join(self.attr)}}}\n"
# for row in self.tuples:
# value = []
# for val in row:
# # Try to print as number if possible
# try:
# float_val = float(val)
# # If it's an integer, print as int
# if float_val.is_integer():
# value.append(str(int(float_val)))
# else:
# value.append(str(float_val))
# except ValueError:
# value.append(f'"{val}"')
# result += f" {', '.join(value)}\n"
# result += "}"
# return result

def get_attr_val(self, row, attr):
if(attr in self.attrIndex):
Expand Down Expand Up @@ -46,7 +64,7 @@ def selection(self, relName, condition):
if(self._evaluate_condition(row, relation, condition)):
result.append(row)

return Relation(f"select_{condition.replace(" ", "_")}({relName})", relation.get_attrs(), result)
return Relation(relName, relation.attr, result)

def projection(self, relName, attributes):
relation = self.get_relation(relName)
Expand All @@ -63,6 +81,12 @@ def projection(self, relName, attributes):
result.append(new)

return Relation(f"project_{','.join(attributes)}({relName})", attributes, result)

# base_name = relation.name
# if base_name.startswith("temp_") and hasattr(relation, "source_name"):
# base_name = relation.source_name
# return Relation(f"project_{','.join(attributes)}({base_name})", attributes, result)


def intersection(self, rel1Name, rel2Name):
rel1 = self.get_relation(rel1Name)
Expand All @@ -77,9 +101,9 @@ def intersection(self, rel1Name, rel2Name):
if(tuple(row) in rel2_set):
commons.append(row)

return Relation(f"{rel1Name}_intersection_{rel2Name}", rel1.get_attrs(), commons)
return Relation(f"{rel1Name}_intersection_{rel2Name}", rel1.attr, commons)

def join(self, rel1Name, rel2Name):
def join(self, rel1Name, rel2Name, condition = None):
rel1 = self.get_relation(rel1Name)
rel2 = self.get_relation(rel2Name)
commons = set(rel1.attr) & set(rel2.attr)
Expand Down Expand Up @@ -113,7 +137,7 @@ def union(self, rel1Name, rel2Name):
rel2 = self.get_relation(rel2Name)

if(rel1.attr != rel2.attr):
return ValueError("Relations must have the same attributes to do a union operations.")
raise ValueError("Relations must have the same attributes to do a union operations.")

allPairs = rel1.tuples + rel2.tuples
uniquePairs = []
Expand All @@ -124,7 +148,7 @@ def union(self, rel1Name, rel2Name):
seen.add(key)
uniquePairs.append(row)

return Relation(f"{rel1Name}_union_{rel2Name}", rel1.get_attrs(), uniquePairs)
return Relation(f"{rel1Name}_union_{rel2Name}", rel1.attr, uniquePairs)

def difference(self, rel1Name, rel2Name):
rel1 = self.get_relation(rel1Name)
Expand All @@ -139,7 +163,7 @@ def difference(self, rel1Name, rel2Name):
if(tuple(row) not in rel2_set):
diff.append(row)

return Relation(f"{rel1Name}_difference_{rel2Name}", rel1.get_attrs(), diff)
return Relation(f"{rel1Name}_difference_{rel2Name}", rel1.attr, diff)

def _evaluate_condition(self, row, relation: Relation, condition):
condition = condition.strip()
Expand Down Expand Up @@ -270,6 +294,11 @@ def _parse_and_run(cpu: Processor, query):
if(rORq.startswith("(") or any(op in rORq for op in ops)):
nestedResult = _parse_and_run(cpu, rORq)
temp = f"temp_{len(cpu.relations)}"
# temp = f"temp_{len(cpu.relations)}"
# rel = Relation(temp, nestedResult.attr, nestedResult.tuples)
# rel.source_name = nestedResult.name if hasattr(nestedResult, "name") else temp
# cpu.add_relation(rel)
# temp = f"{len(cpu.relations)}"
cpu.add_relation(Relation(temp, nestedResult.attr, nestedResult.tuples))
result = cpu.selection(temp, condition)
del cpu.relations[temp]
Expand All @@ -285,6 +314,11 @@ def _parse_and_run(cpu: Processor, query):
if(rORq.startswith("(") or any(op in rORq for op in ops)):
nestedResult = _parse_and_run(cpu, rORq)
temp = f"temp_{len(cpu.relations)}"
# temp = f"temp_{len(cpu.relations)}"
# rel = Relation(temp, nestedResult.attr, nestedResult.tuples)
# rel.source_name = nestedResult.name if hasattr(nestedResult, "name") else temp
# cpu.add_relation(rel)
# temp = f"{len(cpu.relations)}"
cpu.add_relation(Relation(temp, nestedResult.attr, nestedResult.tuples))
result = cpu.projection(temp, attrs)
del cpu.relations[temp]
Expand Down
31 changes: 27 additions & 4 deletions input.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
// Test the specification requirement: πName(σAge > 30(Employees))
Employees (EID, Name, Age) = {
E1, John, 32
E2, Alice, 28
E3, Bob, 29
}

// Table with employees of ages about 30
Query: select Age > 30 (Employees)



Employees (EID, Name, Age, Department) = {
E1, John, 32, IT
E2, Alice, 28, HR
E3, Bob, 29, IT
E4, Carol, 35, Finance
}

// This should be equivalent to the specification example:
// πName(σAge > 30(Employees)) = project Name (select Age > 30 (Employees))
Query: project Name (select Age > 30 (Employees))
// Table with the departments only
Query: project Department (Employees)



Students (SID, Name, Age, GPA) = {
101111111, Siddig, 100, 12.0
101222222, Ahmed, 50, 11.0
101333333, Khalid, 25, 10.0
}

// Displays student id and names of students aged 50
Query: project SID, Name (select Age = 50 (Students))

// Names of students with GPA > 10.5
Query: project Name (select GPA > 10.5 (Students))
21 changes: 20 additions & 1 deletion output.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
Error running query 'project Name (select Age > 30 (Employees))': 'function' object has no attribute 'attrIndex'.
Employees = {EID, Name, Age, Department}
"E1", "John", "32", "IT"
"E4", "Carol", "35", "Finance"


project_Department(Employees) = {Department}
"IT"
"HR"
"IT"
"Finance"


project_SID,Name(temp_2) = {SID, Name}
"101222222", "Ahmed"


project_Name(temp_2) = {Name}
"Siddig"
"Ahmed"


Loading