Skip to content
Open
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
136 changes: 136 additions & 0 deletions equivalent_ra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
def ra_eq1(query):
'''
σθ1∧θ2(E) = σθ1(σθ2(E))
'''
#Έλεγχος για το αν υπάρχει condition στο where και αν υπαρχει και and
if query['where'] is not None and query['where'].find('and') != -1:
#Split το condition σε 2 μέρη και ανακατασκευή.
condition1, condition2 = query['where'].split('and')
equiv_query = query.copy()
equiv_query['where'] = condition1
equiv_query['from'] = {'select': '*', 'from': equiv_query['from'], 'where': condition2,'distinct': None, 'orderby': None,'limit': None,'desc': None}
return equiv_query
return None

def ra_eq2(query):
'''
E1 inner join θ E2 ---> E2 inner join θ
'''
if 'from' in query and 'join' in query['from']:
left = query['from']['left']
right = query['from']['right']
equiv_query = query.copy()
equiv_query['from']['left'] = right
equiv_query['from']['right'] = left
return equiv_query
else:
return query

def ra_eq3(query):
'''
select θ (Ε1 Inner Join θ1 Ε2) ----> (select θ Ε1) Inner Join θ1 Ε2
'''
if 'from' in query and 'join' in query['from'] and query['from']['join'] == 'inner':
left_table = query['from']['left']
right_table = query['from']['right']
on_condition = query['from']['on']
where_condition = query['where']

#Ελεγχος για το table στο οποίο ανήκει το condition (π.χ classroom.capacity το condition ανήκει στο table classroom)
if where_condition and where_condition.startswith(f"{left_table}."):
nested_left = {
'select': query['select'],
'from': left_table,
'where': where_condition
}
nested_right = right_table
elif where_condition and where_condition.startswith(f"{right_table}."):
nested_left = left_table
nested_right = {
'select': query['select'],
'from': right_table,
'where': where_condition
}
else:
nested_left = left_table
nested_right = right_table

transformed_query = {
'select': query['select'],
'from': {
'join': 'inner',
'left': nested_left,
'right': nested_right,
'on': on_condition
},
'where': None, # Αφου το where condition έχει ήδη γίνει apply πιο πάνω.
'distinct': query['distinct'],
'order by': query['order by'],
'limit': query['limit']
}
print("OEOOOO")
print(transformed_query)
return transformed_query
else:
return query

def ra_eq4(query):
'''
Given a query, apply the transformation rule
select θ1 AND θ2 (Ε1 inner join Ε2) -> (select θ1 Ε1) INNER JOIN (select θ2 Ε2)
'''
if 'from' in query and 'join' in query['from'] and query['from']['join'] == 'inner':
left_table = query['from']['left']
right_table = query['from']['right']
on_condition = query['from']['on']
where_condition = query['where']

theta1, theta2 = where_condition.split('and')

transformed_query_left = {
'select': query['select'],
'from': left_table,
'where': "",
'distinct': query['distinct'],
'order by': query['order by'],
'limit': query['limit']
}

transformed_query_right = {
'select': query['select'],
'from': right_table,
'where': "",
'distinct': query['distinct'],
'order by': query['order by'],
'limit': query['limit']
}

#Λοοπ μέσα από όλα τα conditions και κάνει assign το κάθε where condition στο αντίστοιχο table
for condition in (theta1, theta2):
condition = condition.strip()
if left_table in condition:
transformed_query_left['where'] += f"{condition}"
elif right_table in condition:
transformed_query_right['where'] += f"{condition}"

transformed_query_left['where'] = transformed_query_left['where'].rstrip('and')
transformed_query_right['where'] = transformed_query_right['where'].rstrip('and')


final_transformed_query = {
'select': '*',
'from': {
'join': 'inner',
'left': transformed_query_left,
'right': transformed_query_right,
'on': on_condition
},
'where': None,
'distinct': query['distinct'],
'order by': query['order by'],
'limit': query['limit']
}

return final_transformed_query

return query
60 changes: 53 additions & 7 deletions mdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
import re
from pprint import pprint
import sys
import readline
#import pyreadline
import traceback
import shutil
sys.path.append('miniDB')

from equivalent_ra import ra_eq1
from equivalent_ra import ra_eq2
from equivalent_ra import ra_eq3
from equivalent_ra import ra_eq4

from database import Database
from table import Table
# art font is "big"
Expand Down Expand Up @@ -89,23 +94,44 @@ def create_query_plan(query, keywords, action):
else:
dic['desc'] = False
dic['order by'] = dic['order by'].removesuffix(' asc').removesuffix(' desc')

else:
dic['desc'] = None

if action=='create table':
args = dic['create table'][dic['create table'].index('('):dic['create table'].index(')')+1]
dic['create table'] = dic['create table'].removesuffix(args).strip()

arg_nopk = args.replace('primary key', '')[1:-1]

arglist = [val.strip().split(' ') for val in arg_nopk.split(',')]

dic['column_names'] = ','.join([val[0] for val in arglist])

dic['column_types'] = ','.join([val[1] for val in arglist])
if 'primary key' in args:
arglist = args[1:-1].split(' ')
dic['primary key'] = arglist[arglist.index('primary')-2]
arglist_has_pkey = args[1:-1].split(' ')

dic['primary key'] = arglist_has_pkey[arglist_has_pkey.index('primary')-2]
else:
dic['primary key'] = None



unique_columns=[]



for col in arglist:
if 'unique' in col:

unique_columns.append(col[0])

if len(unique_columns)!=0:
dic['unique'] = ','.join(unique_columns)
else:
dic['unique']=None



if action=='import':
dic = {'import table' if key=='import' else key: val for key, val in dic.items()}

Expand All @@ -120,6 +146,23 @@ def create_query_plan(query, keywords, action):
dic['force'] = True
else:
dic['force'] = False

if action=='create index':

args = dic['on'].split(' ')
dic['on'] = dic['on'].split(' ')[0]
if len(args) > 1:

dic['column'] = args[2]
else:
dic['column'] = 'pkey'
args = dic['using'].split(' ')
dic['using'] = dic['using'].split(' ')[0]
if len(args) > 1:
dic['index_type'] = args[1]




return dic

Expand Down Expand Up @@ -157,7 +200,8 @@ def evaluate_from_clause(dic):
join_dic['right'] = interpret(join_dic['right'][1:-1].strip())

dic['from'] = join_dic

print(ra_eq4(dic))
print(dic)
return dic

def interpret(query):
Expand Down Expand Up @@ -200,6 +244,7 @@ def execute_dic(dic):
dic[key] = execute_dic(dic[key])

action = list(dic.keys())[0].replace(' ','_')

return getattr(db, action)(*dic.values())

def interpret_meta(command):
Expand Down Expand Up @@ -251,7 +296,7 @@ def remove_db(db_name):
dbname = os.getenv('DB')

db = Database(dbname, load=True)



if fname is not None:
Expand All @@ -262,6 +307,7 @@ def remove_db(db_name):
pprint(dic, sort_dicts=False)
else :
dic = interpret(line.lower())

result = execute_dic(dic)
if isinstance(result,Table):
result.show()
Expand Down
2 changes: 2 additions & 0 deletions miniDB/btree.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def insert(self, value, ptr, rptr=None):
# insert to it
self.nodes[index].insert(value,ptr)
# if the node has more elements than b-1, split the node

if len(self.nodes[index].values)==self.b:
self.split(index)

Expand Down Expand Up @@ -309,6 +310,7 @@ def find(self, operator, value):
if operator == '>':
for idx, node_value in enumerate(target_node.values):
ops+=1

if node_value > value:
results.append(target_node.ptrs[idx])
while target_node.right_sibling is not None:
Expand Down
Loading