union_and_product_

This commit is contained in:
Salvo 'LtWorf' Tomaselli 2020-06-09 19:15:34 +02:00
parent 34ed9405ea
commit 7806a0a27e
No known key found for this signature in database
GPG Key ID: B3A7CF0C801886CF

View File

@ -433,40 +433,24 @@ def select_union_intersect_subtract(n: parser.Node) -> int:
return changes + recoursive_scan(select_union_intersect_subtract, n) return changes + recoursive_scan(select_union_intersect_subtract, n)
def union_and_product(n: parser.Node) -> int: 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
''' '''
changes = 0
if n.name == UNION and n.left.name in {PRODUCT, JOIN} and n.left.name == n.right.name: if n.name == UNION and n.left.name in {PRODUCT, JOIN} and n.left.name == n.right.name:
newnode = parser.Node()
newnode.kind = parser.BINARY
newnode.name = n.left.name
newchild = parser.Node()
newchild.kind = parser.BINARY
newchild.name = UNION
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:
newnode.left = n.left.left l = n.left.right
newnode.right = newchild r = n.right.left if n.left.left == n.right.right else n.right.right
newchild = parser.Binary(UNION, l, r)
newchild.left = n.left.right return parser.Binary(n.left.name, n.left.left, newchild), 1
newchild.right = n.right.left if n.left.left == n.right.right else n.right.right
replace_node(n, newnode)
changes = 1
elif n.left.right == n.right.left or n.left.left == n.right.right: elif n.left.right == n.right.left or n.left.left == n.right.right:
newnode.left = n.left.right l = n.left.left
newnode.right = newchild r = n.right.left if n.right.left == n.right.right else n.right.right
newchild = parser.Binary(UNION, l, r)
newchild.left = n.left.left return parser.Binary(n.left.name, n.left.right, newchild), 1
newchild.right = n.right.left if n.right.left == n.right.right else n.right.right return n, 0
replace_node(n, newnode)
changes = 1
return changes + recoursive_scan(union_and_product, n)
def projection_and_union(n, rels): def projection_and_union(n, rels):
@ -498,7 +482,7 @@ def projection_and_union(n, rels):
newnode.prop = n.right.prop newnode.prop = n.right.prop
replace_node(n, newnode) replace_node(n, newnode)
changes = 1 changes = 1
return changes + recoursive_scan(projection_and_union, n, rels) return n, 0
def selection_and_product(n, rels): def selection_and_product(n, rels):
@ -623,7 +607,7 @@ general_optimizations = [
swap_union_renames, swap_union_renames,
swap_rename_projection, swap_rename_projection,
#select_union_intersect_subtract, #select_union_intersect_subtract,
#union_and_product, union_and_product,
] ]
specific_optimizations = [ specific_optimizations = [
#selection_and_product, #selection_and_product,