Java Code Examples for org.eclipse.rdf4j.query.algebra.TupleExpr#clone()

The following examples show how to use org.eclipse.rdf4j.query.algebra.TupleExpr#clone() . 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: SemagrowSailQuery.java    From semagrow with Apache License 2.0 5 votes vote down vote up
public TupleExpr getDecomposedQuery() {

        SemagrowSailConnection conn = (SemagrowSailConnection) getConnection().getSailConnection();
        TupleExpr initialExpr = getParsedQuery().getTupleExpr();
        TupleExpr expr = initialExpr.clone();
        Dataset dataset = getDataset();

        if (dataset == null) {
            // No external dataset specified, use query's own dataset (if any)
            dataset = getParsedQuery().getDataset();
        }
        return conn.decompose(expr, dataset, getBindings(), getIncludedSources(), getExcludedSources());
    }
 
Example 3
Source File: AbstractFederationConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private TupleExpr optimize(TupleExpr parsed, Dataset dataset, BindingSet bindings, boolean includeInferred,
		EvaluationStrategy strategy) throws SailException {
	LOGGER.trace("Incoming query model:\n{}", parsed);

	// Clone the tuple expression to allow for more aggressive optimisations
	TupleExpr query = new QueryRoot(parsed.clone());

	new BindingAssigner().optimize(query, dataset, bindings);
	new ConstantOptimizer(strategy).optimize(query, dataset, bindings);
	new CompareOptimizer().optimize(query, dataset, bindings);
	new ConjunctiveConstraintSplitter().optimize(query, dataset, bindings);
	new DisjunctiveConstraintOptimizer().optimize(query, dataset, bindings);
	new SameTermFilterOptimizer().optimize(query, dataset, bindings);
	new QueryModelPruner().optimize(query, dataset, bindings);

	new QueryMultiJoinOptimizer().optimize(query, dataset, bindings);
	// new FilterOptimizer().optimize(query, dataset, bindings);

	// prepare bloom filters
	RepositoryBloomFilter defaultBloomFilter = new AccurateRepositoryBloomFilter(includeInferred);
	Map<Repository, RepositoryBloomFilter> bloomFilters = federation.getBloomFilters();
	java.util.function.Function<Repository, RepositoryBloomFilter> bloomFilterFunction = c -> bloomFilters
			.getOrDefault(c, defaultBloomFilter);

	new EmptyPatternOptimizer(members, bloomFilterFunction).optimize(query, dataset, bindings);
	boolean distinct = federation.isDistinct();
	PrefixHashSet local = federation.getLocalPropertySpace();
	new FederationJoinOptimizer(members, distinct, local, bloomFilterFunction).optimize(query, dataset, bindings);
	new OwnedTupleExprPruner().optimize(query, dataset, bindings);
	new QueryModelPruner().optimize(query, dataset, bindings);
	new QueryMultiJoinOptimizer().optimize(query, dataset, bindings);

	new PrepareOwnedTupleExpr().optimize(query, dataset, bindings);

	LOGGER.trace("Optimized query model:\n{}", query);
	return query;
}
 
Example 4
Source File: PCJOptimizerTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testJoinMatcherRejectsLeftJoinPcj() throws Exception {

    final String query1 = ""//
            + "SELECT ?e ?c ?l" //
            + "{" //
            + "  ?e a ?c . "//
            + "  ?e <uri:talksTo> ?l . "//
            + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
            + "}";//

    final String query2 = ""//
            + "SELECT ?a ?b ?m" //
            + "{" //
            + "  ?a a ?b . "//
            + "  ?a <uri:talksTo> ?m . "//
            + "  OPTIONAL {?a <http://www.w3.org/2000/01/rdf-schema#label> ?m}  . "//
            + "}";//

    final SPARQLParser parser = new SPARQLParser();
    final ParsedQuery pq1 = parser.parseQuery(query1, null);
    final ParsedQuery pq2 = parser.parseQuery(query2, null);
    final TupleExpr te1 = pq1.getTupleExpr();
    final TupleExpr te2 = pq2.getTupleExpr();
    final TupleExpr expected = te1.clone();

    final SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection) te2);
    final List<ExternalTupleSet> externalList = new ArrayList<>();
    externalList.add(pcj);

    provider.setIndices(externalList);
    final PCJOptimizer optimizer = new PCJOptimizer(externalList, false, provider);
    optimizer.optimize(te1, null, null);
    Assert.assertEquals(expected, te1);

}
 
Example 5
Source File: HalyardFilterOptimizerTest.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Test
public void testKeepBindWithFilter() {
    TupleExpr expr = new SPARQLParser().parseQuery("SELECT ?x\nWHERE {BIND (\"x\" AS ?x) FILTER (?x = \"x\")}", "http://baseuri/").getTupleExpr();
    TupleExpr clone = expr.clone();
    new HalyardFilterOptimizer().optimize(clone, null, null);
    assertEquals(expr, clone);
}
 
Example 6
Source File: TupleReArranger.java    From rya with Apache License 2.0 5 votes vote down vote up
private static List<TupleExpr> getPlans(List<Map<Join, List<TupleExpr>>> reOrderings, TupleExpr te) {

        List<TupleExpr> queryPlans = Lists.newArrayList();
        PermInserter pm = new PermInserter();

        for (Map<Join, List<TupleExpr>> order : reOrderings) {
            TupleExpr clone = te.clone();
            pm.setReOrderMap(order);
            clone.visit(pm);
            queryPlans.add(clone);
        }

        return queryPlans;
    }
 
Example 7
Source File: PCJOptimizerTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testSegmentWithUnion() throws Exception {

    final String query1 = ""//
            + "SELECT ?e ?c ?l" //
            + "{" //
            + " {?e <uri:p1> <uri:o1>. } UNION { ?e a ?c. OPTIONAL {?e <uri:talksTo> ?l}. ?e <uri:p5> <uri:o4>. ?e <uri:p4> <uri:o3> }  . "//
            + "  ?e <uri:p2> ?c . "//
            + "  ?e <uri:p3> <uri:o2>  . "//
            + "}";//

    final String query2 = ""//
            + "SELECT ?a ?b ?m" //
            + "{" //
            + " ?a <uri:p5> <uri:o4> ." //
            + " ?a <uri:p4> <uri:o3> ." //
            + "  OPTIONAL {?a <uri:talksTo> ?m} . "//
            + "  ?a a ?b . "//
            + "}";//

    final String query3 = ""//
            + "SELECT ?h ?i" //
            + "{" //
            + "  ?h <uri:p2> ?i . "//
            + "  ?h <uri:p3> <uri:o2>  . "//
            + "}";//

    final SPARQLParser parser = new SPARQLParser();
    final ParsedQuery pq1 = parser.parseQuery(query1, null);
    final ParsedQuery pq2 = parser.parseQuery(query2, null);
    final ParsedQuery pq3 = parser.parseQuery(query3, null);
    final TupleExpr te1 = pq1.getTupleExpr();
    final TupleExpr te2 = pq2.getTupleExpr();
    final TupleExpr te3 = pq3.getTupleExpr();

    final TupleExpr unOpt = te1.clone();
    final List<QueryModelNode> remainingNodes = getNodes(te1);
    final Set<QueryModelNode> unMatchedNodes = new HashSet<>();
    unMatchedNodes.add(remainingNodes.get(0));

    final SimpleExternalTupleSet pcj1 = new SimpleExternalTupleSet((Projection) te2);
    final SimpleExternalTupleSet pcj2 = new SimpleExternalTupleSet((Projection) te3);
    final List<ExternalTupleSet> externalList = new ArrayList<>();
    externalList.add(pcj1);
    externalList.add(pcj2);

    provider.setIndices(externalList);
    final PCJOptimizer optimizer = new PCJOptimizer(externalList, false, provider);
    optimizer.optimize(te1, null, null);

    Assert.assertEquals(true, validatePcj(te1, unOpt, externalList, unMatchedNodes));



}
 
Example 8
Source File: PCJOptimizerTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testSegmentWithLeftJoinsAndFilters() throws Exception {

    final String query1 = ""//
            + "SELECT ?e ?c ?l" //
            + "{" //
            + " Filter(?e = <uri:s1>) " //
            + " Filter(?c = <uri:s2>) " //
            + " ?e <uri:p1> <uri:o1>. " + " OPTIONAL {?e <uri:p2> ?l}. " + " ?c <uri:p3> <uri:o3>  . "//
            + "  ?c <uri:p4> ?e  . "//
            + " OPTIONAL {?e <uri:p2> ?c } . "//
            + "}";//

    final String query2 = ""//
            + "SELECT ?e ?c ?l" //
            + "{" //
            + " Filter(?c = <uri:s2>) " //
            + " ?e <uri:p1> <uri:o1>. " + " OPTIONAL {?e <uri:p2> ?l}. " + " ?c <uri:p3> <uri:o3>  . "//
            + "}";//

    final String query3 = ""//
            + "SELECT ?e ?c" //
            + "{" //
            + " Filter(?e = <uri:s1>) " //
            + "  ?c <uri:p4> ?e  . "//
            + " OPTIONAL {?e <uri:p2> ?c } . "//
            + "}";//

    final SPARQLParser parser = new SPARQLParser();
    final ParsedQuery pq1 = parser.parseQuery(query1, null);
    final ParsedQuery pq2 = parser.parseQuery(query2, null);
    final ParsedQuery pq3 = parser.parseQuery(query3, null);
    final TupleExpr te1 = pq1.getTupleExpr();
    final TupleExpr te2 = pq2.getTupleExpr();
    final TupleExpr te3 = pq3.getTupleExpr();

    final TupleExpr unOpt = te1.clone();

    final SimpleExternalTupleSet pcj1 = new SimpleExternalTupleSet((Projection) te2);
    final SimpleExternalTupleSet pcj2 = new SimpleExternalTupleSet((Projection) te3);
    final List<ExternalTupleSet> externalList = new ArrayList<>();
    externalList.add(pcj1);
    externalList.add(pcj2);

    provider.setIndices(externalList);
    final PCJOptimizer optimizer = new PCJOptimizer(externalList, false, provider);
    optimizer.optimize(te1, null, null);

    Assert.assertEquals(true, validatePcj(te1, unOpt, externalList, new HashSet<QueryModelNode>()));
}
 
Example 9
Source File: PCJOptimizerTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testSegmentWithUnionAndFilters() throws Exception {

    final String query1 = ""//
            + "SELECT ?e ?c ?l" //
            + "{" //
            + " Filter(?e = <uri:s1>) " //
            + " Filter(?c = <uri:s2>) " //
            + " {?e <uri:p1> <uri:o1>. } UNION { ?e a ?c. OPTIONAL {?e <uri:talksTo> ?l}. ?e <uri:p5> <uri:o4>. ?e <uri:p4> <uri:o3> }  . "//
            + "  ?e <uri:p2> ?c . "//
            + "  ?e <uri:p3> <uri:o2>  . "//
            + "}";//

    final String query2 = ""//
            + "SELECT ?a ?b ?m" //
            + "{" //
            + " Filter(?b = <uri:s2>) " //
            + " ?a <uri:p5> <uri:o4> ." //
            + " ?a <uri:p4> <uri:o3> ." //
            + "  OPTIONAL {?a <uri:talksTo> ?m} . "//
            + "  ?a a ?b . "//
            + "}";//

    final String query3 = ""//
            + "SELECT ?h ?i" //
            + "{" //
            + " Filter(?h = <uri:s1>) " //
            + "  ?h <uri:p2> ?i . "//
            + "  ?h <uri:p3> <uri:o2>  . "//
            + "}";//

    final SPARQLParser parser = new SPARQLParser();
    final ParsedQuery pq1 = parser.parseQuery(query1, null);
    final ParsedQuery pq2 = parser.parseQuery(query2, null);
    final ParsedQuery pq3 = parser.parseQuery(query3, null);
    final TupleExpr te1 = pq1.getTupleExpr();
    final TupleExpr te2 = pq2.getTupleExpr();
    final TupleExpr te3 = pq3.getTupleExpr();

    final TupleExpr unOpt = te1.clone();
    final List<QueryModelNode> remainingNodes = getNodes(te1);
    final Set<QueryModelNode> unMatchedNodes = new HashSet<>();
    unMatchedNodes.add(remainingNodes.get(0));

    final SimpleExternalTupleSet pcj1 = new SimpleExternalTupleSet((Projection) te2);
    final SimpleExternalTupleSet pcj2 = new SimpleExternalTupleSet((Projection) te3);
    final List<ExternalTupleSet> externalList = new ArrayList<>();
    externalList.add(pcj1);
    externalList.add(pcj2);

    provider.setIndices(externalList);
    final PCJOptimizer optimizer = new PCJOptimizer(externalList, false, provider);
    optimizer.optimize(te1, null, null);

    Assert.assertEquals(true, validatePcj(te1, unOpt, externalList, unMatchedNodes));

}
 
Example 10
Source File: PCJOptimizerTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testSegmentWithLargeUnion() throws Exception {

    final String query1 = ""//
            + "SELECT ?e ?c ?l" //
            + "{" //
            + " {?e <uri:p1> <uri:o1>. } UNION { " //
            + "  ?e <uri:p0> ?l ." //
            + "  ?l <uri:p5> <uri:o5> ." //
            + " OPTIONAL{?l <uri:p4> <uri:o4>} ." + "  ?c<uri:p1> ?l ." //
            + " OPTIONAL{ ?e <uri:p1> ?c } ." //
            + " ?e <uri:p2> <uri:o2>. " //
            + " ?c <uri:p3> <uri:o3> " //
            + " }  . "//
            + "  ?e <uri:p2> ?c . "//
            + "  ?e <uri:p3> <uri:o2>  . "//
            + "}";//

    final String query2 = ""//
            + "SELECT ?a ?b ?c " //
            + "{" //
            + " ?a <uri:p2> <uri:o2>. " //
            + " ?b <uri:p3> <uri:o3>. " //
            + " OPTIONAL{ ?a <uri:p1> ?b } ." //
            + "  ?a <uri:p0> ?c ." //
            + "  ?b<uri:p1> ?c " //
            + "}";//

    final String query3 = ""//
            + "SELECT ?h ?i" //
            + "{" //
            + "  ?h <uri:p2> ?i . "//
            + "  ?h <uri:p3> <uri:o2>  . "//
            + "}";//

    final SPARQLParser parser = new SPARQLParser();
    final ParsedQuery pq1 = parser.parseQuery(query1, null);
    final ParsedQuery pq2 = parser.parseQuery(query2, null);
    final ParsedQuery pq3 = parser.parseQuery(query3, null);
    final TupleExpr te1 = pq1.getTupleExpr();
    final TupleExpr te2 = pq2.getTupleExpr();
    final TupleExpr te3 = pq3.getTupleExpr();

    final TupleExpr unOpt = te1.clone();
    final List<QueryModelNode> remainingNodes = getNodes(te1);
    final Set<QueryModelNode> unMatchedNodes = new HashSet<>();
    unMatchedNodes.add(remainingNodes.get(0));
    unMatchedNodes.add(remainingNodes.get(2));
    unMatchedNodes.add(remainingNodes.get(3));

    final SimpleExternalTupleSet pcj1 = new SimpleExternalTupleSet((Projection) te2);
    final SimpleExternalTupleSet pcj2 = new SimpleExternalTupleSet((Projection) te3);
    final List<ExternalTupleSet> externalList = new ArrayList<>();
    externalList.add(pcj1);
    externalList.add(pcj2);

    provider.setIndices(externalList);
    final PCJOptimizer optimizer = new PCJOptimizer(externalList, false, provider);
    optimizer.optimize(te1, null, null);

    Assert.assertEquals(true, validatePcj(te1, unOpt, externalList, unMatchedNodes));

}
 
Example 11
Source File: PCJOptimizerTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testSwitchTwoBoundVars() throws Exception {

    final String query1 = ""//
            + "SELECT ?a ?b ?c " //
            + "{" //
            + "  ?a <uri:p0> ?c ." //
            + "  ?c <uri:p5> <uri:o5> ." //
            + " OPTIONAL{?c <uri:p4> <uri:o4>} ."
            + " ?b<uri:p1> ?c ." //
            + " OPTIONAL{ ?a <uri:p1> ?b } ." //
            + " ?a <uri:p2> <uri:o2>. " //
            + " ?b <uri:p3> <uri:o3> " //
            + "}";//

    final String query2 = ""//
            + "SELECT ?a ?b ?c " //
            + "{" //
            + " ?a <uri:p2> <uri:o2>. " //
            + " ?b <uri:p3> <uri:o3>. " //
            + " OPTIONAL{ ?a <uri:p1> ?b } ." //
            + "  ?a <uri:p0> ?c ." //
            + "  ?b<uri:p1> ?c " //
            + "}";//

    final SPARQLParser parser = new SPARQLParser();
    final ParsedQuery pq1 = parser.parseQuery(query1, null);
    final ParsedQuery pq2 = parser.parseQuery(query2, null);
    final TupleExpr te1 = pq1.getTupleExpr();
    final TupleExpr te2 = pq2.getTupleExpr();

    final TupleExpr unOpt = te1.clone();
    final List<QueryModelNode> remainingNodes = getNodes(te1);
    final Set<QueryModelNode> unMatchedNodes = new HashSet<>();
    unMatchedNodes.add(remainingNodes.get(1));
    unMatchedNodes.add(remainingNodes.get(2));

    final SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection) te2);
    final List<ExternalTupleSet> externalList = new ArrayList<>();
    externalList.add(pcj);

    provider.setIndices(externalList);
    final PCJOptimizer optimizer = new PCJOptimizer(externalList, false, provider);
    optimizer.optimize(te1, null, null);

    Assert.assertEquals(true, validatePcj(te1, unOpt, externalList, unMatchedNodes));
}
 
Example 12
Source File: PCJOptimizerTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testSubsetMatchLargeReOrdered() throws Exception {

    final 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} . "//
            + "  OPTIONAL{?b <uri:p5> ?i. ?i <uri:p6> ?j} . "//
            + "  OPTIONAL{?b <uri:p5> ?k. ?k <uri:p6> ?l} . "//
            + "  OPTIONAL{?b <uri:p5> ?m. ?m <uri:p6> ?n} . "//
            + "  OPTIONAL{?b <uri:p4> ?o. ?o <uri:p1> ?p} . "//
            + "}";//

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

    final SPARQLParser parser = new SPARQLParser();
    final ParsedQuery pq1 = parser.parseQuery(query1, null);
    final ParsedQuery pq2 = parser.parseQuery(query2, null);
    final TupleExpr te1 = pq1.getTupleExpr();
    final TupleExpr te2 = pq2.getTupleExpr();

    final TupleExpr unOpt = te1.clone();
    final List<QueryModelNode> remainingNodes = getNodes(te1);
    final Set<QueryModelNode> unMatchedNodes = new HashSet<>();
    unMatchedNodes.add(remainingNodes.get(8));
    unMatchedNodes.add(remainingNodes.get(9));
    unMatchedNodes.add(remainingNodes.get(10));
    unMatchedNodes.add(remainingNodes.get(11));
    unMatchedNodes.add(remainingNodes.get(12));
    unMatchedNodes.add(remainingNodes.get(7));

    final SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection) te2);
    final List<ExternalTupleSet> externalList = new ArrayList<>();
    externalList.add(pcj);

    provider.setIndices(externalList);
    final PCJOptimizer optimizer = new PCJOptimizer(externalList, false, provider);
    optimizer.optimize(te1, null, null);

    Assert.assertEquals(true, validatePcj(te1, unOpt, externalList, unMatchedNodes));
}
 
Example 13
Source File: PCJOptimizerTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testExactMatchLargeReOrdered() throws Exception {


    final 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} . "//
            + "  OPTIONAL{?b <uri:p4> ?i. ?i <uri:p1> ?j} . "//
            + "  OPTIONAL{?b <uri:p4> ?k. ?k <uri:p1> ?l} . "//
            + "  OPTIONAL{?b <uri:p4> ?m. ?m <uri:p1> ?n} . "//
            + "  OPTIONAL{?b <uri:p4> ?o. ?o <uri:p1> ?p} . "//
            + "}";//

    final String query2 = ""//
            + "SELECT ?a ?b ?c ?d ?e ?f ?g ?h" //
            + "{" //
            + "  ?a <uri:p0> ?b ." //
            + "  OPTIONAL{?b <uri:p4> ?o. ?o <uri:p1> ?p} . "//
            + "  OPTIONAL{?b <uri:p4> ?g. ?g <uri:p1> ?h} . "//
            + "  OPTIONAL{?b <uri:p2> ?c. ?c <uri:p1> ?d} . " //
            + "  OPTIONAL{?b <uri:p4> ?i. ?i <uri:p1> ?j} . "//
            + "  OPTIONAL{?b <uri:p4> ?m. ?m <uri:p1> ?n} . "//
            + "  OPTIONAL{?b <uri:p4> ?k. ?k <uri:p1> ?l} . "//
            + "  OPTIONAL{?b <uri:p3> ?e. ?e <uri:p1> ?f} . "//
            + "}";//

    final SPARQLParser parser = new SPARQLParser();
    final ParsedQuery pq1 = parser.parseQuery(query1, null);
    final ParsedQuery pq2 = parser.parseQuery(query2, null);
    final TupleExpr te1 = pq1.getTupleExpr();
    final TupleExpr te2 = pq2.getTupleExpr();

    final TupleExpr unOpt = te1.clone();

    final SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection) te2);
    final List<ExternalTupleSet> externalList = new ArrayList<>();
    externalList.add(pcj);


    provider.setIndices(externalList);
    final PCJOptimizer optimizer = new PCJOptimizer(externalList, false, provider);
    optimizer.optimize(te1, null, null);

    Assert.assertEquals(true, validatePcj(te1, unOpt, externalList, new HashSet<QueryModelNode>()));
}
 
Example 14
Source File: FederationEvalStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public TupleExpr optimize(TupleExpr expr, EvaluationStatistics evaluationStatistics,
		BindingSet bindings) {

	if (!(evaluationStatistics instanceof FederationEvaluationStatistics)) {
		throw new FedXRuntimeException(
				"Expected FederationEvaluationStatistics, was " + evaluationStatistics.getClass());
	}

	FederationEvaluationStatistics stats = (FederationEvaluationStatistics) evaluationStatistics;
	QueryInfo queryInfo = stats.getQueryInfo();
	Dataset dataset = stats.getDataset();

	FederationContext federationContext = queryInfo.getFederationContext();
	List<Endpoint> members;
	if (dataset instanceof FedXDataset) {
		// run the query against a selected set of endpoints
		FedXDataset ds = (FedXDataset) dataset;
		members = federationContext.getEndpointManager().getEndpoints(ds.getEndpoints());
	} else {
		// evaluate against entire federation
		FedX fed = federationContext.getFederation();
		members = fed.getMembers();
	}

	// Clone the tuple expression to allow for more aggressive optimizations
	TupleExpr query = new QueryRoot(expr.clone());

	GenericInfoOptimizer info = new GenericInfoOptimizer(queryInfo);

	// collect information and perform generic optimizations
	info.optimize(query);

	// if the federation has a single member only, evaluate the entire query there
	if (members.size() == 1 && queryInfo.getQuery() != null && propagateServices(info.getServices())
			&& queryInfo.getQueryType() != QueryType.UPDATE) {
		return new SingleSourceQuery(expr, members.get(0), queryInfo);
	}

	if (log.isTraceEnabled()) {
		log.trace("Query before Optimization: " + query);
	}

	/* original RDF4J optimizers */
	new ConstantOptimizer(this).optimize(query, dataset, bindings); // maybe remove this optimizer later

	new DisjunctiveConstraintOptimizer().optimize(query, dataset, bindings);

	/*
	 * TODO add some generic optimizers: - FILTER ?s=1 && ?s=2 => EmptyResult - Remove variables that are not
	 * occurring in query stmts from filters
	 */

	/* custom optimizers, execute only when needed */

	// if the query has a single relevant source (and if it is not a SERVICE query), evaluate at this source only
	// Note: UPDATE queries are always handled in the federation engine to adhere to the configured
	// write strategy
	Set<Endpoint> relevantSources = performSourceSelection(members, cache, queryInfo, info);
	if (relevantSources.size() == 1 && propagateServices(info.getServices())
			&& queryInfo.getQueryType() != QueryType.UPDATE) {
		return new SingleSourceQuery(query, relevantSources.iterator().next(), queryInfo);
	}

	if (info.hasService()) {
		new ServiceOptimizer(queryInfo).optimize(query);
	}

	// optimize unions, if available
	if (info.hasUnion()) {
		new UnionOptimizer(queryInfo).optimize(query);
	}

	optimizeExclusiveExpressions(query, queryInfo, info);

	// optimize statement groups and join order
	optimizeJoinOrder(query, queryInfo, info);

	// potentially push limits (if applicable)
	if (info.hasLimit()) {
		new LimitOptimizer().optimize(query);
	}

	// optimize Filters, if available
	// Note: this is done after the join order is determined to ease filter pushing
	if (info.hasFilter()) {
		new FilterOptimizer().optimize(query);
	}

	if (log.isTraceEnabled()) {
		log.trace("Query after Optimization: " + query);
	}

	return query;
}
 
Example 15
Source File: PCJOptimizerTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testBasicSegment() throws Exception {

    final String query1 = ""//
            + "SELECT ?e ?c ?l" //
            + "{" //
            + "  ?e a ?c . "//
            + "  OPTIONAL {?e <uri:talksTo> ?l}  . "//
            + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
            + "}";//

    final String query2 = ""//
            + "SELECT ?a ?b ?m" //
            + "{" //
            + "  ?a a ?b . "//
            + "  OPTIONAL {?a <uri:talksTo> ?m}  . "//
            + "}";//

    final SPARQLParser parser = new SPARQLParser();
    final ParsedQuery pq1 = parser.parseQuery(query1, null);
    final ParsedQuery pq2 = parser.parseQuery(query2, null);
    final TupleExpr te1 = pq1.getTupleExpr();
    final TupleExpr te2 = pq2.getTupleExpr();

    final TupleExpr unOpt = te1.clone();
    final List<QueryModelNode> remainingNodes = getNodes(te1);
    final Set<QueryModelNode> unMatchedNodes = new HashSet<>();
    unMatchedNodes.add(remainingNodes.get(2));

    final SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection) te2);
    final List<ExternalTupleSet> externalList = new ArrayList<>();
    externalList.add(pcj);

    provider.setIndices(externalList);
    final PCJOptimizer optimizer = new PCJOptimizer(externalList, false, provider);
    optimizer.optimize(te1, null, null);

    Assert.assertEquals(true, validatePcj(te1, unOpt, externalList, unMatchedNodes));


}
 
Example 16
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Iterates through list of normalized indexes and replaces all subtrees of query which match index with index.
 *
 * @param query
 * @return TupleExpr
 */
public static TupleExpr process(TupleExpr query, List<ExternalTupleSet> indexSet) {

    boolean indexPlaced = false;
    TupleExpr rtn = query.clone();
    QueryNodeCount qnc = new QueryNodeCount();
    rtn.visit(qnc);

    if(qnc.getNodeCount()/2 < indexSet.size()) {
        return null;
    }


    //move BindingSetAssignment Nodes out of the way
    organizeBSAs(rtn);


    // test to see if query contains no other nodes
    // than filter, join, projection, and statement pattern and
    // test whether query contains duplicate StatementPatterns and filters
    if (isTupleValid(rtn)) {

        for (ExternalTupleSet index : indexSet) {

            // test to see if index contains at least one StatementPattern,
            // that StatementPatterns are unique,
            // and that all variables found in filters occur in some
            // StatementPattern
            if (isTupleValid(index.getTupleExpr())) {

                ExternalTupleSet eTup = (ExternalTupleSet) index.clone();
                SPBubbleDownVisitor indexVistor = new SPBubbleDownVisitor(eTup);
                rtn.visit(indexVistor);
                FilterBubbleManager fbmv = new FilterBubbleManager(eTup);
                rtn.visit(fbmv);
                SubsetEqualsVisitor subIndexVis = new SubsetEqualsVisitor(eTup, rtn);
                rtn.visit(subIndexVis);
                indexPlaced = subIndexVis.indexPlaced();
                if(!indexPlaced) {
                    break;
                }

            }

        }
        if(indexPlaced) {
            return rtn;
        } else {
            return null;
        }

    } else {
        throw new IllegalArgumentException("Invalid Query.");
    }
}
 
Example 17
Source File: HalyardEvaluationStrategy.java    From Halyard with Apache License 2.0 4 votes vote down vote up
public ServiceRoot(TupleExpr serviceArgs) {
    super(serviceArgs.clone());
    this.originalServiceArgs = serviceArgs;
}
 
Example 18
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitDeleteWhere(Resource query) throws RDF4JException {
	TupleExpr whereExpr = visitWhere(query);
	updateRoot = new Modify(whereExpr, null, whereExpr.clone());
}