private IQTree liftInnerJoin(NaryIQTree queryTree, ImmutableList<IQTree> newChildren, VariableGenerator variableGenerator) { InnerJoinNode joinNode = (InnerJoinNode) queryTree.getRootNode(); NaryIQTree newQueryTree = newChildren.equals(queryTree.getChildren()) ? queryTree : iqFactory.createNaryIQTree(joinNode, newChildren); return extractCandidateVariables(queryTree, joinNode.getOptionalFilterCondition(), newChildren) .map(newQueryTree::liftIncompatibleDefinitions) .filter(t -> !t.equals(queryTree)) .findFirst() .orElse(newQueryTree) .liftBinding(variableGenerator); }
@Override public Stream<IQ> splitUnion(IQ query) { DistinctVariableOnlyDataAtom projectionAtom = query.getProjectionAtom(); VariableGenerator variableGenerator = query.getVariableGenerator(); IQTree tree = query.getTree(); return findFirstSplittableUnion(query) .map(unionTree -> unionTree.getChildren().stream() .map(c -> tree.replaceSubTree(unionTree, c)) .map(t -> t.liftBinding(variableGenerator)) .map(t -> iqFactory.createIQ(projectionAtom, t)) .map(IQ::liftBinding)) .orElseGet(() -> Stream.of(query)); }
private IQTree liftUnionChild(int childIndex, NaryIQTree newUnionChild, ImmutableList<IQTree> initialChildren) { UnionNode newUnionNode = iqFactory.createUnionNode(initialChildren.stream() .flatMap(c -> c.getVariables().stream()) .collect(ImmutableCollectors.toSet())); return iqFactory.createNaryIQTree(newUnionNode, newUnionChild.getChildren().stream() .map(unionGrandChild -> createJoinSubtree(childIndex, unionGrandChild, initialChildren)) .collect(ImmutableCollectors.toList())); }
private IQTree liftNary(NaryIQTree queryTree, VariableGenerator variableGenerator) { NaryOperatorNode root = queryTree.getRootNode(); ImmutableList<IQTree> newChildren = queryTree.getChildren().stream() // Recursive .map(queryTree1 -> liftTree(queryTree1, variableGenerator)) .collect(ImmutableCollectors.toList()); if (root instanceof InnerJoinNode) { return liftInnerJoin(queryTree, newChildren, variableGenerator); } else return newChildren.equals(queryTree.getChildren()) ? queryTree : iqFactory.createNaryIQTree(root, newChildren); }