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
30 changes: 24 additions & 6 deletions mdb.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import os
import re
from pprint import pprint
import sys
import readline
import traceback
import shutil
sys.path.append('miniDB')

from database import Database
from table import Table
from miniDB.database import Database
from miniDB.table import Table
# art font is "big"
art = '''
_ _ _____ ____
Expand Down Expand Up @@ -124,6 +120,28 @@ def create_query_plan(query, keywords, action):
return dic


def equivalent_queries():
equivalence_rules = {'select * from R where t1 and t2': 'select * from (select * from R where t2) as w where t1',
# σ θ1^θ2(R) = σ θ1(σ θ2 (R))
'select * from (select * from R where t2) as w where t1': 'select * from (select * from R where t1) as w where t2', #σ θ1(σ θ2 (R)) =σ θ2(σ θ1 (R))
'select * from R where R.attribute ="value" natural join s': 'select * from R join s on R.key = S.key where R.attribute="value"', # σ θ (RxS) = R ⋈θ S
'': '', # σ θ1 (R ⋈θ2 S) = R ⋈θ1^θ2 S
'': '', # R ⋈θ S = S ⋈θ R
'': '', # (R ⋈ S) ⋈ T = R ⋈ (S ⋈ T)
'': '', # (R ⋈θ1 S) ⋈θ2^θ3 T = R ⋈θ1^θ3 (S ⋈θ2 T)
'': '', # σ θ1 (R ⋈θ S) = σ θ1 (R) ⋈θ S
'': '', # σ θ1^θ2 (R ⋈θ S) = σ θ1 (R) ⋈θ σ θ2 (S)
'': '', # R ⋃ S = S ⋃ R
'': '', # R ⋂ S = S ⋂ R
'': '', # (R ⋃ S) ⋃ T = R ⋃ S ⋃ T
'': '', # (R ⋂ S) ⋂ T = R ⋂ S ⋂ T
'': '', # σ θ (R-S) = σ θ (R) - σ θ (S)
'': '', # σ θ (R-S) = σ θ (R) - S
'': '', # σ θ (R ⋂ S) = σ θ (R) ⋂ σ θ (S)
'': '', # σ θ (R ⋂ S) = σ θ (R) ⋂ S
'': '', # σ θ (R ⋃ S) = σ θ (R) ⋃ σ θ (S)
'': ''} # σ θ (R ⋃ S) = σ θ (R) ⋃ S


def evaluate_from_clause(dic):
'''
Expand Down
1 change: 0 additions & 1 deletion miniDB/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os,sys
import logging
import warnings
import readline
from tabulate import tabulate

sys.path.append(f'{os.path.dirname(os.path.dirname(os.path.abspath(__file__)))}/miniDB')
Expand Down
77 changes: 74 additions & 3 deletions miniDB/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

sys.path.append(f'{os.path.dirname(os.path.dirname(os.path.abspath(__file__)))}/miniDB')

from misc import get_op, split_condition
from misc import get_op, split_condition,reverse_op


class Table:
Expand Down Expand Up @@ -147,7 +147,7 @@ def _update_rows(self, set_value, set_column, condition):
condition: string. A condition using the following format:
'column[<,<=,=,>=,>]value' or
'value[<,<=,=,>=,>]column'.

Operatores supported: (<,<=,=,>=,>)
'''
# parse the condition
Expand Down Expand Up @@ -233,9 +233,14 @@ def _select_where(self, return_columns, condition=None, distinct=False, order_by
# if condition is None, return all rows
# if not, return the rows with values where condition is met for value
if condition is not None:
# split the condition into parts
condition_parts = self._parse_condition(condition)
# get the rows that satisfy the condition
rows = self._get_rows_satisfying_condition(condition_parts)
"""
column_name, operator, value = self._parse_condition(condition)
column = self.column_by_name(column_name)
rows = [ind for ind, x in enumerate(column) if get_op(operator, x, value)]
rows = [ind for ind, x in enumerate(column) if get_op(operator, x, value)]"""
else:
rows = [i for i in range(len(self.data))]

Expand Down Expand Up @@ -269,6 +274,24 @@ def _select_where(self, return_columns, condition=None, distinct=False, order_by

return s_table

def _get_rows_satisfying_condition(self, condition):
# Return the list of rows that satisfy the given condition.
rows = set()
"""logical_operators = {'AND': lamda a,b:a and b,
'OR':,
'NOT':,
'BETWEEN':,
'and':,
'or':,
'not':,
'between':}

for op in logical_operators:
if op in condition:

"""

return list(rows)

def _select_where_with_btree(self, return_columns, bt, condition, distinct=False, order_by=None, desc=True, limit=None):

Expand Down Expand Up @@ -316,6 +339,54 @@ def _select_where_with_btree(self, return_columns, bt, condition, distinct=False

s_table.data = list(set(map(lambda x: tuple(x), s_table.data))) if distinct else s_table.data

if order_by:
s_table.order_by(order_by, desc)

if isinstance(limit,str):
s_table.data = [row for row in s_table.data if row is not None][:int(limit)]

return s_table
def _select_where_with_hash(self, return_columns, hsh, condition, distinct=False, order_by=None, desc=True, limit=None):

# if * return all columns, else find the column indexes for the columns specified
if return_columns == '*':
return_cols = [i for i in range(len(self.column_names))]
else:
return_cols = [self.column_names.index(colname) for colname in return_columns]


column_name, operator, value = self._parse_condition(condition)

# here we run the same select twice, sequentially and using the btree.
# we then check the results match and compare performance (number of operation)
column = self.column_by_name(column_name)

rows = []
operators = ('<', '>', '<=', '>=')
if operator in operators:
column = self.column_by_name(column_name)
rows = [ind for ind, x in enumerate(column) if get_op(operator, x, value)]
#else:
# find in hash dictionary

rows = list(rows)

try:
k = int(limit)
except TypeError:
k = None
# same as simple select from now on
rows = rows[:k]
# TODO: this needs to be dumbed down
dict = {(key):([[self.data[i][j] for j in return_cols] for i in rows] if key=="data" else value) for key,value in self.__dict__.items()}

dict['column_names'] = [self.column_names[i] for i in return_cols]
dict['column_types'] = [self.column_types[i] for i in return_cols]

s_table = Table(load=dict)

s_table.data = list(set(map(lambda x: tuple(x), s_table.data))) if distinct else s_table.data

if order_by:
s_table.order_by(order_by, desc)

Expand Down