Search in sources :

Example 51 with ExternalTupleSet

use of org.apache.rya.indexing.external.tupleSet.ExternalTupleSet in project incubator-rya by apache.

the class GeneralizedExternalProcessor method process.

/**
 * Iterates through list of normalized indexes and replaces all subtrees of query which match index with index.
 *
 * @param query
 * @return TupleExpr
 */
public static TupleExpr process(TupleExpr query, List<ExternalTupleSet> indexSet) {
    boolean indexPlaced = false;
    TupleExpr rtn = query.clone();
    QueryNodeCount qnc = new QueryNodeCount();
    rtn.visit(qnc);
    if (qnc.getNodeCount() / 2 < indexSet.size()) {
        return null;
    }
    // move BindingSetAssignment Nodes out of the way
    organizeBSAs(rtn);
    // test whether query contains duplicate StatementPatterns and filters
    if (isTupleValid(rtn)) {
        for (ExternalTupleSet index : indexSet) {
            // StatementPattern
            if (isTupleValid(index.getTupleExpr())) {
                ExternalTupleSet eTup = (ExternalTupleSet) index.clone();
                SPBubbleDownVisitor indexVistor = new SPBubbleDownVisitor(eTup);
                rtn.visit(indexVistor);
                FilterBubbleManager fbmv = new FilterBubbleManager(eTup);
                rtn.visit(fbmv);
                SubsetEqualsVisitor subIndexVis = new SubsetEqualsVisitor(eTup, rtn);
                rtn.visit(subIndexVis);
                indexPlaced = subIndexVis.indexPlaced();
                if (!indexPlaced) {
                    break;
                }
            }
        }
        if (indexPlaced) {
            return rtn;
        } else {
            return null;
        }
    } else {
        throw new IllegalArgumentException("Invalid Query.");
    }
}
Also used : TupleExpr(org.openrdf.query.algebra.TupleExpr) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)

Example 52 with ExternalTupleSet

use of org.apache.rya.indexing.external.tupleSet.ExternalTupleSet in project incubator-rya by apache.

the class IndexedExecutionPlanGenerator method getNormalizedIndices.

private List<ExternalTupleSet> getNormalizedIndices(List<ExternalTupleSet> indexSet) {
    ExternalTupleSet tempIndex;
    final List<ExternalTupleSet> normalizedIndexSet = Lists.newArrayList();
    for (final ExternalTupleSet e : indexSet) {
        List<TupleExpr> tupList = null;
        try {
            tupList = QueryVariableNormalizer.getNormalizedIndex(query, e.getTupleExpr());
        } catch (final Exception e1) {
            e1.printStackTrace();
            throw new Error(e1);
        }
        for (final TupleExpr te : tupList) {
            tempIndex = (ExternalTupleSet) e.clone();
            tempIndex.setProjectionExpr((Projection) te);
            normalizedIndexSet.add(tempIndex);
        }
    }
    return normalizedIndexSet;
}
Also used : TupleExpr(org.openrdf.query.algebra.TupleExpr) NoSuchElementException(java.util.NoSuchElementException) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)

Example 53 with ExternalTupleSet

use of org.apache.rya.indexing.external.tupleSet.ExternalTupleSet in project incubator-rya by apache.

the class ValidIndexCombinationGenerator method getValidIndexCombos.

public Iterator<List<ExternalTupleSet>> getValidIndexCombos(List<ExternalTupleSet> indexSet) {
    Collections.shuffle(indexSet);
    final List<ExternalTupleSet> list = indexSet;
    final Iterator<List<Integer>> iter = getValidCombos(list);
    return new Iterator<List<ExternalTupleSet>>() {

        private List<ExternalTupleSet> next = null;

        private List<Integer> nextCombo = null;

        private boolean hasNextCalled = false;

        private boolean isEmpty = false;

        @Override
        public boolean hasNext() {
            if (!hasNextCalled && !isEmpty) {
                if (!iter.hasNext()) {
                    isEmpty = true;
                    return false;
                } else {
                    nextCombo = iter.next();
                    List<ExternalTupleSet> indexCombo = Lists.newArrayList();
                    for (Integer i : nextCombo) {
                        indexCombo.add(list.get(i));
                    }
                    next = indexCombo;
                    hasNextCalled = true;
                    return true;
                }
            } else if (isEmpty) {
                return false;
            } else {
                return true;
            }
        }

        @Override
        public List<ExternalTupleSet> next() {
            if (hasNextCalled) {
                hasNextCalled = false;
                return next;
            } else if (isEmpty) {
                throw new NoSuchElementException();
            } else {
                if (this.hasNext()) {
                    hasNextCalled = false;
                    return next;
                } else {
                    throw new NoSuchElementException();
                }
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Cannot delete from iterator!");
        }
    };
}
Also used : Iterator(java.util.Iterator) List(java.util.List) NoSuchElementException(java.util.NoSuchElementException) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)

Example 54 with ExternalTupleSet

use of org.apache.rya.indexing.external.tupleSet.ExternalTupleSet 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 ExternalTupleSet

use of org.apache.rya.indexing.external.tupleSet.ExternalTupleSet 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

ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)107 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)99 Test (org.junit.Test)97 ParsedQuery (org.openrdf.query.parser.ParsedQuery)97 TupleExpr (org.openrdf.query.algebra.TupleExpr)92 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)86 ArrayList (java.util.ArrayList)73 QueryModelNode (org.openrdf.query.algebra.QueryModelNode)56 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)46 Projection (org.openrdf.query.algebra.Projection)27 StatementPattern (org.openrdf.query.algebra.StatementPattern)23 HashSet (java.util.HashSet)19 Join (org.openrdf.query.algebra.Join)14 LeftJoin (org.openrdf.query.algebra.LeftJoin)14 AccumuloIndexSetProvider (org.apache.rya.indexing.pcj.matching.provider.AccumuloIndexSetProvider)12 Configuration (org.apache.hadoop.conf.Configuration)10 QueryNodeConsolidator (org.apache.rya.indexing.external.matching.QueryNodeConsolidator)10 List (java.util.List)9 MalformedQueryException (org.openrdf.query.MalformedQueryException)7 Filter (org.openrdf.query.algebra.Filter)6