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;
}
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;
}
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;
}
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;
}
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());
}
Aggregations