Search in sources :

Example 76 with QueryModelNode

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);
    }
}
Also used : QueryModelNode(org.openrdf.query.algebra.QueryModelNode)

Example 77 with QueryModelNode

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();
    }
}
Also used : ValueExpr(org.openrdf.query.algebra.ValueExpr) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr)

Example 78 with QueryModelNode

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);
}
Also used : Filter(org.openrdf.query.algebra.Filter) ArrayList(java.util.ArrayList) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr) HashSet(java.util.HashSet)

Example 79 with QueryModelNode

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));
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) PCJOptimizer(org.apache.rya.indexing.pcj.matching.PCJOptimizer) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) 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) Test(org.junit.Test)

Example 80 with QueryModelNode

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));
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) 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) StatementPattern(org.openrdf.query.algebra.StatementPattern) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) PCJOptimizer(org.apache.rya.indexing.pcj.matching.PCJOptimizer) Test(org.junit.Test)

Aggregations

QueryModelNode (org.openrdf.query.algebra.QueryModelNode)98 TupleExpr (org.openrdf.query.algebra.TupleExpr)74 Test (org.junit.Test)68 ArrayList (java.util.ArrayList)63 ParsedQuery (org.openrdf.query.parser.ParsedQuery)63 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)62 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)56 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)48 StatementPattern (org.openrdf.query.algebra.StatementPattern)33 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)27 HashSet (java.util.HashSet)26 Projection (org.openrdf.query.algebra.Projection)23 Filter (org.openrdf.query.algebra.Filter)15 LeftJoin (org.openrdf.query.algebra.LeftJoin)12 Join (org.openrdf.query.algebra.Join)11 ValueExpr (org.openrdf.query.algebra.ValueExpr)11 QueryNodeConsolidator (org.apache.rya.indexing.external.matching.QueryNodeConsolidator)8 Configuration (org.apache.hadoop.conf.Configuration)7 AccumuloIndexSetProvider (org.apache.rya.indexing.pcj.matching.provider.AccumuloIndexSetProvider)7 BatchWriter (org.apache.accumulo.core.client.BatchWriter)6