Java Code Examples for org.eclipse.rdf4j.query.algebra.Filter#replaceWith()

The following examples show how to use org.eclipse.rdf4j.query.algebra.Filter#replaceWith() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DisjunctiveConstraintOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(Filter filter) {
	if (filter.getCondition() instanceof Or && containsSameTerm(filter.getCondition())) {
		Or orNode = (Or) filter.getCondition();
		TupleExpr filterArg = filter.getArg();

		ValueExpr leftConstraint = orNode.getLeftArg();
		ValueExpr rightConstraint = orNode.getRightArg();

		// remove filter
		filter.replaceWith(filterArg);

		// Push UNION down below other filters to avoid cloning them
		TupleExpr node = findNotFilter(filterArg);

		Filter leftFilter = new Filter(node.clone(), leftConstraint);
		Filter rightFilter = new Filter(node.clone(), rightConstraint);
		Union union = new Union(leftFilter, rightFilter);
		node.replaceWith(union);

		filter.getParentNode().visit(this);
	} else {
		super.meet(filter);
	}
}
 
Example 2
Source File: QueryModelNormalizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(Filter node) {
	super.meet(node);

	TupleExpr arg = node.getArg();
	ValueExpr condition = node.getCondition();

	if (arg instanceof EmptySet) {
		// see #meetUnaryTupleOperator
	} else if (condition instanceof ValueConstant) {
		boolean conditionValue;
		try {
			conditionValue = QueryEvaluationUtil.getEffectiveBooleanValue(((ValueConstant) condition).getValue());
		} catch (ValueExprEvaluationException e) {
			conditionValue = false;
		}

		if (conditionValue == false) {
			// Constraint is always false
			node.replaceWith(new EmptySet());
		} else {
			node.replaceWith(arg);
		}
	}
}
 
Example 3
Source File: SeparateFilterJoinsVisitor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(final Filter node) throws Exception {
    super.meet(node);

    final ValueExpr condition = node.getCondition();
    final TupleExpr arg = node.getArg();
    if (!(arg instanceof Join)) {
        return;
    }

    final Join join = (Join) arg;
    final TupleExpr leftArg = join.getLeftArg();
    final TupleExpr rightArg = join.getRightArg();

    if (leftArg instanceof StatementPattern && rightArg instanceof StatementPattern) {
        final Filter left = new Filter(leftArg, condition);
        final Filter right = new Filter(rightArg, condition);
        node.replaceWith(new Join(left, right));
    }

}
 
Example 4
Source File: PeriodicQueryUtil.java    From rya with Apache License 2.0 6 votes vote down vote up
public void meet(Filter node) {
    if (node.getCondition() instanceof FunctionCall) {
        try {
            Optional<PeriodicQueryNode> optNode = getPeriodicQueryNode((FunctionCall) node.getCondition(), node.getArg());
            if (optNode.isPresent()) {
                if (count > 0) {
                    throw new IllegalArgumentException("Query cannot contain more than one PeriodicQueryNode");
                }
                periodicNode = optNode.get();
                node.replaceWith(periodicNode);
                count++;
                periodicNode.visit(this);
            } else {
                super.meet(node);
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    } else {
        super.meet(node);
    }
}
 
Example 5
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(Filter node) {

          Set<QueryModelNode> eSet = getQNodes(node);

          if (eSet.containsAll(sSet)) {

              if (eSet.equals(sSet)) {
                  node.replaceWith(set);
                  indexPlaced = true;
                  return;
              } else {
                  node.getArg().visit(this);
              }
          }
      }
 
Example 6
Source File: FilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void relocate(Filter filter, TupleExpr newFilterArg) {
	if (filter.getArg() != newFilterArg) {
		if (filter.getParentNode() != null) {
			// Remove filter from its original location
			filter.replaceWith(filter.getArg());
		}

		// Insert filter at the new location
		newFilterArg.replaceWith(filter);
		filter.setArg(newFilterArg);
	}
}
 
Example 7
Source File: SameTermFilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void renameVar(Var oldVar, Var newVar, Filter filter) {
	filter.getArg().visit(new VarRenamer(oldVar, newVar));

	// TODO: skip this step if old variable name is not used
	// Replace SameTerm-filter with an Extension, the old variable name
	// might still be relevant to nodes higher in the tree
	Extension extension = new Extension(filter.getArg());
	extension.addElement(new ExtensionElem(new Var(newVar.getName()), oldVar.getName()));
	filter.replaceWith(extension);
}
 
Example 8
Source File: SparqlToPipelineTransformVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(Filter filterNode) throws Exception {
    filterNode.visitChildren(this);
    if (filterNode.getArg() instanceof AggregationPipelineQueryNode && filterNode.getParentNode() != null) {
        AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) filterNode.getArg();
        if (pipelineNode.filter(filterNode.getCondition())) {
            filterNode.replaceWith(pipelineNode);
        }
    }
}
 
Example 9
Source File: PCJOptimizerUtilities.java    From rya with Apache License 2.0 5 votes vote down vote up
protected void relocate(final Filter filter, final TupleExpr newFilterArg) {
	if (!filter.getArg().equals(newFilterArg)) {
		if (filter.getParentNode() != null) {
			// Remove filter from its original location
			filter.replaceWith(filter.getArg());
		}
		// Insert filter at the new location
		newFilterArg.replaceWith(filter);
		filter.setArg(newFilterArg);
	}
}
 
Example 10
Source File: FilterOptimizer.java    From CostFed with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void meet(Filter filter)  {
	
	if (filter.getArg() instanceof EmptyResult) {
		log.debug("Argument of filter expression does not yield results at the provided sources, replacing Filter node.");
		filter.replaceWith(filter.getArg());
		return;
	}
				
	/*
	 * TODO idea:
	 * if we have a FILTER such as ?s='a' OR ?s='b' OR ?s='c' handle this appropriately
	 */
	
	ValueExpr valueExpr = filter.getCondition();
	
	/*
	 * TODO transform condition into some normal form, e.g. CNF
	 */
	
	// determine conjunctive expressions
	List<ValueExpr> conjunctiveExpressions = new ArrayList<ValueExpr>();
	getConjunctiveExpressions(valueExpr, conjunctiveExpressions);
			
	FilterExprInsertVisitor filterExprVst = new FilterExprInsertVisitor();
	List<ValueExpr> remainingExpr = new ArrayList<ValueExpr>(conjunctiveExpressions.size());
	
	for (ValueExpr cond : conjunctiveExpressions) {
		
		/*
		 * Determine if this filter is applicable for optimization.
		 * Currently only leaf expressions are applicable, i.e.
		 * not combined expressions.
		 */
		if (isCompatibleExpr(cond)) {
						
			HashSet<String> exprVars = new VarFinder().findVars(cond);
			FilterExpr filterExpr = new FilterExpr(cond, exprVars);
			
			filterExprVst.initialize(filterExpr);
			filter.getArg().visit(filterExprVst);
			
			// if the filter expr. is handled in the stmt we do not have to keep it
			if (filterExprVst.canRemove())
				continue;
			
			remainingExpr.add(filterExpr.getExpression());
			
		} else {
			remainingExpr.add(cond);
		}
		
	}
	
	if (remainingExpr.size()==0) {
		filter.replaceWith(filter.getArg()); 	// remove the filter			
	}
	
	else if (remainingExpr.size()==1) {
		filter.setCondition(remainingExpr.get(0));		// just apply the remaining condition
	}
	
	else {
		
		// construct conjunctive value expr
		And root = new And();	
		root.setLeftArg(remainingExpr.get(0));
		And tmp = root;
		for (int i=1; i<remainingExpr.size()-1; i++) {
			And _a = new And();
			_a.setLeftArg(remainingExpr.get(i));
			tmp.setRightArg(_a);
			tmp = _a;				
		}
		tmp.setRightArg(remainingExpr.get(remainingExpr.size()-1));
		
		filter.setCondition(root);
	}
	
}
 
Example 11
Source File: FilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Filter filter) {

	if (filter.getArg() instanceof EmptyResult) {
		log.debug(
				"Argument of filter expression does not yield results at the provided sources, replacing Filter node.");
		filter.replaceWith(filter.getArg());
		return;
	}

	/*
	 * TODO idea: if we have a FILTER such as ?s='a' OR ?s='b' OR ?s='c' handle this appropriately
	 */

	ValueExpr valueExpr = filter.getCondition();

	/*
	 * TODO transform condition into some normal form, e.g. CNF
	 */

	// determine conjunctive expressions
	List<ValueExpr> conjunctiveExpressions = new ArrayList<>();
	getConjunctiveExpressions(valueExpr, conjunctiveExpressions);

	FilterExprInsertVisitor filterExprVst = new FilterExprInsertVisitor();
	List<ValueExpr> remainingExpr = new ArrayList<>(conjunctiveExpressions.size());

	for (ValueExpr cond : conjunctiveExpressions) {

		/*
		 * Determine if this filter is applicable for optimization. Currently only leaf expressions are applicable,
		 * i.e. not combined expressions.
		 */
		if (isCompatibleExpr(cond)) {

			HashSet<String> exprVars = new VarFinder().findVars(cond);
			FilterExpr filterExpr = new FilterExpr(cond, exprVars);

			filterExprVst.initialize(filterExpr);
			filter.getArg().visit(filterExprVst);

			// if the filter expr. is handled in the stmt we do not have to keep it
			if (filterExprVst.canRemove()) {
				continue;
			}

			remainingExpr.add(filterExpr.getExpression());

		} else {
			remainingExpr.add(cond);
		}

	}

	if (remainingExpr.isEmpty()) {
		filter.replaceWith(filter.getArg()); // remove the filter
	} else if (remainingExpr.size() == 1) {
		filter.setCondition(remainingExpr.get(0)); // just apply the remaining condition
	} else {

		// construct conjunctive value expr
		And root = new And();
		root.setLeftArg(remainingExpr.get(0));
		And tmp = root;
		for (int i = 1; i < remainingExpr.size() - 1; i++) {
			And _a = new And();
			_a.setLeftArg(remainingExpr.get(i));
			tmp.setRightArg(_a);
			tmp = _a;
		}
		tmp.setRightArg(remainingExpr.get(remainingExpr.size() - 1));

		filter.setCondition(root);
	}

}
 
Example 12
Source File: SameTermFilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Filter filter) {
	super.meet(filter);

	if (filter.getCondition() instanceof SameTerm) {
		// SameTerm applies to the filter's argument
		SameTerm sameTerm = (SameTerm) filter.getCondition();
		TupleExpr filterArg = filter.getArg();

		ValueExpr leftArg = sameTerm.getLeftArg();
		ValueExpr rightArg = sameTerm.getRightArg();

		// Verify that vars are (potentially) bound by filterArg
		Set<String> bindingNames = filterArg.getBindingNames();
		if (isUnboundVar(leftArg, bindingNames) || isUnboundVar(rightArg, bindingNames)) {
			// One or both var(s) are unbound, this expression will never
			// return any results
			filter.replaceWith(new EmptySet());
			return;
		}

		Set<String> assuredBindingNames = filterArg.getAssuredBindingNames();
		if (isUnboundVar(leftArg, assuredBindingNames) || isUnboundVar(rightArg, assuredBindingNames)) {
			// One or both var(s) are potentially unbound, inlining could
			// invalidate the result e.g. in case of left joins
			return;
		}

		if (leftArg instanceof Var || rightArg instanceof Var) {
			if (filterArg instanceof ArbitraryLengthPath && leftArg instanceof Var && rightArg instanceof Var) {
				final ArbitraryLengthPath alp = (ArbitraryLengthPath) filterArg;
				final List<Var> sameTermArgs = Arrays.asList((Var) leftArg, (Var) rightArg);

				if (sameTermArgs.contains(alp.getSubjectVar()) && sameTermArgs.contains(alp.getObjectVar())) {
					// SameTerm provides a deferred mapping to allow arbitrary-length property path to produce
					// cyclic paths. See SES-1685.
					// we can not inline.
					return;
				}
			}

			BindingSetAssignmentCollector collector = new BindingSetAssignmentCollector();
			filterArg.visit(collector);

			for (BindingSetAssignment bsa : collector.getBindingSetAssignments()) {
				// check if the VALUES clause / bindingsetassignment contains
				// one of the arguments of the sameTerm.
				// if so, we can not inline.
				Set<String> names = bsa.getAssuredBindingNames();
				if (leftArg instanceof Var) {
					if (names.contains(((Var) leftArg).getName())) {
						return;
					}
				}
				if (rightArg instanceof Var) {
					if (names.contains(((Var) rightArg).getName())) {
						return;
					}
				}
			}
		}

		Value leftValue = getValue(leftArg);
		Value rightValue = getValue(rightArg);

		if (leftValue != null && rightValue != null) {
			// ConstantOptimizer should have taken care of this
		} else if (leftValue != null && rightArg instanceof Var) {
			bindVar((Var) rightArg, leftValue, filter);
		} else if (rightValue != null && leftArg instanceof Var) {
			bindVar((Var) leftArg, rightValue, filter);
		} else if (leftArg instanceof Var && rightArg instanceof Var) {
			// Two unbound variables, rename rightArg to leftArg
			renameVar((Var) rightArg, (Var) leftArg, filter);
		}
	}
}
 
Example 13
Source File: TopOfQueryFilterRelocator.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public void meet(Filter node) {
    filterCond.add(node.getCondition());
    node.replaceWith(node.getArg());
}