private Stream<Variable> getProjectedVariableStream(QueryNode node) { if (node instanceof ExplicitVariableProjectionNode) { return ((ExplicitVariableProjectionNode) node).getVariables().stream(); } else { return getChildrenStream(node) .flatMap(this::getProjectedVariableStream); } }
private Stream<Variable> getProjectedVariableStream(QueryNode node) { if (node instanceof ExplicitVariableProjectionNode) { return ((ExplicitVariableProjectionNode) node).getVariables().stream(); } else { return getChildrenStream(node) .flatMap(this::getProjectedVariableStream); } }
/** * TODO: optimize by it but materializing (and maintaining) the results. */ @Override public ImmutableSet<Variable> getVariables(QueryNode node) { if (node instanceof ExplicitVariableProjectionNode) { return ((ExplicitVariableProjectionNode) node).getVariables(); } else { return getProjectedVariableStream(node) .collect(ImmutableCollectors.toSet()); } }
@Override public ImmutableSet<Variable> getVariables() { if (rootNode instanceof ExplicitVariableProjectionNode) return ((ExplicitVariableProjectionNode) rootNode).getVariables(); else return children.stream() .flatMap(c -> c.getVariables().stream()) .collect(ImmutableCollectors.toSet()); }
/** * TODO: optimize by it but materializing (and maintaining) the results. */ @Override public ImmutableSet<Variable> getVariables(QueryNode node) { if (node instanceof ExplicitVariableProjectionNode) { return ((ExplicitVariableProjectionNode) node).getVariables(); } else { return getProjectedVariableStream(node) .collect(ImmutableCollectors.toSet()); } }
private Optional<ProjectionShrinkingProposal> makeProposal(ExplicitVariableProjectionNode node, IntermediateQuery query, ImmutableSet<Variable> allRetainedVariables) { if (node instanceof UnionNode || node instanceof ConstructionNode) { Map<Boolean, List<Variable>> splitVariables = node.getVariables().stream() .collect(Collectors.partitioningBy(v -> allRetainedVariables.contains(v))); if (splitVariables.get(false).iterator().hasNext()) { return Optional.of( new ProjectionShrinkingProposalImpl( node, splitVariables.get(true).stream().collect(ImmutableCollectors.toSet()) )); } return Optional.empty(); } throw new IllegalStateException("A projection shrinking proposal can only be made for a Union or Construction node"); }
private Optional<ProjectionShrinkingProposal> makeProposal(ExplicitVariableProjectionNode node, ImmutableSet<Variable> allRetainedVariables) { if (node instanceof UnionNode || node instanceof ConstructionNode) { Map<Boolean, List<Variable>> splitVariables = node.getVariables().stream() .collect(Collectors.partitioningBy(allRetainedVariables::contains)); if (splitVariables.get(false).iterator().hasNext()) { return Optional.of( new ProjectionShrinkingProposalImpl( node, splitVariables.get(true).stream().collect(ImmutableCollectors.toSet()) )); } return Optional.empty(); } throw new IllegalStateException("A projection shrinking proposal can only be made for a Union or Construction node"); }
private Optional<ExplicitVariableProjectionNode> getProjectorReplacementNode(ExplicitVariableProjectionNode replacedNode, ImmutableExpression expressionToPropagate) { if (expressionToPropagate.getVariables().size() == 0) { return Optional.empty(); } ImmutableSet.Builder<Variable> allProjectedVariablesBuilder = ImmutableSet.builder(); allProjectedVariablesBuilder.addAll(replacedNode.getVariables()); allProjectedVariablesBuilder.addAll(expressionToPropagate.getVariables()); if (replacedNode instanceof UnionNode) { return Optional.of(iqFactory.createUnionNode(allProjectedVariablesBuilder.build())); } if (replacedNode instanceof ConstructionNode) { return Optional.of(iqFactory.createConstructionNode(allProjectedVariablesBuilder.build(), ((ConstructionNode) replacedNode).getSubstitution())); } throw new IllegalStateException("Unsupported node type"); }
@Override public void init(DistinctVariableOnlyDataAtom projectionAtom, QueryNode rootNode){ if (tree != null) throw new IllegalArgumentException("Already initialized IntermediateQueryBuilder."); if ((rootNode instanceof ExplicitVariableProjectionNode) && !projectionAtom.getVariables().equals(((ExplicitVariableProjectionNode)rootNode).getVariables())) { throw new IllegalArgumentException("The root node " + rootNode + " is not consistent with the projection atom " + projectionAtom); } // TODO: use Guice to construct this tree tree = new DefaultTree(rootNode); this.projectionAtom = projectionAtom; canEdit = true; }
@Override public void init(DistinctVariableOnlyDataAtom projectionAtom, QueryNode rootNode){ if (tree != null) throw new IllegalArgumentException("Already initialized IntermediateQueryBuilder."); if ((rootNode instanceof ExplicitVariableProjectionNode) && !projectionAtom.getVariables().equals(((ExplicitVariableProjectionNode)rootNode).getVariables())) { throw new IllegalArgumentException("The root node " + rootNode + " is not consistent with the projection atom " + projectionAtom); } // TODO: use Guice to construct this tree tree = new DefaultTree(rootNode); this.projectionAtom = projectionAtom; canEdit = true; }
private Optional<ExplicitVariableProjectionNode> getProjectorReplacementNode(ExplicitVariableProjectionNode replacedNode, ImmutableExpression expressionToPropagate) { if (expressionToPropagate.getVariables().size() == 0) { return Optional.empty(); } ImmutableSet.Builder<Variable> allProjectedVariablesBuilder = ImmutableSet.builder(); allProjectedVariablesBuilder.addAll(replacedNode.getVariables()); allProjectedVariablesBuilder.addAll(expressionToPropagate.getVariables()); if (replacedNode instanceof UnionNode) { return Optional.of(iqFactory.createUnionNode(allProjectedVariablesBuilder.build())); } if (replacedNode instanceof ConstructionNode) { return Optional.of(iqFactory.createConstructionNode(allProjectedVariablesBuilder.build(), ((ConstructionNode) replacedNode).getSubstitution(), ((ConstructionNode) replacedNode).getOptionalModifiers())); } throw new IllegalStateException("Unsupported node type"); }
private Stream<Variable> extractPriorityVariables(IntermediateQuery query, QueryNode node, Optional<ArgumentPosition> childPosition) { if (node instanceof ExplicitVariableProjectionNode) return ((ExplicitVariableProjectionNode)node).getVariables().stream(); /* * LJ: look for variables on the left * only when the focus node is on the right */ else if (node instanceof LeftJoinNode) { switch (childPosition.get()) { case RIGHT: return query.getChild(node, LEFT) .map(c -> query.getVariables(c).stream()) .orElseThrow(() -> new IllegalStateException("A LJ must have a left child")); case LEFT: default: return Stream.empty(); } } else { return Stream.empty(); } }
private Stream<Variable> extractPriorityVariables(IntermediateQuery query, QueryNode node, Optional<ArgumentPosition> childPosition) { if (node instanceof ExplicitVariableProjectionNode) return ((ExplicitVariableProjectionNode)node).getVariables().stream(); /* * LJ: look for variables on the left * only when the focus node is on the right */ else if (node instanceof LeftJoinNode) { switch (childPosition.get()) { case RIGHT: return query.getChild(node, LEFT) .map(c -> query.getVariables(c).stream()) .orElseThrow(() -> new IllegalStateException("A LJ must have a left child")); case LEFT: default: return Stream.empty(); } } else { return Stream.empty(); } }
private void checkProjection() { if (focusNode instanceof ExplicitVariableProjectionNode) { ExplicitVariableProjectionNode node = (ExplicitVariableProjectionNode) focusNode; if (!node.getVariables().containsAll(substitutionToPropagate.getDomain())) { throw new InvalidQueryOptimizationProposalException("Only variables projected by " + focusNode + " can be propagated.\n Invalid substitution was " + substitutionToPropagate); } } }
@Override public ImmutableSet<Variable> getVariables() { UnaryOperatorNode rootNode = getRootNode(); if (rootNode instanceof ExplicitVariableProjectionNode) return ((ExplicitVariableProjectionNode) rootNode).getVariables(); else return getChild().getVariables(); }
private void checkProjection() { if (focusNode instanceof ExplicitVariableProjectionNode) { ExplicitVariableProjectionNode node = (ExplicitVariableProjectionNode) focusNode; if (!node.getVariables().containsAll(substitutionToPropagate.getDomain())) { throw new InvalidQueryOptimizationProposalException("Only variables projected by " + focusNode + " can be propagated.\n Invalid substitution was " + substitutionToPropagate); } } }