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

The following examples show how to use org.eclipse.rdf4j.query.algebra.QueryRoot. 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: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void visitInsertData(Resource query) throws RDF4JException {
	SingletonSet stub = new SingletonSet();
	tupleRoot = new QueryRoot(stub);
	tupleNode = stub;
	TupleExpr insertExpr;
	Value insert = TripleSources.singleValue(query, SP.DATA_PROPERTY, store);
	if (!(insert instanceof Resource)) {
		throw new MalformedSpinException(String.format("Value of %s is not a resource", SP.DATA_PROPERTY));
	}
	visitInsert((Resource) insert);
	insertExpr = tupleNode;
	insertExpr.setParentNode(null);

	DataVisitor visitor = new DataVisitor();
	insertExpr.visit(visitor);
	updateRoot = new InsertData(visitor.getData());
}
 
Example #2
Source File: SparqlToPigTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
public void testLimit() throws Exception {
        String query = "select * where {\n" +
                "?subj <urn:lubm:rdfts#name> 'Department0'.\n" +
                "?subj <urn:lubm:rdfts#subOrganizationOf> ?suborg.\n" +
                "} limit 100";
//        System.out.println(query);
        QueryParser parser = new SPARQLParser();
        ParsedQuery parsedQuery = parser.parseQuery(query, null);

//        System.out.println(parsedQuery);

        SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor();
        visitor.setTablePrefix(tablePrefix);
        visitor.setInstance(instance);
        visitor.setZk(zk);
        visitor.setUser(user);
        visitor.setPassword(password);
        visitor.meet(new QueryRoot(parsedQuery.getTupleExpr()));
//        System.out.println(visitor.getPigScript());
    }
 
Example #3
Source File: SparqlToPigTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
public void testCross() throws Exception {
        String query = "select * where {\n" +
                "?subj0 <urn:lubm:rdfts#name> 'Department0'.\n" +
                "?subj1 <urn:lubm:rdfts#name> 'Department1'.\n" +
                "?subj0 <urn:lubm:rdfts#subOrganizationOf> <http://www.University0.edu>.\n" +
                "?subj1 <urn:lubm:rdfts#subOrganizationOf> <http://www.University0.edu>.\n" +
                "}";
//        System.out.println(query);
        QueryParser parser = new SPARQLParser();
        ParsedQuery parsedQuery = parser.parseQuery(query, null);
        QueryRoot tupleExpr = new QueryRoot(parsedQuery.getTupleExpr());

        SimilarVarJoinOptimizer similarVarJoinOptimizer = new SimilarVarJoinOptimizer();
        similarVarJoinOptimizer.optimize(tupleExpr, null, null);

//        System.out.println(tupleExpr);

        SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor();
        visitor.setTablePrefix(tablePrefix);
        visitor.setInstance(instance);
        visitor.setZk(zk);
        visitor.setUser(user);
        visitor.setPassword(password);
        visitor.meet(tupleExpr);
//        System.out.println(visitor.getPigScript());
    }
 
Example #4
Source File: SparqlToPigTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
public void testMutlipleJoins() throws Exception {
        String query = "select * where {\n" +
                "?subj <urn:lubm:rdfts#name> 'Department0'.\n" +
                "?subj <urn:lubm:rdfts#subOrganizationOf> <http://www.University0.edu>.\n" +
                "?subj <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <urn:lubm:rdfts#Department>.\n" +
                "}";
//        System.out.println(query);
        QueryParser parser = new SPARQLParser();
        ParsedQuery parsedQuery = parser.parseQuery(query, null);

//        System.out.println(parsedQuery);

        SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor();
        visitor.setTablePrefix(tablePrefix);
        visitor.setInstance(instance);
        visitor.setZk(zk);
        visitor.setUser(user);
        visitor.setPassword(password);
        visitor.meet(new QueryRoot(parsedQuery.getTupleExpr()));
//        System.out.println(visitor.getPigScript());
    }
 
Example #5
Source File: SparqlToPigTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
public void testMutliReturnJoin() throws Exception {
    String query = "select * where {\n" +
            "?subj <urn:lubm:rdfts#name> 'Department0'.\n" +
            "?subj <urn:lubm:rdfts#subOrganizationOf> ?suborg.\n" +
            "}";
    QueryParser parser = new SPARQLParser();
    ParsedQuery parsedQuery = parser.parseQuery(query, null);

    System.out.println(query);

    SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor();
    visitor.setTablePrefix(tablePrefix);
    visitor.setInstance(instance);
    visitor.setZk(zk);
    visitor.setUser(user);
    visitor.setPassword(password);
    visitor.meet(new QueryRoot(parsedQuery.getTupleExpr()));
    System.out.println(visitor.getPigScript());
}
 
Example #6
Source File: SparqlToPigTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
public void testJoin() throws Exception {
        String query = "select * where {\n" +
                "?subj <urn:lubm:rdfts#name> 'Department0'.\n" +
                "?subj <urn:lubm:rdfts#subOrganizationOf> <http://www.University0.edu>.\n" +
                "}";
//        System.out.println(query);
        QueryParser parser = new SPARQLParser();
        ParsedQuery parsedQuery = parser.parseQuery(query, null);

//        System.out.println(parsedQuery);

        SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor();
        visitor.setTablePrefix(tablePrefix);
        visitor.setInstance(instance);
        visitor.setZk(zk);
        visitor.setUser(user);
        visitor.setPassword(password);
        visitor.meet(new QueryRoot(parsedQuery.getTupleExpr()));
//        System.out.println(visitor.getPigScript());
    }
 
Example #7
Source File: SparqlToPigTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
public void testStatementPatternContextVar() throws Exception {
        String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                " PREFIX ub: <urn:lubm:rdfts#>\n" +
                "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
                " SELECT * WHERE\n" +
                " {\n" +
                " GRAPH ?g {\n" +
                "\t?x rdf:type ub:UndergraduateStudent\n" +
                " }\n" +
                " ?x ub:pred ?g." +
                " }\n" +
                "";
        QueryParser parser = new SPARQLParser();
        ParsedQuery parsedQuery = parser.parseQuery(query, null);

//        System.out.println(parsedQuery);

        SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor();
        visitor.setTablePrefix(tablePrefix);
        visitor.setInstance(instance);
        visitor.setZk(zk);
        visitor.setUser(user);
        visitor.setPassword(password);
        visitor.meet(new QueryRoot(parsedQuery.getTupleExpr()));
//        System.out.println(visitor.getPigScript());
    }
 
Example #8
Source File: SparqlToPigTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
public void testStatementPatternContext() throws Exception {
        String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                " PREFIX ub: <urn:lubm:rdfts#>\n" +
                "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
                " SELECT * WHERE\n" +
                " {\n" +
                " GRAPH ub:g1 {\n" +
                "\t?x rdf:type ub:UndergraduateStudent\n" +
                " }\n" +
                " }\n" +
                "";
        QueryParser parser = new SPARQLParser();
        ParsedQuery parsedQuery = parser.parseQuery(query, null);

//        System.out.println(parsedQuery);

        SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor();
        visitor.setTablePrefix(tablePrefix);
        visitor.setInstance(instance);
        visitor.setZk(zk);
        visitor.setUser(user);
        visitor.setPassword(password);
        visitor.meet(new QueryRoot(parsedQuery.getTupleExpr()));
//        System.out.println(visitor.getPigScript());
    }
 
Example #9
Source File: SparqlToPigTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
public void testStatementPattern() throws Exception {
        String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                " PREFIX ub: <urn:lubm:rdfts#>\n" +
                "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
                " SELECT * WHERE\n" +
                " {\n" +
                "\t?x rdf:type ub:UndergraduateStudent\n" +
                " }\n" +
                "";
        QueryParser parser = new SPARQLParser();
        ParsedQuery parsedQuery = parser.parseQuery(query, null);

//        System.out.println(parsedQuery);

        SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor();
        visitor.setTablePrefix(tablePrefix);
        visitor.setInstance(instance);
        visitor.setZk(zk);
        visitor.setUser(user);
        visitor.setPassword(password);
        visitor.meet(new QueryRoot(parsedQuery.getTupleExpr()));
//        System.out.println(visitor.getPigScript());
    }
 
Example #10
Source File: SparqlQueryPigEngine.java    From rya with Apache License 2.0 6 votes vote down vote up
public String generatePigScript(final String sparql) throws Exception {
        Preconditions.checkNotNull(sparql, "Sparql query cannot be null");
        final QueryParser parser = new SPARQLParser();
        final ParsedQuery parsedQuery = parser.parseQuery(sparql, null);
        final QueryRoot tupleExpr = new QueryRoot(parsedQuery.getTupleExpr());

//        SimilarVarJoinOptimizer similarVarJoinOptimizer = new SimilarVarJoinOptimizer();
//        similarVarJoinOptimizer.optimize(tupleExpr, null, null);

        if (inference || stats) {
            if (inference) {
                tupleExpr.visit(new TransitivePropertyVisitor(conf, inferenceEngine));
                tupleExpr.visit(new SymmetricPropertyVisitor(conf, inferenceEngine));
                tupleExpr.visit(new InverseOfVisitor(conf, inferenceEngine));
            }
            if (stats) {
                (new QueryJoinOptimizer(rdfCloudTripleStoreEvaluationStatistics)).optimize(tupleExpr, null, null);
            }
        }

        sparqlToPigTransformVisitor.meet(tupleExpr);
        return sparqlToPigTransformVisitor.getPigScript();
    }
 
Example #11
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnsupportedExtension() throws Exception {
    StatementPattern sp = new StatementPattern(new Var("x"), constant(TAKES), new Var("c"));
    List<ExtensionElem> elements = Arrays.asList(new ExtensionElem(new Var("x"), "renamed"),
            new ExtensionElem(new Not(new ValueConstant(VF.createLiteral(true))), "notTrue"),
            new ExtensionElem(new ValueConstant(TAKES), "constant"));
    Extension extensionNode = new Extension(sp, elements);
    QueryRoot queryTree = new QueryRoot(extensionNode);
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof Extension);
    Assert.assertEquals(elements, ((Extension) queryTree.getArg()).getElements());
    TupleExpr innerQuery = ((Extension) queryTree.getArg()).getArg();
    Assert.assertTrue(innerQuery instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) innerQuery;
    Assert.assertEquals(Sets.newHashSet("x", "c"), pipelineNode.getAssuredBindingNames());
}
 
Example #12
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiProjection() throws Exception {
    StatementPattern isUndergrad = new StatementPattern(new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD));
    StatementPattern isCourse = new StatementPattern(new Var("course"), constant(RDF.TYPE), constant(COURSE));
    StatementPattern hasEdge = new StatementPattern(new Var("x"), new Var("p"), new Var("course"));
    ProjectionElemList courseHasRelation = new ProjectionElemList(
            new ProjectionElem("p", "relation"),
            new ProjectionElem("course"));
    ProjectionElemList studentHasRelation = new ProjectionElemList(
            new ProjectionElem("p", "relation"),
            new ProjectionElem("x", "student"));
    QueryRoot queryTree = new QueryRoot(new MultiProjection(
            new Join(new Join(isCourse, hasEdge), isUndergrad),
            Arrays.asList(courseHasRelation, studentHasRelation)));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    Assert.assertEquals(Sets.newHashSet("relation"), pipelineNode.getAssuredBindingNames());
    Assert.assertEquals(Sets.newHashSet("relation", "course", "student"), pipelineNode.getBindingNames());
}
 
Example #13
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testProjection() throws Exception {
    StatementPattern isUndergrad = new StatementPattern(new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD));
    StatementPattern isCourse = new StatementPattern(new Var("course"), constant(RDF.TYPE), constant(COURSE));
    StatementPattern hasEdge = new StatementPattern(new Var("x"), new Var("p"), new Var("course"));
    ProjectionElemList projectionElements = new ProjectionElemList(
            new ProjectionElem("p", "relation"),
            new ProjectionElem("course"));
    QueryRoot queryTree = new QueryRoot(new Projection(
            new Join(new Join(isCourse, hasEdge), isUndergrad),
            projectionElements));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    Assert.assertEquals(Sets.newHashSet("relation", "course"), pipelineNode.getAssuredBindingNames());
}
 
Example #14
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testComplexJoin() throws Exception {
    StatementPattern isUndergrad = new StatementPattern(new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD));
    StatementPattern isProfessor = new StatementPattern(new Var("y"), constant(RDF.TYPE), constant(PROFESSOR));
    StatementPattern takesCourse = new StatementPattern(new Var("x"), constant(TAKES), new Var("c"));
    StatementPattern teachesCourse = new StatementPattern(new Var("y"), constant(TEACHES), new Var("c"));
    QueryRoot queryTree = new QueryRoot(new Join(
            new Join(isUndergrad, takesCourse),
            new Join(isProfessor, teachesCourse)));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof Join);
    Join topJoin = (Join) queryTree.getArg();
    Assert.assertTrue(topJoin.getLeftArg() instanceof AggregationPipelineQueryNode);
    Assert.assertTrue(topJoin.getRightArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode leftPipeline = (AggregationPipelineQueryNode) topJoin.getLeftArg();
    AggregationPipelineQueryNode rightPipeline = (AggregationPipelineQueryNode) topJoin.getRightArg();
    Assert.assertEquals(Sets.newHashSet("x", "c"), leftPipeline.getAssuredBindingNames());
    Assert.assertEquals(Sets.newHashSet("y", "c"), rightPipeline.getAssuredBindingNames());
}
 
Example #15
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedJoins() throws Exception {
    StatementPattern isUndergrad = new StatementPattern(new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD));
    StatementPattern isProfessor = new StatementPattern(new Var("y"), constant(RDF.TYPE), constant(PROFESSOR));
    StatementPattern takesCourse = new StatementPattern(new Var("x"), constant(TAKES), new Var("c"));
    StatementPattern teachesCourse = new StatementPattern(new Var("y"), constant(TEACHES), new Var("c"));
    QueryRoot queryTree = new QueryRoot(new Join(
            isProfessor,
            new Join(
                    new Join(isUndergrad, takesCourse),
                    teachesCourse)));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    Assert.assertEquals(Sets.newHashSet("x", "y", "c"), pipelineNode.getAssuredBindingNames());
}
 
Example #16
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void visitDeleteData(Resource query) throws RDF4JException {
	SingletonSet stub = new SingletonSet();
	tupleRoot = new QueryRoot(stub);
	tupleNode = stub;
	TupleExpr deleteExpr;
	Value delete = TripleSources.singleValue(query, SP.DATA_PROPERTY, store);
	if (!(delete instanceof Resource)) {
		throw new MalformedSpinException(String.format("Value of %s is not a resource", SP.DATA_PROPERTY));
	}
	visitDelete((Resource) delete);
	deleteExpr = tupleNode;
	deleteExpr.setParentNode(null);

	DataVisitor visitor = new DataVisitor();
	deleteExpr.visit(visitor);
	updateRoot = new DeleteData(visitor.getData());
}
 
Example #17
Source File: ParallelEvaluationStrategyImpl.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
    public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(TupleExpr expr, BindingSet bindings) throws QueryEvaluationException {
        if (expr instanceof QueryRoot) {
            if (displayQueryPlan) {
//                System.out.println("Tables: ");
//                System.out.println("--SPO: \t" + RdfCloudTripleStoreConstants.TBL_SPO);
//                System.out.println("--PO: \t" + RdfCloudTripleStoreConstants.TBL_PO);
//                System.out.println("--OSP: \t" + RdfCloudTripleStoreConstants.TBL_OSP);
                logger.info("=================== Rya Query ===================");
                for (String str : expr.toString().split("\\r?\\n")) {
                    logger.info(str);
                }
                logger.info("================= End Rya Query =================");
            }
        }
        return super.evaluate(expr, bindings);
    }
 
Example #18
Source File: QueryJoinOptimizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSES2116JoinBind() throws Exception {

	StringBuilder qb = new StringBuilder();
	qb.append("SELECT ?subject ?name ?row {\n" + "  ?subject <http://localhost/table_1> ?uri .\n"
			+ "  BIND(STR(?uri) AS ?name)\n"
			+ "  ?table <http://linked.opendata.cz/ontology/odcs/tabular/hasRow> ?row .\n"
			+ "  ?table <http://linked.opendata.cz/ontology/odcs/tabular/symbolicName> ?name .\n" + "}");

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery q = parser.parseQuery(qb.toString(), null);
	QueryJoinOptimizer opt = new QueryJoinOptimizer();
	QueryRoot optRoot = new QueryRoot(q.getTupleExpr());
	opt.optimize(optRoot, null, null);
	TupleExpr leaf = findLeaf(optRoot);
	Assert.assertTrue("Extension must be evaluated before StatementPattern",
			leaf.getParentNode() instanceof Extension);
}
 
Example #19
Source File: QueryJoinOptimizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void bindSubselectJoinOrder() throws Exception {
	String query = "SELECT * WHERE {\n" + "    BIND (bnode() as ?ct01) \n" + "    { SELECT ?s WHERE {\n"
			+ "            ?s ?p ?o .\n" + "      }\n" + "      LIMIT 10\n" + "    }\n" + "}";

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery q = parser.parseQuery(query, null);
	QueryJoinOptimizer opt = new QueryJoinOptimizer();
	QueryRoot optRoot = new QueryRoot(q.getTupleExpr());
	opt.optimize(optRoot, null, null);

	JoinFinder joinFinder = new JoinFinder();
	optRoot.visit(joinFinder);
	Join join = joinFinder.getJoin();

	assertThat(join.getLeftArg()).as("BIND clause should be left-most argument of join")
			.isInstanceOf(Extension.class);
}
 
Example #20
Source File: SimpleQueryCompiler.java    From semagrow with Apache License 2.0 5 votes vote down vote up
private QueryBlock blockify(QueryRoot query, Dataset dataset, BindingSet bindings) {
    QueryBlock block = QueryBlockBuilder.build(query);   // translate TupleExpr to simple QueryBlocks
    block.visit(new DistinctStrategyVisitor());          // relax duplicate restriction if possible to facilitate merging
    block.visit(new ExistToEachQuantificationVisitor()); // try unnest existential queries if possible
    block.visit(new UnionMergeVisitor());                // try merge union blocks if possible
    block.visit(new SelectMergeVisitor());               // try merge select blocks if possible
    return block;
}
 
Example #21
Source File: PipelineQueryIT.java    From rya with Apache License 2.0 5 votes vote down vote up
private void testPipelineQuery(final String query, final Multiset<BindingSet> expectedSolutions) throws Exception {
    // Prepare query and convert to pipeline
    final QueryRoot queryTree = new QueryRoot(PARSER.parseQuery(query, null).getTupleExpr());
    final SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(getRyaCollection());
    queryTree.visit(visitor);
    // Execute pipeline and verify results
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    final AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    final Multiset<BindingSet> solutions = HashMultiset.create();
    final CloseableIteration<BindingSet, QueryEvaluationException> iter = pipelineNode.evaluate(new QueryBindingSet());
    while (iter.hasNext()) {
        solutions.add(iter.next());
    }
    Assert.assertEquals(expectedSolutions, solutions);
}
 
Example #22
Source File: SimpleQueryCompiler.java    From semagrow with Apache License 2.0 5 votes vote down vote up
@Override
public Plan compile(QueryRoot query, Dataset dataset, BindingSet bindings) {

    // transformations on logical query.
    rewrite(query.getArg(), dataset, bindings);

    // split query to queryblocks.
    QueryBlock blockRoot = blockify(query, dataset, bindings);

    // infer interesting properties for each query block.
    blockRoot.visit(new InterestingPropertiesVisitor());     // infer interesting properties for each block

    // traverse Blocks and compile them bottom-up.
    Collection<Plan> plans = blockRoot.getPlans(getContext());

    // enforce Site = Local
    RequestedPlanProperties props = new RequestedPlanProperties();
    props.setSite(LocalSite.getInstance());

    plans = getContext().enforceProps(plans, props);
    getContext().prune(plans);

    if (plans.isEmpty())
        return null;
    else
        return plans.iterator().next();
}
 
Example #23
Source File: QueryGraphDecomposer.java    From semagrow with Apache License 2.0 5 votes vote down vote up
@Override
public void decompose(TupleExpr expr, Dataset dataset, BindingSet bindings) {

    /* creates the context of operation of the decomposer.
     * Specifically, collects FILTER statements */
    QueryGraphDecomposerContext ctx = new QueryGraphDecomposerContext( expr );

    SourceSelector staticSelector = new StaticSourceSelector(sourceSelector.getSources(expr, dataset, bindings));
    PlanFactory planFactory = new SimplePlanFactory(costEstimator, cardinalityEstimator);

    /* uses the SourceSelector provided in order to identify the
     * sub-expressions that can be executed at each data source,
     * and annotates with cardinality and selectivity metadata */

    PlanGenerator planGenerator = new QueryGraphPlanGenerator(ctx, staticSelector, planFactory);

    /* optimizes the plans generated by the PlanGenerator */
    DPPlanOptimizer planOptimizer = new DPPlanOptimizer(planGenerator);

    /* selects the optimal plan  */
    Optional<Plan> maybePlan = planOptimizer.getBestPlan(ctx.getQueryGraph().getVertices(), bindings, dataset);

    if (maybePlan.isPresent()) {
        /* grafts the optimal plan into expr */
        if (expr instanceof QueryRoot) {
            ((QueryRoot)expr).getArg().replaceWith(maybePlan.get());
        }
    }
}
 
Example #24
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 #25
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(UnaryTupleOperator expr,
		BindingSet bindings) throws QueryEvaluationException {
	if (expr instanceof Projection) {
		return evaluate((Projection) expr, bindings);
	} else if (expr instanceof MultiProjection) {
		return evaluate((MultiProjection) expr, bindings);
	} else if (expr instanceof Filter) {
		return evaluate((Filter) expr, bindings);
	} else if (expr instanceof Service) {
		return evaluate((Service) expr, bindings);
	} else if (expr instanceof Slice) {
		return evaluate((Slice) expr, bindings);
	} else if (expr instanceof Extension) {
		return evaluate((Extension) expr, bindings);
	} else if (expr instanceof Distinct) {
		return evaluate((Distinct) expr, bindings);
	} else if (expr instanceof Reduced) {
		return evaluate((Reduced) expr, bindings);
	} else if (expr instanceof Group) {
		return evaluate((Group) expr, bindings);
	} else if (expr instanceof Order) {
		return evaluate((Order) expr, bindings);
	} else if (expr instanceof QueryRoot) {
		// new query, reset shared return value for successive calls of
		// NOW()
		this.sharedValueOfNow = null;
		return evaluate(expr.getArg(), bindings);
	} else if (expr instanceof DescribeOperator) {
		return evaluate((DescribeOperator) expr, bindings);
	} else if (expr == null) {
		throw new IllegalArgumentException("expr must not be null");
	} else {
		throw new QueryEvaluationException("Unknown unary tuple operator type: " + expr.getClass());
	}
}
 
Example #26
Source File: QueryJoinOptimizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testOptimizer(String expectedQuery, String actualQuery)
		throws MalformedQueryException, UnsupportedQueryLanguageException {
	ParsedQuery pq = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, actualQuery, null);
	QueryJoinOptimizer opt = new QueryJoinOptimizer();
	QueryRoot optRoot = new QueryRoot(pq.getTupleExpr());
	opt.optimize(optRoot, null, null);

	ParsedQuery expectedParsedQuery = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, expectedQuery, null);
	QueryRoot root = new QueryRoot(expectedParsedQuery.getTupleExpr());
	assertQueryModelTrees(root, optRoot);
}
 
Example #27
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtension() throws Exception {
    QueryRoot queryTree = new QueryRoot(new Extension(
            new StatementPattern(new Var("x"), constant(TAKES), new Var("c")),
            new ExtensionElem(new Var("x"), "renamed"),
            new ExtensionElem(new ValueConstant(TAKES), "constant")));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    Assert.assertEquals(Sets.newHashSet("x", "c", "renamed", "constant"), pipelineNode.getAssuredBindingNames());
}
 
Example #28
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testStatementPattern() throws Exception {
    QueryRoot query = new QueryRoot(new StatementPattern(
            new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD)));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    query.visit(visitor);
    Assert.assertTrue(query.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) query.getArg();
    Assert.assertEquals(Sets.newHashSet("x"), pipelineNode.getAssuredBindingNames());
}
 
Example #29
Source File: PipelineQueryIT.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoVariableSP() throws Exception {
    // Insert data
    insert(OWL.THING, RDF.TYPE, OWL.CLASS);
    insert(FOAF.PERSON, RDF.TYPE, OWL.CLASS, 1);
    insert(FOAF.PERSON, RDFS.SUBCLASSOF, OWL.THING);
    insert(VF.createIRI("urn:Alice"), RDF.TYPE, FOAF.PERSON);
    dao.flush();
    // Define query and expected results
    final String query = "SELECT * WHERE {\n"
            + "  owl:Thing a owl:Class .\n"
            + "}";
    final Multiset<BindingSet> expectedSolutions = HashMultiset.create();
    expectedSolutions.add(new EmptyBindingSet());
    // Execute pipeline and verify results
    final QueryRoot queryTree = new QueryRoot(PARSER.parseQuery(query, null).getTupleExpr());
    final SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(getRyaCollection());
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof Projection);
    final Projection projection = (Projection) queryTree.getArg();
    Assert.assertTrue(projection.getArg() instanceof AggregationPipelineQueryNode);
    final AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) projection.getArg();
    final Multiset<BindingSet> solutions = HashMultiset.create();
    final CloseableIteration<BindingSet, QueryEvaluationException> iter = pipelineNode.evaluate(new QueryBindingSet());
    while (iter.hasNext()) {
        solutions.add(iter.next());
    }
    Assert.assertEquals(expectedSolutions, solutions);
}
 
Example #30
Source File: PipelineQueryIT.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testTriplePipeline() throws Exception {
    final IRI alice = VF.createIRI("urn:Alice");
    final IRI bob = VF.createIRI("urn:Bob");
    final IRI eve = VF.createIRI("urn:Eve");
    final IRI friend = VF.createIRI("urn:friend");
    final IRI knows = VF.createIRI("urn:knows");
    final IRI year = VF.createIRI("urn:year");
    final Literal yearLiteral = VF.createLiteral("2017", XMLSchema.GYEAR);
    final String query = "CONSTRUCT {\n"
            + "    ?x <urn:knows> ?y .\n"
            + "    ?x <urn:year> \"2017\"^^<" + XMLSchema.GYEAR + "> .\n"
            + "} WHERE { ?x <urn:friend> ?y }";
    insert(alice, friend, bob);
    insert(bob, knows, eve);
    insert(eve, knows, alice);
    // Prepare query and convert to pipeline
    final QueryRoot queryTree = new QueryRoot(PARSER.parseQuery(query, null).getTupleExpr());
    final SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(getRyaCollection());
    queryTree.visit(visitor);
    // Get pipeline, add triple conversion, and verify that the result is a
    // properly serialized statement
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    final AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    final List<Bson> triplePipeline = pipelineNode.getTriplePipeline(System.currentTimeMillis(), false);
    final SimpleMongoDBStorageStrategy strategy = new SimpleMongoDBStorageStrategy();
    final List<Statement> results = new LinkedList<>();
    for (final Document doc : getRyaCollection().aggregate(triplePipeline)) {
        final RyaStatement rstmt = strategy.deserializeDocument(doc);
        final Statement stmt = RyaToRdfConversions.convertStatement(rstmt);
        results.add(stmt);
    }
    Assert.assertEquals(2, results.size());
    Assert.assertTrue(results.contains(VF.createStatement(alice, knows, bob)));
    Assert.assertTrue(results.contains(VF.createStatement(alice, year, yearLiteral)));
}