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