More type safety

This commit is contained in:
Salvo 'LtWorf' Tomaselli 2020-06-14 22:54:58 +02:00
parent 16459868cf
commit e35fc0f4bd
No known key found for this signature in database
GPG Key ID: B3A7CF0C801886CF

View File

@ -195,7 +195,7 @@ def futile_renames(n: parser.Node) -> Tuple[parser.Node, int]:
or removes the operation entirely if they all get removed or removes the operation entirely if they all get removed
''' '''
if n.name == RENAME: if isinstance(n, Unary) and n.name == RENAME:
renames = n.get_rename_prop() renames = n.get_rename_prop()
changes = False changes = False
for k, v in renames.items(): for k, v in renames.items():
@ -217,7 +217,10 @@ def subsequent_renames(n: parser.Node) -> Tuple[parser.Node, int]:
into into
ρ ... (A) ρ ... (A)
''' '''
if n.name == RENAME and n.child.name == RENAME: if isinstance(n, Unary) and \
n.name == RENAME and \
isinstance(n.child, Unary) and \
n.child.name == RENAME:
# Located two nested renames. # Located two nested renames.
prop = n.prop + ',' + n.child.prop prop = n.prop + ',' + n.child.prop
child = n.child.child child = n.child.child
@ -296,7 +299,10 @@ def swap_rename_projection(n: parser.Node) -> Tuple[parser.Node, int]:
Will also eliminate fields in the rename that are cut in the projection. Will also eliminate fields in the rename that are cut in the projection.
''' '''
if isinstance(n, Unary) and n.name == PROJECTION and n.child.name == RENAME: if isinstance(n, Unary) and \
n.name == PROJECTION and \
isinstance(n.child, Unary) and \
n.child.name == RENAME:
# π index,name(ρ id➡index(R)) # π index,name(ρ id➡index(R))
renames = n.child.get_rename_prop() renames = n.child.get_rename_prop()
projections = set(n.get_projection_prop()) projections = set(n.get_projection_prop())
@ -329,7 +335,10 @@ def swap_rename_select(n: parser.Node) -> int:
Renaming the attributes used in the Renaming the attributes used in the
selection, so the operation is still valid.''' selection, so the operation is still valid.'''
if isinstance(n, Unary) and n.name == SELECTION and n.child.name == RENAME: if isinstance(n, Unary) and \
n.name == SELECTION and \
isinstance(n.child, Unary) and \
n.child.name == RENAME:
# This is an inverse mapping for the rename # This is an inverse mapping for the rename
renames = {v: k for k, v in n.child.get_rename_prop().items()} renames = {v: k for k, v in n.child.get_rename_prop().items()}
@ -355,8 +364,11 @@ def select_union_intersect_subtract(n: parser.Node) -> int:
and replaces them with and replaces them with
σ (i OR q) (a) σ (i OR q) (a)
Removing a O() operation like the union''' Removing a O() operation like the union'''
if n.name in {UNION, INTERSECTION, DIFFERENCE} and \ if isinstance(n, Binary) and \
n.name in {UNION, INTERSECTION, DIFFERENCE} and \
isinstance(n.left, Unary) and \
n.left.name == SELECTION and \ n.left.name == SELECTION and \
isinstance(n.right, Unary) and \
n.right.name == SELECTION and \ n.right.name == SELECTION and \
n.left.child == n.right.child: n.left.child == n.right.child:
@ -388,7 +400,12 @@ def union_and_product(n: parser.Node) -> Tuple[parser.Node, int]:
A * B A * C = A * (B C) A * B A * C = A * (B C)
Same thing with inner join Same thing with inner join
''' '''
if n.name == UNION and n.left.name in {PRODUCT, JOIN} and n.left.name == n.right.name: if isinstance(n, Binary) and \
n.name == UNION and \
isinstance(n.left, Binary) and \
n.left.name in {PRODUCT, JOIN} and \
isinstance(n.right, Binary) and \
n.left.name == n.right.name:
if n.left.left == n.right.left or n.left.left == n.right.right: if n.left.left == n.right.left or n.left.left == n.right.right:
l = n.left.right l = n.left.right
@ -429,7 +446,9 @@ def selection_and_product(n: parser.Node, rels: Dict[str, Relation]) -> parser.N
σ l (σ j (R) * σ i (Q)). Where j contains only attributes belonging to R, σ l (σ j (R) * σ i (Q)). Where j contains only attributes belonging to R,
i contains attributes belonging to Q and l contains attributes belonging to both''' i contains attributes belonging to Q and l contains attributes belonging to both'''
if isinstance(n, Unary) and n.name == SELECTION and n.child.name in (PRODUCT, JOIN): if isinstance(n, Unary) and n.name == SELECTION and \
isinstance(n.child, Binary) and \
n.child.name in (PRODUCT, JOIN):
l_attr = n.child.left.result_format(rels) l_attr = n.child.left.result_format(rels)
r_attr = n.child.right.result_format(rels) r_attr = n.child.right.result_format(rels)