Search in sources :

Example 46 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class SparqlToPigTransformVisitorTest method testMutlipleJoins.

public void testMutlipleJoins() throws Exception {
    String query = "select * where {\n" + "?subj <urn:lubm:rdfts#name> 'Department0'.\n" + "?subj <urn:lubm:rdfts#subOrganizationOf> <http://www.University0.edu>.\n" + "?subj <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <urn:lubm:rdfts#Department>.\n" + "}";
    // System.out.println(query);
    QueryParser parser = new SPARQLParser();
    ParsedQuery parsedQuery = parser.parseQuery(query, null);
    // System.out.println(parsedQuery);
    SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor();
    visitor.setTablePrefix(tablePrefix);
    visitor.setInstance(instance);
    visitor.setZk(zk);
    visitor.setUser(user);
    visitor.setPassword(password);
    visitor.meet(new QueryRoot(parsedQuery.getTupleExpr()));
// System.out.println(visitor.getPigScript());
}
Also used : QueryParser(org.openrdf.query.parser.QueryParser) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) QueryRoot(org.openrdf.query.algebra.QueryRoot) ParsedQuery(org.openrdf.query.parser.ParsedQuery)

Example 47 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class SparqlToPigTransformVisitorTest method testLimit.

public void testLimit() throws Exception {
    String query = "select * where {\n" + "?subj <urn:lubm:rdfts#name> 'Department0'.\n" + "?subj <urn:lubm:rdfts#subOrganizationOf> ?suborg.\n" + "} limit 100";
    // System.out.println(query);
    QueryParser parser = new SPARQLParser();
    ParsedQuery parsedQuery = parser.parseQuery(query, null);
    // System.out.println(parsedQuery);
    SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor();
    visitor.setTablePrefix(tablePrefix);
    visitor.setInstance(instance);
    visitor.setZk(zk);
    visitor.setUser(user);
    visitor.setPassword(password);
    visitor.meet(new QueryRoot(parsedQuery.getTupleExpr()));
// System.out.println(visitor.getPigScript());
}
Also used : QueryParser(org.openrdf.query.parser.QueryParser) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) QueryRoot(org.openrdf.query.algebra.QueryRoot) ParsedQuery(org.openrdf.query.parser.ParsedQuery)

Example 48 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class IndexWritingTool method setVarOrders.

public void setVarOrders(final String s, final Configuration conf) throws MalformedQueryException {
    final SPARQLParser parser = new SPARQLParser();
    final TupleExpr query = parser.parseQuery(s, null).getTupleExpr();
    final List<String> projList = Lists.newArrayList(((Projection) query).getProjectionElemList().getTargetNames());
    final String projElems = Joiner.on(";").join(projList);
    conf.set("projElems", projElems);
    final Pattern splitPattern1 = Pattern.compile("\n");
    final Pattern splitPattern2 = Pattern.compile(",");
    final String[] lines = splitPattern1.split(s);
    final List<String> varOrders = Lists.newArrayList();
    final List<String> varOrderPos = Lists.newArrayList();
    int orderNum = 0;
    final int projSizeSq = projList.size() * projList.size();
    for (String t : lines) {
        if (orderNum > projSizeSq) {
            break;
        }
        String[] order = null;
        if (t.startsWith("#prefix")) {
            t = t.substring(7).trim();
            order = splitPattern2.split(t, projList.size());
        }
        String tempVarOrder = "";
        String tempVarOrderPos = "";
        if (order != null) {
            for (final String u : order) {
                if (tempVarOrder.length() == 0) {
                    tempVarOrder = u.trim();
                } else {
                    tempVarOrder = tempVarOrder + ";" + u.trim();
                }
                final int pos = projList.indexOf(u.trim());
                if (pos < 0) {
                    throw new IllegalArgumentException("Invalid variable order!");
                } else {
                    if (tempVarOrderPos.length() == 0) {
                        tempVarOrderPos = tempVarOrderPos + pos;
                    } else {
                        tempVarOrderPos = tempVarOrderPos + ";" + pos;
                    }
                }
            }
            varOrders.add(tempVarOrder);
            varOrderPos.add(tempVarOrderPos);
        }
        if (tempVarOrder.length() > 0) {
            orderNum++;
        }
    }
    if (orderNum == 0) {
        varOrders.add(projElems);
        String tempVarPos = "";
        for (int i = 0; i < projList.size(); i++) {
            if (i == 0) {
                tempVarPos = Integer.toString(0);
            } else {
                tempVarPos = tempVarPos + ";" + i;
            }
        }
        varOrderPos.add(tempVarPos);
    }
    final String[] vOrders = varOrders.toArray(new String[varOrders.size()]);
    final String[] vOrderPos = varOrderPos.toArray(new String[varOrderPos.size()]);
    conf.setStrings("varOrders", vOrders);
    conf.setStrings("varOrderPos", vOrderPos);
}
Also used : Pattern(java.util.regex.Pattern) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) Projection(org.openrdf.query.algebra.Projection) TupleExpr(org.openrdf.query.algebra.TupleExpr)

Example 49 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class IndexedExecutionPlanGeneratorTest method testThreeSingleNodeIndex.

@Test
public void testThreeSingleNodeIndex() throws Exception {
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = parser.parseQuery(q19, null);
    ParsedQuery pq2 = parser.parseQuery(q20, null);
    SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
    List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup1);
    IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
    List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
    Assert.assertEquals(3, indexSet.size());
    Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
    int size = 0;
    while (processedTups.hasNext()) {
        Assert.assertTrue(processedTups.hasNext());
        processedTups.next();
        size++;
    }
    Assert.assertTrue(!processedTups.hasNext());
    Assert.assertEquals(3, size);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) TupleExpr(org.openrdf.query.algebra.TupleExpr) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) Test(org.junit.Test)

Example 50 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class IndexedExecutionPlanGeneratorTest method testThreeIndexQuery.

@Test
public void testThreeIndexQuery() throws Exception {
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = parser.parseQuery(q16, null);
    ParsedQuery pq2 = parser.parseQuery(q17, null);
    ParsedQuery pq3 = parser.parseQuery(q18, null);
    SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
    SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
    List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup2);
    list.add(extTup1);
    IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
    List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
    Assert.assertEquals(6, indexSet.size());
    Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
    int size = 0;
    while (processedTups.hasNext()) {
        Assert.assertTrue(processedTups.hasNext());
        processedTups.next();
        size++;
    }
    Assert.assertTrue(!processedTups.hasNext());
    Assert.assertEquals(9, size);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) TupleExpr(org.openrdf.query.algebra.TupleExpr) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) Test(org.junit.Test)

Aggregations

SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)265 ParsedQuery (org.openrdf.query.parser.ParsedQuery)249 Test (org.junit.Test)212 TupleExpr (org.openrdf.query.algebra.TupleExpr)162 ArrayList (java.util.ArrayList)100 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)99 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)86 StatementPattern (org.openrdf.query.algebra.StatementPattern)82 QueryModelNode (org.openrdf.query.algebra.QueryModelNode)62 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)46 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)45 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)43 RyaURI (org.apache.rya.api.domain.RyaURI)42 BindingSet (org.openrdf.query.BindingSet)41 RyaStatement (org.apache.rya.api.domain.RyaStatement)39 Projection (org.openrdf.query.algebra.Projection)37 RyaType (org.apache.rya.api.domain.RyaType)35 HashSet (java.util.HashSet)27 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)20 LiteralImpl (org.openrdf.model.impl.LiteralImpl)19