/** * Immutable fields, can be shared. */ @Override public ConstructionNode clone() { return iqFactory.createConstructionNode(projectedVariables, substitution); }
public Optional<ConstructionNode> generateTopConstructionNode() { return Optional.of(ascendingSubstitution) .filter(s -> !s.isEmpty()) .map(s -> iqFactory.createConstructionNode(projectedVariables, s)); }
private Optional<RightProvenance> createProvenanceElements(Map.Entry<Variable, Constant> provenanceVariableDefinition, IQTree rightTree) { Variable rightProvenanceVariable = provenanceVariableDefinition.getKey(); ImmutableSet<Variable> newRightProjectedVariables = Stream.concat(Stream.of(rightProvenanceVariable), rightTree.getVariables().stream()) .collect(ImmutableCollectors.toSet()); ConstructionNode newRightConstructionNode = iqFactory.createConstructionNode( newRightProjectedVariables, substitutionFactory.getSubstitution(rightProvenanceVariable, provenanceVariableDefinition.getValue())); return Optional.of(new RightProvenance(rightProvenanceVariable, newRightConstructionNode)); }
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"); }
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 IQTree getProjection(ImmutableSet<Variable> signature, IQTree tree) { QueryNode root = tree.getRootNode(); if (root instanceof ConstructionNode){ return iqFactory.createUnaryIQTree( iqFactory.createConstructionNode(signature, ((ConstructionNode)root).getSubstitution()), ((UnaryIQTree)tree).getChild() ); } return iqFactory.createUnaryIQTree( iqFactory.createConstructionNode(signature), tree ); } }
private static void insertRootConstructionNode(IntermediateQuery query, QueryTreeComponent treeComponent, ImmutableSubstitution<? extends ImmutableTerm> propagatedSubstitution, IntermediateQueryFactory iqFactory) { ImmutableSet<Variable> projectedVariables = query.getProjectionAtom().getVariables(); ImmutableMap<Variable, ImmutableTerm> newSubstitutionMap = propagatedSubstitution.getImmutableMap().entrySet().stream() .filter(e -> projectedVariables.contains(e.getKey())) .collect(ImmutableCollectors.toMap( Map.Entry::getKey, e -> (ImmutableTerm) e.getValue())); ConstructionNode newRootNode = iqFactory.createConstructionNode(projectedVariables, SUBSTITUTION_FACTORY.getSubstitution(newSubstitutionMap)); treeComponent.insertParent(treeComponent.getRootNode(), newRootNode); }
private IQTree getIntensionalCanonizedTree(IQ assertion, DistinctVariableOnlyDataAtom projAtom, IntensionalDataNode intensionalDataNode) { return iqFactory.createUnaryIQTree( iqFactory.createConstructionNode(projAtom.getVariables()), iqFactory.createNaryIQTree( iqFactory.createInnerJoinNode(), ImmutableList.of( assertion.getTree(), intensionalDataNode ))); }
@Override public ConstructionNode transform(ConstructionNode constructionNode) { return iqFactory.createConstructionNode(renameProjectedVars(constructionNode.getVariables()), renameSubstitution(constructionNode.getSubstitution())); }
@Override public IQTree transformInnerJoin(IQTree tree, InnerJoinNode rootNode, ImmutableList<IQTree> children) { ImmutableList<InjectiveVar2VarSubstitution> substitutions = computeSubstitutions(children); if (substitutions.stream().allMatch(ImmutableSubstitution::isEmpty)) return tree; ImmutableList<IQTree> updatedChildren = updateJoinChildren(substitutions, children); return iqFactory.createUnaryIQTree( iqFactory.createConstructionNode(tree.getVariables()), iqFactory.createNaryIQTree( iqFactory.createInnerJoinNode( Optional.of(updateJoinCondition( rootNode.getOptionalFilterCondition(), substitutions ))), updatedChildren )); }
private IQTree propagate(IQTree subTree, ImmutableSubstitution<? extends ImmutableTerm> substitution) throws InvalidQueryOptimizationProposalException { IQTree newSubTree = subTree.applyDescendingSubstitution(substitution.getVariableOrGroundTermFragment(), Optional.empty()); if (substitution.getNonGroundFunctionalTermFragment().getDomain().stream() .anyMatch(v -> newSubTree.getVariables().contains(v))) throw new InvalidQueryOptimizationProposalException("Non ground functional terms are not supported for propagation down " + "(only for up)"); ConstructionNode constructionNode = iqFactory.createConstructionNode( Sets.union(newSubTree.getVariables(), substitution.getDomain()).immutableCopy(), (ImmutableSubstitution<ImmutableTerm>) substitution); return iqFactory.createUnaryIQTree(constructionNode, newSubTree); } }
private IQTree enforceRootCn(IQTree tree, ImmutableSet<Variable> projectedVariables) { if (tree.getRootNode() instanceof ConstructionNode) { ConstructionNode currentRootNode = (ConstructionNode) tree.getRootNode(); if (currentRootNode.getVariables().equals(projectedVariables)) return tree; ConstructionNode newRootNode = iqFactory.createConstructionNode(projectedVariables, currentRootNode.getSubstitution() .reduceDomainToIntersectionWith(projectedVariables)); return iqFactory.createUnaryIQTree(newRootNode, ((UnaryIQTree) tree).getChild()); } else return iqFactory.createUnaryIQTree( iqFactory.createConstructionNode(projectedVariables), tree); }
private NodeCentricOptimizationResults<ConstructionNode> flattenConstructionNodeChain(IntermediateQuery query, QueryTreeComponent treeComponent, ConstructionNode focusNode, QueryNode childSubtreeRoot) { IntermediateQuery snapshot = query.createSnapshot(); ConstructionNode replacingNode = iqFactory.createConstructionNode( focusNode.getVariables(), focusNode.getSubstitution()); treeComponent.replaceSubTree(focusNode, replacingNode); treeComponent.addChild(replacingNode, childSubtreeRoot, Optional.empty(), false); treeComponent.addSubTree(snapshot, childSubtreeRoot, childSubtreeRoot); return new NodeCentricOptimizationResultsImpl<>(query, replacingNode); } }
private ConstructionNode mergeCns(ConstructionNode parent, ConstructionNode child) { ImmutableSubstitution composition = child.getSubstitution() .composeWith(parent.getSubstitution()) .reduceDomainToIntersectionWith(parent.getVariables()); return iqFactory.createConstructionNode(parent.getVariables(), composition); }
@Override public ConstructionNode transform(ConstructionNode constructionNode) { return iqFactory.createConstructionNode(renameProjectedVars(constructionNode.getVariables()), renameSubstitution(constructionNode.getSubstitution()), renameOptionalModifiers(constructionNode.getOptionalModifiers()) ); }
private NodeCentricOptimizationResults<ConstructionNode> flattenConstructionNodeChain(IntermediateQuery query, QueryTreeComponent treeComponent, ConstructionNode focusNode, QueryNode childSubtreeRoot, Optional<ImmutableQueryModifiers> modifiers) { IntermediateQuery snapshot = query.createSnapshot(); ConstructionNode replacingNode = iqFactory.createConstructionNode( focusNode.getVariables(), focusNode.getSubstitution(), modifiers ); treeComponent.replaceSubTree(focusNode, replacingNode); treeComponent.addChild(replacingNode, childSubtreeRoot, Optional.empty(), false); treeComponent.addSubTree(snapshot, childSubtreeRoot, childSubtreeRoot); return new NodeCentricOptimizationResultsImpl<>(query, replacingNode); } }
private IQTree liftBinding(ConstructionNode childConstructionNode, UnaryIQTree childIQ, IQProperties currentIQProperties) { AscendingSubstitutionNormalization ascendingNormalization = normalizeAscendingSubstitution( childConstructionNode.getSubstitution().composeWith(substitution), projectedVariables); ImmutableSubstitution<ImmutableTerm> newSubstitution = ascendingNormalization.getAscendingSubstitution(); IQTree grandChildIQTree = ascendingNormalization.normalizeChild(childIQ.getChild()); ConstructionNode newConstructionNode = iqFactory.createConstructionNode(projectedVariables, newSubstitution); return iqFactory.createUnaryIQTree(newConstructionNode, grandChildIQTree, currentIQProperties.declareLifted()); }
private IQTree transformDataNode(DataNode dn) { ImmutableList<Optional<Variable>> replacementVars = getArgumentReplacement(dn); if (empt(replacementVars)) return dn; FilterNode filter = createFilter(dn.getProjectionAtom(), replacementVars); DataAtom atom = replaceVars(dn.getProjectionAtom(), replacementVars); return iqFactory.createUnaryIQTree( iqFactory.createConstructionNode(dn.getVariables()), iqFactory.createUnaryIQTree( filter, dn.newAtom(atom)) ); }
@Override public IntermediateQuery enforceRootCn(IntermediateQuery query) { QueryNode root = query.getRootNode(); if(root instanceof ConstructionNode){ return query; } IntermediateQueryBuilder builder = iqFactory.createIQBuilder( query.getDBMetadata(), query.getExecutorRegistry() ); ConstructionNode rootCn = iqFactory.createConstructionNode(query.getVariables(root)); builder.init(query.getProjectionAtom(), rootCn); builder.addChild(rootCn, root); builder.appendSubtree(root, query); return builder.build(); }
/** * TODO: describe */ public IQ convertDatalogRule(CQIE datalogRule, Collection<Predicate> tablePredicates, Optional<ImmutableQueryModifiers> optionalModifiers, IntermediateQueryFactory iqFactory) throws DatalogProgram2QueryConverterImpl.InvalidDatalogProgramException { TargetAtom targetAtom = datalogConversionTools.convertFromDatalogDataAtom(datalogRule.getHead()); DistinctVariableOnlyDataAtom projectionAtom = targetAtom.getProjectionAtom(); ConstructionNode topConstructionNode = iqFactory.createConstructionNode(projectionAtom.getVariables(), targetAtom.getSubstitution()); List<Function> bodyAtoms = List.iterableList(datalogRule.getBody()); if (bodyAtoms.isEmpty()) { return createFact(topConstructionNode, optionalModifiers, projectionAtom, iqFactory); } else { return createDefinition(topConstructionNode, optionalModifiers, projectionAtom, tablePredicates, bodyAtoms, iqFactory); } }