default boolean isLeaf() { return getChildren().isEmpty(); }
private Stream<IQTree> splitRootUnion(IQTree tree) { return (tree.getRootNode() instanceof UnionNode) ? tree.getChildren().stream() : Stream.of(tree); }
/** * Recursive */ private static String printSubtree(IQTree subTree, String offset) { String childOffset = offset + TAB_STR; return offset + subTree.getRootNode() + "\n" + subTree.getChildren().stream() .map(c -> printSubtree(c, childOffset)) .reduce("", (c, a) -> c + a); }
@Override public IQTree liftIncompatibleDefinitions(Variable variable, ImmutableList<IQTree> children) { return IntStream.range(0, children.size()).boxed() .map(i -> Maps.immutableEntry(i, children.get(i))) .filter(e -> e.getValue().isConstructed(variable)) // index -> new child .map(e -> Maps.immutableEntry(e.getKey(), e.getValue().liftIncompatibleDefinitions(variable))) .filter(e -> { QueryNode newRootNode = e.getValue().getRootNode(); return (newRootNode instanceof UnionNode) && ((UnionNode) newRootNode).hasAChildWithLiftableDefinition(variable, e.getValue().getChildren()); }) .findFirst() .map(e -> liftUnionChild(e.getKey(), (NaryIQTree) e.getValue(), children)) .orElseGet(() -> iqFactory.createNaryIQTree(this, children)); }
private ImmutableList<IQTree> extractChildrenToVisit(IQTree tree) { QueryNode node = tree.getRootNode(); if (node instanceof BinaryNonCommutativeOperatorNode) { if (node instanceof LeftJoinNode) { return ImmutableList.of(((BinaryNonCommutativeIQTree)tree).getLeftChild()); } /* * Not supported BinaryNonCommutativeOperatorNode: we ignore them */ else { return ImmutableList.of(); } } else { return tree.getChildren(); } }
@Override public boolean isEquivalentTo(IQTree tree) { if (!getRootNode().isEquivalentTo(tree.getRootNode())) return false; ImmutableList<IQTree> otherChildren = tree.getChildren(); return (children.size() == otherChildren.size()) && IntStream.range(0, children.size()) .allMatch(i -> children.get(i).isEquivalentTo(otherChildren.get(i))); }
/** * Recursive */ private void insertIQChildren(QueryNode parentNode, ImmutableList<IQTree> childrenTrees) { ImmutableList<QueryNode> newChildren = childrenTrees.stream() .map(IQTree::getRootNode) .map(n -> contains(n) ? n.clone() : n) .collect(ImmutableCollectors.toList()); if (parentNode instanceof BinaryOrderedOperatorNode) { addChild(parentNode, newChildren.get(0), Optional.of(LEFT), false); addChild(parentNode, newChildren.get(1), Optional.of(RIGHT), false); } else { newChildren .forEach(c -> addChild(parentNode, c, Optional.empty(),false)); } IntStream.range(0, childrenTrees.size()) .forEach(i -> insertIQChildren(newChildren.get(i), childrenTrees.get(i).getChildren())); }
@Override // merge consecutive unions public IQTree transformUnion(IQTree tree, UnionNode rootNode, ImmutableList<IQTree> children) { ImmutableList<IQTree> transformedChildren = children.stream() .map(t -> t.acceptTransformer(this)) .collect(ImmutableCollectors.toList()); ImmutableList<IQTree> unionGrandChildren = transformedChildren.stream() .filter(t -> t.getRootNode() instanceof UnionNode) .flatMap(t -> t.getChildren().stream()) .collect(ImmutableCollectors.toList()); return iqFactory.createNaryIQTree( rootNode, Stream.concat( transformedChildren.stream() .filter(t -> !(t.getRootNode() instanceof UnionNode)), unionGrandChildren.stream() ).collect(ImmutableCollectors.toList()) ); } }
private void insertChildTree(QueryNode parent, QueryNode child, IQTree childTree, IntermediateQueryBuilder queryBuilder, int position) { if (parent instanceof BinaryOrderedOperatorNode) { queryBuilder.addChild(parent, child, (position == 0)? LEFT : RIGHT); } else { queryBuilder.addChild(parent, child); } insertChildren(child, childTree.getChildren(), queryBuilder); } }
@Override public IQTree liftIncompatibleDefinitions(Variable variable, IQTree child) { IQTree newChild = child.liftIncompatibleDefinitions(variable); QueryNode newChildRoot = newChild.getRootNode(); /* * Lift the union above the filter */ if ((newChildRoot instanceof UnionNode) && ((UnionNode) newChildRoot).hasAChildWithLiftableDefinition(variable, newChild.getChildren())) { UnionNode unionNode = (UnionNode) newChildRoot; ImmutableList<IQTree> grandChildren = newChild.getChildren(); ImmutableList<IQTree> newChildren = grandChildren.stream() .map(c -> (IQTree) iqFactory.createUnaryIQTree(this, c)) .collect(ImmutableCollectors.toList()); return iqFactory.createNaryIQTree(unionNode, newChildren); } return iqFactory.createUnaryIQTree(this, newChild); }
Optional<ImmutableExpression> filter = node.getOptionalFilterCondition(); List<Function> atoms = new ArrayList<>(); List<IQTree> listnode = tree.getChildren(); for (IQTree child: listnode) { List<Function> atomsList = getAtomFrom(child, heads, subQueryProjectionAtoms, true);
@Override public IQTree liftIncompatibleDefinitions(Variable variable, IQTree child) { if (!childVariables.contains(variable)) { return iqFactory.createUnaryIQTree(this, child); } IQTree newChild = child.liftIncompatibleDefinitions(variable); QueryNode newChildRoot = newChild.getRootNode(); /* * Lift the union above the construction node */ if ((newChildRoot instanceof UnionNode) && ((UnionNode) newChildRoot).hasAChildWithLiftableDefinition(variable, newChild.getChildren())) { ImmutableList<IQTree> grandChildren = newChild.getChildren(); ImmutableList<IQTree> newChildren = grandChildren.stream() .map(c -> (IQTree) iqFactory.createUnaryIQTree(this, c)) .collect(ImmutableCollectors.toList()); UnionNode newUnionNode = iqFactory.createUnionNode(getVariables()); return iqFactory.createNaryIQTree(newUnionNode, newChildren); } return iqFactory.createUnaryIQTree(this, newChild); }
@Override public IQTree liftIncompatibleDefinitions(Variable variable, IQTree leftChild, IQTree rightChild) { if (leftChild.getVariables().contains(variable)) { IQTree liftedLeftChild = leftChild.liftIncompatibleDefinitions(variable); QueryNode leftChildRoot = liftedLeftChild.getRootNode(); if (leftChildRoot instanceof UnionNode && ((UnionNode) leftChildRoot).hasAChildWithLiftableDefinition(variable, leftChild.getChildren())) { UnionNode newUnionNode = iqFactory.createUnionNode( Stream.of(leftChild, rightChild) .flatMap(c -> c.getVariables().stream()) .collect(ImmutableCollectors.toSet())); return iqFactory.createNaryIQTree(newUnionNode, liftedLeftChild.getChildren().stream() .map(unionChild -> (IQTree) iqFactory.createBinaryNonCommutativeIQTree(this, unionChild, rightChild)) .collect(ImmutableCollectors.toList())); } } // By default, nothing lifted return iqFactory.createBinaryNonCommutativeIQTree(this, leftChild, rightChild); }
/** * TODO: explain */ @Override public IQTree liftBinding(ImmutableList<IQTree> initialChildren, VariableGenerator variableGenerator, IQProperties currentIQProperties) { IQTree newParentTree = propagateDownCondition(Optional.empty(), initialChildren); /* * If after propagating down the condition the root is still a join node, goes to the next step */ if (newParentTree.getRootNode() instanceof InnerJoinNodeImpl) { return ((InnerJoinNodeImpl)newParentTree.getRootNode()).liftBindingAfterPropagatingCondition( newParentTree.getChildren(), variableGenerator, currentIQProperties); } else /* * Otherwise, goes back to the general method */ return newParentTree.liftBinding(variableGenerator); }
ImmutableCQ cq; if (child.getRootNode() instanceof InnerJoinNode && !child.getChildren().stream() .anyMatch(c -> !(c.getRootNode() instanceof IntensionalDataNode))) { cq = new ImmutableCQ(avs, child.getChildren().stream() .map(c -> ((IntensionalDataNode)c.getRootNode()).getProjectionAtom()) .collect(ImmutableCollectors.toList())); ImmutableSubstitution<ImmutableTerm> substitution = ((ConstructionNode)child.getRootNode()).getSubstitution(); ImmutableList<Variable> avs1 = (ImmutableList<Variable>) substitution.apply(avs); IQTree subtree = child.getChildren().get(0); if ((subtree.getRootNode() instanceof InnerJoinNode) && !subtree.getChildren().stream() .anyMatch(c -> !(c.getRootNode() instanceof IntensionalDataNode))) { cq = new ImmutableCQ(avs1, subtree.getChildren().stream() .map(c -> ((IntensionalDataNode)c.getRootNode()).getProjectionAtom()) .collect(ImmutableCollectors.toList()));
@Override public QueryNode replaceSubTreeByIQ(QueryNode subTreeRoot, IQTree replacingSubTree) { QueryNode iqRoot = replacingSubTree.getRootNode(); QueryNode newSubTreeRoot = contains(iqRoot) ? iqRoot.clone() : iqRoot; replaceSubTree(subTreeRoot, newSubTreeRoot); insertIQChildren(newSubTreeRoot, replacingSubTree.getChildren()); return newSubTreeRoot; }
@Override public IQTree transformConstruction(IQTree tree, ConstructionNode rootCn, IQTree child) { IQTree transformedChild = child.acceptTransformer(this); QueryNode transformedChildRoot = transformedChild.getRootNode(); // if the child is a union, lift it if (transformedChildRoot instanceof UnionNode) { return iqFactory.createNaryIQTree( iqFactory.createUnionNode(rootCn.getVariables()), transformedChild.getChildren().stream() .map(t -> iqFactory.createUnaryIQTree(rootCn, t)) .collect(ImmutableCollectors.toList()) ); } // if the child is a construction node, merge it if (transformedChildRoot instanceof ConstructionNode) { return rootCn.liftBinding( transformedChild, variableGenerator, iqFactory.createIQProperties() ); } return iqFactory.createUnaryIQTree(rootCn, transformedChild); }
List<IQTree> children = tree.getChildren(); body.addAll(getAtomFrom(children.get(0), heads, subQueryProjectionAtoms, true)); for (IQTree child : tree.getChildren()) {
@Override public IntermediateQuery convert(IQ query, DBMetadata dbMetadata, ExecutorRegistry executorRegistry) throws EmptyQueryException { if (query.getTree().isDeclaredAsEmpty()) throw new EmptyQueryException(); IntermediateQueryBuilder queryBuilder = iqFactory.createIQBuilder(dbMetadata, executorRegistry); IQTree topTree = query.getTree(); QueryNode rootNode = topTree.getRootNode(); queryBuilder.init(query.getProjectionAtom(), rootNode); insertChildren(rootNode, topTree.getChildren(), queryBuilder); return queryBuilder.build(); }