Search in sources :

Example 51 with QueryModelNode

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

the class GeneralizedExternalProcessor method getQNodes.

private static Set<QueryModelNode> getQNodes(String node, QueryModelNode queryNode) {
    if (node.equals("sp")) {
        Set<QueryModelNode> eSet = new HashSet<QueryModelNode>();
        StatementPatternCollector spc = new StatementPatternCollector();
        queryNode.visit(spc);
        List<StatementPattern> spList = spc.getStatementPatterns();
        eSet.addAll(spList);
        // returns empty set if list contains duplicate StatementPatterns
        if (spList.size() > eSet.size()) {
            return Sets.newHashSet();
        } else {
            return eSet;
        }
    } else if (node.equals("filter")) {
        FilterCollector fvis = new FilterCollector();
        queryNode.visit(fvis);
        return Sets.newHashSet(fvis.getFilters());
    } else {
        throw new IllegalArgumentException("Invalid node type.");
    }
}
Also used : StatementPatternCollector(org.openrdf.query.algebra.helpers.StatementPatternCollector) StatementPattern(org.openrdf.query.algebra.StatementPattern) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) HashSet(java.util.HashSet)

Example 52 with QueryModelNode

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

the class AccumuloSelectivityEvalDAO method getJoinSelect.

public double getJoinSelect(RdfCloudTripleStoreConfiguration conf, TupleExpr te1, TupleExpr te2) throws TableNotFoundException {
    SpExternalCollector spe = new SpExternalCollector();
    te2.visit(spe);
    List<QueryModelNode> espList = spe.getSpExtTup();
    double min = Double.MAX_VALUE;
    for (QueryModelNode node : espList) {
        double select = getSelectivity(conf, te1, node);
        if (min > select) {
            min = select;
        }
    }
    return min;
}
Also used : QueryModelNode(org.openrdf.query.algebra.QueryModelNode)

Example 53 with QueryModelNode

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

the class AccumuloSelectivityEvalDAO method getSpJoinSelect.

// TODO currently computes average selectivity of sp1 with each node in TupleExpr te (is this best?)
private double getSpJoinSelect(RdfCloudTripleStoreConfiguration conf, TupleExpr te, StatementPattern sp1) throws TableNotFoundException {
    if (te instanceof StatementPattern) {
        return getJoinSelect(conf, (StatementPattern) te, sp1);
    } else {
        SpExternalCollector spe = new SpExternalCollector();
        te.visit(spe);
        List<QueryModelNode> espList = spe.getSpExtTup();
        if (espList.size() == 0) {
            Set<String> tupBn = te.getAssuredBindingNames();
            Set<String> eBn = sp1.getAssuredBindingNames();
            Set<String> intersect = Sets.intersection(tupBn, eBn);
            return Math.pow(1.0 / 10000.0, intersect.size());
        }
        double min = Double.MAX_VALUE;
        double select = Double.MAX_VALUE;
        for (QueryModelNode node : espList) {
            if (node instanceof StatementPattern)
                select = getJoinSelect(conf, sp1, (StatementPattern) node);
            else if (node instanceof ExternalSet) {
                select = getExtJoinSelect(sp1, (ExternalSet) node);
            }
            if (min > select) {
                min = select;
            }
        }
        // System.out.println("Max is " + max);
        return min;
    }
}
Also used : StatementPattern(org.openrdf.query.algebra.StatementPattern) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) ExternalSet(org.openrdf.query.algebra.evaluation.impl.ExternalSet)

Example 54 with QueryModelNode

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

the class PCJOptionalTestIT method testSimpleOptionalTest2.

@Test
public void testSimpleOptionalTest2() throws Exception {
    final String query = // 
    "" + // 
    "SELECT ?u ?s ?t " + // 
    "{" + // 
    "  ?s a ?t ." + // 
    "  OPTIONAL{?t <http://www.w3.org/2000/01/rdf-schema#label> ?u } ." + // 
    "  ?u <uri:talksTo> ?s . " + // 
    "  ?s a ?u ." + // 
    "}";
    final String pcj = // 
    "" + // 
    "SELECT ?d ?b ?c " + // 
    "{" + // 
    "  ?b a ?c ." + // 
    "  OPTIONAL{?c <http://www.w3.org/2000/01/rdf-schema#label> ?d } ." + // 
    "  ?d <uri:talksTo> ?b . " + // 
    "}";
    final String relabel_pcj = // 
    "" + // 
    "SELECT ?u ?s ?t " + // 
    "{" + // 
    "  ?s a ?t ." + // 
    "  OPTIONAL{?t <http://www.w3.org/2000/01/rdf-schema#label> ?u } ." + // 
    "  ?u <uri:talksTo> ?s . " + // 
    "}";
    final SPARQLParser parser = new SPARQLParser();
    final ParsedQuery pq1 = parser.parseQuery(query, null);
    final ParsedQuery pq2 = parser.parseQuery(pcj, null);
    final ParsedQuery pq3 = parser.parseQuery(relabel_pcj, null);
    final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
    final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup1);
    final List<QueryModelNode> optTupNodes = Lists.newArrayList();
    optTupNodes.add(extTup2);
    final PCJOptimizer opt = new PCJOptimizer(list, true, new AccumuloIndexSetProvider(new Configuration(), list));
    final TupleExpr te = pq1.getTupleExpr();
    opt.optimize(te, null, null);
    final NodeCollector nc = new NodeCollector();
    te.visit(nc);
    final List<QueryModelNode> qNodes = nc.getNodes();
    Assert.assertEquals(qNodes.size(), optTupNodes.size() + 1);
    for (final QueryModelNode node : optTupNodes) {
        Assert.assertTrue(qNodes.contains(node));
    }
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) Configuration(org.apache.hadoop.conf.Configuration) ParsedQuery(org.openrdf.query.parser.ParsedQuery) AccumuloIndexSetProvider(org.apache.rya.indexing.pcj.matching.provider.AccumuloIndexSetProvider) 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) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) PCJOptimizer(org.apache.rya.indexing.pcj.matching.PCJOptimizer) NodeCollector(org.apache.rya.indexing.external.PrecompJoinOptimizerTest.NodeCollector) Test(org.junit.Test)

Example 55 with QueryModelNode

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

the class PrecompJoinOptimizerTest2 method testContextFilter2.

@Test
public void testContextFilter2() throws Exception {
    final SPARQLParser parser1 = new SPARQLParser();
    final SPARQLParser parser2 = new SPARQLParser();
    final String query1 = // 
    "" + // 
    "SELECT ?k ?l ?m ?n " + // 
    "{" + // 
    " GRAPH ?z { " + // 
    " ?l <uri:talksTo> ?n . " + // 
    " ?l a ?n." + // 
    " ?k a ?m." + // 
    "  FILTER ((?k < ?l) && (?m < ?n)). " + // 
    "		}" + // 
    "}";
    final String query2 = // 
    "" + // 
    "SELECT ?s ?t " + // 
    "{" + // 
    " GRAPH ?r { " + // 
    " ?s <uri:talksTo> ?t . " + // 
    " ?s a ?t." + // 
    "	}" + // 
    "}";
    final ParsedQuery pq1 = parser1.parseQuery(query1, null);
    final ParsedQuery pq2 = parser2.parseQuery(query2, null);
    final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup1);
    final TupleExpr tup = pq1.getTupleExpr().clone();
    provider.setIndices(list);
    final PCJOptimizer pcj = new PCJOptimizer(list, false, provider);
    pcj.optimize(tup, null, null);
    final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
    final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil.getTupleSets(tup);
    final Set<StatementPattern> set = Sets.newHashSet();
    for (final QueryModelNode s : eTupSet) {
        set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s).getTupleExpr()));
    }
    Assert.assertTrue(qSet.containsAll(set) && eTupSet.size() == 1);
}
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