Search in sources :

Example 6 with LeftJoin

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

the class TopologyFactory method build.

@Override
public TopologyBuilder build(final String sparqlQuery, final String statementsTopic, final String resultsTopic, final BNodeIdFactory bNodeIdFactory) throws MalformedQueryException, TopologyBuilderException {
    requireNonNull(sparqlQuery);
    requireNonNull(statementsTopic);
    requireNonNull(resultsTopic);
    final ParsedQuery parsedQuery = new SPARQLParser().parseQuery(sparqlQuery, null);
    final TopologyBuilder builder = new TopologyBuilder();
    final TupleExpr expr = parsedQuery.getTupleExpr();
    final QueryVisitor visitor = new QueryVisitor(bNodeIdFactory);
    expr.visit(visitor);
    processorEntryList = visitor.getProcessorEntryList();
    final Map<TupleExpr, String> idMap = visitor.getIDs();
    // add source node
    builder.addSource(SOURCE, new StringDeserializer(), new VisibilityStatementDeserializer(), statementsTopic);
    // processing the processor entry list in reverse order means we go from leaf
    // nodes -> parent nodes.
    // So, when the parent processing nodes get added, the upstream
    // processing node will already exist.
    ProcessorEntry entry = null;
    for (int ii = processorEntryList.size() - 1; ii >= 0; ii--) {
        entry = processorEntryList.get(ii);
        // statement patterns need to be connected to the Source.
        if (entry.getNode() instanceof StatementPattern) {
            builder.addProcessor(entry.getID(), entry.getSupplier(), SOURCE);
        } else {
            final List<TupleExpr> parents = entry.getUpstreamNodes();
            final String[] parentIDs = new String[parents.size()];
            for (int id = 0; id < parents.size(); id++) {
                parentIDs[id] = idMap.get(parents.get(id));
            }
            builder.addProcessor(entry.getID(), entry.getSupplier(), parentIDs);
        }
        // Add a state store for any node type that requires one.
        if (entry.getNode() instanceof Join || entry.getNode() instanceof LeftJoin || entry.getNode() instanceof Group) {
            // Add a state store for the join processor.
            final StateStoreSupplier joinStoreSupplier = Stores.create(entry.getID()).withStringKeys().withValues(new VisibilityBindingSetSerde()).persistent().build();
            builder.addStateStore(joinStoreSupplier, entry.getID());
        }
    }
    // Add a formatter that converts the ProcessorResults into the output format.
    final SinkEntry<?, ?> sinkEntry = visitor.getSinkEntry();
    builder.addProcessor("OUTPUT_FORMATTER", sinkEntry.getFormatterSupplier(), entry.getID());
    // Add the sink.
    builder.addSink(SINK, resultsTopic, sinkEntry.getKeySerializer(), sinkEntry.getValueSerializer(), "OUTPUT_FORMATTER");
    return builder;
}
Also used : Group(org.openrdf.query.algebra.Group) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) TopologyBuilder(org.apache.kafka.streams.processor.TopologyBuilder) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) LeftJoin(org.openrdf.query.algebra.LeftJoin) LeftOuterJoin(org.apache.rya.api.function.join.LeftOuterJoin) Join(org.openrdf.query.algebra.Join) IterativeJoin(org.apache.rya.api.function.join.IterativeJoin) NaturalJoin(org.apache.rya.api.function.join.NaturalJoin) VisibilityBindingSetSerde(org.apache.rya.streams.kafka.serialization.VisibilityBindingSetSerde) VisibilityStatementDeserializer(org.apache.rya.streams.kafka.serialization.VisibilityStatementDeserializer) TupleExpr(org.openrdf.query.algebra.TupleExpr) StatementPattern(org.openrdf.query.algebra.StatementPattern) LeftJoin(org.openrdf.query.algebra.LeftJoin) StateStoreSupplier(org.apache.kafka.streams.processor.StateStoreSupplier)

Example 7 with LeftJoin

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

the class FlattenedOptionalTest method getJoinArgs.

private List<TupleExpr> getJoinArgs(TupleExpr tupleExpr, List<TupleExpr> joinArgs) {
    if (tupleExpr instanceof Projection) {
        Projection projection = (Projection) tupleExpr;
        getJoinArgs(projection.getArg(), joinArgs);
    } else if (tupleExpr instanceof Join) {
        Join join = (Join) tupleExpr;
        getJoinArgs(join.getLeftArg(), joinArgs);
        getJoinArgs(join.getRightArg(), joinArgs);
    } else if (tupleExpr instanceof LeftJoin) {
        LeftJoin lj = (LeftJoin) tupleExpr;
        joinArgs.add(new FlattenedOptional(lj));
        getJoinArgs(lj.getLeftArg(), joinArgs);
    } else if (tupleExpr instanceof Filter) {
        getJoinArgs(((Filter) tupleExpr).getArg(), joinArgs);
    } else {
        joinArgs.add(tupleExpr);
    }
    return joinArgs;
}
Also used : FlattenedOptional(org.apache.rya.indexing.external.matching.FlattenedOptional) LeftJoin(org.openrdf.query.algebra.LeftJoin) Filter(org.openrdf.query.algebra.Filter) Projection(org.openrdf.query.algebra.Projection) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join)

Example 8 with LeftJoin

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

the class FlattenedOptional method getJoinArgs.

/**
 * This method is used to retrieve a set view of all descendants of the
 * rightArg of the LeftJoin (the optional part)
 *
 * @param tupleExpr
 *            - tupleExpr whose args are being retrieved
 * @param joinArgs
 *            - set view of all non-join args that are descendants of
 *            tupleExpr
 * @return joinArgs
 */
private Set<TupleExpr> getJoinArgs(TupleExpr tupleExpr, Set<TupleExpr> joinArgs) {
    if (tupleExpr instanceof Join) {
        if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern) && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) {
            Join join = (Join) tupleExpr;
            getJoinArgs(join.getLeftArg(), joinArgs);
            getJoinArgs(join.getRightArg(), joinArgs);
        }
    } else if (tupleExpr instanceof LeftJoin) {
        // TODO probably not
        // necessary if not
        // including leftarg
        LeftJoin lj = (LeftJoin) tupleExpr;
        joinArgs.add(new FlattenedOptional(lj));
        getJoinArgs(lj.getLeftArg(), joinArgs);
    } else if (tupleExpr instanceof Filter) {
        getJoinArgs(((Filter) tupleExpr).getArg(), joinArgs);
    } else {
        joinArgs.add(tupleExpr);
    }
    return joinArgs;
}
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) DoNotExpandSP(org.apache.rya.rdftriplestore.inference.DoNotExpandSP) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern)

Example 9 with LeftJoin

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

the class OptionalJoinSegment method getJoinArgs.

/**
 * @param tupleExpr
 *            - the query object that will be traversed by this method
 * @param joinArgs
 *            - all nodes connected by Joins, LeftJoins, and Filters
 * @return - List containing all nodes connected by Joins, LeftJoins, and
 *         Filters. This List contains the {@link ValueExpr} in place of the
 *         Filter and a {@link FlattenedOptional} in place of the LeftJoin
 *         for ease of comparison with PCJ nodes.
 */
private List<QueryModelNode> getJoinArgs(TupleExpr tupleExpr, List<QueryModelNode> joinArgs) {
    if (tupleExpr instanceof Join) {
        if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern) && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) {
            Join join = (Join) tupleExpr;
            getJoinArgs(join.getRightArg(), joinArgs);
            getJoinArgs(join.getLeftArg(), joinArgs);
        }
    } else if (tupleExpr instanceof LeftJoin) {
        LeftJoin lj = (LeftJoin) tupleExpr;
        joinArgs.add(new FlattenedOptional(lj));
        getJoinArgs(lj.getLeftArg(), joinArgs);
    } else if (tupleExpr instanceof Filter) {
        Filter filter = (Filter) tupleExpr;
        joinArgs.add(filter.getCondition());
        conditionMap.put(filter.getCondition(), filter);
        getJoinArgs(filter.getArg(), joinArgs);
    } else {
        joinArgs.add(tupleExpr);
    }
    return joinArgs;
}
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) DoNotExpandSP(org.apache.rya.rdftriplestore.inference.DoNotExpandSP) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern)

Example 10 with LeftJoin

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

the class PCJNodeConsolidatorTest method testInvalidOrder.

@Test
public void testInvalidOrder() throws Exception {
    String query1 = // 
    "" + // 
    "SELECT ?a ?b ?c ?d" + // 
    "{" + // 
    "  ?a <uri:p5> <uri:const3>" + // 
    "  OPTIONAL{<uri:const2> <uri:p4> ?d } . " + // 
    "  ?a <uri:p4> ?b . " + // 
    "  OPTIONAL{?a <uri:p3> ?c} . " + // 
    "  OPTIONAL{<uri:const1> <uri:p2> ?b} . " + "  ?c <uri:p1> ?d " + // 
    "}";
    String query2 = // 
    "" + // 
    "SELECT ?a ?b ?c ?d" + // 
    "{" + // 
    "  ?a <uri:p4> ?b . " + "  ?c <uri:p1> ?d " + // 
    "  OPTIONAL{<uri:const2> <uri:p4> ?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();
    Join join1 = (Join) ((Projection) te1).getArg();
    LeftJoin join2 = (LeftJoin) ((Projection) te2).getArg();
    QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join1);
    QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(join2);
    QueryNodeConsolidator consolidator = new QueryNodeConsolidator(seg1.getOrderedNodes(), seg2.getOrderedNodes());
    Assert.assertTrue(!consolidator.consolidateNodes());
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) LeftJoin(org.openrdf.query.algebra.LeftJoin) ParsedQuery(org.openrdf.query.parser.ParsedQuery) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join) 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

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