use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.
the class QueryNodeConsolidator method updateNodeList.
// assumes input data fall within capacity of list
private void updateNodeList(PositionNode node, int finalPos, List<QueryModelNode> list) {
int initialPos = node.getPosition();
QueryModelNode qNode = list.remove(initialPos);
if (finalPos < list.size()) {
list.add(finalPos, qNode);
} else {
list.add(qNode);
}
}
use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.
the class QueryNodeConsolidator method getBounds.
/**
* @param node
* @param finalPos
* @param list
* @param leftJoinNodes
* @return PositionNode - if node cannot be move to final position, this
* method returns a non-empty PositionNode representing a bound to
* the move. If it can, it returns an empty PositionNode.
*/
// determine if given node can be moved to finalPos
// assumes node.position and finalPos fall within index range of list
private PositionNode getBounds(PositionNode node, int finalPos, List<QueryModelNode> list, TreeSet<PositionNode> leftJoinNodes) {
// automatically be bound
if (node.getNode() instanceof ValueExpr) {
return new PositionNode();
}
int diff = finalPos - node.getPosition();
if (diff == 0) {
return new PositionNode();
}
if (node.isOptional) {
FlattenedOptional optional = ((FlattenedOptional) node.getNode()).clone();
if (diff < 0) {
for (int i = node.getPosition() - 1; i > finalPos - 1; i--) {
QueryModelNode tempNode = list.get(i);
if (tempNode instanceof ValueExpr) {
continue;
}
if (!optional.canAddTuple((TupleExpr) tempNode)) {
return new PositionNode(tempNode, i);
}
if (tempNode instanceof FlattenedOptional) {
FlattenedOptional tempOptional = (FlattenedOptional) tempNode;
if (!tempOptional.canRemoveTuple(optional)) {
return new PositionNode(tempNode, i);
}
}
optional.addArg((TupleExpr) tempNode);
}
} else {
for (int i = node.getPosition() + 1; i < finalPos + 1; i++) {
// TODO
// check
// bounds
QueryModelNode tempNode = list.get(i);
if (tempNode instanceof ValueExpr) {
continue;
}
if (!optional.canRemoveTuple((TupleExpr) tempNode)) {
return new PositionNode(tempNode, i);
}
if (tempNode instanceof FlattenedOptional) {
FlattenedOptional tempOptional = (FlattenedOptional) tempNode;
if (!tempOptional.canAddTuple(optional)) {
return new PositionNode(tempNode, i);
}
}
optional.removeArg((TupleExpr) tempNode);
}
}
return new PositionNode();
} else {
TupleExpr te = (TupleExpr) node.getNode();
SortedSet<PositionNode> leftJoins;
if (diff < 0) {
leftJoins = leftJoinNodes.subSet(new PositionNode(finalPos), true, node, false);
for (PositionNode pNode : leftJoins) {
FlattenedOptional optional = (FlattenedOptional) pNode.getNode();
if (!optional.canRemoveTuple(te)) {
return new PositionNode(pNode);
}
}
} else {
leftJoins = leftJoinNodes.subSet(node, false, new PositionNode(finalPos), true);
for (PositionNode pNode : leftJoins) {
FlattenedOptional optional = (FlattenedOptional) pNode.getNode();
if (!optional.canAddTuple(te)) {
return new PositionNode(pNode);
}
}
}
return new PositionNode();
}
}
use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.
the class QueryNodesToTupleExpr method getTupleAndNodes.
/**
* @return - a TupleExprAndNodes object that consists of the the TupleExpr
* representation of the List of QueryModelNodes and the nodes used
* to build the TupleExpr.
*/
public TupleExprAndNodes getTupleAndNodes() {
List<QueryModelNode> nodeCopy = new ArrayList<>();
Set<Filter> setCopy = new HashSet<>();
for (QueryModelNode q : queryNodes) {
nodeCopy.add(q.clone());
}
for (Filter f : filters) {
setCopy.add(f.clone());
}
TupleExpr te = buildQuery(nodeCopy, setCopy);
return new TupleExprAndNodes(te, nodeCopy, setCopy);
}
use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.
the class PrecompJoinOptimizerTest method testSixIndex.
@Test
public void testSixIndex() throws Exception {
final String q1 = //
"" + //
"SELECT ?f ?m ?d ?h ?i " + //
"{" + //
" ?f a ?m ." + //
" ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ." + //
" ?d <uri:talksTo> ?f . " + //
" ?d <uri:hangOutWith> ?f ." + //
" ?f <uri:hangOutWith> ?h ." + //
" ?f <uri:associatesWith> ?i ." + //
" ?i <uri:associatesWith> ?h ." + //
"}";
final String q2 = //
"" + //
"SELECT ?t ?s ?u " + //
"{" + //
" ?s a ?t ." + //
" ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ." + //
" ?u <uri:talksTo> ?s . " + //
"}";
final String q3 = //
"" + //
"SELECT ?s ?t ?u " + //
"{" + //
" ?s <uri:hangOutWith> ?t ." + //
" ?t <uri:hangOutWith> ?u ." + //
"}";
final String q4 = //
"" + //
"SELECT ?s ?t ?u " + //
"{" + //
" ?s <uri:associatesWith> ?t ." + //
" ?t <uri:associatesWith> ?u ." + //
"}";
final String q5 = //
"" + //
"SELECT ?m ?f ?d " + //
"{" + //
" ?f a ?m ." + //
" ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ." + //
" ?d <uri:talksTo> ?f . " + //
"}";
final String q6 = //
"" + //
"SELECT ?d ?f ?h " + //
"{" + //
" ?d <uri:hangOutWith> ?f ." + //
" ?f <uri:hangOutWith> ?h ." + //
"}";
final String q7 = //
"" + //
"SELECT ?f ?i ?h " + //
"{" + //
" ?f <uri:associatesWith> ?i ." + //
" ?i <uri:associatesWith> ?h ." + //
"}";
final SPARQLParser parser = new SPARQLParser();
final ParsedQuery pq1 = parser.parseQuery(q1, null);
final ParsedQuery pq2 = parser.parseQuery(q2, null);
final ParsedQuery pq3 = parser.parseQuery(q3, null);
final ParsedQuery pq4 = parser.parseQuery(q4, null);
final ParsedQuery pq5 = parser.parseQuery(q5, null);
final ParsedQuery pq6 = parser.parseQuery(q6, null);
final ParsedQuery pq7 = parser.parseQuery(q7, null);
final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
final SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
final SimpleExternalTupleSet extTup5 = new SimpleExternalTupleSet((Projection) pq6.getTupleExpr());
final SimpleExternalTupleSet extTup6 = new SimpleExternalTupleSet((Projection) pq7.getTupleExpr());
final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
list.add(extTup2);
list.add(extTup1);
list.add(extTup3);
final List<QueryModelNode> optTupNodes = Lists.newArrayList();
optTupNodes.add(extTup4);
optTupNodes.add(extTup6);
optTupNodes.add(extTup5);
provider.setIndices(list);
final PCJOptimizer pcj = new PCJOptimizer(list, true, provider);
final TupleExpr te = pq1.getTupleExpr();
pcj.optimize(te, null, null);
System.out.println(te);
final NodeCollector nc = new NodeCollector();
te.visit(nc);
Assert.assertEquals(nc.qNodes, Sets.newHashSet(optTupNodes));
}
use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.
the class PrecompJoinOptimizerTest method testThreeIndex2.
@Test
public void testThreeIndex2() throws Exception {
final String q1 = //
"" + //
"SELECT ?f ?m ?d ?e ?l ?c " + //
"{" + //
" ?f a ?m ." + //
" ?c a ?l ." + //
" ?d <uri:talksTo> ?f . " + //
" ?e <uri:talksTo> ?c . " + //
" ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ." + //
" ?l <http://www.w3.org/2000/01/rdf-schema#label> ?e ." + //
" ?m <uri:talksTo> ?e . " + //
"}";
final String q2 = //
"" + //
"SELECT ?u ?s ?t " + //
"{" + //
" ?s a ?t ." + //
" ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ." + //
" ?u <uri:talksTo> ?s . " + //
"}";
final String q3 = //
"" + //
"SELECT ?e ?c ?l " + //
"{" + //
" ?c a ?l ." + //
" ?l <http://www.w3.org/2000/01/rdf-schema#label> ?e ." + //
" ?e <uri:talksTo> ?c . " + //
"}";
final String q4 = //
"" + //
"SELECT ?d ?f ?m " + //
"{" + //
" ?f a ?m ." + //
" ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ." + //
" ?d <uri:talksTo> ?f . " + //
"}";
final SPARQLParser parser = new SPARQLParser();
final ParsedQuery pq1 = parser.parseQuery(q1, null);
final ParsedQuery pq2 = parser.parseQuery(q2, null);
final ParsedQuery pq3 = parser.parseQuery(q3, null);
final ParsedQuery pq4 = parser.parseQuery(q4, null);
final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
list.add(extTup1);
final List<StatementPattern> spList = StatementPatternCollector.process(pq1.getTupleExpr());
final List<QueryModelNode> optTupNodes = Lists.newArrayList();
optTupNodes.add(extTup3);
optTupNodes.add(spList.get(6));
optTupNodes.add(extTup2);
provider.setIndices(list);
final PCJOptimizer pcj = new PCJOptimizer(list, true, provider);
final TupleExpr te = pq1.getTupleExpr();
pcj.optimize(te, null, null);
final NodeCollector nc = new NodeCollector();
te.visit(nc);
Assert.assertEquals(nc.qNodes, Sets.newHashSet(optTupNodes));
}
Aggregations