/** * TODO: inject termFactory and typeFactory * */ default Optional<TermType> getOptionalTermType() throws IncompatibleTermException { try { OperationPredicate predicate = getFunctionSymbol(); return predicate.inferType(getTerms()); } catch (IncompatibleTermException e) { throw new IncompatibleTermException(this, e); } }
/** * Returns the boolean conjuncts shared by all providers. */ private ImmutableSet<ImmutableExpression> getExpressionsToPropagateAboveUnion(ImmutableSet<CommutativeJoinOrFilterNode> providers) { return providers.stream() .map(n -> n.getOptionalFilterCondition().get().flattenAND()) .reduce(this::computeIntersection).get(); }
@Override public ImmutableExpression normalize(ImmutableExpression expression) { OperationPredicate functionSymbol = expression.getFunctionSymbol(); if (functionSymbol instanceof ExpressionOperation) { switch((ExpressionOperation)functionSymbol) { case ADD: case MULTIPLY: //case AND: //case OR: case EQ: case NEQ: return normalizeCommutative(functionSymbol, expression.getArguments()); default: return normalizeArguments(functionSymbol, expression.getArguments()); } } else { return expression; } }
ImmutableSet<ImmutableExpression> expressions = expression.flattenAND(); ImmutableSet<ImmutableExpression> downSubstitutionExpressions = expressions.stream() .filter(e -> e.getFunctionSymbol().equals(EQ)) .filter(e -> { ImmutableList<? extends ImmutableTerm> arguments = e.getTerms(); return arguments.stream().allMatch(t -> t instanceof NonFunctionalTerm) && arguments.stream().anyMatch(rightVariables::contains); expressions.stream() .filter(e -> (!downSubstitutionExpressions.contains(e)) || e.getTerms().stream().anyMatch(rightSpecificVariables::contains))) .map(downSubstitution::applyToBooleanExpression);
ImmutableList<Optional<TermType>> argumentTypes = expression.getTerms().stream() .map(t -> getCastTypeFromSubRule(t, bodyDataAtoms, alreadyKnownCastTypes)) .map(Optional::of) .collect(ImmutableCollectors.toList()); return expression.getOptionalTermType(argumentTypes) .orElseThrow(() -> new IllegalStateException("No type could be inferred for " + term));
@Override public ImmutableSet<Variable> getLocalVariables() { if (optionalFilterCondition.isPresent()) { return optionalFilterCondition.get().getVariables(); } else { return ImmutableSet.of(); } }
private boolean isExpressionNullable(ImmutableExpression expression, ImmutableSet<Variable> nullableVariables) { OperationPredicate functionSymbol = expression.getFunctionSymbol(); if (functionSymbol instanceof ExpressionOperation) { switch((ExpressionOperation) functionSymbol) { case IS_NOT_NULL: case IS_NULL: return false; default: break; } } // TODO: support COALESCE and IF-THEN-ELSE (they will need to use isFilteringNullValue) return hasNullableArgument(expression, nullableVariables); } }
return ((ImmutableExpression) f).getOptionalTermType();
public static ImmutableSet<ImmutableExpression> retainVar2VarEqualityConjuncts(ImmutableExpression expression) { return filterOuterMostConjuncts(e -> e.isVar2VarEquality(), expression); }
protected void checkExpression(ImmutableExpression expression, ImmutableList<IQTree> children) throws InvalidIntermediateQueryException { ImmutableSet<Variable> childrenVariables = children.stream() .flatMap(c -> c.getVariables().stream()) .collect(ImmutableCollectors.toSet()); ImmutableSet<Variable> unboundVariables = expression.getVariableStream() .filter(v -> !childrenVariables.contains(v)) .collect(ImmutableCollectors.toSet()); if (!unboundVariables.isEmpty()) { throw new InvalidIntermediateQueryException("Expression " + expression + " of " + expression + " uses unbound variables (" + unboundVariables + ").\n" + this); } }
@Override public ImmutableSet<ImmutableExpression> flatten(OperationPredicate operator) { /** * Only flattens OR expressions. */ if (getFunctionSymbol().equals(operator)) { ImmutableSet.Builder<ImmutableExpression> setBuilder = ImmutableSet.builder(); for (ImmutableTerm subTerm : getArguments()) { /** * Recursive call */ if (subTerm instanceof ImmutableExpression) { setBuilder.addAll(((ImmutableExpression) subTerm).flatten(operator)); } else { throw new IllegalStateException("An AND-expression must be only composed of " + "ImmutableBooleanExpression(s), not of a " + subTerm); } } return setBuilder.build(); } else { return ImmutableSet.of(this); } } }
throws UnsatisfiableConditionException { ImmutableSet<ImmutableExpression> expressions = expression.flattenAND(); ImmutableSet<ImmutableExpression> functionFreeEqualities = expressions.stream() .filter(e -> e.getFunctionSymbol().equals(EQ)) .filter(e -> { ImmutableList<? extends ImmutableTerm> arguments = e.getTerms(); return arguments.stream().allMatch(t -> t instanceof NonFunctionalTerm); })
@Override public ImmutableSet<Variable> getLocalVariables() { if (optionalFilterCondition.isPresent()) { return optionalFilterCondition.get().getVariables(); } else { return ImmutableSet.of(); } }
private boolean isExpressionNullable(ImmutableExpression expression, ImmutableSet<Variable> nullableVariables) { OperationPredicate functionSymbol = expression.getFunctionSymbol(); if (functionSymbol instanceof ExpressionOperation) { switch((ExpressionOperation) functionSymbol) { case IS_NOT_NULL: case IS_NULL: return false; default: break; } } // TODO: support COALESCE and IF-THEN-ELSE (they will need to use isFilteringNullValue) return hasNullableArgument(expression, nullableVariables); } }
public ImmutableSet<ImmutableExpression> retainVar2VarEqualityConjuncts(ImmutableExpression expression) { return filterOuterMostConjuncts(e -> e.isVar2VarEquality(), expression); }
private void checkExpression(JoinOrFilterNode node, ImmutableExpression expression) { ImmutableSet<Variable> unboundVariables = expression.getVariableStream() .filter(v -> !(query.getChildren(node).stream() .flatMap(c -> query.getVariables(c).stream()) .collect(ImmutableCollectors.toSet()) .contains(v))) .collect(ImmutableCollectors.toSet()); if (!unboundVariables.isEmpty()) { throw new InvalidIntermediateQueryException("Expression " + expression + " of " + expression + " uses unbound variables (" + unboundVariables + ").\n" + query); } }
@Override public ImmutableSet<ImmutableExpression> flatten(OperationPredicate operator) { /** * Only flattens OR expressions. */ if (getFunctionSymbol().equals(operator)) { ImmutableSet.Builder<ImmutableExpression> setBuilder = ImmutableSet.builder(); for (ImmutableTerm subTerm : getTerms()) { /** * Recursive call */ if (subTerm instanceof ImmutableExpression) { setBuilder.addAll(((ImmutableExpression) subTerm).flatten(operator)); } else { throw new IllegalStateException("An AND-expression must be only composed of " + "ImmutableBooleanExpression(s), not of a " + subTerm); } } return setBuilder.build(); } else { return ImmutableSet.of(this); } } }
/** * This method takes a immutable boolean term and convert it into an old mutable boolean function. */ public Expression convertToMutableBooleanExpression(ImmutableExpression booleanExpression) { OperationPredicate pred = booleanExpression.getFunctionSymbol(); return termFactory.getExpression(pred, convertToMutableTerms(booleanExpression.getTerms())); }
/** * Returns the boolean conjuncts shared by all providers. */ private ImmutableSet<ImmutableExpression> getExpressionsToPropagateAboveUnion(ImmutableSet<CommutativeJoinOrFilterNode> providers) { return providers.stream() .map(n -> n.getOptionalFilterCondition().get().flattenAND()) .reduce(this::computeIntersection).get(); }
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"); }