Search in sources :

Example 16 with Projection

use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.

the class PrecompJoinOptimizerVarToConstTest method testFreeTestIndexFunction.

@Test
public void testFreeTestIndexFunction() throws Exception {
    final SPARQLParser parser1 = new SPARQLParser();
    final SPARQLParser parser2 = new SPARQLParser();
    final ParsedQuery pq1 = parser1.parseQuery(q22, null);
    final ParsedQuery pq2 = parser2.parseQuery(q24, null);
    System.out.println("Query is " + pq1.getTupleExpr());
    System.out.println("Index is " + pq2.getTupleExpr());
    final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup);
    final TupleExpr tup = pq1.getTupleExpr().clone();
    final PCJOptimizer pcj = new PCJOptimizer(list, false, new AccumuloIndexSetProvider(new Configuration(), list));
    pcj.optimize(tup, null, null);
    System.out.println("Processed query is " + tup);
    final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
    final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil.getTupleSets(tup);
    final Set<StatementPattern> set = Sets.newHashSet();
    Assert.assertEquals(2, eTupSet.size());
    for (final QueryModelNode s : eTupSet) {
        final Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s).getTupleExpr()));
        set.addAll(tempSet);
    }
    Assert.assertTrue(qSet.containsAll(set));
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) Configuration(org.apache.hadoop.conf.Configuration) ParsedQuery(org.openrdf.query.parser.ParsedQuery) AccumuloIndexSetProvider(org.apache.rya.indexing.pcj.matching.provider.AccumuloIndexSetProvider) ArrayList(java.util.ArrayList) Projection(org.openrdf.query.algebra.Projection) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) StatementPattern(org.openrdf.query.algebra.StatementPattern) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) PCJOptimizer(org.apache.rya.indexing.pcj.matching.PCJOptimizer) Test(org.junit.Test)

Example 17 with Projection

use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.

the class PrecompJoinOptimizerVarToConstTest method testContextFilterFourIndex.

@Test
public void testContextFilterFourIndex() throws Exception {
    final SPARQLParser parser1 = new SPARQLParser();
    final SPARQLParser parser3 = new SPARQLParser();
    final SPARQLParser parser4 = new SPARQLParser();
    final ParsedQuery pq1 = parser1.parseQuery(q19, null);
    final ParsedQuery pq3 = parser3.parseQuery(q17, null);
    final ParsedQuery pq4 = parser4.parseQuery(q18, null);
    System.out.println("Query is " + pq1.getTupleExpr());
    System.out.println("Indexes are " + pq3.getTupleExpr() + " , " + pq4.getTupleExpr());
    final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
    final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(new Projection(pq4.getTupleExpr()));
    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup3);
    list.add(extTup2);
    final TupleExpr tup = pq1.getTupleExpr().clone();
    final PCJOptimizer pcj = new PCJOptimizer(list, false, new AccumuloIndexSetProvider(new Configuration(), list));
    pcj.optimize(tup, null, null);
    System.out.println("Processed query is " + tup);
    final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
    final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil.getTupleSets(tup);
    Assert.assertEquals(2, eTupSet.size());
    final Set<StatementPattern> set = Sets.newHashSet();
    for (final QueryModelNode s : eTupSet) {
        final Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s).getTupleExpr()));
        set.addAll(tempSet);
    }
    Assert.assertTrue(qSet.containsAll(set));
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) Configuration(org.apache.hadoop.conf.Configuration) ParsedQuery(org.openrdf.query.parser.ParsedQuery) AccumuloIndexSetProvider(org.apache.rya.indexing.pcj.matching.provider.AccumuloIndexSetProvider) ArrayList(java.util.ArrayList) Projection(org.openrdf.query.algebra.Projection) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) StatementPattern(org.openrdf.query.algebra.StatementPattern) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) PCJOptimizer(org.apache.rya.indexing.pcj.matching.PCJOptimizer) Test(org.junit.Test)

Example 18 with Projection

use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.

the class JoinSegmentPCJMatcherTest method testBasicSegment.

@Test
public void testBasicSegment() throws MalformedQueryException {
    String query1 = // 
    "" + // 
    "SELECT ?e ?c ?l" + // 
    "{" + // 
    "  ?e a ?c . " + // 
    "  ?e <uri:talksTo> ?l  . " + // 
    "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l " + // 
    "}";
    String query2 = // 
    "" + // 
    "SELECT ?e ?c ?l" + // 
    "{" + // 
    "  ?e a ?c . " + // 
    "  ?e <uri:talksTo> ?l  . " + // 
    "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = parser.parseQuery(query1, null);
    ParsedQuery pq2 = parser.parseQuery(query2, null);
    TupleExpr te1 = pq1.getTupleExpr();
    TupleExpr te2 = pq2.getTupleExpr();
    Projection proj = (Projection) te1;
    Join join = (Join) proj.getArg();
    ExternalSetMatcher<ExternalTupleSet> jsm = pcjFactory.getMatcher(qFactory.getQuerySegment(join));
    SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection) te2);
    Assert.assertEquals(true, jsm.match(pcj));
    TupleExpr te = jsm.getQuery();
    Assert.assertEquals(new HashSet<QueryModelNode>(), jsm.getUnmatchedArgNodes());
    Set<QueryModelNode> qNodes = QueryNodeGatherer.getNodes(te);
    List<QueryModelNode> nodes = jsm.getOrderedNodes();
    Set<QueryModelNode> nodeSet = new HashSet<>();
    nodeSet.add(nodes.get(0));
    nodeSet.add(pcj);
    Assert.assertEquals(nodeSet, new HashSet<QueryModelNode>(nodes));
    Assert.assertEquals(nodeSet, qNodes);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Projection(org.openrdf.query.algebra.Projection) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with Projection

use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.

the class OptionalJoinSegmentPCJMatcherTest method testWithUnmatchedNodes.

@Test
public void testWithUnmatchedNodes() throws Exception {
    String query1 = // 
    "" + // 
    "SELECT ?a ?b ?c ?d ?e" + // 
    "{" + // 
    " Filter(?a = <uri:s1>)" + // 
    " Filter(?b = <uri:s2>)" + " {?e <uri:p3> <uri:o2> } UNION {?d <uri:p4> <uri:o3>} ." + " ?a <uri:workAt> <uri:Company1> ." + // 
    "  ?b <uri:talksTo> ?c  . " + // 
    " OPTIONAL { ?b <uri:p1> ?c } . " + // 
    "  ?b <uri:p2> <uri:o1> . " + // 
    "}";
    String query2 = // 
    "" + // 
    "SELECT ?e ?c ?l" + // 
    "{" + // 
    "  ?b <uri:talksTo> ?c  . " + // 
    " OPTIONAL { ?b <uri:p1> ?c } . " + // 
    "  ?b <uri:p2> <uri:o1> . " + // 
    " Filter(?b = <uri:s2>)" + // 
    "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = parser.parseQuery(query1, null);
    ParsedQuery pq2 = parser.parseQuery(query2, null);
    TupleExpr te1 = pq1.getTupleExpr();
    TupleExpr te2 = pq2.getTupleExpr();
    Projection proj = (Projection) te1;
    Join join = (Join) proj.getArg();
    ExternalSetMatcher<ExternalTupleSet> jsm = pcjFactory.getMatcher(qFactory.getQuerySegment(join));
    SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection) te2);
    Assert.assertEquals(true, jsm.match(pcj));
    TupleExpr te = jsm.getQuery();
    Set<QueryModelNode> qNodes = LeftJoinQueryNodeGatherer.getNodes(te);
    List<QueryModelNode> nodes = jsm.getOrderedNodes();
    Assert.assertEquals(1, jsm.getUnmatchedArgNodes().size());
    Assert.assertEquals(true, jsm.getUnmatchedArgNodes().contains(nodes.get(3)));
    Set<QueryModelNode> nodeSet = new HashSet<>();
    nodeSet.add(nodes.get(0));
    nodeSet.add(nodes.get(2));
    nodeSet.add(nodes.get(3));
    nodeSet.add(pcj);
    Assert.assertEquals(nodeSet, new HashSet<QueryModelNode>(nodes));
    Assert.assertEquals(nodeSet, qNodes);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Projection(org.openrdf.query.algebra.Projection) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with Projection

use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.

the class OptionalJoinSegmentPCJMatcherTest method testMultipleFilters.

@Test
public void testMultipleFilters() throws Exception {
    String query1 = // 
    "" + // 
    "SELECT ?e ?c ?l" + // 
    "{" + // 
    " Filter(?e = <uri:Bob>)" + // 
    " Filter(?c = <uri:Lawyer>)" + " ?l <uri:workAt> <uri:Company1> ." + // 
    " OPTIONAL { ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l } . " + // 
    "  ?e a ?c . " + // 
    "  ?e <uri:talksTo> ?l  . " + // 
    "}";
    String query2 = // 
    "" + // 
    "SELECT ?e ?c ?l" + // 
    "{" + // 
    " Filter(?e = <uri:Bob>)" + // 
    " ?e a ?c . " + // 
    " ?e <uri:talksTo> ?l . " + // 
    "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = parser.parseQuery(query1, null);
    ParsedQuery pq2 = parser.parseQuery(query2, null);
    TupleExpr te1 = pq1.getTupleExpr();
    TupleExpr te2 = pq2.getTupleExpr();
    Projection proj = (Projection) te1;
    Join join = (Join) proj.getArg();
    ExternalSetMatcher<ExternalTupleSet> jsm = pcjFactory.getMatcher(qFactory.getQuerySegment(join));
    SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection) te2);
    Assert.assertEquals(true, jsm.match(pcj));
    TupleExpr te = jsm.getQuery();
    Assert.assertEquals(new HashSet<QueryModelNode>(), jsm.getUnmatchedArgNodes());
    Set<QueryModelNode> qNodes = LeftJoinQueryNodeGatherer.getNodes(te);
    List<QueryModelNode> nodes = jsm.getOrderedNodes();
    Set<QueryModelNode> nodeSet = new HashSet<>();
    nodeSet.add(nodes.get(0));
    nodeSet.add(nodes.get(2));
    nodeSet.add(nodes.get(3));
    nodeSet.add(pcj);
    Assert.assertEquals(nodeSet, new HashSet<QueryModelNode>(nodes));
    Assert.assertEquals(nodeSet, qNodes);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Projection(org.openrdf.query.algebra.Projection) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Projection (org.openrdf.query.algebra.Projection)76 Test (org.junit.Test)64 StatementPattern (org.openrdf.query.algebra.StatementPattern)41 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)37 ParsedQuery (org.openrdf.query.parser.ParsedQuery)34 TupleExpr (org.openrdf.query.algebra.TupleExpr)33 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)27 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)27 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)24 Var (org.openrdf.query.algebra.Var)24 ArrayList (java.util.ArrayList)23 QueryModelNode (org.openrdf.query.algebra.QueryModelNode)23 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)22 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)17 Join (org.openrdf.query.algebra.Join)15 HashSet (java.util.HashSet)14 Union (org.openrdf.query.algebra.Union)10 Resource (org.openrdf.model.Resource)9 MultiProjection (org.openrdf.query.algebra.MultiProjection)9 HashMap (java.util.HashMap)8