Search in sources :

Example 1 with LeftJoin

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

the class OptionalJoinSegmentTest method testBasicOptional.

@Test
public void testBasicOptional() 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();
    Join join = (Join) ((Projection) te1).getArg();
    LeftJoin lj = (LeftJoin) ((Projection) te2).getArg();
    QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join);
    QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(lj);
    Assert.assertEquals(true, seg1.containsQuerySegment(seg2));
    Assert.assertEquals(join, seg1.getQuery().getTupleExpr());
    SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection) te2);
    List<QueryModelNode> nodes = seg1.getOrderedNodes();
    QueryModelNode node = nodes.get(0);
    seg1.replaceWithExternalSet(seg2, pcj);
    Set<QueryModelNode> nodeSet = new HashSet<>();
    nodeSet.add(node);
    nodeSet.add(pcj);
    Assert.assertEquals(nodeSet, seg1.getUnOrderedNodes());
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) 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) LeftJoin(org.openrdf.query.algebra.LeftJoin) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with LeftJoin

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

the class PCJNodeConsolidatorTest method testAlreadyInOrder.

@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);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) LeftJoin(org.openrdf.query.algebra.LeftJoin) 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 3 with LeftJoin

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

the class FilterFunctionOptimizer method buildQuery.

private void buildQuery(final TupleExpr tupleExpr, final StatementPattern matchStatement) {
    // 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));
}
Also used : LeftJoin(org.openrdf.query.algebra.LeftJoin) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join)

Example 4 with LeftJoin

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

the class GeoEnabledFilterFunctionOptimizer method buildQuery.

private void buildQuery(final TupleExpr tupleExpr, final StatementPattern matchStatement) {
    // 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));
}
Also used : LeftJoin(org.openrdf.query.algebra.LeftJoin) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join)

Example 5 with LeftJoin

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

the class FlattenedOptional method getVarCounts.

/**
 * 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());
    }
}
Also used : LeftJoin(org.openrdf.query.algebra.LeftJoin) Filter(org.openrdf.query.algebra.Filter) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join)

Aggregations

LeftJoin (org.openrdf.query.algebra.LeftJoin)12 Join (org.openrdf.query.algebra.Join)10 TupleExpr (org.openrdf.query.algebra.TupleExpr)6 ParsedQuery (org.openrdf.query.parser.ParsedQuery)6 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)6 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)5 Test (org.junit.Test)5 QueryNodeConsolidator (org.apache.rya.indexing.external.matching.QueryNodeConsolidator)4 Filter (org.openrdf.query.algebra.Filter)4 QueryModelNode (org.openrdf.query.algebra.QueryModelNode)4 ArrayList (java.util.ArrayList)3 DoNotExpandSP (org.apache.rya.rdftriplestore.inference.DoNotExpandSP)2 FixedStatementPattern (org.apache.rya.rdftriplestore.utils.FixedStatementPattern)2 HashSet (java.util.HashSet)1 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)1 StateStoreSupplier (org.apache.kafka.streams.processor.StateStoreSupplier)1 TopologyBuilder (org.apache.kafka.streams.processor.TopologyBuilder)1 IterativeJoin (org.apache.rya.api.function.join.IterativeJoin)1 LeftOuterJoin (org.apache.rya.api.function.join.LeftOuterJoin)1 NaturalJoin (org.apache.rya.api.function.join.NaturalJoin)1