Search in sources :

Example 86 with QueryModelNode

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

the class VarConstQueryVariableNormalizerTest method queryNAryListMatch.

@Test
public void queryNAryListMatch() throws Exception {
    String q1 = // 
    "" + // 
    "SELECT ?a ?b ?c ?d ?e ?f ?q ?g ?h " + // 
    "{" + // 
    " GRAPH ?x { " + // 
    "  ?a a ?b ." + // 
    "  ?b <http://www.w3.org/2000/01/rdf-schema#label> ?c ." + // 
    "  ?d <uri:talksTo> ?e . " + // 
    "  FILTER(bound(?f) && sameTerm(?a,?b)&&bound(?q)). " + // 
    "  FILTER ( ?e < ?f && (?a > ?b || ?c = ?d) ). " + // 
    "  FILTER(?g IN (1,2,3) && ?h NOT IN(5,6,7)). " + // 
    "  ?x <http://www.w3.org/2000/01/rdf-schema#label> ?g. " + // 
    "  ?b a ?q ." + // 
    "     }" + // 
    "}";
    String q2 = // 
    "" + // 
    "SELECT ?m ?n ?r ?y " + // 
    "{" + // 
    " GRAPH ?q { " + // 
    "  FILTER(?m IN (1,?y,3) && ?n NOT IN(?r,6,7)). " + // 
    "  ?q <http://www.w3.org/2000/01/rdf-schema#label> ?m. " + // 
    "     }" + // 
    "}";
    SPARQLParser parser1 = new SPARQLParser();
    SPARQLParser parser2 = new SPARQLParser();
    ParsedQuery pq1 = parser1.parseQuery(q1, null);
    ParsedQuery pq2 = parser2.parseQuery(q2, null);
    List<TupleExpr> normalize = QueryVariableNormalizer.getNormalizedIndex(pq1.getTupleExpr(), pq2.getTupleExpr());
    Assert.assertEquals(1, normalize.size());
    for (TupleExpr te : normalize) {
        Assert.assertTrue(isTupleSubset(pq1.getTupleExpr(), te));
    }
    FilterCollector fc1 = new FilterCollector();
    pq1.getTupleExpr().visit(fc1);
    List<QueryModelNode> fList1 = fc1.getFilters();
    for (TupleExpr te : normalize) {
        FilterCollector fc2 = new FilterCollector();
        te.visit(fc2);
        List<QueryModelNode> fList2 = fc2.getFilters();
        for (QueryModelNode q : fList2) {
            Assert.assertTrue(fList1.contains(q));
        }
    }
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr) Test(org.junit.Test)

Example 87 with QueryModelNode

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

the class OptionalJoinSegmentPCJMatcherTest method testBasicMatchWithFilter.

@Test
public void testBasicMatchWithFilter() throws Exception {
    String query1 = // 
    "" + // 
    "SELECT ?e ?c ?l" + // 
    "{" + // 
    " Filter(?e = <uri:Bob>)" + // 
    " Filter(?c = <uri:Lawyer>)" + // 
    "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " + // 
    "  ?e a ?c . " + // 
    "  OPTIONAL {?e <uri:talksTo> ?l}  . " + // 
    "}";
    String query2 = // 
    "" + // 
    "SELECT ?e ?c ?l" + // 
    "{" + // 
    " Filter(?e = <uri:Bob>)" + // 
    " ?e a ?c . " + // 
    " OPTIONAL {?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;
    Filter filter = (Filter) proj.getArg();
    ExternalSetMatcher<ExternalTupleSet> jsm = pcjFactory.getMatcher(qFactory.getQuerySegment(filter));
    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(pcj);
    nodeSet.add(nodes.get(2));
    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) 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) Filter(org.openrdf.query.algebra.Filter) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 88 with QueryModelNode

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

the class OptionalJoinSegmentPCJMatcherTest method testBasicSegment.

@Test
public void testBasicSegment() throws MalformedQueryException {
    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 " + // 
    "}";
    String query2 = // 
    "" + // 
    "SELECT ?e ?c ?l" + // 
    "{" + // 
    "  ?e a ?c . " + // 
    "  OPTIONAL {?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(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 89 with QueryModelNode

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

the class PCJNodeConsolidatorTest method testSwitchTwoBoundVars.

@Test
public void testSwitchTwoBoundVars() throws Exception {
    String query1 = // 
    "" + // 
    "SELECT ?a ?b ?c " + // 
    "{" + // 
    "  ?a <uri:p0> ?c ." + // 
    "  ?b<uri:p1> ?c ." + // 
    " OPTIONAL{ ?a <uri:p1> ?b } ." + // 
    " ?a <uri:p2> <uri:o2>. " + // 
    " ?b <uri:p3> <uri:o3> " + // 
    "}";
    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 " + // 
    "}";
    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();
    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<>(seg2.getOrderedNodes());
    Assert.assertTrue(consolidator.consolidateNodes());
    Assert.assertEquals(consolidator.getQueryNodes(), queryNodes);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) QueryNodeConsolidator(org.apache.rya.indexing.external.matching.QueryNodeConsolidator) TupleExpr(org.openrdf.query.algebra.TupleExpr) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) Test(org.junit.Test)

Example 90 with QueryModelNode

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

the class PCJNodeConsolidatorTest method testMoveAcrossMultipleLeftJoins.

@Test
public void testMoveAcrossMultipleLeftJoins() throws Exception {
    String query1 = // 
    "" + // 
    "SELECT ?a ?b ?c ?e ?f" + // 
    "{" + // 
    "  ?c <uri:p5> <uri:o2> ." + // 
    "  ?a <uri:p4> <uri:o1> . " + // 
    "  OPTIONAL{?a <uri:p3> ?b} . " + // 
    "  OPTIONAL{<uri:s2> <uri:p2> ?e} . " + // 
    "  OPTIONAL{<uri:s2> <uri:p2> ?f} . " + "  <uri:s1> <uri:p1> ?f " + // 
    "}";
    String query2 = // 
    "" + // 
    "SELECT ?f ?c" + // 
    "{" + // 
    "  ?c <uri:p5> <uri:o2> . " + "  <uri:s1> <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();
    Join join1 = (Join) ((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());
    QueryModelNode node = queryNodes.remove(5);
    queryNodes.add(1, node);
    Assert.assertTrue(consolidator.consolidateNodes());
    Assert.assertEquals(consolidator.getQueryNodes(), queryNodes);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) QueryNodeConsolidator(org.apache.rya.indexing.external.matching.QueryNodeConsolidator) TupleExpr(org.openrdf.query.algebra.TupleExpr) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) Test(org.junit.Test)

Aggregations

QueryModelNode (org.openrdf.query.algebra.QueryModelNode)98 TupleExpr (org.openrdf.query.algebra.TupleExpr)74 Test (org.junit.Test)68 ArrayList (java.util.ArrayList)63 ParsedQuery (org.openrdf.query.parser.ParsedQuery)63 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)62 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)56 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)48 StatementPattern (org.openrdf.query.algebra.StatementPattern)33 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)27 HashSet (java.util.HashSet)26 Projection (org.openrdf.query.algebra.Projection)23 Filter (org.openrdf.query.algebra.Filter)15 LeftJoin (org.openrdf.query.algebra.LeftJoin)12 Join (org.openrdf.query.algebra.Join)11 ValueExpr (org.openrdf.query.algebra.ValueExpr)11 QueryNodeConsolidator (org.apache.rya.indexing.external.matching.QueryNodeConsolidator)8 Configuration (org.apache.hadoop.conf.Configuration)7 AccumuloIndexSetProvider (org.apache.rya.indexing.pcj.matching.provider.AccumuloIndexSetProvider)7 BatchWriter (org.apache.accumulo.core.client.BatchWriter)6