org.eclipse.rdf4j.query.algebra.LeftJoin Java Examples

The following examples show how to use org.eclipse.rdf4j.query.algebra.LeftJoin. 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: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate {@link BinaryTupleOperator} query model nodes
 * @param parent
 * @param expr
 * @param bindings
 */
private void evaluateBinaryTupleOperator(BindingSetPipe parent, BinaryTupleOperator expr, BindingSet bindings) {
    if (expr instanceof Join) {
        evaluateJoin(parent, (Join) expr, bindings);
    } else if (expr instanceof LeftJoin) {
        evaluateLeftJoin(parent, (LeftJoin) expr, bindings);
    } else if (expr instanceof Union) {
        evaluateUnion(parent, (Union) expr, bindings);
    } else if (expr instanceof Intersection) {
        evaluateIntersection(parent, (Intersection) expr, bindings);
    } else if (expr instanceof Difference) {
        evaluateDifference(parent, (Difference) expr, bindings);
    } else if (expr == null) {
        parent.handleException(new IllegalArgumentException("expr must not be null"));
    } else {
        parent.handleException(new QueryEvaluationException("Unsupported binary tuple operator type: " + expr.getClass()));
    }
}
 
Example #2
Source File: GeoEnabledFilterFunctionOptimizer.java    From rya with Apache License 2.0 6 votes vote down vote up
private void buildQuery(final TupleExpr tupleExpr, final StatementPattern matchStatement) {
    //If our IndexerExpr (to be) is the rhs-child of LeftJoin, we can safely make that a Join:
    //  the IndexerExpr will (currently) not return results that can deliver unbound variables.
    //This optimization should probably be generalized into a LeftJoin -> Join optimizer under certain conditions. Until that
    //  has been done, this code path at least takes care of queries generated by OpenSahara SparqTool that filter on OPTIONAL
    //  projections. E.g. summary~'full text search' (summary is optional). See #379
    if (matchStatement.getParentNode() instanceof LeftJoin) {
        final LeftJoin leftJoin = (LeftJoin)matchStatement.getParentNode();
        if (leftJoin.getRightArg() == matchStatement && leftJoin.getCondition() == null) {
            matchStatement.getParentNode().replaceWith(new Join(leftJoin.getLeftArg(), leftJoin.getRightArg()));
        }
    }
    final FilterFunction fVisitor = new FilterFunction(matchStatement.getObjectVar().getName());
    tupleExpr.visit(fVisitor);
    final List<IndexingExpr> results = Lists.newArrayList();
    for(int i = 0; i < fVisitor.func.size(); i++){
        results.add(new IndexingExpr(fVisitor.func.get(i), matchStatement, fVisitor.args.get(i)));
    }
    removeMatchedPattern(tupleExpr, matchStatement, new IndexerExprReplacer(results));
}
 
Example #3
Source File: FederationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(LeftJoin leftJoin,
		final BindingSet bindings) throws QueryEvaluationException {
	// Check whether optional join is "well designed" as defined in section
	// 4.2 of "Semantics and Complexity of SPARQL", 2006, Jorge Pérez et al.
	Set<String> boundVars = bindings.getBindingNames();
	Set<String> leftVars = leftJoin.getLeftArg().getBindingNames();
	Set<String> optionalVars = leftJoin.getRightArg().getBindingNames();

	final Set<String> problemVars = new HashSet<>(boundVars);
	problemVars.retainAll(optionalVars);
	problemVars.removeAll(leftVars);

	CloseableIteration<BindingSet, QueryEvaluationException> result;
	if (problemVars.isEmpty()) {
		// left join is "well designed"
		result = new ParallelLeftJoinCursor(this, leftJoin, bindings);
		executor.execute((Runnable) result);
	} else {
		result = new BadlyDesignedLeftJoinIterator(this, leftJoin, bindings, problemVars);
	}
	return result;
}
 
Example #4
Source File: FlattenedOptionalTest.java    From rya with Apache License 2.0 6 votes vote down vote up
private List<TupleExpr> getJoinArgs(TupleExpr tupleExpr,
		List<TupleExpr> joinArgs) {
	if (tupleExpr instanceof Projection) {
		Projection projection = (Projection) tupleExpr;
		getJoinArgs(projection.getArg(), joinArgs);
	} else if (tupleExpr instanceof Join) {
		Join join = (Join) tupleExpr;
		getJoinArgs(join.getLeftArg(), joinArgs);
		getJoinArgs(join.getRightArg(), joinArgs);
	} else if (tupleExpr instanceof LeftJoin) {
		LeftJoin lj = (LeftJoin) tupleExpr;
		joinArgs.add(new FlattenedOptional(lj));
		getJoinArgs(lj.getLeftArg(), joinArgs);
	} else if (tupleExpr instanceof Filter) {
		getJoinArgs(((Filter) tupleExpr).getArg(), joinArgs);
	} else {
		joinArgs.add(tupleExpr);
	}
	return joinArgs;
}
 
Example #5
Source File: QueryGraphPlanGenerator.java    From semagrow with Apache License 2.0 6 votes vote down vote up
public Collection<Plan> combineWith(PlanCollection p1, PlanCollection p2, LeftJoinPredicate pred)
{
    Collection<Plan> plans =  new LinkedList<Plan>();

    for (Plan pp1 : p1) {
        for (Plan pp2 : p2) {

            if (pp1.getProperties().getSite().isRemote() &&
                    pp2.getProperties().getSite().isRemote() &&
                    pp1.getProperties().getSite().equals(pp2.getProperties().getSite())) {

                Plan ppp = create(new LeftJoin(pp1, pp2));
                logger.debug("Plan added {}", ppp);
                plans.add(ppp);
            }
            plans.add(create(new LeftJoin(enforce(pp1, LocalSite.getInstance()), enforce(pp2, LocalSite.getInstance()))));
        }
    }
    return plans;
}
 
Example #6
Source File: FederationJoinOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void addOwners(LeftJoin node, RepositoryConnection leftOwner, RepositoryConnection rightOwner,
		boolean local) {
	if (leftOwner == null && rightOwner == null) {
		if (local) {
			TupleExpr union = null;
			for (RepositoryConnection member : members) {
				OwnedTupleExpr arg = new OwnedTupleExpr(member, // NOPMD
						node.clone());
				union = union == null ? arg : new Union(union, arg); // NOPMD
			}
			node.replaceWith(union);
		}
	} else if (leftOwner == rightOwner) { // NOPMD
		node.replaceWith(new OwnedTupleExpr(leftOwner, node.clone()));
	} else {
		if (local) {
			addDistinctOwnersLocal(node, leftOwner, rightOwner);
		} else {
			addDistinctOwnersNonLocal(node, leftOwner, rightOwner);
		}
	}
}
 
Example #7
Source File: FilterFunctionOptimizer.java    From rya with Apache License 2.0 6 votes vote down vote up
private void buildQuery(final TupleExpr tupleExpr, final StatementPattern matchStatement) {
    //If our IndexerExpr (to be) is the rhs-child of LeftJoin, we can safely make that a Join:
    //  the IndexerExpr will (currently) not return results that can deliver unbound variables.
    //This optimization should probably be generalized into a LeftJoin -> Join optimizer under certain conditions. Until that
    //  has been done, this code path at least takes care of queries generated by OpenSahara SparqTool that filter on OPTIONAL
    //  projections. E.g. summary~'full text search' (summary is optional). See #379
    if (matchStatement.getParentNode() instanceof LeftJoin) {
        final LeftJoin leftJoin = (LeftJoin)matchStatement.getParentNode();
        if (leftJoin.getRightArg() == matchStatement && leftJoin.getCondition() == null) {
            matchStatement.getParentNode().replaceWith(new Join(leftJoin.getLeftArg(), leftJoin.getRightArg()));
        }
    }
    final FilterFunction fVisitor = new FilterFunction(matchStatement.getObjectVar().getName());
    tupleExpr.visit(fVisitor);
    final List<IndexingExpr> results = Lists.newArrayList();
    for(int i = 0; i < fVisitor.func.size(); i++){
        results.add(new IndexingExpr(fVisitor.func.get(i), matchStatement, Arrays.stream(fVisitor.args.get(i)).toArray()));
    }
    removeMatchedPattern(tupleExpr, matchStatement, new IndexerExprReplacer(results));
}
 
Example #8
Source File: PrepareOwnedTupleExpr.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(LeftJoin node) throws RepositoryException {
	if (node.getCondition() == null) {
		Map<String, String> vars = new HashMap<>();
		StringBuilder builder = new StringBuilder();
		node.getLeftArg().visit(this);
		if (patternNode != null) {
			builder.append(pattern);
			vars.putAll(variables);
			node.getRightArg().visit(this);
			if (patternNode != null) {
				builder.append("OPTIONAL {").append(pattern).append("}\n");
				vars.putAll(variables);
				this.variables = vars;
				this.pattern = builder.toString();
				this.patternNode = node;
			}
		}
	} else {
		super.meet(node);
	}
}
 
Example #9
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(BinaryTupleOperator expr,
		BindingSet bindings) throws QueryEvaluationException {
	if (expr instanceof Join) {
		return evaluate((Join) expr, bindings);
	} else if (expr instanceof LeftJoin) {
		return evaluate((LeftJoin) expr, bindings);
	} else if (expr instanceof Union) {
		return evaluate((Union) expr, bindings);
	} else if (expr instanceof Intersection) {
		return evaluate((Intersection) expr, bindings);
	} else if (expr instanceof Difference) {
		return evaluate((Difference) expr, bindings);
	} else if (expr == null) {
		throw new IllegalArgumentException("expr must not be null");
	} else {
		throw new QueryEvaluationException("Unsupported binary tuple operator type: " + expr.getClass());
	}
}
 
Example #10
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(LeftJoin leftJoin,
		final BindingSet bindings) throws QueryEvaluationException {
	if (TupleExprs.containsSubquery(leftJoin.getRightArg())) {
		return new HashJoinIteration(this, leftJoin, bindings);
	}

	// Check whether optional join is "well designed" as defined in section
	// 4.2 of "Semantics and Complexity of SPARQL", 2006, Jorge Pérez et al.
	VarNameCollector optionalVarCollector = new VarNameCollector();
	leftJoin.getRightArg().visit(optionalVarCollector);
	if (leftJoin.hasCondition()) {
		leftJoin.getCondition().visit(optionalVarCollector);
	}

	Set<String> problemVars = optionalVarCollector.getVarNames();
	problemVars.removeAll(leftJoin.getLeftArg().getBindingNames());
	problemVars.retainAll(bindings.getBindingNames());

	if (problemVars.isEmpty()) {
		// left join is "well designed"
		return new LeftJoinIterator(this, leftJoin, bindings);
	} else {
		return new BadlyDesignedLeftJoinIterator(this, leftJoin, bindings, problemVars);
	}
}
 
Example #11
Source File: FlattenedOptional.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * This method is used to retrieve a set view of all descendants of the
 * rightArg of the LeftJoin (the optional part)
 *
 * @param tupleExpr
 *            - tupleExpr whose args are being retrieved
 * @param joinArgs
 *            - set view of all non-join args that are descendants of
 *            tupleExpr
 * @return joinArgs
 */
private Set<TupleExpr> getJoinArgs(TupleExpr tupleExpr, Set<TupleExpr> joinArgs) {
    if (tupleExpr instanceof Join) {
        if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern)
                && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) {
            Join join = (Join) tupleExpr;
            getJoinArgs(join.getLeftArg(), joinArgs);
            getJoinArgs(join.getRightArg(), joinArgs);
        }
    } else if (tupleExpr instanceof LeftJoin) { // TODO probably not
                                                // necessary if not
                                                // including leftarg
        LeftJoin lj = (LeftJoin) tupleExpr;
        joinArgs.add(new FlattenedOptional(lj));
        getJoinArgs(lj.getLeftArg(), joinArgs);
    } else if (tupleExpr instanceof Filter) {
        getJoinArgs(((Filter) tupleExpr).getArg(), joinArgs);
    } else {
        joinArgs.add(tupleExpr);
    }

    return joinArgs;
}
 
Example #12
Source File: OptionalJoinSegment.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param tupleExpr
 *            - the query object that will be traversed by this method
 * @param joinArgs
 *            - all nodes connected by Joins, LeftJoins, and Filters
 * @return - List containing all nodes connected by Joins, LeftJoins, and
 *         Filters. This List contains the {@link ValueExpr} in place of the
 *         Filter and a {@link FlattenedOptional} in place of the LeftJoin
 *         for ease of comparison with PCJ nodes.
 */
private List<QueryModelNode> getJoinArgs(TupleExpr tupleExpr, List<QueryModelNode> joinArgs) {

    if (tupleExpr instanceof Join) {
        if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern)
                && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) {
            Join join = (Join) tupleExpr;
            getJoinArgs(join.getRightArg(), joinArgs);
            getJoinArgs(join.getLeftArg(), joinArgs);
        }
    } else if (tupleExpr instanceof LeftJoin) {
        LeftJoin lj = (LeftJoin) tupleExpr;
        joinArgs.add(new FlattenedOptional(lj));
        getJoinArgs(lj.getLeftArg(), joinArgs);
    } else if (tupleExpr instanceof Filter) {
        Filter filter = (Filter) tupleExpr;
        joinArgs.add(filter.getCondition());
        conditionMap.put(filter.getCondition(), filter);
        getJoinArgs(filter.getArg(), joinArgs);
    } else {
        joinArgs.add(tupleExpr);
    }
    return joinArgs;
}
 
Example #13
Source File: QuerySegmentFactory.java    From rya with Apache License 2.0 6 votes vote down vote up
public QuerySegment<T> getQuerySegment(final QueryModelNode node) {
    Preconditions.checkNotNull(node);
    if(node instanceof Filter) {
        final Filter filter = (Filter)node;
        if(MatcherUtilities.segmentContainsLeftJoins(filter)) {
            return new OptionalJoinSegment<T>(filter);
        } else {
            return new JoinSegment<T>(filter);
        }
    } else if(node instanceof Join) {
        final Join join = (Join) node;
        if(MatcherUtilities.segmentContainsLeftJoins(join)) {
            return new OptionalJoinSegment<T>(join);
        } else {
            return new JoinSegment<T>(join);
        }
    } else if (node instanceof LeftJoin) {
        return new OptionalJoinSegment<T>((LeftJoin) node);
    } else {
        throw new IllegalArgumentException("Node must be a Join, Filter, or LeftJoin");
    }

}
 
Example #14
Source File: MatcherUtilities.java    From rya with Apache License 2.0 5 votes vote down vote up
public static boolean segmentContainsLeftJoins(final TupleExpr tupleExpr) {
    if (tupleExpr instanceof Projection) {
        return segmentContainsLeftJoins(((Projection) tupleExpr).getArg());
    } else if (tupleExpr instanceof Join) {
        final Join join = (Join) tupleExpr;
        return segmentContainsLeftJoins(join.getRightArg())
                || segmentContainsLeftJoins(join.getLeftArg());
    } else if (tupleExpr instanceof LeftJoin) {
        return true;
    } else if (tupleExpr instanceof Filter) {
        return segmentContainsLeftJoins(((Filter) tupleExpr).getArg());
    } else {
        return false;
    }
}
 
Example #15
Source File: PCJNodeConsolidatorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testAlreadyInOrder() throws Exception {

	String query1 = ""//
			+ "SELECT ?a ?b ?c ?d" //
			+ "{" //
			+ "  ?a <uri:p5> <uri:const3>" //
			+ "  OPTIONAL{?a <uri:p3> ?c} . " //
			+ "  ?a <uri:p4> ?b . "//
			+ "  OPTIONAL{<uri:const2> <uri:p4> ?d } . "//
			+ "  ?c <uri:p1> ?d "
			+ "  OPTIONAL{<uri:const1> <uri:p2> ?b} . "//
			+ "}";//

	String query2 = ""//
			+ "SELECT ?a ?b ?c ?d" //
			+ "{" //
			+ "  ?a <uri:p4> ?b . "//
			+ "  OPTIONAL{<uri:const2> <uri:p4> ?d } . "//
			+ "  ?c <uri:p1> ?d "
			+ "}";//

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery pq1 = parser.parseQuery(query1, null);
	ParsedQuery pq2 = parser.parseQuery(query2, null);
	TupleExpr te1 = pq1.getTupleExpr();
	TupleExpr te2 = pq2.getTupleExpr();
	LeftJoin join1 = (LeftJoin) ((Projection) te1).getArg();
	Join join2 = (Join) ((Projection) te2).getArg();

	QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join1);
       QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(join2);

	QueryNodeConsolidator consolidator = new QueryNodeConsolidator(seg1.getOrderedNodes(), seg2.getOrderedNodes());
	List<QueryModelNode> queryNodes = new ArrayList<>(seg1.getOrderedNodes());

	Assert.assertTrue(consolidator.consolidateNodes());
	Assert.assertEquals(consolidator.getQueryNodes(), queryNodes);

}
 
Example #16
Source File: QueryNodesToTupleExpr.java    From rya with Apache License 2.0 5 votes vote down vote up
private static TupleExpr getJoin(TupleExpr oldJoin, TupleExpr newArg) {
    if (newArg instanceof FlattenedOptional) {
        return new LeftJoin(oldJoin, ((FlattenedOptional) newArg).getRightArg());
    } else {
        return new Join(oldJoin, newArg);
    }
}
 
Example #17
Source File: PCJNodeConsolidatorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testExactMatchReOrdered() throws Exception {

	String query1 = ""//
			+ "SELECT ?a ?b ?c ?d ?e ?f ?g ?h" //
			+ "{" //
			+ "  ?a <uri:p0> ?b ." //
			+ "  OPTIONAL{?b <uri:p2> ?c. ?c <uri:p1> ?d} . " //
			+ "  OPTIONAL{?b <uri:p3> ?e. ?e <uri:p1> ?f} . "//
			+ "  OPTIONAL{?b <uri:p4> ?g. ?g <uri:p1> ?h} . "//
			+ "}";//

	String query2 = ""//
			+ "SELECT ?a ?b ?c ?d ?e ?f ?g ?h" //
			+ "{" //
			+ "  ?a <uri:p0> ?b ." //
			+ "  OPTIONAL{?b <uri:p2> ?c. ?c <uri:p1> ?d} . " //
			+ "  OPTIONAL{?b <uri:p4> ?g. ?g <uri:p1> ?h} . "//
			+ "  OPTIONAL{?b <uri:p3> ?e. ?e <uri:p1> ?f} . "//
			+ "}";//

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery pq1 = parser.parseQuery(query1, null);
	ParsedQuery pq2 = parser.parseQuery(query2, null);
	TupleExpr te1 = pq1.getTupleExpr();
	TupleExpr te2 = pq2.getTupleExpr();
	LeftJoin join1 = (LeftJoin) ((Projection) te1).getArg();
	LeftJoin join2 = (LeftJoin) ((Projection) te2).getArg();

	QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join1);
    QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(join2);

	QueryNodeConsolidator consolidator = new QueryNodeConsolidator(seg1.getOrderedNodes(), seg2.getOrderedNodes());
	List<QueryModelNode> queryNodes = new ArrayList<>(seg2.getOrderedNodes());

	Assert.assertTrue(consolidator.consolidateNodes());
	Assert.assertEquals(consolidator.getQueryNodes(), queryNodes);
}
 
Example #18
Source File: PCJNodeConsolidatorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidOrder() throws Exception {

	String query1 = ""//
			+ "SELECT ?a ?b ?c ?d" //
			+ "{" //
			+ "  ?a <uri:p5> <uri:const3>" //
			+ "  OPTIONAL{<uri:const2> <uri:p4> ?d } . "//
			+ "  ?a <uri:p4> ?b . "//
			+ "  OPTIONAL{?a <uri:p3> ?c} . " //
			+ "  OPTIONAL{<uri:const1> <uri:p2> ?b} . "//
			+ "  ?c <uri:p1> ?d "
			+ "}";//

	String query2 = ""//
			+ "SELECT ?a ?b ?c ?d" //
			+ "{" //
			+ "  ?a <uri:p4> ?b . "//
			+ "  ?c <uri:p1> ?d "
			+ "  OPTIONAL{<uri:const2> <uri:p4> ?d } . "//
			+ "}";//

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery pq1 = parser.parseQuery(query1, null);
	ParsedQuery pq2 = parser.parseQuery(query2, null);
	TupleExpr te1 = pq1.getTupleExpr();
	TupleExpr te2 = pq2.getTupleExpr();
	Join join1 = (Join) ((Projection) te1).getArg();
	LeftJoin join2 = (LeftJoin) ((Projection) te2).getArg();

	QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join1);
    QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(join2);

	QueryNodeConsolidator consolidator = new QueryNodeConsolidator(seg1.getOrderedNodes(), seg2.getOrderedNodes());

	Assert.assertTrue(!consolidator.consolidateNodes());
}
 
Example #19
Source File: SPARQLTupleExprRenderer.java    From semagrow with Apache License 2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(LeftJoin theJoin)
        throws Exception
{
    ctxOpen(theJoin);

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

    if (aNeedsNewScope) {
        mJoinBuffer.append("{\n");
    }

    theJoin.getLeftArg().visit(this);

    mJoinBuffer.append(indent()).append("OPTIONAL {\n");

    mIndent += 2;
    theJoin.getRightArg().visit(this);

    if (theJoin.getCondition() != null) {
        mJoinBuffer.append(indent()).append("filter").append(renderValueExpr(theJoin.getCondition())).append(
                "\n");
    }

    mIndent -= 2;

    mJoinBuffer.append(indent()).append("}.\n");

    if (aNeedsNewScope) {
        mJoinBuffer.append("}.\n");
    }

    ctxClose(theJoin);
}
 
Example #20
Source File: FilterFunctionOptimizer.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(final FunctionCall call) {
    final IRI fnUri = VF.createIRI(call.getURI());
    final Var resultVar = IndexingFunctionRegistry.getResultVarFromFunctionCall(fnUri, call.getArgs());
    if (resultVar != null && resultVar.getName().equals(matchVar)) {
        addFilter(VF.createIRI(call.getURI()), extractArguments(matchVar, call));
        if (call.getParentNode() instanceof Filter || call.getParentNode() instanceof And || call.getParentNode() instanceof LeftJoin) {
            call.replaceWith(new ValueConstant(VF.createLiteral(true)));
        } else {
            throw new IllegalArgumentException("Query error: Found " + call + " as part of an expression that is too complex");
        }
    }
}
 
Example #21
Source File: FlattenedOptional.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * This method counts the number of times each variable appears in the
 * leftArg of the LeftJoin defining this FlattenedOptional. This information
 * is used to whether nodes can be moved out of the leftarg above the
 * LeftJoin in the query.
 *
 * @param tupleExpr
 */
private void getVarCounts(TupleExpr tupleExpr) {
    if (tupleExpr instanceof Join) {
        Join join = (Join) tupleExpr;
        getVarCounts(join.getLeftArg());
        getVarCounts(join.getRightArg());
    } else if (tupleExpr instanceof LeftJoin) {
        LeftJoin lj = (LeftJoin) tupleExpr;
        getVarCounts(lj.getLeftArg());
    } else if (tupleExpr instanceof Filter) {
        getVarCounts(((Filter) tupleExpr).getArg());
    } else {
        incrementVarCounts(tupleExpr.getBindingNames());
    }
}
 
Example #22
Source File: ContextCollector.java    From semagrow with Apache License 2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(LeftJoin theJoin)
        throws Exception
{
    binaryOpMeet(theJoin, theJoin.getLeftArg(), theJoin.getRightArg());
}
 
Example #23
Source File: PCJOptimizerUtilities.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(final LeftJoin leftJoin) {
	if (leftJoin.getLeftArg().getBindingNames().containsAll(filterVars)) {
		leftJoin.getLeftArg().visit(this);
	} else {
		relocate(filter, leftJoin.getLeftArg());
	}
}
 
Example #24
Source File: PCJOptimizerUtilities.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meetNode(final QueryModelNode node) {
	if (!(node instanceof Join || node instanceof LeftJoin
			|| node instanceof StatementPattern || node instanceof Var
			|| node instanceof Union || node instanceof Filter || node instanceof Projection)) {
		isValid = false;
		return;
	}
	super.meetNode(node);
}
 
Example #25
Source File: SparqlFluoQueryBuilder.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(final LeftJoin node) {
    // Extract the metadata that will be stored for the node.
    final String leftJoinNodeId = nodeIds.getOrMakeId(node);
    final QueryModelNode left = node.getLeftArg();
    final QueryModelNode right = node.getRightArg();

    // Update the metadata for the JoinMetadata.Builder.
    makeJoinMetadata(leftJoinNodeId, JoinType.LEFT_OUTER_JOIN, left, right);

    // Walk to the next node.
    super.meet(node);
}
 
Example #26
Source File: SparqlFluoQueryBuilder.java    From rya with Apache License 2.0 5 votes vote down vote up
private String makeId(final QueryModelNode node) {
    checkNotNull(node);

    // Create the prefix of the id. This makes it a little bit more human readable.
    String prefix;
    if (node instanceof StatementPattern) {
        prefix = SP_PREFIX;
    } else if (node instanceof Filter) {
        prefix = FILTER_PREFIX;
    } else if (node instanceof Join || node instanceof LeftJoin) {
        prefix = JOIN_PREFIX;
    } else if (node instanceof Projection) {
        prefix = PROJECTION_PREFIX;
    } else if(node instanceof Extension) {
        prefix = AGGREGATION_PREFIX;
    }  else if (node instanceof Reduced) {
        prefix = CONSTRUCT_PREFIX;
    } else if(node instanceof PeriodicQueryNode) {
        prefix = PERIODIC_QUERY_PREFIX;
    } else {
        throw new IllegalArgumentException("Node must be of type {StatementPattern, Join, Filter, Extension, Projection} but was " + node.getClass());
    }

    final String unique = UUID.randomUUID().toString().replaceAll("-", "");
    // Put them together to create the Node ID.
    return prefix + "_" + unique;
}
 
Example #27
Source File: ControlledWorkerLeftJoin.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ControlledWorkerLeftJoin(ControlledWorkerScheduler<BindingSet> scheduler, FederationEvalStrategy strategy,
		CloseableIteration<BindingSet, QueryEvaluationException> leftIter,
		LeftJoin join, BindingSet bindings, QueryInfo queryInfo)
		throws QueryEvaluationException {
	super(strategy, leftIter, join.getRightArg(), bindings, queryInfo);
	this.scheduler = scheduler;
	this.join = join;
}
 
Example #28
Source File: QueryJoinOptimizer.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(LeftJoin leftJoin) {
    leftJoin.getLeftArg().visit(this);

    Set<String> origBoundVars = boundVars;
    try {
        boundVars = new HashSet<String>(boundVars);
        boundVars.addAll(leftJoin.getLeftArg().getBindingNames());

        leftJoin.getRightArg().visit(this);
    } finally {
        boundVars = origBoundVars;
    }
}
 
Example #29
Source File: SimilarVarJoinOptimizer.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(LeftJoin leftJoin) {
    leftJoin.getLeftArg().visit(this);

    Set<String> origBoundVars = boundVars;
    try {
        boundVars = new HashSet<String>(boundVars);
        boundVars.addAll(leftJoin.getLeftArg().getBindingNames());

        leftJoin.getRightArg().visit(this);
    } finally {
        boundVars = origBoundVars;
    }
}
 
Example #30
Source File: FlattenedOptional.java    From rya with Apache License 2.0 5 votes vote down vote up
public FlattenedOptional(LeftJoin node) {
    rightArgs = getJoinArgs(node.getRightArg(), new HashSet<TupleExpr>());
    boundVars = setWithOutConstants(
            Sets.intersection(node.getLeftArg().getAssuredBindingNames(), node.getRightArg().getBindingNames()));
    unboundVars = setWithOutConstants(Sets.difference(node.getRightArg().getBindingNames(), boundVars));
    condition = node.getCondition();
    rightArg = node.getRightArg();
    getVarCounts(node);
    assuredBindingNames = new HashSet<>(leftArgVarCounts.keySet());
    bindingNames = new HashSet<>(Sets.union(assuredBindingNames, unboundVars));
}