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

The following examples show how to use org.eclipse.rdf4j.query.algebra.Filter#getArg() . 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: ConjunctiveConstraintSplitter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(Filter filter) {
	super.meet(filter);

	List<ValueExpr> conjunctiveConstraints = new ArrayList<>(16);
	getConjunctiveConstraints(filter.getCondition(), conjunctiveConstraints);

	TupleExpr filterArg = filter.getArg();

	for (int i = conjunctiveConstraints.size() - 1; i >= 1; i--) {
		Filter newFilter = new Filter(filterArg, conjunctiveConstraints.get(i));
		filterArg = newFilter;
	}

	filter.setCondition(conjunctiveConstraints.get(0));
	filter.setArg(filterArg);
}
 
Example 2
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 3
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 4
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 5
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 6
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 7
Source File: SparqlTupleExprRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(final Filter theFilter) throws Exception {
	ctxOpen(theFilter);

	if (theFilter.getArg() != null) {
		theFilter.getArg().visit(this);
	}

	// try and reverse engineer the original scoping intent of the query
	final boolean aNeedsNewScope = theFilter.getParentNode() != null
			&& (theFilter.getParentNode() instanceof Join || theFilter.getParentNode() instanceof LeftJoin);

	String aFilter = renderValueExpr(theFilter.getCondition());
	if (theFilter.getCondition() instanceof ValueConstant || theFilter.getCondition() instanceof Var) {
		// means the filter is something like "filter (true)" or "filter (?v)"
		// so we'll need to wrap it in parens since they can't live
		// in the query w/o them, but we can't always wrap them in parens in
		// the normal renderer

		aFilter = "(" + aFilter + ")";
	}

	mJoinBuffer.append(indent());

	// if (aNeedsNewScope) {
	// mJoinBuffer.append("{ ");
	// }

	mJoinBuffer.append("filter ").append(aFilter).append(".");

	// if (aNeedsNewScope) {
	// mJoinBuffer.append("}.");
	// }

	mJoinBuffer.append("\n");

	ctxClose(theFilter);
}
 
Example 8
Source File: RdfCloudTripleStoreEvaluationStatistics.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public double getCardinality(final TupleExpr expr) {
    if (expr instanceof Filter) {
        final Filter f = (Filter) expr;
        // filters must make sets smaller
        return super.getCardinality(f.getArg()) / 10;
    }
    return super.getCardinality(expr);
}
 
Example 9
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 10
Source File: SparqlFluoQueryBuilder.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(final Filter node) {

    // Get or create a builder for this node populated with the known metadata.
    final String filterId = nodeIds.getOrMakeId(node);

    FilterMetadata.Builder filterBuilder = fluoQueryBuilder.getFilterBuilder(filterId).orNull();
    if(filterBuilder == null) {
        filterBuilder = FilterMetadata.builder(filterId);
        fluoQueryBuilder.addFilterMetadata(filterBuilder);
    }

    String filterString;
    try {
        filterString = FilterSerializer.serialize(node);
    } catch (final FilterParseException e) {
        throw new RuntimeException(e);
    }
    filterBuilder.setFilterSparql(filterString);

    final QueryModelNode child = node.getArg();
    if(child == null) {
        throw new IllegalArgumentException("Filter arg connot be null.");
    }

    final String childNodeId = nodeIds.getOrMakeId(child);
    filterBuilder.setChildNodeId(childNodeId);

    // Update the child node's metadata.
    final Set<String> childVars = getVars((TupleExpr)child);
    final VariableOrder childVarOrder = new VariableOrder(childVars);
    setChildMetadata(fluoQueryBuilder, childNodeId, childVarOrder, filterId);

    // Walk to the next node.
    super.meet(node);
}
 
Example 11
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(Filter node) {

          Set<QueryModelNode> eSet = getQNodes(node);
          Set<QueryModelNode> compSet = Sets.difference(eSet, sSet);

          // if index contains filter node and it hasn't already been moved,
          // move it down
          // query tree just above position of statement pattern nodes found
          // in both query tree
          // and index (assuming that SPBubbleDownVisitor has already been
          // called)
          if (sSet.contains(node.getCondition()) && !bubbledFilters.contains(node.getCondition())) {
              FilterBubbleDownVisitor fbdv = new FilterBubbleDownVisitor(node.clone(), compSet);
              node.visit(fbdv);
              bubbledFilters.add(node.getCondition());
              // checks if filter correctly placed, and if it has been,
              // removes old copy of filter
              if (fbdv.filterPlaced()) {

                  QueryModelNode pNode = node.getParentNode();
                  TupleExpr cNode = node.getArg();
                  pNode.replaceChildNode(node, cNode);


                  super.meetNode(pNode);
              }
              super.meet(node);

          } else {
              super.meet(node);
          }
      }
 
Example 12
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 13
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 14
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 15
Source File: SPARQLTupleExprRenderer.java    From semagrow with Apache License 2.0 4 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(final Filter theFilter)
        throws Exception
{
    ctxOpen(theFilter);

    if (theFilter.getArg() != null) {
        theFilter.getArg().visit(this);
    }

    // try and reverse engineer the original scoping intent of the query
    final boolean aNeedsNewScope = theFilter.getParentNode() != null
            && (theFilter.getParentNode() instanceof Join || theFilter.getParentNode() instanceof LeftJoin);

    String aFilter = renderValueExpr(theFilter.getCondition());
    if (theFilter.getCondition() instanceof ValueConstant || theFilter.getCondition() instanceof Var) {
        // means the filter is something like "filter (true)" or "filter (?v)"
        // so we'll need to wrap it in parens since they can't live
        // in the query w/o them, but we can't always wrap them in parens in
        // the normal renderer

        aFilter = "(" + aFilter + ")";
    }

    mJoinBuffer.append(indent());

    // if (aNeedsNewScope) {
    // mJoinBuffer.append("{ ");
    // }

    mJoinBuffer.append("filter ").append(aFilter).append(".");

    // if (aNeedsNewScope) {
    // mJoinBuffer.append("}.");
    // }

    mJoinBuffer.append("\n");

    ctxClose(theFilter);
}