_rearrange() is an identity if not needed

Operations are possible even when the order of the attributes is
not the same. Because ordering is just an implementation detail and
not an actual part of relational algebra.

A _rearrange() function exists to convert relations to have a similar
format and be able to operate on them.

The function would perform a projection in any case, which would cause
the creation of a new temporary relation, even when the relations where
already using the same order of attributes.

This commit fixes the issue by making it return an identity if the
operation is not necessary.
master
Salvo 'LtWorf' Tomaselli 2015-06-25 16:46:10 +07:00
parent 95e375f44a
commit f6b4c6708b
1 changed files with 9 additions and 7 deletions

@ -89,15 +89,17 @@ class relation (object):
writer.writerows(self.content) writer.writerows(self.content)
fp.close() # Closing file fp.close() # Closing file
def _rearrange_(self, other): def _rearrange(self, other):
'''If two relations share the same attributes in a different order, this method '''If two relations share the same attributes in a different order, this method
will use projection to make them have the same attributes' order. will use projection to make them have the same attributes' order.
It is not exactely related to relational algebra. Just a method used It is not exactely related to relational algebra. Just a method used
internally. internally.
Will return None if they don't share the same attributes''' Will raise an exception if they don't share the same attributes'''
if (self.__class__ != other.__class__): if (self.__class__ != other.__class__):
raise Exception('Expected an instance of the same class') raise Exception('Expected an instance of the same class')
if self.header.sharedAttributes(other.header) == len(self.header): elif self.header == other.header:
return other
elif self.header.sharedAttributes(other.header) == len(self.header):
return other.projection(self.header) return other.projection(self.header)
raise Exception('Relations differ: [%s] [%s]' % ( raise Exception('Relations differ: [%s] [%s]' % (
','.join(self.header) , ','.join(other.header) ','.join(self.header) , ','.join(other.header)
@ -182,7 +184,7 @@ class relation (object):
Will return an empty one if there are no common items. Will return an empty one if there are no common items.
Will return None if headers are different. Will return None if headers are different.
It is possible to use projection and rename to make headers match.''' It is possible to use projection and rename to make headers match.'''
other = self._rearrange_(other) # Rearranges attributes' order other = self._rearrange(other) # Rearranges attributes' order
newt = relation() newt = relation()
newt.header = header(self.header) newt.header = header(self.header)
@ -195,7 +197,7 @@ class relation (object):
Will return an empty one if the second is a superset of first. Will return an empty one if the second is a superset of first.
Will return None if headers are different. Will return None if headers are different.
It is possible to use projection and rename to make headers match.''' It is possible to use projection and rename to make headers match.'''
other = self._rearrange_(other) # Rearranges attributes' order other = self._rearrange(other) # Rearranges attributes' order
newt = relation() newt = relation()
newt.header = header(self.header) newt.header = header(self.header)
@ -237,7 +239,7 @@ class relation (object):
Will not insert tuplicated items. Will not insert tuplicated items.
Will return None if headers are different. Will return None if headers are different.
It is possible to use projection and rename to make headers match.''' It is possible to use projection and rename to make headers match.'''
other = self._rearrange_(other) # Rearranges attributes' order other = self._rearrange(other) # Rearranges attributes' order
newt = relation() newt = relation()
newt.header = header(self.header) newt.header = header(self.header)
@ -350,7 +352,7 @@ class relation (object):
return False return False
# Rearranges attributes' order so can compare tuples directly # Rearranges attributes' order so can compare tuples directly
other = self._rearrange_(other) other = self._rearrange(other)
# comparing content # comparing content
return self.content == other.content return self.content == other.content